-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.d
223 lines (186 loc) · 7.14 KB
/
connection.d
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
module connection;
struct EmailClient
{
static int[] imapOkayTable=[MAILIMAP_NO_ERROR,MAILIMAP_NO_ERROR_AUTHENTICATED,MAILIMAP_NO_ERROR_NON_AUTHENTICATED];
static int[string] storageTable=[ "POP3": POP3_STORAGE,
"IMAP": IMAP_STORAGE,
"NNTP": NNTP_STORAGE,
"MBOX": MBOX_STORAGE,
"MH": MH_STORAGE,
"MAILDIR": MAILDIR_STORAGE,
"FEED": FEED_STORAGE];
int driver;
char* server;
int port;
int connection_type = IMAP_STORAGE;
char* user;
char* password;
int auth_type = IMAP_AUTH_TYPE_PLAIN;
char* path;
char* cache_directory;
char* flags_directory;
int cached=0;
mailstorage *storage;
mailimap * imap=null;
mailpop3 * pop3=null;
this(string server, string auth)
{
int r;
auto parse=parseURI(server);
this.server=toZString(parse[1]);
this.port=to!short(parse[2]);
this.connection_type=toZString(storageTable[parse[0]]);
this.auth_type=toZString(authtype(auth,ret[0]));
this.flags_directory="/tmp";
switch(this.connection_type)
{
case POP3_STORAGE:
enforce((r=pop3_mailstorage_init(storage, server, port, NULL, connection_type,
auth_type, user, password, cached, cache_directory, flags_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initializing POP3 connection",r));
break;
case IMAP_STORAGE:
enforce((r=imap_mailstorage_init(storage, server, port, NULL, connection_type,
IMAP_AUTH_TYPE_PLAIN, user, password, cached, cache_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initializing IMAP connection",r));
break;
case NNTP_STORAGE:
enforce((r=nntp_mailstorage_init(storage, server, port, NULL, connection_type,
NNTP_AUTH_TYPE_PLAIN, user, password, cached, cache_directory, flags_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initializing NNTP connection",r));
break;
case MBOX_STORAGE:
enforce((r=mbox_mailstorage_init(storage, path, cached, cache_directory, flags_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initializing MBOX storage",r));
break;
case MH_STORAGE:
enforce((r=mh_mailstorage_init(storage, path, cached, cache_directory, flags_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initialzing MH storage",r));
break;
case MAILDIR_STORAGE:
enforce((r=maildir_mailstorage_init(storage, path, cached, cache_directory, flags_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initializing MAILDIR storage",r));
break;
case FEED_STORAGE:
enforce((r=feed_mailstorage_init(storage, path, cached, cache_directory, flags_directory))==MAIL_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error initialzing FEED storage",r));
break;
default:
new EmailException(EmailException.Kind.error,
"EmailClient: error initiialzing unknown storage type: "~this.connection_type);
}
}
~this()
{
switch(this.connection_type)
{
case IMAPS_STORAGE:
case IMAP_STORAGE:
mailimap_logout(imap);
mailimap_free(imap);
break;
case POP3_STORAGE:
mailpop3_quit(pop3);
mailpop3_free(pop3);
break;
default:
break;
}
}
void login(string user, string password)
{
int r;
this.user=user;
this.password=password;
switch(this.connection_type)
{
case IMAPS_STORAGE:
this.imap = mailimap_new(0, NULL);
enforce(imapOkayTable.contains(r=mailimap_ssl_connect(imap, this.server,this.port)),
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error making IMAP connection",r));
enforce(imapOkayTable.contains(r=mailimap_login(imap, argv[1], argv[2]),imapErrorTable)),
new EmailAuthException(EmailException.Kind.libetpan_auth,
"EmailClient: IMAP authentication failed for user "~user,r));
break;
case POP3_STORAGE:
this.pop3 = mailpop3_new(0, NULL);
enforce((r=mailpop3_ssl_connect(pop3, host, port))==MAILPOP3_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error making POP3 connection",r));
enforce((r=mailpop3_user(pop3, user))==MAILPOP3_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error setting POP3 user for"~ ZtoString(user),r));
enforce((r=mailpop3_pass(pop3, pass))==MAILPOP3_NO_ERROR,
new EmailException(EmailException.Kind.libetpan_abort,
"EmailClient: libetpan error setting POP3 password for"~ ZtoString(user),r));
default:
throw new EmailException(EmailException.Kind.args,
"EmailClient: "~this.connection_type~" not yet implemented in wrappers");
break;
}
return;
}
// should really return server response
void logout()
{
switch(this.connection_type)
{
case IMAP_STORAGE:
mailimap_logout(imap);
mailimap_free(imap);
break;
default:
break;
}
}
string[] capabilities()
{
return [""];
}
bool hasCapability(string capability)
{
}
void idle()
{
}
string[2][] idleCheck()
{
}
string[] idleDone()
{
}
// return folder type - should be Returns a dictionary containing the SELECT response. At least the EXISTS, FLAGS and RECENT keys are guaranteed to exist
// string[][2][]
void openFolder(string folderName)
{
enforce(imapOkayTable.contains(r=mailimap_select(imap, toZString(folderName))),
new EmailAuthException(EmailException.Kind.libetpan_auth,
"EmailClient: openFolder failed for: "~foldername,r));
}
}
struct UUID
{
mailstorage* storage;
mailfolder *folder;
void this(int driver, string server, int port, int connection_type,
string user,string password, int auth_type,string path,string cache_directory,string flags_directory)
{
throwOnError(storage = mailstorage_new(NULL));
throwOnError(init_storage(storage, driver, server, port, connection_type, user, password, auth_type, path, cache_directory, flags_directory),MAIL_NO_ERROR);
throwOnError(mailfolder * folder = mailfolder_new(storage, path, NULL));
throwOnError(mailfolder_connect(folder),MAIL_NO_ERROR);
}
void ~this()
{
mailfolder_free(folder);
mailstorage_free(storage);
}
}