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.c74
1 files changed, 53 insertions, 21 deletions
diff --git a/testing/toxic/chat.c b/testing/toxic/chat.c
index c7979843..c7a0f98e 100644
--- a/testing/toxic/chat.c
+++ b/testing/toxic/chat.c
@@ -2,12 +2,12 @@
2 * Toxic -- Tox Curses Client 2 * Toxic -- Tox Curses Client
3 */ 3 */
4 4
5#include <curses.h>
6#include <stdlib.h> 5#include <stdlib.h>
7#include <string.h> 6#include <string.h>
8#include <stdint.h> 7#include <stdint.h>
9#include <ctype.h> 8#include <ctype.h>
10#include <time.h> 9#include <time.h>
10#include <limits.h>
11 11
12#include "../../core/Messenger.h" 12#include "../../core/Messenger.h"
13#include "../../core/network.h" 13#include "../../core/network.h"
@@ -20,7 +20,7 @@
20 20
21typedef struct { 21typedef struct {
22 int friendnum; 22 int friendnum;
23 char line[MAX_STR_SIZE]; 23 wchar_t line[MAX_STR_SIZE];
24 size_t pos; 24 size_t pos;
25 WINDOW *history; 25 WINDOW *history;
26 WINDOW *linewin; 26 WINDOW *linewin;
@@ -50,8 +50,6 @@ static void chat_onMessage(ToxWindow *self, Messenger *m, int num, uint8_t *msg,
50 getname(m, num, (uint8_t *) &nick); 50 getname(m, num, (uint8_t *) &nick);
51 msg[len - 1] = '\0'; 51 msg[len - 1] = '\0';
52 nick[MAX_NAME_LENGTH - 1] = '\0'; 52 nick[MAX_NAME_LENGTH - 1] = '\0';
53 fix_name(msg);
54 fix_name(nick);
55 53
56 wattron(ctx->history, COLOR_PAIR(2)); 54 wattron(ctx->history, COLOR_PAIR(2));
57 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); 55 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
@@ -74,7 +72,6 @@ static void chat_onAction(ToxWindow *self, Messenger *m, int num, uint8_t *actio
74 return; 72 return;
75 73
76 action[len - 1] = '\0'; 74 action[len - 1] = '\0';
77 fix_name(action);
78 75
79 wattron(ctx->history, COLOR_PAIR(2)); 76 wattron(ctx->history, COLOR_PAIR(2));
80 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); 77 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
@@ -101,7 +98,6 @@ static void chat_onNickChange(ToxWindow *self, int num, uint8_t *nick, uint16_t
101 wattroff(ctx->history, COLOR_PAIR(2)); 98 wattroff(ctx->history, COLOR_PAIR(2));
102 99
103 nick[len - 1] = '\0'; 100 nick[len - 1] = '\0';
104 fix_name(nick);
105 snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num); 101 snprintf(self->title, sizeof(self->title), "[%s (%d)]", nick, num);
106 102
107 wattron(ctx->history, COLOR_PAIR(3)); 103 wattron(ctx->history, COLOR_PAIR(3));
@@ -122,7 +118,7 @@ static void chat_onStatusChange(ToxWindow *self, int num, uint8_t *status, uint1
122 wattroff(ctx->history, COLOR_PAIR(2)); 118 wattroff(ctx->history, COLOR_PAIR(2));
123 119
124 status[len - 1] = '\0'; 120 status[len - 1] = '\0';
125 fix_name(status); 121 snprintf(self->title, sizeof(self->title), "[%s (%d)]", status, num);
126 122
127 wattron(ctx->history, COLOR_PAIR(3)); 123 wattron(ctx->history, COLOR_PAIR(3));
128 wprintw(ctx->history, "* Your partner changed status to '%s'\n", status); 124 wprintw(ctx->history, "* Your partner changed status to '%s'\n", status);
@@ -140,7 +136,43 @@ int string_is_empty(char *string)
140 return rc; 136 return rc;
141} 137}
142 138
143static void chat_onKey(ToxWindow *self, Messenger *m, int key) 139/* convert wide characters to null terminated string */
140static char *wcs_to_char(wchar_t *string)
141{
142 size_t len = 0;
143 char *ret = NULL;
144
145 len = wcstombs(NULL, string, 0);
146 if (len != (size_t) -1) {
147 len++;
148 ret = malloc(len);
149 wcstombs(ret, string, len);
150 } else {
151 ret = malloc(2);
152 ret[0] = ' ';
153 ret[1] = '\0';
154 }
155 return ret;
156}
157
158/* convert a wide char to null terminated string */
159static char *wc_to_char(wchar_t ch)
160{
161 int len = 0;
162 static char ret[MB_LEN_MAX + 1];
163
164 len = wctomb(ret, ch);
165 if (len == -1) {
166 ret[0] = ' ';
167 ret[1] = '\0';
168 } else {
169 ret[len] = '\0';
170 }
171
172 return ret;
173}
174
175static void chat_onKey(ToxWindow *self, Messenger *m, wint_t key)
144{ 176{
145 ChatContext *ctx = (ChatContext *) self->x; 177 ChatContext *ctx = (ChatContext *) self->x;
146 struct tm *timeinfo = get_time(); 178 struct tm *timeinfo = get_time();
@@ -150,18 +182,18 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
150 getmaxyx(self->window, y2, x2); 182 getmaxyx(self->window, y2, x2);
151 183
152 /* Add printable chars to buffer and print on input space */ 184 /* Add printable chars to buffer and print on input space */
153 if (isprint(key)) { 185 if (iswprint(key)) {
154 if (ctx->pos != sizeof(ctx->line) - 1) { 186 if (ctx->pos != sizeof(ctx->line) - 1) {
155 mvwaddch(self->window, y, x, key); 187 mvwaddstr(self->window, y, x, wc_to_char(key));
156 ctx->line[ctx->pos++] = key; 188 ctx->line[ctx->pos++] = key;
157 ctx->line[ctx->pos] = '\0'; 189 ctx->line[ctx->pos] = L'\0';
158 } 190 }
159 } 191 }
160 192
161 /* BACKSPACE key: Remove one character from line */ 193 /* BACKSPACE key: Remove one character from line */
162 else if (key == 0x107 || key == 0x8 || key == 0x7f) { 194 else if (key == 0x107 || key == 0x8 || key == 0x7f) {
163 if (ctx->pos > 0) { 195 if (ctx->pos > 0) {
164 ctx->line[--ctx->pos] = '\0'; 196 ctx->line[--ctx->pos] = L'\0';
165 197
166 if (x == 0) 198 if (x == 0)
167 mvwdelch(self->window, y - 1, x2 - 1); 199 mvwdelch(self->window, y - 1, x2 - 1);
@@ -172,18 +204,18 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
172 204
173 /* RETURN key: Execute command or print line */ 205 /* RETURN key: Execute command or print line */
174 else if (key == '\n') { 206 else if (key == '\n') {
207 char *line = wcs_to_char(ctx->line);
175 wclear(ctx->linewin); 208 wclear(ctx->linewin);
176 wmove(self->window, y2 - CURS_Y_OFFSET, 0); 209 wmove(self->window, y2 - CURS_Y_OFFSET, 0);
177 wclrtobot(self->window); 210 wclrtobot(self->window);
178 211
179 if (ctx->line[0] == '/') 212 if (line[0] == '/')
180 execute(self, ctx, m, ctx->line); 213 execute(self, ctx, m, line);
181 else { 214 else {
182 /* make sure the string has at least non-space character */ 215 /* make sure the string has at least non-space character */
183 if (!string_is_empty(ctx->line)) { 216 if (!string_is_empty(line)) {
184 uint8_t selfname[MAX_NAME_LENGTH]; 217 uint8_t selfname[MAX_NAME_LENGTH];
185 getself_name(m, selfname, sizeof(selfname)); 218 getself_name(m, selfname, sizeof(selfname));
186 fix_name(selfname);
187 219
188 wattron(ctx->history, COLOR_PAIR(2)); 220 wattron(ctx->history, COLOR_PAIR(2));
189 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec); 221 wprintw(ctx->history, "[%02d:%02d:%02d] ", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
@@ -191,9 +223,9 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
191 wattron(ctx->history, COLOR_PAIR(1)); 223 wattron(ctx->history, COLOR_PAIR(1));
192 wprintw(ctx->history, "%s: ", selfname); 224 wprintw(ctx->history, "%s: ", selfname);
193 wattroff(ctx->history, COLOR_PAIR(1)); 225 wattroff(ctx->history, COLOR_PAIR(1));
194 wprintw(ctx->history, "%s\n", ctx->line); 226 wprintw(ctx->history, "%s\n", line);
195 227
196 if (m_sendmessage(m, ctx->friendnum, (uint8_t *) ctx->line, strlen(ctx->line) + 1) == 0) { 228 if (m_sendmessage(m, ctx->friendnum, (uint8_t *) line, strlen(line) + 1) == 0) {
197 wattron(ctx->history, COLOR_PAIR(3)); 229 wattron(ctx->history, COLOR_PAIR(3));
198 wprintw(ctx->history, " * Failed to send message.\n"); 230 wprintw(ctx->history, " * Failed to send message.\n");
199 wattroff(ctx->history, COLOR_PAIR(3)); 231 wattroff(ctx->history, COLOR_PAIR(3));
@@ -201,8 +233,9 @@ static void chat_onKey(ToxWindow *self, Messenger *m, int key)
201 } 233 }
202 } 234 }
203 235
204 ctx->line[0] = '\0'; 236 ctx->line[0] = L'\0';
205 ctx->pos = 0; 237 ctx->pos = 0;
238 free(line);
206 } 239 }
207} 240}
208 241
@@ -331,7 +364,7 @@ void execute(ToxWindow *self, ChatContext *ctx, Messenger *m, char *cmd)
331 wprintw(ctx->history, "%s\n", id); 364 wprintw(ctx->history, "%s\n", id);
332 } 365 }
333 366
334 else if (strcmp(ctx->line, "/close") == 0) { 367 else if (strcmp(cmd, "/close") == 0) {
335 int f_num = ctx->friendnum; 368 int f_num = ctx->friendnum;
336 delwin(ctx->linewin); 369 delwin(ctx->linewin);
337 del_window(self); 370 del_window(self);
@@ -398,7 +431,6 @@ ToxWindow new_chat(Messenger *m, int friendnum)
398 431
399 uint8_t nick[MAX_NAME_LENGTH] = {0}; 432 uint8_t nick[MAX_NAME_LENGTH] = {0};
400 getname(m, friendnum, (uint8_t *) &nick); 433 getname(m, friendnum, (uint8_t *) &nick);
401 fix_name(nick);
402 434
403 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum); 435 snprintf(ret.title, sizeof(ret.title), "[%s (%d)]", nick, friendnum);
404 436