summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/generic/nntpdriver_tools.c
blob: 26be916200a81295bb08fa3e33dcc1b222365f8d (plain)
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
/*
 * libEtPan! -- a mail stuff library
 *
 * Copyright (C) 2001, 2002 - DINH Viet Hoa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the libEtPan! project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * $Id$
 */

#include "nntpdriver_tools.h"

#include "mail.h"
#include "nntpdriver.h"
#include "nntpdriver_cached.h"
#include "newsnntp.h"
#include "maildriver_types.h"
#include "generic_cache.h"
#include "imfcache.h"
#include "mailmessage.h"
#include "mail_cache_db.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

int nntpdriver_nntp_error_to_mail_error(int error)
{
  switch (error) {
  case NEWSNNTP_NO_ERROR:
    return MAIL_NO_ERROR;

  case NEWSNNTP_ERROR_STREAM:
    return MAIL_ERROR_STREAM;

  case NEWSNNTP_ERROR_UNEXPECTED:
    return MAIL_ERROR_PROGRAM_ERROR;

  case NEWSNNTP_ERROR_NO_NEWSGROUP_SELECTED:
    return MAIL_ERROR_FOLDER_NOT_FOUND;

  case NEWSNNTP_ERROR_NO_ARTICLE_SELECTED:
  case NEWSNNTP_ERROR_INVALID_ARTICLE_NUMBER:
  case NEWSNNTP_ERROR_ARTICLE_NOT_FOUND:
    return MAIL_ERROR_MSG_NOT_FOUND;

  case NEWSNNTP_ERROR_UNEXPECTED_RESPONSE:
  case NEWSNNTP_ERROR_INVALID_RESPONSE:
    return MAIL_ERROR_PARSE;

  case NEWSNNTP_ERROR_NO_SUCH_NEWS_GROUP:
    return MAIL_ERROR_FOLDER_NOT_FOUND;

  case NEWSNNTP_ERROR_POSTING_NOT_ALLOWED:
    return MAIL_ERROR_READONLY;

  case NEWSNNTP_ERROR_POSTING_FAILED:
    return MAIL_ERROR_APPEND;

  case NEWSNNTP_ERROR_PROGRAM_ERROR:
    return MAIL_ERROR_PROGRAM_ERROR;

  case NEWSNNTP_ERROR_NO_PERMISSION:
    return MAIL_ERROR_NO_PERMISSION;

  case NEWSNNTP_ERROR_COMMAND_NOT_UNDERSTOOD:
  case NEWSNNTP_ERROR_COMMAND_NOT_SUPPORTED:
    return MAIL_ERROR_COMMAND_NOT_SUPPORTED;

  case NEWSNNTP_ERROR_CONNECTION_REFUSED:
    return MAIL_ERROR_CONNECT;

  case NEWSNNTP_ERROR_MEMORY:
    return MAIL_ERROR_MEMORY;

  case NEWSNNTP_ERROR_AUTHENTICATION_REJECTED:
    return MAIL_ERROR_LOGIN;

  case NEWSNNTP_ERROR_BAD_STATE:
    return MAIL_ERROR_BAD_STATE;

  case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME:
  case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
  default:
    return MAIL_ERROR_INVAL;
  }
}

static inline struct nntp_session_state_data *
session_get_data(mailsession * session)
{
  return session->sess_data;
}

static inline newsnntp * session_get_nntp_session(mailsession * session)
{
  return session_get_data(session)->nntp_session;
}

static inline struct nntp_cached_session_state_data *
cached_session_get_data(mailsession * session)
{
  return session->sess_data;
}

static inline mailsession * cached_session_get_ancestor(mailsession * session)
{
  return cached_session_get_data(session)->nntp_ancestor;
}

static inline struct nntp_session_state_data *
cached_session_get_ancestor_data(mailsession * session)
{
  return session_get_data(cached_session_get_ancestor(session));
}

static inline newsnntp * cached_session_get_nntp_session(mailsession * session)
{
  return session_get_nntp_session(cached_session_get_ancestor(session));
}


