-
Notifications
You must be signed in to change notification settings - Fork 24
/
ftnq.c
1153 lines (1033 loc) · 27 KB
/
ftnq.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
/*
* ftnq.c -- BSO methods implementation
*
* ftnq.c is a part of binkd project
*
* Copyright (C) 1996-1997 Dima Maloff, 5047/13
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. See COPYING.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "sys.h"
#include "readcfg.h"
#include "ftnq.h"
#include "ftnnode.h"
#include "ftnaddr.h"
#include "tools.h"
#include "readdir.h"
#include "iphdr.h"
#ifdef WITH_PERL
#include "perlhooks.h"
#endif
const char prio[] = "IiCcDdOoFfHh";
static const char flo_flvrs[] = "icdfhICDFH";
static const char out_flvrs[] = "icdohICDOH";
static FTNQ *q_add_dir (FTNQ *q, char *dir, FTN_ADDR *fa1, BINKD_CONFIG *config);
FTNQ *q_add_file (FTNQ *q, char *filename, FTN_ADDR *fa1, char flvr, char action, char type, BINKD_CONFIG *config);
/*
* q_free(): frees memory allocated by q_scan()
*/
static int qn_free (FTN_NODE *fn, void *arg)
{
UNUSED_ARG(arg);
fn->hold_until = 0;
fn->mail_flvr = fn->files_flvr = 0;
fn->busy = 0;
return 0;
}
void q_free (FTNQ *q, BINKD_CONFIG *config)
{
if (q != SCAN_LISTED)
{
FTNQ *last = 0;
while (q)
{
last = q;
q = q->next;
}
while (last)
{
if (!last->prev)
{
free (last);
break;
}
else
{
last = last->prev;
xfree (last->next);
}
}
}
else
foreach_node (qn_free, 0, config);
}
/*
* q_scan: scans outbound. Return value must be q_free()'d.
*/
/* Need to pass two parameters: queue and config */
struct qn_scan_params
{
FTNQ **pq;
BINKD_CONFIG *config;
};
static int qn_scan (FTN_NODE *fn, void *arg)
{
struct qn_scan_params *params = arg;
*(params->pq) = q_scan_boxes (*(params->pq), &fn->fa, 1, 1, params->config);
return 0;
}
FTNQ *q_scan (FTNQ *q, BINKD_CONFIG *config)
{
char *s;
char buf[MAXPATHLEN + 1], outb_path[MAXPATHLEN + 1];
FTN_DOMAIN *curr_domain;
struct qn_scan_params qn_params;
for (curr_domain = config->pDomains.first; curr_domain; curr_domain = curr_domain->next)
{
DIR *dp;
struct dirent *de;
int len;
if (curr_domain->alias4 == 0)
{
strnzcpy (outb_path, curr_domain->path, sizeof (buf) - 1);
/* `FOO:/bar means FOO:\..\bar on Amiga */
#ifndef AMIGA
if (outb_path[strlen (outb_path) - 1] == ':')
strcat (outb_path, PATH_SEPARATOR);
#endif
if ((dp = opendir (outb_path)) == 0)
{
Log (1, "cannot opendir %s: %s", outb_path, strerror (errno));
continue;
}
len = strlen (curr_domain->dir);
strnzcpy (buf, curr_domain->path, sizeof (buf));
strnzcpy (buf + strlen (buf), PATH_SEPARATOR, sizeof (buf) - strlen (buf));
s = buf + strlen (buf);
while ((de = readdir (dp)) != 0)
{
if (!STRNICMP (de->d_name, curr_domain->dir, len) &&
(de->d_name[len] == 0 ||
(de->d_name[len] == '.' && isxdigit (de->d_name[len + 1]))))
{
FTN_ADDR fa;
FA_ZERO (&fa);
#ifdef AMIGADOS_4D_OUTBOUND
if (!config->aso)
#endif
fa.z = ((de->d_name[len] == '.') ?
strtol (de->d_name + len + 1, (char **) NULL, 16) :
curr_domain->z[0]);
if (de->d_name[len] == 0 || fa.z != curr_domain->z[0])
{
strcpy (fa.domain, curr_domain->name);
strnzcpy (buf + strlen (buf), de->d_name, sizeof (buf) - strlen (buf));
q = q_add_dir (q, buf, &fa, config);
}
*s = 0;
}
}
closedir (dp);
}
}
qn_params.pq = &q;
qn_params.config = config;
foreach_node (qn_scan, &qn_params, config);
return q;
}
/*
* Adds to the q all files for n akas stored in fa
*/
FTNQ *q_scan_addrs (FTNQ *q, FTN_ADDR *fa, int n, int to, BINKD_CONFIG *config)
{
char buf[MAXPATHLEN + 1];
int i;
char *s;
for (i = 0; i < n; ++i)
{
if (!to && config->send_if_pwd)
{
/* do not give unsecure mail even to secure link when send-if-pwd */
FTN_NODE *fn = get_node_info (fa + i, config);;
if (fn == NULL || strcmp(fn->pwd, "-") == 0)
continue;
}
ftnaddress_to_filename (buf, fa + i, config);
if (*buf)
{
if ((s = last_slash(buf)) != 0)
{
*s = 0;
q = q_add_dir (q, buf, fa + i, config);
}
}
}
q = q_scan_boxes (q, fa, n, to, config);
return q;
}
static int weight_by_name (char *s)
{
if (ispkt (s))
return 100;
if (isarcmail (s))
return 50;
if (istic (s))
return -100;
return 0;
}
static int cmp_filebox_files (FTNQ *a, FTNQ *b)
{
int w_a = weight_by_name (a->path);
int w_b = weight_by_name (b->path);
if (w_a - w_b == 0)
return a->time - b->time;
else
return w_b - w_a;
}
#ifdef MAILBOX
static char to32(int N)
{
if ((N >= 0) && (N <=9)) return '0'+N;
if ((N > 9) && (N < 32)) return 'A'+ N - 10;
return 'Z'; /* fake return */
}
static struct {
char * ext;
char flv;
} brakeExt[] =
{
#ifdef UNIX
{"immediate", 'i'},
{"crash", 'c'},
{"direct", 'd'},
{"normal", 'f'},
{"hold", 'h'},
#endif
{"Immediate", 'i'},
{"Crash", 'c'},
{"Direct", 'd'},
{"Normal", 'f'},
{"Hold", 'h'}
};
#endif
static FTNQ *q_scan_box (FTNQ *q, FTN_ADDR *fa, char *boxpath, char flvr, int deleteempty, BINKD_CONFIG *config)
{
int n_files = 0;
DIR *dp;
char buf[MAXPATHLEN + 1], *s;
struct dirent *de;
struct stat sb;
strnzcpy (buf, boxpath, sizeof (buf));
strnzcat (buf, PATH_SEPARATOR, sizeof (buf));
#ifdef UNIX
if (access(boxpath, R_OK | W_OK) != 0) {
if (access(boxpath, F_OK) == 0)
Log (1, "No access to filebox `%s'", boxpath);
return q;
}
#endif
s = buf + strlen (buf);
if ((dp = opendir (boxpath)) != NULL)
{
while ((de = readdir (dp)) != 0)
{
sb.st_mtime = 0; /* ??? val: don't know how to get it if stat() isn't used */
strnzcat (buf, de->d_name, sizeof (buf));
if (de->d_name[0] != '.'
#if defined(_MSC_VER) || defined(DOS) && !defined(DJGPP)
&& (de->d_attrib & 0x1a) == 0 /* not hidden, directory or volume label */
#elif defined(OS2) && !defined(IBMC) && !defined(__WATCOMC__)
&& (de->d_attr & 0x1a) == 0 /* not hidden, directory or volume label */
#elif defined(__FreeBSD__)
&& (DTTOIF(de->d_type) & S_IFDIR) == 0 /* not directory */
#else
&& stat(buf, &sb) == 0 && (sb.st_mode & S_IFDIR) == 0 /* not directory */
#endif
)
{
q = q_add_file (q, buf, fa, flvr, 'd', 0, config);
n_files++;
}
*s = 0;
}
closedir (dp);
if (n_files == 0 && deleteempty) {
if (rmdir (boxpath) == 0)
Log (3, "Empty filebox %s deleted", boxpath);
else
Log (1, "Cannot delete empty filebox %s: %s", boxpath, strerror (errno));
}
}
return q;
}
/*
* Scans fileboxes for n akas stored in fa
*/
FTNQ *q_scan_boxes (FTNQ *q, FTN_ADDR *fa, int n, int to, BINKD_CONFIG *config)
{
FTN_NODE *node;
int i;
#ifdef MAILBOX
char buf[MAXPATHLEN + 1];
char *s;
int j;
#endif
for (i = 0; i < n; ++i)
{
node = get_node_info (fa + i, config);
if (!to && config->send_if_pwd)
{
/* do not give unsecure mail even to secure link when send-if-pwd */
if (node == NULL || strcmp(node->pwd, "-") == 0)
continue;
}
#ifndef MAILBOX
if (node && node->obox)
{
q = q_scan_box (q, fa+i, node->obox, node->obox_flvr, 0, config);
}
#else
if ((node && node->obox) || config->tfilebox[0] || config->bfilebox[0])
{
if (node && node->obox)
q = q_scan_box (q, fa+i, node->obox, node->obox_flvr, 0, config);
if (config->bfilebox[0]) {
strnzcpy (buf, config->bfilebox, sizeof (buf));
strnzcat (buf, PATH_SEPARATOR, sizeof (buf));
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
"%s.%u.%u.%u.%u.",
fa[i].domain,
fa[i].z,
fa[i].net,
fa[i].node,
fa[i].p);
s = buf + strlen(buf);
for (j = 0; j < sizeof(brakeExt)/sizeof(brakeExt[0]); j++) {
strnzcat (buf, brakeExt[j].ext, sizeof (buf));
q = q_scan_box (q, fa+i, buf, brakeExt[j].flv, config->deleteablebox, config);
*s = '\0';
}
}
if (config->tfilebox[0]) {
strnzcpy ( buf, config->tfilebox, sizeof (buf));
strnzcat ( buf, PATH_SEPARATOR, sizeof (buf));
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
"%u.%u.%u.%u",
fa[i].z,
fa[i].net,
fa[i].node,
fa[i].p);
q = q_scan_box (q, fa+i, buf, 'f', config->deleteablebox, config);
strnzcat ( buf, ".H", sizeof (buf));
#ifdef UNIX
{ struct stat st;
if (stat(buf, &st) != 0 || (st.st_mode & S_IFDIR) == 0)
buf[strlen(buf) - 1] = 'h';
}
#endif
q = q_scan_box (q, fa+i, buf, 'h', config->deleteablebox, config);
strnzcpy ( buf, config->tfilebox, sizeof (buf));
strnzcat ( buf, PATH_SEPARATOR, sizeof (buf));
snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
"%c%c%c%c%c%c%c%c.%c%c",
to32(fa[i].z/32), to32(fa[i].z%32),
to32(fa[i].net/1024), to32((fa[i].net/32)%32), to32(fa[i].net%32),
to32(fa[i].node/1024),to32((fa[i].node/32)%32),to32(fa[i].node%32),
to32(fa[i].p/32), to32(fa[i].p%32));
q = q_scan_box (q, fa+i, buf, 'f', config->deleteablebox, config);
strnzcat (buf, "H", sizeof (buf));
#ifdef UNIX
{ struct stat st;
if (stat(buf, &st) != 0 || (st.st_mode & S_IFDIR) == 0)
buf[strlen(buf) - 1] = 'h';
}
#endif
q = q_scan_box (q, fa+i, buf, 'h', config->deleteablebox, config);
}
}
#endif
}
return q;
}
void process_hld (FTN_ADDR *fa, char *path, BINKD_CONFIG *config)
{
FTN_NODE *node;
long hold_until_tmp;
if ((node = get_node_info(fa, config)) != NULL)
{
FILE *f;
if ((f = fopen (path, "r")) == NULL ||
fscanf (f, "%ld", &hold_until_tmp) != 1)
{
node->hold_until = 0;
}
else
{
node->hold_until = (time_t)hold_until_tmp;
}
if (f)
fclose (f);
if (node->hold_until <= safe_time())
{
node->hold_until = 0;
delete (path);
}
}
}
static void process_bsy (FTN_ADDR *fa, char *path, BINKD_CONFIG *config)
{
char *s = path + strlen (path) - 4;
FTN_NODE *node;
struct stat sb;
if (stat (path, &sb) == 0 && config->kill_old_bsy != 0
&& time (0) - sb.st_mtime > config->kill_old_bsy)
{
char buf[FTN_ADDR_SZ + 1];
ftnaddress_to_str (buf, fa);
Log (2, "found old %s file for %s", s, buf);
delete (path);
}
else
{
if ((node = get_node_info (fa, config)) != 0 && node->busy != 'b' &&
(!STRICMP (s, ".bsy") || !STRICMP (s, ".csy")))
{
node->busy = tolower (s[1]);
}
}
}
/*
* Adds files from outbound directory _dir_ to _q_. _fa1_ is
* the address wildcard for this outbound. E.g.
* c:\bbs\outbound\ fa1 = 2:-1/-1.-1@fidonet
* or
* c:\bbs\outbound\00030004.pnt\ fa1 = 2:3/4.-1@fidonet
* or even
* c:\bbs\outbound\00030004.pnt\ fa1 = 2:3/4.5@fidonet
*/
static FTNQ *q_add_dir (FTNQ *q, char *dir, FTN_ADDR *fa1, BINKD_CONFIG *config)
{
DIR *dp;
FTN_ADDR fa2;
char buf[MAXPATHLEN + 1];
int j;
char *s;
if ((dp = opendir (dir)) != 0)
{
struct dirent *de;
while ((de = readdir (dp)) != 0)
{
#ifdef AMIGADOS_4D_OUTBOUND
if (config->aso)
{
char ext[4];
int matched = 0;
size_t nlen = strlen(s = de->d_name);
for (; *s && isgraph(*s) != 0; s++);
if ((size_t)(s - de->d_name) != nlen)
continue;
memcpy (&fa2, fa1, sizeof(FTN_ADDR));
if (sscanf(s = de->d_name, "%u.%u.%u.%u.%3s%n",
(unsigned*)(&fa2.z), (unsigned*)(&fa2.net), (unsigned*)(&fa2.node),
(unsigned*)(&fa2.p), ext, &matched) != 5 ||
(size_t)matched != nlen || strlen(ext) != 3)
continue;
if ((fa1->z != -1 && fa1->z != fa2.z) ||
(fa1->net != -1 && fa1->net != fa2.net) ||
(fa1->node != -1 && fa1->node != fa2.node) ||
(fa1->p != -1 && fa1->p != fa2.p))
continue;
strnzcpy(buf, dir, sizeof(buf));
strnzcpy(buf + strlen(buf), PATH_SEPARATOR, sizeof(buf) - strlen(buf));
strnzcpy(buf + strlen(buf), s, sizeof(buf) - strlen(buf));
if (!STRICMP(ext, "bsy") || !STRICMP(ext, "csy"))
process_bsy(&fa2, buf, config);
if (!get_node_info(&fa2, config) && !is5D(fa1))
continue;
if (strchr(out_flvrs, ext[0]) &&
tolower(ext[1]) == 'u' && tolower(ext[2]) == 't')
/* Adding *.?ut */
q = q_add_file(q, buf, &fa2, ext[0], 'd', 'm', config);
else if (!STRICMP(ext, "req"))
/* Adding *.req */
q = q_add_file(q, buf, &fa2, 'h', 's', 'r', config);
else if (!STRICMP(ext, "hld"))
process_hld(&fa2, buf, config);
else if (strchr(flo_flvrs, ext[0]) &&
tolower(ext[1]) == 'l' && tolower(ext[2]) == 'o')
/* Adding *.?lo */
q = q_add_file(q, buf, &fa2, ext[0], 'd', 'l', config);
else if (!STRICMP (s + 9, "stc"))
{
/* Adding *.stc */
q = q_add_file (q, buf, &fa2, 'h', 0, 's', config);
}
}
else
#endif /* AMIGADOS_4D_OUTBOUND */
{
s = de->d_name;
for (j = 0; j < 8; ++j)
if (!isxdigit (s[j]))
break;
if (j != 8 || strlen(s) != 12 || s[8] != '.' || strchr(s+9, '.'))
continue;
/* fa2 will store dest.address for the current (de->d_name) file */
memcpy (&fa2, fa1, sizeof (FTN_ADDR));
if (fa1->node != -1 && fa1->p != 0)
sscanf (s, "%8x", (unsigned *)&fa2.p); /* We now in /xxxxyyyy.pnt */
else
sscanf (s, "%4x%4x", (unsigned *)&fa2.net, (unsigned *)&fa2.node);
/* add the file if wildcard (f1) match the address (fa2) */
if (fa1->node != -1 && fa1->p != -1 && ftnaddress_cmp (fa1, &fa2))
continue;
strnzcpy (buf, dir, sizeof (buf));
strnzcpy (buf + strlen (buf), PATH_SEPARATOR,
sizeof (buf) - strlen (buf));
strnzcpy (buf + strlen (buf), s, sizeof (buf) - strlen (buf));
if (!STRICMP (s + 9, "pnt") && fa2.p == -1)
{
struct stat sb;
if (stat (buf, &sb) == 0 && sb.st_mode & S_IFDIR)
q = q_add_dir (q, buf, &fa2, config);
continue;
}
if (fa2.p == -1)
fa2.p = 0;
if (!STRICMP (s + 9, "bsy") || !STRICMP (s + 9, "csy"))
process_bsy (&fa2, buf, config);
if (!config->havedefnode && !get_node_info (&fa2, config) && !is5D (fa1))
continue;
if (strchr (out_flvrs, s[9]) &&
tolower (s[10]) == 'u' && tolower (s[11]) == 't')
{
/* Adding *.?ut */
q = q_add_file (q, buf, &fa2, s[9], 'd', 'm', config);
}
else if (!STRICMP (s + 9, "req"))
{
/* Adding *.req */
q = q_add_file (q, buf, &fa2, 'h', 's', 'r', config);
}
else if (!STRICMP (s + 9, "hld"))
process_hld (&fa2, buf, config);
else if (strchr (flo_flvrs, s[9]) &&
tolower (s[10]) == 'l' && tolower (s[11]) == 'o')
{
/* Adding *.?lo */
q = q_add_file (q, buf, &fa2, s[9], 'd', 'l', config);
}
else if (!STRICMP (s + 9, "stc"))
{
/* Adding *.stc */
q = q_add_file (q, buf, &fa2, 'h', 0, 's', config);
}
}
}
closedir (dp);
}
else
Log (1, "cannot opendir %s: %s", dir, strerror (errno));
return q;
}
/*
* Add a file to the queue.
*/
FTNQ *q_add_file (FTNQ *q, char *filename, FTN_ADDR *fa1, char flvr, char action, char type, BINKD_CONFIG *config)
{
const int argc=3;
char *argv[3];
char str[MAXPATHLEN+1];
SHARED_CHAIN * chn;
FTN_ADDR_CHAIN * fcn;
/* If filename for shared address was scanned,
* call this functions for all nodes for this
* sharea address (assumed, that recursively
* shared aka impossible due to user brains :))
* Recursion in shared aka definitions imply
* infinite recursion in this function -
* please, be careful!
*/
for(chn = config->shares.first;chn;chn = chn->next)
{
if (ftnaddress_cmp(fa1,&chn->sha) == 0)
{
for(fcn = chn->sfa.first; fcn; fcn = fcn->next)
{
q_add_file(q,filename,&fcn->fa,flvr,action,type, config);
}
}
}
if (q != SCAN_LISTED)
{
FTNQ *new_file;
if (type == 's')
{ char *p;
FILE *f;
f = fopen(filename, "r");
if (f == NULL)
{ Log(1, "Can't open %s: %s", filename, strerror(errno));
return q;
}
if (!fgets(str, sizeof(str), f))
{ Log(1, "Incorrect status (can't fgets), ignored");
fclose(f);
return q;
}
fclose(f);
if (*str && isspace(*str))
{ Log(1, "Incorrect status (space first), ignored");
return q;
}
for (p=str+strlen(str)-1; isspace(*p); *p--='\0');
Log(2, "Status is '%s'", str);
if (!parse_args (argc, argv, str, "Status"))
{ Log(1, "Incorrect status, ignored");
return q;
}
}
new_file = xalloc (sizeof (FTNQ));
FQ_ZERO (new_file);
new_file->next = q;
if (q)
q->prev = new_file;
q = new_file;
if (fa1)
memcpy (&q->fa, fa1, sizeof (FTN_ADDR));
q->flvr = flvr;
q->action = action;
q->type = type;
q->sent = 0;
if (type == 's')
{ q->size = (boff_t) strtoumax(argv[1], NULL, 10);
q->time = safe_atol(argv[2], NULL);
strnzcpy (q->path, argv[0], MAXPATHLEN);
}
else
strnzcpy (q->path, filename, MAXPATHLEN);
}
else
{
FTN_NODE *node;
if ((node = get_node_info (fa1, config)) != NULL)
{
if (type == 'm')
node->mail_flvr = MAXFLVR (flvr, node->mail_flvr);
else
node->files_flvr = MAXFLVR (flvr, node->files_flvr);
}
}
return q;
}
/*
* Add a file to the end of queue.
*/
FTNQ *q_add_last_file (FTNQ *q, char *filename, FTN_ADDR *fa1, char flvr, char action, char type, BINKD_CONFIG *config)
{
FTNQ *new_file, *pq;
new_file = q_add_file (NULL, filename, fa1, flvr, action, type, config);
if (new_file == NULL) return q;
if (q == NULL) return new_file;
for (pq = q; pq->next; pq = pq->next);
new_file->prev = pq;
pq->next = new_file;
return q;
}
/*
* q_list: just lists q, not more
*/
typedef struct
{
int first_pass;
FILE *out;
} qn_list_arg;
static int qn_list (FTN_NODE *fn, void *arg)
{
char tmp[60], buf[FTN_ADDR_SZ + 1];
qn_list_arg *a = (qn_list_arg *) arg;
if (fn->mail_flvr || fn->files_flvr || fn->busy)
{
if (fn->hold_until > 0)
{
struct tm tm;
safe_localtime (&fn->hold_until, &tm);
strftime (tmp, sizeof (tmp), " (hold until %Y/%m/%d %H:%M:%S)", &tm);
}
else
*tmp = 0;
ftnaddress_to_str (buf, &fn->fa);
fprintf (a->out, "%c %c%c %s%s%s\n",
a->first_pass ? '$' : ' ', fn->mail_flvr ? fn->mail_flvr : '-',
fn->files_flvr ? fn->files_flvr : '-', buf, tmp,
fn->busy ? (fn->busy == 'c' ? "\tcall" : "\tbusy") : "");
if (a->first_pass)
a->first_pass = 0;
}
return 0;
}
void q_list (FILE *out, FTNQ *q, BINKD_CONFIG *config)
{
char buf[FTN_ADDR_SZ + 1];
if (q == SCAN_LISTED)
{
qn_list_arg qnla;
qnla.first_pass = 1;
qnla.out = out;
foreach_node (qn_list, &qnla, config);
}
else
{
for (; q; q = q->next)
{
if (!q->sent)
{
ftnaddress_to_str (buf, &q->fa);
fprintf (out, "%-20s %c%c%c %8" PRIuMAX " %s\n",
buf, q->flvr, q->action ? q->action : '-',
q->type ? q->type : '-',
(uintmax_t)q->size, q->path);
}
}
}
}
static int q_cmp(FTNQ *a, FTNQ *b, FTN_ADDR *fa, int nAka)
{
int i;
/* 1. Do not sort sent files, move its to the end of queue */
if (a->sent || b->sent)
return b->sent - a->sent;
/* 2. Compare AKA */
if (!ftnaddress_cmp (&a->fa, &b->fa)) {
if (FA_ISNULL(&a->fa)) return -1;
if (FA_ISNULL(&b->fa)) return 1;
for (i = 0; i < nAka; i++) {
if (!ftnaddress_cmp (&a->fa, fa + i)) return -1;
if (!ftnaddress_cmp (&b->fa, fa + i)) return 1;
}
/* Files for different unknown akas? Hmm... */
}
/* 3. Compare status */
if (a->type != b->type) {
char typeorder[] = { 's', 'r', 'm', 'l', 'd' };
for (i = 0; i < sizeof(typeorder)/sizeof(typeorder[0]); i++) {
if (a->type == typeorder[i]) return -1;
if (b->type == typeorder[i]) return 1;
}
/* Different unknown types? Hmm... */
}
/* 4. Compare files in filebox */
if (a->type == 'd' && b->type == 'd') {
return cmp_filebox_files(a, b);
}
/* No differences */
return 0;
}
FTNQ *q_sort (FTNQ *q, FTN_ADDR *fa, int nAka, BINKD_CONFIG *cfg)
{
/*
* InsertSort
* You're free to improve this function, replace it with
* quick/merge/heap or any other effective sorting algorithm
*/
FTNQ *head, *tail, *qnext, *cur;
if (q == NULL) return q;
qnext = q->next;
head = tail = q;
q->next = NULL;
while ((q = qnext)) {
qnext = q->next;
/* insert q into new queue */
for (cur = head; cur; cur = cur->next) {
if (q_cmp(cur, q, fa, nAka) > 0)
break;
}
q->next = cur;
if (cur) {
q->prev = cur->prev;
if (cur->prev)
cur->prev->next = q;
else
head = q;
cur->prev = q;
} else {
q->prev = tail;
tail->next = q;
tail = q;
}
}
return head;
}
/*
* Selects from q the next file for fa (Returns a pointer to a q element)
*/
FTNQ *select_next_file (FTNQ *q, FTN_ADDR *fa, int nAka)
{
FTNQ *curr;
for (curr = q; curr; curr = curr->next) {
if (!curr->sent) { curr->sent = 1; return curr; }
}
return NULL;
}
/*
* get size of files in the queue
*/
void q_get_sizes (FTNQ *q, uintmax_t *netsize, uintmax_t *filessize)
{
FTNQ *curr;
struct stat st;
char *p;
*netsize = *filessize = 0;
for (curr = q; curr; curr = curr->next)
{
if (curr->type == 'l')
{ FILE *f;
char str[MAXPATHLEN+2];
if (curr->size)
*filessize += curr->size;
else if ((f = fopen(curr->path, "r")) != NULL)
{
curr->size = 0;
curr->time = 0;
while (fgets (str, sizeof(str), f))
{
if (*str == '~' || *str == '$') continue;
if ((p = strchr(str, '\n')) != NULL) *p='\0';
p=str;
if (*str == '#' || *str == '^') p++;
if (stat(p, &st) == 0) {
*filessize += st.st_size;
curr->size += st.st_size;
if (st.st_mtime > curr->time) curr->time = st.st_mtime;
}
}
fclose(f);
}
}
else if (curr->type == 's')
*filessize += curr->size;
else
{
if (curr->size == 0)
{
if (stat(curr->path, &st) == 0) {
curr->size = st.st_size;
curr->time = st.st_mtime;
}
}
*(curr->type == 'm' ? netsize : filessize) += curr->size;
}
}
}
/*
* Calculate quantity of freqs in the queue
*/
int q_freq_num (FTNQ *q)
{
FTNQ *curr;
int freqs = 0;
for (curr = q; curr; curr = curr->next)
if (curr->type == 'r')
freqs++;
return freqs;
}
/*
* q_not_empty () == 0: the queue is empty.
*/
typedef struct
{
int maxflvr;
FTN_NODE *fn;
} qn_not_empty_arg;
static int qn_not_empty (FTN_NODE *fn, void *arg)
{
qn_not_empty_arg *a = (qn_not_empty_arg *) arg;
if (!fn->busy && strcmp (fn->hosts, "-") && fn->hold_until < safe_time())
{
if (a->maxflvr != MAXFLVR (fn->mail_flvr, MAXFLVR (fn->files_flvr, a->maxflvr)))
{
a->maxflvr = MAXFLVR (fn->mail_flvr, fn->files_flvr);
a->fn = fn;
}
}
return 0;
}
FTN_NODE *q_not_empty (BINKD_CONFIG *config)
{
qn_not_empty_arg arg;
arg.maxflvr = 0;
arg.fn = 0;
foreach_node (qn_not_empty, &arg, config);
if (arg.maxflvr && tolower (arg.maxflvr) != 'h')
return arg.fn;
else
return 0;
}
FTN_NODE *q_next_node (BINKD_CONFIG *config)
{
FTN_NODE *fn = q_not_empty (config);
if (fn == 0)
return 0;
else
{
fn->mail_flvr = fn->files_flvr = 0;
fn->busy = 'c';
return fn;
}
}
/*
* Creates an empty .?lo
*/
int create_poll (FTN_ADDR *fa, int flvr, BINKD_CONFIG *config)
{
char buf[MAXPATHLEN + 1];
char ext[5];
int rc = 0;
struct stat st;
strcpy (ext, ".flo");