summaryrefslogtreecommitdiff
path: root/testing/toxic/prompt.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic/prompt.c')
-rw-r--r--testing/toxic/prompt.c284
1 files changed, 128 insertions, 156 deletions
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index 20f6b480..89c87d8f 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -16,83 +16,78 @@ uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX
16uint8_t num_requests=0; // XXX 16uint8_t num_requests=0; // XXX
17 17
18extern void on_friendadded(int friendnumber); 18extern void on_friendadded(int friendnumber);
19static void print_usage(ToxWindow* self); 19static void print_usage(ToxWindow *self);
20static char prompt_buf[256] = {0};
21static int prompt_buf_pos = 0;
20 22
21// XXX: 23// XXX:
22int add_req(uint8_t* public_key) { 24int add_req(uint8_t *public_key)
25{
23 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); 26 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
24 ++num_requests; 27 ++num_requests;
25
26 return num_requests-1; 28 return num_requests-1;
27} 29}
28 30
29// XXX: FIX 31// XXX: FIX
30unsigned char * hex_string_to_bin(char hex_string[]) 32unsigned char *hex_string_to_bin(char hex_string[])
31{ 33{
32 size_t len = strlen(hex_string); 34 size_t len = strlen(hex_string);
33 unsigned char *val = malloc(len); 35 unsigned char *val = malloc(len);
34 char *pos = hex_string; 36 char *pos = hex_string;
35 int i; 37 int i;
36 for(i = 0; i < len; ++i, pos+=2) 38 for (i = 0; i < len; ++i, pos+=2)
37 sscanf(pos,"%2hhx",&val[i]); 39 sscanf(pos,"%2hhx",&val[i]);
38 return val; 40 return val;
39} 41}
40 42
41static char prompt_buf[256] = {0}; 43static void execute(ToxWindow *self, char *u_cmd)
42static int prompt_buf_pos=0; 44{
43 45 int newlines = 0;
44static void execute(ToxWindow* self, char* u_cmd) { 46 char cmd[256] = {0};
45 int i; 47 int i;
46 int newlines = 0; 48 for (i = 0; i < strlen(prompt_buf); ++i) {
47 char cmd[256] = {0};
48 for(i = 0; i < strlen(prompt_buf); i++)
49 {
50 if (u_cmd[i] == '\n') 49 if (u_cmd[i] == '\n')
51 ++newlines; 50 ++newlines;
52 else 51 else
53 cmd[i - newlines] = u_cmd[i]; 52 cmd[i - newlines] = u_cmd[i];
54 } 53 }
55 54
56 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { 55 if (!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
57 endwin(); 56 endwin();
58 exit(0); 57 exit(0);
59 } 58 }
60 else if(!strncmp(cmd, "connect ", strlen("connect "))) {
61 char* ip;
62 char* port;
63 char* key;
64 IP_Port dht;
65 59
66 ip = strchr(cmd, ' '); 60 else if (!strncmp(cmd, "connect ", strlen("connect "))) {
67 if(ip == NULL) { 61 IP_Port dht;
62 char *ip = strchr(cmd, ' ');
63 if (ip == NULL) {
68 wprintw(self->window, "Invalid syntax.\n"); 64 wprintw(self->window, "Invalid syntax.\n");
69 return; 65 return;
70 } 66 }
71 ip++; 67 ip++;
72 68
73 port = strchr(ip, ' '); 69 char *port = strchr(ip, ' ');
74 if(port == NULL) { 70 if (port == NULL) {
75 wprintw(self->window, "Invalid syntax.\n"); 71 wprintw(self->window, "Invalid syntax.\n");
76 return; 72 return;
77 } 73 }
78 port[0] = 0; 74 port[0] = 0;
79 port++; 75 port++;
80 76
81 key = strchr(port, ' '); 77 char *key = strchr(port, ' ');
82 if(key == NULL) { 78 if (key == NULL) {
83 wprintw(self->window, "Invalid syntax.\n"); 79 wprintw(self->window, "Invalid syntax.\n");
84 return; 80 return;
85 } 81 }
86 key[0] = 0; 82 key[0] = 0;
87 key++; 83 key++;
88 84
89 if(atoi(port) == 0) { 85 if (atoi(port) == 0) {
90 wprintw(self->window, "Invalid syntax.\n"); 86 wprintw(self->window, "Invalid syntax.\n");
91 return; 87 return;
92 } 88 }
93 89
94 dht.port = htons(atoi(port)); 90 dht.port = htons(atoi(port));
95
96 uint32_t resolved_address = resolve_addr(ip); 91 uint32_t resolved_address = resolve_addr(ip);
97 if (resolved_address == 0) { 92 if (resolved_address == 0) {
98 return; 93 return;
@@ -103,49 +98,39 @@ static void execute(ToxWindow* self, char* u_cmd) {
103 DHT_bootstrap(dht, binary_string); 98 DHT_bootstrap(dht, binary_string);
104 free(binary_string); 99 free(binary_string);
105 } 100 }
106 else if(!strncmp(cmd, "add ", strlen("add "))) { 101
102 else if (!strncmp(cmd, "add ", strlen("add "))) {
107 uint8_t id_bin[32]; 103 uint8_t id_bin[32];
108 size_t i;
109 char xx[3]; 104 char xx[3];
110 uint32_t x; 105 uint32_t x;
111 106 char *id = strchr(cmd, ' ');
112 char* id; 107 if (id == NULL) {
113 char* msg;
114 int num;
115
116 id = strchr(cmd, ' ');
117 if(id == NULL) {
118 wprintw(self->window, "Invalid syntax.\n"); 108 wprintw(self->window, "Invalid syntax.\n");
119 return; 109 return;
120 } 110 }
121 id++; 111 id++;
122 112 char *msg = strchr(id, ' ');
123 msg = strchr(id, ' '); 113 if (msg != NULL) {
124 if(msg != NULL) {
125 msg[0] = 0; 114 msg[0] = 0;
126 msg++; 115 msg++;
127 } 116 }
128 else msg = ""; 117 else msg = "";
129 118 if (strlen(id) != 2*32) {
130 if(strlen(id) != 2*32) {
131 wprintw(self->window, "Invalid ID length.\n"); 119 wprintw(self->window, "Invalid ID length.\n");
132 return; 120 return;
133 } 121 }
134 122 int i;
135 for(i=0; i<32; i++) { 123 for (i = 0; i < 32; ++i) {
136 xx[0] = id[2*i]; 124 xx[0] = id[2*i];
137 xx[1] = id[2*i+1]; 125 xx[1] = id[2*i+1];
138 xx[2] = '\0'; 126 xx[2] = '\0';
139 127 if (sscanf(xx, "%02x", &x) != 1) {
140 if(sscanf(xx, "%02x", &x) != 1) {
141 wprintw(self->window, "Invalid ID.\n"); 128 wprintw(self->window, "Invalid ID.\n");
142 return; 129 return;
143 } 130 }
144
145 id_bin[i] = x; 131 id_bin[i] = x;
146 } 132 }
147 133 int num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
148 num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
149 switch (num) { 134 switch (num) {
150 case -1: 135 case -1:
151 wprintw(self->window, "Message is too long.\n"); 136 wprintw(self->window, "Message is too long.\n");
@@ -168,178 +153,167 @@ static void execute(ToxWindow* self, char* u_cmd) {
168 break; 153 break;
169 } 154 }
170 } 155 }
171 else if(!strcmp(cmd, "clear")) { 156
172 wclear(self->window); 157 else if (!strcmp(cmd, "clear")) {
158 wclear(self->window);
173 } 159 }
174 else if(!strcmp(cmd, "help")) { 160
175 wclear(self->window); 161 else if (!strcmp(cmd, "help")) {
176 print_usage(self); 162 wclear(self->window);
163 print_usage(self);
177 } 164 }
178 else if(!strncmp(cmd, "status ", strlen("status "))) {
179 char* msg;
180 165
181 msg = strchr(cmd, ' '); 166 else if (!strncmp(cmd, "status ", strlen("status "))) {
182 if(msg == NULL) { 167 char *msg = strchr(cmd, ' ');
168 if (msg == NULL) {
183 wprintw(self->window, "Invalid syntax.\n"); 169 wprintw(self->window, "Invalid syntax.\n");
184 return; 170 return;
185 } 171 }
186 msg++; 172 msg++;
187
188 m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); 173 m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
189 wprintw(self->window, "Status set to: %s\n", msg); 174 wprintw(self->window, "Status set to: %s\n", msg);
190 } 175 }
191 else if(!strncmp(cmd, "nick ", strlen("nick "))) {
192 char* nick;
193 176
194 nick = strchr(cmd, ' '); 177 else if (!strncmp(cmd, "nick ", strlen("nick "))) {
195 if(nick == NULL) { 178 char *nick = strchr(cmd, ' ');
179 if (nick == NULL) {
196 wprintw(self->window, "Invalid syntax.\n"); 180 wprintw(self->window, "Invalid syntax.\n");
197 return; 181 return;
198 } 182 }
199 nick++; 183 nick++;
200
201 setname((uint8_t*) nick, strlen(nick)+1); 184 setname((uint8_t*) nick, strlen(nick)+1);
202 wprintw(self->window, "Nickname set to: %s\n", nick); 185 wprintw(self->window, "Nickname set to: %s\n", nick);
203 } 186 }
204 else if(!strcmp(cmd, "myid")) { 187
188 else if (!strcmp(cmd, "myid")) {
205 char id[32*2 + 1] = {0}; 189 char id[32*2 + 1] = {0};
206 size_t i; 190 size_t i;
207 191 for (i = 0; i < 32; ++i) {
208 for(i=0; i<32; i++) {
209 char xx[3]; 192 char xx[3];
210 snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); 193 snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
211 strcat(id, xx); 194 strcat(id, xx);
212 } 195 }
213
214 wprintw(self->window, "Your ID: %s\n", id); 196 wprintw(self->window, "Your ID: %s\n", id);
215 } 197 }
216 else if(!strncmp(cmd, "accept ", strlen("accept "))) {
217 char* id;
218 int num;
219 198
220 id = strchr(cmd, ' '); 199 else if (!strncmp(cmd, "accept ", strlen("accept "))) {
221 if(id == NULL) { 200 char *id = strchr(cmd, ' ');
201 if (id == NULL) {
222 wprintw(self->window, "Invalid syntax.\n"); 202 wprintw(self->window, "Invalid syntax.\n");
223 return; 203 return;
224 } 204 }
225 id++; 205 id++;
226 206
227 num = atoi(id); 207 int num = atoi(id);
228 if(num >= num_requests) { 208 if (num >= num_requests) {
229 wprintw(self->window, "Invalid syntax.\n"); 209 wprintw(self->window, "Invalid syntax.\n");
230 return; 210 return;
231 } 211 }
232 212
233 num = m_addfriend_norequest(pending_requests[num]); 213 num = m_addfriend_norequest(pending_requests[num]);
234 214 if (num == -1)
235 if(num == -1) {
236 wprintw(self->window, "Failed to add friend.\n"); 215 wprintw(self->window, "Failed to add friend.\n");
237 }
238 else { 216 else {
239 wprintw(self->window, "Friend accepted as: %d.\n", num); 217 wprintw(self->window, "Friend accepted as: %d.\n", num);
240 on_friendadded(num); 218 on_friendadded(num);
241 } 219 }
242 } 220 }
243 else if(!strncmp(cmd, "msg ", strlen("msg "))) {
244 char* id;
245 char* msg;
246
247 id = strchr(cmd, ' ');
248 221
249 if(id == NULL) { 222 else if (!strncmp(cmd, "msg ", strlen("msg "))) {
223 char *id = strchr(cmd, ' ');
224 if (id == NULL) {
250 wprintw(self->window, "Invalid syntax.\n"); 225 wprintw(self->window, "Invalid syntax.\n");
251 return; 226 return;
252 } 227 }
253 id++; 228 char *msg = strchr(++id, ' ');
254 229 if (msg == NULL) {
255 msg = strchr(id, ' ');
256 if(msg == NULL) {
257 wprintw(self->window, "Invalid syntax.\n"); 230 wprintw(self->window, "Invalid syntax.\n");
258 return; 231 return;
259 } 232 }
260 msg[0] = 0; 233 msg[0] = 0;
261 msg++; 234 msg++;
262 235 if (m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0)
263 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
264 wprintw(self->window, "Error occurred while sending message.\n"); 236 wprintw(self->window, "Error occurred while sending message.\n");
265 } 237 else
266 else {
267 wprintw(self->window, "Message successfully sent.\n"); 238 wprintw(self->window, "Message successfully sent.\n");
268 }
269 } 239 }
270 else { 240 else
271 wprintw(self->window, "Invalid command.\n"); 241 wprintw(self->window, "Invalid command.\n");
272 }
273} 242}
274 243
275static void prompt_onKey(ToxWindow* self, int key) { 244static void prompt_onKey(ToxWindow *self, int key)
276 // PRINTABLE characters: Add to line. 245{
277 if(isprint(key)) { 246 /* Add printable characters to line */
278 if (prompt_buf_pos == (sizeof(prompt_buf) - 1)){ 247 if (isprint(key)) {
279 wprintw(self->window, "\nToo Long.\n"); 248 if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
280 prompt_buf_pos = 0; 249 wprintw(self->window, "\nToo Long.\n");
281 prompt_buf[0] = 0; 250 prompt_buf_pos = 0;
282 } 251 prompt_buf[0] = 0;
283 else if(!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) && (prompt_buf_pos % (COLS - 3) == 0)) { 252 }
284 prompt_buf[prompt_buf_pos++] = '\n'; 253 else if (!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS)
254 && (prompt_buf_pos % (COLS - 3) == 0)) {
255 prompt_buf[prompt_buf_pos++] = '\n';
285 } 256 }
286 else if(!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { 257 else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
287 prompt_buf[prompt_buf_pos++] = '\n'; 258 && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
259 prompt_buf[prompt_buf_pos++] = '\n';
288 } 260 }
289 prompt_buf[prompt_buf_pos++] = key; 261 prompt_buf[prompt_buf_pos++] = key;
290 prompt_buf[prompt_buf_pos] = 0; 262 prompt_buf[prompt_buf_pos] = 0;
291 } 263 }
292 264
293 // RETURN key: execute command. 265 /* RETURN key: execute command */
294 else if(key == '\n') { 266 else if (key == '\n') {
295 wprintw(self->window, "\n"); 267 wprintw(self->window, "\n");
296 execute(self, prompt_buf); 268 execute(self, prompt_buf);
297 prompt_buf_pos = 0; 269 prompt_buf_pos = 0;
298 prompt_buf[0] = 0; 270 prompt_buf[0] = 0;
299 } 271 }
300 272
301 // BACKSPACE key: Remove one character from line. 273 /* BACKSPACE key: Remove one character from line */
302 else if(key == 0x107 || key == 0x8 || key == 0x7f) { 274 else if (key == 0x107 || key == 0x8 || key == 0x7f) {
303 if(prompt_buf_pos != 0) { 275 if (prompt_buf_pos != 0) {
304 prompt_buf[--prompt_buf_pos] = 0; 276 prompt_buf[--prompt_buf_pos] = 0;
305 } 277 }
306 } 278 }
307} 279}
308 280
309static void prompt_onDraw(ToxWindow* self) { 281static void prompt_onDraw(ToxWindow *self)
310 curs_set(1); 282{
311 int x, y; 283 curs_set(1);
312 getyx(self->window, y, x); 284 int x, y;
313 (void) x; 285 getyx(self->window, y, x);
314 int i; 286 (void) x;
315 for (i = 0; i < (strlen(prompt_buf)); i++) 287 int i;
316 { 288 for (i = 0; i < (strlen(prompt_buf)); ++i) {
317 if ((prompt_buf[i] == '\n') && (y != 0)) 289 if ((prompt_buf[i] == '\n') && (y != 0))
318 --y; 290 --y;
319 } 291 }
320 wattron(self->window, COLOR_PAIR(1)); 292
321 mvwprintw(self->window, y, 0, "# "); 293 wattron(self->window, COLOR_PAIR(1));
322 wattroff(self->window, COLOR_PAIR(1)); 294 mvwprintw(self->window, y, 0, "# ");
323 mvwprintw(self->window, y, 2, "%s", prompt_buf); 295 wattroff(self->window, COLOR_PAIR(1));
324 wclrtoeol(self->window); 296 mvwprintw(self->window, y, 2, "%s", prompt_buf);
325 wrefresh(self->window); 297 wclrtoeol(self->window);
298 wrefresh(self->window);
326} 299}
327 300
328static void print_usage(ToxWindow* self) { 301static void print_usage(ToxWindow *self)
302{
329 wattron(self->window, COLOR_PAIR(2) | A_BOLD); 303 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
330 wprintw(self->window, "Commands:\n"); 304 wprintw(self->window, "Commands:\n");
331 wattroff(self->window, A_BOLD); 305 wattroff(self->window, A_BOLD);
332
333 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
334 wprintw(self->window, " add <id> <message> : Add friend\n");
335 wprintw(self->window, " status <message> : Set your status\n");
336 wprintw(self->window, " nick <nickname> : Set your nickname\n");
337 wprintw(self->window, " accept <number> : Accept friend request\n");
338 wprintw(self->window, " myid : Print your ID\n");
339 wprintw(self->window, " quit/exit : Exit program\n");
340 wprintw(self->window, " help : Print this message again\n");
341 wprintw(self->window, " clear : Clear this window\n");
342 306
307 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
308 wprintw(self->window, " add <id> <message> : Add friend\n");
309 wprintw(self->window, " status <message> : Set your status\n");
310 wprintw(self->window, " nick <nickname> : Set your nickname\n");
311 wprintw(self->window, " accept <number> : Accept friend request\n");
312 wprintw(self->window, " myid : Print your ID\n");
313 wprintw(self->window, " quit/exit : Exit program\n");
314 wprintw(self->window, " help : Print this message again\n");
315 wprintw(self->window, " clear : Clear this window\n");
316
343 wattron(self->window, A_BOLD); 317 wattron(self->window, A_BOLD);
344 wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n"); 318 wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
345 wattroff(self->window, A_BOLD); 319 wattroff(self->window, A_BOLD);
@@ -347,22 +321,20 @@ static void print_usage(ToxWindow* self) {
347 wattroff(self->window, COLOR_PAIR(2)); 321 wattroff(self->window, COLOR_PAIR(2));
348} 322}
349 323
350static void prompt_onInit(ToxWindow* self) { 324static void prompt_onInit(ToxWindow *self)
325{
351 scrollok(self->window, 1); 326 scrollok(self->window, 1);
352
353 print_usage(self); 327 print_usage(self);
354 wclrtoeol(self->window); 328 wclrtoeol(self->window);
355} 329}
356 330
357ToxWindow new_prompt() { 331ToxWindow new_prompt()
332{
358 ToxWindow ret; 333 ToxWindow ret;
359
360 memset(&ret, 0, sizeof(ret)); 334 memset(&ret, 0, sizeof(ret));
361
362 ret.onKey = &prompt_onKey; 335 ret.onKey = &prompt_onKey;
363 ret.onDraw = &prompt_onDraw; 336 ret.onDraw = &prompt_onDraw;
364 ret.onInit = &prompt_onInit; 337 ret.onInit = &prompt_onInit;
365 strcpy(ret.title, "[prompt]"); 338 strcpy(ret.title, "[prompt]");
366
367 return ret; 339 return ret;
368} 340}