summaryrefslogtreecommitdiff
path: root/testing/toxic/chat.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-08 12:40:15 -0700
committerirungentoo <irungentoo@gmail.com>2013-08-08 12:40:15 -0700
commit5024bab9309b75994663b506a6b7aff2130d611d (patch)
tree8f01fd35da9cbbd583cad65e709f4ab7a80e7334 /testing/toxic/chat.c
parent4d09e907e00657b693fb8db03594b967e6c1405c (diff)
parent982c86df1f0556a767ef16cfd27cf4552d487225 (diff)
Merge pull request #400 from JFreegman/master
Added actions/alternative type of messages
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r--testing/toxic/chat.c66
1 files changed, 60 insertions, 6 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;
27extern void del_window(ToxWindow *w, int f_num); 27extern void del_window(ToxWindow *w, int f_num);
28extern void fix_name(uint8_t *name); 28extern void fix_name(uint8_t *name);
29void print_help(ChatContext *self); 29void print_help(ChatContext *self);
30void execute(ToxWindow *self, ChatContext *ctx, char *cmd); 30void execute(ToxWindow *self, ChatContext *ctx, char *cmd, struct tm *timeinfo);
31 31
32static 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)
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
62static 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
62static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) 88static 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
141void execute(ToxWindow *self, ChatContext *ctx, char *cmd) 167void 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);