summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testing/toxic/CMakeLists.txt3
-rw-r--r--testing/toxic/friendlist.c125
-rw-r--r--testing/toxic/main.c49
-rw-r--r--testing/toxic/prompt.c109
4 files changed, 240 insertions, 46 deletions
diff --git a/testing/toxic/CMakeLists.txt b/testing/toxic/CMakeLists.txt
index 4f9785d5..c70babb7 100644
--- a/testing/toxic/CMakeLists.txt
+++ b/testing/toxic/CMakeLists.txt
@@ -5,7 +5,8 @@ set(exe_name toxic)
5 5
6add_executable(${exe_name} 6add_executable(${exe_name}
7 main.c 7 main.c
8 prompt.c) 8 prompt.c
9 friendlist.c)
9 10
10target_link_libraries(${exe_name} 11target_link_libraries(${exe_name}
11 curses) 12 curses)
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
new file mode 100644
index 00000000..f8b8c840
--- /dev/null
+++ b/testing/toxic/friendlist.c
@@ -0,0 +1,125 @@
1/*
2 * Toxic -- Tox Curses Client
3 */
4
5#include <curses.h>
6#include <string.h>
7#include <stdint.h>
8#include <ctype.h>
9
10#include "../../core/Messenger.h"
11#include "../../core/network.h"
12
13#include "windows.h"
14
15#define MAX_FRIENDS_NUM 100
16
17typedef struct {
18 uint8_t name[MAX_NAME_LENGTH];
19 uint8_t status[MAX_USERSTATUS_LENGTH];
20 int num;
21
22} friend_t;
23
24static friend_t friends[MAX_FRIENDS_NUM];
25static int num_friends = 0;
26
27
28void fix_name(uint8_t* name) {
29
30 // Remove all non alphanumeric characters.
31 uint8_t* p = name;
32 uint8_t* q = name;
33
34 while(*p != 0) {
35 if(isalnum(*p)) {
36 *q++ = *p;
37 }
38
39 p++;
40 }
41
42 *q = 0;
43}
44
45int friendlist_nickchange(int num, uint8_t* str, uint16_t len) {
46
47 if(len >= MAX_NAME_LENGTH || num >= num_friends)
48 return -1;
49
50 memcpy((char*) &friends[num].name, (char*) str, len);
51 friends[num].name[len] = 0;
52 fix_name(friends[num].name);
53
54 return 0;
55}
56
57int friendlist_statuschange(int num, uint8_t* str, uint16_t len) {
58
59 if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends)
60 return -1;
61
62 memcpy((char*) &friends[num].status, (char*) str, len);
63 friends[num].status[len] = 0;
64 fix_name(friends[num].status);
65
66 return 0;
67}
68
69int friendlist_addfriend(int num) {
70
71 if(num_friends == MAX_FRIENDS_NUM)
72 return -1;
73
74 friends[num_friends].num = num;
75 getname(num, friends[num_friends].name);
76 strcpy((char*) friends[num_friends].name, "unknown");
77 strcpy((char*) friends[num_friends].status, "unknown");
78
79 num_friends++;
80 return 0;
81}
82
83static void friendlist_onKey(ToxWindow* self, int key) {
84
85}
86
87static void friendlist_onDraw(ToxWindow* self) {
88 size_t i;
89
90 wclear(self->window);
91
92 if(num_friends == 0) {
93 wprintw(self->window, "Empty. Add some friends! :-)\n");
94 }
95
96 wprintw(self->window, "\n");
97
98 for(i=0; i<num_friends; i++) {
99 wprintw(self->window, "[%d] ", friends[i].num);
100
101 attron(A_BOLD);
102 wprintw(self->window, "%s ", friends[i].name);
103 attroff(A_BOLD);
104
105 wprintw(self->window, "(%s)\n", friends[i].status);
106 }
107
108 wrefresh(self->window);
109}
110
111static void friendlist_onInit(ToxWindow* self) {
112
113}
114
115
116ToxWindow new_friendlist() {
117 ToxWindow ret;
118
119 ret.onKey = &friendlist_onKey;
120 ret.onDraw = &friendlist_onDraw;
121 ret.onInit = &friendlist_onInit;
122 strcpy(ret.title, "[friends]");
123
124 return ret;
125}
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 0aad6777..e1d1ebd0 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -14,6 +14,12 @@
14#include "windows.h" 14#include "windows.h"
15 15
16extern ToxWindow new_prompt(); 16extern ToxWindow new_prompt();
17extern ToxWindow new_friendlist();
18
19extern int friendlist_addfriend(int num);
20extern int friendlist_nickchange(int num, uint8_t* str, uint16_t len);
21extern int friendlist_statuschange(int num, uint8_t* str, uint16_t len);
22
17extern int add_req(uint8_t* public_key); // XXX 23extern int add_req(uint8_t* public_key); // XXX
18 24
19#define TOXWINDOWS_MAX_NUM 32 25#define TOXWINDOWS_MAX_NUM 32
@@ -35,11 +41,19 @@ void on_message(int friendnumber, uint8_t* string, uint16_t length) {
35} 41}
36 42
37void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { 43void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) {
38 wprintw(prompt->window, "\n(nick) %d: %s!\n", friendnumber, string); 44 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
45
46 friendlist_nickchange(friendnumber, string, length);
39} 47}
40 48
41void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) { 49void on_statuschange(int friendnumber, uint8_t* string, uint16_t length) {
42 wprintw(prompt->window, "\n(status) %d: %s!\n", friendnumber, string); 50 wprintw(prompt->window, "\n(statuschange) %d: %s!\n", friendnumber, string);
51
52 friendlist_statuschange(friendnumber, string, length);
53}
54
55void on_friendadded(int friendnumber) {
56 friendlist_addfriend(friendnumber);
43} 57}
44// CALLBACKS END 58// CALLBACKS END
45 59
@@ -95,7 +109,7 @@ static void init_windows() {
95 w_num = 0; 109 w_num = 0;
96 w_active = 0; 110 w_active = 0;
97 111
98 if(add_window(new_prompt()) == -1) { 112 if(add_window(new_prompt()) == -1 || add_window(new_friendlist()) == -1) {
99 fprintf(stderr, "add_window() failed.\n"); 113 fprintf(stderr, "add_window() failed.\n");
100 114
101 endwin(); 115 endwin();
@@ -134,14 +148,18 @@ static void load_data() {
134 148
135 if(buf == NULL) { 149 if(buf == NULL) {
136 fprintf(stderr, "malloc() failed.\n"); 150 fprintf(stderr, "malloc() failed.\n");
151
137 fclose(fd); 152 fclose(fd);
153 endwin();
138 exit(1); 154 exit(1);
139 } 155 }
140 156
141 if(fread(buf, len, 1, fd) != 1){ 157 if(fread(buf, len, 1, fd) != 1){
142 fprintf(stderr, "fread() failed.\n"); 158 fprintf(stderr, "fread() failed.\n");
159
143 free(buf); 160 free(buf);
144 fclose(fd); 161 fclose(fd);
162 endwin();
145 exit(1); 163 exit(1);
146 } 164 }
147 165
@@ -153,6 +171,7 @@ static void load_data() {
153 171
154 if(buf == NULL) { 172 if(buf == NULL) {
155 fprintf(stderr, "malloc() failed.\n"); 173 fprintf(stderr, "malloc() failed.\n");
174 endwin();
156 exit(1); 175 exit(1);
157 } 176 }
158 177
@@ -161,14 +180,18 @@ static void load_data() {
161 fd = fopen("data", "w"); 180 fd = fopen("data", "w");
162 if(fd == NULL) { 181 if(fd == NULL) {
163 fprintf(stderr, "fopen() failed.\n"); 182 fprintf(stderr, "fopen() failed.\n");
183
164 free(buf); 184 free(buf);
185 endwin();
165 exit(1); 186 exit(1);
166 } 187 }
167 188
168 if(fwrite(buf, len, 1, fd) != 1){ 189 if(fwrite(buf, len, 1, fd) != 1){
169 fprintf(stderr, "fwrite() failed.\n"); 190 fprintf(stderr, "fwrite() failed.\n");
191
170 free(buf); 192 free(buf);
171 fclose(fd); 193 fclose(fd);
194 endwin();
172 exit(1); 195 exit(1);
173 } 196 }
174 } 197 }
@@ -186,12 +209,16 @@ static void draw_bar() {
186 209
187 move(LINES - 1, 0); 210 move(LINES - 1, 0);
188 211
212 attron(COLOR_PAIR(3) | A_BOLD);
213 printw(" TOXIC 1.0 |");
214 attroff(COLOR_PAIR(3) | A_BOLD);
215
189 for(i=0; i<w_num; i++) { 216 for(i=0; i<w_num; i++) {
190 if(i == w_active) { 217 if(i == w_active) {
191 attron(A_BOLD); 218 attron(A_BOLD);
192 } 219 }
193 220
194 printw(" %s ", windows[i].title); 221 printw(" %s", windows[i].title);
195 222
196 if(i == w_active) { 223 if(i == w_active) {
197 attroff(A_BOLD); 224 attroff(A_BOLD);
@@ -201,6 +228,11 @@ static void draw_bar() {
201 refresh(); 228 refresh();
202} 229}
203 230
231void prepare_window(WINDOW* w) {
232 mvwin(w, 0, 0);
233 wresize(w, LINES-2, COLS);
234}
235
204int main(int argc, char* argv[]) { 236int main(int argc, char* argv[]) {
205 int ch; 237 int ch;
206 ToxWindow* a; 238 ToxWindow* a;
@@ -211,14 +243,21 @@ int main(int argc, char* argv[]) {
211 init_windows(); 243 init_windows();
212 244
213 while(true) { 245 while(true) {
246 // Update tox.
214 do_tox(); 247 do_tox();
215 248
249 // Draw.
216 a = &windows[w_active]; 250 a = &windows[w_active];
251 prepare_window(a->window);
217 a->onDraw(a); 252 a->onDraw(a);
218 draw_bar(); 253 draw_bar();
219 254
255 // Handle input.
220 ch = getch(); 256 ch = getch();
221 if(ch != ERR) { 257 if(ch == '\t') {
258 w_active = (w_active + 1) % w_num;
259 }
260 else if(ch != ERR) {
222 a->onKey(a, ch); 261 a->onKey(a, ch);
223 } 262 }
224 263
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 @@
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,7 +42,6 @@ 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.
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
211static void prompt_onKey(ToxWindow* self, int key) { 236static void prompt_onKey(ToxWindow* self, int key) {
@@ -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
@@ -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