diff options
author | Jfreegman <Jfreegman@gmail.com> | 2013-08-08 15:01:33 -0400 |
---|---|---|
committer | Jfreegman <Jfreegman@gmail.com> | 2013-08-08 15:01:33 -0400 |
commit | 982c86df1f0556a767ef16cfd27cf4552d487225 (patch) | |
tree | 6c2b68934cf92a955f8c4cb8ab26c4cfadee96e8 /testing | |
parent | 8719f466456ef6000e25cf2c26644762b404980c (diff) |
added /me actions to toxic
Diffstat (limited to 'testing')
-rw-r--r-- | testing/toxic/chat.c | 66 | ||||
-rw-r--r-- | testing/toxic/friendlist.c | 1 | ||||
-rw-r--r-- | testing/toxic/main.c | 26 | ||||
-rw-r--r-- | testing/toxic/windows.h | 1 |
4 files changed, 79 insertions, 15 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c index 83765777..73cb1458 100644 --- a/testing/toxic/chat.c +++ b/testing/toxic/chat.c | |||
@@ -27,7 +27,7 @@ extern int active_window; | |||
27 | extern void del_window(ToxWindow *w, int f_num); | 27 | extern void del_window(ToxWindow *w, int f_num); |
28 | extern void fix_name(uint8_t *name); | 28 | extern void fix_name(uint8_t *name); |
29 | void print_help(ChatContext *self); | 29 | void print_help(ChatContext *self); |
30 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd); | 30 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo); |
31 | 31 | ||
32 | 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) |
33 | { | 33 | { |
@@ -35,7 +35,7 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) | |||
35 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 35 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
36 | time_t now; | 36 | time_t now; |
37 | time(&now); | 37 | time(&now); |
38 | struct tm * timeinfo; | 38 | struct tm *timeinfo; |
39 | timeinfo = localtime(&now); | 39 | timeinfo = localtime(&now); |
40 | 40 | ||
41 | if (ctx->friendnum != num) | 41 | if (ctx->friendnum != num) |
@@ -59,6 +59,32 @@ static void chat_onMessage(ToxWindow *self, int num, uint8_t *msg, uint16_t len) | |||
59 | beep(); | 59 | beep(); |
60 | } | 60 | } |
61 | 61 | ||
62 | static void chat_onAction(ToxWindow *self, int num, uint8_t *action, uint16_t len) | ||
63 | { | ||
64 | ChatContext *ctx = (ChatContext*) self->x; | ||
65 | time_t now; | ||
66 | time(&now); | ||
67 | struct tm *timeinfo; | ||
68 | timeinfo = localtime(&now); | ||
69 | |||
70 | if (ctx->friendnum != num) | ||
71 | return; | ||
72 | |||
73 | action[len-1] = '\0'; | ||
74 | fix_name(action); | ||
75 | |||
76 | wattron(ctx->history, COLOR_PAIR(2)); | ||
77 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
78 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
79 | |||
80 | wattron(ctx->history, COLOR_PAIR(4)); | ||
81 | wprintw(ctx->history, "%s\n", action); | ||
82 | wattroff(ctx->history, COLOR_PAIR(4)); | ||
83 | |||
84 | self->blink = true; | ||
85 | beep(); | ||
86 | } | ||
87 | |||
62 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) | 88 | static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) |
63 | { | 89 | { |
64 | ChatContext *ctx = (ChatContext*) self->x; | 90 | ChatContext *ctx = (ChatContext*) self->x; |
@@ -108,7 +134,7 @@ static void chat_onKey(ToxWindow *self, int key) | |||
108 | /* RETURN key: Execute command or print line */ | 134 | /* RETURN key: Execute command or print line */ |
109 | else if (key == '\n') { | 135 | else if (key == '\n') { |
110 | if (ctx->line[0] == '/') | 136 | if (ctx->line[0] == '/') |
111 | execute(self, ctx, ctx->line); | 137 | execute(self, ctx, ctx->line, timeinfo); |
112 | else { | 138 | else { |
113 | if (!string_is_empty(ctx->line)) { | 139 | if (!string_is_empty(ctx->line)) { |
114 | /* make sure the string has at least non-space character */ | 140 | /* make sure the string has at least non-space character */ |
@@ -138,7 +164,7 @@ static void chat_onKey(ToxWindow *self, int key) | |||
138 | } | 164 | } |
139 | } | 165 | } |
140 | 166 | ||
141 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd) | 167 | void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo) |
142 | { | 168 | { |
143 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { | 169 | if (!strcmp(cmd, "/clear") || !strcmp(cmd, "/c")) { |
144 | wclear(self->window); | 170 | wclear(self->window); |
@@ -153,6 +179,33 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) | |||
153 | exit(0); | 179 | exit(0); |
154 | } | 180 | } |
155 | 181 | ||
182 | else if (!strncmp(cmd, "/me ", strlen("/me "))) { | ||
183 | char *action = strchr(cmd, ' '); | ||
184 | if (action == NULL) { | ||
185 | wprintw(self->window, "Invalid syntax.\n"); | ||
186 | return; | ||
187 | } | ||
188 | action++; | ||
189 | |||
190 | wattron(ctx->history, COLOR_PAIR(2)); | ||
191 | wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); | ||
192 | wattroff(ctx->history, COLOR_PAIR(2)); | ||
193 | |||
194 | uint8_t selfname[MAX_NAME_LENGTH]; | ||
195 | int len = getself_name(selfname); | ||
196 | char msg[MAX_STR_SIZE-len-4]; | ||
197 | snprintf(msg, sizeof(msg), "* %s %s\n", (uint8_t*) selfname, action); | ||
198 | |||
199 | wattron(ctx->history, COLOR_PAIR(1)); | ||
200 | wprintw(ctx->history, msg); | ||
201 | wattroff(ctx->history, COLOR_PAIR(1)); | ||
202 | if (m_sendaction(ctx->friendnum, (uint8_t*) msg, strlen(msg)+1) < 0) { | ||
203 | wattron(ctx->history, COLOR_PAIR(3)); | ||
204 | wprintw(ctx->history, " * Failed to send action\n"); | ||
205 | wattroff(ctx->history, COLOR_PAIR(3)); | ||
206 | } | ||
207 | } | ||
208 | |||
156 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { | 209 | else if (!strncmp(cmd, "/status ", strlen("/status "))) { |
157 | char *status = strchr(cmd, ' '); | 210 | char *status = strchr(cmd, ' '); |
158 | char *msg; | 211 | char *msg; |
@@ -178,8 +231,7 @@ void execute(ToxWindow *self, ChatContext *ctx, char *cmd) | |||
178 | status_text = "BUSY"; | 231 | status_text = "BUSY"; |
179 | } | 232 | } |
180 | 233 | ||
181 | else | 234 | else { |
182 | { | ||
183 | wprintw(ctx->history, "Invalid status.\n"); | 235 | wprintw(ctx->history, "Invalid status.\n"); |
184 | return; | 236 | return; |
185 | } | 237 | } |
@@ -265,6 +317,7 @@ void print_help(ChatContext *self) | |||
265 | 317 | ||
266 | wprintw(self->history, " /status <type> <message> : Set your status\n"); | 318 | wprintw(self->history, " /status <type> <message> : Set your status\n"); |
267 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); | 319 | wprintw(self->history, " /nick <nickname> : Set your nickname\n"); |
320 | wprintw(self->history, " /me <action> : Do an action\n"); | ||
268 | wprintw(self->history, " /myid : Print your ID\n"); | 321 | wprintw(self->history, " /myid : Print your ID\n"); |
269 | wprintw(self->history, " /clear : Clear the screen\n"); | 322 | wprintw(self->history, " /clear : Clear the screen\n"); |
270 | wprintw(self->history, " /close : Close the current chat window\n"); | 323 | wprintw(self->history, " /close : Close the current chat window\n"); |
@@ -285,6 +338,7 @@ ToxWindow new_chat(int friendnum) | |||
285 | ret.onMessage = &chat_onMessage; | 338 | ret.onMessage = &chat_onMessage; |
286 | ret.onNickChange = &chat_onNickChange; | 339 | ret.onNickChange = &chat_onNickChange; |
287 | ret.onStatusChange = &chat_onStatusChange; | 340 | ret.onStatusChange = &chat_onStatusChange; |
341 | ret.onAction = &chat_onAction; | ||
288 | 342 | ||
289 | uint8_t nick[MAX_NAME_LENGTH] = {0}; | 343 | uint8_t nick[MAX_NAME_LENGTH] = {0}; |
290 | getname(friendnum, (uint8_t*) &nick); | 344 | getname(friendnum, (uint8_t*) &nick); |
diff --git a/testing/toxic/friendlist.c b/testing/toxic/friendlist.c index 159217b1..f2aa1cf4 100644 --- a/testing/toxic/friendlist.c +++ b/testing/toxic/friendlist.c | |||
@@ -177,6 +177,7 @@ ToxWindow new_friendlist() { | |||
177 | ret.onDraw = &friendlist_onDraw; | 177 | ret.onDraw = &friendlist_onDraw; |
178 | ret.onInit = &friendlist_onInit; | 178 | ret.onInit = &friendlist_onInit; |
179 | ret.onMessage = &friendlist_onMessage; | 179 | ret.onMessage = &friendlist_onMessage; |
180 | ret.onAction = &friendlist_onMessage; // Action has identical behaviour to message | ||
180 | ret.onNickChange = &friendlist_onNickChange; | 181 | ret.onNickChange = &friendlist_onNickChange; |
181 | ret.onStatusChange = &friendlist_onStatusChange; | 182 | ret.onStatusChange = &friendlist_onStatusChange; |
182 | 183 | ||
diff --git a/testing/toxic/main.c b/testing/toxic/main.c index 7fa9e964..d5999eb2 100644 --- a/testing/toxic/main.c +++ b/testing/toxic/main.c | |||
@@ -44,8 +44,9 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) | |||
44 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); | 44 | wprintw(prompt->window, "%02x", public_key[i] & 0xff); |
45 | } | 45 | } |
46 | 46 | ||
47 | wprintw(prompt->window, "\n"); | 47 | wprintw(prompt->window, "\nWith the message: %s\n", data); |
48 | wprintw(prompt->window, "Use \"accept %d\" to accept it.\n", n); | 48 | wprintw(prompt->window, "\nUse \"accept %d\" to accept it.\n", n); |
49 | |||
49 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 50 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
50 | if (windows[i].onFriendRequest != NULL) | 51 | if (windows[i].onFriendRequest != NULL) |
51 | windows[i].onFriendRequest(&windows[i], public_key, data, length); | 52 | windows[i].onFriendRequest(&windows[i], public_key, data, length); |
@@ -54,7 +55,6 @@ void on_request(uint8_t *public_key, uint8_t *data, uint16_t length) | |||
54 | 55 | ||
55 | void on_message(int friendnumber, uint8_t *string, uint16_t length) | 56 | void on_message(int friendnumber, uint8_t *string, uint16_t length) |
56 | { | 57 | { |
57 | wprintw(prompt->window, "\n(message) %d: %s\n", friendnumber, string); | ||
58 | int i; | 58 | int i; |
59 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | 59 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { |
60 | if (windows[i].onMessage != NULL) | 60 | if (windows[i].onMessage != NULL) |
@@ -62,6 +62,15 @@ void on_message(int friendnumber, uint8_t *string, uint16_t length) | |||
62 | } | 62 | } |
63 | } | 63 | } |
64 | 64 | ||
65 | void on_action(int friendnumber, uint8_t *string, uint16_t length) | ||
66 | { | ||
67 | int i; | ||
68 | for (i = 0; i < MAX_WINDOW_SLOTS; ++i) { | ||
69 | if (windows[i].onAction != NULL) | ||
70 | windows[i].onAction(&windows[i], friendnumber, string, length); | ||
71 | } | ||
72 | } | ||
73 | |||
65 | void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) | 74 | void on_nickchange(int friendnumber, uint8_t *string, uint16_t length) |
66 | { | 75 | { |
67 | wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); | 76 | wprintw(prompt->window, "\n(nickchange) %d: %s!\n", friendnumber, string); |
@@ -117,6 +126,7 @@ static void init_tox() | |||
117 | m_callback_friendmessage(on_message); | 126 | m_callback_friendmessage(on_message); |
118 | m_callback_namechange(on_nickchange); | 127 | m_callback_namechange(on_nickchange); |
119 | m_callback_statusmessage(on_statuschange); | 128 | m_callback_statusmessage(on_statuschange); |
129 | m_callback_action(on_action); | ||
120 | } | 130 | } |
121 | 131 | ||
122 | void init_window_status() | 132 | void init_window_status() |
@@ -272,13 +282,13 @@ static void draw_bar() | |||
272 | attron(A_BOLD); | 282 | attron(A_BOLD); |
273 | 283 | ||
274 | odd = (odd+1) % blinkrate; | 284 | odd = (odd+1) % blinkrate; |
275 | if (windows[i].blink && (odd < (blinkrate/2))) { | 285 | if (windows[i].blink && (odd < (blinkrate/2))) |
276 | attron(COLOR_PAIR(3)); | 286 | attron(COLOR_PAIR(3)); |
277 | } | 287 | |
278 | printw(" %s", windows[i].title); | 288 | printw(" %s", windows[i].title); |
279 | if (windows[i].blink && (odd < (blinkrate/2))) { | 289 | if (windows[i].blink && (odd < (blinkrate/2))) |
280 | attroff(COLOR_PAIR(3)); | 290 | attroff(COLOR_PAIR(3)); |
281 | } | 291 | |
282 | if (i == active_window) { | 292 | if (i == active_window) { |
283 | attroff(A_BOLD); | 293 | attroff(A_BOLD); |
284 | } | 294 | } |
@@ -308,7 +318,6 @@ void set_active_window(int ch) | |||
308 | i = (i + 1) % max; | 318 | i = (i + 1) % max; |
309 | if (f_inf++ > max) { // infinite loop check | 319 | if (f_inf++ > max) { // infinite loop check |
310 | endwin(); | 320 | endwin(); |
311 | clear(); | ||
312 | exit(2); | 321 | exit(2); |
313 | } | 322 | } |
314 | } | 323 | } |
@@ -323,7 +332,6 @@ void set_active_window(int ch) | |||
323 | if (--i < 0) i = max; | 332 | if (--i < 0) i = max; |
324 | if (f_inf++ > max) { | 333 | if (f_inf++ > max) { |
325 | endwin(); | 334 | endwin(); |
326 | clear(); | ||
327 | exit(2); | 335 | exit(2); |
328 | } | 336 | } |
329 | } | 337 | } |
diff --git a/testing/toxic/windows.h b/testing/toxic/windows.h index 287e534a..c6925ce1 100644 --- a/testing/toxic/windows.h +++ b/testing/toxic/windows.h | |||
@@ -24,6 +24,7 @@ struct ToxWindow_ { | |||
24 | void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); | 24 | void(*onMessage)(ToxWindow*, int, uint8_t*, uint16_t); |
25 | void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); | 25 | void(*onNickChange)(ToxWindow*, int, uint8_t*, uint16_t); |
26 | void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); | 26 | void(*onStatusChange)(ToxWindow*, int, uint8_t*, uint16_t); |
27 | void(*onAction)(ToxWindow*, int, uint8_t*, uint16_t); | ||
27 | char title[256]; | 28 | char title[256]; |
28 | 29 | ||
29 | void* x; | 30 | void* x; |