int nntpdriver_authenticate_password(mailsession * session)
{
  struct nntp_session_state_data * data;
  int r;

  data = session_get_data(session);

  if (data->nntp_password == NULL)
    return MAIL_ERROR_LOGIN;

  r = newsnntp_authinfo_password(session_get_nntp_session(session),
      data->nntp_password);

  return nntpdriver_nntp_error_to_mail_error(r);
}

int nntpdriver_mode_reader(mailsession * session)
{
  int done;
  int r;

  done = FALSE;

  do {
    r = newsnntp_mode_reader(session_get_nntp_session(session));
    
    switch (r) {
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME:
      r = nntpdriver_authenticate_user(session);
      if (r != MAIL_NO_ERROR)
        return r;
      break;
      
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
      r = nntpdriver_authenticate_password(session);
      if (r != MAIL_NO_ERROR)
        return r;
      break;
      
    case NEWSNNTP_NO_ERROR:
      done = TRUE;
      break;
      
    default:
      done = TRUE;
      break;
    }
  }
  while (!done);

  return MAIL_NO_ERROR;
}

int nntpdriver_authenticate_user(mailsession * session)
{
  struct nntp_session_state_data * data;
  int r;

  data = session_get_data(session);

  if (data->nntp_userid == NULL)
    return MAIL_ERROR_LOGIN;

  r = newsnntp_authinfo_username(session_get_nntp_session(session),
      data->nntp_userid);

  switch (r) {
  case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
    return nntpdriver_authenticate_password(session);

  default:
    return nntpdriver_nntp_error_to_mail_error(r);
  }
}

