Commit 8f6a087a 8f6a087af2063a2c9e368301799968d995f87d08 by Alain Magloire

Implement the mailbox framework for NNTP.

1 parent ebf3a875
...@@ -56,4 +56,8 @@ libmu_nntp_la_SOURCES = \ ...@@ -56,4 +56,8 @@ libmu_nntp_la_SOURCES = \
56 nntp_sendline.c \ 56 nntp_sendline.c \
57 nntp_stat.c \ 57 nntp_stat.c \
58 nntp_stream.c \ 58 nntp_stream.c \
59 nntp_timeout.c 59 nntp_timeout.c \
60 \
61 url.c \
62 folder.c \
63 mbox.c
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #ifdef ENABLE_NNTP
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #ifdef HAVE_STRINGS_H
29 # include <strings.h>
30 #endif
31
32 #include <mailutils/nntp.h>
33 #include <mailutils/errno.h>
34 #include <mailutils/mailbox.h>
35 #include <mailutils/registrar.h>
36
37 #include <folder0.h>
38 #include "nntp0.h"
39
40 /* We export url parsing and the initialisation of
41 the mailbox, via the register entry/record. */
42
43 static struct _record _nntp_record =
44 {
45 MU_NNTP_URL_SCHEME,
46 _nntp_url_init, /* Url init. */
47 _nntp_mailbox_init, /* Mailbox init. */
48 NULL, /* Mailer init. */
49 _nntp_folder_init, /* Folder init. */
50 NULL, /* No need for an back pointer. */
51 NULL, /* _is_scheme method. */
52 NULL, /* _get_url method. */
53 NULL, /* _get_mailbox method. */
54 NULL, /* _get_mailer method. */
55 NULL /* _get_folder method. */
56 };
57 record_t nntp_record = &_nntp_record;
58
59 static int nntp_folder_open __P ((folder_t, int));
60 static int nntp_folder_close __P ((folder_t));
61 static void nntp_folder_destroy __P ((folder_t folder));
62 static int nntp_folder_list __P ((folder_t folder, const char *ref, const char *name, struct folder_list *pflist));
63
64 int
65 _nntp_folder_init (folder_t folder)
66 {
67 int status;
68 f_nntp_t f_nntp;
69
70 f_nntp = folder->data = calloc (1, sizeof (*f_nntp));
71 if (f_nntp == NULL)
72 return ENOMEM;
73
74 f_nntp->folder = folder;
75
76 folder->_destroy = nntp_folder_destroy;
77
78 folder->_open = nntp_folder_open;
79 folder->_close = nntp_folder_close;
80
81 folder->_list = nntp_folder_list;
82 /* Not supported.
83 folder->_lsub = folder_nntp_lsub;
84 folder->_subscribe = folder_nntp_subscribe;
85 folder->_unsubscribe = folder_nntp_unsubscribe;
86 folder->_delete = folder_nntp_delete;
87 folder->_rename = folder_nntp_rename;
88 */
89
90 return 0;
91 }
92
93 static int
94 nntp_folder_open (folder_t folder, int flags)
95 {
96 f_nntp_t f_nntp = folder->data;
97 stream_t carrier = NULL;
98 char *host;
99 long port = MU_NNTP_DEFAULT_PORT; /* default nntp port. */
100 int status = 0;
101 size_t len = 0;
102
103 /* If we are already open for business, noop. */
104 monitor_wrlock (folder->monitor);
105 if (f_nntp->isopen)
106 {
107 monitor_unlock (folder->monitor);
108 return 0;
109 }
110 monitor_unlock (folder->monitor);
111
112 /* Fetch the server name and the port in the url_t. */
113 status = url_get_host (folder->url, NULL, 0, &len);
114 if (status != 0)
115 return status;
116 host = alloca (len + 1);
117 url_get_host (folder->url, host, len + 1, NULL);
118 url_get_port (folder->url, &port);
119
120 folder->flags = flags;
121
122 /* Create the networking stack. */
123 status = tcp_stream_create (&carrier, host, port, folder->flags);
124 if (status != 0)
125 return status;
126 /* Ask for the stream internal buffering mechanism scheme. */
127 stream_setbufsiz (carrier, BUFSIZ);
128 FOLDER_DEBUG2 (folder, MU_DEBUG_PROT, "folder_nntp_open (%s:%d)\n", host, port);
129
130 status = mu_nntp_create (&f_nntp->nntp);
131 if (status == 0)
132 {
133 status = mu_nntp_set_carrier (f_nntp->nntp, carrier);
134 if (status == 0)
135 {
136 status = mu_nntp_connect (f_nntp->nntp);
137 if (status == 0)
138 {
139 monitor_wrlock (folder->monitor);
140 f_nntp->isopen++;
141 monitor_unlock (folder->monitor);
142 }
143 }
144 }
145
146 return status;
147 }
148
149 static int
150 nntp_folder_close (folder_t folder)
151 {
152 f_nntp_t f_nntp = folder->data;
153 int status = 0;
154
155 monitor_wrlock (folder->monitor);
156 f_nntp->isopen--;
157 if (f_nntp->isopen)
158 {
159 monitor_unlock (folder->monitor);
160 return 0;
161 }
162 monitor_unlock (folder->monitor);
163 status = mu_nntp_quit (f_nntp->nntp);
164 f_nntp->selected = NULL;
165 return status;
166
167 }
168
169 /* Destroy the folder resources. */
170 static void
171 nntp_folder_destroy (folder_t folder)
172 {
173 if (folder->data)
174 {
175 f_nntp_t f_nntp = folder->data;
176 if (f_nntp->nntp)
177 mu_nntp_destroy (&f_nntp->nntp);
178 free (f_nntp);
179 folder->data = NULL;
180 }
181 }
182
183
184 static int
185 nntp_folder_list (folder_t folder, const char *ref, const char *name, struct folder_list *pflist)
186 {
187 return ENOTSUP;
188 }
189 #else
190 #include <stdio.h>
191 #include <registrar0.h>
192 record_t nntp_record = NULL;
193 #endif
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 struct _nntp_folder;
23 struct _nntp_mailbox;
24 struct _nntp_message;
25 typedef struct _nntp_folder *f_nntp_t;
26 typedef struct _nntp_mailbox *m_nntp_t;
27 typedef struct _nntp_message *msg_nntp_t;
28
29 extern int _nntp_url_init __P ((url_t));
30 extern int _nntp_mailbox_init __P ((mailbox_t));
31 extern int _nntp_folder_init __P ((folder_t));
32
33 struct _nntp_folder
34 {
35 /* Refcount. */
36 int isopen;
37
38 /* Back pointer. */
39 folder_t folder;
40
41 /* Selected newsgroup. */
42 m_nntp_t selected;
43
44 /* NNTP object. */
45 mu_nntp_t nntp;
46 };
47
48 struct _nntp_mailbox
49 {
50 char status;
51
52 char *name;
53
54 /* Pointer back to the mailbox/newsgroup. */
55 mailbox_t mailbox;
56
57 /* Our nntp folder. */
58 f_nntp_t f_nntp;
59
60 /* Read Messages on the newsgroup. */
61 msg_nntp_t *messages;
62 size_t messages_count;
63
64 /* Estimated number of articles in the group. */
65 unsigned long number;
66 /* High water mark from "GROUP" command */
67 unsigned long high;
68 /* Low water mark from "GROUP" command */
69 unsigned long low;
70 };
71
72 struct _nntp_message
73 {
74 /* Back pointer. */
75 message_t message;
76
77 /* Our nntp folder. */
78 m_nntp_t m_nntp;
79
80 /* Message id. */
81 char *mid;
82
83 /* mesgno of the post. */
84 unsigned long msgno;
85
86 /* Stream for message. */
87 stream_t mstream;
88 /* Stream for body. */
89 stream_t bstream;
90 /* Stream for header. */
91 stream_t hstream;
92 };
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2004 Free Software Foundation, Inc.
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #ifdef ENABLE_NNTP
23
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #ifdef HAVE_STRINGS_H
29 # include <strings.h>
30 #endif
31
32 #include <mailutils/nntp.h>
33
34 #include <url0.h>
35
36 static void url_nntp_destroy (url_t url);
37
38 static void
39 url_nntp_destroy (url_t url ARG_UNUSED)
40 {
41 }
42
43 /*
44 POP URL:
45 nntp://<host>:<port>/<newsgroup-name>/<article-number>
46 */
47
48 int
49 _nntp_url_init (url_t url)
50 {
51 int status = 0;
52
53 url->_destroy = url_nntp_destroy;
54
55 status = url_parse(url);
56
57 if(status)
58 return status;
59
60 /* is it nntp? */
61 if (strcmp (MU_NNTP_URL_SCHEME, url->scheme) != 0)
62 return EINVAL;
63
64 /* not valid in a nntp url */
65 if (!url->host || !url->path)
66 return EINVAL;
67
68 if (url->port == 0)
69 url->port = MU_NNTP_DEFAULT_PORT;
70
71 return status;
72 }
73
74 #endif