-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsuck.c
2330 lines (2159 loc) · 68.9 KB
/
suck.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
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef HAVE_DIRENT_H
# include <dirent.h>
#else
# ifdef HAVE_SYS_NDIR_H
# include <sys/ndir.h>
# endif
# ifdef HAVE_SYS_DIR_H
# include <sys/dir.h>
# endif
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#include <string.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/stat.h>
#include <math.h>
#include <errno.h>
#include <ctype.h>
#ifdef DMALLOC
#include <dmalloc.h>
#endif
#include "suck_config.h"
#include "both.h"
#include "suck.h"
#include "suckutils.h"
#include "dedupe.h"
#include "phrases.h"
#include "killfile.h"
#include "timer.h"
#include "active.h"
#include "batch.h"
#include "xover.h"
#include "db.h"
#ifdef MYSIGNAL
#include <signal.h>
#endif
#ifdef CHECK_HISTORY
#include "chkhistory.h"
#endif
/* function prototypes */
int get_articles(PMaster);
int get_one_article(PMaster, int, long);
int do_supplemental(PMaster);
int restart_yn(PMaster);
int scan_args(PMaster, int, char *[]);
void do_cleanup(PMaster);
int do_authenticate(PMaster);
void load_phrases(PMaster);
void free_phrases(void);
int parse_args(PMaster, int, char *[]);
int get_group_number(PMaster, char *);
int do_supplemental(PMaster);
int do_sup_bynr(PMaster, char *);
int do_nodownload(PMaster);
#ifdef MYSIGNAL
RETSIGTYPE sighandler(int);
void pause_signal(int, PMaster);
enum {PAUSE_SETUP, PAUSE_DO};
/*------------------------------------------*/
int GotSignal = FALSE;
/* the only static variable allowed, it's */
/* the only graceful way to handle a signal */
/*------------------------------------------*/
#endif
/* set up for phrases */
char **both_phrases=default_both_phrases;
char **suck_phrases=default_suck_phrases;
char **timer_phrases=default_timer_phrases;
char **chkh_phrases=default_chkh_phrases;
char **dedupe_phrases=default_dedupe_phrases;
char **killf_reasons=default_killf_reasons;
char **killf_phrases=default_killf_phrases;
char **killp_phrases=default_killp_phrases;
char **sucku_phrases=default_sucku_phrases;
char **active_phrases=default_active_phrases;
char **batch_phrases=default_batch_phrases;
char **xover_phrases=default_xover_phrases;
char **xover_reasons=default_xover_reasons;
enum { STATUS_STDOUT, STATUS_STDERR };
enum { RESTART_YES, RESTART_NO, RESTART_ERROR };
enum {
ARG_NO_MATCH, ARG_ALWAYS_BATCH, ARG_BATCH_INN, ARG_BATCH_RNEWS, ARG_BATCH_LMOVE, \
ARG_BATCH_INNFEED, ARG_BATCH_POST, ARG_CLEANUP, ARG_DIR_TEMP, ARG_DIR_DATA, ARG_DIR_MSGS, \
ARG_DEF_ERRLOG, ARG_HOST, ARG_NO_POSTFIX, ARG_LANGUAGE, ARG_MULTIFILE, ARG_POSTFIX, \
ARG_QUIET, ARG_RNEWSSIZE, ARG_DEF_STATLOG, ARG_WAIT_SIG, ARG_ACTIVE, ARG_RECONNECT, \
ARG_DEBUG, ARG_ERRLOG, ARG_HISTORY, ARG_KILLFILE, ARG_KLOG_NONE, ARG_KLOG_SHORT, \
ARG_KLOG_LONG, ARG_MODEREADER, ARG_PORTNR, ARG_PASSWD, ARG_RESCAN, ARG_STATLOG, \
ARG_USERID, ARG_VERSION, ARG_WAIT, ARG_LOCALHOST, ARG_TIMEOUT, ARG_NRMODE, ARG_AUTOAUTH, \
ARG_NODEDUPE, ARG_NO_CHK_MSGID, ARG_READACTIVE, ARG_PREBATCH, ARG_SKIP_ON_RESTART, \
ARG_KLOG_NAME, ARG_USEGUI, ARG_XOVER, ARG_CONN_DEDUPE, ARG_POST_FILTER, ARG_CONN_ACTIVE, \
ARG_HIST_FILE, ARG_HEADER_ONLY, ARG_ACTIVE_LASTREAD, ARG_USEXOVER, ARG_RESETCOUNTER, \
ARG_LOW_READ, ARG_SHOW_GROUP, ARG_USE_SSL, ARG_LOCAL_SSL, ARG_BATCH_POST_NR, \
ARG_PASSWD_ENV,
};
typedef struct Arglist{
const char *sarg;
const char *larg;
int nr_params;
int flag;
int errmsg; /* this is an index into suck_phrases */
} Args, *Pargs;
const Args arglist[] = {
{"a", "always_batch", 0, ARG_ALWAYS_BATCH, -1},
{"bi", "batch_inn", 1, ARG_BATCH_INN, 40},
{"br", "batch_rnews", 1, ARG_BATCH_RNEWS, 40},
{"bl", "batch_lmove", 1, ARG_BATCH_LMOVE, 40},
{"bf", "batch_innfeed",1, ARG_BATCH_INNFEED, 40},
{"bp", "batch_post", 0, ARG_BATCH_POST, -1},
{"bP", "batch_post_nr", 1, ARG_BATCH_POST_NR, 72},
{"c", "cleanup", 0, ARG_CLEANUP, -1},
{"dt", "dir_temp", 1, ARG_DIR_TEMP, 37},
{"dd", "dir_data", 1, ARG_DIR_DATA, 37},
{"dm", "dir_msgs", 1, ARG_DIR_MSGS, 37},
{"e", "def_error_log", 0, ARG_DEF_ERRLOG, -1},
{"f", "reconnect_dedupe", 0, ARG_CONN_DEDUPE, -1},
{"g", "header_only", 0, ARG_HEADER_ONLY, -1},
{"h", "host", 1, ARG_HOST, 51},
{"hl", "localhost", 1, ARG_LOCALHOST, 50},
{"i", "default_activeread", 1, ARG_ACTIVE_LASTREAD, 65},
{"k", "kill_no_postfix", 0, ARG_NO_POSTFIX, -1},
{"l", "language_file", 1, ARG_LANGUAGE, 47},
{"lr", "low_read", 0, ARG_LOW_READ, -1},
{"m", "multifile", 0, ARG_MULTIFILE, -1},
{"n", "number_mode", 0, ARG_NRMODE, -1},
{"p", "postfix", 1, ARG_POSTFIX, 36},
{"q", "quiet", 0, ARG_QUIET, -1},
{"r", "rnews_size", 1, ARG_RNEWSSIZE, 35},
{"rc", "reset_counter", 0, ARG_RESETCOUNTER, -1},
{"s", "def_status_log", 0, ARG_DEF_STATLOG, -1},
{"sg", "show_group", 0, ARG_SHOW_GROUP, -1},
#ifdef HAVE_LIBSSL
{"ssl","use_ssl", 0, ARG_USE_SSL, -1},
#endif
{"u", "auto_authorization", 0, ARG_AUTOAUTH, -1},
{"w", "wait_signal", 2, ARG_WAIT_SIG, 46},
{"x", "no_chk_msgid", 0, ARG_NO_CHK_MSGID, -1},
{"y", "post_filter", 1, ARG_POST_FILTER, 62},
{"z", "no_dedupe", 0, ARG_NODEDUPE, -1},
{"A", "active", 0, ARG_ACTIVE, -1},
{"AL", "read_active", 1, ARG_READACTIVE, 56},
{"B", "pre-batch", 0, ARG_PREBATCH, -1},
{"C", "reconnect", 1, ARG_RECONNECT, 49},
{"D", "debug", 0, ARG_DEBUG, -1},
{"E", "error_log", 1, ARG_ERRLOG, 41},
{"F", "reconnect_active", 0, ARG_CONN_ACTIVE, -1},
{"G", "use_gui", 0, ARG_USEGUI, -1},
{"H", "no_history", 0, ARG_HISTORY, -1},
{"HF", "history_file", 1, ARG_HIST_FILE, 64},
{"K", "killfile", 0, ARG_KILLFILE, -1},
{"L", "kill_log_none", 0, ARG_KLOG_NONE, -1},
{"LF", "kill_log_name", 1, ARG_KLOG_NAME, 61},
{"LS", "kill_log_short", 0, ARG_KLOG_SHORT, -1},
{"LL", "kill_log_long", 0, ARG_KLOG_LONG, -1},
{"M", "mode_reader", 0, ARG_MODEREADER, -1},
{"N", "portnr", 1, ARG_PORTNR, 45},
{"O", "skip_on_restart", 0, ARG_SKIP_ON_RESTART, -1},
{"P", "password", 1, ARG_PASSWD, 44},
{"Q", "password_env", 0, ARG_PASSWD_ENV, -1},
{"R", "no_rescan", 0, ARG_RESCAN, -1},
{"S", "status_log", 1, ARG_STATLOG, 42},
#ifdef HAVE_LIBSSL
{"SSL" "local_use_ssl", 0, ARG_LOCAL_SSL, -1},
#endif
#ifdef TIMEOUT
{"T", "timeout", 1, ARG_TIMEOUT, 52},
#endif
{"U", "userid", 1, ARG_USERID, 43},
{"V", "version", 0, ARG_VERSION, -1},
{"W", "wait", 2, ARG_WAIT, 46},
{"X", "no_xover", 0, ARG_XOVER, -1},
{"Z", "use_xover", 0, ARG_USEXOVER, -1},
};
#define MAX_ARG_PARAMS 2 /* max nr of params with any arg */
#define NR_ARGS (sizeof(arglist)/sizeof(arglist[0]))
/*------------------------------------------------------------------*/
int main(int argc, char *argv[]) {
struct stat sbuf;
Master master;
PList temp;
PGroups ptemp;
POverview pov;
char *inbuf, **args, **fargs = NULL;
int nr, resp, loop, fargc, retval = RETVAL_OK;
#ifdef LOCKFILE
const char *lockfile = NULL;
#endif
#ifdef MYSIGNAL
#ifdef HAVE_SIGACTION
struct sigaction sigs;
#endif
#endif
/* initialize master structure */
master.head = master.curr = NULL;
master.nritems = master.nrgot = 0;
master.MultiFile = FALSE;
master.msgs = stdout;
master.sockfd = -1;
master.status_file = FALSE; /* are status messages going to a file */
master.status_file_name = NULL;
master.do_killfile = TRUE;
master.do_chkhistory = TRUE;
master.do_modereader = FALSE;
master.always_batch = FALSE;
master.rnews_size = 0L;
master.batch = BATCH_FALSE;
master.batchfile = NULL;
master.cleanup = FALSE;
master.portnr = DEFAULT_NNRP_PORT;
master.host = getenv("NNTPSERVER"); /* the default */
master.pause_time = -1;
master.pause_nrmsgs = -1;
master.sig_pause_time = -1;
master.sig_pause_nrmsgs = -1;
master.killfile_log = KILL_LOG_LONG; /* do we log killed messages */
master.phrases = NULL;
master.errlog = NULL;
master.debug = FALSE;
master.rescan = TRUE; /* do we do rescan on a restart */
master.quiet = FALSE; /* do we display BPS and msg count */
master.killp = NULL; /* pointer to killfile structure */
master.kill_ignore_postfix = FALSE;
master.reconnect_nr = 0; /* how many x msgs do we disconnect and reconnect 0 = never */
master.innfeed = NULL;
master.do_active = FALSE; /* do we scan the local active list */
master.localhost = NULL;
master.groups = NULL;
master.nrmode = FALSE; /* use nrs or msgids to get article */
master.auto_auth = FALSE; /* do we do auto authorization */
master.passwd = NULL;
master.userid = NULL;
master.no_dedupe = FALSE;
master.chk_msgid = TRUE; /* do we check MsgID for trailing > */
master.activefile = NULL;
master.prebatch = FALSE; /* do we try to batch any left over articles b4 we start? */
master.grpnr = -1; /* what group number are we currently on */
master.skip_on_restart = FALSE;
master.kill_log_name = N_KILLLOG;
master.use_gui = FALSE;
master.do_xover = TRUE;
master.conn_dedupe = FALSE;
master.post_filter= NULL;
master.conn_active = FALSE;
master.history_file = HISTORY_FILE;
master.header_only = FALSE;
master.db = -1;
master.active_lastread = ACTIVE_DEFAULT_LASTREAD;
master.use_xover = FALSE;
master.xoverview = NULL;
master.resetcounter = FALSE;
master.low_read = FALSE;
master.show_group = FALSE;
master.do_ssl = FALSE;
master.ssl_struct = NULL;
master.local_ssl = FALSE;
master.local_ssl_struct = NULL;
master.batch_post_nr = 0;
master.passwd_env = FALSE;
/* have to do this next so if set on cmd line, overrides this */
#ifdef N_PHRASES /* in case someone nukes def */
if(stat(N_PHRASES, &sbuf) == 0 && S_ISREG(sbuf.st_mode)) {
/* we have a regular phrases file make it the default */
master.phrases = N_PHRASES;
}
#endif
/* allow no args, only the hostname, or hostname as first arg */
/* also have to do the file argument checking */
switch(argc) {
case 1:
break;
case 2:
/* the fargs == NULL test so only process first file name */
if(argv[1][0] == FILE_CHAR) {
if((fargs = build_args(&argv[1][1], &fargc)) != NULL) {
retval = scan_args(&master, fargc, fargs);
}
}
else if(argv[1][0] == '-') {
/* in case of suck -V */
retval = scan_args(&master, 1, &(argv[1]));
}
else{
master.host = argv[1];
}
break;
default:
for(loop=1;loop<argc && fargs == NULL;loop++) {
if(argv[loop][0] == FILE_CHAR) {
if((fargs = build_args(&argv[loop][1], &fargc)) != NULL) {
retval = scan_args(&master, fargc, fargs);
}
}
}
/* this is here so anything at command line overrides file */
if(argv[1][0] != '-' && argv[1][0] != FILE_CHAR) {
master.host = argv[1];
argc-= 2;
args = &(argv[2]);
}
else {
args = &(argv[1]);
argc--;
}
retval = scan_args(&master, argc, args);
break;
}
/* print out status stuff */
if(master.debug == TRUE) {
do_debug("Suck version %s\n",SUCK_VERSION);
do_debug("master.MultiFile = %d\n", master.MultiFile);
do_debug("master.status_file = %d\n",master.status_file);
do_debug("master.status_file_name = %s\n", null_str(master.status_file_name));
do_debug("master.do_killfile = %s\n", true_str(master.do_killfile));
do_debug("master.do_chkhistory = %s\n", true_str(master.do_chkhistory));
do_debug("master.do_modereader = %s\n", true_str(master.do_modereader));
do_debug("master.always_batch = %s\n", true_str(master.always_batch));
do_debug("master.rnews_size = %ld\n", master.rnews_size);
do_debug("master.batch = %d\n", master.batch);
do_debug("master.batchfile = %s\n", null_str(master.batchfile));
do_debug("master.cleanup = %s\n", true_str(master.cleanup));
do_debug("master.host = %s\n", null_str(master.host));
do_debug("master.portnr = %u\n", master.portnr);
do_debug("master.pause_time = %d\n", master.pause_time);
do_debug("master.pause_nrmsgs = %d\n", master.pause_nrmsgs);
do_debug("master.sig_pause_time = %d\n", master.sig_pause_time);
do_debug("master.sig_pause_nrmsgs = %d\n", master.sig_pause_nrmsgs);
do_debug("master.killfile_log = %d\n", master.killfile_log);
do_debug("master.phrases = %s\n", null_str(master.phrases));
do_debug("master.errlog = %s\n", null_str(master.errlog));
do_debug("master.rescan = %s\n", true_str(master.rescan == TRUE));
do_debug("master.quiet = %s\n", true_str(master.quiet));
do_debug("master.kill_ignore_postfix = %s\n", true_str(master.kill_ignore_postfix));
do_debug("master.reconnect_nr=%d\n", master.reconnect_nr);
do_debug("master.do_active = %s\n", true_str(master.do_active));
do_debug("master.localhost = %s\n", null_str(master.localhost));
do_debug("master.nrmode = %s\n", true_str(master.nrmode));
do_debug("master.auto_auth = %s\n", true_str(master.auto_auth));
do_debug("master.no_dedupe = %s\n", true_str(master.no_dedupe));
do_debug("master.chk_msgid = %s\n", true_str(master.chk_msgid));
do_debug("master.activefile = %s\n", null_str(master.activefile));
do_debug("master.prebatch = %s\n", true_str(master.prebatch));
do_debug("master.skip_on_restart = %s\n", true_str(master.skip_on_restart));
do_debug("master.kill_log_name = %s\n", null_str(master.kill_log_name));
do_debug("master.use_gui = %s\n", true_str(master.use_gui));
do_debug("master.do_xover = %s\n", true_str(master.do_xover));
do_debug("master.conn_dedupe = %s\n", true_str(master.conn_dedupe));
do_debug("master.post_filter = %s\n", null_str(master.post_filter));
do_debug("master.conn_active = %s\n", true_str(master.conn_active));
do_debug("master.history_file = %s\n", null_str(master.history_file));
do_debug("master.header_only = %s\n", true_str(master.header_only));
do_debug("master.active_lastread = %d\n", master.active_lastread);
do_debug("master.use_xover = %s\n", true_str(master.use_xover));
do_debug("master.do_ssl = %s\n", true_str(master.do_ssl));
do_debug("master.local_ssl = %s\n", true_str(master.local_ssl));
do_debug("master.batch_post_nr = %d\n",master.batch_post_nr);
do_debug("master.passwd_env = %s\n", true_str(master.passwd_env));
#ifdef TIMEOUT
do_debug("TimeOut = %d\n", TimeOut);
#endif
do_debug("master.debug = TRUE\n");
}
/* now do any final args checks needed */
/* check to see if we have enough info to scan the localhost active file */
if((master.do_active == TRUE || master.batch == BATCH_LIHAVE) && master.localhost == NULL) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[6], NULL);
}
else if(master.host == NULL) {
retval = RETVAL_ERROR;
error_log(ERRLOG_REPORT, suck_phrases[74], NULL);
}
/* okay now the main stuff */
if(retval == RETVAL_OK) {
if(master.status_file == FALSE) {
/* if not in multifile mode, all status msgs MUST go to stderr to not mess up articles */
/* this has to go before lockfile code, so lockfile prints msg to correct place. */
master.msgs = ( master.MultiFile == FALSE) ? stderr : stdout ;
}
#ifdef LOCKFILE
/* this has to be here since we use full_path() to get path for lock file */
/* and it isn't set up until we process the args above. */
if(do_lockfile(&master) != RETVAL_OK) {
exit(-1);
}
#endif
#ifdef MYSIGNAL
/* set up signal handlers */
#ifdef HAVE_SIGACTION
sigemptyset(&(sigs.sa_mask));
sigs.sa_handler = sighandler;
sigs.sa_flags = 0;
if(sigaction(MYSIGNAL, &sigs, NULL) == -1
|| sigaction(MYSIGNAL2, &sigs, NULL) == -1
|| sigaction(PAUSESIGNAL, &sigs, NULL) == -1) {
MyPerror(suck_phrases[67]);
}
else {
signal_block(MYSIGNAL_SETUP); /* set up sgetline() to block signal */
pause_signal(PAUSE_SETUP, &master); /* set up routine for pause swap if signal */
}
#else
/* the old-fashioned way */
signal(MYSIGNAL, sighandler);
signal(MYSIGNAL2, sighandler);
signal(PAUSESIGNAL, sighander);
pause_signal(PAUSE_SETUP, &master);
#endif
#endif
load_phrases(&master); /* this has to be here so rest prints okay */
/* set up status log, if none specified or unable to open status log */
/* then use stdout or stderr */
if(master.status_file_name != NULL) {
/* okay attempt to open it up */
if((master.msgs = fopen(master.status_file_name, "a")) == NULL) {
MyPerror(suck_phrases[0]);
master.msgs = stdout; /* reset to default */
}
else {
master.status_file = TRUE;
}
}
#ifdef HAVE_SETVBUF
setvbuf(master.msgs, NULL, _IOLBF, 0); /* set to line buffering */
#endif
/* do we batch up any lingering articles ? */
if(master.prebatch == TRUE) {
switch(master.batch) {
case BATCH_FALSE:
error_log(ERRLOG_REPORT, suck_phrases[58], NULL);
break;
case BATCH_INNXMIT:
do_innbatch(&master);
break;
case BATCH_RNEWS:
do_rnewsbatch(&master);
break;
case BATCH_LMOVE:
do_lmovebatch(&master);
break;
case BATCH_LIHAVE:
do_localpost(&master);
break;
}
}
/* now parse the killfile */
#ifdef KILLFILE
if(master.do_killfile == TRUE) {
master.killp = parse_killfile(KILL_KILLFILE, master.killfile_log, master.debug, master.kill_ignore_postfix);
}
#endif
/* now parse the xover killfile */
if(master.do_xover == TRUE) {
master.xoverp = parse_killfile(KILL_XOVER, master.killfile_log, master.debug, master.kill_ignore_postfix);
}
print_phrases(master.msgs, suck_phrases[1], master.host, NULL);
if(do_connect(&master, CONNECT_FIRST) != RETVAL_OK) {
retval = RETVAL_ERROR;
}
else {
/* if we have XOVER killfiles, we need to get the format of em before any processing */
if(master.xoverp != NULL || master.use_xover == TRUE) {
get_xoverview(&master);
killprg_sendoverview(&master);
}
/* first, check for restart articles, */
/* then scan for any new articles */
if((loop = restart_yn(&master)) == RESTART_ERROR) {
retval = RETVAL_ERROR;
}
else if(loop == RESTART_NO || master.rescan == TRUE) {
/* we don't do scan if we had restart and option = FALSE */
/* do we scan the local active file? */
if(master.do_active == TRUE || master.activefile != NULL) {
if(get_message_index_active(&master) < RETVAL_OK) {
retval = RETVAL_ERROR;
}
}
else if(get_message_index(&master) < RETVAL_OK) {
retval = RETVAL_ERROR;
}
if(retval == RETVAL_OK) {
retval = do_supplemental(&master);
}
if(retval == RETVAL_OK) {
retval = do_nodownload(&master);
}
if(retval == RETVAL_OK && master.head != NULL && master.nritems > 0) {
/* if we don't have any messages, we don't need to do this */
if(master.no_dedupe == FALSE) {
dedupe_list(&master);
}
#ifdef CHECK_HISTORY
if(master.do_chkhistory == TRUE) {
chkhistory(&master);
}
#endif
print_phrases(master.msgs,suck_phrases[20], str_int(master.nritems), NULL);
}
}
if(retval == RETVAL_OK) {
if(master.nritems == 0) {
print_phrases(master.msgs,suck_phrases[3], NULL);
retval = RETVAL_NOARTICLES;
}
else {
/* write out restart db */
retval = db_write(&master);
/* reconnect after dedupe, in case of time out due to long dedupe time*/
if(retval == RETVAL_OK && master.conn_dedupe == TRUE) {
retval = do_connect(&master, CONNECT_AGAIN);
}
if(retval == RETVAL_OK) {
retval = get_articles(&master);
}
}
}
if(retval == RETVAL_OK) { /* if we got disconnected above, don't do this */
/* send quit, and get reply */
sputline(master.sockfd,"quit\r\n", master.do_ssl, master.ssl_struct);
if(master.debug == TRUE) {
do_debug("Sending command: quit\n");
}
do {
resp = sgetline(master.sockfd, &inbuf, master.do_ssl, master.ssl_struct);
if(resp>0) {
if(master.debug == TRUE) {
do_debug("Quitting GOT: %s", inbuf);
}
number(inbuf, &nr);
}
}while(nr != 205 && resp > 0);
}
}
if(master.sockfd >= 0) {
disconnect_from_nntphost(master.sockfd, master.do_ssl, &master.ssl_struct);
print_phrases(master.msgs,suck_phrases[4], master.host, NULL);
}
if(master.debug == TRUE) {
do_debug("retval=%d (RETVAL_OK=%d), m.nrgot=%d, m.batch=%d\n", retval, RETVAL_OK, master.nrgot, master.batch);
}
if((retval == RETVAL_OK || master.always_batch == TRUE) && master.nrgot > 0 && master.header_only == FALSE) {
switch(master.batch) {
case BATCH_INNXMIT:
do_post_filter(&master);
do_innbatch(&master);
break;
case BATCH_RNEWS:
do_post_filter(&master);
do_rnewsbatch(&master);
break;
case BATCH_LMOVE:
do_post_filter(&master);
do_lmovebatch(&master);
break;
case BATCH_LIHAVE:
do_post_filter(&master);
do_localpost(&master);
break;
default:
break;
}
}
if((retval == RETVAL_NOARTICLES || retval == RETVAL_OK) && master.cleanup == TRUE) {
print_phrases(master.msgs, suck_phrases[7], NULL);
do_cleanup(&master);
}
/* close out status log */
if(master.msgs != NULL && master.msgs != stdout && master.msgs != stderr) {
fclose(master.msgs);
}
if(master.head != NULL) {
/* clean up memory */
master.curr = master.head;
while(master.curr != NULL) {
temp = (master.curr)->next;
free_one_node(master.curr);
master.curr = temp;
}
}
/* free up group list */
while (master.groups != NULL) {
ptemp=(master.groups)->next;
free(master.groups);
master.groups = ptemp;
}
/* free up overview.fmt list */
while (master.xoverview != NULL) {
pov = (master.xoverview)->next;
if((master.xoverview)->header != NULL) {
free((master.xoverview)->header);
}
free(master.xoverview);
master.xoverview = pov;
}
#ifdef KILLFILE
free_killfile(master.killp);
#endif
free_killfile(master.xoverp);
if(fargs != NULL) {
free_args(fargc, fargs);
}
#ifdef LOCKFILE
lockfile = full_path(FP_GET, FP_TMPDIR, N_LOCKFILE);
if(lockfile != NULL) {
unlink(lockfile);
}
#endif
}
free_phrases(); /* do this last so everything is displayed correctly */
exit(retval);
}
/*------------------------------------------------------------*/
int do_connect(PMaster master, int which_time) {
char *inbuf;
int nr, resp, retval = RETVAL_OK;
FILE *fp;
if(which_time != CONNECT_FIRST) {
/* close down previous connection */
sputline(master->sockfd, "quit\r\n", master->do_ssl, master->ssl_struct);
do {
resp = sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct);
if(resp>0) {
if(master->debug == TRUE) {
do_debug("Reconnect GOT: %s", inbuf);
}
number(inbuf, &nr);
}
}while(nr != 205 && resp > 0);
disconnect_from_nntphost(master->sockfd, master->do_ssl, &master->ssl_struct);
/* now reset everything */
if(master->curr != NULL) {
(master->curr)->sentcmd = FALSE;
}
master->grpnr = -1;
}
if(master->debug == TRUE) {
do_debug("Connecting to %s on port %d\n", master->host, master->portnr);
}
fp = (which_time == CONNECT_FIRST) ? master->msgs : NULL;
master->sockfd = connect_to_nntphost( master->host, NULL, 0, fp, master->portnr, master->do_ssl, &master->ssl_struct);
if(master->sockfd < 0 ) {
retval = RETVAL_ERROR;
}
else if(sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct) < 0) {
/* Get the announcement line */
retval = RETVAL_ERROR;
}
else {
if(master->debug == TRUE) {
do_debug("Got: %s", inbuf);
}
if(which_time == CONNECT_FIRST) {
fprintf(master->msgs ,"%s", inbuf );
}
/* check to see if we have to do authorization now */
number(inbuf, &resp);
if(resp == 480 ) {
retval = do_authenticate(master);
}
if(retval == RETVAL_OK && master->do_modereader == TRUE) {
retval = send_command(master, "mode reader\r\n", &inbuf, 0);
if(retval == RETVAL_OK) {
/* Again the announcement */
if(which_time == CONNECT_FIRST) {
fprintf(master->msgs ,"%s",inbuf);
}
}
}
if(master->auto_auth == TRUE) {
if(master->passwd == NULL || master->userid == NULL) {
error_log(ERRLOG_REPORT, suck_phrases[55], NULL);
retval = RETVAL_ERROR;
}
else {
/* auto authorize */
retval = do_authenticate(master);
}
}
}
return retval;
}
/*--------------------------------------------------------------------*/
int get_message_index(PMaster master) {
long lastread;
int nrread, retval, maxread;
char buf[MAXLINLEN], group[512];
FILE *ifp,*tmpfp;
retval = RETVAL_OK;
ifp = tmpfp = NULL;
TimerFunc(TIMER_START, 0, NULL);
if((ifp = fopen(full_path(FP_GET, FP_DATADIR, N_OLDRC), "r" )) == NULL) {
MyPerror(full_path(FP_GET, FP_DATADIR, N_OLDRC));
retval = RETVAL_ERROR;
}
else if((tmpfp = fopen(full_path(FP_GET, FP_TMPDIR, N_NEWRC), "w" )) == NULL) {
MyPerror(full_path(FP_GET, FP_TMPDIR, N_NEWRC));
retval = RETVAL_ERROR;
}
while(retval == RETVAL_OK && fgets(buf, MAXLINLEN-1, ifp) != NULL) {
if(buf[0] == SUCKNEWSRC_COMMENT_CHAR) {
/* skip this group */
fputs(buf, tmpfp);
print_phrases(master->msgs, suck_phrases[8], buf, NULL);
}
else {
maxread = -1; /* just in case */
nrread = sscanf(buf, "%s %ld %d\n", group, &lastread, &maxread);
if ( nrread < 2 || nrread > 3) {
error_log(ERRLOG_REPORT, suck_phrases[9], buf, NULL);
fputs(buf, tmpfp); /* rewrite the line */
}
else if(maxread == 0) {
/* just rewrite the line */
fputs(buf, tmpfp);
}
else {
retval = do_one_group(master, buf, group, tmpfp, lastread, maxread);
}
}
}
TimerFunc(TIMER_TIMEONLY, 0,master->msgs);
if(retval == RETVAL_OK) {
print_phrases(master->msgs, suck_phrases[16], str_int(master->nritems), NULL);
}
else if(ifp != NULL) {
/* this is in case we had to abort the above while loop (due to loss of pipe to server) and */
/* we hadn't finished writing out the suck.newrc, this finishes it up. */
do {
fputs(buf, tmpfp);
}
while(fgets(buf, MAXLINLEN-1, ifp) != NULL);
}
if(tmpfp != NULL) {
fclose(tmpfp);
}
if(ifp != NULL) {
fclose(ifp);
}
return retval;
}
/*-----------------------------------------------------------------------------------------------------*/
int do_one_group(PMaster master, char *buf, char *group, FILE *newrc, long lastread, int maxread) {
char *sp, *inbuf, cmd[MAXLINLEN];
long count,low,high;
int response,retval,i;
retval = RETVAL_OK;
sprintf(cmd,"group %s\r\n",group);
if(send_command(master,cmd,&inbuf,0) != RETVAL_OK) {
retval = RETVAL_ERROR;
}
else {
sp = number(inbuf, &response);
if(response != 211) {
fputs(buf, newrc); /* rewrite line AS IS in newrc */
/* handle group not found */
switch(response) {
case 411:
error_log(ERRLOG_REPORT, suck_phrases[11], group, NULL);
break;
case 500:
error_log(ERRLOG_REPORT, suck_phrases[48], NULL);
break;
default:
error_log(ERRLOG_REPORT, suck_phrases[12],group,str_int(response),NULL);
retval = RETVAL_ERROR; /* bomb out on wacko errors */
break;
}
}
else {
sp = get_long(sp, &count);
sp = get_long(sp, &low);
sp = get_long(sp, &high);
fprintf(newrc, "%s %ld", group, high);
if(maxread > 0) {
fprintf(newrc, " %d", maxread);
}
fputs("\n", newrc);
/* add a sanity check in case remote host changes its numbering scheme */
/* the > 0 is needed, since if a nnrp site has no article it will reset */
/* the count to 0. So not an error */
if(lastread > high && high > 0) {
if(master->resetcounter == TRUE) {
/* reset lastread to low, so we pick up all articles in the group */
lastread = low;
print_phrases(master->msgs,suck_phrases[71],group,str_int(high),str_int(low),NULL);
}
else {
print_phrases(master->msgs,suck_phrases[13],group,str_int(high),NULL);
}
}
if(lastread < 0 ) {
/* if a neg number, get the last X nr of messages, handy for starting */
/* a new group off ,say -100 will get the last 100 messages */
lastread += high; /* this works since lastread is neg */
if(lastread < 0) {
lastread = 0; /* just to be on the safeside */
}
}
/* this has to be >= 0 since if there are no article on server high = 0 */
/* so if we write out 0, we must be able to handle zero as valid lastread */
/* the count > 0 so if no messages available we don't even try */
/* or if low > high no messages either */
if (low <= high && count > 0 && lastread < high && lastread >= 0) {
/* add group name to list of groups */
if(lastread < low) {
lastread = low - 1;
}
/* now set the max nr of messages to read */
if(maxread > 0 && high-maxread > lastread) {
if(master->low_read == TRUE) {
/* instead of limiting from the high-water mark (the latest articles) */
/* limit from the low-water mark (the oldest articles) */
high = (lastread+1) + maxread;
}
else {
lastread = high-maxread;
}
print_phrases(master->msgs, suck_phrases[14], group, str_int(maxread), NULL);
}
print_phrases(master->msgs, suck_phrases[15], group, str_long(high-lastread), str_long(lastread+1), str_long(high), NULL);
if(master->xoverp != NULL) {
/* do this via the xover killfile command */
retval = do_group_xover(master, group, lastread+1, high);
}
/* do we use xover or xhdr to get our article list */
if(master->use_xover == TRUE) {
retval = get_xover(master, group, lastread+1, high);
}
else if((master->xoverp == NULL) || (retval == RETVAL_NOXOVER)) {
/* do this the normal way */
sprintf(cmd, "xhdr Message-ID %ld-%ld\r\n", lastread+1,high);
i = send_command(master,cmd,&inbuf,221);
if(i != RETVAL_OK) {
retval = RETVAL_ERROR;
}
else {
do {
if(sgetline(master->sockfd, &inbuf, master->do_ssl, master->ssl_struct) < 0) {
retval = RETVAL_ERROR;
}
else if (*inbuf != '.' ) {
retval = allocnode(master, inbuf, MANDATORY_OPTIONAL, group, 0L);
}
} while (retval == RETVAL_OK && *inbuf != '.' && *(inbuf+1) != '\n');
} /* end if response */
} /* end if xover */
} /* end if lastread */
} /* end response */
} /* end else */
if(retval == RETVAL_ERROR) {
error_log(ERRLOG_REPORT, suck_phrases[59], NULL);
}
return retval;
}
/*-----------------------------------------------------------*/
int get_articles(PMaster master) {
int retval, logcount, i, grpnr, grplen;
long loop, downloaded;
PGroups grps;
const char *grpname;
const char *empty = "";
#ifdef HAVE_GETTIMEOFDAY
double bps;
#endif
#ifdef KILLFILE
int ( *get_message)(PMaster, int, long); /* which function will we use get_one_article or get_one_article_kill) */
grpnr = -1;
grps = NULL;
grpname = empty;
grplen = 0;
/* do we use killfiles? */
get_message = (master->killp == NULL) ? get_one_article : get_one_article_kill;
#endif
retval = RETVAL_OK;
downloaded = loop = 0; /* used to track how many downloaded, for reconnect option */
/* figure out how many digits wide the articleCount is for display purposes */
/* this used to be log10()+1, but that meant I needed the math library */
for(logcount=1, i=master->nritems; i > 9 ; logcount++) {
i /= 10;
}
if(master->MultiFile == TRUE && checkdir(full_path(FP_GET, FP_MSGDIR, NULL)) == FALSE) {
retval = RETVAL_ERROR;
}
else {
retval = db_open(master);
if(retval == RETVAL_OK) {
master->curr = master->head;
if(master->batch == BATCH_INNFEED) {
/* open up batch file for appending to */
if((master->innfeed = fopen(master->batchfile, "a")) == NULL) {
MyPerror(master->batchfile);
}
}
else if(master->batch == BATCH_LIHAVE) {
if((master->innfeed = fopen(full_path(FP_GET, FP_TMPDIR, master->batchfile), "a")) == NULL) {
MyPerror(full_path(FP_GET, FP_TMPDIR, master->batchfile));
retval = RETVAL_ERROR;
}
}
TimerFunc(TIMER_START, 0, NULL);
#ifdef MYSIGNAL
while(retval == RETVAL_OK && master->curr != NULL && GotSignal == FALSE) {
#else
while(retval == RETVAL_OK && master->curr != NULL) {
#endif
if(master->debug == TRUE) {
do_debug("Article nr = %s mandatory = %c\n", (master->curr)->msgnr, (master->curr)->mandatory);
}
loop++;
if((master->curr)->downloaded == FALSE) {
/* we haven't yet downloaded this article */
downloaded++;