summaryrefslogtreecommitdiff
path: root/testing/toxic/chat.c
diff options
context:
space:
mode:
Diffstat (limited to 'testing/toxic/chat.c')
-rw-r--r--testing/toxic/chat.c81
1 files changed, 54 insertions, 27 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;
29extern void del_window(ToxWindow *w, int f_num); 29extern void del_window(ToxWindow *w, int f_num);
30extern void fix_name(uint8_t *name); 30extern void fix_name(uint8_t *name);
31void print_help(ChatContext *self); 31void print_help(ChatContext *self);
32void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo); 32void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd);
33 33
34static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg, uint16_t len) 34struct 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
43static 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,
64static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *action, uint16_t len) 70static 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
90static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t len) 93static 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
105static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint16_t len) 113static 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)
120static void chat_onKey(ToxWindow *self, Messenger *m, int key) 144static 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
181void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd, struct tm *timeinfo) 206void 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);