forked from brong/cyrus-imapd-legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinn.diffs
executable file
·4634 lines (4592 loc) · 114 KB
/
inn.diffs
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
$Id: inn.diffs,v 1.3 2002/05/25 19:57:49 leg Exp $
This is a patch against INN 2.2.1 for allowing INN to deliver directly
to an IMAP server using LMTP and IMAP (to handle control messages)
This patch will hopefully be included in future versions on INN.
To configure INN to deliver to an IMAP server put something like the
following in your newsfeeds file.
imapfeed!:\
!*,\
:Tc,Wnm*,S30000:/usr/news/bin/startinnfeed imapfeed
# A real-time feed through innfeed.
myhostname.cmu.edu\
:!junk,!control/!foo\
:Tm:imapfeed!
*** Makefile.old Mon Feb 28 00:50:47 2000
--- Makefile Thu Feb 10 23:39:19 2000
***************
*** 19,25 ****
SRC = article.c \
buffer.c \
- connection.c \
endpoint.c \
host.c \
innlistener.c \
--- 19,24 ----
***************
*** 51,61 ****
--- 50,62 ----
config_l.o config_y.o
INNFEED_BIN = $D$(PATHBIN)/innfeed
+ IMAPFEED_BIN = $D$(PATHBIN)/imapfeed
STARTINNFEED = $D$(PATHBIN)/startinnfeed
ALL_INSTALLED = $D$(PATHBIN)/innfeed $D$(PATHBIN)/startinnfeed \
+ $D$(PATHBIN)/imapfeed \
$D$(PATHBIN)/procbatch $D$(PATHBIN)/innfeed-convcfg
MAN_INSTALLED = $D$(MANDIR)/man1/innfeed.1 $D$(MANDIR)/man5/innfeed.conf.5
***************
*** 95,101 ****
DEPENDFLAGS = -s -c '$(CC) -E'
.c.o:
! $(COMPILE.c) $< $(OUTPUT_OPTION)
.c.E:
$(CPP.c) $< $(OUTPUT_OPTION)
--- 96,102 ----
DEPENDFLAGS = -s -c '$(CC) -E'
.c.o:
! $(COMPILE.c) $(SASLFLAGS) $< $(OUTPUT_OPTION)
.c.E:
$(CPP.c) $< $(OUTPUT_OPTION)
***************
*** 103,114 ****
.sh:
$(RM) $@ ; $(CP) $< $@ ; $(CHMOD) a-w,a+x $@
! all: innfeed startinnfeed
install: $(ALL_INSTALLED)
! innfeed: $(OBJS) version.o $(MALLOC) $(LIBNEWS)
! $(LINK.c) -o $@ $(OBJS) version.o $(LIBNEWS) $(LIBNEWS) $(LIBS)
$D$(PATHBIN)/innfeed: innfeed
$(LIBTOOL) ../installit.sh $(OWNER) -m 550 -b .OLD $? $@
--- 104,118 ----
.sh:
$(RM) $@ ; $(CP) $< $@ ; $(CHMOD) a-w,a+x $@
! all: innfeed startinnfeed imapfeed
install: $(ALL_INSTALLED)
! innfeed: $(OBJS) version.o connection.o $(MALLOC) $(LIBNEWS)
! $(LINK.c) -o $@ $(OBJS) version.o connection.o $(LIBNEWS) $(LIBNEWS) $(LIBS)
!
! imapfeed: $(OBJS) version.o imap_connection.o $(MALLOC) $(LIBNEWS)
! $(LINK.c) -o $@ $(OBJS) version.o imap_connection.o $(LIBNEWS) $(LIBNEWS) $(LIBS) $(LIB_SASL)
$D$(PATHBIN)/innfeed: innfeed
$(LIBTOOL) ../installit.sh $(OWNER) -m 550 -b .OLD $? $@
***************
*** 135,140 ****
--- 139,145 ----
echo "" ; echo "" ;\
fi
+
objs: $(OBJS)
tags: $(SRC) $(INCS)
***************
*** 227,232 ****
--- 232,239 ----
article.o: article.c article.h buffer.h config.h endpoint.h misc.h \
msgs.h sysconfig.h
buffer.o: buffer.c buffer.h config.h misc.h sysconfig.h
+ imapconnection.o: article.h buffer.h config.h configfile.h imapconnection.c \
+ connection.h endpoint.h host.h misc.h msgs.h sysconfig.h
connection.o: article.h buffer.h config.h configfile.h connection.c \
connection.h endpoint.h host.h misc.h msgs.h sysconfig.h
endpoint.o: buffer.h config.h configfile.h endpoint.c endpoint.h host.h \
polarbear:/afs/andrew/system/src/host/inn/013/innfeed> diff -c main.old main.c
*** main.old Mon Feb 28 00:51:23 2000
--- main.c Tue Feb 1 00:33:54 2000
***************
*** 103,108 ****
--- 103,113 ----
bool useMMap = false ;
void (*gPrintInfo) (void) ;
char *dflTapeDir;
+ /* these are used by imapfeed */
+ char *deliver_username = NULL;
+ char *deliver_authname = NULL;
+ char *deliver_password = NULL;
+ char *deliver_realm = NULL;
/* imports */
extern char *versionInfo ;
***************
*** 162,168 ****
struct rlimit rl;
bool val;
-
strcpy (dateString,ctime(&now)) ;
dateString [24] = '\0' ;
--- 167,172 ----
***************
*** 842,847 ****
--- 846,877 ----
logFile = buildFilename (innconf->pathlog,p) ;
FREE (p) ;
}
+
+ /* For imap/lmtp delivering */
+ if (getString (topScope,"deliver-username",&p, NO_INHERIT))
+ {
+ deliver_username = p;
+ /* don't need to free */
+ }
+
+ if (getString (topScope,"deliver-authname",&p, NO_INHERIT))
+ {
+ deliver_authname = p;
+ /* don't need to free */
+ }
+
+ if (getString (topScope,"deliver-password",&p, NO_INHERIT))
+ {
+ deliver_password = p;
+ /* don't need to free */
+ }
+
+ if (getString (topScope,"deliver-realm",&p, NO_INHERIT))
+ {
+ deliver_realm = p;
+ /* don't need to free */
+ }
+
return 1 ;
polarbear:/afs/andrew/system/src/host/inn/013/innfeed> diff -c startinnfeed.old startinnfeed.c
*** startinnfeed.old Mon Feb 28 00:50:28 2000
--- startinnfeed.c Mon Feb 28 14:06:28 2000
***************
*** 78,86 ****
else
{
char **evp = NULL ;
! innfeed = NEW(char, (strlen(innconf->pathbin)+1+strlen(INNFEED)+1));
! sprintf(innfeed, "%s/%s", innconf->pathbin, INNFEED);
av[0] = (char *) innfeed;
#if defined (USE_DMALLOC)
--- 78,98 ----
else
{
char **evp = NULL ;
+ char *execname = INNFEED;
+
+ if (ac == 2)
+ {
+ if (strcmp(av[1], "imapfeed") == 0) {
+ execname = imapfeed;
+ av[1] = NULL;
+ } else {
+ syslog(LOG_ERR, "Only imapfeed is a valid argument to startinnfeed");
+ exit(1);
+ }
+ }
! innfeed = NEW(char, (strlen(innconf->pathbin)+1+strlen(execname)+1));
! sprintf(innfeed, "%s/%s", innconf->pathbin, execname);
av[0] = (char *) innfeed;
#if defined (USE_DMALLOC)
polarbear:/afs/andrew/system/src/host/inn/013/innfeed> diff -c /dev/null imap_connection.c
*** /dev/null Mon Dec 31 23:00:00 1979
--- imap_connection.c Mon Feb 28 00:45:20 2000
***************
*** 0 ****
--- 1,4408 ----
+ /* feed articles to an IMAP server via LMTP and IMAP
+ Tim Martin
+
+ Instead of feeding articles via nntp to another host this feeds the
+ messages via lmtp to a host and the control messages (cancel's
+ etc..) it preforms via IMAP. This means it has 2 active connections
+ at any given time and 2 queues.
+
+ When an article comes in it is immediatly placed in the lmtp
+ queue. When an article is picked off the lmtp queue for processing
+ first check if it's a control message. if so place it in the IMAP
+ queue. If not attempt to deliver via lmtp.
+
+ This attempts to follow the exact same api as connection.c.
+
+ TODO:
+
+ feed to smtp
+ security layers? <--punt on for now
+ authname/password per connection object
+ untagged IMAP messages
+ */
+
+ #ifdef HAVE_SASL
+ #include <sasl.h>
+ #endif /* HAVE_SASL */
+
+ #include <netinet/in.h>
+ #include <netdb.h>
+ #include <sys/socket.h>
+ #include <sys/file.h>
+ #include <netinet/in.h>
+ #include <netdb.h>
+ #include <time.h>
+
+ #include <errno.h>
+
+ #include "buffer.h"
+ #include "connection.h"
+ #include "endpoint.h"
+ #include "host.h"
+ #include "article.h"
+ #include "msgs.h"
+ #include "configfile.h"
+ #include "clibrary.h"
+
+
+
+ #define IMAP_PORT 143
+
+ #ifdef SMTPMODE
+ #define LMTP_PORT 25
+ #else
+ #define LMTP_PORT 2003
+ #endif /* SMTPMODE */
+
+ /* The name to prepend to deliver directly to newsgroup bboards */
+ #define NEWS_USERNAME "bb"
+
+ #define IMAP_TAGLENGTH 6
+
+ #define QUEUE_MAX_SIZE 250
+
+ #define DOSOMETHING_TIMEOUT 60
+
+
+
+ /* external */
+ extern char *deliver_username;
+ extern char *deliver_authname;
+ extern char *deliver_password;
+ extern char *deliver_realm;
+
+
+ char hostname[MAXHOSTNAMELEN];
+ char *mailfrom_name = ""; /* default to no return path */
+
+ /* states the imap connection may be in */
+ typedef enum {
+
+ IMAP_DISCONNECTED = 1,
+ IMAP_WAITING,
+
+ IMAP_CONNECTED_NOTAUTH,
+
+ IMAP_READING_INTRO,
+
+ IMAP_WRITING_CAPABILITY,
+ IMAP_READING_CAPABILITY,
+
+ IMAP_WRITING_STARTAUTH,
+ IMAP_READING_STEPAUTH,
+ IMAP_WRITING_STEPAUTH,
+
+ IMAP_IDLE_AUTHED,
+
+ IMAP_WRITING_CREATE,
+ IMAP_READING_CREATE,
+
+ IMAP_WRITING_DELETE,
+ IMAP_READING_DELETE,
+
+ IMAP_WRITING_SELECT,
+ IMAP_READING_SELECT,
+
+ IMAP_WRITING_SEARCH,
+ IMAP_READING_SEARCH,
+
+ IMAP_WRITING_STORE,
+ IMAP_READING_STORE,
+
+ IMAP_WRITING_CLOSE,
+ IMAP_READING_CLOSE,
+
+ IMAP_WRITING_QUIT,
+ IMAP_READING_QUIT
+
+ } imap_state_t;
+
+ typedef enum {
+ LMTP_DISCONNECTED = 1,
+ LMTP_WAITING,
+
+ LMTP_CONNECTED_NOTAUTH,
+
+ LMTP_READING_INTRO,
+
+ LMTP_WRITING_LHLO,
+ LMTP_READING_LHLO,
+
+ LMTP_WRITING_STARTAUTH,
+ LMTP_READING_STEPAUTH,
+ LMTP_WRITING_STEPAUTH,
+
+ LMTP_AUTHED_IDLE,
+
+ LMTP_READING_MAILFROM,
+ LMTP_READING_RCPTTO,
+ LMTP_READING_DATA,
+ LMTP_READING_CONTENTS,
+
+ LMTP_WRITING_UPTODATA,
+ LMTP_WRITING_CONTENTS,
+
+ LMTP_WRITING_QUIT,
+ LMTP_READING_QUIT
+
+ } lmtp_state_t;
+
+ typedef struct imap_capabilities_s {
+
+ int imap4; /* does server support imap4bis? */
+ int logindisabled; /* does the server allow the login command? */
+
+ char *saslmechs; /* supported SASL mechanisms */
+
+ } imap_capabilities_t;
+
+ typedef struct lmtp_capabilities_s {
+
+ int Eightbitmime;
+ int EnhancedStatusCodes;
+ int pipelining;
+
+ char *saslmechs;
+
+ } lmtp_capabilities_t;
+
+ typedef enum {
+ STAT_CONT = 0,
+ STAT_NO = 1,
+ STAT_OK = 2,
+ STAT_FAIL = 3
+ } imt_stat;
+
+ /* Message types */
+ typedef enum {
+
+ DELIVER,
+ CREATE_FOLDER,
+ CANCEL_MSG,
+ DELETE_FOLDER
+
+ } control_type_t;
+
+ typedef struct control_item_s {
+
+ Article article;
+ char *folder;
+ char *msgid; /* only for cancel's */
+ unsigned long uid; /* only for cancel's */
+
+ } control_item_t;
+
+ typedef struct article_queue_s {
+
+ control_type_t type;
+
+ time_t arrived;
+ time_t nextsend; /* time we should next try to send article */
+
+ int trys;
+
+ int counts_toward_size;
+
+ union {
+ Article article;
+ control_item_t *control;
+ void *generic;
+ } data;
+
+ struct article_queue_s *next;
+
+ } article_queue_t;
+
+ typedef struct Q_s {
+
+ article_queue_t *head;
+
+ article_queue_t *tail;
+
+ int size;
+
+ } Q_t;
+
+ typedef struct connection_s {
+
+ /* common stuff */
+ char *ServerName;
+
+ char *lmtp_respBuffer; /* buffer all responses are read into */
+ Buffer lmtp_rBuffer; /* buffer all responses are read into */
+
+ Host myHost ; /* the host who owns the connection */
+
+ time_t timeCon ; /* the time the connect happened (last auth suceeded) */
+
+ int issue_quit; /* Three states:
+ * 0 - don't do anything
+ * 1 - after issue quit enter wait state
+ * 2 - after issue quit reconnect
+ * 3 - after issue quit delete connection
+ * 4 - nuke cxn
+ */
+
+ /* Statistics */
+ int lmtp_suceeded;
+ int lmtp_failed;
+
+ int cancel_suceeded;
+ int cancel_failed;
+
+ int create_suceeded;
+ int create_failed;
+
+ int remove_suceeded;
+ int remove_failed;
+
+
+ /* LMTP stuff */
+ int lmtp_port;
+ lmtp_state_t lmtp_state;
+ #ifdef HAVE_SASL
+ sasl_conn_t *saslconn_lmtp;
+ #endif /* HAVE_SASL */
+ int sockfd_lmtp;
+
+ time_t lmtp_timeCon ;
+
+ EndPoint lmtp_endpoint;
+ u_int ident ; /* an identifier for syslogging. */
+
+ lmtp_capabilities_t *lmtp_capabilities;
+
+ int lmtp_have_mailfrom;
+ int lmtp_disconnects;
+ char *lmtp_tofree_str;
+
+ article_queue_t *current_article;
+ Buffer *current_bufs;
+ int current_rcpts_issued;
+ int current_rcpts_okayed;
+
+ /* Timer for the max amount of time to wait for a response from the
+ remote */
+ u_int lmtp_readTimeout ;
+ TimeoutId lmtp_readBlockedTimerId ;
+
+ /* Timer for the max amount of time to wait for a any amount of data
+ to be written to the remote */
+ u_int lmtp_writeTimeout ;
+ TimeoutId lmtp_writeBlockedTimerId ;
+
+ /* Timer for the number of seconds to sleep before attempting a
+ reconnect. */
+ u_int lmtp_sleepTimeout ;
+ TimeoutId lmtp_sleepTimerId ;
+
+ /* Timer for max amount between queueing some articles and trying to send them */
+ u_int dosomethingTimeout ;
+ TimeoutId dosomethingTimerId ;
+
+ Q_t lmtp_todeliver_q;
+
+
+
+ /* IMAP stuff */
+ int imap_port;
+ #ifdef HAVE_SASL
+ sasl_conn_t *imap_saslconn;
+ #endif /* HAVE_SASL */
+
+ char *imap_respBuffer;
+ Buffer imap_rBuffer;
+ EndPoint imap_endpoint;
+
+ imap_capabilities_t *imap_capabilities;
+
+ int imap_sockfd;
+
+ time_t imap_timeCon ;
+
+ imap_state_t imap_state;
+ int imap_disconnects;
+ char *imap_tofree_str;
+
+ char imap_currentTag[IMAP_TAGLENGTH];
+ int imap_tag_num;
+
+ /* Timer for the max amount of time to wait for a response from the
+ remote */
+ u_int imap_readTimeout ;
+ TimeoutId imap_readBlockedTimerId ;
+
+ /* Timer for the max amount of time to wait for a any amount of data
+ to be written to the remote */
+ u_int imap_writeTimeout ;
+ TimeoutId imap_writeBlockedTimerId ;
+
+ /* Timer for the number of seconds to sleep before attempting a
+ reconnect. */
+ u_int imap_sleepTimeout ;
+ TimeoutId imap_sleepTimerId ;
+
+ Q_t imap_controlMsg_q;
+
+ article_queue_t *current_control;
+
+ struct connection_s *next;
+
+ } connection_t;
+
+ static Connection gCxnList = NULL ;
+ static u_int gCxnCount= 0 ;
+ static u_int max_reconnect_period = MAX_RECON_PER ;
+ static u_int init_reconnect_period = INIT_RECON_PER;
+
+ typedef enum {
+ RET_OK = 0,
+ RET_FAIL = 1,
+ RET_QUEUE_EMPTY,
+ RET_EXCEEDS_SIZE,
+ RET_NO_FULLLINE,
+ RET_NO,
+ RET_ARTICLE_BAD
+ } conn_ret;
+
+
+ /********** Private Function Declarations *************/
+
+ static void lmtp_readCB (EndPoint e, IoStatus i, Buffer *b, void *d);
+ static void imap_readCB (EndPoint e, IoStatus i, Buffer *b, void *d);
+ static void imap_writeCB (EndPoint e, IoStatus i, Buffer *b, void *d);
+ static void lmtp_writeCB (EndPoint e, IoStatus i, Buffer *b, void *d);
+
+ static conn_ret lmtp_Connect(connection_t *cxn);
+ static conn_ret imap_Connect(connection_t *cxn);
+
+ static void prepareReopenCbk (Connection cxn, int type);
+
+ static void lmtp_readTimeoutCbk (TimeoutId id, void *data);
+ static void imap_readTimeoutCbk (TimeoutId id, void *data);
+
+ static void dosomethingTimeoutCbk (TimeoutId id, void *data);
+
+ static conn_ret WriteToWire_imapstr(connection_t *cxn, char *str, int slen);
+ static conn_ret WriteToWire_lmtpstr(connection_t *cxn, char *str, int slen);
+
+ static conn_ret WriteToWire(connection_t *cxn, EndpRWCB callback,
+ EndPoint endp, Buffer *array);
+ static void lmtp_sendmessage(connection_t *cxn, Article justadded);
+ static conn_ret imap_ProcessQueue(connection_t *cxn);
+
+ static conn_ret FindHeader(Buffer *bufs, char *header, char **start, char **end);
+ static conn_ret PopFromQueue(Q_t *q, article_queue_t **item);
+ static void QueueForgetAbout(connection_t *cxn, article_queue_t *item, int failed);
+
+ static void delConnection (Connection cxn);
+
+ static void DeferAllArticles(connection_t *cxn, Q_t *q);
+
+ static void lmtp_Disconnect(connection_t *cxn);
+ static void imap_Disconnect(connection_t *cxn);
+ static conn_ret imap_listenintro(connection_t *cxn);
+
+ static void imap_writeTimeoutCbk (TimeoutId id, void *data);
+ static void lmtp_writeTimeoutCbk (TimeoutId id, void *data);
+
+ /******************** PRIVATE FUNCTIONS ***************************/
+
+ static char *imap_stateToString(int state)
+ {
+ switch (state)
+ {
+ case IMAP_DISCONNECTED: return "Disconnected";
+ case IMAP_WAITING: return "Waiting";
+ case IMAP_CONNECTED_NOTAUTH: return "Connected but not Authenticated";
+ case IMAP_READING_INTRO: return "Reading Intro line";
+ case IMAP_WRITING_CAPABILITY: return "Writing capability";
+ case IMAP_READING_CAPABILITY: return "Reading capability";
+ case IMAP_WRITING_STARTAUTH: return "Writing Startauth";
+ case IMAP_READING_STEPAUTH: return "Reading Stepauth";
+ case IMAP_WRITING_STEPAUTH: return "Writing Stepauth";
+ case IMAP_IDLE_AUTHED: return "Idle. Authenticated";
+ case IMAP_WRITING_CREATE: return "Writing create";
+ case IMAP_READING_CREATE: return "Reading create response";
+ case IMAP_WRITING_DELETE: return "Writing Delete command";
+ case IMAP_READING_DELETE: return "Reading Delete response";
+ case IMAP_WRITING_SELECT: return "Writing Select";
+ case IMAP_READING_SELECT: return "Reading Select response";
+ case IMAP_WRITING_SEARCH: return "Writing Search";
+ case IMAP_READING_SEARCH: return "Reading Search response";
+ case IMAP_WRITING_STORE: return "Writing Store";
+ case IMAP_READING_STORE: return "Reading Store response";
+ case IMAP_WRITING_CLOSE: return "Writing Close";
+ case IMAP_READING_CLOSE: return "Reading Close response";
+ case IMAP_WRITING_QUIT: return "Writing Quit";
+ case IMAP_READING_QUIT: return "Reading Quit";
+ default: return "Unknown state";
+ }
+ }
+
+ static char *lmtp_stateToString(int state)
+ {
+ switch(state)
+ {
+ case LMTP_DISCONNECTED: return "Disconnected";
+ case LMTP_WAITING: return "Waiting";
+ case LMTP_CONNECTED_NOTAUTH: return "Connected but not authenticated";
+ case LMTP_READING_INTRO: return "Reading intro";
+ case LMTP_WRITING_LHLO: return "Writing LHLO";
+ case LMTP_READING_LHLO: return "Reading LHLO response";
+ case LMTP_WRITING_STARTAUTH: return "Writing Start Auth";
+ case LMTP_READING_STEPAUTH: return "Reading stepauth";
+ case LMTP_WRITING_STEPAUTH: return "writing stepauth";
+ case LMTP_AUTHED_IDLE: return "Idle. Authenticated";
+ case LMTP_READING_MAILFROM: return "Reading mailfrom";
+ case LMTP_READING_RCPTTO: return "Reading RCPTTO response";
+ case LMTP_READING_DATA: return "Reading DATA response";
+ case LMTP_READING_CONTENTS: return "Reading contents response";
+ case LMTP_WRITING_UPTODATA: return "Writing MAIL FROM, RCPTTO, DATA commands";
+ case LMTP_WRITING_CONTENTS: return "Writing contents of message";
+ case LMTP_WRITING_QUIT: return "Writing Quit";
+ case LMTP_READING_QUIT: return "Reading Quit";
+ default: return "Unknown state";
+ }
+ }
+
+ /******************************* Queue functions ***********************************/
+
+ /*
+ * Add a message to a generic queue
+ *
+ * q - the queue adding to
+ * item - the data to add to the queue
+ * type - the type of item it is (i.e. cancel,lmtp,etc..)
+ * addsmsg - weather this should be counted toward the queue size
+ * this is for control msg's that create multiple queue items.
+ * For example a cancel message canceling a message in multiple
+ * newsgroups will create >1 queue item but we only want it to count
+ * once towards the queue
+ * must - wheather we must take it even though it may put us over our max size
+ */
+
+ static conn_ret AddToQueue(Q_t *q, void *item, control_type_t type, int addsmsg, bool must)
+ {
+ article_queue_t *newentry;
+
+ if (must == false)
+ {
+ if (q->size >= QUEUE_MAX_SIZE)
+ {
+ return RET_EXCEEDS_SIZE;
+ }
+ } else {
+ if (q->size >= QUEUE_MAX_SIZE * 10)
+ {
+ d_printf(0,"Queue has grown way too much. Dropping article\n");
+ return RET_FAIL;
+ }
+ }
+
+ /* add to the end of our queue */
+ newentry = (article_queue_t *) malloc(sizeof(article_queue_t));
+
+ newentry->type = type;
+
+ /* send as soon as possible */
+ newentry->nextsend = newentry->arrived = time(NULL);
+
+ newentry->trys = 0;
+
+ newentry->data.generic = item;
+ newentry->next = NULL;
+ newentry->counts_toward_size = addsmsg;
+
+ /* add to end of queue */
+ if (q->tail == NULL)
+ {
+ q->head = newentry;
+ q->tail = newentry;
+ } else {
+
+ q->tail->next = newentry;
+ q->tail = newentry;
+ }
+
+ q->size+=addsmsg;
+
+ return RET_OK;
+ }
+
+ /*
+ * Pop an item from the queue
+ *
+ * q - the queue to pop from
+ * item - where the item shall be placed upon sucess
+ *
+ */
+
+ static conn_ret PopFromQueue(Q_t *q, article_queue_t **item)
+ {
+ article_queue_t *todel;
+
+ /* if queue empty return error */
+ if ( q->head == NULL)
+ {
+ return RET_QUEUE_EMPTY;
+ }
+
+ /* set what we return */
+ *item = q->head;
+
+ q->head = q->head->next;
+ if (q->head == NULL) q->tail = NULL;
+
+ q->size-=(*item)->counts_toward_size;
+
+ return RET_OK;
+ }
+
+ /*
+ * ReQueue an item. Will either put it back in the queue for another try
+ * or forget about it
+ *
+ * cxn - our connection object (needed so forget about things)
+ * q - the queue to requeue to
+ * entry - the item to put back
+ */
+
+ static void ReQueue(connection_t *cxn, Q_t *q, article_queue_t *entry)
+ {
+ /* look at the time it's been here */
+ entry->nextsend = time(NULL) + (entry->trys *30); /* xxx better formula? */
+
+ entry->trys++;
+
+ /* give up after 5 tries xxx configurable??? */
+ if (entry->trys == 5)
+ {
+ QueueForgetAbout(cxn, entry,1);
+ return;
+ }
+
+
+ /* ok let's add back to the end of the queue */
+ entry->next = NULL;
+
+ /* add to end of queue */
+ if (q->tail == NULL)
+ {
+ q->head = entry;
+ q->tail = entry;
+ } else {
+ q->tail->next = entry;
+ q->tail = entry;
+ }
+
+ q->size+=entry->counts_toward_size;
+ }
+
+
+
+ /*
+ * Forget about an item. Tells host object if we suceeded/failed/etc with the message
+ *
+ * cxn - connection object
+ * item - item
+ * failed - type of failure (see below)
+ *
+ * failed:
+ * 0 - suceeded delivering message
+ * 1 - failed delivering message
+ * 2 - Try to give back to host
+ * 3 - Article missing (i.e. can't find on disk)
+ */
+
+ static void QueueForgetAbout(connection_t *cxn, article_queue_t *item, int failed)
+ {
+ Article art = NULL;
+
+ switch (item->type)
+ {
+ case DELIVER:
+ if (failed>0)
+ cxn->lmtp_failed++;
+ art = item->data.article;
+ break;
+
+ case CANCEL_MSG:
+ if (failed>0)
+ cxn->cancel_failed++;
+ free(item->data.control->msgid);
+ free(item->data.control->folder);
+
+ if (item->counts_toward_size == 1)
+ art = item->data.control->article;
+
+ free(item->data.control );
+ break;
+
+ case CREATE_FOLDER:
+ if (failed>0)
+ cxn->create_failed++;
+ free(item->data.control->folder);
+
+ art = item->data.control->article;
+
+ free(item->data.control );
+ break;
+
+ case DELETE_FOLDER:
+ if (failed>0)
+ cxn->remove_failed++;
+ free(item->data.control->folder);
+
+ art = item->data.control->article;
+
+ free(item->data.control );
+ break;
+
+ default:
+ d_printf(0,"Unknown type to forget about\n");
+ break;
+ }
+
+ if (art!=NULL)
+ switch (failed)
+ {
+ case 0:
+ hostArticleAccepted (cxn->myHost, cxn, art);
+ break;
+ case 1:
+ hostArticleRejected (cxn->myHost, cxn, art);
+ break;
+ case 2:
+ hostTakeBackArticle (cxn->myHost, cxn, art);
+ break;
+ case 3:
+ hostArticleIsMissing(cxn->myHost, cxn, art);
+ break;
+ default:
+ d_printf(0,"Not understood\n");
+
+ }
+
+ free(item);
+ }
+
+ /*
+ * How much space is available in the queue
+ */
+
+ static int QueueSpace(Q_t *q)
+ {
+ int ret = QUEUE_MAX_SIZE - q->size;
+ if (ret < 0) ret = 0;
+ return ret;
+ }
+
+ /*
+ * How many items are in the queue
+ */
+
+ static int QueueItems(Q_t *q)
+ {
+ return q->size;
+ }
+
+
+ /***************************** END Queue functions ***********************************/
+
+ /***************************** Generic Parse Functions *******************************/
+
+ /* returns the end of the header */
+
+ static char *GetUntil(char *str)
+ {
+ while (((*str) != '\0') && ( (*str) != '\r') && ( (*str) != '\n'))
+ {
+ str++;
+ }
+
+ return str;
+ }
+
+ /*
+ * Finds the given header in the message
+ * Returns NULL if not found
+ *
+ * returns something malloc'ed
+ */
+ static conn_ret FindHeader(Buffer *bufs, char *header, char **start, char **end)
+ {
+ Buffer b;
+ int size;
+ char *str_base;
+ char *str;
+ int headerlen = strlen(header);
+
+ if (bufs==NULL)
+ {
+ if (start)
+ *start=NULL;
+ return RET_ARTICLE_BAD;
+ }
+
+ b = bufs[0];
+ size = bufferSize(b);
+ str_base = bufferBase(b);
+ str = str_base;
+
+ while ( (((int)str) - ((int)str_base)) < size - headerlen)
+ {
+ if (*str == header[0])
+ {
+ if ((strncasecmp(header, str, headerlen)==0) && ( *(str + headerlen)==':'))
+ {
+ if (start)
+ {
+ *start = str+headerlen+1;
+
+ /* get rid of leading whitespace */
+ while ( isspace((int) **start))
+ (*start)++;
+ }
+
+ if (end)
+ *end = GetUntil(str+headerlen+1);
+
+ return RET_OK;
+ }
+ } /*else if (*str == '\n') { */
+ /* end of headers */