annotate nnchat.c @ 0:728243125263

Import.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 20 Mar 2008 00:15:03 +0000
parents
children 351e96e01f4c
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;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34
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 },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
47 /*
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 { 6, 'p', "plaintext", "Use plaintext logging", OPT_NONE },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 const int optListN = (sizeof(optList) / sizeof(optarg_t));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
55 void argShowHelp()
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
56 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
57 th_args_help(stdout, optList, optListN, th_prog_name,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
58 "[options] <username> <password>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
59 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
60
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
61
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
62 #ifdef __WIN32
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 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
65 return "???";
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
66 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
67 #endif
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
68
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69 BOOL argHandleOpt(const int optN, char *optArg, char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 switch (optN) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 case 0:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 argShowHelp();
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
74 exit(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
77 case 1:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 th_verbosityLevel++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
80
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
81 case 2:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 optPort = atoi(optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 case 3:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 optServer = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 case 4:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 if (sscanf(optArg, "%06x", &optUserColor) != 1) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 THERR("Invalid color argument '%s', should be a RGB hex triplet '000000'.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92 optArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 THMSG(1, "Using color #%06x\n", optUserColor);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 case 5:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 optLogFilename = optArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
100 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
101
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
102 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
103 THERR("Unknown option '%s'.\n", currArg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
104 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
105 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
106
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
107 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
110
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
111 BOOL argHandleFile(char *currArg)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
112 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
113 if (!optUserName)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
114 optUserName = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
115 else if (!optPassword)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
116 optPassword = currArg;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 THERR("Username '%s' already specified on commandline!\n", optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
121
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
122 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
123 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
124
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
125
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
126 BOOL sendToSocket(int sock, char *buf, const size_t bufLen)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
127 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
128 size_t bufLeft = bufLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
129 char *bufPtr = buf;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
130
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
131 while (bufLeft > 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
132 ssize_t bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
133 bufSent = send(sock, bufPtr, bufLeft, 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
134 if (bufSent < 0) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
135 bufLeft -= bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
136 bufPtr += bufSent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
137 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
138 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
139 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
140
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
141
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
142 void printMsg(char *fmt, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
143 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
144 char tmpStr[64] = "";
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
145 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
146 time_t timeStamp;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
147 struct tm *tmpTime;;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
148
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
149 timeStamp = time(NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
150 if ((tmpTime = localtime(&timeStamp)) != NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
151 strftime(tmpStr, sizeof(tmpStr), "[%H:%M:%S] ", tmpTime);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
152 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
153
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
154 if (optLogFile) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
155 fputs(tmpStr, optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
156 va_start(ap, fmt);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
157 vfprintf(optLogFile, fmt, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
158 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
159 fflush(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
160 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
161
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
162 fputs(tmpStr, stdout);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
163 va_start(ap, fmt);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
164 vfprintf(stdout, fmt, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
165 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
166 fflush(stdout);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
167 }
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 BOOL bufRealloc(char **buf, size_t *size, size_t add)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
171 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
172 return ((*buf = th_realloc(*buf, *size + add)) != NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
173 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
174
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
175 #define pushChar(x) bufPushChar(&result, &resSize, &resPos, x)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
176 BOOL bufPushChar(char **buf, size_t *size, size_t *pos, char ch)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
177 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
178 if (*pos >= *size && !bufRealloc(buf, size, SET_ALLOC_SIZE))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
179 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
180
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
181 (*buf)[*pos] = ch;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
182 (*pos)++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
183 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
184 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
185
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
186 #define pushStr(x) bufPushStr(&result, &resSize, &resPos, x)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
187 BOOL bufPushStr(char **buf, size_t *size, size_t *pos, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
188 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
189 size_t tmpLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
190
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
191 if (!str) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
192 tmpLen = strlen(str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
193
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
194 if ((*pos + tmpLen) >= *size && !bufRealloc(buf, size, tmpLen + SET_ALLOC_SIZE))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
195 return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
196
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
197 strcpy(*buf + *pos, str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
198 (*pos) += tmpLen;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
199 return TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
200 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
201
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
202
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
203 char *encodeStr1(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
204 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
205 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
206 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
207
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
208 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
209
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
210 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
211 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
212 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
213
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
214 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
215 switch (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
216 case 32:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
217 pushChar('+');
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
218 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
219
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
220 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
221 if (th_isalnum(*s))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
222 pushChar(*s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
223 else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
224 char tmpStr[4];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
225 sprintf(tmpStr, "%2X", (unsigned char) *s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
226 pushChar('%');
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
227 pushStr(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
228 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
229 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
230 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
231 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
232 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
233 pushChar(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
234
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
235 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
236 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
237
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
238 int getxdigit(int c, int shift)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
239 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
240 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
241
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
242 if (c >= 'A' && c <= 'F')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
243 i = c - 'A' + 10;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
244 else if (c >= 'a' && c <= 'f')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
245 i = c - 'a' + 10;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
246 else if (c >= '0' && c <= '9')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
247 i = c - '0';
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
248 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
249 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
250
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
251 return i << shift;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
252 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
253
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
254 char *decodeStr1(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
255 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
256 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
257 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
258 int c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
259
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
260 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
261
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
262 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
263 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
264 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
265
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
266 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
267 switch (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
268 case '+':
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
269 pushChar(' ');
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
270 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
271 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
272
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
273 case '%':
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
274 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
275 if (*s == '%')
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
276 pushChar('%');
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
277 else if ((c = getxdigit(*s, 4)) >= 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
278 int i = getxdigit(*(++s), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
279 if (i >= 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
280 pushChar(c | i);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
281 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
282 pushChar('§');
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
283 pushChar(*s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
284 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
285 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
286 pushChar('§');
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
287 pushChar(*s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
288 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
289 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
290 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
291
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
292 default:
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
293 pushChar(*s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
294 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
295 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
296 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
297 pushChar(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
298
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
299 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
300 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
301
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
302
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
303 char *stripTags(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
304 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
305 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
306 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
307
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
308 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
309
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
310 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
311 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
312 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
313
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
314 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
315 if (*s == '<') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
316 while (*s && *s != '>') s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
317 if (*s == '>') s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
318 } else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
319 pushChar(*s++);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
320 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
321 pushChar(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
322
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
323 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
324 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
325
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
326
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
327 typedef struct {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
328 char c;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
329 char *ent;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
330 } html_entity_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
331
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
332
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
333 html_entity_t HTMLEntities[] = {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
334 { '<', "&lt;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
335 { '>', "&gt;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
336 /*
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
337 { '&', "&amp;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
338 { 'ä', "&auml;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
339 { 'ö', "&ouml;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
340 { 'Ä', "&Auml;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
341 { 'Ö', "&Ouml;" },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
342 */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
343 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
344
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
345 const int numHTMLEntities = (sizeof(HTMLEntities) / sizeof(html_entity_t));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
346
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
347
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
348 char *encodeStr2(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
349 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
350 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
351 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
352
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
353 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
354
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
355 resSize = strlen(str) + SET_ALLOC_SIZE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
356 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
357 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
358
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
359 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
360 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
361 BOOL found = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
362 for (i = 0; i < numHTMLEntities; i++)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
363 if (HTMLEntities[i].c == *s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
364 pushStr(HTMLEntities[i].ent);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
365 found = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
366 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
367 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
368 if (!found) pushChar(*s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
369
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
370 s++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
371 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
372 pushChar(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 char *decodeStr2(char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
379 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
380 char *result, *s = str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
381 size_t resSize, resPos = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
382
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
383 if (!str) return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
384
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
385 resSize = strlen(str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
386 if ((result = th_malloc(resSize)) == NULL)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
387 return NULL;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
388
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
389 while (*s) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
390 if (*s == '&') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
391 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
392 BOOL found = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
393 for (i = 0; i < numHTMLEntities; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
394 html_entity_t *ent = &HTMLEntities[i];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
395 int len = strlen(ent->ent);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
396 if (!strncmp(s, ent->ent, len)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
397 pushChar(ent->c);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
398 s += len;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
399 found = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
400 break;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
401 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
402 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
403 if (!found) pushChar(*s++);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
404 } else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
405 pushChar(*s++);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
406 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
407 pushChar(0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
408
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
409 return result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
410 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
411
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
412
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
413 BOOL sendUserMsg(int sock, char *user, char *fmt, ...)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
414 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
415 char tmpBuf[4096], tmpBuf2[4096+256];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
416 int n;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
417 va_list ap;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
418
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
419 va_start(ap, fmt);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
420 n = vsnprintf(tmpBuf, sizeof(tmpBuf), fmt, ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
421 va_end(ap);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
422
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
423 if (n < 0) return FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
424
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
425 snprintf(tmpBuf2, sizeof(tmpBuf2),
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
426 "<USER>%s</USER><MESSAGE>%s</MESSAGE>",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
427 user, tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
428
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
429 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
430 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
431
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
432
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
433 int handleUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
434 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
435 const char *msg = "</USER><MESSAGE>";
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
436 char *p = str, *q, *s;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
437
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
438 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
439
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
440 s = strstr(str, msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
441 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
442 *s = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
443 s += strlen(msg);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
444
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
445 q = strstr(s, "</MESSAGE>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
446 if (!q) return 3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
447 *q = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
448
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
449 s = decodeStr1(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
450 if (!s) return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
451
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
452 p = decodeStr1(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
453 if (!p) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
454 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
455 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
456 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
457
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
458 /* FIXME: decodeStr2() */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
459
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
460 if (*s == '/') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
461 char *t = stripTags(s+1);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
462 printMsg("* %s\n", t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
463 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
464 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
465 char *t = stripTags(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
466 printMsg("<%s> %s\n", p, t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
467 th_free(t);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
468 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
469
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
470 th_free(s);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
471 th_free(p);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
472 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
473 }
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 int handleLogin(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
477 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
478 if (!strncmp(str, "FAILURE", 7)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
479 printMsg("Login failure.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
480 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
481 } else if (!strncmp(str, "SUCCESS", 7)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
482 printMsg("Login success.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
483 sendUserMsg(sock, optUserName2, "%%2FSetFontColor%%20%%2Dcolor%%20%06X", optUserColor);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
484 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
485 } else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
486 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
487 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
488
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
489
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
490 int handleAddUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
491 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
492 char *s = strstr(str, "</ADD_USER>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
493
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
494 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
495
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
496 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
497 *s = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
498 printMsg("! %s ADDED.\n", str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
499 return 0;
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 int handleDeleteUser(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
504 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
505 char *s = strstr(str, "</DELETE_USER>");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
506
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
507 (void) sock;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
508
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
509 if (!s) return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
510 *s = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
511 printMsg("! %s DELETED.\n", str);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
512 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
513 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
514
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
515
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
516 int handleFoo(int sock, char *str)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
517 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
518 (void) sock; (void) str;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
519
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
520 return 0;
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
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
524 typedef struct {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
525 char *cmd;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
526 int (*handler)(int, char *);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
527 } protocmd_t;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
528
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
529
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
530 protocmd_t protoCmds[] = {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
531 { "<USER>", handleUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
532 { "<LOGIN_", handleLogin },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
533 { "<DELETE_USER>", handleDeleteUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
534 { "<ADD_USER>", handleAddUser },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
535 { "<NUMCLIENTS>", handleFoo },
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
536 };
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
537
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
538 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protocmd_t));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
539
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
540
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
541 int handleProtocol(int sock, char *buf, size_t bufLen)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
542 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
543 int i;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
544
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
545 for (i = 0; i < nprotoCmds; i++) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
546 size_t cmdLen = strlen(protoCmds[i].cmd);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
547 if (cmdLen < bufLen && !strncmp(buf, protoCmds[i].cmd, cmdLen)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
548 return protoCmds[i].handler(sock, buf + cmdLen);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
549 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
550 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
551
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
552 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
553 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
554
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
555
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
556 int handleInput(int sock, char *buf, size_t bufLen)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
557 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
558 char *tmpStr, *tmpStr2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
559 BOOL result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
560
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
561 /* Trim right */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
562 buf[--bufLen] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
563 while (bufLen > 0 && (buf[bufLen] == '\n' || buf[bufLen] == '\r' || th_isspace(buf[bufLen])))
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
564 buf[bufLen--] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
565
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
566 //fprintf(stderr, "'%s'\n", buf); fflush(stderr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
567
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
568 /* Check command */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
569 if (*buf == 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
570 return 1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
571 } else if (*buf == '@') {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
572 /* Send 1-pass encoded 'RAW' */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
573 buf++;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
574 printf("RAW>%s\n", buf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
575 fflush(stdout);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
576
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
577 tmpStr = encodeStr1(buf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
578 if (!tmpStr) return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
579
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
580 result = sendUserMsg(sock, optUserName2, "%s", tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
581 th_free(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
582 if (result)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
583 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
584 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
585 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
586 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
587 /* Send double-encoded */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
588 printf("ENC>%s\n", buf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
589 fflush(stdout);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
590
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
591 tmpStr = encodeStr2(buf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
592 if (!tmpStr) return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
593 tmpStr2 = encodeStr1(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
594 if (!tmpStr2) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
595 th_free(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
596 return -3;
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 result = sendUserMsg(sock, optUserName2, "%s", tmpStr2);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
600 th_free(tmpStr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
601 th_free(tmpStr2);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
602 if (result)
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
603 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
604 else
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
605 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
606 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
607 }
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 main(int argc, char *argv[])
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
611 {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
612 int tmpSocket;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
613 struct hostent *tmpHost;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
614 struct sockaddr_in tmpAddr;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
615 BOOL exitProg = FALSE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
616
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
617 /* Initialize */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
618 th_init("NNChat", "Newbie Nudes chat client", "0.2", NULL, NULL);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
619 th_verbosityLevel = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
620
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
621 /* Parse arguments */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
622 th_args_process(argc, argv, optList, optListN,
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
623 argHandleOpt, argHandleFile, FALSE);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
624
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
625 /* Check the mode and arguments */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
626 if (optUserName == NULL || optPassword == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
627 THERR("User/pass not specified, get some --help\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
628 return -1;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
629 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
630
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
631 /* Open logfile */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
632 if (optLogFilename) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
633 THMSG(1, "Opening logfile '%s'\n", optLogFilename);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
634
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
635 if ((optLogFile = fopen(optLogFilename, "a")) == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
636 THERR("Could not open logfile for appending!\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
637 return -9;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
638 }
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 /* Okay ... */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
642 THMSG(1, "Trying to resolve host '%s' ...\n", optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
643 tmpHost = gethostbyname(optServer);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
644 if (tmpHost == NULL) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
645 THERR("Could not resolve hostname: %s.\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
646 hstrerror(h_errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
647 return -3;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
648 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
649 THMSG(2, "True hostname: %s\n", tmpHost->h_name);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
650
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
651 tmpAddr.sin_family = AF_INET;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
652 tmpAddr.sin_port = htons(optPort);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
653 tmpAddr.sin_addr = *((struct in_addr *) tmpHost->h_addr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
654
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
655 THMSG(1, "Connecting to %s:%d ...\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
656 inet_ntoa(tmpAddr.sin_addr), optPort);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
657
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
658 if ((tmpSocket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
659 THERR("Could not open socket: %s\n", strerror(errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
660 return -2;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
661 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
662
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
663 THMSG(2, "Using socket %d.\n", tmpSocket);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
664
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
665 if (connect(tmpSocket, (struct sockaddr *) &tmpAddr, sizeof(tmpAddr)) == -1) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
666 THERR("Could not connect: %s\n", strerror(errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
667 return -5;
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 THMSG(1, "Connected, logging in as '%s'.\n", optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
671 optUserName2 = encodeStr1(optUserName);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
672
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
673 sendUserMsg(tmpSocket, optUserName2, "%%2Flogin%%20%%2Dsite%%20NN%%20%%2Dpassword%%20%s", optPassword);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
674
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
675 struct timeval tv;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
676 fd_set sockfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
677 fd_set inputfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
678
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
679
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
680 FD_ZERO(&inputfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
681 FD_SET(0, &inputfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
682
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
683 FD_ZERO(&sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
684 FD_SET(tmpSocket, &sockfds);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
685
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
686 while (!exitProg) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
687 ssize_t gotBuf;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
688 int result;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
689 char tmpBuf[4096];
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
690 fd_set tmpfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
691
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
692 /* Check for incoming data from the server */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
693 tv.tv_sec = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
694 tv.tv_usec = SET_SELECT_USEC;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
695 tmpfds = sockfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
696 if ((result = select(tmpSocket+1, &tmpfds, NULL, NULL, &tv)) == -1) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
697 THERR("Error occured in select(sockfds): %s\n", strerror(errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
698 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
699 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
700 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
701
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
702 if (gotBuf < 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
703 THERR("Error in recv: %s\n", strerror(errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
704 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
705 } else if (gotBuf == 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
706 THERR("Server closed connection.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
707 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
708 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
709 /* Handle protocol data */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
710 tmpBuf[gotBuf] = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
711 result = handleProtocol(tmpSocket, tmpBuf, gotBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
712
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
713 if (result > 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
714 /* Couldn't handle the message for some reason */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
715 THERR("Could not handle: %s\n", tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
716 } else if (result < 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
717 /* Fatal error, quit */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
718 THERR("Fatal error with message: %s\n", tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
719 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
720 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
721 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
722 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
723
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
724 /* Check for user input */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
725 tv.tv_sec = 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
726 tv.tv_usec = SET_SELECT_USEC;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
727 tmpfds = inputfds;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
728 if ((result = select(1, &tmpfds, NULL, NULL, &tv)) == -1) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
729 THERR("Error occured in select(inputfds): %s\n", strerror(errno));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
730 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
731 } else if (FD_ISSET(0, &tmpfds)) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
732 gotBuf = read(0, tmpBuf, sizeof(tmpBuf));
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
733
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
734 if (gotBuf < 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
735 THERR("Error in reading stdio.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
736 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
737 } else {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
738 /* Call the user input handler */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
739 result = handleInput(tmpSocket, tmpBuf, gotBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
740 if (result < 0) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
741 THERR("Fatal error handling user input: %s\n",
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
742 tmpBuf);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
743 exitProg = TRUE;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
744 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
745 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
746 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
747
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
748 fflush(stdout);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
749 fflush(stderr);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
750 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
751
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
752 /* .. */
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
753 th_free(optUserName2);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
754 close(tmpSocket);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
755
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
756 if (optLogFile) {
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
757 THMSG(1, "Closing logfile.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
758 fclose(optLogFile);
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
759 }
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
760
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
761 THMSG(1, "Connection terminated.\n");
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
762
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
763 return 0;
728243125263 Import.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
764 }