summaryrefslogtreecommitdiff
path: root/vchat-protocol.c
blob: aefe419ae1d3ae463729cb896cc98fafc791bf87 (plain)
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
 * vchat-client - alpha version
 * vchat-protocol.c - handling of server connection & messages
 *
 * 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 <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <readline/readline.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#ifndef NO_LOCALE
#include <locale.h>
#include <langinfo.h>
#endif

/* local includes */
#include "vchat.h"
#include "vchat-ssl.h"

/* version of this module */
char *vchat_io_version = "$Id$";

/* externally used variables */
int serverfd = -1;
unsigned int usingcert = 1;

/* locally global variables */
/* SSL-connection */
static BIO *sslconn = NULL;

/* declaration of local helper functions */
static void usersignon (char *);
static void usersignoff (char *);
static void usernickchange (char *);
static void userjoin (char *);
static void userleave (char *);                                                                                                                                                                         
static void receivenicks (char *message);
static void justloggedin (char *message);
static void nickerr (char *message);
static void login (char *message);
static void anonlogin (char *message);
static void topicinfo (char *message);
static void pubaction (char *message);
static void pubthoughts (char *message);
static void serverlogin (char *message);
static void idleprompt (char *message);
static void topicchange (char *message);
static void pmnotsent (char *message);
static int  getportnum (char *port);

/* declaration of server message array */
#include "vchat-messages.h"

/* status-variable from vchat-client.c
 * eventloop is done as long as this is true */
extern int status;

int usessl = 1;
int ignssl = 0;
char *encoding;

/* connects to server */
int
vcconnect (char *server, char *port)
{
   /* used for tilde expansion of cert & key filenames */
   char *tildex = NULL;

   /* vchat connection x509 store */
   vc_x509store_t vc_store;

   /* SSL-context */
   SSL_CTX *sslctx = NULL;

   /* pointer to tilde-expanded certificate/keyfile-names */
   char *certfile = NULL, *keyfile = NULL;

   SSL_library_init ();
   SSL_load_error_strings();

   vc_init_x509store(&vc_store);

   vc_x509store_setflags(&vc_store, VC_X509S_SSL_VERIFY_PEER);
   /* get name of certificate file */
   certfile = getstroption (CF_CERTFILE);

   /* do we have a certificate file? */
   if (certfile) {
      /* does the filename start with a tilde? expand it! */
      if (certfile[0] == '~')
         tildex = tilde_expand (certfile);
      else
         tildex = certfile;

      if (usingcert) {

         vc_x509store_setflags(&vc_store, VC_X509S_USE_CERTIFICATE);
         vc_x509store_setcertfile(&vc_store, tildex);

         /* get name of key file */
         keyfile = getstroption (CF_KEYFILE);

         /* if we don't have a key file, the key may be in the cert file */
         if (!keyfile)
            keyfile = certfile;

         /* does the filename start with a tilde? expand it! */
         if (keyfile[0] == '~')
            tildex = tilde_expand (keyfile);
         else
            tildex = keyfile;

         vc_x509store_set_pkeycb(&vc_store, (vc_askpass_cb_t)passprompt);
         vc_x509store_setkeyfile(&vc_store, tildex);

         /* check if OpenSSL thinks key & cert belong together */
         /* result = SSL_CTX_check_private_key (sslctx); THS TODO (->
          * vchat-ssl.c) */
      }
   }
   
   usessl = getintoption(CF_USESSL);
   vc_x509store_setignssl(&vc_store, getintoption(CF_IGNSSL));

   sslconn = vc_connect(server, getportnum(port), usessl, &vc_store, &sslctx);

   if(sslconn == NULL) {
      exitui();
      exit(-1);
   }

   serverfd = BIO_get_fd(sslconn, 0);

   /* inform user */
   snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_CONNECTED), server, getportnum(port));
   writechan (tmpstr);

   /* dump x509 details here... TODO */
   writecf (FS_DBG,"# SSL Server information: TODO :)");

   /* if we didn't fail until now, we've got a connection. */
   return 1;
}

