summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-06 16:59:34 -0700
committerirungentoo <irungentoo@gmail.com>2013-08-06 16:59:34 -0700
commit804216580d1db3ad9ad79da5f1f1183b5aec2568 (patch)
tree97bc0945c567d8cdbfdedfd00077a485fdbedaaa /testing
parentba520d15583430d974c7fa7306cb7ba3fcac6759 (diff)
parent16b3ec746e9ab518a1efa221524391f8f692f75b (diff)
Merge pull request #363 from JFreegman/master
code format/clean up
Diffstat (limited to 'testing')
-rw-r--r--testing/toxic/chat.c108
-rw-r--r--testing/toxic/friendlist.c104
-rw-r--r--testing/toxic/main.c217
-rw-r--r--testing/toxic/prompt.c284
4 files changed, 334 insertions, 379 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index 7262e722..20c01620 100644
--- a/testing/toxic/chat.c
+++ b/testing/toxic/chat.c
@@ -16,38 +16,34 @@
16 16
17typedef struct { 17typedef struct {
18 int friendnum; 18 int friendnum;
19
20 char line[256]; 19 char line[256];
21 size_t pos; 20 size_t pos;
22
23 WINDOW* history; 21 WINDOW* history;
24 WINDOW* linewin; 22 WINDOW* linewin;
25
26} ChatContext; 23} ChatContext;
27 24
28extern int w_active; 25extern int active_window;
26
29extern void del_window(ToxWindow *w, int f_num); 27extern void del_window(ToxWindow *w, int f_num);
30extern void fix_name(uint8_t* name); 28extern void fix_name(uint8_t *name);
31void print_help(ChatContext* self); 29void print_help(ChatContext *self);
32void execute(ToxWindow* self, ChatContext* ctx, char* cmd); 30void execute(ToxWindow *self, ChatContext *ctx, char *cmd);
33 31
34static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { 32static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len)
35 ChatContext* ctx = (ChatContext*) self->x; 33{
34 ChatContext *ctx = (ChatContext*) self->x;
36 uint8_t nick[MAX_NAME_LENGTH] = {0}; 35 uint8_t nick[MAX_NAME_LENGTH] = {0};
37
38 time_t now; 36 time_t now;
39 time(&now); 37 time(&now);
40 struct tm * timeinfo; 38 struct tm * timeinfo;
41 timeinfo = localtime(&now); 39 timeinfo = localtime(&now);
42 40
43 if(ctx->friendnum != num) 41 if (ctx->friendnum != num)
44 return; 42 return;
45 43
46 getname(num, (uint8_t*) &nick); 44 getname(num, (uint8_t*) &nick);
47
48 msg[len-1] = '\0'; 45 msg[len-1] = '\0';
49 nick[MAX_NAME_LENGTH-1] = '\0'; 46 nick[MAX_NAME_LENGTH-1] = '\0';
50
51 fix_name(msg); 47 fix_name(msg);
52 fix_name(nick); 48 fix_name(nick);
53 49
@@ -64,15 +60,14 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len)
64 flash(); 60 flash();
65} 61}
66 62
67static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t len) { 63static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len)
68 ChatContext* ctx = (ChatContext*) self->x; 64{
69 65 ChatContext *ctx = (ChatContext*) self->x;
70 if(ctx->friendnum != num) 66 if (ctx->friendnum != num)
71 return; 67 return;
72 68
73 nick[len-1] = '\0'; 69 nick[len-1] = '\0';
74 fix_name(nick); 70 fix_name(nick);
75
76 snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); 71 snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
77 72
78 wattron(ctx->history, COLOR_PAIR(3)); 73 wattron(ctx->history, COLOR_PAIR(3));
@@ -80,7 +75,8 @@ static void chat_onNickChange(ToxWindow* self, int num, uint8_t* nick, uint16_t
80 wattroff(ctx->history, COLOR_PAIR(3)); 75 wattroff(ctx->history, COLOR_PAIR(3));
81} 76}
82 77
83static void chat_onStatusChange(ToxWindow* self, int num, uint8_t* status, uint16_t len) { 78static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len)
79{
84 80
85} 81}
86 82
@@ -89,35 +85,33 @@ int string_is_empty(char *string)
89{ 85{
90 int rc = 0; 86 int rc = 0;
91 char *copy = strdup(string); 87 char *copy = strdup(string);
92
93 rc = ((strtok(copy, " ") == NULL) ? 1:0); 88 rc = ((strtok(copy, " ") == NULL) ? 1:0);
94 free(copy); 89 free(copy);
95
96 return rc; 90 return rc;
97} 91}
98 92
99static void chat_onKey(ToxWindow* self, int key) { 93static void chat_onKey(ToxWindow *self, int key)
100 ChatContext* ctx = (ChatContext*) self->x; 94{
101 95 ChatContext *ctx = (ChatContext*) self->x;
102 time_t now; 96 time_t now;
103 time(&now); 97 time(&now);
104 struct tm * timeinfo; 98 struct tm * timeinfo;
105 timeinfo = localtime(&now); 99 timeinfo = localtime(&now);
106 100
107 /* PRINTABLE characters: Add to line */ 101 /* Add printable characters to line */
108 if(isprint(key)) { 102 if (isprint(key)) {
109 if(ctx->pos != sizeof(ctx->line)-1) { 103 if (ctx->pos != sizeof(ctx->line)-1) {
110 ctx->line[ctx->pos++] = key; 104 ctx->line[ctx->pos++] = key;
111 ctx->line[ctx->pos] = '\0'; 105 ctx->line[ctx->pos] = '\0';
112 } 106 }
113 } 107 }
114 108
115 /* RETURN key: Execute command or print line */ 109 /* RETURN key: Execute command or print line */
116 else if(key == '\n') { 110 else if (key == '\n') {
117 if (ctx->line[0] == '/') 111 if (ctx->line[0] == '/')
118 execute(self, ctx, ctx->line); 112 execute(self, ctx, ctx->line);
119 else { 113 else {
120 if(!string_is_empty(ctx->line)) { 114 if (!string_is_empty(ctx->line)) {
121 /* make sure the string has at least non-space character */ 115 /* make sure the string has at least non-space character */
122 wattron(ctx->history, COLOR_PAIR(2)); 116 wattron(ctx->history, COLOR_PAIR(2));
123 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); 117 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
@@ -127,7 +121,7 @@ static void chat_onKey(ToxWindow* self, int key) {
127 wattroff(ctx->history, COLOR_PAIR(1)); 121 wattroff(ctx->history, COLOR_PAIR(1));
128 wprintw(ctx->history, "%s\n", ctx->line); 122 wprintw(ctx->history, "%s\n", ctx->line);
129 } 123 }
130 if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { 124 if (m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) {
131 wattron(ctx->history, COLOR_PAIR(3)); 125 wattron(ctx->history, COLOR_PAIR(3));
132 wprintw(ctx->history, " * Failed to send message.\n"); 126 wprintw(ctx->history, " * Failed to send message.\n");
133 wattroff(ctx->history, COLOR_PAIR(3)); 127 wattroff(ctx->history, COLOR_PAIR(3));
@@ -138,29 +132,32 @@ static void chat_onKey(ToxWindow* self, int key) {
138 } 132 }
139 133
140 /* BACKSPACE key: Remove one character from line */ 134 /* BACKSPACE key: Remove one character from line */
141 else if(key == 0x107 || key == 0x8 || key == 0x7f) { 135 else if (key == 0x107 || key == 0x8 || key == 0x7f) {
142 if(ctx->pos != 0) { 136 if (ctx->pos != 0) {
143 ctx->line[--ctx->pos] = '\0'; 137 ctx->line[--ctx->pos] = '\0';
144 } 138 }
145 } 139 }
146} 140}
147 141
148void execute(ToxWindow* self, ChatContext* ctx, char* cmd) 142void execute(ToxWindow *self, ChatContext *ctx, char *cmd)
149{ 143{
150 if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { 144 if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) {
151 wclear(self->window); 145 wclear(self->window);
152 wclear(ctx->history); 146 wclear(ctx->history);
153 } 147 }
148
154 else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) 149 else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h"))
155 print_help(ctx); 150 print_help(ctx);
151
156 else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { 152 else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) {
157 endwin(); 153 endwin();
158 exit(0); 154 exit(0);
159 } 155 }
156
160 else if (!strncmp(cmd, "/status ", strlen("/status "))) { 157 else if (!strncmp(cmd, "/status ", strlen("/status "))) {
161 char* msg; 158 char *msg;
162 msg = strchr(cmd, ' '); 159 msg = strchr(cmd, ' ');
163 if(msg == NULL) { 160 if (msg == NULL) {
164 wprintw(ctx->history, "Invalid syntax.\n"); 161 wprintw(ctx->history, "Invalid syntax.\n");
165 return; 162 return;
166 } 163 }
@@ -168,10 +165,11 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
168 m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1); 165 m_set_userstatus(USERSTATUS_KIND_RETAIN, (uint8_t*) msg, strlen(msg)+1);
169 wprintw(ctx->history, "Status set to: %s\n", msg); 166 wprintw(ctx->history, "Status set to: %s\n", msg);
170 } 167 }
168
171 else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { 169 else if (!strncmp(cmd, "/nick ", strlen("/nick "))) {
172 char* nick; 170 char *nick;
173 nick = strchr(cmd, ' '); 171 nick = strchr(cmd, ' ');
174 if(nick == NULL) { 172 if (nick == NULL) {
175 wprintw(ctx->history, "Invalid syntax.\n"); 173 wprintw(ctx->history, "Invalid syntax.\n");
176 return; 174 return;
177 } 175 }
@@ -179,7 +177,8 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
179 setname((uint8_t*) nick, strlen(nick)+1); 177 setname((uint8_t*) nick, strlen(nick)+1);
180 wprintw(ctx->history, "Nickname set to: %s\n", nick); 178 wprintw(ctx->history, "Nickname set to: %s\n", nick);
181 } 179 }
182 else if(!strcmp(cmd, "/myid")) { 180
181 else if (!strcmp(cmd, "/myid")) {
183 char id[32*2 + 1] = {0}; 182 char id[32*2 + 1] = {0};
184 int i; 183 int i;
185 for (i = 0; i < 32; i++) { 184 for (i = 0; i < 32; i++) {
@@ -189,48 +188,46 @@ void execute(ToxWindow* self, ChatContext* ctx, char* cmd)
189 } 188 }
190 wprintw(ctx->history, "Your ID: %s\n", id); 189 wprintw(ctx->history, "Your ID: %s\n", id);
191 } 190 }
191
192 else if (strcmp(ctx->line, "/close") == 0) { 192 else if (strcmp(ctx->line, "/close") == 0) {
193 w_active = 0; // Go to prompt screen 193 active_window = 0; // Go to prompt screen
194 int f_num = ctx->friendnum; 194 int f_num = ctx->friendnum;
195 delwin(ctx->linewin); 195 delwin(ctx->linewin);
196 del_window(self, f_num); 196 del_window(self, f_num);
197 } 197 }
198
198 else 199 else
199 wprintw(ctx->history, "Invalid command.\n"); 200 wprintw(ctx->history, "Invalid command.\n");
200} 201}
201 202
202static void chat_onDraw(ToxWindow* self) { 203static void chat_onDraw(ToxWindow *self)
204{
203 curs_set(1); 205 curs_set(1);
204 int x, y; 206 int x, y;
205 ChatContext* ctx = (ChatContext*) self->x; 207 ChatContext *ctx = (ChatContext*) self->x;
206
207 getmaxyx(self->window, y, x); 208 getmaxyx(self->window, y, x);
208
209 (void) x; 209 (void) x;
210 if(y < 3) 210 if (y < 3) return;
211 return;
212 211
213 wclear(ctx->linewin); 212 wclear(ctx->linewin);
214 mvwhline(ctx->linewin, 0, 0, '_', COLS); 213 mvwhline(ctx->linewin, 0, 0, '_', COLS);
215 mvwprintw(self->window, y-1, 0, "%s\n", ctx->line); 214 mvwprintw(self->window, y-1, 0, "%s\n", ctx->line);
216
217 wrefresh(self->window); 215 wrefresh(self->window);
218} 216}
219 217
220static void chat_onInit(ToxWindow* self) { 218static void chat_onInit(ToxWindow *self)
219{
221 int x, y; 220 int x, y;
222 ChatContext* ctx = (ChatContext*) self->x; 221 ChatContext *ctx = (ChatContext*) self->x;
223
224 getmaxyx(self->window, y, x); 222 getmaxyx(self->window, y, x);
225
226 ctx->history = subwin(self->window, y - 4, x, 0, 0); 223 ctx->history = subwin(self->window, y - 4, x, 0, 0);
227 scrollok(ctx->history, 1); 224 scrollok(ctx->history, 1);
228
229 ctx->linewin = subwin(self->window, 2, x, y - 3, 0); 225 ctx->linewin = subwin(self->window, 2, x, y - 3, 0);
230 print_help(ctx); 226 print_help(ctx);
231} 227}
232 228
233void print_help(ChatContext* self) { 229void print_help(ChatContext *self)
230{
234 wattron(self->history, COLOR_PAIR(2) | A_BOLD); 231 wattron(self->history, COLOR_PAIR(2) | A_BOLD);
235 wprintw(self->history, "Commands:\n"); 232 wprintw(self->history, "Commands:\n");
236 wattroff(self->history, A_BOLD); 233 wattroff(self->history, A_BOLD);
@@ -246,9 +243,9 @@ void print_help(ChatContext* self) {
246 wattroff(self->history, COLOR_PAIR(2)); 243 wattroff(self->history, COLOR_PAIR(2));
247} 244}
248 245
249ToxWindow new_chat(int friendnum) { 246ToxWindow new_chat(int friendnum)
247{
250 ToxWindow ret; 248 ToxWindow ret;
251
252 memset(&ret, 0, sizeof(ret)); 249 memset(&ret, 0, sizeof(ret));
253 250
254 ret.onKey = &chat_onKey; 251 ret.onKey = &chat_onKey;
@@ -264,9 +261,8 @@ ToxWindow new_chat(int friendnum) {
264 261
265 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); 262 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
266 263
267 ChatContext* x = calloc(1, sizeof(ChatContext)); 264 ChatContext *x = calloc(1, sizeof(ChatContext));
268 x->friendnum = friendnum; 265 x->friendnum = friendnum;
269
270 ret.x = (void*) x; 266 ret.x = (void*) x;
271 return ret; 267 return ret;
272} 268}
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c
index 94e8fb47..f03914e6 100644
--- a/testing/toxic/friendlist.c
+++ b/testing/toxic/friendlist.c
@@ -15,60 +15,56 @@
15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM]; 15extern char WINDOW_STATUS[TOXWINDOWS_MAX_NUM];
16extern int add_window(ToxWindow w, int n); 16extern int add_window(ToxWindow w, int n);
17extern ToxWindow new_chat(int friendnum); 17extern ToxWindow new_chat(int friendnum);
18extern int w_active; 18
19extern int active_window;
19 20
20typedef struct { 21typedef struct {
21 uint8_t name[MAX_NAME_LENGTH]; 22 uint8_t name[MAX_NAME_LENGTH];
22 uint8_t status[MAX_USERSTATUS_LENGTH]; 23 uint8_t status[MAX_USERSTATUS_LENGTH];
23 int num; 24 int num;
24 int chatwin; 25 int chatwin;
25} friend_t; 26} friend_t;
26 27
27static friend_t friends[MAX_FRIENDS_NUM]; 28static friend_t friends[MAX_FRIENDS_NUM];
28static int num_friends = 0; 29static int num_friends = 0;
29static int num_selected = 0; 30static int num_selected = 0;
30 31
31 32void fix_name(uint8_t *name)
32void fix_name(uint8_t* name) { 33{
33 34 /* Remove all non alphanumeric characters */
34 // Remove all non alphanumeric characters. 35 uint8_t *p = name;
35 uint8_t* p = name; 36 uint8_t *q = name;
36 uint8_t* q = name;
37
38 while(*p != 0) { 37 while(*p != 0) {
39 if(isprint(*p)) { 38 if (isprint(*p))
40 *q++ = *p; 39 *q++ = *p;
41 }
42
43 p++; 40 p++;
44 } 41 }
45
46 *q = 0; 42 *q = 0;
47} 43}
48 44
49void friendlist_onMessage(ToxWindow* self, int num, uint8_t* str, uint16_t len) { 45void friendlist_onMessage(ToxWindow *self, int num, uint8_t *str, uint16_t len)
50 46{
51 if(num >= num_friends) 47 if (num >= num_friends)
52 return; 48 return;
53 49
54 if(friends[num].chatwin == -1) { 50 if (friends[num].chatwin == -1) {
55 friends[num].chatwin = num; 51 friends[num].chatwin = num;
56 int i; 52 int i;
57 /* Find first open slot to hold chat window */ 53 /* Find first open slot to hold chat window */
58 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { 54 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
59 if (WINDOW_STATUS[i] == -1) { 55 if (WINDOW_STATUS[i] == -1) {
60 WINDOW_STATUS[i] = num; 56 WINDOW_STATUS[i] = num;
61 add_window(new_chat(num), i); 57 add_window(new_chat(num), i);
62 w_active = i; 58 active_window = i;
63 break; 59 break;
64 } 60 }
65 } 61 }
66 } 62 }
67} 63}
68 64
69void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { 65void friendlist_onNickChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
70 66{
71 if(len >= MAX_NAME_LENGTH || num >= num_friends) 67 if (len >= MAX_NAME_LENGTH || num >= num_friends)
72 return; 68 return;
73 69
74 memcpy((char*) &friends[num].name, (char*) str, len); 70 memcpy((char*) &friends[num].name, (char*) str, len);
@@ -76,9 +72,9 @@ void friendlist_onNickChange(ToxWindow* self, int num, uint8_t* str, uint16_t le
76 fix_name(friends[num].name); 72 fix_name(friends[num].name);
77} 73}
78 74
79void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t len) { 75void friendlist_onStatusChange(ToxWindow *self, int num, uint8_t *str, uint16_t len)
80 76{
81 if(len >= MAX_USERSTATUS_LENGTH || num >= num_friends) 77 if (len >= MAX_USERSTATUS_LENGTH || num >= num_friends)
82 return; 78 return;
83 79
84 memcpy((char*) &friends[num].status, (char*) str, len); 80 memcpy((char*) &friends[num].status, (char*) str, len);
@@ -86,9 +82,9 @@ void friendlist_onStatusChange(ToxWindow* self, int num, uint8_t* str, uint16_t
86 fix_name(friends[num].status); 82 fix_name(friends[num].status);
87} 83}
88 84
89int friendlist_onFriendAdded(int num) { 85int friendlist_onFriendAdded(int num)
90 86{
91 if(num_friends == MAX_FRIENDS_NUM) 87 if (num_friends == MAX_FRIENDS_NUM)
92 return -1; 88 return -1;
93 89
94 friends[num_friends].num = num; 90 friends[num_friends].num = num;
@@ -99,34 +95,34 @@ int friendlist_onFriendAdded(int num) {
99 return 0; 95 return 0;
100} 96}
101 97
102static void friendlist_onKey(ToxWindow* self, int key) { 98static void friendlist_onKey(ToxWindow *self, int key)
103 if(key == KEY_UP) { 99{
104 num_selected--; 100 if (key == KEY_UP) {
105 if (num_selected < 0) 101 if (--num_selected < 0)
106 num_selected = num_friends-1; 102 num_selected = num_friends-1;
107 } 103 }
108 else if(key == KEY_DOWN) { 104 else if (key == KEY_DOWN) {
109 if(num_friends != 0) 105 if (num_friends != 0)
110 num_selected = (num_selected+1) % num_friends; 106 num_selected = (num_selected+1) % num_friends;
111 } 107 }
112 else if(key == '\n') { 108 else if (key == '\n') {
113 /* Jump to chat window if already open */ 109 /* Jump to chat window if already open */
114 if (friends[num_selected].chatwin != -1) { 110 if (friends[num_selected].chatwin != -1) {
115 int i; 111 int i;
116 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { 112 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
117 if (WINDOW_STATUS[i] == num_selected) { 113 if (WINDOW_STATUS[i] == num_selected) {
118 w_active = i; 114 active_window = i;
119 break; 115 break;
120 } 116 }
121 } 117 }
122 }else { 118 }else {
123 int i; 119 int i;
124 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { 120 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
125 if (WINDOW_STATUS[i] == -1) { 121 if (WINDOW_STATUS[i] == -1) {
126 WINDOW_STATUS[i] = num_selected; 122 WINDOW_STATUS[i] = num_selected;
127 friends[num_selected].chatwin = num_selected; 123 friends[num_selected].chatwin = num_selected;
128 add_window(new_chat(num_selected), i); 124 add_window(new_chat(num_selected), i);
129 w_active = i; 125 active_window = i;
130 break; 126 break;
131 } 127 }
132 } 128 }
@@ -134,13 +130,11 @@ static void friendlist_onKey(ToxWindow* self, int key) {
134 } 130 }
135} 131}
136 132
137static void friendlist_onDraw(ToxWindow* self) { 133static void friendlist_onDraw(ToxWindow *self)
134{
138 curs_set(0); 135 curs_set(0);
139 size_t i;
140
141 werase(self->window); 136 werase(self->window);
142 137 if (num_friends == 0) {
143 if(num_friends == 0) {
144 wprintw(self->window, "Empty. Add some friends! :-)\n"); 138 wprintw(self->window, "Empty. Add some friends! :-)\n");
145 } 139 }
146 else { 140 else {
@@ -150,12 +144,11 @@ static void friendlist_onDraw(ToxWindow* self) {
150 } 144 }
151 145
152 wprintw(self->window, "\n"); 146 wprintw(self->window, "\n");
153 147 int i;
154 for(i=0; i<num_friends; i++) { 148 for (i = 0; i < num_friends; ++i) {
155 149 if (i == num_selected) wattron(self->window, COLOR_PAIR(3));
156 if(i == num_selected) wattron(self->window, COLOR_PAIR(3));
157 wprintw(self->window, " [#%d] ", friends[i].num); 150 wprintw(self->window, " [#%d] ", friends[i].num);
158 if(i == num_selected) wattroff(self->window, COLOR_PAIR(3)); 151 if (i == num_selected) wattroff(self->window, COLOR_PAIR(3));
159 152
160 attron(A_BOLD); 153 attron(A_BOLD);
161 wprintw(self->window, "%s ", friends[i].name); 154 wprintw(self->window, "%s ", friends[i].name);
@@ -163,22 +156,21 @@ static void friendlist_onDraw(ToxWindow* self) {
163 156
164 wprintw(self->window, "(%s)\n", friends[i].status); 157 wprintw(self->window, "(%s)\n", friends[i].status);
165 } 158 }
166
167 wrefresh(self->window); 159 wrefresh(self->window);
168} 160}
169 161
170void disable_chatwin(int f_num) { 162void disable_chatwin(int f_num)
163{
171 friends[f_num].chatwin = -1; 164 friends[f_num].chatwin = -1;
172} 165}
173 166
174static void friendlist_onInit(ToxWindow* self) { 167static void friendlist_onInit(ToxWindow *self)
168{
175 169
176} 170}
177 171
178
179ToxWindow new_friendlist() { 172ToxWindow new_friendlist() {
180 ToxWindow ret; 173 ToxWindow ret;
181
182 memset(&ret, 0, sizeof(ret)); 174 memset(&ret, 0, sizeof(ret));
183 175
184 ret.onKey = &friendlist_onKey; 176 ret.onKey = &friendlist_onKey;
@@ -187,7 +179,7 @@ ToxWindow new_friendlist() {
187 ret.onMessage = &friendlist_onMessage; 179 ret.onMessage = &friendlist_onMessage;
188 ret.onNickChange = &friendlist_onNickChange; 180 ret.onNickChange = &friendlist_onNickChange;
189 ret.onStatusChange = &friendlist_onStatusChange; 181 ret.onStatusChange = &friendlist_onStatusChange;
190 strcpy(ret.title, "[friends]");
191 182
183 strcpy(ret.title, "[friends]");
192 return ret; 184 return ret;
193} 185}
diff --git a/testing/toxic/main.c b/testing/toxic/main.c
index 19a0b959..1ba8b6c9 100644
--- a/testing/toxic/main.c
+++ b/testing/toxic/main.c
@@ -18,106 +18,108 @@ extern ToxWindow new_friendlist();
18 18
19extern int friendlist_onFriendAdded(int num); 19extern int friendlist_onFriendAdded(int num);
20extern void disable_chatwin(int f_num); 20extern void disable_chatwin(int f_num);
21extern int add_req(uint8_t* public_key); // XXX 21extern int add_req(uint8_t *public_key); // XXX
22
23/* Holds status of chat windows */
24char WINDOW_STATUS[MAX_WINDOW_SLOTS];
22 25
23char WINDOW_STATUS[MAX_WINDOW_SLOTS]; // Holds status of chat windows
24static ToxWindow windows[MAX_WINDOW_SLOTS]; 26static ToxWindow windows[MAX_WINDOW_SLOTS];
25int w_num;
26int w_active;
27static ToxWindow* prompt; 27static ToxWindow* prompt;
28 28
29// CALLBACKS START 29int w_num;
30void on_request(uint8_t* public_key, uint8_t* data, uint16_t length) { 30int active_window;
31 size_t i;
32 int n = add_req(public_key);
33 31
32/* CALLBACKS START */
33void on_request(uint8_t *public_key, uint8_t *data, uint16_t length)
34{
35 int n = add_req(public_key);
34 wprintw(prompt->window, "\nFriend request from:\n"); 36 wprintw(prompt->window, "\nFriend request from:\n");
35 37
36 for(i=0; i<32; i++) { 38 int i;
39 for (i = 0; i < 32; ++i) {
37 wprintw(prompt->window, "%02x", public_key[i] & 0xff); 40 wprintw(prompt->window, "%02x", public_key[i] & 0xff);
38 } 41 }
39 wprintw(prompt->window, "\n");
40 42
43 wprintw(prompt->window, "\n");
41 wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); 44 wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n);
42 45 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
43 for(i=0; i<MAX_WINDOW_SLOTS; i++) { 46 if (windows[i].onFriendRequest != NULL)
44 if(windows[i].onFriendRequest != NULL)
45 windows[i].onFriendRequest(&windows[i], public_key, data, length); 47 windows[i].onFriendRequest(&windows[i], public_key, data, length);
46 } 48 }
47} 49}
48 50
49void on_message(int friendnumber, uint8_t* string, uint16_t length) { 51void on_message(int friendnumber, uint8_t *string, uint16_t length)
50 size_t i; 52{
51
52 wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string); 53 wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string);
53 54 int i;
54 for(i=0; i<MAX_WINDOW_SLOTS; i++) { 55 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
55 if(windows[i].onMessage != NULL) 56 if (windows[i].onMessage != NULL)
56 windows[i].onMessage(&windows[i], friendnumber, string, length); 57 windows[i].onMessage(&windows[i], friendnumber, string, length);
57 } 58 }
58} 59}
59 60
60void on_nickchange(int friendnumber, uint8_t* string, uint16_t length) { 61void on_nickchange(int friendnumber, uint8_t *string, uint16_t length)
61 size_t i; 62{
62
63 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); 63 wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string);
64 64 int i;
65 for(i=0; i<MAX_WINDOW_SLOTS; i++) { 65 for (i = 0; i < MAX_WINDOW_SLOTS; ++i) {
66 if(windows[i].onNickChange != NULL) 66 if (windows[i].onNickChange != NULL)
67 windows[i].onNickChange(&windows[i], friendnumber, string, length); 67 windows[i].onNickChange(&windows[i], friendnumber, string, length);
68 } 68 }
69} 69}
70 70
71void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t* string, uint16_t length) { 71void on_statuschange(int friendnumber, USERSTATUS_KIND kind, uint8_t *string, uint16_t length)
72 size_t i; 72{
73
74 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string); 73 wprintw(prompt->window, "\n(statuschange) %d: %s\n", friendnumber, string);
75 74 int i;
76 for(i=0; i<MAX_WINDOW_SLOTS; i++) { 75 for (i=0; i<MAX_WINDOW_SLOTS; ++i) {
77 if(windows[i].onStatusChange != NULL) 76 if (windows[i].onStatusChange != NULL)
78 windows[i].onStatusChange(&windows[i], friendnumber, string, length); 77 windows[i].onStatusChange(&windows[i], friendnumber, string, length);
79 } 78 }
80} 79}
81 80
82void on_friendadded(int friendnumber) { 81void on_friendadded(int friendnumber)
82{
83 friendlist_onFriendAdded(friendnumber); 83 friendlist_onFriendAdded(friendnumber);
84} 84}
85// CALLBACKS END 85/* CALLBACKS END */
86 86
87static void init_term() { 87static void init_term()
88 // Setup terminal. 88{
89 /* Setup terminal */
89 initscr(); 90 initscr();
90 cbreak(); 91 cbreak();
91 keypad(stdscr, 1); 92 keypad(stdscr, 1);
92 noecho(); 93 noecho();
93 timeout(100); 94 timeout(100);
94 95
95 if(has_colors()) { 96 if (has_colors()) {
96 start_color(); 97 start_color();
97 init_pair(1, COLOR_GREEN, COLOR_BLACK); 98 init_pair(1, COLOR_GREEN, COLOR_BLACK);
98 init_pair(2, COLOR_CYAN, COLOR_BLACK); 99 init_pair(2, COLOR_CYAN, COLOR_BLACK);
99 init_pair(3, COLOR_RED, COLOR_BLACK); 100 init_pair(3, COLOR_RED, COLOR_BLACK);
100 init_pair(4, COLOR_BLUE, COLOR_BLACK); 101 init_pair(4, COLOR_BLUE, COLOR_BLACK);
101 } 102 }
102
103 refresh(); 103 refresh();
104} 104}
105 105
106static void init_tox() { 106static void init_tox()
107 // Init core. 107{
108 /* Init core */
108 initMessenger(); 109 initMessenger();
109 110
110 // Callbacks. 111 /* Callbacks */
111 m_callback_friendrequest(on_request); 112 m_callback_friendrequest(on_request);
112 m_callback_friendmessage(on_message); 113 m_callback_friendmessage(on_message);
113 m_callback_namechange(on_nickchange); 114 m_callback_namechange(on_nickchange);
114 m_callback_userstatus(on_statuschange); 115 m_callback_userstatus(on_statuschange);
115} 116}
116 117
117void init_window_status() { 118void init_window_status()
119{
118 /* Default window values decrement from -2 */ 120 /* Default window values decrement from -2 */
119 int i; 121 int i;
120 for (i = 0; i < N_DEFAULT_WINS; i++) 122 for (i = 0; i < N_DEFAULT_WINS; ++i)
121 WINDOW_STATUS[i] = -(i+2); 123 WINDOW_STATUS[i] = -(i+2);
122 124
123 int j; 125 int j;
@@ -125,15 +127,16 @@ void init_window_status() {
125 WINDOW_STATUS[j] = -1; 127 WINDOW_STATUS[j] = -1;
126} 128}
127 129
128int add_window(ToxWindow w, int n) { 130int add_window(ToxWindow w, int n)
129 if(w_num >= TOXWINDOWS_MAX_NUM) 131{
132 if (w_num >= TOXWINDOWS_MAX_NUM)
130 return -1; 133 return -1;
131 134
132 if(LINES < 2) 135 if (LINES < 2)
133 return -1; 136 return -1;
134 137
135 w.window = newwin(LINES - 2, COLS, 0, 0); 138 w.window = newwin(LINES - 2, COLS, 0, 0);
136 if(w.window == NULL) 139 if (w.window == NULL)
137 return -1; 140 return -1;
138 141
139 windows[n] = w; 142 windows[n] = w;
@@ -143,10 +146,11 @@ int add_window(ToxWindow w, int n) {
143} 146}
144 147
145/* Deletes window w and cleans up */ 148/* Deletes window w and cleans up */
146void del_window(ToxWindow *w, int f_num) { 149void del_window(ToxWindow *w, int f_num)
150{
147 delwin(w->window); 151 delwin(w->window);
148 int i; 152 int i;
149 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; i++) { 153 for (i = N_DEFAULT_WINS; i < MAX_WINDOW_SLOTS; ++i) {
150 if (WINDOW_STATUS[i] == f_num) { 154 if (WINDOW_STATUS[i] == f_num) {
151 WINDOW_STATUS[i] = -1; 155 WINDOW_STATUS[i] = -1;
152 disable_chatwin(f_num); 156 disable_chatwin(f_num);
@@ -157,11 +161,12 @@ void del_window(ToxWindow *w, int f_num) {
157 refresh(); 161 refresh();
158} 162}
159 163
160static void init_windows() { 164static void init_windows()
165{
161 w_num = 0; 166 w_num = 0;
162 int n_prompt = 0; 167 int n_prompt = 0;
163 int n_friendslist = 1; 168 int n_friendslist = 1;
164 if(add_window(new_prompt(), n_prompt) == -1 169 if (add_window(new_prompt(), n_prompt) == -1
165 || add_window(new_friendlist(), n_friendslist) == -1) { 170 || add_window(new_friendlist(), n_friendslist) == -1) {
166 fprintf(stderr, "add_window() failed.\n"); 171 fprintf(stderr, "add_window() failed.\n");
167 endwin(); 172 endwin();
@@ -170,88 +175,79 @@ static void init_windows() {
170 prompt = &windows[n_prompt]; 175 prompt = &windows[n_prompt];
171} 176}
172 177
173static void do_tox() { 178static void do_tox()
179{
174 static bool dht_on = false; 180 static bool dht_on = false;
175 181 if (!dht_on && DHT_isconnected()) {
176 if(!dht_on && DHT_isconnected()) {
177 dht_on = true; 182 dht_on = true;
178 wprintw(prompt->window, "\nDHT connected!\n"); 183 wprintw(prompt->window, "\nDHT connected!\n");
179 } 184 }
180 else if(dht_on && !DHT_isconnected()) { 185 else if (dht_on && !DHT_isconnected()) {
181 dht_on = false; 186 dht_on = false;
182 wprintw(prompt->window, "\nDHT disconnected!\n"); 187 wprintw(prompt->window, "\nDHT disconnected!\n");
183 } 188 }
184
185 doMessenger(); 189 doMessenger();
186} 190}
187 191
188static void load_data(char *path) { 192static void load_data(char *path)
189 FILE* fd; 193{
194 FILE *fd;
190 size_t len; 195 size_t len;
191 uint8_t* buf; 196 uint8_t *buf;
192 197
193 if((fd = fopen(path, "r")) != NULL) { 198 if ((fd = fopen(path, "r")) != NULL) {
194 fseek(fd, 0, SEEK_END); 199 fseek(fd, 0, SEEK_END);
195 len = ftell(fd); 200 len = ftell(fd);
196 fseek(fd, 0, SEEK_SET); 201 fseek(fd, 0, SEEK_SET);
197 202
198 buf = malloc(len); 203 buf = malloc(len);
199 204 if (buf == NULL) {
200 if(buf == NULL) {
201 fprintf(stderr, "malloc() failed.\n"); 205 fprintf(stderr, "malloc() failed.\n");
202
203 fclose(fd); 206 fclose(fd);
204 endwin(); 207 endwin();
205 exit(1); 208 exit(1);
206 } 209 }
207 210 if (fread(buf, len, 1, fd) != 1){
208 if(fread(buf, len, 1, fd) != 1){
209 fprintf(stderr, "fread() failed.\n"); 211 fprintf(stderr, "fread() failed.\n");
210
211 free(buf); 212 free(buf);
212 fclose(fd); 213 fclose(fd);
213 endwin(); 214 endwin();
214 exit(1); 215 exit(1);
215 } 216 }
216
217 Messenger_load(buf, len); 217 Messenger_load(buf, len);
218 } 218 }
219 else { 219 else {
220 len = Messenger_size(); 220 len = Messenger_size();
221 buf = malloc(len); 221 buf = malloc(len);
222 222 if (buf == NULL) {
223 if(buf == NULL) {
224 fprintf(stderr, "malloc() failed.\n"); 223 fprintf(stderr, "malloc() failed.\n");
225 endwin(); 224 endwin();
226 exit(1); 225 exit(1);
227 } 226 }
228
229 Messenger_save(buf); 227 Messenger_save(buf);
230 228
231 fd = fopen(path, "w"); 229 fd = fopen(path, "w");
232 if(fd == NULL) { 230 if (fd == NULL) {
233 fprintf(stderr, "fopen() failed.\n"); 231 fprintf(stderr, "fopen() failed.\n");
234
235 free(buf); 232 free(buf);
236 endwin(); 233 endwin();
237 exit(1); 234 exit(1);
238 } 235 }
239 236
240 if(fwrite(buf, len, 1, fd) != 1){ 237 if (fwrite(buf, len, 1, fd) != 1){
241 fprintf(stderr, "fwrite() failed.\n"); 238 fprintf(stderr, "fwrite() failed.\n");
242
243 free(buf); 239 free(buf);
244 fclose(fd); 240 fclose(fd);
245 endwin(); 241 endwin();
246 exit(1); 242 exit(1);
247 } 243 }
248 } 244 }
249
250 free(buf); 245 free(buf);
251 fclose(fd); 246 fclose(fd);
252} 247}
253 248
254static void draw_bar() { 249static void draw_bar()
250{
255 static int odd = 0; 251 static int odd = 0;
256 252
257 attron(COLOR_PAIR(4)); 253 attron(COLOR_PAIR(4));
@@ -265,21 +261,21 @@ static void draw_bar() {
265 attroff(COLOR_PAIR(4) | A_BOLD); 261 attroff(COLOR_PAIR(4) | A_BOLD);
266 262
267 int i; 263 int i;
268 for (i = 0; i < (MAX_WINDOW_SLOTS-1); i++) { 264 for (i = 0; i < (MAX_WINDOW_SLOTS); ++i) {
269 if (WINDOW_STATUS[i] != -1) { 265 if (WINDOW_STATUS[i] != -1) {
270 if (i == w_active) 266 if (i == active_window)
271 attron(A_BOLD); 267 attron(A_BOLD);
272 268
273 odd = (odd+1) % 10; 269 odd = (odd+1) % 10;
274 if(windows[i].blink && (odd < 5)) { 270 if (windows[i].blink && (odd < 5)) {
275 attron(COLOR_PAIR(3)); 271 attron(COLOR_PAIR(3));
276 } 272 }
277 273
278 printw(" %s", windows[i].title); 274 printw(" %s", windows[i].title);
279 if(windows[i].blink && (odd < 5)) { 275 if (windows[i].blink && (odd < 5)) {
280 attron(COLOR_PAIR(3)); 276 attron(COLOR_PAIR(3));
281 } 277 }
282 if(i == w_active) { 278 if (i == active_window) {
283 attroff(A_BOLD); 279 attroff(A_BOLD);
284 } 280 }
285 } 281 }
@@ -287,35 +283,37 @@ static void draw_bar() {
287 refresh(); 283 refresh();
288} 284}
289 285
290void prepare_window(WINDOW* w) { 286void prepare_window(WINDOW *w)
287{
291 mvwin(w, 0, 0); 288 mvwin(w, 0, 0);
292 wresize(w, LINES-2, COLS); 289 wresize(w, LINES-2, COLS);
293} 290}
294 291
295/* Shows next window when tab or back-tab is pressed */ 292/* Shows next window when tab or back-tab is pressed */
296void set_active_window(int ch) { 293void set_active_window(int ch)
294{
297 int f_inf = 0; 295 int f_inf = 0;
298 int max = MAX_WINDOW_SLOTS-1; 296 int max = MAX_WINDOW_SLOTS-1;
299 if (ch == '\t') { 297 if (ch == '\t') {
300 int i = (w_active + 1) % max; 298 int i = (active_window + 1) % max;
301 while (true) { 299 while (true) {
302 if (WINDOW_STATUS[i] != -1) { 300 if (WINDOW_STATUS[i] != -1) {
303 w_active = i; 301 active_window = i;
304 return; 302 return;
305 } 303 }
306 i = (i + 1) % max; 304 i = (i + 1) % max;
307 if (f_inf++ > max) { // infinite loop check 305 if (f_inf++ > max) { // infinite loop check
308 endwin(); 306 endwin();
309 clear(); 307 clear();
310 exit(2); 308 exit(2);
311 } 309 }
312 } 310 }
313 }else { 311 }else {
314 int i = w_active - 1; 312 int i = active_window - 1;
315 if (i < 0) i = max; 313 if (i < 0) i = max;
316 while (true) { 314 while (true) {
317 if (WINDOW_STATUS[i] != -1) { 315 if (WINDOW_STATUS[i] != -1) {
318 w_active = i; 316 active_window = i;
319 return; 317 return;
320 } 318 }
321 if (--i < 0) i = max; 319 if (--i < 0) i = max;
@@ -328,26 +326,26 @@ void set_active_window(int ch) {
328 } 326 }
329} 327}
330 328
331int main(int argc, char* argv[]) { 329int main(int argc, char *argv[])
330{
332 int ch; 331 int ch;
333 int i = 0;
334 int f_flag = 0; 332 int f_flag = 0;
335 char *filename = "data"; 333 char *filename = "data";
336 ToxWindow* a; 334 ToxWindow* a;
337 335
338 for(i = 0; i < argc; i++) { 336 int i = 0;
339 if (argv[i] == NULL){ 337 for (i = 0; i < argc; ++i) {
340 break; 338 if (argv[i] == NULL)
341 } else if(argv[i][0] == '-') { 339 break;
342 if(argv[i][1] == 'f') { 340 else if (argv[i][0] == '-') {
343 if(argv[i + 1] != NULL) 341 if (argv[i][1] == 'f') {
344 filename = argv[i + 1]; 342 if (argv[i + 1] != NULL)
345 else { 343 filename = argv[i + 1];
346 f_flag = -1; 344 else
347 } 345 f_flag = -1;
348 } 346 }
349 }
350 } 347 }
348 }
351 349
352 init_term(); 350 init_term();
353 init_tox(); 351 init_tox();
@@ -355,7 +353,7 @@ int main(int argc, char* argv[]) {
355 init_windows(); 353 init_windows();
356 init_window_status(); 354 init_window_status();
357 355
358 if(f_flag == -1) { 356 if (f_flag == -1) {
359 attron(COLOR_PAIR(3) | A_BOLD); 357 attron(COLOR_PAIR(3) | A_BOLD);
360 wprintw(prompt->window, "You passed '-f' without giving an argument!\n" 358 wprintw(prompt->window, "You passed '-f' without giving an argument!\n"
361 "defaulting to 'data' for a keyfile...\n"); 359 "defaulting to 'data' for a keyfile...\n");
@@ -363,24 +361,21 @@ int main(int argc, char* argv[]) {
363 } 361 }
364 362
365 while(true) { 363 while(true) {
366 // Update tox. 364 /* Update tox */
367 do_tox(); 365 do_tox();
368 366
369 // Draw. 367 /* Draw */
370 a = &windows[w_active]; 368 a = &windows[active_window];
371 prepare_window(a->window); 369 prepare_window(a->window);
372 a->blink = false; 370 a->blink = false;
373 draw_bar(); 371 draw_bar();
374 a->onDraw(a); 372 a->onDraw(a);
375 373
376 // Handle input. 374 /* Handle input */
377 ch = getch(); 375 ch = getch();
378 if(ch == '\t' || ch == KEY_BTAB) 376 if (ch == '\t' || ch == KEY_BTAB)
379 set_active_window(ch); 377 set_active_window(ch);
380 else if(ch != ERR) { 378 else if (ch != ERR) {
381 a->onKey(a, ch);
382 }
383 else if(ch != ERR) {
384 a->onKey(a, ch); 379 a->onKey(a, ch);
385 } 380 }
386 } 381 }
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c
index 20f6b480..89c87d8f 100644
--- a/testing/toxic/prompt.c
+++ b/testing/toxic/prompt.c
@@ -16,83 +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* u_cmd) { 46 char cmd[256] = {0};
45 int i; 47 int i;
46 int newlines = 0; 48 for (i = 0; i < strlen(prompt_buf); ++i) {
47 char cmd[256] = {0};
48 for(i = 0; i < strlen(prompt_buf); i++)
49 {
50 if (u_cmd[i] == '\n') 49 if (u_cmd[i] == '\n')
51 ++newlines; 50 ++newlines;
52 else 51 else
53 cmd[i - newlines] = u_cmd[i]; 52 cmd[i - newlines] = u_cmd[i];
54 } 53 }
55 54
56 if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { 55 if (!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) {
57 endwin(); 56 endwin();
58 exit(0); 57 exit(0);
59 } 58 }
60 else if(!strncmp(cmd, "connect ", strlen("connect "))) {
61 char* ip;
62 char* port;
63 char* key;
64 IP_Port dht;
65 59
66 ip = strchr(cmd, ' '); 60 else if (!strncmp(cmd, "connect ", strlen("connect "))) {
67 if(ip == NULL) { 61 IP_Port dht;
62 char *ip = strchr(cmd, ' ');
63 if (ip == NULL) {
68 wprintw(self->window, "Invalid syntax.\n"); 64 wprintw(self->window, "Invalid syntax.\n");
69 return; 65 return;
70 } 66 }
71 ip++; 67 ip++;
72 68
73 port = strchr(ip, ' '); 69 char *port = strchr(ip, ' ');
74 if(port == NULL) { 70 if (port == NULL) {
75 wprintw(self->window, "Invalid syntax.\n"); 71 wprintw(self->window, "Invalid syntax.\n");
76 return; 72 return;
77 } 73 }
78 port[0] = 0; 74 port[0] = 0;
79 port++; 75 port++;
80 76
81 key = strchr(port, ' '); 77 char *key = strchr(port, ' ');
82 if(key == NULL) { 78 if (key == NULL) {
83 wprintw(self->window, "Invalid syntax.\n"); 79 wprintw(self->window, "Invalid syntax.\n");
84 return; 80 return;
85 } 81 }
86 key[0] = 0; 82 key[0] = 0;
87 key++; 83 key++;
88 84
89 if(atoi(port) == 0) { 85 if (atoi(port) == 0) {
90 wprintw(self->window, "Invalid syntax.\n"); 86 wprintw(self->window, "Invalid syntax.\n");
91 return; 87 return;
92 } 88 }
93 89
94 dht.port = htons(atoi(port)); 90 dht.port = htons(atoi(port));
95
96 uint32_t resolved_address = resolve_addr(ip); 91 uint32_t resolved_address = resolve_addr(ip);
97 if (resolved_address == 0) { 92 if (resolved_address == 0) {
98 return; 93 return;
@@ -103,49 +98,39 @@ static void execute(ToxWindow* self, char* u_cmd) {
103 DHT_bootstrap(dht, binary_string); 98 DHT_bootstrap(dht, binary_string);
104 free(binary_string); 99 free(binary_string);
105 } 100 }
106 else if(!strncmp(cmd, "add ", strlen("add "))) { 101
102 else if (!strncmp(cmd, "add ", strlen("add "))) {
107 uint8_t id_bin[32]; 103 uint8_t id_bin[32];
108 size_t i;
109 char xx[3]; 104 char xx[3];
110 uint32_t x; 105 uint32_t x;
111 106 char *id = strchr(cmd, ' ');
112 char* id; 107 if (id == NULL) {
113 char* msg;
114 int num;
115
116 id = strchr(cmd, ' ');
117 if(id == NULL) {
118 wprintw(self->window, "Invalid syntax.\n"); 108 wprintw(self->window, "Invalid syntax.\n");
119 return; 109 return;
120 } 110 }
121 id++; 111 id++;
122 112 char *msg = strchr(id, ' ');
123 msg = strchr(id, ' '); 113 if (msg != NULL) {
124 if(msg != NULL) {
125 msg[0] = 0; 114 msg[0] = 0;
126 msg++; 115 msg++;
127 } 116 }
128 else msg = ""; 117 else msg = "";
129 118 if (strlen(id) != 2*32) {
130 if(strlen(id) != 2*32) {
131 wprintw(self->window, "Invalid ID length.\n"); 119 wprintw(self->window, "Invalid ID length.\n");
132 return; 120 return;
133 } 121 }
134 122 int i;
135 for(i=0; i<32; i++) { 123 for (i = 0; i < 32; ++i) {
136 xx[0] = id[2*i]; 124 xx[0] = id[2*i];
137 xx[1] = id[2*i+1]; 125 xx[1] = id[2*i+1];
138 xx[2] = '\0'; 126 xx[2] = '\0';
139 127 if (sscanf(xx, "%02x", &x) != 1) {
140 if(sscanf(xx, "%02x", &x) != 1) {
141 wprintw(self->window, "Invalid ID.\n"); 128 wprintw(self->window, "Invalid ID.\n");
142 return; 129 return;
143 } 130 }
144
145 id_bin[i] = x; 131 id_bin[i] = x;
146 } 132 }
147 133 int num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
148 num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1);
149 switch (num) { 134 switch (num) {
150 case -1: 135 case -1:
151 wprintw(self->window, "Message is too long.\n"); 136 wprintw(self->window, "Message is too long.\n");
@@ -168,178 +153,167 @@ static void execute(ToxWindow* self, char* u_cmd) {
168 break; 153 break;
169 } 154 }
170 } 155 }
171 else if(!strcmp(cmd, "clear")) { 156
172 wclear(self->window); 157 else if (!strcmp(cmd, "clear")) {
158 wclear(self->window);
173 } 159 }
174 else if(!strcmp(cmd, "help")) { 160
175 wclear(self->window); 161 else if (!strcmp(cmd, "help")) {
176 print_usage(self); 162 wclear(self->window);
163 print_usage(self);
177 } 164 }
178 else if(!strncmp(cmd, "status ", strlen("status "))) {
179 char* msg;
180 165
181 msg = strchr(cmd, ' '); 166 else if (!strncmp(cmd, "status ", strlen("status "))) {
182 if(msg == NULL) { 167 char *msg = strchr(cmd, ' ');
168 if (msg == NULL) {
183 wprintw(self->window, "Invalid syntax.\n"); 169 wprintw(self->window, "Invalid syntax.\n");
184 return; 170 return;
185 } 171 }
186 msg++; 172 msg++;
187
188 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);
189 wprintw(self->window, "Status set to: %s\n", msg); 174 wprintw(self->window, "Status set to: %s\n", msg);
190 } 175 }
191 else if(!strncmp(cmd, "nick ", strlen("nick "))) {
192 char* nick;
193 176
194 nick = strchr(cmd, ' '); 177 else if (!strncmp(cmd, "nick ", strlen("nick "))) {
195 if(nick == NULL) { 178 char *nick = strchr(cmd, ' ');
179 if (nick == NULL) {
196 wprintw(self->window, "Invalid syntax.\n"); 180 wprintw(self->window, "Invalid syntax.\n");
197 return; 181 return;
198 } 182 }
199 nick++; 183 nick++;
200
201 setname((uint8_t*) nick, strlen(nick)+1); 184 setname((uint8_t*) nick, strlen(nick)+1);
202 wprintw(self->window, "Nickname set to: %s\n", nick); 185 wprintw(self->window, "Nickname set to: %s\n", nick);
203 } 186 }
204 else if(!strcmp(cmd, "myid")) { 187
188 else if (!strcmp(cmd, "myid")) {
205 char id[32*2 + 1] = {0}; 189 char id[32*2 + 1] = {0};
206 size_t i; 190 size_t i;
207 191 for (i = 0; i < 32; ++i) {
208 for(i=0; i<32; i++) {
209 char xx[3]; 192 char xx[3];
210 snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); 193 snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff);
211 strcat(id, xx); 194 strcat(id, xx);
212 } 195 }
213
214 wprintw(self->window, "Your ID: %s\n", id); 196 wprintw(self->window, "Your ID: %s\n", id);
215 } 197 }
216 else if(!strncmp(cmd, "accept ", strlen("accept "))) {
217 char* id;
218 int num;
219 198
220 id = strchr(cmd, ' '); 199 else if (!strncmp(cmd, "accept ", strlen("accept "))) {
221 if(id == NULL) { 200 char *id = strchr(cmd, ' ');
201 if (id == NULL) {
222 wprintw(self->window, "Invalid syntax.\n"); 202 wprintw(self->window, "Invalid syntax.\n");
223 return; 203 return;
224 } 204 }
225 id++; 205 id++;
226 206
227 num = atoi(id); 207 int num = atoi(id);
228 if(num >= num_requests) { 208 if (num >= num_requests) {
229 wprintw(self->window, "Invalid syntax.\n"); 209 wprintw(self->window, "Invalid syntax.\n");
230 return; 210 return;
231 } 211 }
232 212
233 num = m_addfriend_norequest(pending_requests[num]); 213 num = m_addfriend_norequest(pending_requests[num]);
234 214 if (num == -1)
235 if(num == -1) {
236 wprintw(self->window, "Failed to add friend.\n"); 215 wprintw(self->window, "Failed to add friend.\n");
237 }
238 else { 216 else {
239 wprintw(self->window, "Friend accepted as: %d.\n", num); 217 wprintw(self->window, "Friend accepted as: %d.\n", num);
240 on_friendadded(num); 218 on_friendadded(num);
241 } 219 }
242 } 220 }
243 else if(!strncmp(cmd, "msg ", strlen("msg "))) {
244 char* id;
245 char* msg;
246
247 id = strchr(cmd, ' ');
248 221
249 if(id == NULL) { 222 else if (!strncmp(cmd, "msg ", strlen("msg "))) {
223 char *id = strchr(cmd, ' ');
224 if (id == NULL) {
250 wprintw(self->window, "Invalid syntax.\n"); 225 wprintw(self->window, "Invalid syntax.\n");
251 return; 226 return;
252 } 227 }
253 id++; 228 char *msg = strchr(++id, ' ');
254 229 if (msg == NULL) {
255 msg = strchr(id, ' ');
256 if(msg == NULL) {
257 wprintw(self->window, "Invalid syntax.\n"); 230 wprintw(self->window, "Invalid syntax.\n");
258 return; 231 return;
259 } 232 }
260 msg[0] = 0; 233 msg[0] = 0;
261 msg++; 234 msg++;
262 235 if (m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0)
263 if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) {
264 wprintw(self->window, "Error occurred while sending message.\n"); 236 wprintw(self->window, "Error occurred while sending message.\n");
265 } 237 else
266 else {
267 wprintw(self->window, "Message successfully sent.\n"); 238 wprintw(self->window, "Message successfully sent.\n");
268 }
269 } 239 }
270 else { 240 else
271 wprintw(self->window, "Invalid command.\n"); 241 wprintw(self->window, "Invalid command.\n");
272 }
273} 242}
274 243
275static void prompt_onKey(ToxWindow* self, int key) { 244static void prompt_onKey(ToxWindow *self, int key)
276 // PRINTABLE characters: Add to line. 245{
277 if(isprint(key)) { 246 /* Add printable characters to line */
278 if (prompt_buf_pos == (sizeof(prompt_buf) - 1)){ 247 if (isprint(key)) {
279 wprintw(self->window, "\nToo Long.\n"); 248 if (prompt_buf_pos == (sizeof(prompt_buf) - 1)) {
280 prompt_buf_pos = 0; 249 wprintw(self->window, "\nToo Long.\n");
281 prompt_buf[0] = 0; 250 prompt_buf_pos = 0;
282 } 251 prompt_buf[0] = 0;
283 else if(!(prompt_buf_pos == 0) && (prompt_buf_pos < COLS) && (prompt_buf_pos % (COLS - 3) == 0)) { 252 }
284 prompt_buf[prompt_buf_pos++] = '\n'; 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';
285 } 256 }
286 else if(!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS) && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) { 257 else if (!(prompt_buf_pos == 0) && (prompt_buf_pos > COLS)
287 prompt_buf[prompt_buf_pos++] = '\n'; 258 && ((prompt_buf_pos - (COLS - 3)) % (COLS) == 0)) {
259 prompt_buf[prompt_buf_pos++] = '\n';
288 } 260 }
289 prompt_buf[prompt_buf_pos++] = key; 261 prompt_buf[prompt_buf_pos++] = key;
290 prompt_buf[prompt_buf_pos] = 0; 262 prompt_buf[prompt_buf_pos] = 0;
291 } 263 }
292 264
293 // RETURN key: execute command. 265 /* RETURN key: execute command */
294 else if(key == '\n') { 266 else if (key == '\n') {
295 wprintw(self->window, "\n"); 267 wprintw(self->window, "\n");
296 execute(self, prompt_buf); 268 execute(self, prompt_buf);
297 prompt_buf_pos = 0; 269 prompt_buf_pos = 0;
298 prompt_buf[0] = 0; 270 prompt_buf[0] = 0;
299 } 271 }
300 272
301 // BACKSPACE key: Remove one character from line. 273 /* BACKSPACE key: Remove one character from line */
302 else if(key == 0x107 || key == 0x8 || key == 0x7f) { 274 else if (key == 0x107 || key == 0x8 || key == 0x7f) {
303 if(prompt_buf_pos != 0) { 275 if (prompt_buf_pos != 0) {
304 prompt_buf[--prompt_buf_pos] = 0; 276 prompt_buf[--prompt_buf_pos] = 0;
305 } 277 }
306 } 278 }
307} 279}
308 280
309static void prompt_onDraw(ToxWindow* self) { 281static void prompt_onDraw(ToxWindow *self)
310 curs_set(1); 282{
311 int x, y; 283 curs_set(1);
312 getyx(self->window, y, x); 284 int x, y;
313 (void) x; 285 getyx(self->window, y, x);
314 int i; 286 (void) x;
315 for (i = 0; i < (strlen(prompt_buf)); i++) 287 int i;
316 { 288 for (i = 0; i < (strlen(prompt_buf)); ++i) {
317 if ((prompt_buf[i] == '\n') && (y != 0)) 289 if ((prompt_buf[i] == '\n') && (y != 0))
318 --y; 290 --y;
319 } 291 }
320 wattron(self->window, COLOR_PAIR(1)); 292
321 mvwprintw(self->window, y, 0, "# "); 293 wattron(self->window, COLOR_PAIR(1));
322 wattroff(self->window, COLOR_PAIR(1)); 294 mvwprintw(self->window, y, 0, "# ");
323 mvwprintw(self->window, y, 2, "%s", prompt_buf); 295 wattroff(self->window, COLOR_PAIR(1));
324 wclrtoeol(self->window); 296 mvwprintw(self->window, y, 2, "%s", prompt_buf);
325 wrefresh(self->window); 297 wclrtoeol(self->window);
298 wrefresh(self->window);
326} 299}
327 300
328static void print_usage(ToxWindow* self) { 301static void print_usage(ToxWindow *self)
302{
329 wattron(self->window, COLOR_PAIR(2) | A_BOLD); 303 wattron(self->window, COLOR_PAIR(2) | A_BOLD);
330 wprintw(self->window, "Commands:\n"); 304 wprintw(self->window, "Commands:\n");
331 wattroff(self->window, A_BOLD); 305 wattroff(self->window, A_BOLD);
332
333 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
334 wprintw(self->window, " add <id> <message> : Add friend\n");
335 wprintw(self->window, " status <message> : Set your status\n");
336 wprintw(self->window, " nick <nickname> : Set your nickname\n");
337 wprintw(self->window, " accept <number> : Accept friend request\n");
338 wprintw(self->window, " myid : Print your ID\n");
339 wprintw(self->window, " quit/exit : Exit program\n");
340 wprintw(self->window, " help : Print this message again\n");
341 wprintw(self->window, " clear : Clear this window\n");
342 306
307 wprintw(self->window, " connect <ip> <port> <key> : Connect to DHT server\n");
308 wprintw(self->window, " add <id> <message> : Add friend\n");
309 wprintw(self->window, " status <message> : Set your status\n");
310 wprintw(self->window, " nick <nickname> : Set your nickname\n");
311 wprintw(self->window, " accept <number> : Accept friend request\n");
312 wprintw(self->window, " myid : Print your ID\n");
313 wprintw(self->window, " quit/exit : Exit program\n");
314 wprintw(self->window, " help : Print this message again\n");
315 wprintw(self->window, " clear : Clear this window\n");
316
343 wattron(self->window, A_BOLD); 317 wattron(self->window, A_BOLD);
344 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");
345 wattroff(self->window, A_BOLD); 319 wattroff(self->window, A_BOLD);
@@ -347,22 +321,20 @@ static void print_usage(ToxWindow* self) {
347 wattroff(self->window, COLOR_PAIR(2)); 321 wattroff(self->window, COLOR_PAIR(2));
348} 322}
349 323
350static void prompt_onInit(ToxWindow* self) { 324static void prompt_onInit(ToxWindow *self)
325{
351 scrollok(self->window, 1); 326 scrollok(self->window, 1);
352
353 print_usage(self); 327 print_usage(self);
354 wclrtoeol(self->window); 328 wclrtoeol(self->window);
355} 329}
356 330
357ToxWindow new_prompt() { 331ToxWindow new_prompt()
332{
358 ToxWindow ret; 333 ToxWindow ret;
359
360 memset(&ret, 0, sizeof(ret)); 334 memset(&ret, 0, sizeof(ret));
361
362 ret.onKey = &prompt_onKey; 335 ret.onKey = &prompt_onKey;
363 ret.onDraw = &prompt_onDraw; 336 ret.onDraw = &prompt_onDraw;
364 ret.onInit = &prompt_onInit; 337 ret.onInit = &prompt_onInit;
365 strcpy(ret.title, "[prompt]"); 338 strcpy(ret.title, "[prompt]");
366
367 return ret; 339 return ret;
368} 340}