/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return 0; new_keys(); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; if (init_networking(ip, PORT) == -1) return 0; DHT_init(); LosslessUDP_init(); friendreq_init(); LANdiscovery_init(); set_nospam(random_int()); send_LANdiscovery(htons(PORT)); timer_single(&LANdiscovery, 0, LAN_DISCOVERY_INTERVAL); return m; }
END_TEST START_TEST(test_m_set_userstatus) { char *status = "online!"; uint16_t good_length = strlen(status); uint16_t bad_length = REALLY_BIG_NUMBER; ck_assert_msg((m_set_statusmessage(m, (uint8_t *)status, bad_length) == -1), "m_set_userstatus did NOT catch the following length: %d\n", REALLY_BIG_NUMBER); ck_assert_msg((m_set_statusmessage(m, (uint8_t *)status, good_length) == 0), "m_set_userstatus did NOT return 0 on the following length: %d\n" "MAX_STATUSMESSAGE_LENGTH: %d\n", good_length, MAX_STATUSMESSAGE_LENGTH); }
void prompt_init_statusbar(ToxWindow *self, Tox *m) { int x, y; getmaxyx(self->window, y, x); /* Init statusbar info */ StatusBar *statusbar = self->stb; statusbar->status = TOX_USERSTATUS_NONE; statusbar->is_online = false; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_get_self_name(m, nick, TOX_MAX_NAME_LENGTH); snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); /* temporary until statusmessage saving works */ uint8_t ver[strlen(TOXICVER) + 1]; uint8_t statusmsg[MAX_STR_SIZE]; strcpy(ver, TOXICVER); uint8_t *toxic_ver = strtok(ver, "_"); if (toxic_ver != NULL) snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic v.%s", toxic_ver); else snprintf(statusmsg, MAX_STR_SIZE, "Toxing on Toxic hacker edition"); m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); /* Init statusbar subwindow */ statusbar->topline = subwin(self->window, 2, x, 0, 0); }
void cmd_status(ToxWindow *self, Messenger *m, char **args) { char *status = args[1]; char *status_text; USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(self->window, "Invalid status.\n"); return; } char *msg = args[2]; if (msg == NULL) { m_set_userstatus(m, status_kind); wprintw(self->window, "Status set to: %s\n", status_text); } else { m_set_userstatus(m, status_kind); m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Status set to: %s, %s\n", status_text, msg); } }
/* run this at startup */ int initMessenger(void) { new_keys(); m_set_statusmessage((uint8_t*)"Online", sizeof("Online")); initNetCrypto(); IP ip; ip.i = 0; if(init_networking(ip,PORT) == -1) return -1; DHT_init(); LosslessUDP_init(); friendreq_init(); LANdiscovery_init(); return 0; }
/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return NULL; IP ip; ip.i = 0; m->net = new_networking(ip, PORT); if (m->net == NULL) { free(m); return NULL; } m->net_crypto = new_net_crypto(m->net); if (m->net_crypto == NULL) { kill_networking(m->net); free(m); return NULL; } m->dht = new_DHT(m->net_crypto); if (m->dht == NULL) { kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m); return NULL; } new_keys(m->net_crypto); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); friendreq_init(&(m->fr), m->net_crypto); LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); return m; }
void prompt_init_statusbar(ToxWindow *self, Tox *m) { int x, y; getmaxyx(self->window, y, x); /* Init statusbar info */ StatusBar *statusbar = (StatusBar *) self->stb; statusbar->status = TOX_USERSTATUS_NONE; statusbar->is_online = false; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; tox_getselfname(m, nick, TOX_MAX_NAME_LENGTH); snprintf(statusbar->nick, sizeof(statusbar->nick), "%s", nick); /* temporary until statusmessage saving works */ uint8_t *statusmsg = "Toxing on Toxic v.0.2.2"; m_set_statusmessage(m, statusmsg, strlen(statusmsg) + 1); snprintf(statusbar->statusmsg, sizeof(statusbar->statusmsg), "%s", statusmsg); /* Init statusbar subwindow */ statusbar->topline = subwin(self->window, 2, x, 0, 0); }
void line_eval(char *line) { if (line[0] == '/') { char inpt_command = line[1]; char prompt[STRING_LENGTH+2] = "> "; int prompt_offset = 3; strcat(prompt, line); new_lines(prompt); if (inpt_command == 'f') { // add friend command: /f ID int i; char temp_id[128]; for (i = 0; i < 128; i++) temp_id[i] = line[i+prompt_offset]; unsigned char *bin_string = hex_string_to_bin(temp_id); int num = m_addfriend(bin_string, (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); free(bin_string); char numstring[100]; switch (num) { case FAERR_TOOLONG: sprintf(numstring, "[i] Message is too long."); break; case FAERR_NOMESSAGE: sprintf(numstring, "[i] Please add a message to your request."); break; case FAERR_OWNKEY: sprintf(numstring, "[i] That appears to be your own ID."); break; case FAERR_ALREADYSENT: sprintf(numstring, "[i] Friend request already sent."); break; case FAERR_UNKNOWN: sprintf(numstring, "[i] Undefined error when adding friend."); break; default: sprintf(numstring, "[i] Added friend as %d.", num); break; } new_lines(numstring); do_refresh(); } else if (inpt_command == 'd') { doMessenger(); } else if (inpt_command == 'm') { //message command: /m friendnumber messsage size_t len = strlen(line); if(len < 3) return; char numstring[len-3]; char message[len-3]; int i; for (i = 0; i < len; i++) { if (line[i+3] != ' ') { numstring[i] = line[i+3]; } else { int j; for (j = (i+1); j < (len+1); j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if (m_sendmessage(num, (uint8_t*) message, strlen(message) + 1) != 1) { new_lines("[i] could not send message"); } else { new_lines(format_message(message, -1)); } } else if (inpt_command == 'n') { uint8_t name[MAX_NAME_LENGTH]; int i = 0; size_t len = strlen(line); for (i = 3; i < len; i++) { if (line[i] == 0 || line[i] == '\n') break; name[i-3] = line[i]; } name[i-3] = 0; setname(name, i - 2); char numstring[100]; sprintf(numstring, "[i] changed nick to %s", (char*)name); new_lines(numstring); } else if (inpt_command == 'l') { print_friendlist(); } else if (inpt_command == 's') { uint8_t status[MAX_STATUSMESSAGE_LENGTH]; int i = 0; size_t len = strlen(line); for (i = 3; i < len; i++) { if (line[i] == 0 || line[i] == '\n') break; status[i-3] = line[i]; } status[i-3] = 0; m_set_statusmessage(status, strlen((char*)status) + 1); char numstring[100]; sprintf(numstring, "[i] changed status to %s", (char*)status); new_lines(numstring); } else if (inpt_command == 'a') { uint8_t numf = atoi(line + 3); char numchar[100]; if (numf >= num_requests || pending_requests[numf].accepted) { sprintf(numchar,"[i] you either didn't receive that request or you already accepted it"); new_lines(numchar); } else { int num = m_addfriend_norequest(pending_requests[numf].id); if (num != -1) { pending_requests[numf].accepted = 1; sprintf(numchar, "[i] friend request %u accepted", numf); new_lines(numchar); sprintf(numchar, "[i] added friendnumber %d", num); new_lines(numchar); } else { sprintf(numchar, "[i] failed to add friend"); new_lines(numchar); } } do_refresh(); } else if (inpt_command == 'h') { //help new_lines(help); } else if (inpt_command == 'i') { //info char idstring[200]; get_id(idstring); new_lines(idstring); } else if (inpt_command == 'q') { //exit endwin(); exit(EXIT_SUCCESS); } else { new_lines("[i] invalid command"); } } else { new_lines("[i] invalid command"); //new_lines(line); } }
/* Set our user status; * you are responsible for freeing status after. * * return 0 on success, -1 on failure. */ int tox_set_statusmessage(void *tox, uint8_t *status, uint16_t length) { Messenger *m = tox; return m_set_statusmessage(m, status, length); }
/* Set our user status; * you are responsible for freeing status after. * * return 0 on success, -1 on failure. */ int tox_set_status_message(Tox *tox, const uint8_t *status, uint16_t length) { Messenger *m = tox; return m_set_statusmessage(m, status, length); }
void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); int x, y; getmaxyx(self->window, y, x); (void) x; wmove(self->window, y - CURS_Y_OFFSET, 0); } else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) print_help(ctx); else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); exit(0); } else if (!strncmp(cmd, "/me ", strlen("/me "))) { struct tm *timeinfo = get_time(); char *action = strchr(cmd, ' '); if (action == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } action++; wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); uint8_t selfname[MAX_NAME_LENGTH]; int len = getself_name(m, selfname, sizeof(selfname)); char msg[MAX_STR_SIZE - len - 4]; snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); wattron(ctx->history, COLOR_PAIR(5)); wprintw(ctx->history, msg); wattroff(ctx->history, COLOR_PAIR(5)); if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); char *msg; char *status_text; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } status++; USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(ctx->history, "Invalid status.\n"); return; } msg = strchr(status, ' '); if (msg == NULL) { m_set_userstatus(m, status_kind); wprintw(ctx->history, "Status set to: %s\n", status_text); } else { msg++; m_set_userstatus(m, status_kind); m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); } } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { char *nick; nick = strchr(cmd, ' '); if (nick == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } nick++; setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(ctx->history, "Nickname set to: %s\n", nick); } else if (!strcmp(cmd, "/myid")) { char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; int i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } wprintw(ctx->history, "%s\n", id); } else if (strcmp(ctx->line, "/close") == 0) { int f_num = ctx->friendnum; delwin(ctx->linewin); del_window(self); disable_chatwin(f_num); } else wprintw(ctx->history, "Invalid command.\n"); }
void cmd_statusmsg(ToxWindow *self, Messenger *m, char **args) { char *msg = args[1]; m_set_statusmessage(m, (uint8_t *) msg, strlen(msg) + 1); wprintw(self->window, "Status set to: %s\n", msg); }
static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key) { ChatContext *ctx = (ChatContext *) self->x; struct tm *timeinfo = get_time(); int x, y, y2, x2; getyx(self->window, y, x); getmaxyx(self->window, y2, x2); /* Add printable chars to buffer and print on input space */ #if HAVE_WIDECHAR if (iswprint(key)) { #else if (isprint(key)) { #endif if (ctx->pos != sizeof(ctx->line) - 1) { mvwaddstr(self->window, y, x, wc_to_char(key)); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = L'\0'; } } /* BACKSPACE key: Remove one character from line */ else if (key == 0x107 || key == 0x8 || key == 0x7f) { if (ctx->pos > 0) { ctx->line[--ctx->pos] = L'\0'; if (x == 0) mvwdelch(self->window, y - 1, x2 - 1); else mvwdelch(self->window, y, x - 1); } } /* RETURN key: Execute command or print line */ else if (key == '\n') { char *line = wcs_to_char(ctx->line); wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); if (line[0] == '/') execute(self, ctx, m, line); else { /* make sure the string has at least non-space character */ if (!string_is_empty(line)) { uint8_t selfname[MAX_NAME_LENGTH]; getself_name(m, selfname, sizeof(selfname)); wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s: ", selfname); wattroff(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s\n", line); if (m_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send message.\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } } ctx->line[0] = L'\0'; ctx->pos = 0; free(line); } } void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) { if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { wclear(self->window); wclear(ctx->history); int x, y; getmaxyx(self->window, y, x); (void) x; wmove(self->window, y - CURS_Y_OFFSET, 0); } else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) print_help(ctx); else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { endwin(); exit(0); } else if (!strncmp(cmd, "/me ", strlen("/me "))) { struct tm *timeinfo = get_time(); char *action = strchr(cmd, ' '); if (action == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } action++; wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattroff(ctx->history, COLOR_PAIR(2)); uint8_t selfname[MAX_NAME_LENGTH]; int len = getself_name(m, selfname, sizeof(selfname)); char msg[MAX_STR_SIZE - len - 4]; snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t *) selfname, action); wattron(ctx->history, COLOR_PAIR(5)); wprintw(ctx->history, msg); wattroff(ctx->history, COLOR_PAIR(5)); if (m_sendaction(m, ctx->friendnum, (uint8_t *) msg, strlen(msg) + 1) < 0) { wattron(ctx->history, COLOR_PAIR(3)); wprintw(ctx->history, " * Failed to send action\n"); wattroff(ctx->history, COLOR_PAIR(3)); } } else if (!strncmp(cmd, "/status ", strlen("/status "))) { char *status = strchr(cmd, ' '); char *msg; char *status_text; if (status == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } status++; USERSTATUS status_kind; if (!strncmp(status, "online", strlen("online"))) { status_kind = USERSTATUS_NONE; status_text = "ONLINE"; } else if (!strncmp(status, "away", strlen("away"))) { status_kind = USERSTATUS_AWAY; status_text = "AWAY"; } else if (!strncmp(status, "busy", strlen("busy"))) { status_kind = USERSTATUS_BUSY; status_text = "BUSY"; } else { wprintw(ctx->history, "Invalid status.\n"); return; } msg = strchr(status, ' '); if (msg == NULL) { m_set_userstatus(m, status_kind); wprintw(ctx->history, "Status set to: %s\n", status_text); } else { msg++; m_set_userstatus(m, status_kind); m_set_statusmessage(m, ( uint8_t *) msg, strlen(msg) + 1); wprintw(ctx->history, "Status set to: %s, %s\n", status_text, msg); } } else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { char *nick; nick = strchr(cmd, ' '); if (nick == NULL) { wprintw(ctx->history, "Invalid syntax.\n"); return; } nick++; setname(m, (uint8_t *) nick, strlen(nick) + 1); wprintw(ctx->history, "Nickname set to: %s\n", nick); } else if (!strcmp(cmd, "/myid")) { char id[FRIEND_ADDRESS_SIZE * 2 + 1] = {0}; uint8_t address[FRIEND_ADDRESS_SIZE]; size_t i; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02X", address[i] & 0xff); strcat(id, xx); } wprintw(ctx->history, "%s\n", id); } else if (strcmp(cmd, "/close") == 0) { int f_num = ctx->friendnum; delwin(ctx->linewin); del_window(self); disable_chatwin(f_num); } else wprintw(ctx->history, "Invalid command.\n"); } static void chat_onDraw(ToxWindow *self, Messenger *m) { curs_set(1); int x, y; getmaxyx(self->window, y, x); (void) y; ChatContext *ctx = (ChatContext *) self->x; mvwhline(ctx->linewin, 0, 0, '_', x); wrefresh(self->window); }
/* Set our user status; * you are responsible for freeing status after. * * return 0 on success, -1 on failure. */ int tox_set_status_message(Tox *tox, size_t *status, size_t length) { Messenger *m = tox; return m_set_statusmessage(m, status, length); }