END_TEST START_TEST(test_m_addfriend) { char *good_data = "test"; char *bad_data = ""; int good_len = strlen(good_data); int bad_len = strlen(bad_data); int really_bad_len = (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES + 100); if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, really_bad_len) != FAERR_TOOLONG) ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", really_bad_len); /* this will error if the original m_addfriend_norequest() failed */ if(m_addfriend((uint8_t *)friend_id, (uint8_t *)good_data, good_len) != FAERR_ALREADYSENT) ck_abort_msg("m_addfriend did NOT catch adding a friend we already have.\n" "(this can be caused by the error of m_addfriend_norequest in" " the beginning of the suite)\n"); if(m_addfriend((uint8_t *)good_id_b, (uint8_t *)bad_data, bad_len) != FAERR_NOMESSAGE) ck_abort_msg("m_addfriend did NOT catch the following length: %d\n", bad_len); /* this should REALLY error */ /* * TODO: validate client_id in m_addfriend? if(m_addfriend((uint8_t *)bad_id, (uint8_t *)good_data, good_len) >= 0) ck_abort_msg("The following ID passed through " "m_addfriend without an error:\n'%s'\n", bad_id_str); */ }
int parent_friend_request(void) { char *message = "Watson, come here, I need you."; int len = strlen(message); int i = 0; fputs("Sending child request.", stdout); fflush(stdout); m_addfriend(child_id, (uint8_t *)message, len); /* wait on the status change */ for(i = 0; i < WAIT_COUNT; i++) { do_tox(); if(request_flags & FIRST_FLAG) break; fputs(".", stdout); fflush(stdout); c_sleep(WAIT_TIME); } if(!(request_flags & FIRST_FLAG)) { fputs("\nfriends_test: The child took to long to respond!\n" "Friend requests may be broken, failing build!\n", stderr); kill(child_pid, SIGKILL); return -1; } return 0; }
void add_friend() { 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); ++maxnumfriends; } 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"); }
void cmd_add(ToxWindow *self, Messenger *m, char **args) { uint8_t id_bin[KEY_SIZE_BYTES]; char xx[3]; uint32_t x; char *id = args[1]; char *msg = args[2]; if (!id) { wprintw(self->window, "Invalid command: add expected at least one argument.\n"); return; } if (!msg) msg = ""; if (strlen(id) != 2*KEY_SIZE_BYTES) { wprintw(self->window, "Invalid ID length.\n"); return; } int i; for (i = 0; i < KEY_SIZE_BYTES; ++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; } int num = m_addfriend(m, id_bin, (uint8_t*) msg, strlen(msg)+1); switch (num) { case FAERR_TOOLONG: wprintw(self->window, "Message is too long.\n"); break; case FAERR_NOMESSAGE: wprintw(self->window, "Please add a message to your request.\n"); break; case FAERR_OWNKEY: wprintw(self->window, "That appears to be your own ID.\n"); break; case FAERR_ALREADYSENT: wprintw(self->window, "Friend request already sent.\n"); break; case FAERR_UNKNOWN: wprintw(self->window, "Undefined error when adding friend.\n"); break; default: wprintw(self->window, "Friend added as %d.\n", num); on_friendadded(num); break; } }
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); } }
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* 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 } }
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); }
/* * Add a friend. * Set the data that will be sent along with friend request. * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. * data is the data and length is the length. * * return the friend number if success. * return FA_TOOLONG if message length is too long. * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). * return FAERR_OWNKEY if user's own key. * return FAERR_ALREADYSENT if friend request already sent or already a friend. * return FAERR_UNKNOWN for unknown error. * return FAERR_BADCHECKSUM if bad checksum in address. * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. * (the nospam for that friend was set to the new one). * return FAERR_NOMEM if increasing the friend list size fails. */ int tox_addfriend(void *tox, uint8_t *address, uint8_t *data, uint16_t length) { Messenger *m = tox; return m_addfriend(m, address, data, length); }
/* * Add a friend. * Set the data that will be sent along with friend request. * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. * data is the data and length is the length. * * return the friend number if success. * return FA_TOOLONG if message length is too long. * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). * return FAERR_OWNKEY if user's own key. * return FAERR_ALREADYSENT if friend request already sent or already a friend. * return FAERR_UNKNOWN for unknown error. * return FAERR_BADCHECKSUM if bad checksum in address. * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. * (the nospam for that friend was set to the new one). * return FAERR_NOMEM if increasing the friend list size fails. */ int32_t tox_add_friend(Tox *tox, const uint8_t *address, const uint8_t *data, uint16_t length) { Messenger *m = tox; return m_addfriend(m, address, data, length); }
/* * Add a friend. * Set the data that will be sent along with friend request. * address is the address of the friend (returned by getaddress of the friend you wish to add) it must be FRIEND_ADDRESS_SIZE bytes. TODO: add checksum. * data is the data and length is the length. * * return the friend number if success. * return FA_TOOLONG if message length is too long. * return FAERR_NOMESSAGE if no message (message length must be >= 1 byte). * return FAERR_OWNKEY if user's own key. * return FAERR_ALREADYSENT if friend request already sent or already a friend. * return FAERR_UNKNOWN for unknown error. * return FAERR_BADCHECKSUM if bad checksum in address. * return FAERR_SETNEWNOSPAM if the friend was already there but the nospam was different. * (the nospam for that friend was set to the new one). * return FAERR_NOMEM if increasing the friend list size fails. */ int tox_add_friend(Tox *tox, size_t *address, size_t *data, size_t length) { Messenger *m = tox; return m_addfriend(m, address, data, length); }
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); }
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); } }
void cmd_add(ToxWindow *self, Tox *m, char **args) { uint8_t id_bin[TOX_FRIEND_ADDRESS_SIZE]; char xx[3]; uint32_t x; char *id = args[1]; char *msg = args[2]; if (!id) { wprintw(self->window, "Invalid command: add expected at least one argument.\n"); return; } if (!msg) msg = ""; if (strlen(id) != 2 * TOX_FRIEND_ADDRESS_SIZE) { wprintw(self->window, "Invalid ID length.\n"); return; } int i; for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; ++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; } for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { id[i] = toupper(id[i]); } int num = m_addfriend(m, id_bin, (uint8_t *) msg, strlen(msg) + 1); switch (num) { case TOX_FAERR_TOOLONG: wprintw(self->window, "Message is too long.\n"); break; case TOX_FAERR_NOMESSAGE: wprintw(self->window, "Please add a message to your request.\n"); break; case TOX_FAERR_OWNKEY: wprintw(self->window, "That appears to be your own ID.\n"); break; case TOX_FAERR_ALREADYSENT: wprintw(self->window, "Friend request already sent.\n"); break; case TOX_FAERR_UNKNOWN: wprintw(self->window, "Undefined error when adding friend.\n"); break; case TOX_FAERR_BADCHECKSUM: wprintw(self->window, "Bad checksum in address.\n"); break; case TOX_FAERR_SETNEWNOSPAM: wprintw(self->window, "Nospam was different.\n"); break; default: wprintw(self->window, "Friend added as %d.\n", num); on_friendadded(m, num); break; } }
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"); } } }
/* load the messenger from data of size length. */ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) { if (length == ~0) return -1; if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3) return -1; length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 3; load_keys(data); data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; uint32_t nospam; memcpy(&nospam, data, sizeof(nospam)); set_nospam(nospam); data += sizeof(nospam); uint32_t size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size) return -1; length -= size; if (DHT_load(data, size) == -1) return -1; data += size; memcpy(&size, data, sizeof(size)); data += sizeof(size); if (length < size || size % sizeof(Friend) != 0) return -1; Friend *temp = malloc(size); memcpy(temp, data, size); uint16_t num = size / sizeof(Friend); uint32_t i; for (i = 0; i < num; ++i) { if (temp[i].status >= 3) { int fnum = m_addfriend_norequest(m, temp[i].client_id); setfriendname(m, fnum, temp[i].name); /* set_friend_statusmessage(fnum, temp[i].statusmessage, temp[i].statusmessage_length); */ } else if (temp[i].status != 0) { /* TODO: this is not a good way to do this. */ uint8_t address[FRIEND_ADDRESS_SIZE]; memcpy(address, temp[i].client_id, crypto_box_PUBLICKEYBYTES); memcpy(address + crypto_box_PUBLICKEYBYTES, &(temp[i].friendrequest_nospam), sizeof(uint32_t)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + crypto_box_PUBLICKEYBYTES + sizeof(uint32_t), &checksum, sizeof(checksum)); m_addfriend(m, address, temp[i].info, temp[i].info_size); } } free(temp); data += size; length -= size; uint16_t small_size; if (length < sizeof(small_size)) return -1; memcpy(&small_size, data, sizeof(small_size)); data += sizeof(small_size); length -= sizeof(small_size); if (length != small_size) return -1; setname(m, data, small_size); return 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); } }
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"); } }