Exemplo n.º 1
0
void message_handler(int id, char **msg, uint32_t count, Type type, short int *revents) {
	switch (type)
	{
	case TYPE__text:
		if (!clients[id].isLogin) {
			if (strlen(msg[0]) == 0) {
				kick_user(clients[id], recv_msg[LOGIN_INCORRECT]);
				*revents |= POLLHUP;
			} else if (!log_in(id, msg[0])) {
				kick_user(clients[id], recv_msg[LOGIN_EXIST]);
				*revents |= POLLHUP;
			}
			return;
		}
		break;
	case TYPE__list:
		send_client_list(id);
		return;
	case TYPE__dc:
		kick_user(clients[id], "* disconnect");
		*revents |= POLLHUP;
		return;
	default: break;
	}
	for (uint32_t i = 0; i < count; i++) {
		printf("%s: %s\n", clients[id].nickName, msg[i]);
		broadcast(id, msg[i]);
	}
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{
	int sockfd, n, p_pid;
	char buf[MAX_BUF];
	struct sockaddr_in servaddr;
	
	if(argc!=3){
		printf("Usage : %s <IP> <port>\n",argv[0]);
		exit(1);
	}
//socket create
	sockfd = socket(AF_INET, SOCK_STREAM,0);
	if(sockfd ==-1){
		perror("socket() error");
		exit(1);
	}
//socketaddr_in structure
	memset(&servaddr,0,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
	servaddr.sin_addr.s_addr=inet_addr(argv[1]);
	servaddr.sin_port=htons(atoi(argv[2]));
//connect
	if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1){
		perror("connect() error");
		exit(1);
	}


	n = read(sockfd,buf,MAX_BUF); // read meassage from server 
	buf[n] = '\0';
	//whether client IP is possible for connection or not 
	if(!strcmp(buf,"OK")){//if it is possible
		printf("** Client is connected **\n");
		log_in(sockfd);//call log_in fucntion
	}
	else if(!strcmp(buf,"FAIL")){// if it is not possible
		printf("** Connection refused **\n");
		close(sockfd);
		exit(0);// close socket and exit program
	}

	close(sockfd);
	return 0;
}
Exemplo n.º 3
0
/* 
 * Log in and wait for rootlist to be loaded
 */
void launch(void)
{   
    printf("Welcome to spotify_terminal\n");
    printf("Using libspotify %s\n", sp_build_id());
    init();
    
    get_user_info();
    printf("Logging in user: '******'... \n", username);
    usleep(500000);
    log_in();
    while(is_logged_in != TRUE) {
        sp_session_process_events(g_session, &next_timeout);
    }
    printf("Loading playlists... \n");
    usleep(500000);
    sp_session_process_events(g_session, &next_timeout);
    while(playlist_loaded != TRUE) {
        sp_session_process_events(g_session, &next_timeout);
    }
    printf("ready!\n");
    printf("> ");
    fflush(stdout);
}
Exemplo n.º 4
0
int
ac(FILE	*fp)
{
	struct utmp_list *lp, *head = NULL;
	struct utmp usr;
	struct tm *ltm;
	time_t secs = 0, prev = 0;
	int day = -1;

	while (fread((char *)&usr, sizeof(usr), 1, fp) == 1) {
		if (!FirstTime)
			FirstTime = usr.ut_time;
		if (usr.ut_time < prev)
			continue;	/* broken record */
		prev = usr.ut_time;
		if (Flags & AC_D) {
			ltm = localtime(&usr.ut_time);
			if (day >= 0 && day != ltm->tm_yday) {
				day = ltm->tm_yday;
				/*
				 * print yesterday's total
				 */
				secs = usr.ut_time;
				secs -= ltm->tm_sec;
				secs -= 60 * ltm->tm_min;
				secs -= 3600 * ltm->tm_hour;
				show_today(Users, head, secs);
			} else
				day = ltm->tm_yday;
		}
		switch(*usr.ut_line) {
		case '|':
			secs = usr.ut_time;
			break;
		case '{':
			secs -= usr.ut_time;
			/*
			 * adjust time for those logged in
			 */
			for (lp = head; lp != NULL; lp = lp->next)
				lp->usr.ut_time -= secs;
			break;
		case '~':			/* reboot or shutdown */
			head = log_out(head, &usr);
			FirstTime = usr.ut_time; /* shouldn't be needed */
			break;
		default:
			/*
			 * if they came in on a pseudo-tty, then it is only
			 * a login session if the ut_host field is non-empty
			 */
			if (*usr.ut_name) {
				if (strncmp(usr.ut_line, "tty", 3) != 0 ||
				    strchr("pqrstuvwxyzPQRST", usr.ut_line[3]) != NULL ||
				    *usr.ut_host != '\0')
					head = log_in(head, &usr);
			} else
				head = log_out(head, &usr);
			break;
		}
	}
	(void)fclose(fp);
	if (!(Flags & AC_W))
		usr.ut_time = time(NULL);
	(void)strlcpy(usr.ut_line, "~", sizeof usr.ut_line);

	if (Flags & AC_D) {
		ltm = localtime(&usr.ut_time);
		if (day >= 0 && day != ltm->tm_yday) {
			/*
			 * print yesterday's total
			 */
			secs = usr.ut_time;
			secs -= ltm->tm_sec;
			secs -= 60 * ltm->tm_min;
			secs -= 3600 * ltm->tm_hour;
			show_today(Users, head, secs);
		}
	}
	/*
	 * anyone still logged in gets time up to now
	 */
	head = log_out(head, &usr);

	if (Flags & AC_D)
		show_today(Users, head, time(NULL));
	else {
		if (Flags & AC_P)
			show_users(Users);
		show("total", Total);
	}
	return 0;
}