/* disconnect from server */
void
vcdisconnect ()
{
  close (serverfd);
  serverfd = -1;
}

/* lookup a port number by service string */
static int getportnum (char *port)
{
  char *endpt = NULL;
  struct servent *service = getservbyname(port, "tcp");
  int dport = strtoul( port, &endpt, 10);

  if( service )
    return htons( service->s_port );
  if( (*endpt == 0) && ((char *)port != endpt) )
    return dport;
  return -1;
}

/* handle a pm not sent error
 *  format: 412 %s */
static void
pmnotsent (char *message)
{
  while(*message && *message!=' ') message++;
  snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_ERR),message+1);
  writepriv( tmpstr, 0);

}

/* parse and handle an action string
 *  format: 118 %s %s
 *    vars: %s nick
 *          %s action */
static void
pubaction (char *message)
{
  char *nick = NULL, *action = NULL;
  nick = strchr (message, ' ');
  nick[0] = '\0';
  nick++;

  action = strchr (nick, ' ');
  action[0] = '\0';
  action++;

  snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_PUBACTION),nick,action);
  writechan (tmpstr);
}

/* parse and handle an thought string
 *  format: 124 %s %s
 *    vars: %s nick
 *          %s thought */
static void
pubthoughts (char *message)
{
  char *nick = NULL, *thoughts = NULL;
  nick = strchr (message, ' ');
  nick[0] = '\0';
  nick++;

  thoughts = strchr (nick, ' ');
  thoughts[0] = '\0';
  thoughts++;
 
  snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_PUBTHOUGHT),nick,thoughts);
  writechan (tmpstr);
}

/* parse and handle server logon */
static void
serverlogin (char *message)
{
#ifndef NO_LOCALE
  int utf8=!strcmp(nl_langinfo(CODESET), "UTF-8");
  if (utf8)
    networkoutput(".e utf8");
#endif
}

/* parse and handle an idle message
 *  format: 305
 *    vars: %s message */
static void
idleprompt (char *message)
{
  char *msg = NULL;
  msg = strchr (message, ' ');
  msg[0] = '\0';
  msg++;

  snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_IDLE),msg);
  writechan (tmpstr);
}

/* parse and handle a topic information string
 *  format: 115 %d %s
 *    vars: %d chan  - channel number
 *          %s topic - topic  */
static void
topicinfo (char *message)
{
  char *channel = NULL, *topic = NULL;
  int tmpchan = 0;
  
  /* search start of channel number */
  channel = strchr (message, ' ');
  channel++;

  /* search start of topic and terminate channel number */
  topic = strchr (channel, ' ');
  topic[0] = '\0';
  topic++;

  /* convert channel number to integer */
  tmpchan = atoi (channel);

  if (tmpchan == chan) {
     /* show change in topic window */
     if (strlen(topic))
        snprintf (topicstr, TOPICSTRSIZE, getformatstr(FS_TOPICW), chan, topic);
      else
        snprintf (topicstr, TOPICSTRSIZE, getformatstr(FS_NOTOPICW), chan);
     topicline(NULL);
  }

  /* announce change in channel window */
  if (strlen(topic))
     snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_TOPIC), tmpchan, topic);
   else
     snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_NOTOPIC), tmpchan);
  writechan (tmpstr);
}

/* parse and handle a topic change string
 *  format: 114 %s changes topic to '%s'
 *    vars: %s nick
 *          %s topic */
