void message_friend() { size_t len = strlen(line); 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; j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) printf("\n[i] could not send message (they may be offline): %s\n", message); else printf("\n"); }
void UserItem::sendMessage(const QString &message) { emit m_sendmessage(m_id, message); m_chatlog.append("You: "); m_chatlog.append(message); m_chatlog.append("\n"); //this is kinda ugly emit chatlogChanged(); }
void cmd_msg(ToxWindow *self, Messenger *m, char **args) { char *id = args[1]; char *msg = args[2]; if (m_sendmessage(m, atoi(id), (uint8_t*) msg, strlen(msg)+1) == 0) wprintw(self->window, "Error occurred while sending message.\n"); else wprintw(self->window, "Message successfully sent.\n"); }
static void chat_onKey(ToxWindow* self, int key) { ChatContext* ctx = (ChatContext*) self->x; time_t now; time(&now); struct tm * timeinfo; timeinfo = localtime(&now); /* PRINTABLE characters: Add to line */ if(isprint(key)) { if(ctx->pos != sizeof(ctx->line)-1) { ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } /* RETURN key: Execute command or print line */ else if(key == '\n') { if (ctx->line[0] == '/') execute(self, ctx, ctx->line); else { if(!string_is_empty(ctx->line)) { /* make sure the string has at least non-space character */ 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, "you: ", ctx->line); wattroff(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s\n", ctx->line); } if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->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] = '\0'; ctx->pos = 0; } /* BACKSPACE key: Remove one character from line */ else if(key == 0x107 || key == 0x8 || key == 0x7f) { if(ctx->pos != 0) { ctx->line[--ctx->pos] = '\0'; } } }
static void chat_onKey(ToxWindow* self, int key) { ChatContext* ctx = (ChatContext*) self->x; time_t now; time(&now); struct tm * timeinfo; timeinfo = localtime(&now); if(isprint(key)) { if(ctx->pos != sizeof(ctx->line)-1) { ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } else if(key == '\n') { wattron(ctx->history, COLOR_PAIR(2)); wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); wattron(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "you: ", ctx->line); wattroff(ctx->history, COLOR_PAIR(1)); wprintw(ctx->history, "%s\n", ctx->line); if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->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] = '\0'; ctx->pos = 0; } else if(key == 0x107 || key == 0x8 || key == 0x7f) { if(ctx->pos != 0) { ctx->line[--ctx->pos] = '\0'; } } }
int main(int argc, char *argv[]) { if (argc < 4 && argc != 2) { printf("usage %s ip port public_key (of the DHT bootstrap node)\n or\n %s Save.bak\n", argv[0], argv[0]); exit(0); } m = initMessenger(); if ( !m ) { fputs("Failed to allocate messenger datastructure\n", stderr); exit(0); } if (argc > 3) { IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(m->dht, bootstrap_ip_port, hex_string_to_bin(argv[3])); } else { FILE *file = fopen(argv[1], "rb"); if ( file == NULL ) { return 1; } int read; uint8_t buffer[128000]; read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read)); fclose(file); } m_callback_friendrequest(m, print_request, NULL); m_callback_friendmessage(m, print_message, NULL); printf("OUR ID: "); uint32_t i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { if (address[i] < 16) printf("0"); printf("%hhX", address[i]); } setname(m, (uint8_t *)"Anon", 5); char temp_id[128]; printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); if (scanf("%s", temp_id) != 1) { return 1; } int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); while (1) { uint8_t name[128]; getname(m, num, name); printf("%s\n", name); m_sendmessage(m, num, (uint8_t *)"Test", 5); doMessenger(m); c_sleep(30); FILE *file = fopen("Save.bak", "wb"); if ( file == NULL ) { return 1; } uint8_t *buffer = malloc(Messenger_size(m)); Messenger_save(m, buffer); size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); if (write_result < Messenger_size(m)) { return 1; } free(buffer); fclose(file); } cleanupMessenger(m); }
void print_message(Messenger *m, int friendnumber, uint8_t *string, uint16_t length, void *userdata) { printf("Message with length %u received from %u: %s \n", length, friendnumber, string); m_sendmessage(m, friendnumber, (uint8_t *)"Test1", 6); }
/* Send a text chat message to an online friend. * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * * You will want to retain the return value, it will be passed to your read receipt callback * if one is received. * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ uint32_t tox_sendmessage(void *tox, int friendnumber, uint8_t *message, uint32_t length) { Messenger *m = tox; return m_sendmessage(m, friendnumber, message, length); }
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); } }
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) { if (line[0] == '/') { char command[STRING_LENGTH + 2] = "> "; strcat(command, line); new_lines(command); if (line[1] == 'f') { // add friend command: /f ID int i; char temp_id[128]; for (i=0; i<128; i++) { temp_id[i] = line[i+3]; } int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); char numstring[100]; sprintf(numstring, "[i] added friend %d", num); new_lines(numstring); do_refresh(); } else if (line[1] == 'd') { doMessenger(); } else if (line[1] == 'm') { //message command: /m friendnumber messsage int i; int len = strlen(line); char numstring[len-3]; char message[len-3]; for (i=0; i<len; i++) { if (line[i+3] != ' ') { numstring[i] = line[i+3]; } else { int j; for (j=i+1; j<len; j++) { message[j-i-1] = line[j+3]; } break; } } int num = atoi(numstring); m_sendmessage(num, (uint8_t*) message, sizeof(message)); } else if (line[1] == 'n') { uint8_t name[MAX_NAME_LENGTH]; int i = 0; for (i=3; i<strlen(line); i++) { if (line[i] == 0 || line[i] == '\n') break; name[i - 3] = line[i]; } name[i - 3] = 0; setname(name, i); char numstring[100]; sprintf(numstring, "[i] changed nick to %s", (char*)name); new_lines(numstring); } else if (line[1] == 's') { uint8_t status[MAX_USERSTATUS_LENGTH]; int i = 0; for (i=3; i<strlen(line); i++) { if (line[i] == 0 || line[i] == '\n') break; status[i - 3] = line[i]; } status[i - 3] = 0; m_set_userstatus(status, strlen((char*)status)); char numstring[100]; sprintf(numstring, "[i] changed status to %s", (char*)status); new_lines(numstring); } else if (line[1] == 'q') { //exit endwin(); exit(EXIT_SUCCESS); } } else { //new_lines(line); } }
static void chat_onKey(ToxWindow *self, Messenger *m, int 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 (isprint(key)) { if (ctx->pos != sizeof(ctx->line) - 1) { mvwaddch(self->window, y, x, key); ctx->line[ctx->pos++] = key; ctx->line[ctx->pos] = '\0'; } } /* BACKSPACE key: Remove one character from line */ else if (key == 0x107 || key == 0x8 || key == 0x7f) { if (ctx->pos > 0) { ctx->line[--ctx->pos] = '\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') { wclear(ctx->linewin); wmove(self->window, y2 - CURS_Y_OFFSET, 0); wclrtobot(self->window); if (ctx->line[0] == '/') execute(self, ctx, m, ctx->line); else { /* make sure the string has at least non-space character */ if (!string_is_empty(ctx->line)) { uint8_t selfname[MAX_NAME_LENGTH]; getself_name(m, selfname, sizeof(selfname)); fix_name(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", ctx->line); if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->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] = '\0'; ctx->pos = 0; } }
int main(int argc, char *argv[]) { puts("=========== FRIENDS_TEST ==========="); /* set up the global memory */ parent_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); child_id = mmap(NULL, crypto_box_PUBLICKEYBYTES, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); fputs("friends_test: Starting test...\n", stdout); if((child_pid = fork()) == 0) { /* child */ int i = 0; char *message = "Y-yes Mr. Watson?"; initMessenger(); Messenger_save(child_id); msync(child_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); m_callback_friendrequest(child_got_request); m_callback_userstatus(child_got_statuschange); /* wait on the friend request */ while(!(request_flags & FIRST_FLAG)) do_tox(); /* wait for the status change */ while(!(request_flags & SECOND_FLAG)) do_tox(); for(i = 0; i < 6; i++) { /* send the message six times, just to be sure */ m_sendmessage(0, (uint8_t *)message, strlen(message)); do_tox(); } return 0; } /* parent */ if(atexit(cleanup) != 0) { fputs("friends_test: atexit() failed!\nFailing build...\n", stderr); kill(child_pid, SIGKILL); return -1; } msync(parent_id, crypto_box_PUBLICKEYBYTES, MS_SYNC); m_callback_userstatus(parent_confirm_status); m_callback_friendmessage(parent_confirm_message); /* hacky way to give the child time to set up */ c_sleep(50); initMessenger(); Messenger_save(parent_id); if(parent_friend_request() == -1) return -1; if(parent_wait_for_message() == -1) return -1; wait(NULL); fputs("friends_test: Build passed!\n", stdout); return 0; }
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); }
/* Send a text chat message to an online friend. * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. * m_sendmessage_withid will send a message with the id of your choosing, * however we can generate an id for you by calling plain m_sendmessage. */ size_t tox_send_message(Tox *tox, int friendnumber, size_t *message, size_t length) { Messenger *m = tox; return m_sendmessage(m, friendnumber, message, length); }
void line_eval(char* line) { if(line[0] == '/') { /* Add friend */ if(line[1] == 'f') { int i; char temp_id[128]; for (i=0; i<128; i++) temp_id[i] = line[i+3]; int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); char numstring[100]; sprintf(numstring, "\n[i] added friend %d\n\n", num); printf(numstring); } else if (line[1] == 'd') { doMessenger(); } /* Send message to friend */ else if (line[1] == 'm') { int i; size_t len = strlen(line); char numstring[len-3]; char message[len-3]; for (i=0; i<len; i++) { if (line[i+3] != ' ') { numstring[i] = line[i+3]; } else { int j; for (j=i+1; j<len; j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { printf("\n[i] could not send message: %s\n\n", message); } } else if (line[1] == '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); char numstring[100]; sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); printf(numstring); } else if (line[1] == 's') { uint8_t status[MAX_USERSTATUS_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_userstatus(status, strlen((char*)status)); char numstring[100]; sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); printf(numstring); } else if (line[1] == 'a') { uint8_t numf = atoi(line + 3); char numchar[100]; sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); printf(numchar); int num = m_addfriend_norequest(pending_requests[numf]); sprintf(numchar, "\n[i] added friendnumber %d\n\n", num); printf(numchar); } /* EXIT */ else if (line[1] == 'q') { exit(EXIT_SUCCESS); } } else { //nothing atm } }
void line_eval(char* line) { if(line[0] == '/') { char inpt_command = line[1]; /* Add friend */ if(inpt_command == 'f') { int i; char temp_id[128]; for (i = 0; i < 128; i++) temp_id[i] = line[i+3]; int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); if (num >= 0) { char numstring[100]; sprintf(numstring, "\n[i] Friend request sent. Wait to be accepted. Friend id: %d\n\n", num); printf(numstring); } else if (num == -1) printf("\n[i] Message is too long.\n\n"); else if (num == -2) printf("\n[i] Please add a message to your friend request.\n\n"); else if (num == -3) printf("\n[i] That appears to be your own ID.\n\n"); else if (num == -4) printf("\n[i] Friend request already sent.\n\n"); else if (num == -5) printf("\n[i] Undefined error when adding friend\n\n"); } else if (inpt_command == 'r') { do_header(); printf("\n\n"); } else if (inpt_command == 'l') { int activefriends = 0; int i; for (i = 0; i <= getnumfriends(); i++) { if (m_friendstatus(i) == 4) activefriends++; } printf("\n[i] Friend List | Total: %d\n\n", activefriends); for (i = 0; i <= getnumfriends(); i++) { char name[MAX_NAME_LENGTH]; getname(i, (uint8_t*)name); if (m_friendstatus(i) == 4) printf("[%d] %s\n\n", i, (uint8_t*)name); } } else if (inpt_command == 'd') { size_t len = strlen(line); char numstring[len-3]; int i; for (i = 0; i < len; i++) { if (line[i+3] != ' ') { numstring[i] = line[i+3]; } } int num = atoi(numstring); m_delfriend(num); printf("\n\n"); } /* Send message to friend */ else if (inpt_command == 'm') { size_t len = strlen(line); 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; j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if(m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 1) { printf("\n[i] could not send message (they may be offline): %s\n", message); } else { //simply for aesthetics printf("\n"); } } 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); char numstring[100]; sprintf(numstring, "\n[i] changed nick to %s\n\n", (char*)name); printf(numstring); FILE *name_file = NULL; name_file = fopen("namefile.txt", "w"); fprintf(name_file, "%s", (char*)name); fclose(name_file); } else if (inpt_command == 's') { uint8_t status[MAX_USERSTATUS_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_userstatus(status, strlen((char*)status)); char numstring[100]; sprintf(numstring, "\n[i] changed status to %s\n\n", (char*)status); printf(numstring); FILE* status_file = NULL; status_file = fopen("statusfile.txt", "w"); fprintf(status_file, "%s", (char*)status); fclose(status_file); } else if (inpt_command == 'a') { uint8_t numf = atoi(line + 3); char numchar[100]; sprintf(numchar, "\n[i] friend request %u accepted\n\n", numf); printf(numchar); int num = m_addfriend_norequest(pending_requests[numf]); sprintf(numchar, "\n[i] added friendnumber %d\n\n", num); printf(numchar); } /* EXIT */ else if (inpt_command == 'q') { uint8_t status[MAX_USERSTATUS_LENGTH] = "Offline"; m_set_userstatus(status, strlen((char*)status)); exit(EXIT_SUCCESS); } } else { //nothing atm } }
void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) { if (line[0] == '/') { char inpt_command = line[1]; char prompt[STRING_LENGTH + 2] = "> "; 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+3]; int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); char numstring[100]; sprintf(numstring, "[i] added friend %d", num); 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); 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; j++) message[j-i-1] = line[j+3]; break; } } int num = atoi(numstring); if (m_sendmessage(num, (uint8_t*) message, sizeof(message)) != 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); 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_USERSTATUS_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_userstatus(status, strlen((char*)status)); 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]; int num = m_addfriend_norequest(pending_requests[numf]); if (num != -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("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status)"); new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"); } else if (inpt_command == 'i') { //info char idstring0[200]; char idstring1[PUB_KEY_BYTES][5]; char idstring2[PUB_KEY_BYTES][5]; int i; for (i = 0; i < PUB_KEY_BYTES; i++) { if (self_public_key[i] < (PUB_KEY_BYTES/2)) strcpy(idstring1[i],"0"); else strcpy(idstring1[i], ""); sprintf(idstring2[i], "%hhX", self_public_key[i]); } // strcpy(idstring0,"[i] ID: "); int j; for (j = 0; j < PUB_KEY_BYTES; j++) { strcat(idstring0,idstring1[j]); strcat(idstring0,idstring2[j]); } new_lines(idstring0); } 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); } }
static void execute(ToxWindow* self, char* cmd) { // quit/exit: Exit program. if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { endwin(); exit(0); } else if(!strncmp(cmd, "connect ", strlen("connect "))) { char* ip; char* port; char* key; IP_Port dht; ip = strchr(cmd, ' '); if(ip == NULL) { return; } ip++; port = strchr(ip, ' '); if(port == NULL) { return; } port[0] = 0; port++; key = strchr(port, ' '); if(key == NULL) { return; } key[0] = 0; key++; if(atoi(port) == 0) { return; } wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key); dht.port = htons(atoi(port)); int resolved_address = resolve_addr(ip); if (resolved_address == -1) { return; } dht.ip.i = resolved_address; DHT_bootstrap(dht, hex_string_to_bin(key)); } else if(!strncmp(cmd, "add ", strlen("add "))) { char* id; char* msg; int num; id = strchr(cmd, ' '); if(id == NULL) { return; } id++; msg = strchr(id, ' '); if(msg == NULL) { return; } msg[0] = 0; msg++; num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Friend added as %d.\n", num); } else if(!strncmp(cmd, "status ", strlen("status "))) { char* msg; msg = strchr(cmd, ' '); if(msg == NULL) { return; } msg++; m_set_userstatus((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s.\n", msg); } else if(!strncmp(cmd, "nick ", strlen("nick "))) { char* nick; nick = strchr(cmd, ' '); if(nick == NULL) { return; } nick++; setname((uint8_t*) nick, strlen(nick)+1); wprintw(self->window, "Nickname set to: %s.\n", nick); } else if(!strcmp(cmd, "myid")) { // XXX: Clean this up char idstring0[200]; char idstring1[32][5]; char idstring2[32][5]; uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) strcpy(idstring1[i], "0"); else strcpy(idstring1[i], ""); sprintf(idstring2[i], "%hhX", self_public_key[i]); } for (i=0; i<32; i++) { strcat(idstring0, idstring1[i]); strcat(idstring0, idstring2[i]); } wprintw(self->window, "%s\n", idstring0); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; int num; id = strchr(cmd, ' '); if(id == NULL) { return; } id++; num = atoi(id); if(num >= num_requests) { return; } num = m_addfriend_norequest(pending_requests[num]); wprintw(self->window, "Friend accepted as: %d.\n", num); } else if(!strncmp(cmd, "msg ", strlen("msg "))) { char* id; char* msg; id = strchr(cmd, ' '); if(id == NULL) { return; } id++; msg = strchr(id, ' '); if(msg == NULL) { return; } msg[0] = 0; msg++; if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { wprintw(self->window, "Error occurred while sending message.\n"); } else { wprintw(self->window, "Message successfully sent.\n"); } } }
int main(int argc, char *argv[]) { /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); if (argvoffset < 0) exit(1); /* with optional --ipvx, now it can be 1-4 arguments... */ if ((argc != argvoffset + 2) && (argc != argvoffset + 4)) { printf("Usage: %s [--ipv4|--ipv6] ip port public_key (of the DHT bootstrap node)\n", argv[0]); printf("or\n"); printf(" %s [--ipv4|--ipv6] Save.bak (to read Save.bak as state file)\n", argv[0]); exit(0); } m = initMessenger(ipv6enabled); if ( !m ) { fputs("Failed to allocate messenger datastructure\n", stderr); exit(0); } if (argc == argvoffset + 4) { uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(m->dht, argv[argvoffset + 1], ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } } else { FILE *file = fopen(argv[argvoffset + 1], "rb"); if ( file == NULL ) { printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]); return 1; } int read; uint8_t buffer[128000]; read = fread(buffer, 1, 128000, file); printf("Messenger loaded: %i\n", Messenger_load(m, buffer, read)); fclose(file); } m_callback_friendrequest(m, print_request, NULL); m_callback_friendmessage(m, print_message, NULL); printf("OUR ID: "); uint32_t i; uint8_t address[FRIEND_ADDRESS_SIZE]; getaddress(m, address); for (i = 0; i < FRIEND_ADDRESS_SIZE; i++) { if (address[i] < 16) printf("0"); printf("%hhX", address[i]); } setname(m, (uint8_t *)"Anon", 5); char temp_id[128]; printf("\nEnter the address of the friend you wish to add (38 bytes HEX format):\n"); if (scanf("%s", temp_id) != 1) { return 1; } int num = m_addfriend(m, hex_string_to_bin(temp_id), (uint8_t *)"Install Gentoo", sizeof("Install Gentoo")); perror("Initialization"); while (1) { uint8_t name[128]; getname(m, num, name); printf("%s\n", name); m_sendmessage(m, num, (uint8_t *)"Test", 5); doMessenger(m); c_sleep(30); FILE *file = fopen("Save.bak", "wb"); if ( file == NULL ) { return 1; } uint8_t *buffer = malloc(Messenger_size(m)); Messenger_save(m, buffer); size_t write_result = fwrite(buffer, 1, Messenger_size(m), file); if (write_result < Messenger_size(m)) { return 1; } free(buffer); fclose(file); } cleanupMessenger(m); }
/* Send a text chat message to an online friend. * return the message id if packet was successfully put into the send queue. * return 0 if it was not. * * You will want to retain the return value, it will be passed to your read_receipt callback * if one is received. */ uint32_t tox_send_message(Tox *tox, int32_t friendnumber, const uint8_t *message, uint32_t length) { Messenger *m = tox; return m_sendmessage(m, friendnumber, message, length); }
static void execute(ToxWindow* self, char* cmd) { if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { endwin(); exit(0); } else if(!strncmp(cmd, "connect ", strlen("connect "))) { char* ip; char* port; char* key; IP_Port dht; ip = strchr(cmd, ' '); if(ip == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } ip++; port = strchr(ip, ' '); if(port == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } port[0] = 0; port++; key = strchr(port, ' '); if(key == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } key[0] = 0; key++; if(atoi(port) == 0) { wprintw(self->window, "Invalid syntax.\n"); return; } dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) { return; } dht.ip.i = resolved_address; DHT_bootstrap(dht, hex_string_to_bin(key)); } else if(!strncmp(cmd, "add ", strlen("add "))) { uint8_t id_bin[32]; size_t i; char xx[3]; uint32_t x; char* id; char* msg; int num; id = strchr(cmd, ' '); if(id == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } id++; msg = strchr(id, ' '); if(msg != NULL) { msg[0] = 0; msg++; } else msg = ""; if(strlen(id) != 2*32) { wprintw(self->window, "Invalid ID length.\n"); return; } for(i=0; i<32; i++) { xx[0] = id[2*i]; xx[1] = id[2*i+1]; xx[2] = '\0'; if(sscanf(xx, "%02x", &x) != 1) { wprintw(self->window, "Invalid ID.\n"); return; } id_bin[i] = x; } num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); switch (num) { case -1: wprintw(self->window, "Message is too long.\n"); break; case -2: wprintw(self->window, "Please add a message to your request.\n"); break; case -3: wprintw(self->window, "That appears to be your own ID.\n"); break; case -4: wprintw(self->window, "Friend request already sent.\n"); break; case -5: wprintw(self->window, "[i] Undefined error when adding friend.\n"); break; default: wprintw(self->window, "Friend added as %d.\n", num); on_friendadded(num); break; } } else if(!strcmp(cmd, "help")) { print_usage(self); } else if(!strncmp(cmd, "status ", strlen("status "))) { char* msg; msg = strchr(cmd, ' '); if(msg == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } msg++; m_set_userstatus((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s\n", msg); } else if(!strncmp(cmd, "nick ", strlen("nick "))) { char* nick; nick = strchr(cmd, ' '); if(nick == NULL) { return; } nick++; setname((uint8_t*) nick, strlen(nick)+1); wprintw(self->window, "Nickname set to: %s.\n", nick); } else if(!strcmp(cmd, "myid")) { char id[32*2 + 1] = {0}; size_t i; for(i=0; i<32; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); strcat(id, xx); } wprintw(self->window, "%s\n", id); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; int num; id = strchr(cmd, ' '); if(id == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } id++; num = atoi(id); if(num >= num_requests) { wprintw(self->window, "Invalid syntax.\n"); return; } num = m_addfriend_norequest(pending_requests[num]); if(num == -1) { wprintw(self->window, "Failed to add friend.\n"); } else { wprintw(self->window, "Friend accepted as: %d.\n", num); on_friendadded(num); } } else if(!strncmp(cmd, "msg ", strlen("msg "))) { char* id; char* msg; id = strchr(cmd, ' '); if(id == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } id++; msg = strchr(id, ' '); if(msg == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } msg[0] = 0; msg++; if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) { wprintw(self->window, "Error occurred while sending message.\n"); } else { wprintw(self->window, "Message successfully sent.\n"); } } else { wprintw(self->window, "Invalid syntax.\n"); } }