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.c118
1 files changed, 74 insertions, 44 deletions
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index 4a59cc7b..f1b6e689 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -15,6 +15,8 @@
15uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX 15uint8_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);
19
18// XXX: 20// XXX:
19int add_req(uint8_t* public_key) { 21int add_req(uint8_t* public_key) {
20 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE); 22 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
@@ -40,8 +42,7 @@ static int prompt_buf_pos=0;
40 42
41static void execute(ToxWindow* self, char* cmd) { 43static void execute(ToxWindow* self, char* cmd) {
42 44
43 // quit/exit: Exit program. 45 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
44 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) {
45 endwin(); 46 endwin();
46 exit(0); 47 exit(0);
47 } 48 }
@@ -53,33 +54,32 @@ static void execute(ToxWindow* self, char* cmd) {
53 54
54 ip = strchr(cmd, ' '); 55 ip = strchr(cmd, ' ');
55 if(ip == NULL) { 56 if(ip == NULL) {
57 wprintw(self->window, "Invalid syntax.\n");
56 return; 58 return;
57 } 59 }
58
59 ip++; 60 ip++;
60 61
61 port = strchr(ip, ' '); 62 port = strchr(ip, ' ');
62 if(port == NULL) { 63 if(port == NULL) {
64 wprintw(self->window, "Invalid syntax.\n");
63 return; 65 return;
64 } 66 }
65
66 port[0] = 0; 67 port[0] = 0;
67 port++; 68 port++;
68 69
69 key = strchr(port, ' '); 70 key = strchr(port, ' ');
70 if(key == NULL) { 71 if(key == NULL) {
72 wprintw(self->window, "Invalid syntax.\n");
71 return; 73 return;
72 } 74 }
73
74 key[0] = 0; 75 key[0] = 0;
75 key++; 76 key++;
76 77
77 if(atoi(port) == 0) { 78 if(atoi(port) == 0) {
79 wprintw(self->window, "Invalid syntax.\n");
78 return; 80 return;
79 } 81 }
80 82
81 wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key);
82
83 dht.port = htons(atoi(port)); 83 dht.port = htons(atoi(port));
84 84
85 int resolved_address = resolve_addr(ip); 85 int resolved_address = resolve_addr(ip);
@@ -91,38 +91,62 @@ static void execute(ToxWindow* self, char* cmd) {
91 DHT_bootstrap(dht, hex_string_to_bin(key)); 91 DHT_bootstrap(dht, hex_string_to_bin(key));
92 } 92 }
93 else if(!strncmp(cmd, "add ", strlen("add "))) { 93 else if(!strncmp(cmd, "add ", strlen("add "))) {
94 uint8_t id_bin[32];
95 size_t i;
96 char xx[3];
97 uint32_t x;
98
94 char* id; 99 char* id;
95 char* msg; 100 char* msg;
96 int num; 101 int num;
97 102
98 id = strchr(cmd, ' '); 103 id = strchr(cmd, ' ');
99
100 if(id == NULL) { 104 if(id == NULL) {
105 wprintw(self->window, "Invalid syntax.\n");
101 return; 106 return;
102 } 107 }
103
104 id++; 108 id++;
105 109
106 msg = strchr(id, ' '); 110 msg = strchr(id, ' ');
107 if(msg == NULL) { 111 if(msg != NULL) {
112 msg[0] = 0;
113 msg++;
114 }
115 else msg = "";
116
117 if(strlen(id) != 2*32) {
118 wprintw(self->window, "Invalid ID length.\n");
108 return; 119 return;
109 } 120 }
110 121
111 msg[0] = 0; 122 for(i=0; i<32; i++) {
112 msg++; 123 xx[0] = id[2*i];
124 xx[1] = id[2*i+1];
125 xx[2] = '\0';
126
127 if(sscanf(xx, "%02x", &x) != 1) {
128 wprintw(self->window, "Invalid ID.\n");
129 return;
130 }
131
132 id_bin[i] = x;
133 }
134
135 num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
113 136
114 num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1);
115 wprintw(self->window, "Friend added as %d.\n", num); 137 wprintw(self->window, "Friend added as %d.\n", num);
138 on_friendadded(num);
116 } 139 }
117 else if(!strncmp(cmd, "status ", strlen("status "))) { 140 else if(!strncmp(cmd, "status ", strlen("status "))) {
118 char* msg; 141 char* msg;
119 142
120 msg = strchr(cmd, ' '); 143 msg = strchr(cmd, ' ');
121 if(msg == NULL) { 144 if(msg == NULL) {
145 wprintw(self->window, "Invalid syntax.\n");
122 return; 146 return;
123 } 147 }
124
125 msg++; 148 msg++;
149
126 m_set_userstatus((uint8_t*) msg, strlen(msg)+1); 150 m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
127 wprintw(self->window, "Status set to: %s.\n", msg); 151 wprintw(self->window, "Status set to: %s.\n", msg);
128 } 152 }
@@ -133,33 +157,22 @@ static void execute(ToxWindow* self, char* cmd) {
133 if(nick == NULL) { 157 if(nick == NULL) {
134 return; 158 return;
135 } 159 }
136
137 nick++; 160 nick++;
161
138 setname((uint8_t*) nick, strlen(nick)+1); 162 setname((uint8_t*) nick, strlen(nick)+1);
139 wprintw(self->window, "Nickname set to: %s.\n", nick); 163 wprintw(self->window, "Nickname set to: %s.\n", nick);
140 } 164 }
141 else if(!strcmp(cmd, "myid")) { 165 else if(!strcmp(cmd, "myid")) {
142 // XXX: Clean this up 166 char id[32*2 + 1] = {0};
143 char idstring0[200]; 167 size_t i;
144 char idstring1[32][5]; 168
145 char idstring2[32][5]; 169 for(i=0; i<32; i++) {
146 uint32_t i; 170 char xx[3];
147 171 snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
148 for(i = 0; i < 32; i++) { 172 strcat(id, xx);
149 if(self_public_key[i] < 16)
150 strcpy(idstring1[i], "0");
151 else
152 strcpy(idstring1[i], "");
153
154 sprintf(idstring2[i], "%hhX", self_public_key[i]);
155 } 173 }
156 174
157 for (i=0; i<32; i++) { 175 wprintw(self->window, "%s\n", id);
158 strcat(idstring0, idstring1[i]);
159 strcat(idstring0, idstring2[i]);
160 }
161
162 wprintw(self->window, "%s\n", idstring0);
163 } 176 }
164 else if(!strncmp(cmd, "accept ", strlen("accept "))) { 177 else if(!strncmp(cmd, "accept ", strlen("accept "))) {
165 char* id; 178 char* id;
@@ -167,17 +180,26 @@ static void execute(ToxWindow* self, char* cmd) {
167 180
168 id = strchr(cmd, ' '); 181 id = strchr(cmd, ' ');
169 if(id == NULL) { 182 if(id == NULL) {
183 wprintw(self->window, "Invalid syntax.\n");
170 return; 184 return;
171 } 185 }
172 id++; 186 id++;
173 187
174 num = atoi(id); 188 num = atoi(id);
175 if(num >= num_requests) { 189 if(num >= num_requests) {
190 wprintw(self->window, "Invalid syntax.\n");
176 return; 191 return;
177 } 192 }
178 193
179 num = m_addfriend_norequest(pending_requests[num]); 194 num = m_addfriend_norequest(pending_requests[num]);
180 wprintw(self->window, "Friend accepted as: %d.\n", num); 195
196 if(num == -1) {
197 wprintw(self->window, "Failed to add friend.\n");
198 }
199 else {
200 wprintw(self->window, "Friend accepted as: %d.\n", num);
201 on_friendadded(num);
202 }
181 } 203 }
182 else if(!strncmp(cmd, "msg ", strlen("msg "))) { 204 else if(!strncmp(cmd, "msg ", strlen("msg "))) {
183 char* id; 205 char* id;
@@ -186,26 +208,29 @@ static void execute(ToxWindow* self, char* cmd) {
186 id = strchr(cmd, ' '); 208 id = strchr(cmd, ' ');
187 209
188 if(id == NULL) { 210 if(id == NULL) {
211 wprintw(self->window, "Invalid syntax.\n");
189 return; 212 return;
190 } 213 }
191
192 id++; 214 id++;
193 215
194 msg = strchr(id, ' '); 216 msg = strchr(id, ' ');
195 if(msg == NULL) { 217 if(msg == NULL) {
218 wprintw(self->window, "Invalid syntax.\n");
196 return; 219 return;
197 } 220 }
198
199 msg[0] = 0; 221 msg[0] = 0;
200 msg++; 222 msg++;
201 223
202 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { 224 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
203 wprintw(self->window, "Error occurred while sending message.\n"); 225 wprintw(self->window, "Error occurred while sending message.\n");
204 } 226 }
205 else { 227 else {
206 wprintw(self->window, "Message successfully sent.\n"); 228 wprintw(self->window, "Message successfully sent.\n");
207 } 229 }
208 } 230 }
231 else {
232 wprintw(self->window, "Invalid syntax.\n");
233 }
209} 234}
210 235
211static void prompt_onKey(ToxWindow* self, int key) { 236static void prompt_onKey(ToxWindow* self, int key) {
@@ -231,7 +256,7 @@ static void prompt_onKey(ToxWindow* self, int key) {
231 } 256 }
232 257
233 // BACKSPACE key: Remove one character from line. 258 // BACKSPACE key: Remove one character from line.
234 else if(key == 0x107) { 259 else if(key == 0x107 || key == 0x8 || key == 0x7f) {
235 260
236 if(prompt_buf_pos != 0) { 261 if(prompt_buf_pos != 0) {
237 prompt_buf[--prompt_buf_pos] = 0; 262 prompt_buf[--prompt_buf_pos] = 0;
@@ -242,9 +267,6 @@ static void prompt_onKey(ToxWindow* self, int key) {
242static void prompt_onDraw(ToxWindow* self) { 267static void prompt_onDraw(ToxWindow* self) {
243 int x, y; 268 int x, y;
244 269
245 mvwin(self->window,0,0);
246 wresize(self->window, LINES-2, COLS);
247
248 getyx(self->window, y, x); 270 getyx(self->window, y, x);
249 (void) x; 271 (void) x;
250 272
@@ -260,7 +282,7 @@ static void prompt_onDraw(ToxWindow* self) {
260 282
261static void print_usage(ToxWindow* self) { 283static void print_usage(ToxWindow* self) {
262 wattron(self->window, COLOR_PAIR(2) | A_BOLD); 284 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
263 wprintw(self->window, "Usage:\n"); 285 wprintw(self->window, "Commands:\n");
264 wattroff(self->window, A_BOLD); 286 wattroff(self->window, A_BOLD);
265 287
266 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n"); 288 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
@@ -270,6 +292,12 @@ static void print_usage(ToxWindow* self) {
270 wprintw(self->window, " accept <number> : Accept friend request\n"); 292 wprintw(self->window, " accept <number> : Accept friend request\n");
271 wprintw(self->window, " myid : Print your ID\n"); 293 wprintw(self->window, " myid : Print your ID\n");
272 wprintw(self->window, " quit/exit : Exit program\n"); 294 wprintw(self->window, " quit/exit : Exit program\n");
295
296
297 wattron(self->window, A_BOLD);
298 wprintw(self->window, "TIP: Use the TAB key to navigate through the tabs.\n\n");
299 wattroff(self->window, A_BOLD);
300
273 wattroff(self->window, COLOR_PAIR(2)); 301 wattroff(self->window, COLOR_PAIR(2));
274} 302}
275 303
@@ -283,6 +311,8 @@ static void prompt_onInit(ToxWindow* self) {
283ToxWindow new_prompt() { 311ToxWindow new_prompt() {
284 ToxWindow ret; 312 ToxWindow ret;
285 313
314 memset(&ret, 0, sizeof(ret));
315
286 ret.onKey = &prompt_onKey; 316 ret.onKey = &prompt_onKey;
287 ret.onDraw = &prompt_onDraw; 317 ret.onDraw = &prompt_onDraw;
288 ret.onInit = &prompt_onInit; 318 ret.onInit = &prompt_onInit;