diff options
author | plutooo <tfy12vbr@student.lu.se> | 2013-07-30 12:47:40 -0700 |
---|---|---|
committer | plutooo <tfy12vbr@student.lu.se> | 2013-07-30 12:47:40 -0700 |
commit | 026790e181a4dd26a60bcbada3e546a1c5e50888 (patch) | |
tree | e02161a9747f0bf10415da6d0b273cc77076c2b9 /testing/toxic/prompt.c | |
parent | c51e5c358af7f2bcb9d9dfdb5e63f9a2c8b5cec4 (diff) |
Fixed a bunch of bugs in TOXIC, added friendlist and more.
Diffstat (limited to 'testing/toxic/prompt.c')
-rw-r--r-- | testing/toxic/prompt.c | 109 |
1 files changed, 69 insertions, 40 deletions
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 4a59cc7b..22d9eb9e 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -15,6 +15,8 @@ | |||
15 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX | 15 | uint8_t pending_requests[256][CLIENT_ID_SIZE]; // XXX |
16 | uint8_t num_requests=0; // XXX | 16 | uint8_t num_requests=0; // XXX |
17 | 17 | ||
18 | extern void on_friendadded(int friendnumber); | ||
19 | |||
18 | // XXX: | 20 | // XXX: |
19 | int add_req(uint8_t* public_key) { | 21 | int 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,7 +42,6 @@ static int prompt_buf_pos=0; | |||
40 | 42 | ||
41 | static void execute(ToxWindow* self, char* cmd) { | 43 | static void execute(ToxWindow* self, char* cmd) { |
42 | 44 | ||
43 | // quit/exit: Exit program. | ||
44 | if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { | 45 | if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { |
45 | endwin(); | 46 | endwin(); |
46 | exit(0); | 47 | exit(0); |
@@ -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,16 +208,16 @@ 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 | ||
@@ -206,6 +228,9 @@ static void execute(ToxWindow* self, char* cmd) { | |||
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 | ||
211 | static void prompt_onKey(ToxWindow* self, int key) { | 236 | static void prompt_onKey(ToxWindow* self, int key) { |
@@ -242,9 +267,6 @@ static void prompt_onKey(ToxWindow* self, int key) { | |||
242 | static void prompt_onDraw(ToxWindow* self) { | 267 | static 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 | ||
@@ -265,11 +287,18 @@ static void print_usage(ToxWindow* self) { | |||
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"); |
267 | wprintw(self->window, " add <id> <message> : Add friend\n"); | 289 | wprintw(self->window, " add <id> <message> : Add friend\n"); |
290 | wprintw(self->window, " msg <number> <message> : Send message\n"); | ||
268 | wprintw(self->window, " status <message> : Set your status\n"); | 291 | wprintw(self->window, " status <message> : Set your status\n"); |
269 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); | 292 | wprintw(self->window, " nick <nickname> : Set your nickname\n"); |
270 | wprintw(self->window, " accept <number> : Accept friend request\n"); | 293 | wprintw(self->window, " accept <number> : Accept friend request\n"); |
271 | wprintw(self->window, " myid : Print your ID\n"); | 294 | wprintw(self->window, " myid : Print your ID\n"); |
272 | wprintw(self->window, " quit/exit : Exit program\n"); | 295 | wprintw(self->window, " quit/exit : Exit program\n"); |
296 | |||
297 | |||
298 | wattron(self->window, A_BOLD); | ||
299 | wprintw(self->window, "Use the TAB key to navigate through the tabs.\n"); | ||
300 | wattroff(self->window, A_BOLD); | ||
301 | |||
273 | wattroff(self->window, COLOR_PAIR(2)); | 302 | wattroff(self->window, COLOR_PAIR(2)); |
274 | } | 303 | } |
275 | 304 | ||