-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFBBFWD.C
1895 lines (1660 loc) · 62.6 KB
/
FBBFWD.C
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The code in this file was adapted from forward.c by KF5MG, to support
* FBB-style ax.25 forwarding.
*/
#include <ctype.h>
#include <time.h>
#include "global.h"
#ifdef FBBFWD
#include "bm.h"
#include "mailbox.h"
#include "mailutil.h"
#include "smtp.h"
#include "cmdparse.h"
#include "proc.h"
#include "socket.h"
#include "timer.h"
#include "usock.h"
#include "netuser.h"
#include "ax25.h"
#include "netrom.h"
#include "nr4.h"
#include "files.h"
#include "index.h"
#ifdef FBBCMP
#include "lzhuf.h"
#include "mailfor.h"
#endif /* FBBCMP */
#ifdef UNIX
#include "unix.h"
/* n5knx: Avoid stdout so line endings are handled right */
#define PRINTF tcmdprintf
#define PUTS(x) tcmdprintf("%s%c",x,'\n')
#define PUTCHAR(x) tcmdprintf("%c",x)
#else
#define PRINTF printf
#define PUTS puts
#define PUTCHAR putchar
#endif
/* By setting the fp to NULL, we can check in exitbbs()
* whether a tempfile has been closed or not - WG7J
*/
#define MYFCLOSE(x) { fclose(x); x = (FILE *) 0; }
#define CTLZ 26 /* EOF for CP/M systems */
extern int MbForwarded;
/*
*** Protocole Error (x) messages.
0: Subject Packet does not start with a SOH (01) Byte.
1: Checksum of message is wrong.
2: Message could not be uncompressed.
3: Received binary frame is not STX (02) or EOT (04).
4: Checksum of proposals is wrong.
5: Answer to proposals must start with "F" or "**".
6: Answer to proposals must be "FS".
7: More than 5 answers (with "+", "-" or "=") to proposals.
8: Answer to proposal is not "+", "-" or "=".
9: The number of answers does not match the number of proposals.
10: More than 5 proposals have been received.
11: The number of fields in a proposal is wrong (6 fields).
12: Protocol command must be "FA", "FB", "F>", "FF" or "FQ".
13: Protocol line starting with a letter which is not "F" or "*".
*/
#define FBBerror(x,y) { tprintf("*** Protocol Error (%d)\n", x); log(y,"fbbfwd: detected FBB protocol error %d", x); }
#ifdef FBBVERBOSELOG
static char RmtDisconnect[] = "FBBFWD: remote disconnected early.";
#endif
#ifdef FBBCMP
static int dofbbacceptnote __ARGS((struct mbx *, char *));
static int fbbsendmsg __ARGS((struct fwd *, int, FILE *));
extern char *Mbhaddress;
extern char *Mbfwdinfo;
extern char *Mbqth;
extern char *Mbzip;
extern int Mbsmtptoo;
extern int Mbheader;
extern char shortversion[];
/* fbbsendmsg() is adapted from sendmsg() in forward.c. It writes the
* message to tfile just as we would send it using sendmsg().
* N5KNX 2/95: if we can't send a complete msg, return code -1, otherwise
* return code 0.
*/
static int
fbbsendmsg (f, msgn, tfile)
struct fwd *f;
int msgn;
FILE *tfile; // Data is returned in this
// file handle. It is open
// when we're called and we
// close it when we leave.
{
struct mbx *m = f->m;
int i = 0;
int rheader = 0;
int result = 0;
long start;
char buf[LINELEN];
/* If the data part of the message starts with "R:" the RFC-822
* headers will not be forwarded. Instead we will add an R:
* line of our own.
*/
if (Mbheader)
{
/* First send recv. date/time and bbs address */
fprintf (tfile, "R:%s", mbxtime (f->ind.mydate));
/* If exists, send H-address */
if (Mbhaddress != NULLCHAR)
fprintf (tfile, " @:%s", Mbhaddress);
/* location, if any */
if (Mbqth != NULLCHAR)
fprintf (tfile, " [%s]", Mbqth);
/* if there is info, put it next */
if (Mbfwdinfo != NULLCHAR)
fprintf (tfile, " %s", Mbfwdinfo);
/* number of the message */
fprintf (tfile, " #:%lu", (f->ind.msgid % 100000L)); /* n5knx: use rightmost 5 digits */
/* The BID, if any */
if (f->bid[0] != '\0')
fprintf (tfile, " $:%s", &f->bid[1]);
/* zip code of the bbs */
if (Mbzip != NULLCHAR)
fprintf (tfile, " Z:%s", Mbzip);
// fputc ('\n',tfile);
fputs("\r\n", tfile);
}
/* Open the mailbox file */
sprintf (buf, "%s/%s.txt", Mailspool, m->area);
if ((m->mfile = fopen (buf,READ_BINARY)) == NULLFILE) goto early_quit;
/* point to start of this message in file */
start = m->mbox[msgn].start;
fseek (m->mfile, start, SEEK_SET);
if (ferror(m->mfile)) goto early_quit;
/* If we also send the smtp headers, now see if the message
* has any R: headers. If so, send them first.
*/
if (Mbsmtptoo) {
while (bgets (buf, sizeof (buf), m->mfile) != NULLCHAR) {
if (!*buf)
break; /* End of smtp headers */
}
if(feof(m->mfile) || ferror(m->mfile)) goto early_quit;
/* Found start of msg text, check for R: lines */
while (bgets (buf, sizeof (buf), m->mfile) != NULLCHAR && !strncmp (buf, "R:", 2)) {
rheader = 1;
fputs (buf,tfile);
// fputc ('\n',tfile);
fputs("\r\n", tfile);
}
/* again point to start of this message in file */
fseek (m->mfile, start, SEEK_SET);
if(ferror(m->mfile)) goto early_quit;
}
/* Go past the SMTP headers to the data of the message.
* Check if we need to forward the SMTP headers!
* 920114 - WG7J
*/
if (Mbsmtptoo && (rheader || Mbheader))
// fputc ('\n',tfile);
fputs("\r\n", tfile);
i = NOHEADER;
while (bgets (buf, sizeof (buf), m->mfile) != NULLCHAR && *buf) {
if (Mbsmtptoo) {
/* YES, forward SMTP headers TOO ! */
switch (htype (buf, &i)) {
case XFORWARD: /* Do not forward the "X-Forwarded-To:" lines */
case STATUS: /* Don't forward the "Status:" line either */
case BBSTYPE:
case SUBJECT:
case TO:
case APPARTO:
case CC:
case DATE:
break;
case FROM:
/* Don't forward the "From: " line either.
* make it ">From: "
*/
fputc ( '>', tfile);
/*note fall-through */
default:
if (!strncmp (buf, "From ", 5))
fputc ('>',tfile);
fputs (buf,tfile);
// fputc ('\n',tfile);
fputs("\r\n", tfile);
}
}
}
if(feof(m->mfile) || ferror(m->mfile)) goto early_quit;
/* Now we are at the start of message text.
* the rest of the message is treated below.
* Remember that R: lines have already been sent,
* if we sent smtp headers !
*/
i = 1;
while (bgets (buf, sizeof (buf), m->mfile) != NULLCHAR && strncmp (buf, "From ", 5)) {
if (i) {
if (!strncmp (buf, "R:", 2)) {
if (Mbsmtptoo)
continue;
} else {
i = 0;
if (*buf) {
/* Ensure body is separated from R: line */
// fputc ('\n',tfile);
fputs("\r\n", tfile);
}
}
}
fputs (buf,tfile);
fputs ("\r\n",tfile);
}
if(feof(m->mfile)) clearerr(m->mfile); /* only place EOF is acceptable */
early_quit:
if(m->mfile == NULLFILE || ferror(m->mfile) || feof(m->mfile) ||
(ftell(m->mfile)-start < m->mbox[msgn].size)) {
#ifdef JPDEBUG
log(m->user,"fbbsendmsg: truncated %s msg %d would result from err %d",m->area,msgn,errno);
#endif
fputs("\r\n*** Cannot find complete message body!\r\n", tfile);
#if defined(MAILERROR) && defined(JPDEBUG)
mail_error("FBBsendmsg: %s: Cannot find body for %s msg %d", \
m->name,m->area,msgn);
#endif
/* OK, we were unable to send a complete msg body. We rely on returning an
error code to result in a disconnect rather than a completed msg.
*/
// m->user = -1; /* this will fail subsequent recvline(m->user...) calls */
result = -1; /* this will prevent a /ex or ^Z from being sent */
}
fclose(m->mfile);
m->mfile = NULL;
// close the data file.
fclose(tfile);
return result;
}
#endif /* FBBCMP */
/* FBB Fowarding comments:
FBB Forwarding sends messages in groups instead of normal pbbs forwarding
that sends everything. i.e. You send 5 messages, he sends 5 messages, you
send 5 messages, he sends 5 messages, etc.
In our case... we send up to 5 messages from a forwarding area and then
let the other system send us messages. When we get to the end of the
messages in an area, we may send less than 5 messages. When we're done
with the area... the other system will send us messages and we'll switch
to another message area and start sending messages again. So... he may
send 5 messages each time, and we might send 1 message each time... it just
depends on how many messages are in each area. Hope that makes sense.
*/
#define FBBMAXMSGS 5 /* Maximum number of messages to process. */
#ifdef EXPIRY
extern int Eproc;
int FBBSendingCnt = 0; /* how many dofbbsend()'s in progress (see expire.c) */
#endif
#define fbbUNKNOWN 0
#define fbbNO 1
#define fbbYES 2
#define fbbDEFER 3
// This code converts/parses a FBB FA/FB type message to a fbbpacket structure.
static
int fbbparse(struct fbbpacket *msglst, char *fbline){
char *cp;
char *atbbs;
char *to;
/* Here's an example FBB message header packet.
This one happens to contain 3 message headers.
FB B KF5MG USA NOS 34535_KF5MG 298
FB B KF5MG USA NOS 34537_KF5MG 303
FB B KF5MG USA NOS 34541_KF5MG 309
*/
// Data Type ( FA = compressed ascii msg; FB = compressed binary file, or uncompressed msg)
cp = strtok(fbline," ");
strncpy(msglst->fbbcmd, cp, sizeof(msglst->fbbcmd));
// Message type ( P, B, or T )
cp = strtok('\0', " ");
msglst->type = *cp;
// From id ( Userid only )
cp = strtok('\0', " ");
free(msglst->from);
msglst->from = strdup(cp);
// hostname or flood-area ( USA, WW, KF5MG.#DFW.TX.USA.NOAM, etc.)
cp = strtok('\0', " ");
atbbs = strdup(cp);
// To: id ( 6 char max. )
cp = strtok('\0', " ");
to = strdup(cp);
// Bid ( ax.25 PBBS message id )
cp = strtok('\0', " ");
free(msglst->messageid);
msglst->messageid = strdup(cp);
// Size ( At present, we don't do anything with this. )
cp = strtok('\0', " ");
msglst->size = atoi(cp);
// set msglst->to equal to the 'to id' '@' 'hostname'
free(msglst->to);
msglst->to = malloc(strlen(to) + strlen(atbbs) + 2);
strcpy(msglst->to, to);
#ifdef use_local /* see makecl() in forward.c */
if (stricmp(atbbs, "local")) { /* N5KNX: drop @atbbs if == "local" */
#endif
strcat(msglst->to, "@");
strcat(msglst->to, atbbs);
#ifdef use_local
}
#endif
free(to);
free(atbbs);
return 1;
}
// This code processes a FS packet from a remote system.
// Format is: FS ccccc where each c is a character:
// A '+' means the message is accepted.
// '-' means the message is rejected.
// '=' means the message is accepted but don't send it yet.
// deferred messages (=) are a real pain to process. :(
//
// RC = 0 means an error was detected.
// 1 means an 'FF' was received from the remote system.
// 2 means an 'Fx' (where x is something) was received.
static
int fbbdofs(struct fbbpacket *msglst,
struct fwd *f,
int msgcnt,
long *mindeferred,
long *maxdeferred,
int idx)
{
int i;
struct mbx *m;
#ifdef FBBCMP
FILE *tfile;
int rc;
int NotConnected = FALSE;
#endif
m = f->m;
// Send any data in our buffer.
usflush(m->user);
// Get the FS line.
alarm(Mbtdiscinit*1000L);
if(recvline (m->user, m->line, MBXLINE) == -1 ) {
#ifdef FBBVERBOSELOG
log(m->user, RmtDisconnect);
#endif
return 0;
}
alarm(0);
rip(m->line);
// Make sure we got a "FS " line. Anything else is an error.
if(strnicmp(m->line,"FS ", 3) != 0) {
tprintf("FBB Error: Expected 'FS ' string. Received \'%s\' string.\n",m->line);
log(m->user, "FBB error with %s: expected FS, got %s", m->name,m->line);
return 0;
}
// Check to see if we got the right number of responses.
if((strlen(m->line) != msgcnt + 3))
return 0;
// Validate FS line. Clean up unused entries.
for(i=0;i<FBBMAXMSGS;i++) {
if(i<msgcnt) {
if((m->line[3+i] != '+') &&
(m->line[3+i] != '-') &&
(m->line[3+i] != '=')) {
tprintf("FBB Error: Expected '+, -, or ='. Received \'%c\'.\n",m->line[3+i]);
log(m->user,"FBB error with %s: Expected +-=, got %c", m->name,m->line[3+i]);
return 0;
}
} else {
// Zero out and free rest of the FS structure.
msglst[i].accept = fbbUNKNOWN;
free(msglst[i].to);
free(msglst[i].rewrite_to);
free(msglst[i].from);
free(msglst[i].messageid);
free(msglst[i].sline);
msglst[i].to = \
msglst[i].rewrite_to= \
msglst[i].from = \
msglst[i].messageid = \
msglst[i].sline = NULLCHAR;
}
}
// The FS line is OK. Now we can send the '+' messages and delete the
// '-' messages. The '=' messages can be ignored for now.
for(i=0;i<msgcnt;i++) {
#ifdef FBBCMP
if(NotConnected)
break;
#endif
pwait(NULL);
rip(msglst[i].sline); // pretty up log.
MbForwarded++;
if(m->line[3+i] == '+') {
// Message is wanted. We need to send it.
// Don't update the X-Forwarded Header yet. FBB does not tell us
// if it received the message or not. We have to send all our
// messages and then see if the connect is still there. If we get
// a valid FBB Response, then we can assume that the messages were
// delivered and update the X-Forwarded flag.
msglst[i].accept = fbbYES;
// I don't like having to re-read the index here, but sendmsg()
// needs a lot of index info so it needs to be done.
// Re-position filepointer to index....
lseek(idx,m->mbox[msglst[i].number].indexoffset,SEEK_SET);
// Clear/Free the previous index
default_index(m->area,&f->ind);
// and read the index.
if (read_index(idx,&f->ind) == -1) return(0); /* should not happen */
// sendmsg() assumes that f->bid will hold the message id.
// msglst[i].bid was saved from the makecl() call.
strcpy(f->bid, msglst[i].bid);
#ifdef FBBCMP
if(f->m->sid & MBX_FBBCMP) {
// Open a tmpfile for fbbsendmsg to place
// the mssage data in. This file is not closed,
// but passed to send_yapp for encoding and
// transmitting.
strcpy(f->iFile, tmpnam(NULL));
if((tfile = fopen(f->iFile, "w+b")) == NULLFILE) {
log(m->user, "FBBFWD: fbbdofs: Error %d opening %s", errno, f->iFile);
return 0;
}
// Prepare the text of the message.
if (fbbsendmsg (f, msglst[i].number, tfile) == -1)
NotConnected = TRUE; /* some error preparing msg */
else {
strcpy(f->oFile, tmpnam(NULL));
rc = send_yapp(f->m->user, f, f->ind.subject); // Send it.
if(rc == 0) {
#ifdef FBBVERBOSELOG
log(m->user,"FBBFWD: send_yapp() error.");
#endif
NotConnected = TRUE;
}
}
// erase the input and output file.
unlink(f->iFile);
unlink(f->oFile);
} else
#endif /* FBBCMP */
{
// Send a subject line.
tprintf("%s\n",f->ind.subject);
// Send the text of the message.
if (sendmsg(f, msglst[i].number) == -1) { /* aborted */
default_index(m->area,&f->ind);
break;
}
// Send a ctrl-z ... that's OCTAL(32)
tputs("\032\n");
}
usflush(m->user);
// and finally free the index.
default_index(m->area,&f->ind);
} else
// The remote system doesn't want this message.
// It might be a NO (-) or it might be a LATER (=)
// Go ahead and mark the msg as so. If the link goes
// down, we still want to mark this message as unwanted.
// One the link goes back up, we don't want to ask the
// remote system again about this because it already
// told us no. This is handeled differently than '+'
// messages.
if(m->line[3+i] == '-') {
msglst[i].accept = fbbNO;
// mark message as forwarded or deleted.
if(m->areatype == AREA)
m->mbox[msglst[i].number].status |= BM_FORWARDED;
else
m->mbox[msglst[i].number].status |= BM_DELETE;
m->change = 1;
log(m->user,"MBOX bbs mail refused: %s", msglst[i].sline);
} else
// The remote system want's this message... later.... not now.
// To handle this... we ignore the message for now and we
// attempt to update the m->lastread message number to the
// first deferred message. That way the next time we transfer
// messages, we'll ask about this message again. BUT... during
// this tranfer sequence, we don't want to keep asking about
// the same message so we have to keep track of the highest
// deferred message.
// Anyway... the mindeferred counter is the FIRST deferred
// message we do and is used to update the m->lastread counter.
// The maxdeferred counter is the last deferred message.
if(m->line[3+i] == '=') {
msglst[i].accept = fbbDEFER;
// Re-position filepointer to index....
lseek(idx,m->mbox[msglst[i].number].indexoffset,SEEK_SET);
// Clear/Free the previous index
default_index(m->area,&f->ind);
// and read the index.
if(read_index(idx,&f->ind) == -1) return(0); /* should not happen */
// maxdeferred will point to the highest deferred msg we've
// processed.
if(*maxdeferred < f->ind.msgid)
*maxdeferred = f->ind.msgid;
// mindeferred will point to the first deferred msg we've
// processed.
if(*mindeferred == 0)
*mindeferred = f->ind.msgid;
// and finally free the index.
default_index(m->area,&f->ind);
}
} // end for
#ifdef FBBCMP
if(NotConnected)
return 0;
#endif
// Now we'll get a line from the remote system to make sure that the
// messages were received ok.
usflush(m->user);
alarm(Mbtdiscinit*1000L);
if(recvline(m->user,m->line,MBXLINE) == -1) {
// There was a problem. We did not get a response from the remote
// system after we sent our messages.
// We'll go ahead and delete the '-' messages. He didn't want them
// now, so he won't want them later.
#ifdef FBBVERBOSELOG
log(m->user, RmtDisconnect);
#endif
return 0;
} else {
alarm(0);
if (strncmp (m->line, "*** ", 4) == 0) { /* *** Protocole err, *** Checksum err, ... */
rip(m->line);
log (m->user, "FBB error from %s: %s", m->name, m->line);
return 0;
}
// We got a response back, so we can update our '+' messages.
for(i=0;i<FBBMAXMSGS;i++) {
if(msglst[i].accept == fbbYES) {
// mark message as sent.
if(m->areatype == AREA)
m->mbox[msglst[i].number].status |= BM_FORWARDED;
else
m->mbox[msglst[i].number].status |= BM_DELETE;
m->change = 1;
// For each valid note.
log(m->user,"MBOX bbs mail sent: %s ", msglst[i].sline);
free(msglst[i].to);
free(msglst[i].rewrite_to);
free(msglst[i].from);
free(msglst[i].messageid);
free(msglst[i].sline);
msglst[i].to = \
msglst[i].rewrite_to= \
msglst[i].from = \
msglst[i].messageid = \
msglst[i].sline = NULLCHAR;
}
}
}
// Change forward direction
m->state = MBX_REVFWD;
if(strnicmp(m->line,"FF", 2) == 0)
return 1;
else
return 2;
}
// This code receives the FB packet ( up to 5 messages ) from a FBB
// system. Once we get this FB Packet, we'll figure out which notes we
// want... send our response ( FS string ) and receive the messages.
/* Return codes for dofbbrecv:
0 => Error, unexpected data format in m->line,
or can't deliver mail. Disconnect required.
1 => All OK, msgs received and processed properly.
2 => FF received, remote BBS has no data for us.
3 => FQ received, remote BBS wants to quit.
*/
static
int dofbbrecv(struct fwd *f)
{
int rc;
int msgcnt;
int msgsize = 0;
int FBBok;
int FBBdone;
int i,err;
int FirstTime;
int NotConnected = FALSE;
unsigned int checksum;
char FBBresp[FBBMAXMSGS+4]; /* Must be FBBMAXMSGS+4 bytes */
char *cp;
struct fbbpacket *msglst;
struct mbx *m;
msglst = f->msglst;
m = f->m;
msgcnt = 0;
FBBdone = FALSE;
FirstTime = TRUE;
checksum = 0;
// Loop till we've received a F>, FF, FQ, ***, or 5 FB blocks.
for(;;) {
// If FirstTime = TRUE it means that we've (probably?) already gotten a
// line from the remote system and don't need to ask for it
// again.
if(!FirstTime || m->line[0] == 0) {
alarm(Mbtdiscinit*1000L);
if(recvline (m->user, m->line, MBXLINE) == -1) {
#ifdef FBBVERBOSELOG
log(m->user, RmtDisconnect);
#endif
break; // The connection has closed on us.
}
alarm(0);
}
FirstTime = FALSE;
pwait(NULL);
// Start off with FBBok = FALSE.
// If we get a FB or F> we'll reset it to TRUE and
// continue on. If we get something else we'll exit.
FBBok = FALSE;
// strip any trailing NL characters.
rip(m->line);
#ifdef notdef
// lowercase command.
cp = m->line;
while(*cp){
if(isupper(*cp))
*cp = tolower(*cp);
++cp;
}
#endif
if(strnicmp(m->line,"ff",2) == 0) {
// End of FBB Packet
return 2;
}
if(strnicmp(m->line,"fq",2) == 0) {
// End of FBB Packet
return 3;
}
if(strnicmp(m->line,"f>",2) == 0) {
if (strlen(m->line) > 3) { // Compare computed Checksum against supplied checksum.
checksum += htoi(skipwhite(m->line+2)); /* skip to cksum char(s) */
if (checksum & 0xFF) { /* invalid sum */
log(m->user, "FBBFWD: %s Proposals checksum err (%x)", m->name, checksum&0xFF);
break; /* FBBdone is FALSE, so we'll return code 0 => disconnect */
}
}
// End of FBB Packet
FBBdone = TRUE;
break;
}
if((strnicmp (m->line, "fa", 2) == 0) || (strnicmp (m->line, "fb", 2) == 0)) {
// Another message
if(msgcnt >= FBBMAXMSGS) {
// Too many in fact. We're only supposed to accept
// 5 (FBBMAXMSGS) msgs.
log(m->user, "FBBFWD: > 5 messages offered.");
break; /* we never accept more than 5 ! */
} else {
// add the data to the checksum count.
cp = m->line;
i=0; /* count fields in FA/FB proposal */
while (*cp) {
checksum += *cp & 0xff;
if (*cp == ' ') {
i++; /* we expect single blank delimiters */
if (*(cp+1) == ' ') /* 2 adjacent blanks are illegal */
i=-100; /* assure too-few-fields complaint */
}
cp++;
}
// Add a checksum count for the FBB style end-of-line marker.
// It doesn't uses a CR\LF but uses a CR instead.
checksum += '\r' & 0xff;
#ifdef FBBVERBOSELOG
log(m->user,"MBOX %s: %s",m->name,m->line); /* we don't log rec'd bulls for other-style BBSes */
#endif
// Convert the FBB message string to it's ax.25 counterpart.
if(i < 6) { /* too few fields? */
FBBerror(11,m->user);
break; /* with FBBdone at FALSE */
}
fbbparse(&msglst[msgcnt], m->line);
msgsize += msglst[msgcnt].size;
msgcnt++;
FBBok = TRUE;
}
}
// If we didn't reset the FBBok flag, something is wrong and we
// need to exit.
if(!FBBok) {
break;
}
}
// We've exited the FB....F> loop.
// If FBBdone = TRUE we're ok... otherwise we received bad data.
if((!FBBdone)) /* proposals error, etc. */
return 0;
// FB....F> data looks ok. Now process it and build our FS line.
strcpy(FBBresp, "FS ");
// Check each message in the list to see if we want it or not.
for(i=0;i<msgcnt;i++) {
pwait(NULL);
// first check the msgid. If that works, run it through the
// rewrite file.
if(msgidcheck(msglst[i].messageid)) {
// Nope... already have this one.
msglst[i].accept = fbbNO;
} else {
// Tentativly accept this... now check the validity of the address.
msglst[i].accept = fbbYES;
pwait(NULL);
free(msglst[i].rewrite_to);
msglst[i].rewrite_to = NULLCHAR;
if((cp = rewrite_address(msglst[i].to, REWRITE_TO)) != NULLCHAR) {
// See if this is on the reject list.
if(!strcmp(cp,"refuse")) {
// oops.. rejected...
msglst[i].accept = fbbNO;
free(cp);
} else {
// keep track of new, rewritten address. This is passed
// to the mboxmail dosend() code when we send the msg.
msglst[i].rewrite_to = cp;
}
}
// If we're still OK, make sure the address resolves ok.
if(msglst[i].accept == fbbYES) {
if(msglst[i].rewrite_to == NULLCHAR) {
if(validate_address(msglst[i].to) == 0){
msglst[i].accept = fbbNO;
}
} else {
if(validate_address(msglst[i].rewrite_to) == 0){
msglst[i].accept = fbbNO;
free(msglst[i].rewrite_to);
msglst[i].rewrite_to = NULLCHAR;
}
}
}
}
// By now.. we know if we want the message or not.... update the
// FBBResp string.
//
switch (msglst[i].accept) {
case fbbUNKNOWN:
log(m->user,"FBB Programming err: (%s) msglst.accept = Unknown", m->name);
FBBresp[i+3] = '-';
break;
case fbbNO:
FBBresp[i+3] = '-';
break;
case fbbYES:
FBBresp[i+3] = '+';
break;
case fbbDEFER:
// Currently, we don't defer but here it is anyway.
FBBresp[i+3] = '=';
break;
} /* endswitch */
}
// Add a null to the end of the string.
FBBresp[i+3] = '\0';
// Now we can send it.
tprintf("%s\n", FBBresp);
usflush(m->user);
#ifdef FBBVERBOSELOG
// Log FBB response only if really interested
log(m->user,"our FBB response is %s",FBBresp);
#endif
// The remote system will start sending the messages.
// Loop through the message list.
//
for(i=0;i<msgcnt;i++) {
if(NotConnected)
// We've lost the connection.....
break;
if(msglst[i].accept == fbbYES)
#ifdef FBBCMP
if (f->m->sid & MBX_FBBCMP) {
#ifdef FBBDEBUG2
log(m->user,"FBBFWD: Processing new FBB Compressed Message.");
#endif
// Receive the message and uncompress it.
// Setup input and output file names.
strcpy(f->iFile, tmpnam(NULL));
strcpy(f->oFile, tmpnam(NULL));
rc = recv_yapp(m->user, f, &m->subject, Mbtdiscinit*1000L);
if(rc == 0) {
#ifdef FBBVERBOSELOG
log(m->user,"FBBFWD: recv_yapp() error.");
#endif
NotConnected = TRUE;
// erase the input and output file.
unlink(f->iFile);
unlink(f->oFile);
} else {
// Process the message.
free(m->to);
free(m->tofrom);
free(m->tomsgid);
free(m->origto);
free(m->origbbs);
free(m->date);
// m->to = \
// m->tofrom = \
// m->tomsgid = \
m->origto = \
m->origbbs = \
m->date = NULLCHAR;
// // m->subject has the subject so don't free it.
// free(m->subject);
// m->subject = NULLCHAR;
if (msglst[i].rewrite_to != NULLCHAR) {
strlwr(m->to = strdup(msglst[i].rewrite_to));
strlwr(m->origto = strdup(msglst[i].to));
} else {
strlwr(m->to = strdup(msglst[i].to));
} /* end if */
strlwr(m->tomsgid = strdup(msglst[i].messageid));
strlwr(m->tofrom = strdup(msglst[i].from));
/* n5knx: Bad idea:strlwr(m->subject); */
m->stype = msglst[i].type;
rc = dofbbacceptnote(m, f->oFile);
#ifdef FBBVERBOSELOG
if(rc != 1)
log(m->user, "FBBFWD: dofbbacceptnote() failed.");
#endif
// erase the input and output files.
unlink(f->iFile);
unlink(f->oFile);
free(m->to);
free(m->tofrom);
free(m->tomsgid);
free(m->origto);
free(m->origbbs);
free(m->subject);
free(m->date);
m->to = \
m->tofrom = \
m->tomsgid = \
m->origto = \
m->origbbs = \
m->subject = \
m->date = NULLCHAR;
#ifdef FBBDEBUG2
log(m->user,"FBBFWD: Finished w/ FBB Compressed Message.");
#endif
}
} else
#endif /* FBBCMP */
{
pwait(NULL);
sprintf(m->line, "S%c %s < %s $%s_%s->%s",
msglst[i].type,
msglst[i].to,
msglst[i].from,
"fbbbid",
msglst[i].rewrite_to,
msglst[i].messageid);
// Call mbx_parse to parse the send command.
err=mbx_parse(m);
// Close any open files left from an error.
if (m->tfile) {
fclose(m->tfile);
m->tfile = NULLFILE;
}
if(m->tfp) {
fclose(m->tfp);
m->tfp = NULLFILE;
}
if(err == -2) return(0); /*N5KNX: error while processing msg, no choice but to disconnect */
}
}
#ifdef FBBDEBUG
if(Mtrace)
PUTS("FBBRECV: We've sent our data.");
#endif
if(NotConnected)
// We got disconneted... tell the caller.
return 0;
else
return 1;
}