diff options
Diffstat (limited to 'testing')
-rw-r--r-- | testing/toxic/chat.c | 98 | ||||
-rw-r--r-- | testing/toxic/prompt.c | 19 |
2 files changed, 93 insertions, 24 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index a90bb2aa..936eb868 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -26,6 +26,8 @@ typedef struct { | |||
26 | } ChatContext; | 26 | } ChatContext; |
27 | 27 | ||
28 | extern void fix_name(uint8_t* name); | 28 | extern void fix_name(uint8_t* name); |
29 | void print_help(ChatContext* self); | ||
30 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd); | ||
29 | 31 | ||
30 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { | 32 | static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) { |
31 | ChatContext* ctx = (ChatContext*) self->x; | 33 | ChatContext* ctx = (ChatContext*) self->x; |
@@ -49,6 +51,7 @@ static void chat_onMessage(ToxWindow* self, int num, uint8_t* msg, uint16_t len) | |||
49 | 51 | ||
50 | wattron(ctx->history, COLOR_PAIR(2)); | 52 | wattron(ctx->history, COLOR_PAIR(2)); |
51 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 53 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
54 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
52 | wattron(ctx->history, COLOR_PAIR(4)); | 55 | wattron(ctx->history, COLOR_PAIR(4)); |
53 | wprintw(ctx->history, "%s: ", nick); | 56 | wprintw(ctx->history, "%s: ", nick); |
54 | wattroff(ctx->history, COLOR_PAIR(4)); | 57 | wattroff(ctx->history, COLOR_PAIR(4)); |
@@ -97,41 +100,93 @@ static void chat_onKey(ToxWindow* self, int key) { | |||
97 | struct tm * timeinfo; | 100 | struct tm * timeinfo; |
98 | timeinfo = localtime(&now); | 101 | timeinfo = localtime(&now); |
99 | 102 | ||
103 | /* PRINTABLE characters: Add to line */ | ||
100 | if(isprint(key)) { | 104 | if(isprint(key)) { |
101 | |||
102 | if(ctx->pos != sizeof(ctx->line)-1) { | 105 | if(ctx->pos != sizeof(ctx->line)-1) { |
103 | ctx->line[ctx->pos++] = key; | 106 | ctx->line[ctx->pos++] = key; |
104 | ctx->line[ctx->pos] = '\0'; | 107 | ctx->line[ctx->pos] = '\0'; |
105 | } | 108 | } |
106 | } | 109 | } |
107 | 110 | ||
111 | /* RETURN key: Execute command or print line */ | ||
108 | else if(key == '\n') { | 112 | else if(key == '\n') { |
109 | if(!string_is_empty(ctx->line)) { | 113 | if (ctx->line[0] == '/') |
110 | /* make sure the string has at least non-space character */ | 114 | execute(self, ctx, ctx->line); |
111 | wattron(ctx->history, COLOR_PAIR(2)); | 115 | else { |
112 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 116 | if(!string_is_empty(ctx->line)) { |
113 | wattron(ctx->history, COLOR_PAIR(1)); | 117 | /* make sure the string has at least non-space character */ |
114 | wprintw(ctx->history, "you: ", ctx->line); | 118 | wattron(ctx->history, COLOR_PAIR(2)); |
115 | wattroff(ctx->history, COLOR_PAIR(1)); | 119 | wprintw(ctx->history, "%02d:%02d:%02d ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
116 | wprintw(ctx->history, "%s\n", ctx->line); | 120 | wattroff(ctx->history, COLOR_PAIR(2)); |
117 | 121 | wattron(ctx->history, COLOR_PAIR(1)); | |
122 | wprintw(ctx->history, "you: ", ctx->line); | ||
123 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
124 | wprintw(ctx->history, "%s\n", ctx->line); | ||
125 | } | ||
118 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { | 126 | if(m_sendmessage(ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) < 0) { |
119 | wattron(ctx->history, COLOR_PAIR(3)); | 127 | wattron(ctx->history, COLOR_PAIR(3)); |
120 | wprintw(ctx->history, " * Failed to send message.\n"); | 128 | wprintw(ctx->history, " * Failed to send message.\n"); |
121 | wattroff(ctx->history, COLOR_PAIR(3)); | 129 | wattroff(ctx->history, COLOR_PAIR(3)); |
122 | } | 130 | } |
123 | |||
124 | ctx->line[0] = '\0'; | ||
125 | ctx->pos = 0; | ||
126 | } | 131 | } |
132 | ctx->line[0] = '\0'; | ||
133 | ctx->pos = 0; | ||
127 | } | 134 | } |
128 | 135 | ||
136 | /* BACKSPACE key: Remove one character from line */ | ||
129 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 137 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
130 | if(ctx->pos != 0) { | 138 | if(ctx->pos != 0) { |
131 | ctx->line[--ctx->pos] = '\0'; | 139 | ctx->line[--ctx->pos] = '\0'; |
132 | } | 140 | } |
133 | } | 141 | } |
142 | } | ||
134 | 143 | ||
144 | void execute(ToxWindow* self, ChatContext* ctx, char* cmd) | ||
145 | { | ||
146 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | ||
147 | wclear(self->window); | ||
148 | wclear(ctx->history); | ||
149 | } | ||
150 | else if (!strcmp(cmd, "/help") || !strcmp(cmd, "/h")) | ||
151 | print_help(ctx); | ||
152 | else if (!strcmp(cmd, "/quit") || !strcmp(cmd, "/exit") || !strcmp(cmd, "/q")) { | ||
153 | endwin(); | ||
154 | exit(0); | ||
155 | } | ||
156 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | ||
157 | char* msg; | ||
158 | msg = strchr(cmd, ' '); | ||
159 | if(msg == NULL) { | ||
160 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
161 | return; | ||
162 | } | ||
163 | msg++; | ||
164 | m_set_userstatus((uint8_t*) msg, strlen(msg)+1); | ||
165 | wprintw(ctx->history, "Status set to: %s\n", msg); | ||
166 | } | ||
167 | else if (!strncmp(cmd, "/nick ", strlen("/nick "))) { | ||
168 | char* nick; | ||
169 | nick = strchr(cmd, ' '); | ||
170 | if(nick == NULL) { | ||
171 | wprintw(ctx->history, "Invalid syntax.\n"); | ||
172 | return; | ||
173 | } | ||
174 | nick++; | ||
175 | setname((uint8_t*) nick, strlen(nick)+1); | ||
176 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | ||
177 | } | ||
178 | else if(!strcmp(cmd, "/myid")) { | ||
179 | char id[32*2 + 1] = {0}; | ||
180 | int i; | ||
181 | for (i = 0; i < 32; i++) { | ||
182 | char xx[3]; | ||
183 | snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); | ||
184 | strcat(id, xx); | ||
185 | } | ||
186 | wprintw(ctx->history, "Your ID: %s\n", id); | ||
187 | } | ||
188 | else | ||
189 | wprintw(ctx->history, "Invalid command.\n"); | ||
135 | } | 190 | } |
136 | 191 | ||
137 | static void chat_onDraw(ToxWindow* self) { | 192 | static void chat_onDraw(ToxWindow* self) { |
@@ -164,6 +219,21 @@ static void chat_onInit(ToxWindow* self) { | |||
164 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); | 219 | ctx->linewin = subwin(self->window, 2, x, y - 3, 0); |
165 | } | 220 | } |
166 | 221 | ||
222 | void print_help(ChatContext* self) { | ||
223 | wattron(self->history, COLOR_PAIR(2) | A_BOLD); | ||
224 | wprintw(self->history, "\nCommands:\n"); | ||
225 | wattroff(self->history, A_BOLD); | ||
226 | |||
227 | wprintw(self->history, " /status <message> : Set your status\n"); | ||
228 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | ||
229 | wprintw(self->history, " /myid : Print your ID\n"); | ||
230 | wprintw(self->history, " /clear : Clear the screen\n"); | ||
231 | wprintw(self->history, " /quit or /exit : Exit program\n"); | ||
232 | wprintw(self->history, " /help : Print this message again\n\n"); | ||
233 | |||
234 | wattroff(self->history, COLOR_PAIR(2)); | ||
235 | } | ||
236 | |||
167 | ToxWindow new_chat(int friendnum) { | 237 | ToxWindow new_chat(int friendnum) { |
168 | ToxWindow ret; | 238 | ToxWindow ret; |
169 | 239 | ||
@@ -186,6 +256,6 @@ ToxWindow new_chat(int friendnum) { | |||
186 | x->friendnum = friendnum; | 256 | x->friendnum = friendnum; |
187 | 257 | ||
188 | ret.x = (void*) x; | 258 | ret.x = (void*) x; |
189 | 259 | free(x); | |
190 | return ret; | 260 | return ret; |
191 | } | 261 | } |
diff --git a/testing/toxic/prompt.c b/testing/toxic/prompt.c index 1db60883..463b9352 100644 --- a/testing/toxic/prompt.c +++ b/testing/toxic/prompt.c | |||
@@ -89,7 +89,9 @@ static void execute(ToxWindow* self, char* cmd) { | |||
89 | } | 89 | } |
90 | 90 | ||
91 | dht.ip.i = resolved_address; | 91 | dht.ip.i = resolved_address; |
92 | DHT_bootstrap(dht, hex_string_to_bin(key)); | 92 | unsigned char *binary_string = hex_string_to_bin(key); |
93 | DHT_bootstrap(dht, binary_string); | ||
94 | free(binary_string); | ||
93 | } | 95 | } |
94 | else if(!strncmp(cmd, "add ", strlen("add "))) { | 96 | else if(!strncmp(cmd, "add ", strlen("add "))) { |
95 | uint8_t id_bin[32]; | 97 | uint8_t id_bin[32]; |
@@ -148,7 +150,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
148 | wprintw(self->window, "Friend request already sent.\n"); | 150 | wprintw(self->window, "Friend request already sent.\n"); |
149 | break; | 151 | break; |
150 | case -5: | 152 | case -5: |
151 | wprintw(self->window, "[i] Undefined error when adding friend.\n"); | 153 | wprintw(self->window, "Undefined error when adding friend.\n"); |
152 | break; | 154 | break; |
153 | default: | 155 | default: |
154 | wprintw(self->window, "Friend added as %d.\n", num); | 156 | wprintw(self->window, "Friend added as %d.\n", num); |
@@ -178,12 +180,13 @@ static void execute(ToxWindow* self, char* cmd) { | |||
178 | 180 | ||
179 | nick = strchr(cmd, ' '); | 181 | nick = strchr(cmd, ' '); |
180 | if(nick == NULL) { | 182 | if(nick == NULL) { |
183 | wprintw(self->window, "Invalid syntax.\n"); | ||
181 | return; | 184 | return; |
182 | } | 185 | } |
183 | nick++; | 186 | nick++; |
184 | 187 | ||
185 | setname((uint8_t*) nick, strlen(nick)+1); | 188 | setname((uint8_t*) nick, strlen(nick)+1); |
186 | wprintw(self->window, "Nickname set to: %s.\n", nick); | 189 | wprintw(self->window, "Nickname set to: %s\n", nick); |
187 | } | 190 | } |
188 | else if(!strcmp(cmd, "myid")) { | 191 | else if(!strcmp(cmd, "myid")) { |
189 | char id[32*2 + 1] = {0}; | 192 | char id[32*2 + 1] = {0}; |
@@ -195,7 +198,7 @@ static void execute(ToxWindow* self, char* cmd) { | |||
195 | strcat(id, xx); | 198 | strcat(id, xx); |
196 | } | 199 | } |
197 | 200 | ||
198 | wprintw(self->window, "%s\n", id); | 201 | wprintw(self->window, "Your ID: %s\n", id); |
199 | } | 202 | } |
200 | else if(!strncmp(cmd, "accept ", strlen("accept "))) { | 203 | else if(!strncmp(cmd, "accept ", strlen("accept "))) { |
201 | char* id; | 204 | char* id; |
@@ -251,20 +254,18 @@ static void execute(ToxWindow* self, char* cmd) { | |||
251 | wprintw(self->window, "Message successfully sent.\n"); | 254 | wprintw(self->window, "Message successfully sent.\n"); |
252 | } | 255 | } |
253 | } | 256 | } |
257 | |||
254 | else { | 258 | else { |
255 | wprintw(self->window, "Invalid syntax.\n"); | 259 | wprintw(self->window, "Invalid command.\n"); |
256 | } | 260 | } |
257 | } | 261 | } |
258 | 262 | ||
259 | static void prompt_onKey(ToxWindow* self, int key) { | 263 | static void prompt_onKey(ToxWindow* self, int key) { |
260 | |||
261 | // PRINTABLE characters: Add to line. | 264 | // PRINTABLE characters: Add to line. |
262 | if(isprint(key)) { | 265 | if(isprint(key)) { |
263 | |||
264 | if(prompt_buf_pos == (sizeof(prompt_buf) - 1)) { | 266 | if(prompt_buf_pos == (sizeof(prompt_buf) - 1)) { |
265 | return; | 267 | return; |
266 | } | 268 | } |
267 | |||
268 | prompt_buf[prompt_buf_pos++] = key; | 269 | prompt_buf[prompt_buf_pos++] = key; |
269 | prompt_buf[prompt_buf_pos] = 0; | 270 | prompt_buf[prompt_buf_pos] = 0; |
270 | } | 271 | } |
@@ -273,14 +274,12 @@ static void prompt_onKey(ToxWindow* self, int key) { | |||
273 | else if(key == '\n') { | 274 | else if(key == '\n') { |
274 | wprintw(self->window, "\n"); | 275 | wprintw(self->window, "\n"); |
275 | execute(self, prompt_buf); | 276 | execute(self, prompt_buf); |
276 | |||
277 | prompt_buf_pos = 0; | 277 | prompt_buf_pos = 0; |
278 | prompt_buf[0] = 0; | 278 | prompt_buf[0] = 0; |
279 | } | 279 | } |
280 | 280 | ||
281 | // BACKSPACE key: Remove one character from line. | 281 | // BACKSPACE key: Remove one character from line. |
282 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { | 282 | else if(key == 0x107 || key == 0x8 || key == 0x7f) { |
283 | |||
284 | if(prompt_buf_pos != 0) { | 283 | if(prompt_buf_pos != 0) { |
285 | prompt_buf[--prompt_buf_pos] = 0; | 284 | prompt_buf[--prompt_buf_pos] = 0; |
286 | } | 285 | } |