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.c292
1 files changed, 292 insertions, 0 deletions
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
new file mode 100644
index 00000000..4a59cc7b
--- /dev/null
+++ b/testing/toxic/prompt.c
@@ -0,0 +1,292 @@
1/*
2 * Toxic -- Tox Curses Client
3 */
4
5#include <stdlib.h>
6#include <string.h>
7#include <ctype.h>
8#include <curses.h>
9
10#include "../../core/Messenger.h"
11#include "../../core/network.h"
12
13#include "windows.h"
14
15uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX
16uint8_t num_requests=0; // XXX
17
18// XXX:
19int add_req(uint8_t* public_key) {
20 memcpy(pending_requests[num_requests], public_key, CLIENT_ID_SIZE);
21 ++num_requests;
22
23 return num_requests-1;
24}
25
26// XXX: FIX
27unsigned char * hex_string_to_bin(char hex_string[])
28{
29 size_t len = strlen(hex_string);
30 unsigned char *val = malloc(len);
31 char *pos = hex_string;
32 int i;
33 for(i = 0; i < len; ++i, pos+=2)
34 sscanf(pos,"%2hhx",&val[i]);
35 return val;
36}
37
38static char prompt_buf[256] = {0};
39static int prompt_buf_pos=0;
40
41static void execute(ToxWindow* self, char* cmd) {
42
43 // quit/exit: Exit program.
44 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) {
45 endwin();
46 exit(0);
47 }
48 else if(!strncmp(cmd, "connect ", strlen("connect "))) {
49 char* ip;
50 char* port;
51 char* key;
52 IP_Port dht;
53
54 ip = strchr(cmd, ' ');
55 if(ip == NULL) {
56 return;
57 }
58
59 ip++;
60
61 port = strchr(ip, ' ');
62 if(port == NULL) {
63 return;
64 }
65
66 port[0] = 0;
67 port++;
68
69 key = strchr(port, ' ');
70 if(key == NULL) {
71 return;
72 }
73
74 key[0] = 0;
75 key++;
76
77 if(atoi(port) == 0) {
78 return;
79 }
80
81 wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key);
82
83 dht.port = htons(atoi(port));
84
85 int resolved_address = resolve_addr(ip);
86 if (resolved_address == -1) {
87 return;
88 }
89
90 dht.ip.i = resolved_address;
91 DHT_bootstrap(dht, hex_string_to_bin(key));
92 }
93 else if(!strncmp(cmd, "add ", strlen("add "))) {
94 char* id;
95 char* msg;
96 int num;
97
98 id = strchr(cmd, ' ');
99
100 if(id == NULL) {
101 return;
102 }
103
104 id++;
105
106 msg = strchr(id, ' ');
107 if(msg == NULL) {
108 return;
109 }
110
111 msg[0] = 0;
112 msg++;
113
114 num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1);
115 wprintw(self->window, "Friend added as %d.\n", num);
116 }
117 else if(!strncmp(cmd, "status ", strlen("status "))) {
118 char* msg;
119
120 msg = strchr(cmd, ' ');
121 if(msg == NULL) {
122 return;
123 }
124
125 msg++;
126 m_set_userstatus((uint8_t*) msg, strlen(msg)+1);
127 wprintw(self->window, "Status set to: %s.\n", msg);
128 }
129 else if(!strncmp(cmd, "nick ", strlen("nick "))) {
130 char* nick;
131
132 nick = strchr(cmd, ' ');
133 if(nick == NULL) {
134 return;
135 }
136
137 nick++;
138 setname((uint8_t*) nick, strlen(nick)+1);
139 wprintw(self->window, "Nickname set to: %s.\n", nick);
140 }
141 else if(!strcmp(cmd, "myid")) {
142 // XXX: Clean this up
143 char idstring0[200];
144 char idstring1[32][5];
145 char idstring2[32][5];
146 uint32_t i;
147
148 for(i = 0; i < 32; i++) {
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 }
156
157 for (i=0; i<32; i++) {
158 strcat(idstring0, idstring1[i]);
159 strcat(idstring0, idstring2[i]);
160 }
161
162 wprintw(self->window, "%s\n", idstring0);
163 }
164 else if(!strncmp(cmd, "accept ", strlen("accept "))) {
165 char* id;
166 int num;
167
168 id = strchr(cmd, ' ');
169 if(id == NULL) {
170 return;
171 }
172 id++;
173
174 num = atoi(id);
175 if(num >= num_requests) {
176 return;
177 }
178
179 num = m_addfriend_norequest(pending_requests[num]);
180 wprintw(self->window, "Friend accepted as: %d.\n", num);
181 }
182 else if(!strncmp(cmd, "msg ", strlen("msg "))) {
183 char* id;
184 char* msg;
185
186 id = strchr(cmd, ' ');
187
188 if(id == NULL) {
189 return;
190 }
191
192 id++;
193
194 msg = strchr(id, ' ');
195 if(msg == NULL) {
196 return;
197 }
198
199 msg[0] = 0;
200 msg++;
201
202 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) {
203 wprintw(self->window, "Error occurred while sending message.\n");
204 }
205 else {
206 wprintw(self->window, "Message successfully sent.\n");
207 }
208 }
209}
210
211static void prompt_onKey(ToxWindow* self, int key) {
212
213 // PRINTABLE characters: Add to line.
214 if(isprint(key)) {
215
216 if(prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
217 return;
218 }
219
220 prompt_buf[prompt_buf_pos++] = key;
221 prompt_buf[prompt_buf_pos] = 0;
222 }
223
224 // RETURN key: execute command.
225 else if(key == '\n') {
226 wprintw(self->window, "\n");
227 execute(self, prompt_buf);
228
229 prompt_buf_pos = 0;
230 prompt_buf[0] = 0;
231 }
232
233 // BACKSPACE key: Remove one character from line.
234 else if(key == 0x107) {
235
236 if(prompt_buf_pos != 0) {
237 prompt_buf[--prompt_buf_pos] = 0;
238 }
239 }
240}
241
242static void prompt_onDraw(ToxWindow* self) {
243 int x, y;
244
245 mvwin(self->window,0,0);
246 wresize(self->window, LINES-2, COLS);
247
248 getyx(self->window, y, x);
249 (void) x;
250
251 wattron(self->window, COLOR_PAIR(1));
252 mvwprintw(self->window, y, 0, "# ");
253 wattroff(self->window, COLOR_PAIR(1));
254
255 mvwprintw(self->window, y, 2, "%s", prompt_buf);
256 wclrtoeol(self->window);
257
258 wrefresh(self->window);
259}
260
261static void print_usage(ToxWindow* self) {
262 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
263 wprintw(self->window, "Usage:\n");
264 wattroff(self->window, A_BOLD);
265
266 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
267 wprintw(self->window, " add <id> <message> : Add friend\n");
268 wprintw(self->window, " status <message> : Set your status\n");
269 wprintw(self->window, " nick <nickname> : Set your nickname\n");
270 wprintw(self->window, " accept <number> : Accept friend request\n");
271 wprintw(self->window, " myid : Print your ID\n");
272 wprintw(self->window, " quit/exit : Exit program\n");
273 wattroff(self->window, COLOR_PAIR(2));
274}
275
276static void prompt_onInit(ToxWindow* self) {
277 scrollok(self->window, 1);
278
279 print_usage(self);
280 wclrtoeol(self->window);
281}
282
283ToxWindow new_prompt() {
284 ToxWindow ret;
285
286 ret.onKey = &prompt_onKey;
287 ret.onDraw = &prompt_onDraw;
288 ret.onInit = &prompt_onInit;
289 strcpy(ret.title, "[prompt]");
290
291 return ret;
292}