annotate nnchat.c @ 27:da721f94c60f

Use ANSI colours.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 02 Aug 2008 04:04:58 +0300
parents b84fc46c6035
children 512775f6b081
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 #ifdef __WIN32
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 #include <winsock2.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 #else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 #include <sys/socket.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 #include <sys/types.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 #include <arpa/inet.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 #include <sys/time.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
8 #include <netdb.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 #endif
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #include <unistd.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 #include <stdlib.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #include <stdio.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 #include "th_args.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include "th_string.h"
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 #include <string.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
17 #include <errno.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18 #include <time.h>
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21 #define SET_ALLOC_SIZE (128)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
22 #define SET_SELECT_USEC (100000)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
24 #define ANSI_BLACK "\x1b[0;30m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
25 #define ANSI_RED "\x1b[0;31m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
26 #define ANSI_GREEN "\x1b[0;32m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
27 #define ANSI_YELLOW "\x1b[0;33m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
28 #define ANSI_BLUE "\x1b[0;34m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
29 #define ANSI_MAGENTA "\x1b[0;35m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
30 #define ANSI_CYAN "\x1b[0;36m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
31 #define ANSI_WHITE "\x1b[0;37m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
32
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
33 #define ANSI_L_BLACK "\x1b[0;1;30m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
34 #define ANSI_L_RED "\x1b[0;1;31m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
35 #define ANSI_L_GREEN "\x1b[0;1;32m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
36 #define ANSI_L_YELLOW "\x1b[0;1;33m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
37 #define ANSI_L_BLUE "\x1b[0;1;34m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
38 #define ANSI_L_MAGENTA "\x1b[0;1;35m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
39 #define ANSI_L_CYAN "\x1b[0;1;36m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
40 #define ANSI_L_WHITE "\x1b[0;1;37m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
41
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
42 #define ANSI_END "\x1b[0m"
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
43
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
44
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
45 /* Options
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
46 */
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
47 int optPort = 8005;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
48 int optUserColor = 0x408060;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
49 char *optServer = "www11.servemedata.com",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
50 *optUserName = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
51 *optUserName2 = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
52 *optPassword = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
53 *optLogFilename = NULL,
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
54 *setTarget = NULL;
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
55 BOOL optDaemon = FALSE;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 FILE *optLogFile = NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 /* Arguments
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61 optarg_t optList[] = {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
62 { 0, '?', "help", "Show this help", OPT_NONE },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
63 { 1, 'v', "verbose", "Be more verbose", OPT_NONE },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
64 { 2, 'p', "port", "Connect to port", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
65 { 3, 's', "server", "Server to connect to", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
66 { 4, 'C', "color", "Initial color in RGB hex 000000", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
67 { 5, 'l', "logfile", "Log filename", OPT_ARGREQ },
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
68 { 6, 'D', "daemon", "A pseudo-daemon mode for logging", OPT_NONE },
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 const int optListN = (sizeof(optList) / sizeof(optarg_t));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 void argShowHelp()
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76 th_args_help(stdout, optList, optListN, th_prog_name,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 "[options] <username> <password>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 #ifdef __WIN32
1
351e96e01f4c Fix a warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
82 /* Just a bogus stub
351e96e01f4c Fix a warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
83 */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 const char *hstrerror(int err)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 {
1
351e96e01f4c Fix a warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
86 (void) err;
351e96e01f4c Fix a warning.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
87
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88 return "???";
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 #endif
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
92
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
93 int getColor(char *str)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
94 {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
95 char *p = str;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
96 int len, val = 0;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
97
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
98 for (len = 0; *p && len < 6; p++, len++) {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
99 if (*p >= '0' && *p <= '9') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
100 val *= 16; val += (*p - '0');
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
101 } else if (*p >= 'A' && *p <= 'F') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
102 val *= 16; val += (*p - 'A') + 10;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
103 } else if (*p >= 'a' && *p <= 'f') {
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
104 val *= 16; val += (*p - 'a') + 10;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
105 } else
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
106 return -1;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
107 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
108
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
109 return (len == 6) ? val : -1;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
110 }
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
111
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 switch (optN) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 case 0:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 argShowHelp();
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 exit(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 case 1:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121 th_verbosityLevel++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124 case 2:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125 optPort = atoi(optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 case 3:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 optServer = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 case 4:
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
133 if ((optUserColor = getColor(optArg)) < 0) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 optArg);
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 THMSG(1, "Using color #%06x\n", optUserColor);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141 case 5:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 optLogFilename = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
145 case 6:
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
146 optDaemon = TRUE;
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
147 THMSG(1, "Running in pseudo-daemon mode.\n");
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
148 break;
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
149
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 THERR("Unknown option '%s'.\n", currArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 BOOL argHandleFile(char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161 if (!optUserName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 optUserName = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 else if (!optPassword)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 optPassword = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 THERR("Username '%s' already specified on commandline!\n", optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
168 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
169
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
170 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
174 int openConnection(struct in_addr *addr, int port)
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
175 {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
176 struct sockaddr_in tmpAddr;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
177 int sock = -1;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
178
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
179 tmpAddr.sin_family = AF_INET;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
180 tmpAddr.sin_port = htons(port);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
181 tmpAddr.sin_addr = *addr;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
182
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
183 THMSG(1, "Connecting to %s:%d ...\n",
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
184 inet_ntoa(tmpAddr.sin_addr), port);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
185
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
186 if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
187 THERR("Could not open socket: %s\n", strerror(errno));
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
188 return -2;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
189 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
190
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
191 THMSG(2, "Using socket %d.\n", sock);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
192
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
193 if (connect(sock, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
194 THERR("Could not connect: %s\n", strerror(errno));
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
195 return -5;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
196 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
197
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
198 return sock;
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
199 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
200
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
201
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
202 void closeConnection(int sock)
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
203 {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
204 if (sock >= 0) {
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
205 #ifdef __WIN32
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
206 closesocket(sock);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
207 #else
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
208 close(sock);
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
209 #endif
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
210 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
211 }
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
212
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
213
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 BOOL sendToSocket(int sock, char *buf, const size_t bufLen)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 size_t bufLeft = bufLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 char *bufPtr = buf;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219 while (bufLeft > 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 ssize_t bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 bufSent = send(sock, bufPtr, bufLeft, 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 if (bufSent < 0) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 bufLeft -= bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 bufPtr += bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
230 void printMsg(char *fmt, char *fmt2, ...)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 char tmpStr[64] = "";
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234 time_t timeStamp;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 struct tm *tmpTime;;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237 timeStamp = time(NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 if ((tmpTime = localtime(&timeStamp)) != NULL) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
239 strftime(tmpStr, sizeof(tmpStr), "%H:%M:%S", tmpTime);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 if (optLogFile) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
243 fprintf(optLogFile, "[%s] ", tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
244 va_start(ap, fmt2);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 vfprintf(optLogFile, fmt, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 fflush(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
250 if (!optDaemon) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
251 fprintf(stdout, ANSI_L_BLACK "[" ANSI_L_GREEN "%s" ANSI_L_BLACK "]" ANSI_END " ", tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
252 va_start(ap, fmt2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
253 vfprintf(stdout, fmt2, ap);
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
254 va_end(ap);
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
255 fflush(stdout);
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
256 }
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 BOOL bufRealloc(char **buf, size_t *size, size_t add)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 return ((*buf = th_realloc(*buf, *size + add)) != NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
265 #define PUSHCHAR(x) bufPushChar(&result, &resSize, &resPos, x)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 (*buf)[*pos] = ch;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272 (*pos)++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
276 #define PUSHSTR(x) bufPushStr(&result, &resSize, &resPos, x)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 size_t tmpLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 if (!str) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 tmpLen = strlen(str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 strcpy(*buf + *pos, str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 (*pos) += tmpLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 char *encodeStr1(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 switch (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 case 32:
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
307 PUSHCHAR('+');
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 if (th_isalnum(*s))
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
312 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 char tmpStr[4];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 sprintf(tmpStr, "%2X", (unsigned char) *s);
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
316 PUSHCHAR('%');
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
317 PUSHSTR(tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
323 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
328
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 int getxdigit(int c, int shift)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 if (c >= 'A' && c <= 'F')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 i = c - 'A' + 10;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 else if (c >= 'a' && c <= 'f')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 i = c - 'a' + 10;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 else if (c >= '0' && c <= '9')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 i = c - '0';
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 return i << shift;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
345
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346 char *decodeStr1(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 int c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 switch (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 case '+':
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
361 PUSHCHAR(' ');
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
365 case '\r':
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
366 PUSHCHAR(' ');
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
367 s++;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
368 break;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
369
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 case '%':
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 if (*s == '%')
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
373 PUSHCHAR('%');
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
374 else if ((c = getxdigit(*s, 4)) >= 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
375 int i = getxdigit(*(++s), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
376 if (i >= 0) {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
377 PUSHCHAR(c | i);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
378 } else {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
379 PUSHCHAR('§');
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
380 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382 } else {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
383 PUSHCHAR('§');
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
384 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 default:
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
390 PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
394 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 char *stripTags(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412 if (*s == '<') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 while (*s && *s != '>') s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 if (*s == '>') s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 } else
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
416 PUSHCHAR(*s++);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
418 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424 typedef struct {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 char c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 char *ent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 } html_entity_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 html_entity_t HTMLEntities[] = {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431 { '<', "&lt;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432 { '>', "&gt;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 char *encodeStr2(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451 BOOL found = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 for (i = 0; i < numHTMLEntities; i++)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 if (HTMLEntities[i].c == *s) {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
454 PUSHSTR(HTMLEntities[i].ent);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 found = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
458 if (!found) PUSHCHAR(*s);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
462 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 char *decodeStr2(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
474
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
475 resSize = strlen(str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
476 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 if (*s == '&') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 BOOL found = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 for (i = 0; i < numHTMLEntities; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 html_entity_t *ent = &HTMLEntities[i];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 int len = strlen(ent->ent);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 if (!strncmp(s, ent->ent, len)) {
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
487 PUSHCHAR(ent->c);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488 s += len;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489 found = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
493 if (!found) PUSHCHAR(*s++);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 } else
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
495 PUSHCHAR(*s++);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 }
12
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
497 PUSHCHAR(0);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
500 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
501
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
502
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
503 BOOL sendUserMsg(int sock, char *user, char *fmt, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 char tmpBuf[4096], tmpBuf2[4096+256];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506 int n;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 va_start(ap, fmt);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 if (n < 0) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515 snprintf(tmpBuf2, sizeof(tmpBuf2),
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 "<USER>%s</USER><MESSAGE>%s</MESSAGE>",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 user, tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
521
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
522
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
523 int handleUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 const char *msg = "</USER><MESSAGE>";
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
526 char *p = str, *q, *s, *t, *h;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 s = strstr(str, msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 *s = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 s += strlen(msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 q = strstr(s, "</MESSAGE>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 if (!q) return 3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537 *q = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539 s = decodeStr1(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540 if (!s) return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 p = decodeStr1(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 if (!p) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 if (*s == '/') {
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
550 if (!strncmp(s, "/BPRV", 5)) {
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
551 t = stripTags(s + 2);
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
552 h = decodeStr2(t);
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
553 printMsg("%s\n", ANSI_YELLOW "%s" ANSI_END "\n", h);
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
554 } else {
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
555 t = stripTags(s + 1);
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
556 h = decodeStr2(t);
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
557 printMsg("* %s\n", ANSI_L_YELLOW "* %s" ANSI_END "\n", h);
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
558 }
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
559 th_free(h);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 } else {
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
562 t = stripTags(s);
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
563 h = decodeStr2(t);
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
564 printMsg("<%s> %s\n", ANSI_MAGENTA "<" ANSI_L_CYAN "%s" ANSI_MAGENTA ">" ANSI_END " %s\n", p, h);
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
565 th_free(h);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 th_free(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 int handleLogin(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576 {
19
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
577 char tmpStr[256] = "";
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
578 time_t timeStamp;
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
579 struct tm *tmpTime;;
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
580
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
581 timeStamp = time(NULL);
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
582 if ((tmpTime = localtime(&timeStamp)) != NULL) {
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
583 strftime(tmpStr, sizeof(tmpStr), "%c", tmpTime);
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
584 }
e80072e26178 Add login timestamps.
Matti Hamalainen <ccr@tnsp.org>
parents: 15
diff changeset
585
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 if (!strncmp(str, "FAILURE", 7)) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
587 printMsg("Login failure - %s\n", "Login failure - %s\n", tmpStr);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 } else if (!strncmp(str, "SUCCESS", 7)) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
590 printMsg("Login success - %s\n", "Login success - %s\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
591 sendUserMsg(sock, optUserName2, "%%2FRequestUserList");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 } else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
597
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
598 int handleAddUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
599 {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
600 char *p, *s = strstr(str, "</ADD_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 *s = 0;
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
606
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
607 p = decodeStr1(str);
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
608 if (!p) return -1;
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
609
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
610 printMsg("! %s ADDED.\n", "! " ANSI_GREEN "%s" ANSI_END " ADDED.\n", p);
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
611 th_free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616 int handleDeleteUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
618 char *p, *s = strstr(str, "</DELETE_USER>");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 *s = 0;
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
624
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
625 p = decodeStr1(str);
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
626 if (!p) return -1;
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
627
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
628 printMsg("! %s DELETED.\n", "! " ANSI_RED "%s" ANSI_END " DELETED.\n", p);
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
629 th_free(p);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634 int handleFoo(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 (void) sock; (void) str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
639 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
640
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
641
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 typedef struct {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 char *cmd;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644 int (*handler)(int, char *);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 } protocmd_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648 protocmd_t protoCmds[] = {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 { "<USER>", handleUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650 { "<LOGIN_", handleLogin },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651 { "<DELETE_USER>", handleDeleteUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652 { "<ADD_USER>", handleAddUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
653 { "<NUMCLIENTS>", handleFoo },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
654 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protocmd_t));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 int handleProtocol(int sock, char *buf, size_t bufLen)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 for (i = 0; i < nprotoCmds; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664 size_t cmdLen = strlen(protoCmds[i].cmd);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 return protoCmds[i].handler(sock, buf + cmdLen);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
668 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
669
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
670 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
674 int handleUserInput(int sock, char *buf, size_t bufLen)
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 {
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
676 char *tmpStr, *tmpStr2, tmpBuf[4096];
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 BOOL result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679 /* Trim right */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 buf[--bufLen] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen])))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682 buf[bufLen--] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 //fprintf(stderr, "'%s'\n", buf); fflush(stderr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 /* Check command */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 if (*buf == 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 return 1;
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
689 } else if (!strncmp(buf, "/color ", 7)) {
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
690 if ((optUserColor = getColor(buf+7)) < 0) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
691 printMsg("Invalid color value '%s'\n", "Invalid color value '%s'\n", buf+7);
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
692 return 1;
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
693 }
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
694 printMsg("Setting color to #%06x\n", "Setting color to #%06x\n", optUserColor);
25
3b67a9a806a7 Added /color command to change colour.
Matti Hamalainen <ccr@tnsp.org>
parents: 24
diff changeset
695 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
696 return 0;
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
697 } else if (!strncmp(buf, "/fake ", 6)) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
698 printMsg("Sending /%s\n", "Sending /%s\n", buf+6);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
699 tmpStr = encodeStr2(tmpBuf);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
700 if (!tmpStr) return -2;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
701 tmpStr2 = encodeStr1(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
702 if (!tmpStr2) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
703 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
704 return -3;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
705 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
706 sendUserMsg(sock ,optUserName2, "%%2F%s", tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
707
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
708 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
709 th_free(tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
710 return 0;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
711 } else if (!strncmp(buf, "/flood ", 7)) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
712 int i;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
713
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
714 snprintf(tmpBuf, sizeof(tmpBuf), "/prv -to %s -msg . .",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
715 buf+7);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
716
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
717 tmpStr = encodeStr2(tmpBuf);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
718 if (!tmpStr) return -2;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
719 tmpStr2 = encodeStr1(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
720 if (!tmpStr2) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
721 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
722 return -3;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
723 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
724
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
725 result = TRUE;
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
726 for (i = 0; i < 50 && result; i++) {
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
727 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
728 usleep(250);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
729 }
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
730
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
731 th_free(tmpStr);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
732 th_free(tmpStr2);
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
733 return 0;
24
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
734 } else if (!strncmp(buf, "/msg ", 5)) {
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
735 if (setTarget) {
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
736 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
737 buf = tmpBuf;
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
738 } else {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
739 printMsg("No target set!\n", ANSI_L_RED "No target set!" ANSI_END "\n");
24
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
740 return 1;
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
741 }
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
742 } else if (!strncmp(buf, "/to ", 4)) {
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
743 buf += 4;
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
744 th_free(setTarget);
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
745 setTarget = th_strdup(buf);
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
746 printMsg("Set prv target to '%s'\n",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
747 "Set prv target to '" ANSI_L_GREEN "%s" ANSI_END "'\n", setTarget);
24
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
748 return 0;
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
749 }
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
750
78d260256450 Added two simple commands to simplify private chatting, "/to" and "/msg".
Matti Hamalainen <ccr@tnsp.org>
parents: 23
diff changeset
751 {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752 /* Send double-encoded */
22
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
753 //printf("ENC>%s\n", buf);
ccf302dae898 Add decoding of entities.
Matti Hamalainen <ccr@tnsp.org>
parents: 21
diff changeset
754 //fflush(stdout);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756 tmpStr = encodeStr2(buf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
757 if (!tmpStr) return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 tmpStr2 = encodeStr1(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 if (!tmpStr2) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760 th_free(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761 return -3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
763
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
765 th_free(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
766 th_free(tmpStr2);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
767 if (result)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
768 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
769 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
770 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
771 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
772 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
773
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
774
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
775 int main(int argc, char *argv[])
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
776 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
777 int tmpSocket;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
778 struct hostent *tmpHost;
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
779 BOOL argsOK, exitProg = FALSE, colorSet = 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
780 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
781 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
782 fd_set inputfds;
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
783 char *tmpStr;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
784
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
785 /* Initialize */
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
786 th_init("NNChat", "Newbie Nudes chat client", "0.4",
6
526ba3b578d7 Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
787 "Written and designed by Anonymous Finnish Guy (C) 2008",
526ba3b578d7 Changed copyright etc. again.
Matti Hamalainen <ccr@tnsp.org>
parents: 4
diff changeset
788 "This software is freeware, use and distribute as you wish.");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
789 th_verbosityLevel = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
790
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
791 /* Parse arguments */
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
792 argsOK = th_args_process(argc, argv, optList, optListN,
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
793 argHandleOpt, argHandleFile, FALSE);
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
794
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
795 /* Check the mode and arguments */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
796 if (optUserName == NULL || optPassword == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
797 THERR("User/pass not specified, get some --help\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
798 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
799 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
800
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
801 if (!argsOK)
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
802 return -2;
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
803
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
804 /* Open logfile */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
805 if (optLogFilename) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
806 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
807
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
808 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
809 THERR("Could not open logfile for appending!\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
810 return -9;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
811 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
812 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
813
10
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
814 #ifdef __WIN32
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
815 {
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
816 WSADATA wsaData;
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
817 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
818 THERR("WinSock API v2.0 not supported.\n");
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
819 return -20;
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
820 }
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
821 }
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
822 #endif
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
823
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
824 /* Okay ... */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
825 THMSG(1, "Trying to resolve host '%s' ...\n", optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
826 tmpHost = gethostbyname(optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
827 if (tmpHost == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
828 THERR("Could not resolve hostname: %s.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
829 hstrerror(h_errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
830 return -3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
831 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
832 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
833
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
834 /* 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
835 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
836 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
837 goto err_exit;
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
838 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
839
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
840 tmpStr = "<policy-file-request/>";
23
40fecbab1dc1 Check for error condition properly.
Matti Hamalainen <ccr@tnsp.org>
parents: 22
diff changeset
841 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
842 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
843 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
844 } else {
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
845 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
846 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
847 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
848 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
849 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
850 closeConnection(tmpSocket);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
851 }
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
852
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
853 /* 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
854 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
855 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
856 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
857 }
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
858
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
859
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
860 THMSG(1, "Connected, logging in as '%s'.\n", optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
861 optUserName2 = encodeStr1(optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
862
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
863 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20NN%%20%%2Dpassword%%20%s", optPassword);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
864
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
865 FD_ZERO(&inputfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
866 FD_SET(0, &inputfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
867 FD_ZERO(&sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
868 FD_SET(tmpSocket, &sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
869
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
870 while (!exitProg) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
871 ssize_t gotBuf;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
872 int result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
873 char tmpBuf[4096];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
874 fd_set tmpfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
875
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
876 /* Check for incoming data from the server */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
877 tv.tv_sec = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
878 tv.tv_usec = SET_SELECT_USEC;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
879 tmpfds = sockfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
880 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
881 printMsg("Error occured in select(sockfds): %s\n",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
882 "Error occured in select(sockfds): %s\n",
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
883 strerror(errno));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
884 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
885 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
886 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
887
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
888 if (gotBuf < 0) {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
889 printMsg("Error in recv: %s\n", strerror(errno));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
890 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
891 } else if (gotBuf == 0) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
892 printMsg("Server closed connection.\n", "Server closed connection.\n");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
893 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
894 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
895 /* Handle protocol data */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
896 tmpBuf[gotBuf] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
897 result = handleProtocol(tmpSocket, tmpBuf, gotBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
898
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
899 if (result > 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
900 /* Couldn't handle the message for some reason */
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
901 printMsg("Could not handle: %s\n", "Could not handle: %s\n", tmpBuf);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
902 } else if (result < 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
903 /* Fatal error, quit */
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
904 printMsg("Fatal error with message: %s\n", tmpBuf);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
905 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
906 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
907 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
908 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
909
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
910 /* Check for user input */
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
911 if (!optDaemon) {
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
912 tv.tv_sec = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
913 tv.tv_usec = SET_SELECT_USEC;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
914 tmpfds = inputfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
915 if ((result = select(1, &tmpfds, NULL, NULL, &tv)) == -1) {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
916 printMsg("Error occured in select(inputfds): %s\n", strerror(errno));
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
917 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
918 } else if (FD_ISSET(0, &tmpfds)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
919 gotBuf = read(0, tmpBuf, sizeof(tmpBuf));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
920
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
921 if (gotBuf < 0) {
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
922 printMsg("Error in reading stdio.\n", "Error in reading stdio.\n");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
923 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
924 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
925 /* Call the user input handler */
15
50d8396e7417 Move things around a bit.
Matti Hamalainen <ccr@tnsp.org>
parents: 13
diff changeset
926 result = handleUserInput(tmpSocket, tmpBuf, gotBuf);
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
927 if (result < 0) {
9
a02659cc5bc8 Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 6
diff changeset
928 printMsg("Fatal error handling user input: %s\n",
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
929 tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
930 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
931 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
932 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
933 }
21
29098addfa65 Added a "daemon" mode.
Matti Hamalainen <ccr@tnsp.org>
parents: 19
diff changeset
934 } /* !optDaemon */
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
935
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
936 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
937 colorSet = TRUE;
27
da721f94c60f Use ANSI colours.
Matti Hamalainen <ccr@tnsp.org>
parents: 26
diff changeset
938 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
939 }
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
940
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
941 fflush(stdout);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
942 fflush(stderr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
943 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
944
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
945 /* Shotdiwn */
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
946 err_exit:
26
b84fc46c6035 Improved color hex triplet parsing.
Matti Hamalainen <ccr@tnsp.org>
parents: 25
diff changeset
947 THMSG(1, "Error exit.\n");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
948 th_free(optUserName2);
10
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
949
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
950 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
951
10
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
952 #ifdef __WIN32
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
953 WSACleanup();
53e127854dca WinSock support fixes.
Matti Hamalainen <ccr@tnsp.org>
parents: 9
diff changeset
954 #endif
13
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
955
86fe5f0d1a85 Cleanups; Added probing connection (requesting some policy crap) to emulate the official client.
Matti Hamalainen <ccr@tnsp.org>
parents: 12
diff changeset
956 THMSG(1, "Connection terminated.\n");
0
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
957
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
958 if (optLogFile) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
959 THMSG(1, "Closing logfile.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
960 fclose(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
961 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
962
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
963
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
964 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
965 }