static void load_data(Tox *m, char *path) { if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ return; FILE *fd; size_t len; uint8_t *buf; if ((fd = fopen(path, "r")) != NULL) { fseek(fd, 0, SEEK_END); len = ftell(fd); fseek(fd, 0, SEEK_SET); buf = malloc(len); if (buf == NULL) { fclose(fd); endwin(); fprintf(stderr, "malloc() failed. Aborting...\n"); exit(EXIT_FAILURE); } if (fread(buf, len, 1, fd) != 1) { free(buf); fclose(fd); endwin(); fprintf(stderr, "fread() failed. Aborting...\n"); exit(EXIT_FAILURE); } tox_load(m, buf, len); uint32_t i = 0; uint8_t name[TOX_MAX_NAME_LENGTH]; while (tox_getname(m, i, name) != -1) { on_friendadded(m, i); i++; } free(buf); fclose(fd); } else { int st; if ((st = store_data(m, path)) != 0) { endwin(); fprintf(stderr, "Store messenger failed with return code: %d\n", st); exit(EXIT_FAILURE); } } }
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; } }
static void load_data(Messenger *m, char *path) { if (f_loadfromfile == 0) /*If file loading/saving is disabled*/ return; FILE *fd; size_t len; uint8_t *buf; if ((fd = fopen(path, "r")) != NULL) { fseek(fd, 0, SEEK_END); len = ftell(fd); fseek(fd, 0, SEEK_SET); buf = malloc(len); if (buf == NULL) { fprintf(stderr, "malloc() failed.\n"); fclose(fd); endwin(); exit(1); } if (fread(buf, len, 1, fd) != 1) { fprintf(stderr, "fread() failed.\n"); free(buf); fclose(fd); endwin(); exit(1); } Messenger_load(m, buf, len); uint32_t i; for (i = 0; i < m->numfriends; i++) { on_friendadded(m, i); } free(buf); fclose(fd); } else { int st; if ((st = store_data(m, path)) != 0) { fprintf(stderr, "Store messenger failed with return code: %d\n", st); endwin(); exit(1); } } }
void cmd_add_helper(ToxWindow *self, Tox *m, const char *id_bin, const char *msg) { const char *errmsg; TOX_ERR_FRIEND_ADD err; uint32_t f_num = tox_friend_add(m, (uint8_t *) id_bin, (uint8_t *) msg, strlen(msg), &err); switch (err) { case TOX_ERR_FRIEND_ADD_TOO_LONG: errmsg = "Message is too long."; break; case TOX_ERR_FRIEND_ADD_NO_MESSAGE: errmsg = "Please add a message to your request."; break; case TOX_ERR_FRIEND_ADD_OWN_KEY: errmsg = "That appears to be your own ID."; break; case TOX_ERR_FRIEND_ADD_ALREADY_SENT: errmsg = "Friend request has already been sent."; break; case TOX_ERR_FRIEND_ADD_BAD_CHECKSUM: errmsg = "Bad checksum in address."; break; case TOX_ERR_FRIEND_ADD_SET_NEW_NOSPAM: errmsg = "Nospam was different."; break; case TOX_ERR_FRIEND_ADD_MALLOC: errmsg = "Core memory allocation failed."; break; case TOX_ERR_FRIEND_ADD_OK: errmsg = "Friend request sent."; on_friendadded(m, f_num, true); break; case TOX_ERR_FRIEND_ADD_NULL: /* fallthrough */ default: errmsg = "Faile to add friend: Unknown error."; break; } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); }
void cmd_accept(ToxWindow *self, Messenger *m, char **args) { int num = atoi(args[1]); if (num >= num_requests) { wprintw(self->window, "Invalid syntax.\n"); return; } num = m_addfriend_norequest(m, 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); } }
/* command functions */ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *msg; if (argc != 1) { msg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } int req = atoi(argv[1]); if ((req == 0 && strcmp(argv[1], "0")) || req >= MAX_FRIENDS_NUM) { msg = "No pending friend request with that number."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } if (!strlen(pending_frnd_requests[req])) { msg = "No pending friend request with that number."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } int32_t friendnum = tox_add_friend_norequest(m, (uint8_t *) pending_frnd_requests[req]); if (friendnum == -1) msg = "Failed to add friend."; else { msg = "Friend request accepted."; on_friendadded(m, friendnum, true); } memset(&pending_frnd_requests[req], 0, TOX_CLIENT_ID_SIZE); int i; for (i = num_frnd_requests; i > 0; --i) { if (!strlen(pending_frnd_requests[i - 1])) break; } num_frnd_requests = i; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); }
/* command functions */ void cmd_accept(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (argc < 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Request ID required."); return; } int req = atoi(argv[1]); if ((req == 0 && strcmp(argv[1], "0")) || req < 0 || req > MAX_FRIEND_REQUESTS) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); return; } if (!FrndRequests.request[req].active) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend request with that ID."); return; } TOX_ERR_FRIEND_ADD err; uint32_t friendnum = tox_friend_add_norequest(m, FrndRequests.request[req].key, &err); if (err != TOX_ERR_FRIEND_ADD_OK) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to add friend (error %d\n)", err); return; } else { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Friend request accepted."); on_friendadded(m, friendnum, true); } memset(&FrndRequests.request[req], 0, sizeof(struct friend_request)); int i; for (i = FrndRequests.max_idx; i > 0; --i) { if (FrndRequests.request[i - 1].active) break; } FrndRequests.max_idx = i; --FrndRequests.num_requests; }
void cmd_add_helper(ToxWindow *self, Tox *m, char *id_bin, char *msg) { const char *errmsg; int32_t f_num = tox_add_friend(m, (uint8_t *) id_bin, (uint8_t *) msg, (uint16_t) strlen(msg)); switch (f_num) { case TOX_FAERR_TOOLONG: errmsg = "Message is too long."; break; case TOX_FAERR_NOMESSAGE: errmsg = "Please add a message to your request."; break; case TOX_FAERR_OWNKEY: errmsg = "That appears to be your own ID."; break; case TOX_FAERR_ALREADYSENT: errmsg = "Friend request has already been sent."; break; case TOX_FAERR_UNKNOWN: errmsg = "Undefined error when adding friend."; break; case TOX_FAERR_BADCHECKSUM: errmsg = "Bad checksum in address."; break; case TOX_FAERR_SETNEWNOSPAM: errmsg = "Nospam was different."; break; default: errmsg = "Friend request sent."; on_friendadded(m, f_num, true); break; } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); }
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) { 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"); } }