annotate nnchat.c @ 30:751cff550992

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