static void prompt_onFriendRequest(ToxWindow *self, Tox *m, const uint8_t *key, const uint8_t *data, uint16_t length) { ChatContext *ctx = self->chatwin; uint8_t timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Friend request with the message '%s'", data); line_info_add(self, timefrmt, NULL, NULL, msg, SYS_MSG, 0, 0); write_to_log(msg, "", ctx->log, true); int n = add_friend_request(key); if (n == -1) { uint8_t *errmsg = "Friend request queue is full. Discarding request."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); write_to_log(errmsg, "", ctx->log, true); return; } snprintf(msg, sizeof(msg), "Type \"/accept %d\" to accept it.", n); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); alert_window(self, WINDOW_ALERT_1, true); }
void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { uint8_t *errmsg; if (get_num_active_windows() >= MAX_WINDOWS_NUM) { errmsg = " * Warning: Too many windows are open."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); return; } uint8_t *groupkey = friends[self->num].pending_groupchat; if (groupkey[0] == '\0') { errmsg = "No pending group chat invite."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } int groupnum = tox_join_groupchat(m, self->num, groupkey); if (groupnum == -1) { errmsg = "Group chat instance failed to initialize."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (init_groupchat_win(prompt, m, groupnum) == -1) { errmsg = "Group chat window failed to initialize."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); tox_del_groupchat(m, groupnum); return; } }
void cmd_join_group(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (get_num_active_windows() >= MAX_WINDOWS_NUM) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Warning: Too many windows are open."); return; } const char *groupkey = Friends.list[self->num].group_invite.key; uint16_t length = Friends.list[self->num].group_invite.length; if (!Friends.list[self->num].group_invite.pending) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending group chat invite."); return; } int groupnum = tox_join_groupchat(m, self->num, (uint8_t *) groupkey, length); if (groupnum == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat instance failed to initialize."); return; } if (init_groupchat_win(prompt, m, groupnum) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Group chat window failed to initialize."); tox_del_groupchat(m, groupnum); return; } }
void cmd_requests(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { if (FrndRequests.num_requests == 0) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "No pending friend requests."); return; } int i, j; int count = 0; for (i = 0; i < FrndRequests.max_idx; ++i) { if (!FrndRequests.request[i].active) continue; char id[TOX_CLIENT_ID_SIZE * 2 + 1] = {0}; for (j = 0; j < TOX_CLIENT_ID_SIZE; ++j) { char d[3]; snprintf(d, sizeof(d), "%02X", FrndRequests.request[i].key[j] & 0xff); strcat(id, d); } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%d : %s", i, id); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", FrndRequests.request[i].msg); if (++count < FrndRequests.num_requests) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, ""); } }
void cmd_nick(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *errmsg; /* check arguments */ if (argc < 1) { errmsg = "Invalid name."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } char *nick = argv[1]; int len = strlen(nick); if (nick[0] == '\"') { ++nick; len -= 2; nick[len] = '\0'; } if (!valid_nick(nick)) { errmsg = "Invalid name."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); nick[len] = '\0'; tox_set_name(m, (uint8_t *) nick, (uint16_t) len); prompt_update_nick(prompt, nick); store_data(m, DATA_FILE); }
void cmd_connect(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *errmsg; /* check arguments */ if (argc != 3) { errmsg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } const char *ip = argv[1]; const char *port = argv[2]; const char *key = argv[3]; if (atoi(port) == 0) { errmsg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } char *binary_string = hex_string_to_bin(key); tox_bootstrap_from_address(m, ip, TOX_ENABLE_IPV6_DEFAULT, htons(atoi(port)), (uint8_t *) binary_string); free(binary_string); }
void cmd_groupchat(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *errmsg; if (get_num_active_windows() >= MAX_WINDOWS_NUM) { errmsg = " * Warning: Too many windows are open."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); return; } int groupnum = tox_add_groupchat(m); if (groupnum == -1) { errmsg = "Group chat instance failed to initialize."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } if (init_groupchat_win(prompt, m, groupnum) == -1) { errmsg = "Group chat window failed to initialize."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); tox_del_groupchat(m, groupnum); return; } const char *msg = "Group chat created as %d."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg, groupnum); }
void cmd_groupinvite(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { uint8_t *errmsg; if (argc < 1) { errmsg = "Invalid syntax"; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } int groupnum = atoi(argv[1]); if (groupnum == 0 && strcmp(argv[1], "0")) { /* atoi returns 0 value on invalid input */ errmsg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } if (tox_invite_friend(m, self->num, groupnum) == -1) { errmsg = "Failed to invite friend."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Invited friend to Room #%d.", groupnum); line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); }
int start_transmission(ToxWindow *self) { if ( !ASettins.av || self->call_idx == -1 ) return -1; /* Don't provide support for video */ if ( 0 != toxav_prepare_transmission(ASettins.av, self->call_idx, av_jbufdc * 2, av_VADd, 0) ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Could not prepare transmission"); } if ( !toxav_capability_supported(ASettins.av, self->call_idx, AudioDecoding) || !toxav_capability_supported(ASettins.av, self->call_idx, AudioEncoding) ) return -1; set_call(&ASettins.calls[self->call_idx], _True); ToxAvCSettings csettings; toxav_get_peer_csettings(ASettins.av, self->call_idx, 0, &csettings); if ( open_primary_device(input, &ASettins.calls[self->call_idx].in_idx, csettings.audio_sample_rate, csettings.audio_frame_duration, csettings.audio_channels) != de_None ) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open input device!"); if ( register_device_callback(self->call_idx, ASettins.calls[self->call_idx].in_idx, read_device_callback, &self->call_idx, _True) != de_None) /* Set VAD as true for all; TODO: Make it more dynamic */ line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to register input handler!"); if ( open_primary_device(output, &ASettins.calls[self->call_idx].out_idx, csettings.audio_sample_rate, csettings.audio_frame_duration, csettings.audio_channels) != de_None ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open output device!"); ASettins.calls[self->call_idx].has_output = 0; } return 0; }
void cmd_run(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { FILE *fp; const char *error_str; cur_window = window; self_window = self; if (argc != 1) { if (argc < 1) { error_str = "Path must be specified."; } else { error_str = "Only one argument allowed."; } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, error_str); return; } fp = fopen(argv[1], "r"); if (fp == NULL) { error_str = "Path does not exist."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, error_str); return; } run_python(fp, argv[1]); fclose(fp); }
void cmd_nick(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, "Input required."); return; } char nick[MAX_STR_SIZE]; int len = 0; if (argv[1][0] == '\"') { /* remove opening and closing quotes */ snprintf(nick, sizeof(nick), "%s", &argv[1][1]); len = strlen(nick) - 1; nick[len] = '\0'; } else { snprintf(nick, sizeof(nick), "%s", argv[1]); len = strlen(nick); } if (!valid_nick(nick)) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid name."); return; } len = MIN(len, TOXIC_MAX_NAME_LENGTH - 1); nick[len] = '\0'; tox_set_name(m, (uint8_t *) nick, (uint16_t) len); prompt_update_nick(prompt, nick); store_data(m, DATA_FILE); }
int start_transmission(ToxWindow *self, Call *call) { if ( !self || !CallControl.av ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to prepare transmission"); return -1; } if (set_call(call, true) == -1) return -1; DeviceError error = open_primary_device(input, &call->in_idx, CallControl.audio_sample_rate, CallControl.audio_frame_duration, CallControl.audio_channels); if ( error != de_None ) { if ( error == de_FailedStart) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to start input device"); if ( error == de_InternalError ) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Internal error with opening input device"); } if ( register_device_callback(self->num, call->in_idx, read_device_callback, &self->num, true) != de_None) /* Set VAD as true for all; TODO: Make it more dynamic */ line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to register input handler!"); if ( open_primary_device(output, &call->out_idx, CallControl.audio_sample_rate, CallControl.audio_frame_duration, CallControl.audio_channels) != de_None ) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to open output device!"); call->has_output = 0; } return 0; }
static void friendlist_onAv(ToxWindow *self, ToxAv *av, int call_index) { int id = toxav_get_peer_id(av, call_index, 0); if ( id != av_ErrorUnknown && id >= Friends.max_idx) return; Tox *m = toxav_get_tox(av); if (Friends.list[id].chatwin == -1) { if (get_num_active_windows() < MAX_WINDOWS_NUM) { if (toxav_get_call_state(av, call_index) == av_CallStarting) { /* Only open windows when call is incoming */ Friends.list[id].chatwin = add_window(m, new_chat(m, Friends.list[id].num)); } } else { char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(m, nick, Friends.list[id].num); line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick); const char *errmsg = "* Warning: Too many windows are open."; line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); sound_notify(prompt, error, NT_WNDALERT_1, NULL); } } }
static void prompt_onConnectionChange(ToxWindow *self, Tox *m, int32_t friendnum , uint8_t status) { if (friendnum < 0) return; ChatContext *ctx = self->chatwin; uint8_t nick[TOX_MAX_NAME_LENGTH] = {0}; int n_len = tox_get_name(m, friendnum, nick); n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1); if (!nick[0]) { snprintf(nick, sizeof(nick), "%s", UNKNOWN_NAME); n_len = strlen(UNKNOWN_NAME); } nick[n_len] = '\0'; uint8_t timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); uint8_t *msg; if (status == 1) { msg = "has come online"; line_info_add(self, timefrmt, nick, NULL, msg, CONNECTION, 0, GREEN); write_to_log(msg, nick, ctx->log, true); alert_window(self, WINDOW_ALERT_2, false); } else { msg = "has gone offline"; line_info_add(self, timefrmt, nick, NULL, msg, CONNECTION, 0, RED); write_to_log(msg, nick, ctx->log, true); } }
static void friendlist_onAV(ToxWindow *self, ToxAV *av, uint32_t friend_number, int state) { assert(0); if( friend_number >= Friends.max_idx) return; assert(0); Tox *m = toxav_get_tox(av); if (Friends.list[friend_number].chatwin == -1) { if (get_num_active_windows() < MAX_WINDOWS_NUM) { if(state != TOXAV_FRIEND_CALL_STATE_FINISHED) { Friends.list[friend_number].chatwin = add_window(m, new_chat(m, Friends.list[friend_number].num)); set_active_window(Friends.list[friend_number].chatwin); } } else { char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(m, nick, Friends.list[friend_number].num); line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, 0, "Audio action from: %s!", nick); const char *errmsg = "* Warning: Too many windows are open."; line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, errmsg); sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); } } }
static void friendlist_onMessage(ToxWindow *self, Tox *m, int32_t num, uint8_t *str, uint16_t len) { if (num >= max_friends_index) return; if (friends[num].chatwin == -1) { if (get_num_active_windows() < MAX_WINDOWS_NUM) { friends[num].chatwin = add_window(m, new_chat(m, friends[num].num)); } else { str[len] = '\0'; uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; int n_len = tox_get_name(m, num, nick); n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1); nick[n_len] = '\0'; uint8_t timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt); line_info_add(prompt, timefrmt, nick, NULL, str, IN_MSG, 0, 0); uint8_t *msg = "* Warning: Too many windows are open."; line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_1, true); } } }
static void friendlist_onAv(ToxWindow *self, ToxAv *av, int call_index) { int id = toxav_get_peer_id(av, call_index, 0); /*id++;*/ if ( id != ErrorInternal && id >= max_friends_index) return; Tox *m = toxav_get_tox(av); if (friends[id].chatwin == -1) { if (get_num_active_windows() < MAX_WINDOWS_NUM) { friends[id].chatwin = add_window(m, new_chat(m, friends[id].num)); } else { uint8_t nick[TOX_MAX_NAME_LENGTH] = {'\0'}; int n_len = tox_get_name(m, id, nick); n_len = MIN(n_len, TOXIC_MAX_NAME_LENGTH - 1); nick[n_len] = '\0'; uint8_t msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "Audio action from: %s!", nick); line_info_add(prompt, NULL, NULL, NULL, msg, SYS_MSG, 0, 0); uint8_t *errmsg = "* Warning: Too many windows are open."; line_info_add(prompt, NULL, NULL, NULL, errmsg, SYS_MSG, 0, RED); alert_window(prompt, WINDOW_ALERT_0, true); } } }
static void friendlist_onMessage(ToxWindow *self, Tox *m, uint32_t num, Tox_Message_Type type, const char *str, size_t length) { if (num >= Friends.max_idx) { return; } if (Friends.list[num].chatwin != -1) { return; } if (get_num_active_windows() < MAX_WINDOWS_NUM) { Friends.list[num].chatwin = add_window(m, new_chat(m, Friends.list[num].num)); return; } char nick[TOX_MAX_NAME_LENGTH]; get_nick_truncate(m, nick, num); char timefrmt[TIME_STR_SIZE]; get_time_str(timefrmt, sizeof(timefrmt)); line_info_add(prompt, timefrmt, nick, NULL, IN_MSG, 0, 0, "%s", str); line_info_add(prompt, NULL, NULL, NULL, SYS_MSG, 0, RED, "* Warning: Too many windows are open."); sound_notify(prompt, notif_error, NT_WNDALERT_1, NULL); }
void cmd_decline(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; } 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(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char *errmsg; if (argc < 1) { errmsg = "Invalid syntax."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } char *id = argv[1]; char msg[MAX_STR_SIZE]; if (argc > 1) { char *temp = argv[2]; if (temp[0] != '\"') { errmsg = "Message must be enclosed in quotes."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } ++temp; temp[strlen(temp) - 1] = '\0'; snprintf(msg, sizeof(msg), "%s", temp); } else { char selfname[TOX_MAX_NAME_LENGTH]; uint16_t n_len = tox_get_self_name(m, (uint8_t *) selfname); selfname[n_len] = '\0'; snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname); } char id_bin[TOX_FRIEND_ADDRESS_SIZE] = {0}; uint16_t id_len = (uint16_t) strlen(id); /* try to add tox ID */ if (id_len == 2 * TOX_FRIEND_ADDRESS_SIZE) { size_t i; char xx[3]; uint32_t x; 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) { errmsg = "Invalid ID."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); return; } id_bin[i] = x; } cmd_add_helper(self, m, id_bin, msg); } else { /* assume id is a username@domain address and do DNS lookup */ dns3_lookup(self, m, id_bin, id, msg); } }
static void chat_onFileControl(ToxWindow *self, Tox *m, int32_t num, uint8_t receive_send, uint8_t filenum, uint8_t control_type, const char *data, uint16_t length) { if (self->num != num) return; const char *filename; char msg[MAX_STR_SIZE] = {0}; int i = 0; /* file_sender index */ if (receive_send == 0) { filename = friends[num].file_receiver.filenames[filenum]; } else { for (i = 0; i < MAX_FILES; ++i) { if (file_senders[i].filenum == filenum) break; } filename = file_senders[i].pathname; } switch (control_type) { case TOX_FILECONTROL_ACCEPT: if (receive_send == 1) { const char *r_msg = "File transfer for '%s' accepted."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, r_msg, filename); /* prep progress bar line */ char progline[MAX_STR_SIZE]; prep_prog_line(progline); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, progline); file_senders[i].line_id = self->chatwin->hst->line_end->id + 2; notify(self, silent, NT_NOFOCUS | NT_BEEP | NT_WNDALERT_2); } break; case TOX_FILECONTROL_KILL: snprintf(msg, sizeof(msg), "File transfer for '%s' failed.", filename); if (receive_send == 0) chat_close_file_receiver(num, filenum); notify(self, error, NT_NOFOCUS | NT_WNDALERT_2); break; case TOX_FILECONTROL_FINISHED: if (receive_send == 0) { snprintf(msg, sizeof(msg), "File transfer for '%s' complete.", filename); chat_close_file_receiver(num, filenum); notify(self, transfer_completed, NT_NOFOCUS | NT_WNDALERT_2); } break; } if (msg[0]) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); }
void cmd_nospam(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { uint32_t nospam = rand(); /* should be random enough */ tox_self_set_nospam(m, nospam); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Your Tox ID has been changed to:"); cmd_myid(window, self, m, 0, NULL); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Any services that relied on your old ID will need to be updated manually."); }
void cmd_add(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, "Tox ID or address required."); return; } const char *id = argv[1]; char msg[MAX_STR_SIZE]; if (argc > 1) { if (argv[2][0] != '\"') { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Message must be enclosed in quotes."); return; } /* remove opening and closing quotes */ char tmp[MAX_STR_SIZE]; snprintf(tmp, sizeof(tmp), "%s", &argv[2][1]); int len = strlen(tmp) - 1; tmp[len] = '\0'; snprintf(msg, sizeof(msg), "%s", tmp); } else { char selfname[TOX_MAX_NAME_LENGTH]; tox_self_get_name(m, (uint8_t *) selfname); size_t n_len = tox_self_get_name_size(m); selfname[n_len] = '\0'; snprintf(msg, sizeof(msg), "Hello, my name is %s. Care to Tox?", selfname); } char id_bin[TOX_ADDRESS_SIZE] = {0}; uint16_t id_len = (uint16_t) strlen(id); /* try to add tox ID */ if (id_len == 2 * TOX_ADDRESS_SIZE) { size_t i; char xx[3]; uint32_t x; for (i = 0; i < TOX_ADDRESS_SIZE; ++i) { xx[0] = id[2 * i]; xx[1] = id[2 * i + 1]; xx[2] = '\0'; if (sscanf(xx, "%02x", &x) != 1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid Tox ID."); return; } id_bin[i] = x; } cmd_add_helper(self, m, id_bin, msg); } else { /* assume id is a username@domain address and do DNS lookup */ dns3_lookup(self, m, id_bin, id, msg); } }
/* Loads previous history from chat log */ void load_chat_history(ToxWindow *self, struct chatlog *log) { if (log->file == NULL) return; off_t sz = file_size(log->path); if (sz <= 0) return; char *hstbuf = malloc(sz); if (hstbuf == NULL) exit_toxic_err("failed in load_chat_history", FATALERR_MEMORY); if (fseek(log->file, 0L, SEEK_SET) == -1) { free(hstbuf); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to read log file"); return; } if (fread(hstbuf, sz, 1, log->file) != 1) { free(hstbuf); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, RED, " * Failed to read log file"); return; } /* Number of history lines to load: must not be larger than MAX_LINE_INFO_QUEUE - 2 */ int L = MIN(MAX_LINE_INFO_QUEUE - 2, user_settings->history_size); int start, count = 0; /* start at end and backtrace L lines or to the beginning of buffer */ for (start = sz - 1; start >= 0 && count < L; --start) { if (hstbuf[start] == '\n') ++count; } const char *line = strtok(&hstbuf[start + 1], "\n"); if (line == NULL) { free(hstbuf); return; } while (line != NULL && count--) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", line); line = strtok(NULL, "\n"); } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, ""); free(hstbuf); }
static void print_matches(ToxWindow *self, Tox *m, const void *list, int n_items, int size) { if (m) execute(self->chatwin->history, self, m, "/clear", GLOBAL_COMMAND_MODE); const char *L = (char *) list; int i; for (i = 0; i < n_items; ++i) line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", &L[i * size]); line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, ""); /* formatting */ }
void cmd_myid(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { char id_string[TOX_ADDRESS_SIZE * 2 + 1]; char bin_id[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) bin_id); if (bin_id_to_string(bin_id, sizeof(bin_id), id_string, sizeof(id_string)) == -1) { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Failed to print ID."); return; } line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", id_string); }
void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { bool have_note = false; const char *errmsg; lock_status (); if (argc >= 2) { have_note = true; } else if (argc < 1) { errmsg = "Require a status. Statuses are: online, busy and away."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); goto finish; } const char *status_str = argv[1]; TOX_USER_STATUS status; if (!strcasecmp(status_str, "online")) status = TOX_USER_STATUS_NONE; else if (!strcasecmp(status_str, "away")) status = TOX_USER_STATUS_AWAY; else if (!strcasecmp(status_str, "busy")) status = TOX_USER_STATUS_BUSY; else { errmsg = "Invalid status. Valid statuses are: online, busy and away."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg); goto finish; } tox_self_set_status(m, status); prompt_update_status(prompt, status); if (have_note) { if (argv[2][0] != '\"') { line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Note must be enclosed in quotes."); goto finish; } /* remove opening and closing quotes */ char msg[MAX_STR_SIZE]; snprintf(msg, sizeof(msg), "%s", &argv[2][1]); int len = strlen(msg) - 1; msg[len] = '\0'; prompt_update_statusmessage(prompt, m, msg); } finish: unlock_status (); }
void cmd_status(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { uint8_t *msg = NULL; uint8_t *errmsg; if (argc >= 2) { msg = argv[2]; if (msg[0] != '\"') { errmsg = "Note must be enclosed in quotes."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } } else if (argc != 1) { errmsg = "Wrong number of arguments."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } char *status = argv[1]; int len = strlen(status); char l_status[len + 1]; int i; for (i = 0; i <= len; ++i) l_status[i] = tolower(status[i]); TOX_USERSTATUS status_kind; if (!strcmp(l_status, "online")) status_kind = TOX_USERSTATUS_NONE; else if (!strcmp(l_status, "away")) status_kind = TOX_USERSTATUS_AWAY; else if (!strcmp(l_status, "busy")) status_kind = TOX_USERSTATUS_BUSY; else { errmsg = "Invalid status. Valid statuses are: online, busy and away."; line_info_add(self, NULL, NULL, NULL, errmsg, SYS_MSG, 0, 0); return; } tox_set_user_status(m, status_kind); prompt_update_status(prompt, status_kind); if (msg != NULL) { msg[strlen(++msg) - 1] = L'\0'; /* remove opening and closing quotes */ uint16_t len = strlen(msg); tox_set_status_message(m, msg, len); prompt_update_statusmessage(prompt, msg, len); } }
void cmd_log(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { const char *msg; struct chatlog *log = self->chatwin->log; if (argc == 0) { if (log->log_on) msg = "Logging for this window is ON; type \"/log off\" to disable. (Logs are not encrypted)"; else msg = "Logging for this window is OFF; type \"/log on\" to enable."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } const char *swch = argv[1]; if (!strcmp(swch, "1") || !strcmp(swch, "on")) { char myid[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) myid); int log_ret = -1; if (self->is_chat) { Friends.list[self->num].logging_on = true; log_ret = log_enable(self->name, myid, Friends.list[self->num].pub_key, log, LOG_CHAT); } else if (self->is_prompt) { log_ret = log_enable(self->name, myid, NULL, log, LOG_PROMPT); } else if (self->is_groupchat) { log_ret = log_enable(self->name, myid, NULL, log, LOG_GROUP); } msg = log_ret == 0 ? "Logging enabled." : "Warning: Log failed to initialize."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } else if (!strcmp(swch, "0") || !strcmp(swch, "off")) { if (self->is_chat) Friends.list[self->num].logging_on = false; log_disable(log); msg = "Logging disabled."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); return; } msg = "Invalid option. Use \"/log on\" and \"/log off\" to toggle logging."; line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, msg); }
void cmd_prompt_help(WINDOW *window, ToxWindow *self, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]) { struct history *hst = self->chatwin->hst; line_info_clear(hst); struct line_info *start = hst->line_start; uint8_t *msg = "Global commands:"; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); #ifdef _SUPPORT_AUDIO #define NUMLINES 14 #else #define NUMLINES 12 #endif uint8_t lines[NUMLINES][MAX_STR_SIZE] = { { " /add <id> <msg> : Add friend with optional message" }, { " /accept <n> : Accept friend request" }, { " /connect <ip> <port> <key> : Manually connect to a DHT node" }, { " /status <type> <msg> : Set status with optional note" }, { " /note <msg> : Set a personal note" }, { " /nick <nick> : Set your nickname" }, { " /log <on> or <off> : Enable/disable logging" }, { " /groupchat : Create a group chat" }, { " /myid : Print your ID" }, { " /help : Print this message again" }, { " /clear : Clear window history" }, { " /quit or /exit : Exit Toxic" }, #ifdef _SUPPORT_AUDIO { " /lsdev <type> : List devices where type: in|out" }, { " /sdev <type> <id> : Set active device" }, #endif /* _SUPPORT_AUDIO */ }; int i; for (i = 0; i < NUMLINES; ++i) line_info_add(self, NULL, NULL, NULL, lines[i], SYS_MSG, 0, 0); msg = " * Argument messages must be enclosed in quotation marks."; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); msg = " * Use ctrl-o and ctrl-p to navigate through the tabs."; line_info_add(self, NULL, NULL, NULL, msg, SYS_MSG, 1, CYAN); line_info_add(self, NULL, NULL, NULL, "", SYS_MSG, 0, 0); hst->line_start = start; }