1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
/*
* vchat-client - alpha version
* vchat-client.c - main() and utility functions
*
* Copyright (C) 2001 Andreas Kotes <count@flatline.de>
*
* This program is free software. It can be redistributed and/or modified,
* provided that this copyright notice is kept intact. This program is
* distributed in the hope that it will be useful, but without any warranty;
* without even the implied warranty of merchantability or fitness for a
* particular purpose. In no event shall the copyright holder be liable for
* any direct, indirect, incidental or special damages arising in any way out
* of the use of this software.
*
*/
/* general includes */
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <readline/readline.h>
#include <openssl/ssl.h>
#ifndef NO_LOCALE
#include <locale.h>
#endif
#include "vchat.h"
/* version of this module */
unsigned char *vchat_cl_version = "$Id$";
/* externally used variables */
/* we're logged in */
unsigned int loggedin = 0;
/* we run as long as this is true */
unsigned int status = 1;
/* error string to show after exit */
unsigned char errstr[ERRSTRSIZE] = "\0";
/* locally global variables */
/* our list of filedescriptors */
static fd_set masterfds;
/* declaration of configuration array */
#include "vchat-config.h"
/* servers filedescriptor from vchat-protocol.c */
extern int serverfd;
void setnoption (unsigned char *, unsigned char *);
static void parsecfg(unsigned char *line) {
int bytes;
unsigned char *param=line;
unsigned char *value=NULL;
/* handle quotes value is empty, so wecan use it */
value = strchr(line,'#');
if (value) { /* the line contains a cute little quote */
value[0]='\0'; /* ignore the rest of the line */
}
/* now split the line into two parts */
value = strchr(line,'=');
if (!value) return; /* exit if strchr fails */
value[0]='\0';
value++;
/* "trim" values */
while ((value[0] == ' ')||(value[0] == '\t'))
value++;
bytes = strlen(value);
while ((value[bytes-1] == ' ')||(value[bytes-1] == '\t')) {
value[bytes-1] = '\0';
bytes=strlen(value);
}
/* bytes should be strlen(value) */
if ( value[bytes-1] == '"' ) value[bytes-1] = '\0';
if ( value[0] == '"' ) value++;
/* "trim" param */
while ((param[0] == ' ')||(param[0] == '\t'))
param++;
bytes = strlen(param);
while ((param[bytes-1] == ' ')||(param[bytes-1] == '\t')) {
param[bytes-1] = '\0';
bytes=strlen(param);
}
/* bytes should be strlen(param) */
if ( param[bytes-1] == '\"' ) param[bytes-1] = '\0';
if ( param[0] == '\"' ) param++;
if ((!param)||(!value)) return; /* failsave */
//fprintf(stderr,"\"%s\" -> \"%s\"\n",param,value);
setnoption(param,value);
}
static void parseformats(unsigned char *line) {
int i;
unsigned char *tmp = NULL;
/* read a format line from file, syntax is
FS_XXX = "formatstring"
*/
while( *line == ' ') line++;
if( strlen( line ) > TMPSTRSIZE ) return;
if( *line != '#') /* allow to comment out the line */
for (i = 0; formatstrings[i].formatstr; i++)
if (!strncasecmp(formatstrings[i].idstring, line, strlen( formatstrings[i].idstring) ))
{
unsigned char *tail = line + strlen( formatstrings[i].idstring);
while( *tail==' ' || *tail=='\t') tail++; /* and skip whitespaces */
if( *tail++ == '=' )
{
while( *tail==' ' || *tail=='\t') tail++;
if( *(tail++)=='\"' )
{
int j, k = 0, stringends = 0, backslash=0;
for ( j = 0; tail[j] && !stringends; j++)
{
switch( tail[j] ) {
case '^':
if ( tail[j+1] != '^' )
tmpstr[k++] = 1;
break;
case '\\':
backslash=1-backslash;
tmpstr[k++] = '\\';
break;
case '\"':
if (backslash) k--; else stringends = 1;
default:
tmpstr[k++] = tail[j];
backslash = 0;
}
}
if ( stringends && ( (tmp = (unsigned char *)malloc( 1 + j )) != NULL ) )
{
memcpy( tmp, tmpstr, k);
tmp[k-1]=0;
formatstrings[i].formatstr = tmp;
}
}
}
}
}
/* UNUSED uncomment if needed
static void parseknownhosts(unsigned char *line) {
}
*/
/* load config file */
static void
loadcfg (unsigned char *file,void (*lineparser) (unsigned char *))
{
int fd;
int bytes,bufoff=0;
unsigned char *tmp = NULL;
#define BUFSIZE 4096
unsigned char buf[BUFSIZE]; /* data buffer */
unsigned char *ltmp = buf;
unsigned char *tildex = NULL;
buf[BUFSIZE-1] = '\0'; /* sanity stop */
if (!file) return;
if (!file[0]) return;
if (file[0] == '~')
tildex = tilde_expand(file);
else
tildex = file;
fd = open(tildex,O_RDONLY);
if (fd == -1) {
snprintf (errstr, TMPSTRSIZE, "Can't open config-file \"%s\": %s.", tildex, sys_errlist[errno]);
} else {
while ((bytes = read(fd,&buf[bufoff],BUFSIZE-bufoff-1))) {
if (bytes < 0) {
close(fd);
return;
} else {
/* terminate string */
buf[bytes + bufoff] = '\0';
/* as long as there are lines .. */
while ((tmp = strchr (ltmp, '\n')) != NULL) {
/* did the server send CR+LF instead of LF with the last line? */
if (tmp[-1] == '\r')
tmp[-1] = '\0';
/* remove newline from previous message, advance pointer of next
* message */
tmp[0] = '\0';
tmp++;
/* we have a last message? give away to line handler! */
if (ltmp[0])
{
lineparser(ltmp);
}
/* move line along .. */
ltmp = tmp;
}
/* buffer exhausted, move partial line to start of buffer and go
* on .. */
bufoff = (bytes+bufoff) - (ltmp-buf);
if (bufoff > 0)
memmove (buf, ltmp, bufoff);
else
bufoff = 0;
}
}
close(fd);
}
}
void
loadconfig (unsigned char *file)
{
loadcfg(file,parsecfg);
}
void
loadformats (unsigned char *file)
{
loadcfg(file,parseformats);
}
/* get-format-string */
unsigned char *
getformatstr (formtstr id)
{
int i;
for (i = 0; formatstrings[i].formatstr; i++)
if (formatstrings[i].id == id) return formatstrings[i].formatstr;
return NULL;
}
/* get-string-option, fetches *char-value of variable named by option */
unsigned char *
getstroption (confopt option)
{
int i;
#ifdef DEBUG
fprintf(stderr,"getstroption: %d\n",option);
#endif
for (i = 0; configoptions[i].type != CO_NIL; i++)
if ((configoptions[i].id == option) && (configoptions[i].type == CO_STR)) {
if (!configoptions[i].value)
return configoptions[i].defaultvalue;
else
return configoptions[i].value;
}
return NULL;
}
/* set-string-option, puts *char-value to variable named by option */
void
setstroption (confopt option, unsigned char *string)
{
int i;
#ifdef DEBUG
fprintf(stderr,"setstroption: %d to %s\n",option,string);
#endif
for (i = 0; configoptions[i].type != CO_NIL; i++)
if ((configoptions[i].id == option) && (configoptions[i].type == CO_STR)) {
if (configoptions[i].value)
free(configoptions[i].value);
if (string)
configoptions[i].value = strdup(string);
else
configoptions[i].value = NULL;
if (configoptions[i].localvar.pstr)
*configoptions[i].localvar.pstr = configoptions[i].value;
}
}
/* set-named-option, puts string to variable named by name */
void
setnoption (unsigned char *name, unsigned char *string)
{
int i;
#ifdef DEBUG
fprintf(stderr,"setstrnoption: %s to %s\n",name,string);
#endif
for (i = 0; configoptions[i].type != CO_NIL; i++)
if (!strcmp(configoptions[i].varname,name)) {
if (configoptions[i].type == CO_STR) {
if (configoptions[i].value)
free(configoptions[i].value);
if (string)
configoptions[i].value = strdup(string);
else
configoptions[i].value = NULL;
if (configoptions[i].localvar.pstr)
*configoptions[i].localvar.pstr = configoptions[i].value;
} else if (configoptions[i].type == CO_INT) {
configoptions[i].value = (char *) atoi(string);
if (configoptions[i].localvar.pint)
*configoptions[i].localvar.pint = (int)configoptions[i].value;
}
}
}
/* get-integer-option, fetches int-value of variable named by option */
int
getintoption (confopt option)
{
int i;
#ifdef DEBUG
fprintf(stderr,"getintoption: %d\n",option);
#endif
for (i = 0; configoptions[i].type != CO_NIL; i++)
if ((configoptions[i].id == option) && (configoptions[i].type == CO_INT)) {
if ((int)configoptions[i].value == -1)
return (int) configoptions[i].defaultvalue;
else
return (int) configoptions[i].value;
}
return 0;
}
/* set-integer-option, puts int-value to variable named by option */
void
setintoption (confopt option, int value)
{
int i;
#ifdef DEBUG
fprintf(stderr,"setintoption: %d to %d\n",option,value);
#endif
for (i = 0; configoptions[i].type != CO_NIL; i++)
if ((configoptions[i].id == option) && (configoptions[i].type == CO_INT)) {
configoptions[i].value = (char *) value;
if (configoptions[i].localvar.pint)
*configoptions[i].localvar.pint = (int)configoptions[i].value;
}
}
int quitrequest = 0;
/* cleanup-hook, for SIGINT */
void
cleanup (int signal)
{
if( signal == SIGINT ) {
switch( quitrequest >> 2 ) {
case 0:
flushout( );
writeout( " Press Ctrl+C twice now to confirm ");
showout( );
quitrequest+=4;
return;
break;
case 1:
flushout( );
writeout( " Press Ctrl+C twice now to confirm ");
writeout( " Press Ctrl+C once now to confirm ");
showout( );
quitrequest+=4;
return;
break;
default:
break;
}
}
/* restore terminal state */
exitui ();
/* clear userlist */
ul_clear ();
/* close server connection */
if (serverfd > 0)
close (serverfd);
/* inform user if we where killed by signal */
if (signal > 1)
{
fprintf (stderr, "vchat-client: terminated with signal %d.\n", signal);
} else if (errstr[0])
fputs (errstr, stderr);
/* end of story */
exit (0);
}
static int oldseconds = 0;
void calleverysecond( void ) {
/* timetriggered execution, don't rely on being called every 1000us */
/* rather see it as a chance for being called 9 times in 10 seconds */
/* so check time() */
time_t now = time( NULL );
struct tm *mytime = localtime( &now );
if( mytime->tm_sec < oldseconds ) {
consoleline( NULL );
}
oldseconds = mytime->tm_sec;
if(quitrequest)
quitrequest--;
if(outputcountdown && !--outputcountdown)
hideout( );
}
/* this function is called in the master loop */
void
eventloop (void)
{
/* get fresh copy of filedescriptor list */
fd_set readfds = masterfds;
struct timeval tv = { 1, 0};
switch (select (serverfd + 1, &readfds, NULL, NULL, &tv))
{
case -1:
/* EINTR is most likely a SIGWINCH - ignore for now */
if (errno != EINTR)
{
snprintf (tmpstr, TMPSTRSIZE, "Select fails, %s.", sys_errlist[errno]);
strncpy(errstr,tmpstr,TMPSTRSIZE-2);
errstr[TMPSTRSIZE-2] = '\0';
strcat(errstr,"\n");
writecf (FS_ERR,tmpstr);
/* see this as an error condition and bail out */
status = 0;
}
break;
case 0:
/* time out reached */
calleverysecond();
break;
default:
/* something to read from user & we're logged in or have a cert? */
if (FD_ISSET (0, &readfds) && loggedin)
userinput ();
/* something to read from server? */
if (FD_ISSET (serverfd, &readfds))
networkinput ();
break;
}
}
void usage(unsigned char *name) {
printf ("usage: %s [-C config-file] [-F formats] [-l] [-z] [-s host] [-p port] [-c channel] [-n nickname] [-k] [-K] [-L logfile]\n",name);
puts (" -C load a second config-file, overriding the first one");
puts (" -F load format strings (skins) from this file");
puts (" -l local connect (no SSL + connects localhost:2323)");
puts (" -z don't use certificate files");
printf (" -s set server (default \"%s\")\n",getstroption(CF_SERVERHOST));
printf (" -p set port (default %s)\n",getstroption(CF_SERVERPORT));
printf (" -c set channel (default %d)\n",getintoption(CF_CHANNEL));
if (nick)
printf(" -n set nickname (default \"%s\")\n",nick);
else
puts (" -n set nickname");
printf (" -f set from (default \"%s\")\n",getstroption(CF_FROM));
puts (" -k keep autolog");
puts (" -K don't keep autolog");
printf (" -L use this file as logfile (default \"%s\")\n",getstroption(CF_LOGFILE));
puts (" -h gives this help");
}
/* main - d'oh */
int
main (int argc, char **argv)
{
int pchar;
int cmdsunparsed = 1;
#ifndef NO_LOCALE
setlocale(LC_ALL,"");
#endif
loadconfig (GLOBAL_CONFIG_FILE);
loadconfig (getstroption (CF_CONFIGFILE));
/* parse commandline */
while (cmdsunparsed) {
pchar = getopt(argc,argv,"C:F:lzs:p:c:n:f:kKL:h");
#ifdef DEBUG
fprintf(stderr,"parse commandline: %d ('%c'): %s\n",pchar,pchar,optarg);
#endif
switch (pchar) {
case -1 : cmdsunparsed = 0; break;
case 'C': loadconfig(optarg); break;
case 'F': setstroption(CF_FORMFILE,optarg); break;
case 'l': setintoption(CF_USESSL,0); break;
case 'z': setintoption(CF_USECERT,0); break;
case 's': setstroption(CF_SERVERHOST,optarg); break;
case 'p': setstroption(CF_SERVERPORT,optarg); break;
case 'c': setintoption(CF_CHANNEL,strtol(optarg,NULL,10)); break;
case 'n': setstroption(CF_NICK,optarg); break;
case 'f': setstroption(CF_FROM,optarg); break;
case 'k': setintoption(CF_KEEPLOG,1); break;
case 'K': setintoption(CF_KEEPLOG,0); break;
case 'L': setstroption(CF_LOGFILE,optarg); break;
case 'h': usage(argv[0]); exit(0); break;
default : usage(argv[0]); exit(1);
}
}
if (optind < argc) { usage(argv[0]); exit(1); }
loadformats(GLOBAL_FORMAT_FILE);
loadformats(getstroption (CF_FORMFILE));
if (!getintoption(CF_USESSL)) {
setstroption(CF_SERVERHOST,"localhost");
setstroption(CF_SERVERPORT,"2323");
} else {
SSL_library_init ();
SSL_load_error_strings ();
}
/* install signal handler */
signal (SIGINT, cleanup);
signal (SIGHUP, cleanup);
signal (SIGTERM, cleanup);
signal (SIGQUIT, cleanup);
/* initialize userinterface */
initui ();
/* attempt connection */
if (!vcconnect (getstroption(CF_SERVERHOST), getstroption(CF_SERVERPORT)))
{
snprintf (tmpstr, TMPSTRSIZE, "Could not connect to server, %s.",
sys_errlist[errno]);
strncpy(errstr,tmpstr,TMPSTRSIZE-2);
errstr[TMPSTRSIZE-2] = '\0';
strcat(errstr,"\n");
writecf (FS_ERR,tmpstr);
/* exit condition */
status = 0;
}
else
{
/* add stdin & server to masterdfs */
FD_ZERO (&masterfds);
FD_SET (0, &masterfds);
FD_SET (serverfd, &masterfds);
}
while (status)
eventloop ();
/* sanely close connection to server */
vcdisconnect ();
/* call cleanup-hook without signal */
cleanup (0);
return 0;
}
|