static void cmd_info(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { char outmsg[MAX_COMMAND_LENGTH]; char timestr[64]; uint64_t curtime = (uint64_t) time(NULL); get_elapsed_time_str(timestr, sizeof(timestr), curtime - Tox_Bot.start_time); snprintf(outmsg, sizeof(outmsg), "Uptime: %s", timestr); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); uint32_t numfriends = tox_self_get_friend_list_size(m); snprintf(outmsg, sizeof(outmsg), "Friends: %d (%d online)", numfriends, Tox_Bot.num_online_friends); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); snprintf(outmsg, sizeof(outmsg), "Inactive friends are purged after %"PRIu64" days", Tox_Bot.inactive_limit / SECONDS_IN_DAY); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); /* List active group chats and number of peers in each */ size_t num_chats = tox_conference_get_chatlist_size(m); if (num_chats == 0) { tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) "No active groupchats", strlen("No active groupchats"), NULL); return; } uint32_t groupchat_list[num_chats]; tox_conference_get_chatlist(m, groupchat_list); uint32_t i; for (i = 0; i < num_chats; ++i) { TOX_ERR_CONFERENCE_PEER_QUERY err; uint32_t groupnum = groupchat_list[i]; uint32_t num_peers = tox_conference_peer_count(m, groupnum, &err); if (err == TOX_ERR_CONFERENCE_PEER_QUERY_OK) { int idx = group_index(groupnum); const char *title = Tox_Bot.g_chats[idx].title_len ? Tox_Bot.g_chats[idx].title : "None"; const char *type = tox_conference_get_type(m, groupnum, NULL) == TOX_CONFERENCE_TYPE_AV ? "Audio" : "Text"; snprintf(outmsg, sizeof(outmsg), "Group %d | %s | peers: %d | Title: %s", groupnum, type, num_peers, title); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); } } }
int Core::sendAction(uint32_t friendId, const QString &action) { QMutexLocker ml(&messageSendMutex); CString cMessage(action); int receipt = tox_friend_send_message(tox, friendId, TOX_MESSAGE_TYPE_ACTION, cMessage.data(), cMessage.size(), nullptr); emit messageSentResult(friendId, action, receipt); return receipt; }
int Core::sendMessage(uint32_t friendId, const QString& message) { QMutexLocker ml(&messageSendMutex); CString cMessage(message); int receipt = tox_friend_send_message(tox, friendId, TOX_MESSAGE_TYPE_NORMAL, cMessage.data(), cMessage.size(), nullptr); emit messageSentResult(friendId, message, receipt); return receipt; }
static void cmd_master(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: Tox ID required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } const char *id = argv[1]; if (strlen(id) != TOX_ADDRESS_SIZE * 2) { outmsg = "Error: Invalid Tox ID"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } FILE *fp = fopen(MASTERLIST_FILE, "a"); if (fp == NULL) { outmsg = "Error: could not find masterkeys file"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } fprintf(fp, "%s\n", id); fclose(fp); char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t len = tox_friend_get_name_size(m, friendnum, NULL); name[len] = '\0'; printf("%s added master: %s\n", name, id); outmsg = "ID added to masterkeys list"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); }
static void cmd_leave(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: Group number required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } int groupnum = atoi(argv[1]); if (groupnum == 0 && strcmp(argv[1], "0")) { outmsg = "Error: Invalid group number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } if (!tox_conference_delete(m, groupnum, NULL)) { outmsg = "Error: Invalid group number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } char msg[MAX_COMMAND_LENGTH]; char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t len = tox_friend_get_name_size(m, friendnum, NULL); name[len] = '\0'; group_leave(groupnum); printf("Left group %d (%s)\n", groupnum, name); snprintf(msg, sizeof(msg), "Left group %d", groupnum); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) msg, strlen(msg), NULL); }
static void cmd_status(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: status required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } TOX_USER_STATUS type; const char *status = argv[1]; if (strcasecmp(status, "online") == 0) { type = TOX_USER_STATUS_NONE; } else if (strcasecmp(status, "away") == 0) { type = TOX_USER_STATUS_AWAY; } else if (strcasecmp(status, "busy") == 0) { type = TOX_USER_STATUS_BUSY; } else { outmsg = "Invalid status. Valid statuses are: online, busy and away."; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } tox_self_set_status(m, type); char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t nlen = tox_friend_get_name_size(m, friendnum, NULL); name[nlen] = '\0'; printf("%s set status to %s\n", name, status); save_data(m, DATA_FILE); }
static void handle_friend_message( Tox *tox, uint32_t friend_number, TOX_MESSAGE_TYPE type, const uint8_t *message, size_t length, void *user_data) { TOX_ERR_FRIEND_SEND_MESSAGE err_send; eprintf ("<%d> %s\n", friend_number, message); if (*message != ':') { return; } tox_self_set_typing(tox, friend_number, true, NULL); char *cmd = r_str_ndup (message, length); char *res = r_core_cmd_str (core, cmd + 1); if (res && *res) { const int MAXK = 1280; int bak = 0; int k = 0; char *r = res; while (1) { if (strlen (r) > MAXK) { k = MAXK; bak = r[k]; r[k] = 0; char *nl = r_str_rchr (r, NULL, '\n'); if (nl) { r[k] = bak; bak = *nl; *nl = 0; k = nl - r; } } else { bak = 0; } tox_friend_send_message (tox, friend_number, type, r, strlen (r), &err_send); // tox_friend_send_message (tox, friend_number, type, message, length, &err_send); if (err_send != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { fprintf(stderr, "unable to send message back to friend %d: %d\n", friend_number, err_send); } if (bak) { r[k] = bak; r += k; } else { break; } } } else { eprintf ("Invalid command?\n"); } free (res); free (cmd); tox_self_set_typing(tox, friend_number, false, NULL); }
static void cmd_statusmessage(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: message required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } if (argv[1][0] != '\"') { outmsg = "Error: message must be enclosed in quotes"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } /* remove opening and closing quotes */ char msg[MAX_COMMAND_LENGTH]; snprintf(msg, sizeof(msg), "%s", &argv[1][1]); int len = strlen(msg) - 1; msg[len] = '\0'; tox_self_set_status_message(m, (uint8_t *) msg, len, NULL); char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t nlen = tox_friend_get_name_size(m, friendnum, NULL); name[nlen] = '\0'; printf("%s set status message to \"%s\"\n", name, msg); save_data(m, DATA_FILE); }
static void cmd_purge(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: number > 0 required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } uint64_t days = (uint64_t) atoi(argv[1]); if (days <= 0) { outmsg = "Error: number > 0 required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } uint64_t seconds = days * SECONDS_IN_DAY; Tox_Bot.inactive_limit = seconds; char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t nlen = tox_friend_get_name_size(m, friendnum, NULL); name[nlen] = '\0'; char msg[MAX_COMMAND_LENGTH]; snprintf(msg, sizeof(msg), "Purge time set to %"PRIu64" days", days); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) msg, strlen(msg), NULL); printf("Purge time set to %"PRIu64" days by %s\n", days, name); }
static void cmd_default(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: Room number required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } int groupnum = atoi(argv[1]); if ((groupnum == 0 && strcmp(argv[1], "0")) || groupnum < 0) { outmsg = "Error: Invalid room number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } Tox_Bot.default_groupnum = groupnum; char msg[MAX_COMMAND_LENGTH]; snprintf(msg, sizeof(msg), "Default room number set to %d", groupnum); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) msg, strlen(msg), NULL); char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t len = tox_friend_get_name_size(m, friendnum, NULL); name[len] = '\0'; printf("Default room number set to %d by %s", groupnum, name); }
static void cb_friend_message(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length, void *userdata) { if (type != TOX_MESSAGE_TYPE_NORMAL) return; const char *outmsg; char message[TOX_MAX_MESSAGE_LENGTH]; length = copy_tox_str(message, sizeof(message), (const char *) string, length); message[length] = '\0'; if (length && execute(m, friendnumber, message, length) == -1) { outmsg = "Invalid command. Type help for a list of commands"; tox_friend_send_message(m, friendnumber, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); } }
static void cmd_id(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { char outmsg[TOX_ADDRESS_SIZE * 2 + 1]; char address[TOX_ADDRESS_SIZE]; tox_self_get_address(m, (uint8_t *) address); int i; for (i = 0; i < TOX_ADDRESS_SIZE; ++i) { char d[3]; sprintf(d, "%02X", address[i] & 0xff); memcpy(outmsg + i * 2, d, 2); } outmsg[TOX_ADDRESS_SIZE * 2] = '\0'; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); }
ssize_t toxstream_write(void *c, const char *message, size_t size) { struct toxstream_cookie *cookie = c; TOX_ERR_FRIEND_SEND_MESSAGE error; size_t length, offset; for ( offset = 0; offset < size; offset += length) { length = size - offset > TOX_MAX_MESSAGE_LENGTH ? TOX_MAX_MESSAGE_LENGTH : size - offset; tox_friend_send_message(cookie->tox, cookie->friend_number, cookie->type, (uint8_t*)message + offset, length, &error); if (error != TOX_ERR_FRIEND_SEND_MESSAGE_OK) { perror("tox_friend_send_message"); return 0; } } return offset; }
static void addfriend(Tox *t) { const char *himsg = "hihihi"; // 4A06ABE7C9846F227A173B788802B4E5FEE3A0BAE1D5F449FF50C09089C88B20676C0EB625F8 const uint8_t friend_address[38] = { 0x4a, 0x06, 0xab, 0xe7, 0xc9, 0x84, 0x6f, 0x22, 0x7a, 0x17, 0x3b, 0x78, 0x88, 0x02, 0xb4, 0xe5, 0xfe, 0xe3, 0xa0, 0xba, 0xe1, 0xd5, 0xf4, 0x49, 0xff, 0x50, 0xc0, 0x90, 0x89, 0xc8, 0x8b, 0x20, 0x67, 0x6c, 0x0e, 0xb6, 0x25, 0xf8 }; TOX_ERR_FRIEND_ADD err; // int child = tox_friend_add (t, friend_address, himsg, strlen (himsg), &err); int child = tox_friend_add_norequest(t, friend_address, &err); printf ("SENDING HI TO %d\n", child); tox_friend_send_message(t, child, TOX_MESSAGE_TYPE_NORMAL, "hi", 2, &err); printf ("TOX ADD %d\n", err); printf ("FRIEND IS %d\n", child); // printf ("UDP %d\n", tox_self_get_udp_port(t, NULL)); //printf ("TDP %d\n", tox_self_get_tcp_port(t, NULL)); }
static void cmd_name(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: Name required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } char name[TOX_MAX_NAME_LENGTH]; int len = 0; if (argv[1][0] == '\"') { /* remove opening and closing quotes */ snprintf(name, sizeof(name), "%s", &argv[1][1]); len = strlen(name) - 1; } else { snprintf(name, sizeof(name), "%s", argv[1]); len = strlen(name); } name[len] = '\0'; tox_self_set_name(m, (uint8_t *) name, (uint16_t) len, NULL); char m_name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) m_name, NULL); size_t nlen = tox_friend_get_name_size(m, friendnum, NULL); m_name[nlen] = '\0'; printf("%s set name to %s\n", m_name, name); save_data(m, DATA_FILE); }
static void cmd_help(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; outmsg = "info : Print my current status and list active group chats"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); outmsg = "id : Print my Tox ID"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); outmsg = "invite : Request invite to default group chat"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); outmsg = "invite <n> <p> : Request invite to group chat n (with password p if protected)"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); outmsg = "group <type> <pass> : Creates a new groupchat with type: text | audio (optional password)"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); if (friend_is_master(m, friendnum)) { outmsg = "For a list of master commands see the commands.txt file"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); } }
static void cmd_gmessage(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: Group number required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } if (argc < 2) { outmsg = "Error: Message required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } int groupnum = atoi(argv[1]); if (groupnum == 0 && strcmp(argv[1], "0")) { outmsg = "Error: Invalid group number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } if (group_index(groupnum) == -1) { outmsg = "Error: Invalid group number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } if (argv[2][0] != '\"') { outmsg = "Error: Message must be enclosed in quotes"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } /* remove opening and closing quotes */ char msg[MAX_COMMAND_LENGTH]; snprintf(msg, sizeof(msg), "%s", &argv[2][1]); int len = strlen(msg) - 1; msg[len] = '\0'; TOX_ERR_CONFERENCE_SEND_MESSAGE err; if (!tox_conference_send_message(m, groupnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) msg, strlen(msg), &err)) { outmsg = "Error: Failed to send message."; send_error(m, friendnum, outmsg, err); return; } char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t nlen = tox_friend_get_name_size(m, friendnum, NULL); name[nlen] = '\0'; outmsg = "Message sent."; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); printf("<%s> message to group %d: %s\n", name, groupnum, msg); }
static void send_error(Tox *m, uint32_t friendnum, const char *message, int err) { char outmsg[TOX_MAX_MESSAGE_LENGTH]; snprintf(outmsg, sizeof(outmsg), "%s (error %d)", message, err); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); }
static void cmd_passwd(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (!friend_is_master(m, friendnum)) { authent_failed(m, friendnum); return; } if (argc < 1) { outmsg = "Error: group number required"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } int groupnum = atoi(argv[1]); if (groupnum == 0 && strcmp(argv[1], "0")) { outmsg = "Error: Invalid group number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } int idx = group_index(groupnum); if (idx == -1) { outmsg = "Error: Invalid group number"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t nlen = tox_friend_get_name_size(m, friendnum, NULL); name[nlen] = '\0'; /* no password */ if (argc < 2) { Tox_Bot.g_chats[idx].has_pass = false; memset(Tox_Bot.g_chats[idx].password, 0, MAX_PASSWORD_SIZE); outmsg = "No password set"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); printf("No password set for group %d by %s\n", groupnum, name); return; } if (strlen(argv[2]) >= MAX_PASSWORD_SIZE) { outmsg = "Password too long"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } Tox_Bot.g_chats[idx].has_pass = true; snprintf(Tox_Bot.g_chats[idx].password, sizeof(Tox_Bot.g_chats[idx].password), "%s", argv[2]); outmsg = "Password set"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); printf("Password for group %d set by %s\n", groupnum, name); }
static void cmd_group(Tox *m, uint32_t friendnum, int argc, char (*argv)[MAX_COMMAND_LENGTH]) { const char *outmsg = NULL; if (argc < 1) { outmsg = "Please specify the group type: audio or text"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } uint8_t type = TOX_CONFERENCE_TYPE_AV ? !strcasecmp(argv[1], "audio") : TOX_CONFERENCE_TYPE_TEXT; char name[TOX_MAX_NAME_LENGTH]; tox_friend_get_name(m, friendnum, (uint8_t *) name, NULL); size_t len = tox_friend_get_name_size(m, friendnum, NULL); name[len] = '\0'; int groupnum = -1; if (type == TOX_CONFERENCE_TYPE_TEXT) { TOX_ERR_CONFERENCE_NEW err; groupnum = tox_conference_new(m, &err); if (err != TOX_ERR_CONFERENCE_NEW_OK) { printf("Group chat creation by %s failed to initialize\n", name); outmsg = "Group chat instance failed to initialize."; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } } else if (type == TOX_CONFERENCE_TYPE_AV) { groupnum = toxav_add_av_groupchat(m, NULL, NULL); if (groupnum == -1) { printf("Group chat creation by %s failed to initialize\n", name); outmsg = "Group chat instance failed to initialize."; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } } const char *password = argc >= 2 ? argv[2] : NULL; if (password && strlen(argv[2]) >= MAX_PASSWORD_SIZE) { printf("Group chat creation by %s failed: Password too long\n", name); outmsg = "Group chat instance failed to initialize: Password too long"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); return; } if (group_add(groupnum, type, password) == -1) { printf("Group chat creation by %s failed\n", name); outmsg = "Group chat creation failed"; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); tox_conference_delete(m, groupnum, NULL); return; } const char *pw = password ? " (Password protected)" : ""; printf("Group chat %d created by %s%s\n", groupnum, name, pw); char msg[MAX_COMMAND_LENGTH]; snprintf(msg, sizeof(msg), "Group chat %d created%s", groupnum, pw); tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) msg, strlen(msg), NULL); }
static void authent_failed(Tox *m, uint32_t friendnum) { const char *outmsg = "You do not have permission to use this command."; tox_friend_send_message(m, friendnum, TOX_MESSAGE_TYPE_NORMAL, (uint8_t *) outmsg, strlen(outmsg), NULL); }
int main(int argc, char *argv[]) { uint8_t ipv6enabled = 1; /* 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]); exit(0); } int *master = (int *)malloc(sizeof(int)); int ret = forkpty(master, NULL, NULL, NULL); if (ret == -1) { printf("fork failed\n"); free(master); return 1; } if (ret == 0) { execl("/bin/sh", "sh", NULL); return 0; } int flags = fcntl(*master, F_GETFL, 0); int r = fcntl(*master, F_SETFL, flags | O_NONBLOCK); if (r < 0) { printf("error setting flags\n"); } Tox *tox = tox_new(0, 0); tox_callback_friend_connection_status(tox, print_online); tox_callback_friend_message(tox, print_message); uint16_t port = atoi(argv[argvoffset + 2]); unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); int res = tox_bootstrap(tox, argv[argvoffset + 1], port, binary_string, 0); free(binary_string); if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } uint8_t address[TOX_ADDRESS_SIZE]; tox_self_get_address(tox, address); uint32_t i; for (i = 0; i < TOX_ADDRESS_SIZE; i++) { printf("%02X", address[i]); } char temp_id[128]; printf("\nEnter the address of the other id you want to sync with (38 bytes HEX format):\n"); if (scanf("%s", temp_id) != 1) { return 1; } uint8_t *bin_id = hex_string_to_bin(temp_id); uint32_t num = tox_friend_add(tox, bin_id, (const uint8_t *)"Install Gentoo", sizeof("Install Gentoo"), 0); free(bin_id); if (num == UINT32_MAX) { printf("\nSomething went wrong when adding friend.\n"); return 1; } uint8_t notconnected = 1; while (1) { if (tox_self_get_connection_status(tox) && notconnected) { printf("\nDHT connected.\n"); notconnected = 0; } while (tox_friend_get_connection_status(tox, num, 0)) { uint8_t buf[TOX_MAX_MESSAGE_LENGTH]; ret = read(*master, buf, sizeof(buf)); if (ret <= 0) { break; } tox_friend_send_message(tox, num, TOX_MESSAGE_TYPE_NORMAL, buf, ret, 0); } tox_iterate(tox, master); c_sleep(1); } }