Beispiel #1
0
int
main(int argc, char *argv[])
{
	char line[INPUTLEN];
	char buff[BUFFSIZE];

	char *peersfile;
	struct stat st;
	int rc;

	sigset_t set;

	if (argc < 3) {
		fprintf(stderr, "Usage: %s <peers_file> <self_id>\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	peersfile = argv[1];
	rc = stat(peersfile, &st);
	if (rc == -1) {
		if (errno == ENOENT) {
			fprintf(stderr, "Can't open %s.\n", peersfile);
			exit(EXIT_FAILURE);
		}
		else {
			fprintf(stderr, "Error stating %s: %d\n", peersfile, errno);
			exit(EXIT_FAILURE);
		}
	}
	self_info = NULL;
	load_peers(peersfile, argv[2]);
	if (self_info == NULL) {
		fprintf(stderr, "Can't find id %s in %s.\n", argv[2], peersfile);
		exit(EXIT_FAILURE);
	}

	init_gui();

	main_tid = pthread_self();

	sigemptyset(&set);
	sigaddset(&set, SIGINT);
	pthread_sigmask(SIG_BLOCK, &set, NULL);

	pthread_create(&heartbeat_tid, NULL, heartbeat, NULL);
	pthread_create(&chatserver_tid, NULL, chatserver, NULL);

	create_peers_poller();

	pthread_sigmask(SIG_UNBLOCK, &set, NULL);
	signal(SIGINT, sigint_handler);

	usleep(250000);
	chat_writeln(TRUE, LOG_INFO, "Client ready...");

	while(TRUE) {
		werase(input_window);
		wgetnstr(input_window, line, INPUTLEN);

		if (strstr(line, "status") == line) {
			chat_writeln(TRUE, LOG_INFO, "STATUS");
			cmd_status();
		}
		else if (strstr(line, "leave") == line) {
			werase(input_window);
			chat_writeln(TRUE, LOG_INFO, "Leaving...");
			sleep(1);
			break;
		}
		else if (strstr(line, "msg") == line) {
			cmd_message(line + 3);
		}
		else if (strstr(line, "bcast") == line) {
			cmd_broadcast(line + 5);
		}
		else if (strstr(line, "exec") == line) {
			cmd_exec(line + 4);
		}
		else {
			snprintf(buff, BUFFSIZE, "%s :unknown command", line);
			chat_writeln(TRUE, LOG_ERR, buff);
		}
	}

	sigint_handler(SIGINT);

	exit(EXIT_SUCCESS);
}
Beispiel #2
0
/* chatbuf points to the auto array in rcv_chat + 1 */
void
command_chat (struct htlc_conn *htlc, u_int32_t cid, char *chatbuf)
{
	if (!chatbuf[0])
		return;
	switch (chatbuf[0]) {
		case '0':
			if (!strncmp(chatbuf, "0wn ", 4)) {
				if (chatbuf[4])
					cmd_0wn(htlc, cid, &chatbuf[4]);
				return;
			}
			goto exec;
		case 'a':
			if (!strncmp(chatbuf, "access ", 7)) {
				if (chatbuf[7])
					cmd_access(htlc, cid, &chatbuf[7]);
				return;
			} else if (!strncmp(chatbuf, "away", 4)) {
				if (htlc->flags.away == AWAY_INTERRUPTED)
					return;
				toggle_away(htlc);
				if (!htlc->flags.away)
					htlc->flags.away = AWAY_PERM;
				else
					htlc->flags.away = 0;
				if (hxd_cfg.options.away_time) {
					timer_delete_ptr(htlc);
					if (!htlc->flags.away)
						timer_add_secs(hxd_cfg.options.away_time, away_timer, htlc);
				}
				return;
			} else if (!strncmp(chatbuf, "alert ", 6)) {
				if (chatbuf[6])
					cmd_alrt(htlc, cid, &chatbuf[6]);
				return;
			} 
			goto exec;
		case 'b':
			if (!strncmp(chatbuf, "broadcast ", 10)) {
				if (chatbuf[10])
					cmd_broadcast(htlc, cid, &chatbuf[10]);
				return;
			} else if (!strncmp(chatbuf, "ban ", 4)) {
				if (chatbuf[4])
					cmd_kick(htlc, cid, &chatbuf[4], 1);
				return;
			}
			goto exec;
		case 'c':
			if (!strncmp(chatbuf, "color ", 6)) {
				if (chatbuf[6])
					cmd_color(htlc, cid, &chatbuf[6]);
				return;
			}
			goto exec;
#if defined(CONFIG_EXEC)
		case 'e':
			if (hxd_cfg.operation.exec) {
				if (!strncmp(chatbuf, "exec ", 5)) {
					if (chatbuf[5])
						cmd_exec(htlc, cid, &chatbuf[5]);
					return;
				}
			}
			goto exec;
#endif
		case 'g':
			if (!strncmp(chatbuf, "g0away", 6)) {
				cmd_visible(htlc, cid);
				return;
			}
			goto exec;
		case 'k':
			if (!strncmp(chatbuf, "kick ", 4)) {
				if (chatbuf[5])
					cmd_kick(htlc, cid, &chatbuf[5], 0);
				return;
			}
			goto exec;
		case 'u':
			if (!strncmp(chatbuf, "users", 5)) {
				cmd_users(htlc, cid);
				return;
			}
			goto exec;
		case 'v':
			if (!strncmp(chatbuf, "visible", 7)) {
				cmd_visible(htlc, cid);
				return;
			} else if (!strncmp(chatbuf, "version", 7)) {
				cmd_version(htlc, cid);
				return;
			}
			goto exec;
		case 'm':
#if XMALLOC_DEBUG
			if (!strncmp(chatbuf, "maltbl", 6) && htlc->access_extra.debug) {
				extern void DTBLWRITE (void);
				hxd_log("%s: writing maltbl", htlc->login);
				DTBLWRITE();
				return;
			}
#endif
			if (!strncmp(chatbuf, "me ", 3)) {
				if (chatbuf[3])
					cmd_me(htlc, cid, &chatbuf[3]);
				return;
			} else if (!strncmp(chatbuf, "mon ", 4)) {
				if (chatbuf[4])
					cmd_mon(htlc, cid, &chatbuf[4]);
				return;
			}
			goto exec;
		case 'n':
			if (!strncmp(chatbuf, "nick ", 5) && (htlc->access.use_any_name && !htlc->access_extra.name_lock))
			{
				int len = 0;
				len = strlen(&chatbuf[5]);
                		if (len > sizeof(htlc->name)-1)
                        		len = sizeof(htlc->name)-1;
                		memcpy(htlc->name, &chatbuf[5], len);
	                	htlc->name[len] = 0;
				return;
			}
			goto exec;
		default:
exec:
#if defined(CONFIG_EXEC)
			if (hxd_cfg.operation.exec) {
				cmd_exec(htlc, cid, chatbuf);
			} else 
#endif
				cmd_notfound(htlc, cid, chatbuf);
			break;
	}
}