static void
topicchange (char *message)
{
  char *nick = NULL, *topic = NULL;
  int len;

  /* search start of nickname */
  nick = strchr (message, ' ');
  nick++;

  /* search start of message before topic, terminate nick */
  topic = strchr (nick, ' ');
  topic[0] = '\0';
  topic++;

  /* search start of real topic and terminate channel number */
  topic = strchr (topic, '\'');
  topic[0] = '\0';
  topic++;

  /* remove ending '\'', if there */
  len = strlen (topic);
  if (topic[len-1] == '\'')
    topic[len-1] = '\0';

  /* show change in topic window */
  snprintf (topicstr, TOPICSTRSIZE, getformatstr(FS_TOPICW), chan, topic);
  topicline(NULL);

  /* announce change in channel window */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_CHGTOPIC), nick, topic);
  writechan (tmpstr);
}

/* parse and handle a login message
 *  format: 212 %s %s
 *    vars: %s str1 - nick used to login
 *          %s str2 - servers message */
static void
justloggedin (char *message)
{
  char *str1 = NULL, *str2 = NULL;
  /* search start of nickname */
  str1 = strchr (message, ' ');
  str1++;

  /* search start of message, terminate nick */
  str2 = strchr (str1, ' ');
  str2[0] = '\0';
  str2++;

  /* if we have a new nick, store it */
  if (!nick || strcasecmp (nick, str1))
     setstroption(CF_NICK,str1);

  /* show change in console window */
  snprintf (consolestr, CONSOLESTRSIZE, getformatstr(FS_CONSOLE), nick, getstroption (CF_SERVERHOST), getportnum(getstroption (CF_SERVERPORT)));
  consoleline (NULL);

  /* announce login as servermessage */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_SIGNON), nick, str2);
  writechan (tmpstr);

  /* we're not logged in, change status and request nicks */
  if (!loggedin)
    {
      loadcfg(getstroption(CF_LOGINSCRIPT),handleline);
      handleline(".S");
      loggedin = 1;
    }
}

/* this user joins a different channel */
void
ownjoin (int channel)
{
  /* change global channel info */
  chan = channel;
  networkoutput(".t");
  snprintf(tmpstr, TMPSTRSIZE, ".S %d",chan);
  networkoutput(tmpstr);
}

/* this user leaves a channel */
void
ownleave (int channel)
{
  /* change global channel info */
  chan = 0;
}

/* this user changes his nick */
void
ownnickchange (char *newnick)
{
  /* free old nick, store copy of new nick */
  setstroption(CF_NICK,newnick);

  /* show change in console window */
  snprintf (consolestr, CONSOLESTRSIZE, getformatstr(FS_CONSOLE), nick, getstroption (CF_SERVERHOST), getportnum(getstroption (CF_SERVERPORT)));
  consoleline (NULL);
}

/* parse and handle a nick error message
 *  format: 401 %s
 *          403 %s
 *          415 %s
 *    vars: %s - server message */
static void
nickerr (char *message)
{
  char *helpkiller = NULL;
  /* mutate message for output */
  message[2] = '!';
  /* help information found? remove it. */
  if ((helpkiller = strstr (message, " Type .h for help")))
    helpkiller[0] = '\0';
  /* nickchange not done? eliminate message */
  if (loggedin && (helpkiller = strstr (message, " - Nick not changed")))
    helpkiller[0] = '\0';
  /* show errormessage */
  writecf (FS_ERR,&message[2]);

  /* not logged in? insist on getting a new nick */
  if (!loggedin)
    {
      /* free bogus nick */
      setstroption(CF_NICK,NULL);
      /* get new nick via vchat-ui.c */
      nickprompt ();
      
      /* form login message and send it to server */
      snprintf (tmpstr, TMPSTRSIZE, ".l %s %s %d", nick, getstroption (CF_FROM), getintoption (CF_CHANNEL));
      networkoutput (tmpstr);
    }
}

/* parse and handle a registered nick information
 *  format: 120 %s %s
 *    vars: %s      - this users registered nick
 *          %s msg  - server message */