int nntpdriver_article(mailsession * session, uint32_t index,
		       char ** result,
		       size_t * result_len)
{
  char * msg_content;
  size_t msg_length;
  int r;
  int done;

  done = FALSE;
  do {
    r = newsnntp_article(session_get_nntp_session(session),
        index, &msg_content, &msg_length);
    
    switch (r) {
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME:
      r = nntpdriver_authenticate_user(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;
      
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
      r = nntpdriver_authenticate_password(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;

    case NEWSNNTP_NO_ERROR:
      done = TRUE;
      break;

    default:
      return nntpdriver_nntp_error_to_mail_error(r);
    }
  }
  while (!done);

  * result = msg_content;
  * result_len = msg_length;

  return MAIL_NO_ERROR;
}

int nntpdriver_head(mailsession * session, uint32_t index,
		    char ** result,
		    size_t * result_len)
{
  char * headers;
  size_t headers_length;
  int r;
  int done;

  done = FALSE;
  do {
    r = newsnntp_head(session_get_nntp_session(session),
        index, &headers, &headers_length);
    
    switch (r) {
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME:
      r = nntpdriver_authenticate_user(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;
      
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
      r = nntpdriver_authenticate_password(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;

    case NEWSNNTP_NO_ERROR:
      done = TRUE;
      break;
      
    default:
      return nntpdriver_nntp_error_to_mail_error(r);
    }
  }
  while (!done);

  * result = headers;
  * result_len = headers_length;

  return MAIL_NO_ERROR;
}

int nntpdriver_size(mailsession * session, uint32_t index,
		    size_t * result)
{
  newsnntp * nntp;
  struct newsnntp_xover_resp_item * item;
  int r;
  int done;

  nntp = session_get_nntp_session(session);

  done = FALSE;
  do {
    r = newsnntp_xover_single(nntp, index, &item);
    switch (r) {
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME:
      r = nntpdriver_authenticate_user(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;
      
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
      r = nntpdriver_authenticate_password(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;

    case NEWSNNTP_NO_ERROR:
      done = TRUE;
      break;

    default:
      return nntpdriver_nntp_error_to_mail_error(r);
    }
  }
  while (!done);

  * result = item->ovr_size;

  xover_resp_item_free(item);

  return MAIL_NO_ERROR;
}

int
nntpdriver_get_cached_flags(struct mail_cache_db * cache_db,
    MMAPString * mmapstr,
    uint32_t num, 
    struct mail_flags ** result)
{
  int r;
  char keyname[PATH_MAX];
  struct mail_flags * flags;
  int res;

  snprintf(keyname, PATH_MAX, "%u-flags", num);

  r = generic_cache_flags_read(cache_db, mmapstr, keyname, &flags);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }
  
  * result = flags;

  return MAIL_NO_ERROR;

 err:
  return res;
}

int
nntpdriver_write_cached_flags(struct mail_cache_db * cache_db,
    MMAPString * mmapstr,
    uint32_t num,
    struct mail_flags * flags)
{
  int r;
  char keyname[PATH_MAX];
  int res;

  snprintf(keyname, PATH_MAX, "%u-flags", num);

  r = generic_cache_flags_write(cache_db, mmapstr, keyname, flags);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }

  return MAIL_NO_ERROR;

 err:
  return res;
}



int nntpdriver_select_folder(mailsession * session, char * mb)
{
  int r;
  struct newsnntp_group_info * info;
  newsnntp * nntp_session;
  struct nntp_session_state_data * data;
  char * new_name;
  int done;

  data = session_get_data(session);

  if (!data->nntp_mode_reader) {
    r = nntpdriver_mode_reader(session);
    if (r != MAIL_NO_ERROR)
      return r;
    
    data->nntp_mode_reader = TRUE;
  }

  if (data->nntp_group_name != NULL)
    if (strcmp(data->nntp_group_name, mb) == 0)
      return MAIL_NO_ERROR;

  nntp_session = session_get_nntp_session(session);

  done = FALSE;
  do {
    r = newsnntp_group(nntp_session, mb, &info);

    switch (r) {
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_USERNAME:
      r = nntpdriver_authenticate_user(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;
      
    case NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD:
      r = nntpdriver_authenticate_password(session);
      if (r != MAIL_NO_ERROR)
	return r;
      break;
      
    case NEWSNNTP_NO_ERROR:
      done = TRUE;
      break;
      
    default:
      return nntpdriver_nntp_error_to_mail_error(r);
    }

  }
  while (!done);

  new_name = strdup(mb);
  if (new_name == NULL)
    return MAIL_ERROR_MEMORY;

  if (data->nntp_group_name != NULL)
    free(data->nntp_group_name);
  data->nntp_group_name = new_name;
  if (data->nntp_group_info != NULL)
    newsnntp_group_free(data->nntp_group_info);
  data->nntp_group_info = info;

  return MAIL_NO_ERROR;
}


int nntp_get_messages_list(mailsession * nntp_session,
			   mailsession * session,
			   mailmessage_driver * driver,
			   struct mailmessage_list ** result)
{
  carray * tab;
  struct mailmessage_list * env_list;
  uint32_t i;
  int res;
  int r;
  struct nntp_session_state_data * data;
  struct newsnntp_group_info * group_info;
  uint32_t max;
  unsigned int cur;

  data = session_get_data(nntp_session);

  if (data->nntp_group_name == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  r = nntpdriver_select_folder(nntp_session, data->nntp_group_name);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }
  
  group_info = data->nntp_group_info;
  
  if (group_info == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  max = group_info->grp_first;
  if (data->nntp_max_articles != 0) {
    if (group_info->grp_last - data->nntp_max_articles + 1 > max)
      max = group_info->grp_last - data->nntp_max_articles + 1;
  }

  tab = carray_new(128);
  if (tab == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  for(i = max ; i <= group_info->grp_last ; i++) {
    mailmessage * msg;

    msg = mailmessage_new();
    if (msg == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto free_list;
    }

    r = mailmessage_init(msg, session, driver, i, 0);
    if (r != MAIL_NO_ERROR) {
      mailmessage_free(msg);
      res = r;
      goto free_list;
    }

    r = carray_add(tab, msg, NULL);
    if (r < 0) {
      mailmessage_free(msg);
      res = MAIL_ERROR_MEMORY;
      goto free_list;
    }
  }

  env_list = mailmessage_list_new(tab);
  if (env_list == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_list;
  }

  * result = env_list;

  return MAIL_NO_ERROR;

 free_list:
  for(cur = 0 ; cur < carray_count(tab) ; cur ++)
    mailmessage_free(carray_get(tab, cur));
  carray_free(tab);
 err:
  return res;
}