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