Mercurial > hg > nnchat
comparison nnchat.c @ 325:c086345d176b
Move some functions to libnnchat and rename nn_find_window to findWindow()
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 11 Jun 2011 10:03:44 +0300 |
parents | ba4cc7ebe311 |
children | 2f7849e505f3 |
comparison
equal
deleted
inserted
replaced
324:ba4cc7ebe311 | 325:c086345d176b |
---|---|
30 #define SET_CONFIG_FILE ".nnchat" | 30 #define SET_CONFIG_FILE ".nnchat" |
31 #define SET_DIR_SEPARATOR "/" | 31 #define SET_DIR_SEPARATOR "/" |
32 #define SET_DELAY (5) | 32 #define SET_DELAY (5) |
33 #endif | 33 #endif |
34 | 34 |
35 #define SET_BACKBUF_LEN (512) /* Backbuffer size (in lines) */ | |
36 #define SET_MAX_HISTORY (16) /* Command history length */ | 35 #define SET_MAX_HISTORY (16) /* Command history length */ |
37 #define SET_KEEPALIVE (15*60) /* Ping/keepalive period in seconds */ | 36 #define SET_KEEPALIVE (15*60) /* Ping/keepalive period in seconds */ |
38 #define SET_MAX_WINDOWS (32) | 37 #define SET_MAX_WINDOWS (32) |
39 | |
40 | |
41 typedef struct { | |
42 qringbuf_t *data; /* "Backbuffer" data for this window */ | |
43 int pos; /* Current position in the window, 0 = real time */ | |
44 BOOL dirty; | |
45 | |
46 char *id; /* Chatter ID, NULL = main window */ | |
47 int num; /* Window number */ | |
48 | |
49 char *buf; | |
50 size_t len, bufsize; | |
51 } nn_window_t; | |
52 | 38 |
53 | 39 |
54 /* Options | 40 /* Options |
55 */ | 41 */ |
56 int optPort = 8005; | 42 int optPort = 8005; |
201 return FALSE; | 187 return FALSE; |
202 } | 188 } |
203 } | 189 } |
204 | 190 |
205 | 191 |
206 nn_window_t *nn_window_new(const char *id) | 192 nn_window_t *findWindow(const char *id) |
207 { | |
208 nn_window_t *res = th_calloc(1, sizeof(nn_window_t)); | |
209 | |
210 if (res == NULL) return NULL; | |
211 | |
212 res->data = th_ringbuf_new(SET_BACKBUF_LEN, th_free); | |
213 if (res->data == NULL) { | |
214 th_free(res); | |
215 return NULL; | |
216 } | |
217 | |
218 res->id = th_strdup(id); | |
219 | |
220 return res; | |
221 } | |
222 | |
223 | |
224 void nn_window_free(nn_window_t *win) | |
225 { | |
226 if (win != NULL) { | |
227 th_ringbuf_free(win->data); | |
228 th_free(win->id); | |
229 th_free(win); | |
230 } | |
231 } | |
232 | |
233 | |
234 nn_window_t *nn_find_window(const char *id) | |
235 { | 193 { |
236 int i; | 194 int i; |
237 | 195 |
238 for (i = 0; i < SET_MAX_WINDOWS; i++) | 196 for (i = 0; i < SET_MAX_WINDOWS; i++) |
239 if (chatWindows[i] != NULL && | 197 if (chatWindows[i] != NULL && |
644 else | 602 else |
645 msg = ""; | 603 msg = ""; |
646 *tmp = 0; | 604 *tmp = 0; |
647 | 605 |
648 isIgnored = setIgnoreMode && checkIgnoreList(name); | 606 isIgnored = setIgnoreMode && checkIgnoreList(name); |
649 win = nn_find_window(name); | 607 win = findWindow(name); |
650 | 608 |
651 if (win != NULL) { | 609 if (win != NULL) { |
652 printMsgF(win, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE), | 610 printMsgF(win, isIgnored ? LOG_FILE : (LOG_WINDOW | LOG_FILE), |
653 "½5½<½%d½%s½5½>½0½ %s\n", | 611 "½5½<½%d½%s½5½>½0½ %s\n", |
654 isMine ? 14 : 15, isMine ? optUserName : name, msg); | 612 isMine ? 14 : 15, isMine ? optUserName : name, msg); |
713 *s = 0; | 671 *s = 0; |
714 | 672 |
715 p = nn_dbldecode_str(str); | 673 p = nn_dbldecode_str(str); |
716 if (!p) return -1; | 674 if (!p) return -1; |
717 | 675 |
718 win = nn_find_window(p); | 676 win = findWindow(p); |
719 nn_userhash_insert(nnUsers, nn_username_encode(p)); | 677 nn_userhash_insert(nnUsers, nn_username_encode(p)); |
720 | 678 |
721 printMsg(NULL, "! ½3½%s½0½ ½2½ADDED.½0½\n", p); | 679 printMsg(NULL, "! ½3½%s½0½ ½2½ADDED.½0½\n", p); |
722 if (win != NULL) | 680 if (win != NULL) |
723 printMsg(win, "! ½3½%s½0½ ½2½joined the chat.½0½\n", p); | 681 printMsg(win, "! ½3½%s½0½ ½2½joined the chat.½0½\n", p); |
738 *s = 0; | 696 *s = 0; |
739 | 697 |
740 p = nn_dbldecode_str(str); | 698 p = nn_dbldecode_str(str); |
741 if (!p) return -1; | 699 if (!p) return -1; |
742 | 700 |
743 win = nn_find_window(p); | 701 win = findWindow(p); |
744 nn_userhash_delete(nnUsers, nn_username_encode(p)); | 702 nn_userhash_delete(nnUsers, nn_username_encode(p)); |
745 | 703 |
746 printMsg(NULL, "! ½3½%s½0½ ½1½DELETED.½0½\n", p); | 704 printMsg(NULL, "! ½3½%s½0½ ½1½DELETED.½0½\n", p); |
747 if (win != NULL) | 705 if (win != NULL) |
748 printMsg(win, "! ½3½%s½0½ ½1½left the chat.½0½\n", p); | 706 printMsg(win, "! ½3½%s½0½ ½1½left the chat.½0½\n", p); |
919 return 0; | 877 return 0; |
920 } | 878 } |
921 else if (!strncasecmp(buf, "/close", 6)) { | 879 else if (!strncasecmp(buf, "/close", 6)) { |
922 char *name = trimLeft(buf + 6); | 880 char *name = trimLeft(buf + 6); |
923 if (strlen(name) > 0) { | 881 if (strlen(name) > 0) { |
924 nn_window_t *win; | 882 nn_window_t *win = findWindow(name); |
925 win = nn_find_window(name); | |
926 if (win != NULL) { | 883 if (win != NULL) { |
927 closeWindow(win); | 884 closeWindow(win); |
928 printMsgQ(currWin, "Closed PRV query to '%s'.\n", name); | 885 printMsgQ(currWin, "Closed PRV query to '%s'.\n", name); |
929 } else { | 886 } else { |
930 printMsgQ(currWin, "No PRV query by name '%s'.\n", name); | 887 printMsgQ(currWin, "No PRV query by name '%s'.\n", name); |