Mercurial > hg > nnchat
comparison nnchat.c @ 191:4007b9ac7cde
Handle usernames with whitespace by converting whitespaces to chr 255 in
userhashes and internal buffers, and then back to 32 when outputting to
screen/UI or sending to network.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 16 Nov 2010 20:07:05 +0200 |
parents | df284de8b118 |
children | 829457be9eea |
comparison
equal
deleted
inserted
replaced
190:df284de8b118 | 191:4007b9ac7cde |
---|---|
166 str[0] = 0; | 166 str[0] = 0; |
167 return FALSE; | 167 return FALSE; |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 char *encodeUsername(char *str) | |
172 { | |
173 unsigned char *c = (unsigned char *) str; | |
174 if (str == NULL) return NULL; | |
175 for (; *c ; c++) | |
176 if (*c == ' ') *c = 255; | |
177 return str; | |
178 } | |
179 | |
180 char *decodeUsername(char *str) | |
181 { | |
182 unsigned char *c = (unsigned char *) str; | |
183 if (str == NULL) return NULL; | |
184 for (; *c ; c++) | |
185 if (*c == 255) *c = ' '; | |
186 return str; | |
187 } | |
188 | |
171 void updateStatus(BOOL insertMode) | 189 void updateStatus(BOOL insertMode) |
172 { | 190 { |
173 char tmpStr[128]; | 191 char tmpStr[128]; |
174 | 192 |
175 if (statusWin == NULL) return; | 193 if (statusWin == NULL) return; |
207 wrefresh(statusWin); | 225 wrefresh(statusWin); |
208 } | 226 } |
209 | 227 |
210 void printEditBuf(const char *str, nn_editbuf_t *buf) | 228 void printEditBuf(const char *str, nn_editbuf_t *buf) |
211 { | 229 { |
230 char *tmp; | |
212 if (statusWin == NULL || buf == NULL) return; | 231 if (statusWin == NULL || buf == NULL) return; |
213 | 232 |
214 buf->data[buf->len] = 0; | 233 buf->data[buf->len] = 0; |
234 tmp = decodeUsername(th_strdup(buf->data)); | |
235 | |
215 werase(editWin); | 236 werase(editWin); |
216 | 237 |
217 wattrset(editWin, A_BOLD); | 238 wattrset(editWin, A_BOLD); |
218 mvwaddstr(editWin, 0, 0, str); | 239 mvwaddstr(editWin, 0, 0, str); |
219 waddstr(editWin, "> "); | 240 waddstr(editWin, "> "); |
220 wattrset(editWin, A_NORMAL); | 241 wattrset(editWin, A_NORMAL); |
221 | 242 |
222 if (buf->pos < buf->len) { | 243 if (buf->pos < buf->len) { |
223 waddnstr(editWin, buf->data, buf->pos); | 244 waddnstr(editWin, tmp, buf->pos); |
224 wattrset(editWin, A_REVERSE); | 245 wattrset(editWin, A_REVERSE); |
225 waddch(editWin, buf->data[buf->pos]); | 246 waddch(editWin, tmp[buf->pos]); |
226 wattrset(editWin, A_NORMAL); | 247 wattrset(editWin, A_NORMAL); |
227 waddnstr(editWin, buf->data + buf->pos + 1, buf->len - buf->pos - 1); | 248 waddnstr(editWin, tmp + buf->pos + 1, buf->len - buf->pos - 1); |
228 } else { | 249 } else { |
229 waddnstr(editWin, buf->data, buf->len); | 250 waddnstr(editWin, tmp, buf->len); |
230 wattrset(editWin, A_REVERSE); | 251 wattrset(editWin, A_REVERSE); |
231 waddch(editWin, ' '); | 252 waddch(editWin, ' '); |
232 wattrset(editWin, A_NORMAL); | 253 wattrset(editWin, A_NORMAL); |
233 } | 254 } |
234 wrefresh(editWin); | 255 wrefresh(editWin); |
256 th_free(tmp); | |
235 } | 257 } |
236 | 258 |
237 int printWin(WINDOW *win, const char *fmt) | 259 int printWin(WINDOW *win, const char *fmt) |
238 { | 260 { |
239 const char *s = fmt; | 261 const char *s = fmt; |
252 val += (*s - '0'); | 274 val += (*s - '0'); |
253 s++; | 275 s++; |
254 } | 276 } |
255 if (*s != '˝') return -1; | 277 if (*s != '˝') return -1; |
256 s++; | 278 s++; |
257 | 279 |
258 if (val < 9) { | 280 if (val < 9) { |
259 col = A_DIM | COLOR_PAIR(val); | 281 col = A_DIM | COLOR_PAIR(val); |
260 } else if (val < 30) { | 282 } else if (val < 30) { |
261 col = A_BOLD | COLOR_PAIR(val - 9); | 283 col = A_BOLD | COLOR_PAIR(val - 9); |
262 } | 284 } |
263 } | 285 } |
264 } else { | 286 } else { |
287 if ((unsigned char) *s == 255) | |
288 waddch(win, ((unsigned char) ' ') | col); | |
289 else | |
265 if (*s != '\r') | 290 if (*s != '\r') |
266 waddch(win, ((unsigned char) *s) | col); | 291 waddch(win, ((unsigned char) *s) | col); |
267 s++; | 292 s++; |
268 } | 293 } |
269 } | 294 } |
285 while (*s && isdigit((int) *s)) s++; | 310 while (*s && isdigit((int) *s)) s++; |
286 if (*s != '˝') return -1; | 311 if (*s != '˝') return -1; |
287 s++; | 312 s++; |
288 } | 313 } |
289 } else { | 314 } else { |
290 fputc((unsigned char) *s, outFile); | 315 if ((unsigned char) *s == 255) |
316 fputc(' ', outFile); | |
317 else | |
318 fputc((unsigned char) *s, outFile); | |
291 s++; | 319 s++; |
292 } | 320 } |
293 } | 321 } |
294 | 322 |
295 return 0; | 323 return 0; |
355 va_end(ap1); | 383 va_end(ap1); |
356 | 384 |
357 THERR_V(fmt, ap2); | 385 THERR_V(fmt, ap2); |
358 va_end(ap2); | 386 va_end(ap2); |
359 } | 387 } |
360 | |
361 | 388 |
362 int handleUser(nn_conn_t *conn, const char *str) | 389 int handleUser(nn_conn_t *conn, const char *str) |
363 { | 390 { |
364 const char *msg = "</USER><MESSAGE>", *p = str; | 391 const char *msg = "</USER><MESSAGE>", *p = str; |
365 char *q, *s, *t, *h, *p2; | 392 char *q, *s, *t, *h, *p2; |
464 *s = 0; | 491 *s = 0; |
465 | 492 |
466 p = nn_dbldecode_str(str); | 493 p = nn_dbldecode_str(str); |
467 if (!p) return -1; | 494 if (!p) return -1; |
468 | 495 |
469 nn_userhash_insert(nnUsers, p); | 496 nn_userhash_insert(nnUsers, encodeUsername(p)); |
470 | 497 |
471 printMsg("! ˝3˝%s˝0˝ ˝2˝ADDED.˝0˝\n", p); | 498 printMsg("! ˝3˝%s˝0˝ ˝2˝ADDED.˝0˝\n", p); |
472 th_free(p); | 499 th_free(p); |
473 return 0; | 500 return 0; |
474 } | 501 } |
484 *s = 0; | 511 *s = 0; |
485 | 512 |
486 p = nn_dbldecode_str(str); | 513 p = nn_dbldecode_str(str); |
487 if (!p) return -1; | 514 if (!p) return -1; |
488 | 515 |
489 nn_userhash_delete(nnUsers, p); | 516 nn_userhash_delete(nnUsers, encodeUsername(p)); |
490 | 517 |
491 printMsg("! ˝3˝%s˝0˝ ˝1˝DELETED.˝0˝\n", p); | 518 printMsg("! ˝3˝%s˝0˝ ˝1˝DELETED.˝0˝\n", p); |
492 th_free(p); | 519 th_free(p); |
493 return 0; | 520 return 0; |
494 } | 521 } |
572 /* Trim right */ | 599 /* Trim right */ |
573 bufLen--; | 600 bufLen--; |
574 buf[bufLen--] = 0; | 601 buf[bufLen--] = 0; |
575 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen]))) | 602 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen]))) |
576 buf[bufLen--] = 0; | 603 buf[bufLen--] = 0; |
604 decodeUsername(buf); | |
577 | 605 |
578 /* Check for special user commands */ | 606 /* Check for special user commands */ |
579 if (*buf == 0) { | 607 if (*buf == 0) { |
580 return 1; | 608 return 1; |
581 } else if (!strncasecmp(buf, "/color ", 7)) { | 609 } else if (!strncasecmp(buf, "/color ", 7)) { |
685 return 1; | 713 return 1; |
686 } | 714 } |
687 } | 715 } |
688 | 716 |
689 /* Send double-encoded */ | 717 /* Send double-encoded */ |
690 tmpStr = nn_dblencode_str(buf); | 718 tmpStr = nn_dblencode_str(decodeUsername(buf)); |
691 if (tmpStr == 0) return -2; | 719 if (tmpStr == 0) return -2; |
692 result = nn_conn_send_msg(conn, optUserName2, "%s", tmpStr); | 720 result = nn_conn_send_msg(conn, optUserName2, "%s", tmpStr); |
693 th_free(tmpStr); | 721 th_free(tmpStr); |
694 | 722 |
695 return result ? 0 : -1; | 723 return result ? 0 : -1; |