int main(){ blog_t * newBlog = blog_new(0, 1); user_t * newUser = blog_userLogin(newBlog, 200, 0); user_add(newUser, "Fedia", "UK", 18, 1/*, int id*/); user_print(newUser); printf("-------------------------------------------------\n"); blog_delete(newBlog); return 0; }
void user_print_array(size_t size,user_t * user[size]) { for(int i = 0; i<size;i++) { if(user[i]==NULL) { status = USER_ERROR_NULL_PTR; return; } user_print(user[i]); } status = USER_OK; }
/* -1 don't continue, it's done */ int user_message_parse(struct user_list_t *user_list, char *buf, int fromsfd) { /* socket fd to whom the message should be send */ char *str_p; /* did user wrote command? */ if (buf[0]=='!') { /* list users connected to server */ if (strcmp(buf,"!ls\n")==0) { user_print(user_list, fromsfd); } if (strcmp(buf,"!logout\n")==0) { } return -1; } else if ((str_p = strstr(buf, str2))!=NULL) { /* length of nick*/ int length=str_p-buf; char name[MAXNAME+1]; memset(name,0,sizeof(name)); for (int i=0;i<length;i++) { name[i]=buf[i]; } /* name match */ struct user_t *p; if ((p=user_find_name(user_list, name))!=NULL) { return p->sfd; } else /* no name match */ { /* the message is for all users */ if (strcmp(name,"all")==0) { return 0; } else return -2; } } return -2; }
void session_print(Session *s,int indent){ char *space,*space1; GList *it; Resource *r,*cr; char *njid; space=g_strnfill(indent*2,' '); space1=g_strnfill((indent+1)*2,' '); njid=jid_normalized(s->jid,0); g_message(L_("%sSession: %p"),space,s); g_message("%sJID: %s",space,s->jid); g_message(L_("%sUser:"******"%sResources:"),space); cr=session_get_cur_resource(s); for(it=g_list_first(s->resources);it;it=g_list_next(it)){ r=(Resource *)it->data; g_message(L_("%sResource: %p%s"),space1,r,(r==cr)?N_(" (current)"):""); if (njid && r->name) g_message("%sJID: %s/%s",space1,njid,r->name); else g_message("%sJID: %s",space1,s->jid); g_message(L_("%sPriority: %i"),space1,r->priority); g_message(L_("%sAvailable: %i"),space1,r->available); if (r->show) g_message(L_("%sShow: %s"),space1,r->show); if (r->status) g_message(L_("%sStatus: %s"),space1,r->status); } g_message(L_("%sGG session: %p"),space,s->ggs); g_message(L_("%sGSource: %p"),space,s->g_source); g_message(L_("%sStream: %p"),space,s->s); g_message(L_("%sConnected: %i"),space,s->connected); g_message(L_("%sRequest id: %s"),space,s->req_id?s->req_id:"(null)"); g_message(L_("%sRequest query: %s"),space,s->query?xmlnode2str(s->query):"(null)"); g_message(L_("%sWaiting for ping: %i"),space,(int)s->waiting_for_pong); g_free(njid); g_free(space1); g_free(space); }
int main(int argc, char *argv[]) { struct user_list_t user_list={0, NULL,NULL}; fd_set main_fds, read_fds; struct sockaddr_in cl_addr; char buf[MAXBUF]; Init(); #ifdef DEBUG printf("Init() succesful!\n"); #endif FD_ZERO(&main_fds); FD_ZERO(&read_fds); /* add server to main_fds */ FD_SET(server_fd, &main_fds); /* the highest file descriptor */ int nfds = server_fd; while (1) { #ifdef DEBUG printf("In the main loop!\n\n"); #endif read_fds = main_fds; if (select(nfds+1, &read_fds, NULL, NULL, NULL)==-1) { perror("select"); exit(1); } #ifdef DEBUG printf("select() ... OK\n"); #endif /* going throug filedescriptors to read data*/ for (int i=0; i<=nfds; i++) { /* */ if (FD_ISSET(i, &read_fds)) { #ifdef DEBUG printf("connection from ... OK \n"); #endif /* NEW CONNECTION */ if (i==server_fd) { size_t addrlen = sizeof(cl_addr); if ((new_fd = accept(server_fd, (struct sockaddr * )&cl_addr, &addrlen))==-1) { perror("accept()"); } else { FD_SET(new_fd,&main_fds); if (new_fd>nfds) { nfds=new_fd; } if (send(new_fd, q1, strlen(q1)+1, 0) == -1) printf("send() error %d",new_fd); printf("Client from %s on socket %d\n", inet_ntoa(cl_addr.sin_addr), new_fd); user_add(&user_list, &cl_addr, new_fd); user_print(&user_list, 1); } } else /* Handling old Clients */ { int nbytes; memset(buf, 0, sizeof buf); if ((nbytes=recv(i, buf, sizeof buf, 0))<=0) { // Error if (nbytes == 0) { printf("Client from socket %d is gone \n",i); if (user_rem(&user_list, i)==-1) { perror("user_rem"); } } else { perror("recv()..."); } close(i); FD_CLR(i, &main_fds); } else { /* handling data from client */ /* who is writing */ #ifdef DEBUG printf("data from client from socket %d \n",i); #endif struct user_t *p=user_find_sfd(&user_list, i); /* is user authentificated? */ if (user_auth_true(p)==0) { /* Did he send username? */ if ((strlen(buf))<MAXNAME) { /* Make it a username */ if (buf[strlen(buf)-1]=='\n') { buf[strlen(buf)-1]='\0'; memset(p->name,0,sizeof(p->name)); strcat (p->name, buf); p->auth=1; /* Welcome new user */ memset(buf, 0, MAXBUF*(sizeof(char))); sprintf(buf, "Welcome %s!\n",p->name); if (send(i, buf, strlen(buf)+1, 0) == -1) printf("send() error %d",i); } else { printf("Authorization of client on socket %d unsucesfull", i); } } } else { /* Client is authorized and he sended message */ user_message(&user_list, &main_fds, buf, server_fd, i, nfds); } #ifdef DEBUG user_print(&user_list,1); #endif } } } } } }