8 |
|
|
9 |
|
IMAPHandler::IMAPHandler(const string& server, bool tls) |
10 |
|
{ |
11 |
+ |
letter = 'a'; |
12 |
+ |
number = 0; |
13 |
+ |
|
14 |
|
this->tls = tls; |
15 |
|
buffer = new char[BUFSIZ + 1]; |
16 |
|
|
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
< |
string input = getline(); |
87 |
< |
if (debug) cerr << input << "\n"; |
89 |
> |
getline(); |
90 |
|
} |
91 |
|
|
92 |
|
IMAPHandler::~IMAPHandler() |
113 |
|
|
114 |
|
if (success) |
115 |
|
{ |
116 |
< |
// |
116 |
> |
tls = true; |
117 |
> |
|
118 |
> |
SSL_load_error_strings(); |
119 |
> |
SSL_library_init(); |
120 |
> |
|
121 |
> |
char* seed = new char[BUFSIZ + 1]; |
122 |
> |
time_t moment = time(NULL); |
123 |
> |
sprintf(seed, "froofy%sfoofoo", ctime(&moment)); |
124 |
> |
|
125 |
> |
RAND_add(seed, strlen(seed), 0.007456); |
126 |
> |
|
127 |
> |
delete [] seed; |
128 |
> |
|
129 |
> |
ctx = SSL_CTX_new(TLSv1_client_method()); |
130 |
> |
if (ctx == NULL) |
131 |
> |
{ |
132 |
> |
cerr << program << ": SSL CTX New: " << |
133 |
> |
ERR_reason_error_string(ERR_get_error()) << "\n"; |
134 |
> |
exit(1); |
135 |
> |
} |
136 |
> |
|
137 |
> |
ssl = SSL_new(ctx); |
138 |
> |
|
139 |
> |
if (SSL_set_fd(ssl, sock) == 0) |
140 |
> |
{ |
141 |
> |
cerr << program << ": SSL Set FD: " << |
142 |
> |
ERR_reason_error_string(ERR_get_error()) << "\n"; |
143 |
> |
exit(1); |
144 |
> |
} |
145 |
> |
|
146 |
> |
if (int code = SSL_connect(ssl) <= 0) |
147 |
> |
{ |
148 |
> |
error(program + ": SSL Connect", code); |
149 |
> |
exit(1); |
150 |
> |
} |
151 |
|
} |
152 |
|
|
153 |
|
return answer; |
154 |
|
} |
155 |
|
|
156 |
+ |
string IMAPHandler::command() |
157 |
+ |
{ |
158 |
+ |
char buffer[4]; |
159 |
+ |
sprintf(buffer, "%03u", number++); |
160 |
+ |
|
161 |
+ |
string sequence = letter + string(buffer); |
162 |
+ |
|
163 |
+ |
if (number > 999) |
164 |
+ |
{ |
165 |
+ |
letter++; |
166 |
+ |
number = 0; |
167 |
+ |
} |
168 |
+ |
|
169 |
+ |
if (letter > 'z') |
170 |
+ |
{ |
171 |
+ |
letter = 'a'; |
172 |
+ |
} |
173 |
+ |
|
174 |
+ |
return sequence; |
175 |
+ |
} |
176 |
+ |
|
177 |
|
string IMAPHandler::imap(const string& imap) |
178 |
|
{ |
179 |
|
string result; |
180 |
|
|
181 |
+ |
string sequence = command(); |
182 |
+ |
putline(sequence + " " + imap); |
183 |
+ |
|
184 |
+ |
while (true) |
185 |
+ |
{ |
186 |
+ |
string input = getline(); |
187 |
+ |
|
188 |
+ |
if (input.find(sequence + " OK") == 0) |
189 |
+ |
{ |
190 |
+ |
success = true; |
191 |
+ |
break; |
192 |
+ |
} |
193 |
+ |
else if (input.find(sequence + " NO") == 0 || input.find(sequence + |
194 |
+ |
" BAD") == 0) |
195 |
+ |
{ |
196 |
+ |
success = false; |
197 |
+ |
break; |
198 |
+ |
} |
199 |
+ |
else |
200 |
+ |
{ |
201 |
+ |
result += input + "\r\n"; |
202 |
+ |
} |
203 |
+ |
} |
204 |
+ |
|
205 |
+ |
return result; |
206 |
+ |
} |
207 |
+ |
|
208 |
+ |
string IMAPHandler::imap(const string& imap, const string& args) |
209 |
+ |
{ |
210 |
+ |
string result; |
211 |
+ |
|
212 |
+ |
string sequence = command(); |
213 |
+ |
putline(sequence + " " + imap + " " + args); |
214 |
+ |
|
215 |
+ |
while (true) |
216 |
+ |
{ |
217 |
+ |
string input = getline(); |
218 |
+ |
|
219 |
+ |
if (input.find(sequence + " OK") == 0) |
220 |
+ |
{ |
221 |
+ |
success = true; |
222 |
+ |
break; |
223 |
+ |
} |
224 |
+ |
else if (input.find(sequence + " NO") == 0 || input.find(sequence + |
225 |
+ |
" BAD") == 0) |
226 |
+ |
{ |
227 |
+ |
success = false; |
228 |
+ |
break; |
229 |
+ |
} |
230 |
+ |
else |
231 |
+ |
{ |
232 |
+ |
result += input + "\r\n"; |
233 |
+ |
} |
234 |
+ |
} |
235 |
+ |
|
236 |
+ |
return result; |
237 |
+ |
} |
238 |
+ |
|
239 |
+ |
string IMAPHandler::imap(const string& imap, const string& args, const string& |
240 |
+ |
message) |
241 |
+ |
{ |
242 |
+ |
string result; |
243 |
+ |
|
244 |
|
// |
245 |
|
|
246 |
|
return result; |
248 |
|
|
249 |
|
void IMAPHandler::putline(const string line) |
250 |
|
{ |
251 |
+ |
if (debug) cerr << line << "\n"; |
252 |
+ |
|
253 |
|
sprintf(buffer, "%s\r\n", line.c_str()); |
254 |
|
|
255 |
|
if (tls) |
301 |
|
} |
302 |
|
while (byte != '\n'); |
303 |
|
|
304 |
+ |
if (debug) cerr << line << "\n"; |
305 |
+ |
|
306 |
|
return line; |
307 |
|
} |
308 |
|
|