* AUTHORS
[claws.git] / src / common / nntp.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "intl.h"
29 #include "nntp.h"
30 #include "socket.h"
31 #include "utils.h"
32 #include "log.h"
33 #if USE_OPENSSL
34 #  include "ssl.h"
35 #endif
36
37 static gint verbose = 1;
38
39 static void nntp_session_destroy(Session        *session);
40
41 static gint nntp_ok             (SockInfo       *sock,
42                                  gchar          *argbuf);
43
44 static void nntp_gen_send       (SockInfo       *sock,
45                                  const gchar    *format,
46                                  ...);
47 static gint nntp_gen_recv       (SockInfo       *sock,
48                                  gchar          *buf,
49                                  gint            size);
50 static gint nntp_gen_command    (NNTPSession    *session,
51                                  gchar          *argbuf,
52                                  const gchar    *format,
53                                  ...);
54
55 #if USE_OPENSSL
56 Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
57                           const gchar *userid, const gchar *passwd,
58                           SSLType ssl_type)
59 #else
60 Session *nntp_session_new(const gchar *server, gushort port, gchar *buf,
61                           const gchar *userid, const gchar *passwd)
62 #endif
63 {
64         NNTPSession *session;
65         SockInfo *sock;
66
67         if ((sock = sock_connect(server, port)) == NULL) {
68                 log_warning(_("Can't connect to NNTP server: %s:%d\n"),
69                             server, port);
70                 return NULL;
71         }
72
73 #if USE_OPENSSL
74         if (ssl_type == SSL_TUNNEL && !ssl_init_socket(sock)) {
75                 sock_close(sock);
76                 return NULL;
77         }
78 #endif
79
80         if (nntp_ok(sock, buf) != NN_SUCCESS) {
81                 sock_close(sock);
82                 return NULL;
83         }
84
85         session = g_new0(NNTPSession, 1);
86         SESSION(session)->type                  = SESSION_NEWS;
87         SESSION(session)->server                = g_strdup(server);
88         SESSION(session)->sock                  = sock;
89         SESSION(session)->last_access_time      = time(NULL);
90         SESSION(session)->data                  = NULL;
91
92         SESSION(session)->destroy               = nntp_session_destroy;
93
94         session->group = NULL;
95
96         if (userid && passwd) {
97                 session->userid = g_strdup(userid);
98                 session->passwd = g_strdup(passwd);
99         }
100
101         return SESSION(session);
102 }
103
104 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
105
106 {
107         if (!session) return;
108                 
109         nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
110
111
112 }
113
114 static void nntp_session_destroy(Session *session)
115 {
116         NNTPSession *nntp_session = NNTP_SESSION(session);
117
118         g_return_if_fail(session != NULL);
119
120         g_free(nntp_session->group);
121         g_free(nntp_session->userid);
122         g_free(nntp_session->passwd);
123 }
124
125 gint nntp_group(NNTPSession *session, const gchar *group,
126                 gint *num, gint *first, gint *last)
127 {
128         gint ok;
129         gint resp;
130         gchar buf[NNTPBUFSIZE];
131
132         ok = nntp_gen_command(session, buf, "GROUP %s", group);
133
134         if (ok != NN_SUCCESS) {
135                 ok = nntp_mode(session, FALSE);
136                 if (ok == NN_SUCCESS)
137                         ok = nntp_gen_command(session, buf, "GROUP %s", group);
138         }
139
140         if (ok != NN_SUCCESS)
141                 return ok;
142
143         if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
144             != 4) {
145                 log_warning(_("protocol error: %s\n"), buf);
146                 return NN_PROTOCOL;
147         }
148
149         return NN_SUCCESS;
150 }
151
152 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
153                       gchar **msgid)
154 {
155         gint ok;
156         gchar buf[NNTPBUFSIZE];
157
158         if (num > 0)
159                 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
160         else
161                 ok = nntp_gen_command(session, buf, cmd);
162
163         if (ok != NN_SUCCESS)
164                 return ok;
165
166         extract_parenthesis(buf, '<', '>');
167         if (buf[0] == '\0') {
168                 log_warning(_("protocol error\n"));
169                 return NN_PROTOCOL;
170         }
171         *msgid = g_strdup(buf);
172
173         return NN_SUCCESS;
174 }
175
176 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
177 {
178         return nntp_get_article(session, "ARTICLE", num, msgid);
179 }
180
181 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
182 {
183         return nntp_get_article(session, "BODY", num, msgid);
184 }
185
186 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
187 {
188         return nntp_get_article(session, "HEAD", num, msgid);
189 }
190
191 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
192 {
193         return nntp_get_article(session, "STAT", num, msgid);
194 }
195
196 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
197 {
198         gint ok;
199         gint resp;
200         gchar buf[NNTPBUFSIZE];
201
202         ok = nntp_gen_command(session, buf, "NEXT");
203
204         if (ok != NN_SUCCESS)
205                 return ok;
206
207         if (sscanf(buf, "%d %d", &resp, num) != 2) {
208                 log_warning(_("protocol error: %s\n"), buf);
209                 return NN_PROTOCOL;
210         }
211
212         extract_parenthesis(buf, '<', '>');
213         if (buf[0] == '\0') {
214                 log_warning(_("protocol error\n"));
215                 return NN_PROTOCOL;
216         }
217         *msgid = g_strdup(buf);
218
219         return NN_SUCCESS;
220 }
221
222 gint nntp_xover(NNTPSession *session, gint first, gint last)
223 {
224         gint ok;
225         gchar buf[NNTPBUFSIZE];
226
227         ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
228         if (ok != NN_SUCCESS)
229                 return ok;
230
231         return NN_SUCCESS;
232 }
233
234 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
235 {
236         gint ok;
237         gchar buf[NNTPBUFSIZE];
238
239         ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
240                               header, first, last);
241         if (ok != NN_SUCCESS)
242                 return ok;
243
244         return NN_SUCCESS;
245 }
246
247 gint nntp_list(NNTPSession *session)
248 {
249         return nntp_gen_command(session, NULL, "LIST");
250 }
251
252 gint nntp_post(NNTPSession *session, FILE *fp)
253 {
254         gint ok;
255         gchar buf[NNTPBUFSIZE];
256         gchar *msg;
257
258         ok = nntp_gen_command(session, buf, "POST");
259         if (ok != NN_SUCCESS)
260                 return ok;
261
262         msg = get_outgoing_rfc2822_str(fp);
263         if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
264                 log_warning(_("Error occurred while posting\n"));
265                 g_free(msg);
266                 return NN_SOCKET;
267         }
268         g_free(msg);
269
270         sock_write_all(SESSION(session)->sock, ".\r\n", 3);
271         if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
272                 return ok;
273
274         return NN_SUCCESS;
275 }
276
277 gint nntp_newgroups(NNTPSession *session)
278 {
279         return NN_SUCCESS;
280 }
281
282 gint nntp_newnews(NNTPSession *session)
283 {
284         return NN_SUCCESS;
285 }
286
287 gint nntp_mode(NNTPSession *session, gboolean stream)
288 {
289         gint ok;
290
291         if (session->auth_failed)
292                 return NN_AUTHREQ;
293
294         ok = nntp_gen_command(session, NULL, "MODE %s",
295                               stream ? "STREAM" : "READER");
296
297         return ok;
298 }
299
300 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
301 {
302         gint ok;
303         gchar buf[NNTPBUFSIZE];
304
305         if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
306                 if (strlen(buf) < 3)
307                         return NN_ERROR;
308
309                 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
310                     (buf[3] == ' ' || buf[3] == '\0')) {
311                         if (argbuf)
312                                 strcpy(argbuf, buf);
313
314                         if (!strncmp(buf, "381", 3))
315                                 return NN_AUTHCONT;
316
317                         return NN_SUCCESS;
318                 } else if (!strncmp(buf, "480", 3))
319                         return NN_AUTHREQ;
320                 else
321                         return NN_ERROR;
322         }
323
324         return ok;
325 }
326
327 static void nntp_gen_send(SockInfo *sock, const gchar *format, ...)
328 {
329         gchar buf[NNTPBUFSIZE];
330         va_list args;
331
332         va_start(args, format);
333         g_vsnprintf(buf, sizeof(buf), format, args);
334         va_end(args);
335
336         if (verbose) {
337                 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
338                         log_print("NNTP> AUTHINFO PASS ********\n");
339                 else
340                         log_print("NNTP> %s\n", buf);
341         }
342
343         strcat(buf, "\r\n");
344         sock_write_all(sock, buf, strlen(buf));
345 }
346
347 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
348 {
349         if (sock_gets(sock, buf, size) == -1)
350                 return NN_SOCKET;
351
352         strretchomp(buf);
353
354         if (verbose)
355                 log_print("NNTP< %s\n", buf);
356
357         return NN_SUCCESS;
358 }
359
360 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
361                              const gchar *format, ...)
362 {
363         gchar buf[NNTPBUFSIZE];
364         va_list args;
365         gint ok;
366         SockInfo *sock;
367
368         va_start(args, format);
369         g_vsnprintf(buf, sizeof(buf), format, args);
370         va_end(args);
371
372         sock = SESSION(session)->sock;
373         nntp_gen_send(sock, "%s", buf);
374         ok = nntp_ok(sock, argbuf);
375         if (ok == NN_AUTHREQ) {
376                 if (!session->userid || !session->passwd) {
377                         session->auth_failed = TRUE;
378                         return ok;
379                 }
380
381                 nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
382                 ok = nntp_ok(sock, NULL);
383                 if (ok == NN_AUTHCONT) {
384                         nntp_gen_send(sock, "AUTHINFO PASS %s",
385                                       session->passwd);
386                         ok = nntp_ok(sock, NULL);
387                 }
388                 if (ok != NN_SUCCESS) {
389                         session->auth_failed = TRUE;
390                         return ok;
391                 }
392
393                 nntp_gen_send(sock, "%s", buf);
394                 ok = nntp_ok(sock, argbuf);
395
396         } else if (ok == NN_AUTHCONT) {
397                 nntp_gen_send(sock, "AUTHINFO PASS %s", session->passwd);
398                 ok = nntp_ok(sock, NULL);
399
400                 if (ok != NN_SUCCESS) {
401                         session->auth_failed = TRUE;
402                         return ok;
403                 }
404         }
405
406         return ok;
407 }