annotate nnchat.c @ 96:7c9538e71c89

Add connection keepalive by sending /listallusers every 15 minutes, due to NN server booting after about 30 minutes of inactivity; Improve handling of error messages; Add handling for "<BOOT />" protocol token.
author Matti Hamalainen <ccr@tnsp.org>
date Sun, 05 Jul 2009 16:55:40 +0300
parents 638f88374e3e
children 218efd2f0641
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
41
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
1 /*
Matti Hamalainen <ccr@tnsp.org>
parents: 40
diff changeset
2 * NNChat - Custom chat client for NewbieNudes.com chatrooms
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
3 * Written by Matti 'ccr' Hämäläinen
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
4 * (C) Copyright 2008-2009 Tecnic Software productions (TNSP)
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
5 */
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
6 #include "libnnchat.h"
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include <stdlib.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include "th_args.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #include <string.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 #include <errno.h>
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
11 #ifdef __WIN32
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
12 #define usleep(t) Sleep((t) / 1000)
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
13 /* Undefine because both windows.h and curses.h #define it */
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
14 #undef MOUSE_MOVED
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
15 #endif
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
16 #include <curses.h>
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
18
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
19 #define SET_MAX_BACKBUF (1024)
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
20 #define SET_MAX_HISTORY (16)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
21 #define SET_DELAY (15)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
22 #define SET_DELAY_USEC (SET_DELAY * 1000)
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
23 #define SET_KEEPALIVE (15*60) /* Ping/keepalive period in seconds */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
25
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 /* Options
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 */
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
28 int optPort = 8005;
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
29 int optUserColor = 0x006080;
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
30 char *optServer = "chat.newbienudes.com",
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
31 *optUserName = NULL,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
32 *optUserName2 = NULL,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
33 *optPassword = NULL,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
34 *optLogFilename = NULL,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
35 *setTarget = NULL,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
36 *optSite = "NN";
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
37 BOOL optDaemon = FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
38 FILE *optLogFile = NULL;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
39 WINDOW *mainWin = NULL,
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
40 *statusWin = NULL,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
41 *editWin = NULL;
59
d57a8acf92bf Remove morse code mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 58
diff changeset
42 BOOL setPrvMode = FALSE;
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
43
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
44
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 /* Arguments
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 optarg_t optList[] = {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
48 { 0, '?', "help", "Show this help", OPT_NONE },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
49 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
50 { 2, 'p', "port", "Connect to port", OPT_ARGREQ },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
51 { 3, 's', "server", "Server to connect to", OPT_ARGREQ },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
52 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
53 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
54 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
55 { 7, 'S', "site", "Site (default: NN)", OPT_ARGREQ },
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
58 const int optListN = (sizeof(optList) / sizeof(optList[0]));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
61 int getColor(char *str)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
62 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
63 char *p = str;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
64 int len, val = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
65
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
66 for (len = 0; *p && len < 6; p++, len++) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
67 if (*p >= '0' && *p <= '9') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
68 val *= 16; val += (*p - '0');
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
69 } else if (*p >= 'A' && *p <= 'F') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
70 val *= 16; val += (*p - 'A') + 10;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
71 } else if (*p >= 'a' && *p <= 'f') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
72 val *= 16; val += (*p - 'a') + 10;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
73 } else
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
74 return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
75 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
76
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
77 return (len == 6) ? val : -1;
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
78 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
79
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
80
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
81 void argShowHelp()
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
82 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
83 th_args_help(stdout, optList, optListN, th_prog_name,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
84 "[options] <username> <password>");
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
85 }
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
86
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
87
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
90 switch (optN) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
91 case 0:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
92 argShowHelp();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
93 exit(0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
94 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
96 case 1:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
97 th_verbosityLevel++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
98 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
99
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
100 case 2:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
101 optPort = atoi(optArg);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
102 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
104 case 3:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
105 optServer = optArg;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
106 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
107
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
108 case 4:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
109 if ((optUserColor = getColor(optArg)) < 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
110 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n",
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
111 optArg);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
112 return FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
113 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
114 THMSG(1, "Using color #%06x\n", optUserColor);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
115 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
117 case 5:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
118 optLogFilename = optArg;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
119 break;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
121 case 7:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
122 optSite = optArg;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
123 break;
33
e49ea6febfeb Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents: 32
diff changeset
124
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
125 case 6:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
126 optDaemon = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
127 THMSG(1, "Running in pseudo-daemon mode.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
128 break;
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
129
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
130 default:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
131 THERR("Unknown option '%s'.\n", currArg);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
132 return FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
133 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
134
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
135 return TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 BOOL argHandleFile(char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
141 if (!optUserName)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
142 optUserName = currArg;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
143 else if (!optPassword)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
144 optPassword = currArg;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
145 else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
146 THERR("Username '%s' already specified on commandline!\n", optUserName);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
147 return FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
148 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
149
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
150 return TRUE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152
49
0bcc38910a77 Simple ringbuffer implementation.
Matti Hamalainen <ccr@tnsp.org>
parents: 46
diff changeset
153
30
Matti Hamalainen <ccr@tnsp.org>
parents: 29
diff changeset
154 void updateStatus(BOOL insertMode)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
155 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
156 char tmpStr[128] = "";
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
157 time_t timeStamp;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
158 struct tm *tmpTime;;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
159
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
160 if (statusWin == NULL) return;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
161
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
162 timeStamp = time(NULL);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
163 if ((tmpTime = localtime(&timeStamp)) != NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
164 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
165 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
166
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
167 wbkgdset(statusWin, 0x0d00);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
168 werase(statusWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
169
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
170 wattrset(statusWin, A_BOLD);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
171 mvwaddstr(statusWin, 0, 1, tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
172
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
173 waddstr(statusWin, " | ");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
174 wattrset(statusWin, A_BOLD | COLOR_PAIR(16));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
175 waddstr(statusWin, optUserName);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
176 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
177
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
178 waddstr(statusWin, " | ");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
179 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
180 waddstr(statusWin, insertMode ? "INS" : "DEL");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
181
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
182 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
183 waddstr(statusWin, " | Prv: ");
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
184
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
185 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
186 waddstr(statusWin, setTarget != NULL ? setTarget : "-");
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
187
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
188 wattrset(statusWin, A_BOLD | COLOR_PAIR(13));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
189 waddstr(statusWin, " | P/C: ");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
190 wattrset(statusWin, A_BOLD | COLOR_PAIR(11));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
191 snprintf(tmpStr, sizeof(tmpStr), "%d / #%06x", optPort, optUserColor);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
192 waddstr(statusWin, tmpStr);
29
a27ef0e359b9 Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents: 28
diff changeset
193
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
194 wrefresh(statusWin);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
195 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
196
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
197 void printEditBuf(char *str, nn_editbuf_t *buf)
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
198 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
199 if (statusWin == NULL || buf == NULL) return;
32
67ec4073e38c Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents: 31
diff changeset
200
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
201 buf->data[buf->len] = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
202 werase(editWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
203
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
204 wattrset(editWin, A_BOLD);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
205 mvwaddstr(editWin, 0, 0, str);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
206 waddstr(editWin, "> ");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
207 wattrset(editWin, A_NORMAL);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
208
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
209 if (buf->pos < buf->len) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
210 waddnstr(editWin, buf->data, buf->pos);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
211 wattrset(editWin, A_REVERSE);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
212 waddch(editWin, buf->data[buf->pos]);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
213 wattrset(editWin, A_NORMAL);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
214 waddnstr(editWin, buf->data + buf->pos + 1, buf->len - buf->pos - 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
215 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
216 waddnstr(editWin, buf->data, buf->len);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
217 wattrset(editWin, A_REVERSE);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
218 waddch(editWin, ' ');
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
219 wattrset(editWin, A_NORMAL);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
220 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
221 wrefresh(editWin);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
222 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
223
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
224 int printWin(WINDOW *win, const char *fmt)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
225 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
226 const char *s = fmt;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
227 int col = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
228
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
229 while (*s) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
230 if (*s == '½') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
231 int val = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
232 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
233 if (*s == '½') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
234 waddch(win, ((unsigned char) *s) | col);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
235 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
236 } else {
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
237 while (*s && isdigit((int) *s)) {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
238 val *= 10;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
239 val += (*s - '0');
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
240 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
241 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
242 if (*s != '½') return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
243 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
244
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
245 if (val < 9) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
246 col = A_DIM | COLOR_PAIR(val);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
247 } else if (val < 30) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
248 col = A_BOLD | COLOR_PAIR(val - 9);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
249 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
250 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
251 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
252 waddch(win, ((unsigned char) *s) | col);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
253 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
254 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
255 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
256 return 0;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
257 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
259
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
260 int printFile(FILE *outFile, const char *fmt)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
262 const char *s = fmt;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
263
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
264 while (*s) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
265 if (*s == '½') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
266 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
267 if (*s == '½') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
268 fputc((unsigned char) *s, outFile);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
269 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
270 } else {
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
271 while (*s && isdigit((int) *s)) s++;
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
272 if (*s != '½') return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
273 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
274 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
275 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
276 fputc((unsigned char) *s, outFile);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
277 s++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
278 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
279 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
280
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
281 return 0;
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
282 }
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
283
62
ff5d74f0d428 Moved some functions to "libnnchat".
Matti Hamalainen <ccr@tnsp.org>
parents: 61
diff changeset
284
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
285 void printMsg(char *fmt, ...)
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
286 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
287 char tmpStr[128] = "", buf[8192];
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
288 va_list ap;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
289 time_t timeStamp;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
290 struct tm *tmpTime;;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
291
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
292 timeStamp = time(NULL);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
293 if ((tmpTime = localtime(&timeStamp)) != NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
294 strftime(tmpStr, sizeof(tmpStr), "½17½[½11½%H:%M:%S½17½]½0½ ", tmpTime);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
295 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
296
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
297 va_start(ap, fmt);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
298 vsnprintf(buf, sizeof(buf), fmt, ap);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
299 va_end(ap);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
300
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
301 if (optLogFile) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
302 printFile(optLogFile, tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
303 printFile(optLogFile, buf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
304 fflush(optLogFile);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
305 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
306
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
307 if (!optDaemon) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
308 printWin(mainWin, tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
309 printWin(mainWin, buf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
310 wrefresh(mainWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
311 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
315 void errorMsg(char *fmt, ...)
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
316 {
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
317 char buf[8192];
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
318 va_list ap;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
319
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
320 va_start(ap, fmt);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
321 vsnprintf(buf, sizeof(buf), fmt, ap);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
322 va_end(ap);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
323
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
324 printMsg(buf);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
325 THERR(buf);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
326 }
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
327
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
328
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 int handleUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
331 const char *msg = "</USER><MESSAGE>";
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
332 char *p = str, *q, *s, *t, *h;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
333
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
334 (void) sock;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
335
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
336 s = strstr(str, msg);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
337 if (!s) return 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
338 *s = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
339 s += strlen(msg);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
340
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
341 q = strstr(s, "</MESSAGE>");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
342 if (!q) return 3;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
343 *q = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
344
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
345 s = decodeStr1(s);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
346 if (!s) return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
347
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
348 p = decodeStr1(p);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
349 if (!p) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
350 th_free(s);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
351 return -2;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
352 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
353
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
354
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
355 if (*s == '/') {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
356 t = stripXMLTags(s + 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
357 if (!strncmp(t, "BPRV", 4)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
358 h = decodeStr2(t + 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
359 printMsg("½11½%s½0½\n", h);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
360 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
361 h = decodeStr2(t);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
362 printMsg("½9½* %s½0½\n", h);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
363 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
364 th_free(h);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
365 th_free(t);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
366 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
367 BOOL isMine = strcmp(p, optUserName) == 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
368 t = stripXMLTags(s);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
369 h = decodeStr2(t);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
370 printMsg("½5½<½%d½%s½5½>½0½ %s\n", isMine ? 14 : 15, p, h);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
371 th_free(h);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
372 th_free(t);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
373 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
374
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
375 th_free(s);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
376 th_free(p);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
377 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 int handleLogin(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
383 char tmpStr[256] = "";
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
384 time_t timeStamp;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
385 struct tm *tmpTime;;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
386
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
387 timeStamp = time(NULL);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
388 if ((tmpTime = localtime(&timeStamp)) != NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
389 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
390 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
391
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
392 if (!strncmp(str, "FAILURE", 7)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
393 printMsg("½1½Login failure½0½ - ½3½%s½0½\n", tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
394 return -2;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
395 } else if (!strncmp(str, "SUCCESS", 7)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
396 printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
397 sendUserMsg(sock, optUserName2, "%%2FRequestUserList");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
398 return 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
399 } else
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
400 return 1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 int handleAddUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
406 char *p, *s = strstr(str, "</ADD_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
408 (void) sock;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
410 if (!s) return 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
411 *s = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
412
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
413 p = doubleDecodeStr(str);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
414 if (!p) return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
415
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
416 printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
417 th_free(p);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
418 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422 int handleDeleteUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
424 char *p, *s = strstr(str, "</DELETE_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
426 (void) sock;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
428 if (!s) return 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
429 *s = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
430
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
431 p = doubleDecodeStr(str);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
432 if (!p) return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
433
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
434 printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
435 th_free(p);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
436 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 int handleFoo(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
442 (void) sock; (void) str;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
443
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
444 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
448 int handleBoot(int sock, char *str)
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
449 {
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
450 (void) sock; (void) str;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
451 errorMsg("Booted by server.\n");
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
452 return -1;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
453 }
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
454
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
455
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 typedef struct {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
457 char *cmd;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
458 int (*handler)(int, char *);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459 } protocmd_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 protocmd_t protoCmds[] = {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
463 { "<USER>", handleUser },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
464 { "<LOGIN_", handleLogin },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
465 { "<DELETE_USER>", handleDeleteUser },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
466 { "<ADD_USER>", handleAddUser },
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
467 { "<NUMCLIENTS>", handleFoo },
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
468 { "<BOOT />", handleBoot },
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470
43
40ba8cdcf03a Cleanups, added a silly morse code conversion mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 41
diff changeset
471 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
474 int handleProtocol(const int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
476 int i;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
477
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
478 for (i = 0; i < nprotoCmds; i++) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
479 size_t cmdLen = strlen(protoCmds[i].cmd);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
480 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
481 return protoCmds[i].handler(sock, buf + cmdLen);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
482 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
483 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
484
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
485 return 1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488
51
cf7789d88350 Fix the input packet handling to properly handle packets with multiple
Matti Hamalainen <ccr@tnsp.org>
parents: 50
diff changeset
489 int handleUserInput(const int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 {
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
491 char *tmpStr, tmpBuf[4096];
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
492 BOOL result;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
493
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
494 /* Trim right */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
495 buf[--bufLen] = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
496 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen])))
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
497 buf[bufLen--] = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
498
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
499 /* Check command */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
500 if (*buf == 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
501 return 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
502 } else if (!strncmp(buf, "/color ", 7)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
503 int tmpInt;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
504 if ((tmpInt = getColor(buf+7)) < 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
505 printMsg("Invalid color value '%s'\n", buf+7);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
506 return 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
507 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
508 optUserColor = tmpInt;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
509 printMsg("Setting color to #%06x\n", optUserColor);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
510 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
511 return 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
512 } else if (!strncmp(buf, "/flood ", 7)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
513 int i;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
514
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
515 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .",
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
516 buf+7);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
517
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
518 tmpStr = doubleEncodeStr(tmpBuf);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
519 if (!tmpStr) return -2;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
520
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
521 result = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
522 for (i = 0; i < 50 && result; i++) {
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
523 result = sendUserMsg(sock, optUserName2, "%s", tmpStr);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
524 usleep(250);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
525 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
526
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
527 th_free(tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
528 return 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
529 } else if (!strncmp(buf, "/to ", 4)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
530 th_free(setTarget);
79
e36df57c5b0f Use th_strdup() again.
Matti Hamalainen <ccr@tnsp.org>
parents: 78
diff changeset
531 setTarget = th_strdup(buf + 4);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
532 printMsg("Set prv target to '%s'\n", setTarget);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
533 return 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
534 } else if (!strncmp(buf, "/who", 4)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
535 snprintf(tmpBuf, sizeof(tmpBuf), "/listallusers");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
536 buf = tmpBuf;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
537 } else if (setPrvMode) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
538 if (setTarget != NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
539 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
540 buf = tmpBuf;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
541 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
542 printMsg("No target set, exiting prv mode.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
543 setPrvMode = FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
544 return 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
545 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
546 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
547
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
548
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
549 /* Send double-encoded */
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
550 tmpStr = doubleEncodeStr(buf);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
551 if (!tmpStr) return -2;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
552
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
553 result = sendUserMsg(sock, optUserName2, "%s", tmpStr);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
554 th_free(tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
555 if (result)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
556 return 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
557 else
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
558 return -1;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
562 BOOL initializeWindows(void)
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
563 {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
564 if (mainWin) delwin(mainWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
565 if (statusWin) delwin(statusWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
566 if (editWin) delwin(editWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
567
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
568 mainWin = newwin(LINES - 4, COLS, 0, 0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
569 statusWin = newwin(1, COLS, LINES - 4, 0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
570 editWin = newwin(3, COLS, LINES - 3, 0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
571
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
572 if (mainWin == NULL || statusWin == NULL || editWin == NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
573 THERR("Could not create ncurses windows!\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
574 return FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
575 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
576 scrollok(mainWin, 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
577
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
578 return TRUE;
46
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
579 }
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
580
65b1ac6a1e2e Support for resizing the terminal on fly. Not perfect, but works.
Matti Hamalainen <ccr@tnsp.org>
parents: 45
diff changeset
581
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 int main(int argc, char *argv[])
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 {
85
1534b2c08c74 Initialize some variables.
Matti Hamalainen <ccr@tnsp.org>
parents: 80
diff changeset
584 int tmpSocket = -1, curVis = ERR, updateCount = 0;
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
585 struct hostent *tmpHost;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
586 BOOL argsOK, isError = FALSE,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
587 exitProg = FALSE,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
588 colorSet = FALSE,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
589 cursesInit = FALSE,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
590 networkInit = FALSE,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
591 insertMode = TRUE;
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
592 time_t prevTime;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
593 struct timeval socktv;
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
594 fd_set sockfds;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
595 char *tmpStr;
89
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
596 nn_editbuf_t *editBuf = newBuf(SET_BUFSIZE);
c2d916b340bf Change some typedef names; Add struct for user list handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 86
diff changeset
597 nn_editbuf_t *histBuf[SET_MAX_HISTORY+2];
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
598 int histPos = 0, histMax = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
599
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
600 memset(histBuf, 0, sizeof(histBuf));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
601
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
602 /* Initialize */
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
603 th_init("NNChat", "Newbie Nudes chat client", "0.7.2",
80
335b5a74c22e Updatec copyrights to 2009.
Matti Hamalainen <ccr@tnsp.org>
parents: 79
diff changeset
604 "Written and designed by Anonymous Finnish Guy (C) 2008-2009",
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
605 "This software is freeware, use and distribute as you wish.");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
606 th_verbosityLevel = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
607
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
608 /* Parse arguments */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
609 argsOK = th_args_process(argc, argv, optList, optListN,
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
610 argHandleOpt, argHandleFile, FALSE);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
611
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
612 /* Check the mode and arguments */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
613 if (optUserName == NULL || optPassword == NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
614 THERR("User/pass not specified, get some --help\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
615 return -1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
616 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
617
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
618 if (!argsOK)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
619 return -2;
64
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
620
6a3a917303e4 Some random cleanups, bring back WinSock support.
Matti Hamalainen <ccr@tnsp.org>
parents: 63
diff changeset
621
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
622 /* Open logfile */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
623 if (optLogFilename) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
624 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
625
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
626 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
627 THERR("Could not open logfile for appending!\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
628 return -9;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
629 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
630 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
631
91
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
632 if (!initNetwork()) {
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
633 THERR("Could not initialize network subsystem.\n");
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
634 goto err_exit;
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
635 } else
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
636 networkInit = TRUE;
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
637
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
638 /* Okay ... */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
639 THMSG(1, "Trying to resolve host '%s' ...\n", optServer);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
640 tmpHost = gethostbyname(optServer);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
641 if (tmpHost == NULL) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
642 THERR("Could not resolve hostname: %s.\n",
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
643 hstrerror(h_errno));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
644 goto err_exit;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
645 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
646 THMSG(2, "True hostname: %s\n", tmpHost->h_name);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
647
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
648
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
649 #if 1
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
650 /* To emulate the official client, we first make a request for
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
651 * policy file, even though we don't use it for anything...
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
652 */
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
653 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, 843)) < 0) {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
654 THERR("Policy file request connection setup failed!\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
655 goto err_exit;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
656 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
657
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
658 tmpStr = "<policy-file-request/>";
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
659 if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) {
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
660 THERR("Failed to send policy file request.\n");
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
661 goto err_exit;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
662 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
663 ssize_t gotBuf;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
664 char tmpBuf[SET_BUFSIZE];
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
665 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
666 tmpBuf[gotBuf-1] = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
667 THMSG(2, "Probe got: %s\n", tmpBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
668 closeConnection(tmpSocket);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
669 }
39
Matti Hamalainen <ccr@tnsp.org>
parents: 36
diff changeset
670 #endif
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
671
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
672 /* Okay, now do the proper connection ... */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
673 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
674 THERR("Main connection setup failed!\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
675 goto err_exit;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
676 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
677
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
678 THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite);
78
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
679 optUserName2 = doubleEncodeStr(optUserName);
745f670068dc Add functions to simplify code.
Matti Hamalainen <ccr@tnsp.org>
parents: 77
diff changeset
680 tmpStr = doubleEncodeStr(optSite);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
681 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
682 th_free(tmpStr);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
683
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
684 /* Initialize NCurses */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
685 if (!optDaemon) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
686 if (LINES < 0 || LINES > 1000) LINES = 24;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
687 if (COLS < 0 || COLS > 1000) COLS = 80;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
688 LINES=24;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
689 initscr();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
690 raw();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
691 keypad(stdscr, TRUE);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
692 noecho();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
693 meta(stdscr, TRUE);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
694 timeout(SET_DELAY);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
695 curVis = curs_set(0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
696 if (has_colors()) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
697 start_color();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
698
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
699 init_pair(1, COLOR_RED, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
700 init_pair(2, COLOR_GREEN, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
701 init_pair(3, COLOR_YELLOW, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
702 init_pair(4, COLOR_BLUE, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
703 init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
704 init_pair(6, COLOR_CYAN, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
705 init_pair(7, COLOR_WHITE, COLOR_BLACK);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
706 init_pair(8, COLOR_BLACK, COLOR_BLACK);
28
512775f6b081 A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents: 27
diff changeset
707
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
708 init_pair(10, COLOR_BLACK, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
709 init_pair(11, COLOR_WHITE, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
710 init_pair(12, COLOR_GREEN, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
711 init_pair(13, COLOR_YELLOW, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
712 init_pair(14, COLOR_BLUE, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
713 init_pair(15, COLOR_MAGENTA, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
714 init_pair(16, COLOR_CYAN, COLOR_RED);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
715
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
716 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
717
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
718 cursesInit = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
719
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
720 if (!initializeWindows())
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
721 goto err_exit;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
722
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
723 clearBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
724 printEditBuf("", editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
725 updateStatus(insertMode);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
726 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
727
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
728 /* Enter mainloop */
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
729 prevTime = time(NULL);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
730 FD_ZERO(&sockfds);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
731 FD_SET(tmpSocket, &sockfds);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
733 while (!isError && !exitProg) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
734 int result;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
735 fd_set tmpfds;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
736
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
737 /* Check for incoming data from the server */
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
738 socktv.tv_sec = 0;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
739 socktv.tv_usec = SET_DELAY_USEC;
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
740 tmpfds = sockfds;
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
741 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &socktv)) == -1) {
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
742 int res = getSocketErrno();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
743 if (res != EINTR) {
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
744 errorMsg("Error occured in select(sockfds): %d, %s\n",
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
745 res, getSocketErrStr(res));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
746 isError = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
747 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
748 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
749 ssize_t gotBuf;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
750 char tmpBuf[8192];
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
751 char *bufPtr = tmpBuf;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
752 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
753
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
754 if (gotBuf < 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
755 int res = getSocketErrno();
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
756 errorMsg("Error in recv: %d, %s\n", res, getSocketErrStr(res));
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
757 isError = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
758 } else if (gotBuf == 0) {
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
759 errorMsg("Server closed connection.\n");
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
760 isError = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
761 } else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
762 /* Handle protocol data */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
763 tmpBuf[gotBuf] = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
764 do {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
765 size_t bufLen = strlen(bufPtr) + 1;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
766 result = handleProtocol(tmpSocket, bufPtr, bufLen);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
767
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
768 if (result > 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
769 /* Couldn't handle the message for some reason */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
770 printMsg("Could not handle: %s\n", tmpBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
771 } else if (result < 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
772 /* Fatal error, quit */
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
773 errorMsg("Fatal error with message: %s\n", tmpBuf);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
774 isError = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
775 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
776
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
777 gotBuf -= bufLen;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
778 bufPtr += bufLen;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
779 } while (gotBuf > 0 && !isError);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
780 updateStatus(insertMode);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
781 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
782 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
783
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
784 /* Handle user input */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
785 if (!optDaemon) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
786 int c, cnt = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
787 BOOL update = FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
788
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
789 /* Handle several buffered keypresses at once */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
790 do {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
791 c = wgetch(stdscr);
77
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
792
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
793 if (c == 0x1b) {
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
794 c = wgetch(stdscr);
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
795 if (c == 'O') {
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
796 c = wgetch(stdscr);
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
797 switch (c) {
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
798 case 'd': c = 0x204; break;
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
799 case 'c': c = 0x206; break;
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
800 default:
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
801 printMsg("Unhandled ESC-O key sequence 0x%02x\n", c);
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
802 break;
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
803 }
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
804 } else {
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
805 printMsg("Unhandled ESC key sequence 0x%02x\n", c);
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
806 continue;
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
807 }
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
808 }
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
809
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
810 switch (c) {
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
811 #ifdef KEY_RESIZE
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
812 case KEY_RESIZE:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
813 if (!initializeWindows()) {
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
814 errorMsg("Error resizing ncurses windows\n");
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
815 isError = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
816 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
817 break;
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
818 #endif
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
819
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
820 case KEY_ENTER:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
821 case '\n':
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
822 case '\r':
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
823 /* Call the user input handler */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
824 if (editBuf->len > 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
825
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
826 if (histMax > 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
827 freeBuf(histBuf[SET_MAX_HISTORY+1]);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
828 histBuf[SET_MAX_HISTORY+1] = NULL;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
829 memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0]));
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
830 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
831
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
832 histPos = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
833 histBuf[1] = copyBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
834 if (histMax < SET_MAX_HISTORY) histMax++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
835
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
836 insertBuf(editBuf, editBuf->len, 0);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
837 result = handleUserInput(tmpSocket, editBuf->data, editBuf->len);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
838
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
839 clearBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
840
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
841 if (result < 0) {
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
842 errorMsg("Fatal error handling user input: %s\n", editBuf->data);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
843 isError = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
844 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
845
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
846 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
847 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
848 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
849
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
850 case 0x09: /* Tab = switch between PRV */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
851 if (setPrvMode)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
852 setPrvMode = FALSE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
853 else {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
854 if (setTarget != NULL)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
855 setPrvMode = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
856 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
857 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
858 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
859
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
860 case KEY_UP: /* Backwards in input history */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
861 if (histPos == 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
862 freeBuf(histBuf[0]);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
863 histBuf[0] = copyBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
864 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
865 if (histPos < histMax) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
866 histPos++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
867 freeBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
868 editBuf = copyBuf(histBuf[histPos]);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
869 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
870 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
871 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
872
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
873 case KEY_DOWN: /* Forwards in input history */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
874 if (histPos > 0) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
875 histPos--;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
876 freeBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
877 editBuf = copyBuf(histBuf[histPos]);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
878 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
879 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
880 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
881
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
882 case 0x204: /* ctrl+left = Skip words left */
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
883 while (editBuf->pos > 0 && isspace((int) editBuf->data[editBuf->pos - 1]))
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
884 editBuf->pos--;
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
885 while (editBuf->pos > 0 && !isspace((int) editBuf->data[editBuf->pos - 1]))
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
886 editBuf->pos--;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
887 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
888 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
889
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
890 case 0x206: /* ctrl+right = Skip words right */
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
891 while (editBuf->pos < editBuf->len && isspace((int) editBuf->data[editBuf->pos]))
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
892 editBuf->pos++;
74
98004129202a Portability fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 73
diff changeset
893 while (editBuf->pos < editBuf->len && !isspace((int) editBuf->data[editBuf->pos]))
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
894 editBuf->pos++;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
895 if (editBuf->pos > editBuf->len)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
896 editBuf->pos = editBuf->len;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
897 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
898 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
899
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
900 case 0x111: /* F9 = Quit */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
901 printMsg("Quitting per user request.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
902 exitProg = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
903 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
904
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
905 case 0x109: /* F1 = Toggle insert / overwrite mode */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
906 insertMode = !insertMode;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
907 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
908 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
909
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
910 case 0x10a: /* F2 = Clear editbuffer */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
911 clearBuf(editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
912 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
913 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
914
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
915 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
916 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
917 case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
918 case KEY_RIGHT: setBufPos(editBuf, editBuf->pos + 1); update = TRUE; break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
919
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
920 case KEY_BACKSPACE:
73
c56e766acce1 Support keycode 0x08 as backspace for Win32 compatibility.
Matti Hamalainen <ccr@tnsp.org>
parents: 70
diff changeset
921 case 0x08:
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
922 deleteBuf(editBuf, editBuf->pos - 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
923 setBufPos(editBuf, editBuf->pos - 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
924 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
925 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
926
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
927 case 0x14a:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
928 /* Delete */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
929 deleteBuf(editBuf, editBuf->pos);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
930 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
931 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
932
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
933 case 0x0c:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
934 /* ctrl+l */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
935 redrawwin(mainWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
936 redrawwin(statusWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
937 redrawwin(editWin);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
938 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
939
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
940 case ERR:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
941 /* Ignore */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
942 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
943
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
944 default:
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
945 if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
946 if (insertMode)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
947 insertBuf(editBuf, editBuf->pos, c);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
948 else
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
949 writeBuf(editBuf, editBuf->pos, c);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
950 setBufPos(editBuf, editBuf->pos + 1);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
951 update = TRUE;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
952 } else {
77
e8c9d7d13866 Handle certain key sequences better.
Matti Hamalainen <ccr@tnsp.org>
parents: 75
diff changeset
953 printMsg("Unhandled key: 0x%02x\n", c);
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
954 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
955 break;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
956 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
957 } while (c != ERR && !exitProg && ++cnt < 10);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
958
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
959 if (update) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
960 /* Update edit line */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
961 printEditBuf(setPrvMode ? setTarget : "", editBuf);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
962 updateStatus(insertMode);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
963 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
964 } /* !optDaemon */
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
965
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
966 if (++updateCount > 10) {
96
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
967 time_t tmpTime = time(NULL);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
968 if (tmpTime - prevTime > SET_KEEPALIVE) {
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
969 sendUserMsg(tmpSocket, optUserName2, "/listallusers");
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
970 prevTime = tmpTime;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
971 }
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
972
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
973 if (!colorSet) {
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
974 colorSet = TRUE;
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
975 printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
976 printMsg("%s\n", th_prog_author);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
977 printMsg("%s\n", th_prog_license);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
978 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
979 }
7c9538e71c89 Add connection keepalive by sending /listallusers every 15 minutes,
Matti Hamalainen <ccr@tnsp.org>
parents: 92
diff changeset
980
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
981 updateStatus(insertMode);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
982 updateCount = 0;
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
983 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
984
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
985 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
986
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
987 /* Shutdown */
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
988 err_exit:
75
abbdd101b267 Free some memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
989 freeBuf(editBuf);
abbdd101b267 Free some memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
990 for (histPos = 0; histPos <= SET_MAX_HISTORY; histPos++)
abbdd101b267 Free some memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
991 freeBuf(histBuf[histPos]);
abbdd101b267 Free some memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
992
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
993 if (cursesInit) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
994 if (curVis != ERR)
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
995 curs_set(curVis);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
996 endwin();
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
997 THMSG(1, "NCurses deinitialized.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
998 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
999
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1000 if (isError) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1001 THMSG(1, "Error exit.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1002 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1003
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1004 th_free(optUserName2);
10
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
1005
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1006 closeConnection(tmpSocket);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1007
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1008 if (networkInit)
91
acfc4b4bc180 Create network initialization functions in libnnchat and move Win32/WinSock code there.
Matti Hamalainen <ccr@tnsp.org>
parents: 90
diff changeset
1009 closeNetwork();
68
3ab7751fdad1 MingW compatibility, with one ugly kludge.
Matti Hamalainen <ccr@tnsp.org>
parents: 67
diff changeset
1010
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1011 THMSG(1, "Connection terminated.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1012
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1013 if (optLogFile) {
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1014 THMSG(1, "Closing logfile.\n");
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1015 fclose(optLogFile);
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1016 }
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1017
75
abbdd101b267 Free some memory.
Matti Hamalainen <ccr@tnsp.org>
parents: 74
diff changeset
1018
70
5228ad7b4f57 Remove tabs from indentation.
Matti Hamalainen <ccr@tnsp.org>
parents: 69
diff changeset
1019 return 0;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1020 }