static void
login (char *message)
{
  char *msg = NULL;

  /* mutate message for output */
  message[2] = '*';
  /* show message to user */
  writecf (FS_SERV,&message[2]);

  /* we don't know our nick? */
  if (!nick)
    {
      /* find message after nick */
      msg = strchr (&message[4], ' ');
      if (msg)
	{
          /* terminate string before message and copy nick */
	  msg[0] = '\0';
	  setstroption(CF_NICK,&message[4]);
	}
      else
	{
          /* no string in servers message (huh?), ask user for nick */
	  nickprompt ();
	}
    }
  
  /* form login message and send it to server */
  snprintf (tmpstr, TMPSTRSIZE, ".l %s %s %d", nick, getstroption (CF_FROM), getintoption (CF_CHANNEL));
  networkoutput (tmpstr);
}

/* parse and handle anon login request
 *  format: 121 %s
 *    vars: %s - server message */
static void
anonlogin (char *message)
{
  /* mutate message for output */
  message[2] = '*';
  /* show message to user */
  writecf (FS_SERV,&message[2]);

  /* we don't know our nick? ask for it! */
  if (!nick)
    nickprompt ();

  /* form login message and send it to server */
  snprintf (tmpstr, TMPSTRSIZE, ".l %s %s %d", nick, getstroption (CF_FROM), getintoption (CF_CHANNEL));
  networkoutput (tmpstr);
}

/* parse and handle list of nicks (from '.S')
 *  format: 119 %s ..
 *    vars: %s nick - a users nick */
static void
receivenicks (char *message)
{
  char *str1 = NULL, *str2 = NULL;
  int mychan = 0;
  void (*ul_myfunc)(char*,int);

  /* show message to user */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_USONLINE), &message[4]);
  writechan (tmpstr);

  /* search for channelnumber */
  str1 = strchr (message, ' ');
  str1++;
  if (str1[0] == '*') {
      if (nicks) return;
      ul_myfunc = ul_add;
      str1++;
  } else {
      str2 = str1;
      str1 = strchr(str2,' ');
      str1[0] = '\0';
      mychan = atoi(str2);
      ul_myfunc = ul_moveuser;
  }
  str1++;

  /* while user .. */
  while (str1)
    {
      /* search next user */
      str2 = strchr (str1, ' ');
      /* there is another user? terminate this one */
      if (str2) {
	str2[0] = '\0';
        str2++;
      }

      /* add this user via vchat-user.c */
      ul_myfunc (str1,mychan);

      /* next user .. */
      str1 = str2;
    }
}

/* parse and handle a login message
 *  format: 211 %s %s
 *    vars: %s nick - who logged on
 *          %s msg  - servers message */
static void
usersignon (char *message)
{
  char *nick = NULL, *msg = NULL;
  /* search start of nickname */
  nick = strchr (message, ' ');
  nick++;

  /* search start of message, terminate nick */
  msg = strchr (nick, ' ');
  msg[0] = '\0';
  msg++;

  /* add this user via vchat-user.c */
  ul_add (nick, 0);

  /* show message to user */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_SIGNON), nick, msg);
  writechan (tmpstr);
}

/* parse and handle a logoff message
 *  format: 221 %s %s
 *    vars: %s nick - who logged off
 *          %s msg  - servers message */
static void
usersignoff (char *message)
{
  char *nick = NULL, *msg = NULL;
  /* search start of nickname */
  nick = strchr (message, ' ');
  nick++;

  /* search start of message, terminate nick */
  msg = strchr (nick, ' ');
  msg[0] = '\0';
  msg++;

  /* delete this user via vchat-user.c */
  ul_del (nick, 0);

  /* show message to user */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_SIGNOFF), nick, msg);
  writechan (tmpstr);
}

/* parse and handle a join message
 *  format: 232 %s %s %d
 *    vars: %s nick - who joined
 *          %s msg  - servers message
 *          %d chan - channel joined */
