| 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
 | /*
** Copyright 2002-2004, Double Precision Inc.
**
** See COPYING for distribution information.
*/
#include "mail.H"
#include "sync.H"
#include "driver.H"
#include "mbox.H"
#include "runlater.H"
#include "logininfo.H"
#include <iostream>
#include <errno.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pwd.h>
#include <arpa/inet.h>
#include "rfc822/rfc822.h"
#include "unicode/courier-unicode.h"
#include "maildir/maildiraclt.h"
#include "misc.H"
#include <fstream>
using namespace std;
list<mail::account *> mail::account::mailaccount_list;
//
// Initialize mail::folder's default.  mail::folder is a superclass for
// mail folder objects.  Each mail account type is expected to use a
// subclass for the actual implementation.
//
// Each mail::folder object is associated with a corresponding mail::account
// object.  The constructor saves a pointer, and provides methods that
// the subclass can use to determine whether the mail::account object still exists
mail::folder::folder(mail::account *ptr) : myServer(ptr)
{
}
mail::folder::~folder()
{
}
//
// The first order of business for any mail::folder method call is to
// determine whether the mail::account object still exists.  This method is
// a shortcut for most methods that receive a mail::callback object.
// If the mail::account object was destroyed, callback's fail method is called
// appropriately.
bool mail::folder::isDestroyed(mail::callback &callback) const
{
	if (myServer.isDestroyed())
	{
		callback.fail("Server connection closed.");
		return true;
	}
	return false;
}
bool mail::folder::isDestroyed() const
{
	return myServer.isDestroyed();
}
string mail::folder::isNewsgroup() const
{
	return "";
}
void mail::folder::getMyRights(mail::callback &getCallback,
			       std::string &rights) const
{
	getCallback.fail("This server does not implement access control lists.");
}
void mail::folder::getRights(mail::callback &getCallback,
			     std::list<std::pair<std::string, std::string> >
			     &rights) const
{
	rights.clear();
	getCallback.fail("This server does not implement access control lists.");
}
void mail::folder::setRights(mail::callback &setCallback,
			     string &errorIdentifier,
			     vector<std::string> &errorRights,
			     string identifier,
			     string rights) const
{
	errorIdentifier="";
	errorRights.clear();
	setCallback.fail("This server does not implement access control lists.");
}
void mail::folder::delRights(mail::callback &setCallback,
			     string &errorIdentifier,
			     vector<std::string> &errorRights,
			     std::string identifier) const
{
	errorIdentifier="";
	errorRights.clear();
	setCallback.fail("This server does not implement access control lists.");
}
//
// callback::folder miscellania
mail::callback::folder::folder()
{
}
mail::callback::folder::~folder()
{
}
void mail::callback::folder::saveSnapshot(std::string snapshotId)
{
}
// mail::callback::folderList miscellania
mail::callback::folderList::folderList()
{
}
mail::callback::folderList::~folderList()
{
}
// mail::callback::folderInfo miscellania
mail::callback::folderInfo::folderInfo()
	: messageCount(0), unreadCount(0), fastInfo(false)
{
}
mail::callback::folderInfo::~folderInfo()
{
}
void mail::callback::folderInfo::success()
{
}
// messageInfo miscellania
mail::messageInfo::messageInfo()
{
	uid="";
	draft=replied=marked=deleted=unread=recent=false;
}
mail::messageInfo::messageInfo(string s)
{
	uid="";
	draft=replied=marked=deleted=unread=recent=false;
	string::iterator b=s.begin(), e=s.end();
	while (b != e)
	{
		switch (*b++) {
		case 'D':
			draft=true;
			break;
		case 'R':
			replied=true;
			break;
		case 'M':
			marked=true;
			break;
		case 'T':
			deleted=true;
			break;
		case 'U':
			unread=true;
			break;
		case 'C':
			recent=true;
			break;
		case ':':
			uid=string(b, e);
			b=e;
			break;
		}
	}
}
mail::messageInfo::~messageInfo()
{
}
mail::messageInfo::operator string() const
{
	string s;
	if (draft) s += "D";
	if (replied) s += "R";
	if (marked) s += "M";
	if (deleted) s += "T";
	if (unread) s += "U";
	if (recent) s += "C";
	return s + ":" + uid;
}
//
// mail::account superclass constructor.  The main task at hand is to save
// a reference to the disconnect callback, for later use, and to initialize
// the default character set.
//
mail::account::account(mail::callback::disconnect &callbackArg)
	: disconnect_callback(callbackArg)
{
	my_mailaccount_p=mailaccount_list.insert(mailaccount_list.end(), this);
}
mail::account::~account()
{
	// We can't just remove (this) from mailaccount_list, because we
	// might be iterating mail:;account::process().
	*my_mailaccount_p=NULL;
}
//////////////////////////////////////////////////////////////////////////////
//
// Utility functions.
//
//////////////////////////////////////////////////////////////////////////////
LIBMAIL_START
string hostname()
{
	char h[512];
	h[sizeof(h)-1]=0;
	if (gethostname(h, sizeof(h)-1) < 0)
		strcpy(h, "localhost");
	return h;
}
string homedir()
{
	const char *h=getenv("HOME");
	struct passwd *pw=getpwuid(getuid());
	if (!pw)
		return "";
	if (h == NULL || !*h)
		h=pw->pw_dir;
	return h;
}
void upper(string &w)
{
	for (string::iterator b=w.begin(), e=w.end(); b != e; ++b)
		if (*b >= 'a' && *b <= 'z')
			*b += 'A'-'a';
}
LIBMAIL_END
static const char x[]="0123456789ABCDEF";
static string encword(string w)
{
	string ww="";
	string::iterator b=w.begin(), e=w.end(), c;
	while (b != e)
	{
		for (c=b; c != e; c++)
			if ( *c == '@' || *c == '/' || *c == '%' || *c == ':')
				break;
		ww.append(b, c);
		b=c;
		if (b != e)
		{
			ww += '%';
			ww += x[ (*b / 16) & 15 ];
			ww += x[ *b & 15];
			b++;
		}
	}
	return ww;
}
LIBMAIL_START
string loginUrlEncode(string method, string server, string uid,
		      string pwd)
{
	return (method + "://" +
		(uid.size() > 0 ? encword(uid)
		 + (pwd.size() > 0 ? ":" + encword(pwd):"") + "@":"")
		+ server);
}
LIBMAIL_END
static string decword(string w)
{
	string ww="";
	string::iterator b=w.begin(), e=w.end(), c;
	while (b != e)
	{
		for (c=b; c != e; c++)
			if ( *c == '%' && e-c > 2)
				break;
		ww.append(b, c);
		b=c;
		if (b != e)
		{
			b++;
			const char *c1= strchr(x, *b++);
			const char *c2= strchr(x, *b++);
			char c=0;
			if (c1) c= (c1-x) * 16;
			if (c2) c += c2-x;
			ww += c;
		}
	}
	return ww;
}
LIBMAIL_START
// Parse url format: method://uid:pwd@server/option/option...
bool loginUrlDecode(string url, mail::loginInfo &loginInfo)
{
	size_t n=url.find(':');
	if (n == std::string::npos)
		return false;
	loginInfo.method=url.substr(0, n);
	if (url.size() - n < 3 || url[n+1] != '/' || url[n+2] != '/')
		return false;
	string serverStr=url.substr(n+3);
	string options="";
	n=serverStr.find('/');
	if (n != std::string::npos)
	{
		options=serverStr.substr(n+1);
		serverStr.erase(n);
	}
	n=serverStr.rfind('@');
	string uidpwd="";
	if (n != std::string::npos)
	{
		uidpwd=serverStr.substr(0, n);
		serverStr=serverStr.substr(n+1);
	}
	loginInfo.server=serverStr;
	n=uidpwd.find(':');
	string pwd="";
	if (n != std::string::npos)
	{
		pwd=uidpwd.substr(n+1);
		uidpwd=uidpwd.substr(0, n);
	}
	loginInfo.uid=decword(uidpwd);
	loginInfo.pwd=decword(pwd);
	while (options.size() > 0)
	{
		n=options.find('/');
		string option;
		if (n == std::string::npos)
		{
			option=options;
			options="";
		}
		else
		{
			option=options.substr(0, n);
			options=options.substr(n+1);
		}
		string optionVal;
		n=option.find('=');
		if (n != std::string::npos)
		{
			optionVal=option.substr(n+1);
			option=option.substr(0, n);
		}
		loginInfo.options.insert(make_pair(option, optionVal));
	}
	return true;
}
LIBMAIL_END
mail::account::openInfo::openInfo() : loginCallbackObj(NULL)
{
}
mail::account::openInfo::~openInfo()
{
}
mail::account *mail::account::open(mail::account::openInfo oi,
				   mail::callback &callback,
				   mail::callback::disconnect
				   &disconnectCallback)
{
	mail::driver **l=mail::driver::get_driver_list();
	while (*l)
	{
		mail::account *a;
		if ( (*(*l)->open_func)(a, oi, callback, disconnectCallback))
		{
			if (!a)
				callback.fail(strerror(errno));
			return a;
		}
		l++;
	}
	callback.fail("Invalid mail account URL.");
	return NULL;
}
bool mail::account::isRemoteUrl(std::string url)
{
	mail::driver **l=mail::driver::get_driver_list();
	while (*l)
	{
		bool flag;
		if ( (*(*l)->isRemote_func)(url, flag))
			return flag;
		l++;
	}
	return false;
}
// Default implementation of getSendFolder: no such luck.
mail::folder *mail::account::getSendFolder(const class mail::smtpInfo &info,
					   const mail::folder *folder,
					   string &errmsg)
{
	errno=ENOENT;
	errmsg="Not implemented.";
	return NULL;
}
//
// The main function.  Where everything good happens.
//
// mail::account::process calls each existing mail::account object's handler method.
//
void mail::account::process(vector<pollfd> &fds, int &timeout)
{
	list<mail::account *>::iterator b, e, cur;
	fds.clear();
	b=mailaccount_list.begin();
	e=mailaccount_list.end();
	while (b != e)
	{
		// mail::account may destroy itself, so increment the iterator
		// before invoking the method
		cur=b;
		b++;
		if (*cur == NULL)
			mailaccount_list.erase(cur);
		else
		{
			(*cur)->handler(fds, timeout);
		}
	}
	// Postponed handlers.
	mail::runLater::checkRunLater(timeout);
}
//
// Resume after suspend.
//
void mail::account::resume()
{
	list<mail::account *>::iterator b, e, cur;
	b=mailaccount_list.begin();
	e=mailaccount_list.end();
	while (b != e)
	{
		// mail::account may destroy itself, so increment the iterator
		// before invoking the method
		cur=b;
		++b;
		if (*cur)
			(*cur)->resumed();
	}
}
void mail::account::updateNotify(bool enableDisable, callback &callbackArg)
{
	callbackArg.success("OK");
}
void mail::account::moveMessagesTo(const std::vector<size_t> &messages,
				   mail::folder *copyTo,
				   mail::callback &callback)
{
	generic::genericMoveMessages(this, messages, copyTo, callback);
}
#if 0
void mail::account::removeMessages(const std::vector<size_t> &messages,
				   callback &cb)
{
	cb.fail("TODO");
}
#endif
void mail::account::updateKeywords(const vector<size_t> &messages,
				   const set<string> &keywords,
				   bool setOrChange,
				   // false: set, true: see changeTo
				   bool changeTo,
				   mail::callback &cb)
{
	cb.fail("Not implemented");
}
void mail::account::updateKeywords(const vector<size_t> &messages,
				   const vector<string> &keywords,
				   bool setOrChange,
				   bool changeTo,
				   mail::callback &cb)
{
	set<string> s;
	s.insert(keywords.begin(), keywords.end());
	updateKeywords(messages, s, setOrChange, changeTo, cb);
}
void mail::account::updateKeywords(const vector<size_t> &messages,
				   const list<string> &keywords,
				   bool setOrChange,
				   bool changeTo,
				   mail::callback &cb)
{
	set<string> s;
	s.insert(keywords.begin(), keywords.end());
	updateKeywords(messages, s, setOrChange, changeTo, cb);
}
void mail::account::getFolderKeywordInfo(size_t messageNum,
					 set<string> &keywordRet)
{
	keywordRet.clear();
}
//
// Must provide default methods for mail::callback::message, not all
// subclasses must implement every method.
//
mail::callback::message::message()
{
}
mail::callback::message::~message()
{
}
void mail::callback::message::messageEnvelopeCallback(size_t n,
						      const mail::envelope
						      &env)
{
}
void mail::callback::message::messageReferencesCallback(size_t n,
							const vector<string>
							&references)
{
}
void mail::callback::message::messageStructureCallback(size_t n,
							 const mail::mimestruct
							 &mstruct)
{
}
void mail::callback::message::messageTextCallback(size_t n, string text)
{
}
void mail::callback::message
::messageArrivalDateCallback(size_t messageNumber,
			     time_t datetime)
{
}
void mail::callback::message::messageSizeCallback(size_t messageNumber,
						    unsigned long messagesize)
{
}
LIBMAIL_START
string toutf8(string s)
{
	string u;
	char *p=unicode_convert_toutf8(s.c_str(),
					 unicode_default_chset(), NULL);
	try {
		u=p;
		free(p);
	} catch (...) {
		free(p);
		LIBMAIL_THROW(LIBMAIL_THROW_EMPTY);
	}
	return u;
}
string fromutf8(string s)
{
	string u;
	char *p=unicode_convert_fromutf8(s.c_str(), unicode_default_chset(),
					   NULL);
	try {
		u=p;
		free(p);
	} catch (...) {
		free(p);
		LIBMAIL_THROW(LIBMAIL_THROW_EMPTY);
	}
	return u;
}
LIBMAIL_END
//
// Common code for mbox and maildir
//
string mail::mbox::translatePathCommon(string path,
				       const char *verbotten,
				       const char *sep)
{
	string newpath;
	do
	{
		string component;
		size_t n=path.find('/');
		if (n == std::string::npos)
		{
			component=path;
			path="";
		}
		else
		{
			component=path.substr(0, n);
			path=path.substr(n+1);
		}
		vector<unicode_char> ucvec;
		unicode_char *uc;
		size_t ucsize;
		unicode_convert_handle_t h;
		if ((h=unicode_convert_tou_init(unicode_default_chset(),
						  &uc, &ucsize, 1)) == NULL)
		{
			uc=NULL;
		}
		else
		{
			unicode_convert(h, component.c_str(),
					  component.size());
			if (unicode_convert_deinit(h, NULL))
				uc=NULL;
		}
		if (!uc)
		{
			errno=EINVAL;
			return "";
		}
		try {
			size_t n;
			for (n=0; uc[n]; n++)
				;
			ucvec.insert(ucvec.end(), uc, uc+n);
			for (n=0; n<ucvec.size(); )
			{
				if (ucvec[n] == '\\' && n+1 < ucvec.size())
				{
					ucvec.erase(ucvec.begin()+n,
						    ucvec.begin()+n+1);
					n++;
					continue;
				}
				if (ucvec[n] == '%')
				{
					unicode_char ucnum=0;
					size_t o=n+1;
					while (o < ucvec.size())
					{
						if ((unsigned char)ucvec[o]
						    != ucvec[o])
							break;
						if (!isdigit(ucvec[o]))
							break;
						ucnum=ucnum * 10 +
							ucvec[o]-'0';
						++o;
					}
					if (o < ucvec.size() &&
					    ucvec[o] == ';')
						++o;
					ucvec[n++]=ucnum;
					ucvec.erase(ucvec.begin()+n,
						    ucvec.begin()+o);
					continue;
				}
				n++;
			}
		} catch (...) {
			free(uc);
			LIBMAIL_THROW(LIBMAIL_THROW_EMPTY);
		}
		free(uc);
		std::string p(mail::iconvert::convert(ucvec,
						      std::string(unicode_x_imap_modutf7 " ")
						      + verbotten));
		if (newpath.size() > 0)
			newpath += sep;
		newpath += p;
	} while (path.size() > 0);
	return newpath;
}
 |