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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
|
/*
* todo:
* 1. sync with imap server's flags
* 2. better algorithm for avoiding downloading message list.
* 3. get sender — eating envelope is lots of work!
*/
#include "common.h"
#include <libsec.h>
#include <auth.h>
#include "dat.h"
#define idprint(i, ...) if(i->flags & Fdebug) fprint(2, __VA_ARGS__); else {}
#pragma varargck argpos imap4cmd 2
#pragma varargck type "Z" char*
#pragma varargck type "U" uvlong
#pragma varargck type "U" vlong
static char confused[] = "confused about fetch response";
static char qsep[] = " \t\r\n";
static char Eimap4ctl[] = "bad imap4 control message";
enum{
/* cap */
Cnolog = 1<<0,
Ccram = 1<<1,
Cntlm = 1<<2,
/* flags */
Fssl = 1<<0,
Fdebug = 1<<1,
Fgmail = 1<<2,
};
typedef struct {
uvlong uid;
ulong sizes;
ulong dates;
} Fetchi;
typedef struct Imap Imap;
struct Imap {
char *mbox;
/* free this to free the strings below */
char *freep;
char *host;
char *user;
int refreshtime;
uchar cap;
uchar flags;
ulong tag;
ulong validity;
int nmsg;
int size;
Fetchi *f;
int nuid;
int muid;
/* open network connection */
Biobuf bin;
Biobuf bout;
int binit;
int fd;
};
enum
{
Qok = 0,
Qquote,
Qbackslash,
};
static int
needtoquote(Rune r)
{
if(r >= Runeself)
return Qquote;
if(r <= ' ')
return Qquote;
if(r == '(' || r == ')' || r == '{' || r == '%' || r == '*' || r == ']')
return Qquote;
if(r == '\\' || r == '"')
return Qbackslash;
return Qok;
}
static int
Zfmt(Fmt *f)
{
char *s, *t;
int w, quotes;
Rune r;
s = va_arg(f->args, char*);
if(s == 0 || *s == 0)
return fmtstrcpy(f, "\"\"");
quotes = 0;
for(t = s; *t; t += w){
w = chartorune(&r, t);
quotes |= needtoquote(r);
}
if(quotes == 0)
return fmtstrcpy(f, s);
fmtrune(f, '"');
for(t = s; *t; t += w){
w = chartorune(&r, t);
if(needtoquote(r) == Qbackslash)
fmtrune(f, '\\');
fmtrune(f, r);
}
return fmtrune(f, '"');
}
static int
Ufmt(Fmt *f)
{
char buf[20*2 + 2];
ulong a, b;
uvlong u;
u = va_arg(f->args, uvlong);
if(u == 1)
return fmtstrcpy(f, "nil");
if(u == 0)
return fmtstrcpy(f, "-");
a = u>>32;
b = u;
snprint(buf, sizeof buf, "%lud:%lud", a, b);
return fmtstrcpy(f, buf);
}
static void
imap4cmd(Imap *imap, char *fmt, ...)
{
char buf[256], *p;
va_list va;
va_start(va, fmt);
p = buf + sprint(buf, "9x%lud ", imap->tag);
vseprint(p, buf + sizeof buf, fmt, va);
va_end(va);
p = buf + strlen(buf);
if(p > buf + sizeof buf - 3)
sysfatal("imap4 command too long");
idprint(imap, "-> %s\n", buf);
strcpy(p, "\r\n");
Bwrite(&imap->bout, buf, strlen(buf));
Bflush(&imap->bout);
}
enum {
Ok,
No,
Bad,
Bye,
Exists,
Status,
Fetch,
Cap,
Auth,
Unknown,
};
static char *verblist[] = {
[Ok] "ok",
[No] "no",
[Bad] "bad",
[Bye] "bye",
[Exists] "exists",
[Status] "status",
[Fetch] "fetch",
[Cap] "capability",
[Auth] "authenticate",
};
static int
verbcode(char *verb)
{
int i;
char *q;
if(q = strchr(verb, ' '))
*q = '\0';
for(i = 0; i < nelem(verblist) - 1; i++)
if(strcmp(verblist[i], verb) == 0)
break;
if(q)
*q = ' ';
return i;
}
static vlong
mkuid(Imap *i, char *id)
{
vlong v;
v = (vlong)i->validity<<32;
return v | strtoul(id, 0, 10);
}
static vlong
xnum(char *s, int a, int b)
{
vlong v;
if(*s != a)
return -1;
v = strtoull(s + 1, &s, 10);
if(*s != b)
return -1;
return v;
}
static struct{
char *flag;
int e;
} ftab[] = {
"Answered", Fanswered,
"\\Deleted", Fdeleted,
"\\Draft", Fdraft,
"\\Flagged", Fflagged,
"\\Recent", Frecent,
"\\Seen", Fseen,
"\\Stored", Fstored,
};
static void
parseflags(Message *m, char *s)
{
char *f[10];
int i, j, j0, n;
n = tokenize(s, f, nelem(f));
qsort(f, n, sizeof *f, (int (*)(void*,void*))strcmp);
j = 0;
for(i = 0; i < n; i++)
for(j0 = j;; j++){
if(j == nelem(ftab)){
j = j0; /* restart search */
break;
}
if(strcmp(f[i], ftab[j].flag) == 0){
m->flags |= ftab[j].e;
break;
}
}
}
/* "17-Jul-1996 02:44:25 -0700" */
long
internaltounix(char *s)
{
Tm tm;
if(strlen(s) < 20 || s[2] != '-' || s[6] != '-')
return -1;
s[2] = ' ';
s[6] = ' ';
if(strtotm(s, &tm) == -1)
return -1;
return tm2sec(&tm);
}
static char*
qtoken(char *s, char *sep)
{
int quoting;
char *t;
quoting = 0;
t = s; /* s is output string, t is input string */
while(*t!='\0' && (quoting || utfrune(sep, *t)==nil)){
if(*t != '"' && *t != '(' && *t != ')'){
*s++ = *t++;
continue;
}
/* *t is a quote */
if(!quoting || *t == '('){
quoting++;
t++;
continue;
}
/* quoting and we're on a quote */
if(t[1] != '"'){
/* end of quoted section; absorb closing quote */
t++;
if(quoting > 0)
quoting--;
continue;
}
/* doubled quote; fold one quote into two */
t++;
*s++ = *t++;
}
if(*s != '\0'){
*s = '\0';
if(t == s)
t++;
}
return t;
}
int
imaptokenize(char *s, char **args, int maxargs)
{
int nargs;
for(nargs=0; nargs < maxargs; nargs++){
while(*s!='\0' && utfrune(qsep, *s)!=nil)
s++;
if(*s == '\0')
break;
args[nargs] = s;
s = qtoken(s, qsep);
}
return nargs;
}
static char*
fetchrsp(Imap *imap, char *p, Mailbox *, Message *m)
{
char *f[15], *s, *q;
int i, n, a;
ulong o, l;
uvlong v;
static char error[256];
extern void msgrealloc(Message*, ulong);
redux:
n = imaptokenize(p, f, nelem(f));
if(n%2)
return confused;
for(i = 0; i < n; i += 2){
if(strcmp(f[i], "internaldate") == 0){
l = internaltounix(f[i + 1]);
if(l < 418319360)
abort();
if(imap->nuid < imap->muid)
imap->f[imap->nuid].dates = l;
}else if(strcmp(f[i], "rfc822.size") == 0){
l = strtoul(f[i + 1], 0, 0);
if(m)
m->size = l;
else if(imap->nuid < imap->muid)
imap->f[imap->nuid].sizes = l;
}else if(strcmp(f[i], "uid") == 0){
v = mkuid(imap, f[1]);
if(m)
m->imapuid = v;
if(imap->nuid < imap->muid)
imap->f[imap->nuid].uid = v;
}else if(strcmp(f[i], "flags") == 0)
parseflags(m, f[i + 1]);
else if(strncmp(f[i], "body[]", 6) == 0){
s = f[i]+6;
o = 0;
if(*s == '<')
o = xnum(s, '<', '>');
if(o == -1)
return confused;
l = xnum(f[i + 1], '{', '}');
a = o + l - m->ibadchars - m->size;
if(a > 0){
assert(imap->flags & Fgmail);
m->size = o + l;
msgrealloc(m, m->size);
m->size -= m->ibadchars;
}
if(Bread(&imap->bin, m->start + o, l) != l){
snprint(error, sizeof error, "read: %r");
return error;
}
if(Bgetc(&imap->bin) == ')'){
while(Bgetc(&imap->bin) != '\n')
;
return 0;
}
/* evil */
if(!(p = Brdline(&imap->bin, '\n')))
return 0;
q = p + Blinelen(&imap->bin);
while(q > p && (q[-1] == '\n' || q[-1] == '\r'))
q--;
*q = 0;
lowercase(p);
idprint(imap, "<- %s\n", p);
goto redux;
}else
return confused;
}
return 0;
}
void
parsecap(Imap *imap, char *s)
{
char *t[32], *p;
int n, i;
s = strdup(s);
n = getfields(s, t, nelem(t), 0, " ");
for(i = 0; i < n; i++){
if(strncmp(t[i], "auth=", 5) == 0){
p = t[i] + 5;
if(strcmp(p, "cram-md5") == 0)
imap->cap |= Ccram;
if(strcmp(p, "ntlm") == 0)
imap->cap |= Cntlm;
}else if(strcmp(t[i], "logindisabled") == 0)
imap->cap |= Cnolog;
}
free(s);
}
/*
* get imap4 response line. there might be various
* data or other informational lines mixed in.
*/
static char*
imap4resp0(Imap *imap, Mailbox *mb, Message *m)
{
char *e, *line, *p, *ep, *op, *q, *verb;
int n, unexp;
static char error[256];
unexp = 0;
while(p = Brdline(&imap->bin, '\n')){
ep = p + Blinelen(&imap->bin);
while(ep > p && (ep[-1] == '\n' || ep[-1] == '\r'))
*--ep = '\0';
idprint(imap, "<- %s\n", p);
if(unexp && p[0] != '9' && p[1] != 'x')
if(strtoul(p + 2, &p, 10) != imap->tag)
continue;
if(p[0] != '+')
lowercase(p); /* botch */
switch(p[0]){
case '+': /* cram challenge */
if(ep - p > 2)
return p + 2;
break;
case '*':
if(p[1] != ' ')
continue;
p += 2;
line = p;
n = strtol(p, &p, 10);
if(*p == ' ')
p++;
verb = p;
if(p = strchr(verb, ' '))
p++;
else
p = verb + strlen(verb);
switch(verbcode(verb)){
case Bye:
/* early disconnect */
snprint(error, sizeof error, "%s", p);
return error;
case Ok:
case No:
case Bad:
/* human readable text at p; */
break;
case Exists:
imap->nmsg = n;
break;
case Cap:
parsecap(imap, p);
break;
case Status:
/* * status inbox (messages 2 uidvalidity 960164964) */
if(q = strstr(p, "messages"))
imap->nmsg = strtoul(q + 8, 0, 10);
if(q = strstr(p, "uidvalidity"))
imap->validity = strtoul(q + 11, 0, 10);
break;
case Fetch:
if(*p == '('){
p++;
if(ep[-1] == ')')
*--ep = 0;
}
if(e = fetchrsp(imap, p, mb, m))
eprint("imap: fetchrsp: %s\n", e);
imap->nuid++;
break;
case Auth:
break;
}
if(imap->tag == 0)
return line;
break;
case '9': /* response to our message */
op = p;
if(p[1] == 'x' && strtoul(p + 2, &p, 10) == imap->tag){
while(*p == ' ')
p++;
imap->tag++;
return p;
}
eprint("imap: expected %lud; got %s\n", imap->tag, op);
break;
default:
if(imap->flags&Fdebug || *p){
eprint("imap: unexpected line: %s\n", p);
unexp = 1;
}
}
}
snprint(error, sizeof error, "i/o error: %r\n");
return error;
}
static char*
imap4resp(Imap *i)
{
return imap4resp0(i, 0, 0);
}
static int
isokay(char *resp)
{
return cistrncmp(resp, "OK", 2) == 0;
}
static char*
findflag(int idx)
{
int i;
for(i = 0; i < nelem(ftab); i++)
if(ftab[i].e == 1<<idx)
return ftab[i].flag;
return nil;
}
static void
imap4modflags(Mailbox *mb, Message *m, int flags)
{
char buf[128], *p, *e, *fs;
int i, f;
Imap *imap;
imap = mb->aux;
e = buf + sizeof buf;
p = buf;
f = flags & ~Frecent;
for(i = 0; i < Nflags; i++)
if(f & 1<<i && (fs = findflag(i)))
p = seprint(p, e, "%s ", fs);
if(p > buf){
p[-1] = 0;
imap4cmd(imap, "uid store %lud flags (%s)", (ulong)m->imapuid, buf);
imap4resp(imap);
}
}
static char*
imap4cram(Imap *imap)
{
char *s, *p, ch[128], usr[64], rbuf[128], ubuf[128], ebuf[192];
int i, n, l;
fmtinstall('[', encodefmt);
imap4cmd(imap, "authenticate cram-md5");
p = imap4resp(imap);
if(p == nil)
return "no challenge";
l = dec64((uchar*)ch, sizeof ch, p, strlen(p));
if(l == -1)
return "bad base64";
ch[l] = 0;
idprint(imap, "challenge [%s]\n", ch);
if(imap->user == nil)
imap->user = getlog();
n = auth_respond(ch, l, usr, sizeof usr, rbuf, sizeof rbuf, auth_getkey,
"proto=cram role=client server=%q user=%s", imap->host, imap->user);
if(n == -1)
return "cannot find IMAP password";
for(i = 0; i < n; i++)
if(rbuf[i] >= 'A' && rbuf[i] <= 'Z')
rbuf[i] += 'a' - 'A';
l = snprint(ubuf, sizeof ubuf, "%s %.*s", usr, n, rbuf);
idprint(imap, "raw cram [%s]\n", ubuf);
snprint(ebuf, sizeof ebuf, "%.*[", l, ubuf);
imap->tag = 1;
idprint(imap, "-> %s\n", ebuf);
Bprint(&imap->bout, "%s\r\n", ebuf);
Bflush(&imap->bout);
if(!isokay(s = imap4resp(imap)))
return s;
return nil;
}
/*
* authenticate to IMAP4 server using NTLM (untested)
*
* http://davenport.sourceforge.net/ntlm.html#ntlmImapAuthentication
* http://msdn.microsoft.com/en-us/library/cc236621%28PROT.13%29.aspx
*/
static uchar*
psecb(uchar *p, uint o, int n)
{
p[0] = n;
p[1] = n>>8;
p[2] = n;
p[3] = n>>8;
p[4] = o;
p[5] = o>>8;
p[6] = o>>16;
p[7] = o>>24;
return p+8;
}
static uchar*
psecq(uchar *q, char *s, int n)
{
memcpy(q, s, n);
return q+n;
}
static char*
imap4ntlm(Imap *imap)
{
char *s, ruser[64], enc[256];
uchar buf[128], *p, *ep, *q, *eq, *chal;
int n;
MSchapreply mcr;
imap4cmd(imap, "authenticate ntlm");
imap4resp(imap);
/* simple NtLmNegotiate blob with NTLM+OEM flags */
imap4cmd(imap, "TlRMTVNTUAABAAAAAgIAAA==");
s = imap4resp(imap);
n = dec64(buf, sizeof buf, s, strlen(s));
if(n < 32 || memcmp(buf, "NTLMSSP", 8) != 0)
return "bad NtLmChallenge";
chal = buf+24;
if(auth_respond(chal, 8, ruser, sizeof ruser,
&mcr, sizeof mcr, auth_getkey,
"proto=mschap role=client service=imap server=%q user?",
imap->host) < 0)
return "auth_respond failed";
/* prepare NtLmAuthenticate blob */
memset(buf, sizeof buf, 0);
p = buf;
ep = p + 8 + 6*8 + 2*4;
q = ep;
eq = buf + sizeof buf;
memcpy(p, "NTLMSSP", 8); /* magic */
p += 8;
*p++ = 3;
*p++ = 0;
*p++ = 0;
*p++ = 0;
p = psecb(p, q-buf, 24); /* LMresp */
q = psecq(q, mcr.LMresp, 24);
p = psecb(p, q-buf, 24); /* NTresp */
q = psecq(q, mcr.NTresp, 24);
p = psecb(p, q-buf, 0); /* realm */
n = strlen(ruser);
p = psecb(p, q-buf, n); /* user name */
q = psecq(q, ruser, n);
p = psecb(p, q-buf, 0); /* workstation name */
p = psecb(p, q-buf, 0); /* session key */
*p++ = 0x02; /* flags: oem(2)|ntlm(0x200) */
*p++ = 0x02;
*p++ = 0;
*p++ = 0;
if(p > ep || q > eq)
return "error creating NtLmAuthenticate";
enc64(enc, sizeof enc, buf, q-buf);
imap4cmd(imap, enc);
if(!isokay(s = imap4resp(imap)))
return s;
return nil;
}
static char*
imap4passwd(Imap *imap)
{
char *s;
UserPasswd *up;
if(imap->user != nil)
up = auth_getuserpasswd(auth_getkey, "proto=pass service=imap server=%q user=%q", imap->host, imap->user);
else
up = auth_getuserpasswd(auth_getkey, "proto=pass service=imap server=%q", imap->host);
if(up == nil)
return "cannot find IMAP password";
imap->tag = 1;
imap4cmd(imap, "login %Z %Z", up->user, up->passwd);
free(up);
if(!isokay(s = imap4resp(imap)))
return s;
return nil;
}
static char*
imap4login(Imap *imap)
{
char *e;
if(imap->cap & Ccram)
e = imap4cram(imap);
else if(imap->cap & Cntlm)
e = imap4ntlm(imap);
else
e = imap4passwd(imap);
if(e)
return e;
imap4cmd(imap, "select %Z", imap->mbox);
if(!isokay(e = imap4resp(imap)))
return e;
return nil;
}
static char*
imaperrstr(char *host, char *port)
{
char err[ERRMAX];
static char buf[256];
err[0] = 0;
errstr(err, sizeof err);
snprint(buf, sizeof buf, "%s/%s:%s", host, port, err);
return buf;
}
static void
imap4disconnect(Imap *imap)
{
if(imap->binit){
Bterm(&imap->bin);
Bterm(&imap->bout);
imap->binit = 0;
}
if(imap->fd >= 0){
close(imap->fd);
imap->fd = -1;
}
}
char*
capabilties(Imap *imap)
{
char * err;
imap4cmd(imap, "capability");
imap4resp(imap);
err = imap4resp(imap);
if(isokay(err))
err = 0;
return err;
}
static char*
imap4dial(Imap *imap)
{
char *err, *port;
if(imap->fd >= 0){
imap4cmd(imap, "noop");
if(isokay(imap4resp(imap)))
return nil;
imap4disconnect(imap);
}
if(imap->flags & Fssl)
port = "imaps";
else
port = "imap";
if((imap->fd = dial(netmkaddr(imap->host, "net", port), 0, 0, 0)) < 0)
return imaperrstr(imap->host, port);
if(imap->flags & Fssl && (imap->fd = wraptls(imap->fd, imap->host)) < 0){
err = imaperrstr(imap->host, port);
imap4disconnect(imap);
return err;
}
assert(imap->binit == 0);
Binit(&imap->bin, imap->fd, OREAD);
Binit(&imap->bout, imap->fd, OWRITE);
imap->binit = 1;
imap->tag = 0;
err = imap4resp(imap);
if(!isokay(err))
return "error in initial IMAP handshake";
if((err = capabilties(imap)) || (err = imap4login(imap))){
eprint("imap: err is %s\n", err);
imap4disconnect(imap);
return err;
}
return nil;
}
static void
imap4hangup(Imap *imap)
{
imap4cmd(imap, "logout");
imap4resp(imap);
imap4disconnect(imap);
}
/* gmail lies about message sizes */
static ulong
gmaildiscount(Message *m, uvlong o, ulong l)
{
if((m->cstate&Cidx) == 0)
if(o + l == m->size)
return l + 100 + (o + l)/5;
return l;
}
static int
imap4fetch(Mailbox *mb, Message *m, uvlong o, ulong l)
{
Imap *imap;
imap = mb->aux;
if(imap->flags & Fgmail)
l = gmaildiscount(m, o, l);
idprint(imap, "uid fetch %lud (body.peek[]<%llud.%lud>)\n", (ulong)m->imapuid, o, l);
imap4cmd(imap, "uid fetch %lud (body.peek[]<%llud.%lud>)", (ulong)m->imapuid, o, l);
if(!isokay(imap4resp0(imap, mb, m))){
eprint("imap: imap fetch failed\n");
return -1;
}
return 0;
}
static uvlong
datesec(Imap *imap, int i)
{
int j;
uvlong v;
Fetchi *f;
f = imap->f;
v = (uvlong)f[i].dates << 8;
/* shifty; these sequences should be stable. */
for(j = i; j-- > 0; )
if(f[i].dates != f[j].dates)
break;
v |= i - (j + 1);
return v;
}
static int
vcmp(vlong a, vlong b)
{
a -= b;
if(a > 0)
return 1;
if(a < 0)
return -1;
return 0;
}
static int
fetchicmp(Fetchi *f1, Fetchi *f2)
{
return vcmp(f1->uid, f2->uid);
}
static char*
imap4read(Imap *imap, Mailbox *mb)
{
char *s;
int i, n, c;
Fetchi *f;
Message *m, **ll;
imap4cmd(imap, "status %Z (messages uidvalidity)", imap->mbox);
if(!isokay(s = imap4resp(imap)))
return s;
imap->nuid = 0;
imap->muid = imap->nmsg;
imap->f = erealloc(imap->f, imap->nmsg*sizeof imap->f[0]);
if(imap->nmsg > 0){
imap4cmd(imap, "uid fetch 1:* (uid rfc822.size internaldate)");
if(!isokay(s = imap4resp(imap)))
return s;
}
f = imap->f;
n = imap->nuid;
if(n > imap->muid){
idprint(imap, "partial sync %d > %d\n", n, imap->muid);
n = imap->nuid = imap->muid;
} else if(n < imap->nmsg)
idprint(imap, "partial sync %d < %d\n", n, imap->nmsg);
qsort(f, n, sizeof f[0], (int(*)(void*, void*))fetchicmp);
ll = &mb->root->part;
for(i = 0; (m = *ll) != nil || i < n; ){
c = -1;
if(i >= n)
c = 1;
else if(m){
if(m->imapuid == 0)
m->imapuid = strtoull(m->idxaux, 0, 0);
c = vcmp(f[i].uid, m->imapuid);
}
idprint(imap, "consider %U and %U -> %d\n", i<n? f[i].uid: 0, m? m->imapuid: 1, c);
if(c < 0){
/* new message */
idprint(imap, "new: %U (%U)\n", f[i].uid, m? m->imapuid: 0);
if(f[i].sizes == 0 || f[i].sizes > Maxmsg){
idprint(imap, "skipping bad size: %lud\n", f[i].sizes);
i++;
continue;
}
m = newmessage(mb->root);
m->inmbox = 1;
m->idxaux = smprint("%llud", f[i].uid);
m->imapuid = f[i].uid;
m->fileid = datesec(imap, i);
m->size = f[i].sizes;
m->next = *ll;
*ll = m;
ll = &m->next;
i++;
}else if(c > 0){
/* deleted message; */
idprint(imap, "deleted: %U (%U)\n", i<n? f[i].uid: 0, m? m->imapuid: 0);
m->inmbox = 0;
m->deleted = Disappear;
ll = &m->next;
}else{
ll = &m->next;
i++;
}
}
return nil;
}
static void
imap4delete(Mailbox *mb, Message *m)
{
Imap *imap;
imap = mb->aux;
if((ulong)(m->imapuid>>32) == imap->validity){
imap4cmd(imap, "uid store %lud +flags (\\Deleted)", (ulong)m->imapuid);
imap4resp(imap);
imap4cmd(imap, "expunge");
imap4resp(imap);
// if(!isokay(imap4resp(imap))
// return -1;
}
m->inmbox = 0;
}
static char*
imap4sync(Mailbox *mb)
{
char *err;
Imap *imap;
imap = mb->aux;
if(err = imap4dial(imap))
goto out;
err = imap4read(imap, mb);
out:
mb->waketime = (ulong)time(0) + imap->refreshtime;
return err;
}
static char*
imap4ctl(Mailbox *mb, int argc, char **argv)
{
Imap *imap;
imap = mb->aux;
if(argc < 1)
return Eimap4ctl;
if(argc == 1 && strcmp(argv[0], "debug") == 0){
imap->flags ^= Fdebug;
return nil;
}
if(argc == 2 && strcmp(argv[0], "uid") == 0){
uvlong l;
Message *m;
for(m = mb->root->part; m; m = m->next)
if(strcmp(argv[1], m->name) == 0){
l = strtoull(m->idxaux, 0, 0);
fprint(2, "uid %s %lud %lud %lud %lud\n", m->name, (ulong)(l>>32), (ulong)l,
(ulong)(m->imapuid>>32), (ulong)m->imapuid);
}
return nil;
}
if(strcmp(argv[0], "refresh") == 0)
switch(argc){
case 1:
imap->refreshtime = 60;
return nil;
case 2:
imap->refreshtime = atoi(argv[1]);
return nil;
}
return Eimap4ctl;
}
static void
imap4close(Mailbox *mb)
{
Imap *imap;
imap = mb->aux;
imap4disconnect(imap);
free(imap->f);
free(imap);
}
static char*
mkmbox(Imap *imap, char *p, char *e)
{
p = seprint(p, e, "%s/box/%s/imap.%s", MAILROOT, getlog(), imap->host);
if(imap->user && strcmp(imap->user, getlog()))
p = seprint(p, e, ".%s", imap->user);
if(cistrcmp(imap->mbox, "inbox"))
p = seprint(p, e, ".%s", imap->mbox);
return p;
}
static char*
findmbox(char *p)
{
char *f[10], path[Pathlen];
int nf;
snprint(path, sizeof path, "%s", p);
nf = getfields(path, f, 5, 0, "/");
if(nf < 3)
return nil;
return f[nf - 1];
}
static char*
imap4rename(Mailbox *mb, char *p2, int)
{
char *r, *new;
Imap *imap;
imap = mb->aux;
new = findmbox(p2);
idprint(imap, "rename %s %s\n", imap->mbox, new);
imap4cmd(imap, "rename %s %s", imap->mbox, new);
r = imap4resp(imap);
if(!isokay(r))
return r;
free(imap->mbox);
imap->mbox = smprint("%s", new);
mkmbox(imap, mb->path, mb->path + sizeof mb->path);
return 0;
}
/*
* incomplete; when we say remove we want to get subfolders, too.
* so we need to to a list, and recursivly nuke folders.
*/
static char*
imap4remove(Mailbox *mb, int flags)
{
char *r;
Imap *imap;
imap = mb->aux;
idprint(imap, "remove %s\n", imap->mbox);
imap4cmd(imap, "delete %s", imap->mbox);
r = imap4resp(imap);
if(!isokay(r))
return r;
if(flags & Rtrunc){
imap4cmd(imap, "create %s", imap->mbox);
r = imap4resp(imap);
if(!isokay(r))
return r;
}
return 0;
}
char*
imap4mbox(Mailbox *mb, char *path)
{
char *f[10];
uchar flags;
int nf;
Imap *imap;
fmtinstall('Z', Zfmt);
fmtinstall('U', Ufmt);
if(strncmp(path, "/imap/", 6) == 0)
flags = 0;
else if(strncmp(path, "/imaps/", 7) == 0)
flags = Fssl;
else
return Enotme;
path = strdup(path);
if(path == nil)
return "out of memory";
nf = getfields(path, f, 5, 0, "/");
if(nf < 3){
free(path);
return "bad imap path syntax /imap[s]/system[/user[/mailbox]]";
}
imap = emalloc(sizeof *imap);
imap->fd = -1;
imap->freep = path;
imap->flags = flags;
imap->host = f[2];
if(strstr(imap->host, "gmail.com"))
imap->flags |= Fgmail;
imap->refreshtime = 60;
if(nf < 4)
imap->user = nil;
else
imap->user = f[3];
if(nf < 5)
imap->mbox = strdup("inbox");
else
imap->mbox = strdup(f[4]);
mkmbox(imap, mb->path, mb->path + sizeof mb->path);
mb->aux = imap;
mb->sync = imap4sync;
mb->close = imap4close;
mb->ctl = imap4ctl;
mb->fetch = imap4fetch;
mb->delete = imap4delete;
mb->rename = imap4rename;
// mb->remove = imap4remove;
mb->modflags = imap4modflags;
mb->addfrom = 1;
return nil;
}
|