mbox.c
18.2 KB
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
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2004, 2005, 2007 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#ifdef ENABLE_NNTP
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <md5.h>
#include <mailutils/body.h>
#include <mailutils/debug.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/header.h>
#include <mailutils/message.h>
#include <mailutils/observer.h>
#include <mailutils/property.h>
#include <mailutils/stream.h>
#include <mailutils/iterator.h>
#include <mailutils/url.h>
#include <mailutils/nntp.h>
#include <folder0.h>
#include <mailbox0.h>
#include "nntp0.h"
/* Functions/Methods that implements the mu_mailbox_t API. */
static void nntp_mailbox_destroy (mu_mailbox_t);
static int nntp_mailbox_open (mu_mailbox_t, int);
static int nntp_mailbox_close (mu_mailbox_t);
static int nntp_mailbox_get_message (mu_mailbox_t, size_t, mu_message_t *);
static int nntp_mailbox_messages_count (mu_mailbox_t, size_t *);
static int nntp_mailbox_scan (mu_mailbox_t, size_t, size_t *);
/* FIXME
static int nntp_mailbox_get_size (mu_mailbox_t, mu_off_t *); */
static int nntp_message_get_transport2 (mu_stream_t, mu_transport_t *, mu_transport_t *);
static int nntp_message_read (mu_stream_t, char *, size_t, mu_off_t, size_t *);
static int nntp_message_size (mu_message_t, size_t *);
/* FIXME
static int nntp_message_line (mu_message_t, size_t *); */
static int nntp_message_uidl (mu_message_t, char *, size_t, size_t *);
static int nntp_message_uid (mu_message_t, size_t *);
/* FIXME
static int nntp_header_get_transport2 (mu_header_t, char *,
size_t, mu_off_t, size_t *); */
static int nntp_header_fill (mu_header_t, char *, size_t, mu_off_t, size_t *);
static int nntp_body_get_transport2 (mu_stream_t, mu_transport_t *, mu_transport_t *);
static int nntp_body_read (mu_stream_t, char *, size_t, mu_off_t, size_t *);
static int nntp_body_size (mu_body_t, size_t *);
static int nntp_body_lines (mu_body_t, size_t *);
static int nntp_get_transport2 (msg_nntp_t, mu_transport_t *, mu_transport_t *);
int
_nntp_mailbox_init (mu_mailbox_t mbox)
{
m_nntp_t m_nntp;
int status = 0;
/* Allocate specifics for nntp data. */
m_nntp = mbox->data = calloc (1, sizeof (*m_nntp));
if (mbox->data == NULL)
return ENOMEM;
/* Get the back pointer of the concrete folder. */
if (mbox->folder)
m_nntp->f_nntp = mbox->folder->data;
m_nntp->mailbox = mbox; /* Back pointer. */
/* Retrieve the name of the newsgroup from the URL. */
status = mu_url_aget_path (mbox->url, &m_nntp->name);
if (status == MU_ERR_NOENT)
{
m_nntp->name = strdup ("INBOX");
if (!m_nntp->name)
return ENOMEM;
}
else if (status)
return status;
else
{
char *p;
p = strchr (m_nntp->name, '/');
if (p)
*p = '\0';
}
/* Initialize the structure. */
mbox->_destroy = nntp_mailbox_destroy;
mbox->_open = nntp_mailbox_open;
mbox->_close = nntp_mailbox_close;
/* Messages. */
mbox->_get_message = nntp_mailbox_get_message;
mbox->_messages_count = nntp_mailbox_messages_count;
mbox->_messages_recent = nntp_mailbox_messages_count;
mbox->_message_unseen = nntp_mailbox_messages_count;
/*mbox->_expunge = nntp_mailbox_expunge;*/
mbox->_scan = nntp_mailbox_scan;
/*mbox->_is_updated = nntp_mailbox_is_updated; */
/*mbox->_get_size = nntp_mailbox_get_size; */
/* Set our properties. */
{
mu_property_t property = NULL;
mu_mailbox_get_property (mbox, &property);
mu_property_set_value (property, "TYPE", "NNTP", 1);
}
return status;
}
/* Cleaning up all the ressources associate with a newsgroup/mailbox. */
static void
nntp_mailbox_destroy (mu_mailbox_t mbox)
{
if (mbox->data)
{
m_nntp_t m_nntp = mbox->data;
f_nntp_t f_nntp = m_nntp->f_nntp;
size_t i;
/* Deselect. */
if (m_nntp == f_nntp->selected)
f_nntp->selected = NULL;
mu_monitor_wrlock (mbox->monitor);
if (m_nntp->name)
free (m_nntp->name);
/* Destroy the nntp messages and ressources associated to them. */
for (i = 0; i < m_nntp->messages_count; i++)
{
if (m_nntp->messages[i])
{
mu_message_destroy (&(m_nntp->messages[i]->message), m_nntp->messages[i]);
if (m_nntp->messages[i]->mid)
free (m_nntp->messages[i]->mid);
free (m_nntp->messages[i]);
m_nntp->messages[i] = NULL;
}
}
if (m_nntp->messages)
free (m_nntp->messages);
free (m_nntp);
mbox->data = NULL;
mu_monitor_unlock (mbox->monitor);
}
}
/* If the connection was not up it is open by the folder since the stream
socket is actually created by the folder. It is not necessary
to set select the mailbox/newsgoup right away, there are maybe on going operations.
But on any operation by a particular mailbox, it will be selected first. */
static int
nntp_mailbox_open (mu_mailbox_t mbox, int flags)
{
int status = 0;
m_nntp_t m_nntp = mbox->data;
f_nntp_t f_nntp = m_nntp->f_nntp;
mu_iterator_t iterator;
/* m_nntp must have been created during mailbox initialization. */
/* assert (mbox->data);
assert (m_nntp->name); */
mbox->flags = flags;
/* make sure the connection is up. */
if ((status = mu_folder_open (f_nntp->folder, flags)))
return status;
mu_nntp_set_debug (f_nntp->nntp, mbox->debug);
/* We might not have to SELECT the newsgroup, but we need to know it
exists. */
status = mu_nntp_list_active (f_nntp->nntp, m_nntp->name, &iterator);
if (status == 0)
{
for (mu_iterator_first (iterator);
!mu_iterator_is_done (iterator); mu_iterator_next (iterator))
{
char *buffer = NULL;
mu_iterator_current (iterator, (void **) &buffer);
mu_nntp_parse_list_active (buffer, NULL, &m_nntp->high, &m_nntp->low, &m_nntp->status);
}
mu_iterator_destroy (&iterator);
}
return status;
}
/* We can not close the folder in term of shuting down the connection but if
we were the selected mailbox/newsgroup we deselect ourself. */
static int
nntp_mailbox_close (mu_mailbox_t mailbox)
{
m_nntp_t m_nntp = mailbox->data;
f_nntp_t f_nntp = m_nntp->f_nntp;
int i;
mu_monitor_wrlock (mailbox->monitor);
/* Destroy the nntp posts and ressources associated to them. */
for (i = 0; i < m_nntp->messages_count; i++)
{
if (m_nntp->messages[i])
{
msg_nntp_t msg_nntp = m_nntp->messages[i];
if (msg_nntp->message)
mu_message_destroy (&(msg_nntp->message), msg_nntp);
}
free (m_nntp->messages[i]);
}
if (m_nntp->messages)
free (m_nntp->messages);
m_nntp->messages = NULL;
m_nntp->messages_count = 0;
m_nntp->number = 0;
m_nntp->low = 0;
m_nntp->high = 0;
mu_monitor_unlock (mailbox->monitor);
/* Deselect. */
if (m_nntp != f_nntp->selected)
f_nntp->selected = NULL;
/* Decrement the ref count. */
return mu_folder_close (mailbox->folder);
}
static int
nntp_mailbox_get_message (mu_mailbox_t mbox, size_t msgno, mu_message_t *pmsg)
{
m_nntp_t m_nntp = mbox->data;
msg_nntp_t msg_nntp;
mu_message_t msg = NULL;
int status;
size_t i;
/* Sanity. */
if (pmsg == NULL)
return MU_ERR_OUT_PTR_NULL;
msgno--;
mu_monitor_rdlock (mbox->monitor);
/* See if we have already this message. */
for (i = 0; i < m_nntp->messages_count; i++)
{
if (m_nntp->messages[i])
{
if (m_nntp->messages[i]->msgno == msgno + m_nntp->low)
{
*pmsg = m_nntp->messages[i]->message;
mu_monitor_unlock (mbox->monitor);
return 0;
}
}
}
mu_monitor_unlock (mbox->monitor);
msg_nntp = calloc (1, sizeof (*msg_nntp));
if (msg_nntp == NULL)
return ENOMEM;
/* Back pointer. */
msg_nntp->m_nntp = m_nntp;
msg_nntp->msgno = msgno + m_nntp->low;
/* Create the message. */
{
mu_stream_t stream = NULL;
if ((status = mu_message_create (&msg, msg_nntp)) != 0
|| (status = mu_stream_create (&stream, mbox->flags, msg)) != 0)
{
mu_stream_destroy (&stream, msg);
mu_message_destroy (&msg, msg_nntp);
free (msg_nntp);
return status;
}
/* Help for the readline()s */
mu_stream_set_read (stream, nntp_message_read, msg);
mu_stream_set_get_transport2 (stream, nntp_message_get_transport2, msg);
mu_message_set_stream (msg, stream, msg_nntp);
mu_message_set_size (msg, nntp_message_size, msg_nntp);
}
/* Create the header. */
{
mu_header_t header = NULL;
if ((status = mu_header_create (&header, NULL, 0, msg)) != 0)
{
mu_message_destroy (&msg, msg_nntp);
free (msg_nntp);
return status;
}
mu_header_set_fill (header, nntp_header_fill, msg);
mu_message_set_header (msg, header, msg_nntp);
}
/* Create the body and its stream. */
{
mu_body_t body = NULL;
mu_stream_t stream = NULL;
if ((status = mu_body_create (&body, msg)) != 0
|| (status = mu_stream_create (&stream, mbox->flags, body)) != 0)
{
mu_body_destroy (&body, msg);
mu_stream_destroy (&stream, body);
mu_message_destroy (&msg, msg_nntp);
free (msg_nntp);
return status;
}
/* Helps for the readline()s */
mu_stream_set_read (stream, nntp_body_read, body);
mu_stream_set_get_transport2 (stream, nntp_body_get_transport2, body);
mu_body_set_size (body, nntp_body_size, msg);
mu_body_set_lines (body, nntp_body_lines, msg);
mu_body_set_stream (body, stream, msg);
mu_message_set_body (msg, body, msg_nntp);
}
/* Set the UID on the message. */
mu_message_set_uid (msg, nntp_message_uid, msg_nntp);
/* Add it to the list. */
mu_monitor_wrlock (mbox->monitor);
{
msg_nntp_t *m ;
m = realloc (m_nntp->messages, (m_nntp->messages_count + 1)*sizeof (*m));
if (m == NULL)
{
mu_message_destroy (&msg, msg_nntp);
free (msg_nntp);
mu_monitor_unlock (mbox->monitor);
return ENOMEM;
}
m_nntp->messages = m;
m_nntp->messages[m_nntp->messages_count] = msg_nntp;
m_nntp->messages_count++;
}
mu_monitor_unlock (mbox->monitor);
/* Save The message pointer. */
mu_message_set_mailbox (msg, mbox, msg_nntp);
*pmsg = msg_nntp->message = msg;
return 0;
}
/* There is no explicit call to get the message count. The count is send on
a "GROUP" command. The function is also use as a way to select newsgoupr by other functions. */
static int
nntp_mailbox_messages_count (mu_mailbox_t mbox, size_t *pcount)
{
m_nntp_t m_nntp = mbox->data;
f_nntp_t f_nntp = m_nntp->f_nntp;
int status = 0;
status = mu_folder_open (mbox->folder, mbox->flags);
if (status != 0)
return status;
/* Are we already selected ? */
if (m_nntp == (f_nntp->selected))
{
if (pcount)
*pcount = m_nntp->number;
return 0;
}
/* Put the mailbox as selected. */
f_nntp->selected = m_nntp;
status = mu_nntp_group (f_nntp->nntp, m_nntp->name, &m_nntp->number, &m_nntp->low, &m_nntp->high, NULL);
if (pcount)
*pcount = m_nntp->number;
return status;
}
/* Update and scanning. FIXME: Is not used */
static int
nntp_is_updated (mu_mailbox_t mbox)
{
return 1;
}
/* We just simulate by sending a notification for the total msgno. */
/* FIXME is message is set deleted should we sent a notif ? */
static int
nntp_mailbox_scan (mu_mailbox_t mbox, size_t msgno, size_t *pcount)
{
int status;
size_t i;
size_t count = 0;
/* Select first. */
status = nntp_mailbox_messages_count (mbox, &count);
if (pcount)
*pcount = count;
if (status != 0)
return status;
if (mbox->observable == NULL)
return 0;
for (i = msgno; i <= count; i++)
{
size_t tmp = i;
if (mu_observable_notify (mbox->observable, MU_EVT_MESSAGE_ADD,
&tmp) != 0)
break;
if ((i +1) % 10 == 0)
mu_observable_notify (mbox->observable, MU_EVT_MAILBOX_PROGRESS, NULL);
}
return 0;
}
static int
nntp_message_size (mu_message_t msg, size_t *psize)
{
if (psize)
*psize = 0;
return 0;
}
static int
nntp_body_size (mu_body_t body, size_t *psize)
{
if (psize)
*psize = 0;
return 0;
}
/* Not know until the whole message get downloaded. */
static int
nntp_body_lines (mu_body_t body, size_t *plines)
{
if (plines)
*plines = 0;
return 0;
}
/* Stub to call the fd from body object. */
static int
nntp_body_get_transport2 (mu_stream_t stream, mu_transport_t *pin, mu_transport_t *pout)
{
mu_body_t body = mu_stream_get_owner (stream);
mu_message_t msg = mu_body_get_owner (body);
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
return nntp_get_transport2 (msg_nntp, pin, pout);
}
/* Stub to call the fd from message object. */
static int
nntp_message_get_transport2 (mu_stream_t stream, mu_transport_t *pin, mu_transport_t *pout)
{
mu_message_t msg = mu_stream_get_owner (stream);
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
return nntp_get_transport2 (msg_nntp, pin, pout);
}
static int
nntp_get_transport2 (msg_nntp_t msg_nntp, mu_transport_t *pin, mu_transport_t *pout)
{
int status = EINVAL;
if (msg_nntp && msg_nntp->m_nntp
&& msg_nntp->m_nntp->f_nntp && msg_nntp->m_nntp->f_nntp->folder)
{
mu_stream_t carrier;
status = mu_nntp_get_carrier (msg_nntp->m_nntp->f_nntp->nntp, &carrier);
if (status == 0)
return mu_stream_get_transport2 (carrier, pin, pout);
}
return status;
}
static int
nntp_message_uid (mu_message_t msg, size_t *puid)
{
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
m_nntp_t m_nntp = msg_nntp->m_nntp;
int status;
if (puid)
return 0;
/* Select first. */
status = nntp_mailbox_messages_count (m_nntp->mailbox, NULL);
if (status != 0)
return status;
if (puid)
*puid = msg_nntp->msgno;
return 0;
}
static int
nntp_message_uidl (mu_message_t msg, char *buffer, size_t buflen,
size_t *pnwriten)
{
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
m_nntp_t m_nntp = msg_nntp->m_nntp;
int status = 0;
/* Select first. */
status = nntp_mailbox_messages_count (m_nntp->mailbox, NULL);
if (status != 0)
return status;
if (msg_nntp->mid)
{
size_t len = strlen (msg_nntp->mid);
if (buffer)
{
buflen--; /* Leave space for the null. */
buflen = (len > buflen) ? buflen : len;
memcpy (buffer, msg_nntp->mid, buflen);
buffer[buflen] = '\0';
}
else
buflen = len;
}
else
buflen = 0;
if (pnwriten)
*pnwriten = buflen;
return status;
}
/* Message read overload */
static int
nntp_message_read (mu_stream_t stream, char *buffer, size_t buflen, mu_off_t offset, size_t *plen)
{
mu_message_t msg = mu_stream_get_owner (stream);
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
m_nntp_t m_nntp = msg_nntp->m_nntp;
f_nntp_t f_nntp = m_nntp->f_nntp;
int status;
size_t len = 0;
/* Start over. */
if (plen == NULL)
plen = &len;
/* Select first. */
status = nntp_mailbox_messages_count (m_nntp->mailbox, NULL);
if (status != 0)
return status;
if (msg_nntp->mstream == NULL)
{
status = mu_nntp_article (f_nntp->nntp, msg_nntp->msgno, NULL, &msg_nntp->mid, &msg_nntp->mstream);
if (status != 0)
return status;
}
status = mu_stream_read (msg_nntp->mstream, buffer, buflen, offset, plen);
if (status == 0)
{
/* Destroy the stream. */
if (*plen == 0)
{
mu_stream_destroy (&msg_nntp->mstream, NULL);
}
}
return status;
}
/* Message read overload */
static int
nntp_body_read (mu_stream_t stream, char *buffer, size_t buflen, mu_off_t offset, size_t *plen)
{
mu_body_t body = mu_stream_get_owner (stream);
mu_message_t msg = mu_body_get_owner (body);
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
m_nntp_t m_nntp = msg_nntp->m_nntp;
f_nntp_t f_nntp = m_nntp->f_nntp;
int status;
size_t len = 0;
/* Start over. */
if (plen == NULL)
plen = &len;
/* Select first. */
status = nntp_mailbox_messages_count (m_nntp->mailbox, NULL);
if (status != 0)
return status;
if (msg_nntp->bstream == NULL)
{
status = mu_nntp_body (f_nntp->nntp, msg_nntp->msgno, NULL, &msg_nntp->mid, &msg_nntp->bstream);
if (status != 0)
return status;
}
status = mu_stream_read (msg_nntp->bstream, buffer, buflen, offset, plen);
if (status == 0)
{
/* Destroy the stream. */
if (*plen == 0)
{
mu_stream_destroy (&msg_nntp->bstream, NULL);
}
}
return status;
}
/* Header read overload */
static int
nntp_header_fill (mu_header_t header, char *buffer, size_t buflen, mu_off_t offset, size_t *plen)
{
mu_message_t msg = mu_header_get_owner (header);
msg_nntp_t msg_nntp = mu_message_get_owner (msg);
m_nntp_t m_nntp = msg_nntp->m_nntp;
f_nntp_t f_nntp = m_nntp->f_nntp;
int status;
size_t len = 0;
/* Start over. */
if (plen == NULL)
plen = &len;
/* Select first. */
status = nntp_mailbox_messages_count (m_nntp->mailbox, NULL);
if (status != 0)
return status;
if (msg_nntp->hstream == NULL)
{
status = mu_nntp_head (f_nntp->nntp, msg_nntp->msgno, NULL, &msg_nntp->mid, &msg_nntp->hstream);
if (status != 0)
return status;
}
status = mu_stream_read (msg_nntp->hstream, buffer, buflen, offset, plen);
if (status == 0)
{
/* Destroy the stream. */
if (*plen == 0)
{
mu_stream_destroy (&msg_nntp->hstream, NULL);
}
}
return status;
}
#endif