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