diff options
-rw-r--r-- | testing/toxic/chat.c | 81 | ||||
-rw-r--r-- | testing/toxic/main.c | 14 |
2 files changed, 64 insertions, 31 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 59b13492..32c05dec 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -29,16 +29,22 @@ extern int active_window; | |||
29 | extern void del_window(ToxWindow *w, int f_num); | 29 | extern void del_window(ToxWindow *w, int f_num); |
30 | extern void fix_name(uint8_t *name); | 30 | extern void fix_name(uint8_t *name); |
31 | void print_help(ChatContext *self); | 31 | void print_help(ChatContext *self); |
32 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo); | 32 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd); |
33 | 33 | ||
34 | static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) | 34 | struct tm *get_time(void) |
35 | { | 35 | { |
36 | ChatContext *ctx = (ChatContext*) self->x; | 36 | struct tm *timeinfo; |
37 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | ||
38 | time_t now; | 37 | time_t now; |
39 | time(&now); | 38 | time(&now); |
40 | struct tm *timeinfo; | ||
41 | timeinfo = localtime(&now); | 39 | timeinfo = localtime(&now); |
40 | return timeinfo; | ||
41 | } | ||
42 | |||
43 | static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) | ||
44 | { | ||
45 | ChatContext *ctx = (ChatContext*) self->x; | ||
46 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | ||
47 | struct tm *timeinfo = get_time(); | ||
42 | 48 | ||
43 | if (ctx->friendnum != num) | 49 | if (ctx->friendnum != num) |
44 | return; | 50 | return; |
@@ -64,10 +70,7 @@ static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, | |||
64 | static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) | 70 | static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) |
65 | { | 71 | { |
66 | ChatContext *ctx = (ChatContext*) self->x; | 72 | ChatContext *ctx = (ChatContext*) self->x; |
67 | time_t now; | 73 | struct tm *timeinfo = get_time(); |
68 | time(&now); | ||
69 | struct tm *timeinfo; | ||
70 | timeinfo = localtime(&now); | ||
71 | 74 | ||
72 | if (ctx->friendnum != num) | 75 | if (ctx->friendnum != num) |
73 | return; | 76 | return; |
@@ -79,9 +82,9 @@ static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *actio | |||
79 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 82 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
80 | wattroff(ctx->history, COLOR_PAIR(2)); | 83 | wattroff(ctx->history, COLOR_PAIR(2)); |
81 | 84 | ||
82 | wattron(ctx->history, COLOR_PAIR(4)); | 85 | wattron(ctx->history, COLOR_PAIR(5)); |
83 | wprintw(ctx->history, "%s\n", action); | 86 | wprintw(ctx->history, "%s\n", action); |
84 | wattroff(ctx->history, COLOR_PAIR(4)); | 87 | wattroff(ctx->history, COLOR_PAIR(5)); |
85 | 88 | ||
86 | self->blink = true; | 89 | self->blink = true; |
87 | beep(); | 90 | beep(); |
@@ -90,21 +93,42 @@ static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *actio | |||
90 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) | 93 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) |
91 | { | 94 | { |
92 | ChatContext *ctx = (ChatContext*) self->x; | 95 | ChatContext *ctx = (ChatContext*) self->x; |
96 | struct tm *timeinfo = get_time(); | ||
93 | if (ctx->friendnum != num) | 97 | if (ctx->friendnum != num) |
94 | return; | 98 | return; |
95 | 99 | ||
100 | wattron(ctx->history, COLOR_PAIR(2)); | ||
101 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
102 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
103 | |||
96 | nick[len-1] = '\0'; | 104 | nick[len-1] = '\0'; |
97 | fix_name(nick); | 105 | fix_name(nick); |
98 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); | 106 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); |
99 | 107 | ||
100 | wattron(ctx->history, COLOR_PAIR(3)); | 108 | wattron(ctx->history, COLOR_PAIR(3)); |
101 | wprintw(ctx->history, " * Your partner changed nick to '%s'\n", nick); | 109 | wprintw(ctx->history, "* Your partner changed nick to '%s'\n", nick); |
102 | wattroff(ctx->history, COLOR_PAIR(3)); | 110 | wattroff(ctx->history, COLOR_PAIR(3)); |
103 | } | 111 | } |
104 | 112 | ||
105 | static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) | 113 | static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) |
106 | { | 114 | { |
115 | ChatContext *ctx = (ChatContext*) self->x; | ||
116 | struct tm *timeinfo = get_time(); | ||
117 | if (ctx->friendnum != num) | ||
118 | return; | ||
119 | |||
120 | wattron(ctx->history, COLOR_PAIR(2)); | ||
121 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
122 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
107 | 123 | ||
124 | status[len-1] = '\0'; | ||
125 | fix_name(status); | ||
126 | snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num); | ||
127 | |||
128 | wattron(ctx->history, COLOR_PAIR(3)); | ||
129 | wprintw(ctx->history, "* Your partner changed status to '%s'\n", status); | ||
130 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
131 | |||
108 | } | 132 | } |
109 | 133 | ||
110 | /* check that the string has one non-space character */ | 134 | /* check that the string has one non-space character */ |
@@ -120,10 +144,7 @@ int string_is_empty(char *string) | |||
120 | static void chat_onKey(ToxWindow *self, Messenger *m, int key) | 144 | static void chat_onKey(ToxWindow *self, Messenger *m, int key) |
121 | { | 145 | { |
122 | ChatContext *ctx = (ChatContext*) self->x; | 146 | ChatContext *ctx = (ChatContext*) self->x; |
123 | time_t now; | 147 | struct tm *timeinfo = get_time(); |
124 | time(&now); | ||
125 | struct tm * timeinfo; | ||
126 | timeinfo = localtime(&now); | ||
127 | 148 | ||
128 | int x, y, y2, x2; | 149 | int x, y, y2, x2; |
129 | getyx(self->window, y, x); | 150 | getyx(self->window, y, x); |
@@ -155,22 +176,26 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key) | |||
155 | wmove(self->window, y2-CURS_Y_OFFSET, 0); | 176 | wmove(self->window, y2-CURS_Y_OFFSET, 0); |
156 | wclrtobot(self->window); | 177 | wclrtobot(self->window); |
157 | if (ctx->line[0] == '/') | 178 | if (ctx->line[0] == '/') |
158 | execute(self, ctx, m, ctx->line, timeinfo); | 179 | execute(self, ctx, m, ctx->line); |
159 | else { | 180 | else { |
181 | /* make sure the string has at least non-space character */ | ||
160 | if (!string_is_empty(ctx->line)) { | 182 | if (!string_is_empty(ctx->line)) { |
161 | /* make sure the string has at least non-space character */ | 183 | uint8_t selfname[MAX_NAME_LENGTH]; |
184 | getself_name(m, selfname); | ||
185 | fix_name(selfname); | ||
186 | |||
162 | wattron(ctx->history, COLOR_PAIR(2)); | 187 | wattron(ctx->history, COLOR_PAIR(2)); |
163 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | 188 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); |
164 | wattroff(ctx->history, COLOR_PAIR(2)); | 189 | wattroff(ctx->history, COLOR_PAIR(2)); |
165 | wattron(ctx->history, COLOR_PAIR(1)); | 190 | wattron(ctx->history, COLOR_PAIR(1)); |
166 | wprintw(ctx->history, "you: ", ctx->line); | 191 | wprintw(ctx->history, "%s: ", selfname); |
167 | wattroff(ctx->history, COLOR_PAIR(1)); | 192 | wattroff(ctx->history, COLOR_PAIR(1)); |
168 | wprintw(ctx->history, "%s\n", ctx->line); | 193 | wprintw(ctx->history, "%s\n", ctx->line); |
169 | } | 194 | if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) { |
170 | if (m_sendmessage(m, ctx->friendnum, (uint8_t*) ctx->line, strlen(ctx->line)+1) == 0) { | 195 | wattron(ctx->history, COLOR_PAIR(3)); |
171 | wattron(ctx->history, COLOR_PAIR(3)); | 196 | wprintw(ctx->history, " * Failed to send message.\n"); |
172 | wprintw(ctx->history, " * Failed to send message.\n"); | 197 | wattroff(ctx->history, COLOR_PAIR(3)); |
173 | wattroff(ctx->history, COLOR_PAIR(3)); | 198 | } |
174 | } | 199 | } |
175 | } | 200 | } |
176 | ctx->line[0] = '\0'; | 201 | ctx->line[0] = '\0'; |
@@ -178,7 +203,7 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key) | |||
178 | } | 203 | } |
179 | } | 204 | } |
180 | 205 | ||
181 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo) | 206 | void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd) |
182 | { | 207 | { |
183 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | 208 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { |
184 | wclear(self->window); | 209 | wclear(self->window); |
@@ -198,6 +223,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct | |||
198 | } | 223 | } |
199 | 224 | ||
200 | else if (!strncmp(cmd, "/me ", strlen("/me "))) { | 225 | else if (!strncmp(cmd, "/me ", strlen("/me "))) { |
226 | struct tm *timeinfo = get_time(); | ||
201 | char *action = strchr(cmd, ' '); | 227 | char *action = strchr(cmd, ' '); |
202 | if (action == NULL) { | 228 | if (action == NULL) { |
203 | wprintw(self->window, "Invalid syntax.\n"); | 229 | wprintw(self->window, "Invalid syntax.\n"); |
@@ -214,9 +240,9 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct | |||
214 | char msg[MAX_STR_SIZE-len-4]; | 240 | char msg[MAX_STR_SIZE-len-4]; |
215 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); | 241 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); |
216 | 242 | ||
217 | wattron(ctx->history, COLOR_PAIR(1)); | 243 | wattron(ctx->history, COLOR_PAIR(5)); |
218 | wprintw(ctx->history, msg); | 244 | wprintw(ctx->history, msg); |
219 | wattroff(ctx->history, COLOR_PAIR(1)); | 245 | wattroff(ctx->history, COLOR_PAIR(5)); |
220 | if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { | 246 | if (m_sendaction(m, ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { |
221 | wattron(ctx->history, COLOR_PAIR(3)); | 247 | wattron(ctx->history, COLOR_PAIR(3)); |
222 | wprintw(ctx->history, " * Failed to send action\n"); | 248 | wprintw(ctx->history, " * Failed to send action\n"); |
@@ -274,6 +300,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct | |||
274 | wprintw(ctx->history, "Invalid syntax.\n"); | 300 | wprintw(ctx->history, "Invalid syntax.\n"); |
275 | return; | 301 | return; |
276 | } | 302 | } |
303 | |||
277 | nick++; | 304 | nick++; |
278 | setname(m, (uint8_t*) nick, strlen(nick)+1); | 305 | setname(m, (uint8_t*) nick, strlen(nick)+1); |
279 | wprintw(ctx->history, "Nickname set to: %s\n", nick); | 306 | wprintw(ctx->history, "Nickname set to: %s\n", nick); |
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 397f9391..8ef44012 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -124,6 +124,7 @@ static void init_term() | |||
124 | init_pair(2, COLOR_CYAN, COLOR_BLACK); | 124 | init_pair(2, COLOR_CYAN, COLOR_BLACK); |
125 | init_pair(3, COLOR_RED, COLOR_BLACK); | 125 | init_pair(3, COLOR_RED, COLOR_BLACK); |
126 | init_pair(4, COLOR_BLUE, COLOR_BLACK); | 126 | init_pair(4, COLOR_BLUE, COLOR_BLACK); |
127 | init_pair(5, COLOR_YELLOW, COLOR_BLACK); | ||
127 | } | 128 | } |
128 | refresh(); | 129 | refresh(); |
129 | } | 130 | } |
@@ -139,6 +140,7 @@ static void init_tox() | |||
139 | m_callback_namechange(m, on_nickchange, NULL); | 140 | m_callback_namechange(m, on_nickchange, NULL); |
140 | m_callback_statusmessage(m, on_statuschange, NULL); | 141 | m_callback_statusmessage(m, on_statuschange, NULL); |
141 | m_callback_action(m, on_action, NULL); | 142 | m_callback_action(m, on_action, NULL); |
143 | setname(m, (uint8_t*) "n00b", strlen("n00b")+1); | ||
142 | } | 144 | } |
143 | 145 | ||
144 | #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ | 146 | #define MAXLINE 90 /* Approx max number of chars in a sever line (IP + port + key) */ |
@@ -179,7 +181,7 @@ int init_connection(void) | |||
179 | dht.port = htons(atoi(port)); | 181 | dht.port = htons(atoi(port)); |
180 | uint32_t resolved_address = resolve_addr(ip); | 182 | uint32_t resolved_address = resolve_addr(ip); |
181 | if (resolved_address == 0) | 183 | if (resolved_address == 0) |
182 | return 4; | 184 | return 0; |
183 | dht.ip.i = resolved_address; | 185 | dht.ip.i = resolved_address; |
184 | unsigned char *binary_string = hex_string_to_bin(key); | 186 | unsigned char *binary_string = hex_string_to_bin(key); |
185 | DHT_bootstrap(dht, binary_string); | 187 | DHT_bootstrap(dht, binary_string); |
@@ -250,10 +252,15 @@ static void init_windows() | |||
250 | static void do_tox() | 252 | static void do_tox() |
251 | { | 253 | { |
252 | static int conn_try = 0; | 254 | static int conn_try = 0; |
255 | static int conn_err = 0; | ||
253 | static bool dht_on = false; | 256 | static bool dht_on = false; |
254 | if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) { | 257 | if (!dht_on && !DHT_isconnected() && !(conn_try++ % 100)) { |
255 | init_connection(); | 258 | if (!conn_err) { |
256 | wprintw(prompt->window, "\nEstablishing connection...\n"); | 259 | conn_err = init_connection(); |
260 | wprintw(prompt->window, "\nEstablishing connection...\n"); | ||
261 | if (conn_err) | ||
262 | wprintw(prompt->window, "\nAuto-connect failed with error code %d\n", conn_err); | ||
263 | } | ||
257 | } | 264 | } |
258 | else if (!dht_on && DHT_isconnected()) { | 265 | else if (!dht_on && DHT_isconnected()) { |
259 | dht_on = true; | 266 | dht_on = true; |
@@ -262,7 +269,6 @@ static void do_tox() | |||
262 | else if (dht_on && !DHT_isconnected()) { | 269 | else if (dht_on && !DHT_isconnected()) { |
263 | dht_on = false; | 270 | dht_on = false; |
264 | wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); | 271 | wprintw(prompt->window, "\nDHT disconnected. Attempting to reconnect.\n"); |
265 | init_connection(); | ||
266 | } | 272 | } |
267 | doMessenger(m); | 273 | doMessenger(m); |
268 | } | 274 | } |