static void
userjoin (char *message)
{
  char *nick = NULL, *msg = NULL, *channel = NULL;
  int chan = 0;

  /* search start of nickname */
  nick = strchr (message, ' ');
  nick++;

  /* search start of message, terminate nick */
  msg = strchr (nick, ' ');
  msg[0] = '\0';
  msg++;

  /* search start of channel, terminate message */
  channel = strrchr (msg, ' ');
  channel[0] = '\0';
  channel++;

  /* convert channel to integer */
  chan = atoi (channel);

  /* notice channel join via vchat-user.c */
  ul_join (nick, chan);

  /* show message to user */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_JOIN), nick, msg, chan);
  writechan (tmpstr);
}

/* parse and handle a leave message
 *  format: 231 %s %s %d
 *    vars: %s nick - who left
 *          %s msg  - servers message
 *          %d chan - channel joined */
static void
userleave (char *message)
{
  char *nick = NULL, *msg = NULL, *channel = NULL;
  int chan = 0;

  /* search start of nickname */
  nick = strchr (message, ' ');
  nick++;

  /* search start of message, terminate nick */
  msg = strchr (nick, ' ');
  msg[0] = '\0';
  msg++;

  /* convert channel to integer */
  channel = strrchr (msg, ' ');
  channel[0] = '\0';
  channel++;

  /* convert channel to integer */
  chan = atoi (channel);

  /* notice channel leave via vchat-user.c */
  ul_leave (nick, chan);

  /* show message to user */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_LEAVE), nick, msg, chan);
  writechan (tmpstr);
}

/* parse and handle a nickchange message
 *  format: 241 %s %s %s
 *    vars: %s oldnick - users old nick
 *          %s newnick - users new nick
 *          %s msg     - server message */
static void
usernickchange (char *message)
{
  char *oldnick = NULL, *newnick = NULL, *msg = NULL;

  /* search start of old nickname */
  oldnick = strchr (message, ' ');
  oldnick++;

  /* search start of new nickname, terminate old nick */
  newnick = strchr (oldnick, ' ');
  newnick[0] = '\0';
  newnick++;

  /* search start of message, terminate new nick */
  msg = strchr (newnick, ' ');
  msg[0] = '\0';
  msg++;

  /* notice nickchange via vchat-user.c */
  ul_nickchange (oldnick, newnick);

  /* show message to user */
  snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_NICKCHANGE), oldnick, newnick, msg);
  writechan (tmpstr);
}

