#include #include #include #include #include #define LINE_MAX_LEN 2048 static int running = 1; static barev_client_t *g_client = 0; static void on_log(const char *lvl, const char *msg, void *ud) { (void)ud; printf("[%s] %s\n", lvl ? lvl : "?", msg ? msg : ""); } static void on_msg(barev_buddy_t *b, const char *msg, void *ud) { (void)ud; printf("\n*** Message from %s: %s\n> ", barev_buddy_get_jid(b), msg ? msg : ""); fflush(stdout); } static void on_conn(barev_buddy_t *b, int st, void *ud) { (void)ud; printf("*** Conn %s state=%d\n", barev_buddy_get_jid(b), st); } static void on_typing(barev_buddy_t *b, int typing, void *ud) { (void)ud; printf("*** %s %s typing\n", barev_buddy_get_jid(b), typing ? "is" : "stopped"); } static void on_ft_offer(barev_buddy_t *b, const char *sid, const char *name, long sz, void *ud) { (void)ud; printf("*** FT offer from %s sid=%s file=%s size=%ld\n", barev_buddy_get_jid(b), sid, name, sz); } static void on_ft_progress(barev_buddy_t *b, const char *sid, long done, long total, void *ud) { (void)ud; (void)b; printf("*** FT %s progress %ld/%ld\n", sid, done, total); } static void on_ft_complete(barev_buddy_t *b, const char *sid, const char *path, void *ud) { (void)ud; printf("*** FT complete with %s sid=%s path=%s\n", barev_buddy_get_jid(b), sid, path); } static void on_ft_error(barev_buddy_t *b, const char *sid, const char *err, void *ud) { (void)ud; printf("*** FT error with %s sid=%s err=%s\n", barev_buddy_get_jid(b), sid, err ? err : ""); } static void show_help(void) { printf("Commands:\n"); printf(" help\n"); printf(" add [port]\n"); printf(" connect \n"); printf(" msg \n"); printf(" typing \n"); printf(" paused \n"); printf(" sendfile \n"); printf(" acceptfile [save_path]\n"); printf(" rejectfile \n"); printf(" quit\n"); } static void run_command(char *line) { char *cmd = strtok(line, " "); if (!cmd || !*cmd) return; if (strcmp(cmd, "help") == 0) { show_help(); } else if (strcmp(cmd, "add") == 0) { char *nick = strtok(0, " "); char *ip = strtok(0, " "); char *p = strtok(0, " "); unsigned short port = (unsigned short)(p ? atoi(p) : BAREV_DEFAULT_PORT); barev_buddy_t *b; if (!nick || !ip) { printf("usage: add [port]\n"); return; } b = barev_client_add_buddy(g_client, nick, ip, port); if (b) printf("added %s\n", barev_buddy_get_jid(b)); else printf("failed add\n"); } else if (strcmp(cmd, "connect") == 0) { char *jid = strtok(0, " "); if (!jid) printf("usage: connect \n"); else printf("connect %s\n", barev_client_connect(g_client, jid) ? "ok" : "fail"); } else if (strcmp(cmd, "msg") == 0) { char *jid = strtok(0, " "); char *txt = strtok(0, ""); if (!jid || !txt) printf("usage: msg \n"); else printf("send %s\n", barev_client_send_message(g_client, jid, txt) ? "ok" : "fail"); } else if (strcmp(cmd, "typing") == 0) { char *jid = strtok(0, " "); if (!jid) printf("usage: typing \n"); else printf("typing %s\n", barev_client_send_typing(g_client, jid) ? "ok" : "fail"); } else if (strcmp(cmd, "paused") == 0) { char *jid = strtok(0, " "); if (!jid) printf("usage: paused \n"); else printf("paused %s\n", barev_client_send_paused(g_client, jid) ? "ok" : "fail"); } else if (strcmp(cmd, "sendfile") == 0) { char *jid = strtok(0, " "); char *name = strtok(0, " "); char *sz = strtok(0, " "); if (!jid || !name || !sz) { printf("usage: sendfile \n"); } else { printf("sendfile %s\n", barev_client_send_file_offer(g_client, jid, name, atol(sz)) ? "ok" : "fail"); } } else if (strcmp(cmd, "acceptfile") == 0) { char *sid = strtok(0, " "); char *path = strtok(0, ""); int ok; if (!sid) { printf("usage: acceptfile [save_path]\n"); return; } ok = path ? barev_client_accept_file_offer_as(g_client, sid, path) : barev_client_accept_file_offer(g_client, sid); printf("acceptfile %s\n", ok ? "ok" : "fail"); } else if (strcmp(cmd, "rejectfile") == 0) { char *sid = strtok(0, " "); if (!sid) printf("usage: rejectfile \n"); else printf("rejectfile %s\n", barev_client_reject_file_offer(g_client, sid) ? "ok" : "fail"); } else if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "exit") == 0) { running = 0; } else { printf("unknown command: %s\n", cmd); } } int main(int argc, char **argv) { char line[LINE_MAX_LEN]; if (argc < 3) { printf("usage: %s [port]\n", argv[0]); return 2; } g_client = barev_client_new(argv[1], argv[2], (unsigned short)((argc > 3) ? atoi(argv[3]) : BAREV_DEFAULT_PORT)); if (!g_client) return 1; barev_set_log_cb(g_client, on_log); barev_set_message_cb(g_client, on_msg); barev_set_conn_state_cb(g_client, on_conn); barev_set_typing_cb(g_client, on_typing); barev_set_ft_offer_cb(g_client, on_ft_offer); barev_set_ft_progress_cb(g_client, on_ft_progress); barev_set_ft_complete_cb(g_client, on_ft_complete); barev_set_ft_error_cb(g_client, on_ft_error); if (!barev_client_start(g_client)) { barev_client_free(g_client); return 1; } printf("barvclient started as %s\n", barev_client_myjid(g_client)); show_help(); while (running) { fd_set rfds; struct timeval tv; int rc; barev_client_process(g_client); FD_ZERO(&rfds); FD_SET(0, &rfds); tv.tv_sec = 0; tv.tv_usec = 20000; rc = select(1, &rfds, 0, 0, &tv); if (rc > 0 && FD_ISSET(0, &rfds)) { printf("> "); fflush(stdout); if (!fgets(line, sizeof(line), stdin)) break; line[strcspn(line, "\r\n")] = '\0'; run_command(line); } } barev_client_stop(g_client); barev_client_free(g_client); return 0; }