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