Mercurial > hg > nnchat
annotate nnchat.c @ 40:3e2548c3eb51
Fix PRV handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 26 Oct 2008 02:50:41 +0300 |
parents | bb25bdfc0d71 |
children | fe3140cec818 |
rev | line source |
---|---|
39 | 1 /* NNChat - Custom chat client for NewbieNudes.com chatrooms |
2 * Written by Matti 'ccr' Hämäläinen | |
3 * (C) Copyright 2008 Tecnic Software productions (TNSP) | |
4 */ | |
0 | 5 #include <sys/socket.h> |
6 #include <sys/types.h> | |
7 #include <arpa/inet.h> | |
8 #include <sys/time.h> | |
9 #include <netdb.h> | |
10 | |
11 #include <unistd.h> | |
12 #include <stdlib.h> | |
13 #include <stdio.h> | |
14 #include "th_args.h" | |
15 #include "th_string.h" | |
16 #include <string.h> | |
17 #include <errno.h> | |
18 #include <time.h> | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
19 #include <ncurses.h> |
0 | 20 |
30 | 21 #define SET_MAX_HISTORY (16) |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
22 #define SET_BUFSIZE (4096) |
0 | 23 #define SET_ALLOC_SIZE (128) |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
24 #define SET_DELAY (15) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
25 #define SET_DELAY_USEC (SET_DELAY * 1000) |
0 | 26 |
27 /* Options | |
28 */ | |
27 | 29 int optPort = 8005; |
39 | 30 int optUserColor = 0x006080; |
27 | 31 char *optServer = "www11.servemedata.com", |
32 *optUserName = NULL, | |
33 *optUserName2 = NULL, | |
34 *optPassword = NULL, | |
35 *optLogFilename = NULL, | |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
36 *setTarget = NULL, |
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
37 *optSite = "NN"; |
21 | 38 BOOL optDaemon = FALSE; |
0 | 39 FILE *optLogFile = NULL; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
40 WINDOW *mainWin = NULL, |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
41 *statusWin = NULL, |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
42 *editWin = NULL; |
30 | 43 BOOL setPrvMode = FALSE; |
0 | 44 |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
45 |
0 | 46 /* Arguments |
47 */ | |
48 optarg_t optList[] = { | |
27 | 49 { 0, '?', "help", "Show this help", OPT_NONE }, |
50 { 1, 'v', "verbose", "Be more verbose", OPT_NONE }, | |
51 { 2, 'p', "port", "Connect to port", OPT_ARGREQ }, | |
52 { 3, 's', "server", "Server to connect to", OPT_ARGREQ }, | |
53 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ }, | |
54 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ }, | |
55 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE }, | |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
56 { 7, 'S', "site", "Site (default: NN)", OPT_ARGREQ }, |
0 | 57 }; |
58 | |
59 const int optListN = (sizeof(optList) / sizeof(optarg_t)); | |
60 | |
61 | |
62 void argShowHelp() | |
63 { | |
64 th_args_help(stdout, optList, optListN, th_prog_name, | |
65 "[options] <username> <password>"); | |
66 } | |
67 | |
68 | |
26
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
69 int getColor(char *str) |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
70 { |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
71 char *p = str; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
72 int len, val = 0; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
73 |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
74 for (len = 0; *p && len < 6; p++, len++) { |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
75 if (*p >= '0' && *p <= '9') { |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
76 val *= 16; val += (*p - '0'); |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
77 } else if (*p >= 'A' && *p <= 'F') { |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
78 val *= 16; val += (*p - 'A') + 10; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
79 } else if (*p >= 'a' && *p <= 'f') { |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
80 val *= 16; val += (*p - 'a') + 10; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
81 } else |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
82 return -1; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
83 } |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
84 |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
85 return (len == 6) ? val : -1; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
86 } |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
87 |
0 | 88 BOOL argHandleOpt(const int optN, char *optArg, char *currArg) |
89 { | |
90 switch (optN) { | |
91 case 0: | |
92 argShowHelp(); | |
93 exit(0); | |
94 break; | |
95 | |
96 case 1: | |
97 th_verbosityLevel++; | |
98 break; | |
99 | |
100 case 2: | |
101 optPort = atoi(optArg); | |
102 break; | |
103 | |
104 case 3: | |
105 optServer = optArg; | |
106 break; | |
107 | |
108 case 4: | |
26
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
109 if ((optUserColor = getColor(optArg)) < 0) { |
0 | 110 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n", |
111 optArg); | |
112 return FALSE; | |
113 } | |
114 THMSG(1, "Using color #%06x\n", optUserColor); | |
115 break; | |
116 | |
117 case 5: | |
118 optLogFilename = optArg; | |
119 break; | |
120 | |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
121 case 7: |
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
122 optSite = optArg; |
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
123 break; |
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
124 |
21 | 125 case 6: |
126 optDaemon = TRUE; | |
127 THMSG(1, "Running in pseudo-daemon mode.\n"); | |
128 break; | |
129 | |
0 | 130 default: |
131 THERR("Unknown option '%s'.\n", currArg); | |
132 return FALSE; | |
133 } | |
134 | |
135 return TRUE; | |
136 } | |
137 | |
138 | |
139 BOOL argHandleFile(char *currArg) | |
140 { | |
141 if (!optUserName) | |
142 optUserName = currArg; | |
143 else if (!optPassword) | |
144 optPassword = currArg; | |
145 else { | |
146 THERR("Username '%s' already specified on commandline!\n", optUserName); | |
147 return FALSE; | |
148 } | |
149 | |
150 return TRUE; | |
151 } | |
152 | |
153 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
154 typedef struct { |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
155 ssize_t pos, len, size; |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
156 char *data; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
157 } editbuf_t; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
158 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
159 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
160 int writeBuf(editbuf_t *buf, ssize_t pos, int ch) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
161 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
162 /* Check arguments */ |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
163 if (buf->len+1 >= buf->size) return -3; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
164 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
165 if (pos < 0) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
166 return -1; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
167 else if (pos >= buf->len) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
168 buf->data[buf->len++] = ch; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
169 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
170 buf->data[pos] = ch; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
171 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
172 return 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
173 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
174 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
175 int insertBuf(editbuf_t *buf, ssize_t pos, int ch) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
176 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
177 /* Check arguments */ |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
178 if (buf->len+1 >= buf->size) return -3; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
179 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
180 if (pos < 0) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
181 return -1; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
182 else if (pos >= buf->len) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
183 buf->data[buf->len] = ch; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
184 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
185 memmove(&(buf->data[pos+1]), &(buf->data[pos]), buf->len - pos + 1); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
186 buf->data[pos] = ch; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
187 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
188 buf->len++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
189 return 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
190 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
191 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
192 int deleteBuf(editbuf_t *buf, ssize_t pos) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
193 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
194 /* Check arguments */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
195 if (pos < 0) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
196 return -1; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
197 else if (pos < buf->len) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
198 memmove(&(buf->data[pos]), &(buf->data[pos+1]), buf->len - pos); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
199 buf->len--; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
200 return 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
201 } else |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
202 return -2; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
203 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
204 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
205 void clearBuf(editbuf_t *buf) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
206 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
207 buf->len = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
208 buf->pos = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
209 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
210 |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
211 editbuf_t * newBuf(ssize_t n) |
30 | 212 { |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
213 editbuf_t *res = th_calloc(1, sizeof(editbuf_t)); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
214 |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
215 res->data = (char *) th_malloc(n); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
216 res->size = n; |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
217 |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
218 return res; |
30 | 219 } |
220 | |
221 void freeBuf(editbuf_t *buf) | |
222 { | |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
223 if (buf) { |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
224 th_free(buf->data); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
225 th_free(buf); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
226 } |
30 | 227 } |
228 | |
229 editbuf_t * copyBuf(editbuf_t *src) | |
230 { | |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
231 editbuf_t *res; |
30 | 232 |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
233 assert(src != NULL); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
234 |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
235 if (src == NULL) return NULL; |
30 | 236 |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
237 if ((res = newBuf(src->size)) == NULL) |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
238 return NULL; |
30 | 239 |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
240 memcpy(res->data, src->data, src->size); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
241 res->pos = res->len = src->len; |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
242 |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
243 return res; |
30 | 244 } |
245 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
246 void setBufPos(editbuf_t *buf, ssize_t pos) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
247 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
248 /* Check arguments */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
249 if (pos < 0) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
250 buf->pos = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
251 else if (pos >= buf->len) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
252 buf->pos = buf->len; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
253 else |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
254 buf->pos = pos; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
255 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
256 |
30 | 257 void updateStatus(BOOL insertMode) |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
258 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
259 char tmpStr[128] = ""; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
260 time_t timeStamp; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
261 struct tm *tmpTime;; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
262 |
32
67ec4073e38c
Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
263 if (statusWin == NULL) return; |
67ec4073e38c
Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
264 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
265 timeStamp = time(NULL); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
266 if ((tmpTime = localtime(&timeStamp)) != NULL) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
267 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
268 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
269 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
270 wbkgdset(statusWin, 0x0d00); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
271 werase(statusWin); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
272 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
273 wattrset(statusWin, A_BOLD); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
274 mvwaddstr(statusWin, 0, 1, tmpStr); |
29
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
275 |
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
276 waddstr(statusWin, " | "); |
30 | 277 wattrset(statusWin, A_BOLD | COLOR_PAIR(16)); |
29
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
278 waddstr(statusWin, optUserName); |
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
279 wattrset(statusWin, A_BOLD | COLOR_PAIR(13)); |
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
280 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
281 waddstr(statusWin, " | "); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
282 wattrset(statusWin, A_BOLD | COLOR_PAIR(11)); |
30 | 283 waddstr(statusWin, insertMode ? "INS" : "DEL"); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
284 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
285 wattrset(statusWin, A_BOLD | COLOR_PAIR(13)); |
29
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
286 waddstr(statusWin, " | Prv: "); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
287 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
288 wattrset(statusWin, A_BOLD | COLOR_PAIR(11)); |
29
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
289 waddstr(statusWin, setTarget != NULL ? setTarget : "-"); |
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
290 |
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
291 wattrset(statusWin, A_BOLD | COLOR_PAIR(13)); |
30 | 292 waddstr(statusWin, " | P/C: "); |
29
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
293 wattrset(statusWin, A_BOLD | COLOR_PAIR(11)); |
30 | 294 snprintf(tmpStr, sizeof(tmpStr), "%d / #%06x", optPort, optUserColor); |
29
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
295 waddstr(statusWin, tmpStr); |
a27ef0e359b9
Make statusline more informative.
Matti Hamalainen <ccr@tnsp.org>
parents:
28
diff
changeset
|
296 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
297 wrefresh(statusWin); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
298 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
299 |
30 | 300 void printEditBuf(char *str, editbuf_t *buf) |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
301 { |
32
67ec4073e38c
Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
302 if (statusWin == NULL || buf == NULL) return; |
67ec4073e38c
Check for NULL pointers (fixes "daemon" mode)
Matti Hamalainen <ccr@tnsp.org>
parents:
31
diff
changeset
|
303 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
304 buf->data[buf->len] = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
305 werase(editWin); |
30 | 306 |
307 wattrset(editWin, A_BOLD); | |
308 mvwaddstr(editWin, 0, 0, str); | |
309 waddstr(editWin, "> "); | |
310 wattrset(editWin, A_NORMAL); | |
311 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
312 if (buf->pos < buf->len) { |
30 | 313 waddnstr(editWin, buf->data, buf->pos); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
314 wattrset(editWin, A_REVERSE); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
315 waddch(editWin, buf->data[buf->pos]); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
316 wattrset(editWin, A_NORMAL); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
317 waddnstr(editWin, buf->data + buf->pos + 1, buf->len - buf->pos - 1); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
318 } else { |
30 | 319 waddnstr(editWin, buf->data, buf->len); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
320 wattrset(editWin, A_REVERSE); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
321 waddch(editWin, ' '); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
322 wattrset(editWin, A_NORMAL); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
323 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
324 wrefresh(editWin); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
325 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
326 |
15 | 327 int openConnection(struct in_addr *addr, int port) |
328 { | |
329 struct sockaddr_in tmpAddr; | |
330 int sock = -1; | |
331 | |
332 tmpAddr.sin_family = AF_INET; | |
333 tmpAddr.sin_port = htons(port); | |
334 tmpAddr.sin_addr = *addr; | |
335 | |
336 THMSG(1, "Connecting to %s:%d ...\n", | |
337 inet_ntoa(tmpAddr.sin_addr), port); | |
338 | |
339 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { | |
340 THERR("Could not open socket: %s\n", strerror(errno)); | |
341 return -2; | |
342 } | |
343 | |
344 THMSG(2, "Using socket %d.\n", sock); | |
345 | |
346 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) { | |
347 THERR("Could not connect: %s\n", strerror(errno)); | |
348 return -5; | |
349 } | |
350 | |
351 return sock; | |
352 } | |
353 | |
354 | |
355 void closeConnection(int sock) | |
356 { | |
357 if (sock >= 0) { | |
358 close(sock); | |
359 } | |
360 } | |
361 | |
362 | |
0 | 363 BOOL sendToSocket(int sock, char *buf, const size_t bufLen) |
364 { | |
365 size_t bufLeft = bufLen; | |
366 char *bufPtr = buf; | |
367 | |
368 while (bufLeft > 0) { | |
369 ssize_t bufSent; | |
370 bufSent = send(sock, bufPtr, bufLeft, 0); | |
371 if (bufSent < 0) return FALSE; | |
372 bufLeft -= bufSent; | |
373 bufPtr += bufSent; | |
374 } | |
375 return TRUE; | |
376 } | |
377 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
378 int printWin(WINDOW *win, const char *fmt) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
379 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
380 const char *s = fmt; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
381 int col = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
382 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
383 while (*s) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
384 if (*s == '½') { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
385 int val = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
386 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
387 if (*s == '½') { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
388 waddch(win, *s | col); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
389 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
390 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
391 while (*s && isdigit(*s)) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
392 val *= 10; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
393 val += (*s - '0'); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
394 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
395 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
396 if (*s != '½') return -1; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
397 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
398 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
399 if (val < 9) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
400 col = A_DIM | COLOR_PAIR(val); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
401 } else if (val < 30) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
402 col = A_BOLD | COLOR_PAIR(val - 9); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
403 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
404 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
405 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
406 waddch(win, *s | col); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
407 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
408 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
409 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
410 return 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
411 } |
0 | 412 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
413 int printFile(FILE *outFile, const char *fmt) |
0 | 414 { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
415 const char *s = fmt; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
416 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
417 while (*s) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
418 if (*s == '½') { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
419 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
420 if (*s == '½') { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
421 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
422 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
423 while (*s && isdigit(*s)) s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
424 if (*s != '½') return -1; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
425 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
426 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
427 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
428 fputc(*s, outFile); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
429 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
430 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
431 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
432 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
433 return 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
434 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
435 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
436 void printMsg(char *fmt, ...) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
437 { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
438 char tmpStr[128] = "", buf[8192]; |
0 | 439 va_list ap; |
440 time_t timeStamp; | |
441 struct tm *tmpTime;; | |
442 | |
443 timeStamp = time(NULL); | |
444 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
445 strftime(tmpStr, sizeof(tmpStr), "½17½[½11½%H:%M:%S½17½]½0½ ", tmpTime); |
0 | 446 } |
447 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
448 va_start(ap, fmt); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
449 vsnprintf(buf, sizeof(buf), fmt, ap); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
450 va_end(ap); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
451 |
0 | 452 if (optLogFile) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
453 printFile(optLogFile, tmpStr); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
454 printFile(optLogFile, buf); |
0 | 455 fflush(optLogFile); |
456 } | |
457 | |
21 | 458 if (!optDaemon) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
459 printWin(mainWin, tmpStr); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
460 printWin(mainWin, buf); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
461 wrefresh(mainWin); |
21 | 462 } |
0 | 463 } |
464 | |
465 | |
466 BOOL bufRealloc(char **buf, size_t *size, size_t add) | |
467 { | |
468 return ((*buf = th_realloc(*buf, *size + add)) != NULL); | |
469 } | |
470 | |
12 | 471 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x) |
0 | 472 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch) |
473 { | |
474 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE)) | |
475 return FALSE; | |
476 | |
477 (*buf)[*pos] = ch; | |
478 (*pos)++; | |
479 return TRUE; | |
480 } | |
481 | |
12 | 482 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x) |
0 | 483 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str) |
484 { | |
485 size_t tmpLen; | |
486 | |
487 if (!str) return FALSE; | |
488 tmpLen = strlen(str); | |
489 | |
490 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE)) | |
491 return FALSE; | |
492 | |
493 strcpy(*buf + *pos, str); | |
494 (*pos) += tmpLen; | |
495 return TRUE; | |
496 } | |
497 | |
498 | |
499 char *encodeStr1(char *str) | |
500 { | |
501 char *result, *s = str; | |
502 size_t resSize, resPos = 0; | |
503 | |
504 if (!str) return NULL; | |
505 | |
506 resSize = strlen(str) + SET_ALLOC_SIZE; | |
507 if ((result = th_malloc(resSize)) == NULL) | |
508 return NULL; | |
509 | |
510 while (*s) { | |
511 switch (*s) { | |
512 case 32: | |
12 | 513 PUSHCHAR('+'); |
0 | 514 break; |
515 | |
516 default: | |
517 if (th_isalnum(*s)) | |
12 | 518 PUSHCHAR(*s); |
0 | 519 else { |
520 char tmpStr[4]; | |
521 sprintf(tmpStr, "%2X", (unsigned char) *s); | |
12 | 522 PUSHCHAR('%'); |
523 PUSHSTR(tmpStr); | |
0 | 524 } |
525 break; | |
526 } | |
527 s++; | |
528 } | |
12 | 529 PUSHCHAR(0); |
0 | 530 |
531 return result; | |
532 } | |
533 | |
15 | 534 |
0 | 535 int getxdigit(int c, int shift) |
536 { | |
537 int i; | |
538 | |
539 if (c >= 'A' && c <= 'F') | |
540 i = c - 'A' + 10; | |
541 else if (c >= 'a' && c <= 'f') | |
542 i = c - 'a' + 10; | |
543 else if (c >= '0' && c <= '9') | |
544 i = c - '0'; | |
545 else | |
546 return -1; | |
547 | |
548 return i << shift; | |
549 } | |
550 | |
15 | 551 |
0 | 552 char *decodeStr1(char *str) |
553 { | |
554 char *result, *s = str; | |
555 size_t resSize, resPos = 0; | |
556 int c; | |
557 | |
558 if (!str) return NULL; | |
559 | |
560 resSize = strlen(str) + SET_ALLOC_SIZE; | |
561 if ((result = th_malloc(resSize)) == NULL) | |
562 return NULL; | |
563 | |
564 while (*s) { | |
565 switch (*s) { | |
566 case '+': | |
12 | 567 PUSHCHAR(' '); |
0 | 568 s++; |
569 break; | |
570 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
571 case '½': |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
572 /* Escape these .. */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
573 PUSHCHAR('½'); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
574 PUSHCHAR('½'); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
575 s++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
576 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
577 |
27 | 578 case '\r': |
579 PUSHCHAR(' '); | |
580 s++; | |
581 break; | |
582 | |
0 | 583 case '%': |
584 s++; | |
585 if (*s == '%') | |
12 | 586 PUSHCHAR('%'); |
0 | 587 else if ((c = getxdigit(*s, 4)) >= 0) { |
588 int i = getxdigit(*(++s), 0); | |
589 if (i >= 0) { | |
12 | 590 PUSHCHAR(c | i); |
0 | 591 } else { |
12 | 592 PUSHCHAR('§'); |
593 PUSHCHAR(*s); | |
0 | 594 } |
595 } else { | |
12 | 596 PUSHCHAR('§'); |
597 PUSHCHAR(*s); | |
0 | 598 } |
599 s++; | |
600 break; | |
601 | |
602 default: | |
12 | 603 PUSHCHAR(*s); |
0 | 604 s++; |
605 } | |
606 } | |
12 | 607 PUSHCHAR(0); |
0 | 608 |
609 return result; | |
610 } | |
611 | |
612 | |
613 char *stripTags(char *str) | |
614 { | |
615 char *result, *s = str; | |
616 size_t resSize, resPos = 0; | |
617 | |
618 if (!str) return NULL; | |
619 | |
620 resSize = strlen(str) + SET_ALLOC_SIZE; | |
621 if ((result = th_malloc(resSize)) == NULL) | |
622 return NULL; | |
623 | |
624 while (*s) { | |
625 if (*s == '<') { | |
626 while (*s && *s != '>') s++; | |
627 if (*s == '>') s++; | |
628 } else | |
12 | 629 PUSHCHAR(*s++); |
0 | 630 } |
12 | 631 PUSHCHAR(0); |
0 | 632 |
633 return result; | |
634 } | |
635 | |
636 | |
637 typedef struct { | |
638 char c; | |
639 char *ent; | |
640 } html_entity_t; | |
641 | |
642 | |
643 html_entity_t HTMLEntities[] = { | |
644 { '<', "<" }, | |
645 { '>', ">" }, | |
646 }; | |
647 | |
648 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t)); | |
649 | |
650 | |
651 char *encodeStr2(char *str) | |
652 { | |
653 char *result, *s = str; | |
654 size_t resSize, resPos = 0; | |
655 | |
656 if (!str) return NULL; | |
657 | |
658 resSize = strlen(str) + SET_ALLOC_SIZE; | |
659 if ((result = th_malloc(resSize)) == NULL) | |
660 return NULL; | |
661 | |
662 while (*s) { | |
663 int i; | |
664 BOOL found = FALSE; | |
665 for (i = 0; i < numHTMLEntities; i++) | |
666 if (HTMLEntities[i].c == *s) { | |
12 | 667 PUSHSTR(HTMLEntities[i].ent); |
0 | 668 found = TRUE; |
669 break; | |
670 } | |
12 | 671 if (!found) PUSHCHAR(*s); |
0 | 672 |
673 s++; | |
674 } | |
12 | 675 PUSHCHAR(0); |
0 | 676 |
677 return result; | |
678 } | |
679 | |
680 | |
681 char *decodeStr2(char *str) | |
682 { | |
683 char *result, *s = str; | |
684 size_t resSize, resPos = 0; | |
685 | |
686 if (!str) return NULL; | |
687 | |
688 resSize = strlen(str); | |
689 if ((result = th_malloc(resSize)) == NULL) | |
690 return NULL; | |
691 | |
692 while (*s) { | |
693 if (*s == '&') { | |
694 int i; | |
695 BOOL found = FALSE; | |
696 for (i = 0; i < numHTMLEntities; i++) { | |
697 html_entity_t *ent = &HTMLEntities[i]; | |
698 int len = strlen(ent->ent); | |
699 if (!strncmp(s, ent->ent, len)) { | |
12 | 700 PUSHCHAR(ent->c); |
0 | 701 s += len; |
702 found = TRUE; | |
703 break; | |
704 } | |
705 } | |
12 | 706 if (!found) PUSHCHAR(*s++); |
0 | 707 } else |
12 | 708 PUSHCHAR(*s++); |
0 | 709 } |
12 | 710 PUSHCHAR(0); |
0 | 711 |
712 return result; | |
713 } | |
714 | |
715 | |
716 BOOL sendUserMsg(int sock, char *user, char *fmt, ...) | |
717 { | |
718 char tmpBuf[4096], tmpBuf2[4096+256]; | |
719 int n; | |
720 va_list ap; | |
721 | |
722 va_start(ap, fmt); | |
723 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap); | |
724 va_end(ap); | |
725 | |
726 if (n < 0) return FALSE; | |
727 | |
728 snprintf(tmpBuf2, sizeof(tmpBuf2), | |
729 "<USER>%s</USER><MESSAGE>%s</MESSAGE>", | |
730 user, tmpBuf); | |
731 | |
732 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); | |
733 } | |
734 | |
735 | |
736 int handleUser(int sock, char *str) | |
737 { | |
738 const char *msg = "</USER><MESSAGE>"; | |
22 | 739 char *p = str, *q, *s, *t, *h; |
0 | 740 |
741 (void) sock; | |
742 | |
743 s = strstr(str, msg); | |
744 if (!s) return 1; | |
745 *s = 0; | |
746 s += strlen(msg); | |
747 | |
748 q = strstr(s, "</MESSAGE>"); | |
749 if (!q) return 3; | |
750 *q = 0; | |
751 | |
752 s = decodeStr1(s); | |
753 if (!s) return -1; | |
754 | |
755 p = decodeStr1(p); | |
756 if (!p) { | |
757 th_free(s); | |
758 return -2; | |
759 } | |
760 | |
761 | |
762 if (*s == '/') { | |
40 | 763 t = stripTags(s + 1); |
764 if (!strncmp(t, "BPRV", 4)) { | |
765 h = decodeStr2(t + 1); | |
766 printMsg("½11½%s½0½\n", h); | |
767 } else { | |
22 | 768 h = decodeStr2(t); |
40 | 769 printMsg("½9½* %s½0½\n", h); |
19 | 770 } |
22 | 771 th_free(h); |
0 | 772 th_free(t); |
773 } else { | |
22 | 774 t = stripTags(s); |
775 h = decodeStr2(t); | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
776 printMsg("½5½<½15½%s½5½>½0½ %s\n", p, h); |
22 | 777 th_free(h); |
0 | 778 th_free(t); |
779 } | |
780 | |
781 th_free(s); | |
782 th_free(p); | |
783 return 0; | |
784 } | |
785 | |
786 | |
787 int handleLogin(int sock, char *str) | |
788 { | |
19 | 789 char tmpStr[256] = ""; |
790 time_t timeStamp; | |
791 struct tm *tmpTime;; | |
792 | |
793 timeStamp = time(NULL); | |
794 if ((tmpTime = localtime(&timeStamp)) != NULL) { | |
795 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime); | |
796 } | |
797 | |
0 | 798 if (!strncmp(str, "FAILURE", 7)) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
799 printMsg("½1½Login failure½0½ - ½3½%s½0½\n", tmpStr); |
0 | 800 return -2; |
801 } else if (!strncmp(str, "SUCCESS", 7)) { | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
802 printMsg("½2½Login success½0½ - ½3½%s½0½\n", tmpStr); |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
803 sendUserMsg(sock, optUserName2, "%%2FRequestUserList"); |
0 | 804 return 0; |
805 } else | |
806 return 1; | |
807 } | |
808 | |
809 | |
810 int handleAddUser(int sock, char *str) | |
811 { | |
9 | 812 char *p, *s = strstr(str, "</ADD_USER>"); |
0 | 813 |
814 (void) sock; | |
815 | |
816 if (!s) return 1; | |
817 *s = 0; | |
9 | 818 |
819 p = decodeStr1(str); | |
820 if (!p) return -1; | |
821 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
822 printMsg("! ½3½%s½0½ ½2½ADDED.½0½\n", p); |
9 | 823 th_free(p); |
0 | 824 return 0; |
825 } | |
826 | |
827 | |
828 int handleDeleteUser(int sock, char *str) | |
829 { | |
9 | 830 char *p, *s = strstr(str, "</DELETE_USER>"); |
0 | 831 |
832 (void) sock; | |
833 | |
834 if (!s) return 1; | |
835 *s = 0; | |
9 | 836 |
837 p = decodeStr1(str); | |
838 if (!p) return -1; | |
839 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
840 printMsg("! ½3½%s½0½ ½1½DELETED.½0½\n", p); |
9 | 841 th_free(p); |
0 | 842 return 0; |
843 } | |
844 | |
845 | |
846 int handleFoo(int sock, char *str) | |
847 { | |
848 (void) sock; (void) str; | |
849 | |
850 return 0; | |
851 } | |
852 | |
853 | |
854 typedef struct { | |
855 char *cmd; | |
856 int (*handler)(int, char *); | |
857 } protocmd_t; | |
858 | |
859 | |
860 protocmd_t protoCmds[] = { | |
861 { "<USER>", handleUser }, | |
862 { "<LOGIN_", handleLogin }, | |
863 { "<DELETE_USER>", handleDeleteUser }, | |
864 { "<ADD_USER>", handleAddUser }, | |
865 { "<NUMCLIENTS>", handleFoo }, | |
866 }; | |
867 | |
868 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protocmd_t)); | |
869 | |
870 | |
871 int handleProtocol(int sock, char *buf, size_t bufLen) | |
872 { | |
873 int i; | |
874 | |
875 for (i = 0; i < nprotoCmds; i++) { | |
876 size_t cmdLen = strlen(protoCmds[i].cmd); | |
877 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) { | |
878 return protoCmds[i].handler(sock, buf + cmdLen); | |
879 } | |
880 } | |
881 | |
882 return 1; | |
883 } | |
884 | |
885 | |
15 | 886 int handleUserInput(int sock, char *buf, size_t bufLen) |
0 | 887 { |
25
3b67a9a806a7
Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
888 char *tmpStr, *tmpStr2, tmpBuf[4096]; |
0 | 889 BOOL result; |
890 | |
891 /* Trim right */ | |
892 buf[--bufLen] = 0; | |
893 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen]))) | |
894 buf[bufLen--] = 0; | |
895 | |
896 /* Check command */ | |
897 if (*buf == 0) { | |
898 return 1; | |
25
3b67a9a806a7
Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
899 } else if (!strncmp(buf, "/color ", 7)) { |
30 | 900 int tmpInt; |
901 if ((tmpInt = getColor(buf+7)) < 0) { | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
902 printMsg("Invalid color value '%s'\n", buf+7); |
25
3b67a9a806a7
Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
903 return 1; |
3b67a9a806a7
Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
904 } |
30 | 905 optUserColor = tmpInt; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
906 printMsg("Setting color to #%06x\n", optUserColor); |
25
3b67a9a806a7
Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
907 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); |
3b67a9a806a7
Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents:
24
diff
changeset
|
908 return 0; |
27 | 909 } else if (!strncmp(buf, "/flood ", 7)) { |
910 int i; | |
911 | |
912 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .", | |
913 buf+7); | |
914 | |
915 tmpStr = encodeStr2(tmpBuf); | |
916 if (!tmpStr) return -2; | |
917 tmpStr2 = encodeStr1(tmpStr); | |
918 if (!tmpStr2) { | |
919 th_free(tmpStr); | |
920 return -3; | |
921 } | |
922 | |
923 result = TRUE; | |
924 for (i = 0; i < 50 && result; i++) { | |
925 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2); | |
926 usleep(250); | |
927 } | |
928 | |
929 th_free(tmpStr); | |
930 th_free(tmpStr2); | |
931 return 0; | |
30 | 932 } else if (!strncmp(buf, "/to ", 4)) { |
933 buf += 4; | |
934 th_free(setTarget); | |
935 setTarget = th_strdup(buf); | |
936 printMsg("Set prv target to '%s'\n", setTarget); | |
937 return 0; | |
24
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
938 } else if (!strncmp(buf, "/msg ", 5)) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
939 if (setTarget != NULL) { |
24
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
940 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf+5); |
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
941 buf = tmpBuf; |
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
942 } else { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
943 printMsg("No target set!\n"); |
24
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
944 return 1; |
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
945 } |
30 | 946 } else if (setPrvMode) { |
947 if (setTarget != NULL) { | |
948 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg %s", setTarget, buf); | |
949 buf = tmpBuf; | |
950 } else { | |
951 printMsg("No target set, exiting prv mode.\n"); | |
952 setPrvMode = FALSE; | |
953 return 1; | |
954 } | |
24
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
955 } |
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
956 |
78d260256450
Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents:
23
diff
changeset
|
957 { |
0 | 958 /* Send double-encoded */ |
959 tmpStr = encodeStr2(buf); | |
960 if (!tmpStr) return -2; | |
961 tmpStr2 = encodeStr1(tmpStr); | |
962 if (!tmpStr2) { | |
963 th_free(tmpStr); | |
964 return -3; | |
965 } | |
966 | |
967 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2); | |
968 th_free(tmpStr); | |
969 th_free(tmpStr2); | |
970 if (result) | |
971 return 0; | |
972 else | |
973 return -1; | |
974 } | |
975 } | |
976 | |
977 | |
978 int main(int argc, char *argv[]) | |
979 { | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
980 int tmpSocket, curVis, updateCount = 0; |
0 | 981 struct hostent *tmpHost; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
982 BOOL argsOK, isError = FALSE, |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
983 exitProg = FALSE, |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
984 colorSet = FALSE, |
30 | 985 cursesInit = FALSE, |
986 insertMode = TRUE; | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
987 struct timeval tv; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
988 fd_set sockfds; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
989 char *tmpStr; |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
990 editbuf_t *editBuf = newBuf(SET_BUFSIZE); |
30 | 991 editbuf_t *histBuf[SET_MAX_HISTORY+2]; |
992 int histPos = 0, histMax = 0; | |
993 | |
994 memset(histBuf, 0, sizeof(histBuf)); | |
995 | |
0 | 996 /* Initialize */ |
34
f5d335771b16
Fix a bug with mixed editBuf->pos vs editBuf->len.
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
997 th_init("NNChat", "Newbie Nudes chat client", "0.6", |
6
526ba3b578d7
Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
998 "Written and designed by Anonymous Finnish Guy (C) 2008", |
526ba3b578d7
Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents:
4
diff
changeset
|
999 "This software is freeware, use and distribute as you wish."); |
0 | 1000 th_verbosityLevel = 0; |
1001 | |
1002 /* Parse arguments */ | |
26
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
1003 argsOK = th_args_process(argc, argv, optList, optListN, |
0 | 1004 argHandleOpt, argHandleFile, FALSE); |
26
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
1005 |
0 | 1006 /* Check the mode and arguments */ |
1007 if (optUserName == NULL || optPassword == NULL) { | |
1008 THERR("User/pass not specified, get some --help\n"); | |
1009 return -1; | |
1010 } | |
1011 | |
26
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
1012 if (!argsOK) |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
1013 return -2; |
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
1014 |
0 | 1015 /* Open logfile */ |
1016 if (optLogFilename) { | |
1017 THMSG(1, "Opening logfile '%s'\n", optLogFilename); | |
1018 | |
1019 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) { | |
1020 THERR("Could not open logfile for appending!\n"); | |
1021 return -9; | |
1022 } | |
1023 } | |
1024 | |
1025 /* Okay ... */ | |
1026 THMSG(1, "Trying to resolve host '%s' ...\n", optServer); | |
1027 tmpHost = gethostbyname(optServer); | |
1028 if (tmpHost == NULL) { | |
1029 THERR("Could not resolve hostname: %s.\n", | |
1030 hstrerror(h_errno)); | |
1031 return -3; | |
1032 } | |
1033 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
|
1034 |
39 | 1035 #if 0 |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1036 /* To emulate the official client, we first make a fake connection ... */ |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1037 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1038 THERR("Fakeprobe connection setup failed!\n"); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1039 goto err_exit; |
0 | 1040 } |
1041 | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1042 tmpStr = "<policy-file-request/>"; |
23
40fecbab1dc1
Check for error condition properly.
Matti Hamalainen <ccr@tnsp.org>
parents:
22
diff
changeset
|
1043 if (sendToSocket(tmpSocket, tmpStr, strlen(tmpStr) + 1) == FALSE) { |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1044 THERR("Failed to send fakeprobe.\n"); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1045 goto err_exit; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1046 } else { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1047 ssize_t gotBuf; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1048 char tmpBuf[4096]; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1049 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1050 tmpBuf[gotBuf-1] = 0; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1051 THMSG(2, "Probe got: %s\n", tmpBuf); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1052 closeConnection(tmpSocket); |
0 | 1053 } |
39 | 1054 #endif |
26
b84fc46c6035
Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents:
25
diff
changeset
|
1055 |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1056 /* Okay, now do the proper connection ... */ |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1057 if ((tmpSocket = openConnection((struct in_addr *) tmpHost->h_addr, optPort)) < 0) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1058 THERR("Main connection setup failed!\n"); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1059 goto err_exit; |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1060 } |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1061 |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
1062 THMSG(1, "Connected, logging in as '%s', site '%s'.\n", optUserName, optSite); |
0 | 1063 optUserName2 = encodeStr1(optUserName); |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
1064 tmpStr = encodeStr1(optSite); |
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
1065 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20%s%%20%%2Dpassword%%20%s", tmpStr, optPassword); |
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
1066 th_free(tmpStr); |
0 | 1067 |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
1068 /* Initialize NCurses */ |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1069 if (!optDaemon) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1070 initscr(); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1071 raw(); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1072 keypad(stdscr, TRUE); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1073 noecho(); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1074 timeout(SET_DELAY); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1075 curVis = curs_set(0); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1076 if (has_colors()) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1077 start_color(); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1078 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1079 init_pair(1, COLOR_RED, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1080 init_pair(2, COLOR_GREEN, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1081 init_pair(3, COLOR_YELLOW, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1082 init_pair(4, COLOR_BLUE, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1083 init_pair(5, COLOR_MAGENTA, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1084 init_pair(6, COLOR_CYAN, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1085 init_pair(7, COLOR_WHITE, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1086 init_pair(8, COLOR_BLACK, COLOR_BLACK); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1087 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1088 init_pair(10, COLOR_BLACK, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1089 init_pair(11, COLOR_WHITE, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1090 init_pair(12, COLOR_GREEN, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1091 init_pair(13, COLOR_YELLOW, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1092 init_pair(14, COLOR_BLUE, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1093 init_pair(15, COLOR_MAGENTA, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1094 init_pair(16, COLOR_CYAN, COLOR_RED); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1095 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1096 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1097 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1098 mainWin = newwin(LINES - 4, COLS, 0, 0); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1099 statusWin = newwin(1, COLS, LINES - 4, 0); |
35
c725b0dc6268
Fix the editline being 2 lines instead of 3.
Matti Hamalainen <ccr@tnsp.org>
parents:
34
diff
changeset
|
1100 editWin = newwin(3, COLS, LINES - 3, 0); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1101 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1102 if (mainWin == NULL || statusWin == NULL || editWin == NULL) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1103 THERR("Could not create ncurses windows!\n"); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1104 goto err_exit; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1105 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1106 scrollok(mainWin, 1); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1107 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1108 clearBuf(editBuf); |
30 | 1109 printEditBuf("", editBuf); |
1110 updateStatus(insertMode); | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1111 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1112 cursesInit = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1113 } |
30 | 1114 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1115 |
30 | 1116 /* Enter mainloop */ |
0 | 1117 FD_ZERO(&sockfds); |
1118 FD_SET(tmpSocket, &sockfds); | |
1119 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1120 while (!isError && !exitProg) { |
0 | 1121 int result; |
1122 fd_set tmpfds; | |
1123 | |
1124 /* Check for incoming data from the server */ | |
1125 tv.tv_sec = 0; | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1126 tv.tv_usec = SET_DELAY_USEC; |
0 | 1127 tmpfds = sockfds; |
1128 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) { | |
27 | 1129 printMsg("Error occured in select(sockfds): %s\n", |
1130 strerror(errno)); | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1131 isError = TRUE; |
0 | 1132 } else if (FD_ISSET(tmpSocket, &tmpfds)) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1133 ssize_t gotBuf; |
30 | 1134 char tmpBuf[8192]; |
0 | 1135 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); |
1136 | |
1137 if (gotBuf < 0) { | |
9 | 1138 printMsg("Error in recv: %s\n", strerror(errno)); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1139 isError = TRUE; |
0 | 1140 } else if (gotBuf == 0) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1141 printMsg("Server closed connection.\n"); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1142 isError = TRUE; |
0 | 1143 } else { |
1144 /* Handle protocol data */ | |
1145 tmpBuf[gotBuf] = 0; | |
1146 result = handleProtocol(tmpSocket, tmpBuf, gotBuf); | |
1147 | |
1148 if (result > 0) { | |
1149 /* Couldn't handle the message for some reason */ | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1150 printMsg("Could not handle: %s\n", tmpBuf); |
0 | 1151 } else if (result < 0) { |
1152 /* Fatal error, quit */ | |
9 | 1153 printMsg("Fatal error with message: %s\n", tmpBuf); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1154 isError = TRUE; |
0 | 1155 } |
30 | 1156 updateStatus(insertMode); |
0 | 1157 } |
1158 } | |
1159 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1160 /* Handle user input */ |
21 | 1161 if (!optDaemon) { |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1162 int c, cnt = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1163 BOOL update = FALSE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1164 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1165 /* Handle several buffered keypresses at once */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1166 do { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1167 c = getch(); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1168 switch (c) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1169 case KEY_ENTER: |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1170 case '\n': |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1171 case '\r': |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1172 /* Call the user input handler */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1173 if (editBuf->len > 0) { |
30 | 1174 |
1175 if (histMax > 0) { | |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
1176 freeBuf(histBuf[SET_MAX_HISTORY+1]); |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
1177 histBuf[SET_MAX_HISTORY+1] = NULL; |
30 | 1178 memmove(&histBuf[2], &histBuf[1], histMax * sizeof(histBuf[0])); |
1179 } | |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
1180 |
30 | 1181 histPos = 0; |
1182 histBuf[1] = copyBuf(editBuf); | |
1183 if (histMax < SET_MAX_HISTORY) histMax++; | |
1184 | |
34
f5d335771b16
Fix a bug with mixed editBuf->pos vs editBuf->len.
Matti Hamalainen <ccr@tnsp.org>
parents:
33
diff
changeset
|
1185 insertBuf(editBuf, editBuf->len, 0); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1186 result = handleUserInput(tmpSocket, editBuf->data, editBuf->len); |
30 | 1187 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1188 clearBuf(editBuf); |
30 | 1189 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1190 if (result < 0) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1191 printMsg("Fatal error handling user input: %s\n", editBuf->data); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1192 isError = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1193 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1194 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1195 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1196 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1197 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1198 |
30 | 1199 case 0x09: /* Tab = switch between PRV */ |
1200 if (setPrvMode) | |
1201 setPrvMode = FALSE; | |
1202 else { | |
1203 if (setTarget != NULL) | |
1204 setPrvMode = TRUE; | |
1205 } | |
1206 update = TRUE; | |
1207 break; | |
1208 | |
1209 case KEY_UP: | |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
1210 if (histPos == 0) { |
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
1211 freeBuf(histBuf[0]); |
30 | 1212 histBuf[0] = copyBuf(editBuf); |
31
9b429b283786
Add command history functionality.
Matti Hamalainen <ccr@tnsp.org>
parents:
30
diff
changeset
|
1213 } |
30 | 1214 if (histPos < histMax) { |
1215 histPos++; | |
1216 freeBuf(editBuf); | |
1217 editBuf = copyBuf(histBuf[histPos]); | |
1218 update = TRUE; | |
1219 } | |
1220 break; | |
1221 | |
1222 case KEY_DOWN: | |
1223 if (histPos > 0) { | |
1224 histPos--; | |
1225 freeBuf(editBuf); | |
1226 editBuf = copyBuf(histBuf[histPos]); | |
1227 update = TRUE; | |
1228 } | |
1229 break; | |
1230 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1231 case 0x109: /* F1 */ |
30 | 1232 if (insertMode) |
1233 insertMode = FALSE; | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1234 else |
30 | 1235 insertMode = TRUE; |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1236 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1237 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1238 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1239 case 0x204: /* ctrl+left */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1240 editBuf->pos--; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1241 while (editBuf->pos > 0 && !isspace(editBuf->data[editBuf->pos])) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1242 editBuf->pos--; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1243 if (editBuf->pos < 0) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1244 editBuf->pos = 0; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1245 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1246 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1247 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1248 case 0x206: /* ctrl+right */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1249 editBuf->pos++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1250 while (editBuf->pos < editBuf->len && !isspace(editBuf->data[editBuf->pos])) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1251 editBuf->pos++; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1252 if (editBuf->pos > editBuf->len) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1253 editBuf->pos = editBuf->len; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1254 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1255 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1256 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1257 case 0x111: /* F9 */ |
33
e49ea6febfeb
Cleanups. Added option to specify/fake "site" where logging in from.
Matti Hamalainen <ccr@tnsp.org>
parents:
32
diff
changeset
|
1258 printMsg("Quitting per user request.\n"); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1259 exitProg = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1260 break; |
0 | 1261 |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1262 case 0x10a: /* F2 */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1263 clearBuf(editBuf); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1264 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1265 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1266 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1267 case KEY_HOME: setBufPos(editBuf, 0); update = TRUE; break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1268 case KEY_END: setBufPos(editBuf, editBuf->len); update = TRUE; break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1269 case KEY_LEFT: setBufPos(editBuf, editBuf->pos - 1); update = TRUE; break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1270 case KEY_RIGHT: setBufPos(editBuf, editBuf->pos + 1); update = TRUE; break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1271 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1272 case KEY_BACKSPACE: |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1273 deleteBuf(editBuf, editBuf->pos - 1); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1274 setBufPos(editBuf, editBuf->pos - 1); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1275 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1276 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1277 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1278 case 0x14a: |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1279 /* Delete */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1280 deleteBuf(editBuf, editBuf->pos); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1281 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1282 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1283 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1284 case 0x0c: |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1285 /* ctrl+l */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1286 redrawwin(mainWin); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1287 redrawwin(statusWin); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1288 redrawwin(editWin); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1289 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1290 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1291 case ERR: |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1292 /* Ignore */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1293 break; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1294 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1295 default: |
36 | 1296 if (isprint(c) || c == 0xe4 || c == 0xf6 || c == 0xc4 || c == 0xd6) { |
30 | 1297 if (insertMode) |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1298 insertBuf(editBuf, editBuf->pos, c); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1299 else |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1300 writeBuf(editBuf, editBuf->pos, c); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1301 setBufPos(editBuf, editBuf->pos + 1); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1302 update = TRUE; |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1303 } else { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1304 printMsg("Unhandled key: %02x\n", c); |
0 | 1305 } |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1306 break; |
0 | 1307 } |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1308 } while (c != ERR && !exitProg && ++cnt < 10); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1309 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1310 if (update) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1311 /* Update edit line */ |
30 | 1312 printEditBuf(setPrvMode ? setTarget : "", editBuf); |
1313 updateStatus(insertMode); | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1314 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1315 } /* !optDaemon */ |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1316 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1317 if (++updateCount > 10) { |
30 | 1318 updateStatus(insertMode); |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1319 updateCount = 0; |
0 | 1320 } |
1321 | |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1322 if (!colorSet) { |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1323 colorSet = TRUE; |
30 | 1324 printMsg("%s v%s - %s\n", th_prog_name, th_prog_version, th_prog_fullname); |
1325 printMsg("%s\n", th_prog_author); | |
1326 printMsg("%s\n", th_prog_license); | |
27 | 1327 sendUserMsg(tmpSocket, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor); |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1328 } |
0 | 1329 } |
1330 | |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1331 /* 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
|
1332 err_exit: |
28
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1333 if (cursesInit) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1334 if (curVis != ERR) |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1335 curs_set(curVis); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1336 endwin(); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1337 THMSG(1, "NCurses deinitialized.\n"); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1338 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1339 |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1340 if (isError) { |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1341 THMSG(1, "Error exit.\n"); |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1342 } |
512775f6b081
A refactored ncurses-based UI.
Matti Hamalainen <ccr@tnsp.org>
parents:
27
diff
changeset
|
1343 |
0 | 1344 th_free(optUserName2); |
10 | 1345 |
13
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1346 closeConnection(tmpSocket); |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1347 |
86fe5f0d1a85
Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents:
12
diff
changeset
|
1348 THMSG(1, "Connection terminated.\n"); |
0 | 1349 |
1350 if (optLogFile) { | |
1351 THMSG(1, "Closing logfile.\n"); | |
1352 fclose(optLogFile); | |
1353 } | |
1354 | |
1355 return 0; | |
1356 } |