/* handle received message from server */
static void
parsemsg (char *message)
{
  char *str1, *str2;
  int i;
  /* message to short or starts with '<'? must be channel */
  if (message[0] == '<')
  {
    str1 = &message[1];
    str2 = strchr(str1,'>');
    str2[0] = '\0';
    str2++;
    if (str2[0] == ' ') str2++;
    if (!strncasecmp(nick,str2,strlen(nick)))
       snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_MYPUBMSG),str1,str2);
    else
       snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_RXPUBMSG),str1,str2);
    writechan (tmpstr);
  }
  else if (message[0] == '[')
  {
    str1 = &message[1];
    str2 = strchr(str1,']');
    str2[0] = '\0';
    str2++;
    if (str2[0] == ' ') str2++;
    if (!strncasecmp(nick,str2,strlen(nick)))
       snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_MYPUBURL),str1,str2);
    else
       snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_RXPUBURL),str1,str2);
    writechan (tmpstr);
  }
  /* message starts with '*'? must be private */
  else if (message[0] == '*')
  {
    str1 = &message[1];
    str2 = strchr(str1,'*');
    str2[0] = '\0';
    str2++;
    if (str2[0] == ' ') str2++;
    snprintf(tmpstr,TMPSTRSIZE,getformatstr(FS_RXPRIVMSG),str1,str2);
    writepriv (tmpstr, 1);
    ul_msgfrom(str1);
  }
  /* message starts with a number? must be a servermessage */
  else if ((message[0] >= '0') && (message[0] <= '9'))
    {
      /* walk server message array */
      for (i = 0; servermessages[i].id[0]; i++)
	{
          /* is this the message? */
	  if (!(strncmp (servermessages[i].id, message, 3)))
	    {
              /* scripting hook available - call it */
	      if (servermessages[i].hook)
		servermessages[i].hook (message);
              /* function available - call it */
	      else if (servermessages[i].funct)
		servermessages[i].funct (message);
              /* no function available, but known - give output */
	      else
		{
                  /* remove continutation mark */
		  if (message[3] == '-')
		    message[3] = ' ';

                  /* mutate message for display */
		  message[2] = '*';
                  /* giveout message by type */
		  switch (servermessages[i].type)
		    {
		    case SM_IGNORE:
		      break;
		    case SM_INFO:
                      /* change marker and send as servermessage */
		      message[2] = '#';
		      writecf (FS_SERV,&message[2]);
		      break;
		    case SM_USERINFO:
                      /* keep marker and send as servermessage */
		      writecf (FS_SERV,&message[2]);
		      break;
		    case SM_CHANNEL:
                      /* keep marker and send as channelmessage */
		      writechan (&message[2]);
		      break;
		    case SM_ERROR:
                      /* change marker and send as errormessage */
		      message[2] = '!';
		      writecf (FS_ERR,&message[2]);
		      break;
		    default:
                      /* fallback: keep marker and send as servermessage */
		      writecf (FS_SERV,&message[2]);
		    }
		}
	      return;
	    }
	}
      /* message not in list, raise errormessage */
      snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_UNKNOWNMSG), message);
      writecf (FS_ERR,tmpstr);
    }
  else
    {
      /* message neither public, private or server, raise errormessage */
      snprintf (tmpstr, TMPSTRSIZE, getformatstr(FS_BOGUSMSG), message);
      writecf (FS_ERR,tmpstr);
    }
}

/* offset in buffer (for linebreaks at packet borders) */
static int bufoff = 0;

/* get data from servers filedescriptor */
void
networkinput (void)
{
  int bytes;
  char *tmp = NULL;
#define BUFSIZE 4096
  char buf[BUFSIZE];     /* data buffer */
  char *ltmp = buf;
  buf[BUFSIZE-1] = '\0'; /* sanity stop */

  /* receive data at offset */
  bytes = BIO_read (sslconn, &buf[bufoff], BUFSIZE-1 - bufoff);

  /* no bytes transferred? raise error message, bail out */
  if (bytes < 0)
    {
      snprintf (tmpstr, TMPSTRSIZE, "Receive fails, %s.", strerror(errno));
      strncpy(errstr,tmpstr,TMPSTRSIZE-2);
      errstr[TMPSTRSIZE-2] = '\0';
      strcat(errstr,"\n");
      writecf (FS_ERR,tmpstr);
      status = 0;
    }
  /* end of file from server? */
  else if (bytes == 0)
    {
      /* inform user, bail out */
      writecf (FS_SERV,"* EOF from server");
      strncpy(errstr,"* EOF from server",TMPSTRSIZE-2);
      errstr[TMPSTRSIZE-2] = '\0';
      strcat(errstr,"\n");
      status = 0;
    }
  else
    {
      /* terminate message */
      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])
            {
#ifdef DEBUG
              /* debugging? log network input! */
              fprintf (stderr, "<| %s\n", ltmp);
#endif
	      parsemsg (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;
    }
}

void
networkoutput (char *msg)
{
#ifdef DEBUG
  /* debugging? log network output! */
  fprintf (stderr, ">| %s\n", msg);
#endif

  /* send data to server */
  if (BIO_write (sslconn, msg, strlen (msg)) != strlen (msg))
    writecf (FS_ERR,"Message sending fuzzy.");

  /* send line termination to server */
  if (BIO_write (sslconn, "\r\n", 2) != 2)
    writecf (FS_ERR,"Message sending fuzzy.");
}