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