annotate nnchat.c @ 21:29098addfa65

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