inital gtk2 patch
[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
87         session_init(SESSION(session));
88
89         SESSION(session)->type                  = SESSION_NEWS;
90         SESSION(session)->server                = g_strdup(server);
91         SESSION(session)->sock                  = sock;
92         SESSION(session)->last_access_time      = time(NULL);
93         SESSION(session)->data                  = NULL;
94
95         SESSION(session)->destroy               = nntp_session_destroy;
96
97         session->group = NULL;
98
99         if (userid && passwd) {
100                 session->userid = g_strdup(userid);
101                 session->passwd = g_strdup(passwd);
102         }
103
104         return SESSION(session);
105 }
106
107 void nntp_forceauth(NNTPSession *session, gchar *buf, const gchar *userid, const gchar *passwd)
108
109 {
110         if (!session) return;
111                 
112         nntp_gen_command(session, buf , "AUTHINFO USER %s", userid);
113
114
115 }
116
117 static void nntp_session_destroy(Session *session)
118 {
119         NNTPSession *nntp_session = NNTP_SESSION(session);
120
121         g_return_if_fail(session != NULL);
122
123         g_free(nntp_session->group);
124         g_free(nntp_session->userid);
125         g_free(nntp_session->passwd);
126 }
127
128 gint nntp_group(NNTPSession *session, const gchar *group,
129                 gint *num, gint *first, gint *last)
130 {
131         gint ok;
132         gint resp;
133         gchar buf[NNTPBUFSIZE];
134
135         ok = nntp_gen_command(session, buf, "GROUP %s", group);
136
137         if (ok != NN_SUCCESS) {
138                 ok = nntp_mode(session, FALSE);
139                 if (ok == NN_SUCCESS)
140                         ok = nntp_gen_command(session, buf, "GROUP %s", group);
141         }
142
143         if (ok != NN_SUCCESS)
144                 return ok;
145
146         if (sscanf(buf, "%d %d %d %d", &resp, num, first, last)
147             != 4) {
148                 log_warning(_("protocol error: %s\n"), buf);
149                 return NN_PROTOCOL;
150         }
151
152         return NN_SUCCESS;
153 }
154
155 gint nntp_get_article(NNTPSession *session, const gchar *cmd, gint num,
156                       gchar **msgid)
157 {
158         gint ok;
159         gchar buf[NNTPBUFSIZE];
160
161         if (num > 0)
162                 ok = nntp_gen_command(session, buf, "%s %d", cmd, num);
163         else
164                 ok = nntp_gen_command(session, buf, cmd);
165
166         if (ok != NN_SUCCESS)
167                 return ok;
168
169         extract_parenthesis(buf, '<', '>');
170         if (buf[0] == '\0') {
171                 log_warning(_("protocol error\n"));
172                 return NN_PROTOCOL;
173         }
174         *msgid = g_strdup(buf);
175
176         return NN_SUCCESS;
177 }
178
179 gint nntp_article(NNTPSession *session, gint num, gchar **msgid)
180 {
181         return nntp_get_article(session, "ARTICLE", num, msgid);
182 }
183
184 gint nntp_body(NNTPSession *session, gint num, gchar **msgid)
185 {
186         return nntp_get_article(session, "BODY", num, msgid);
187 }
188
189 gint nntp_head(NNTPSession *session, gint num, gchar **msgid)
190 {
191         return nntp_get_article(session, "HEAD", num, msgid);
192 }
193
194 gint nntp_stat(NNTPSession *session, gint num, gchar **msgid)
195 {
196         return nntp_get_article(session, "STAT", num, msgid);
197 }
198
199 gint nntp_next(NNTPSession *session, gint *num, gchar **msgid)
200 {
201         gint ok;
202         gint resp;
203         gchar buf[NNTPBUFSIZE];
204
205         ok = nntp_gen_command(session, buf, "NEXT");
206
207         if (ok != NN_SUCCESS)
208                 return ok;
209
210         if (sscanf(buf, "%d %d", &resp, num) != 2) {
211                 log_warning(_("protocol error: %s\n"), buf);
212                 return NN_PROTOCOL;
213         }
214
215         extract_parenthesis(buf, '<', '>');
216         if (buf[0] == '\0') {
217                 log_warning(_("protocol error\n"));
218                 return NN_PROTOCOL;
219         }
220         *msgid = g_strdup(buf);
221
222         return NN_SUCCESS;
223 }
224
225 gint nntp_xover(NNTPSession *session, gint first, gint last)
226 {
227         gint ok;
228         gchar buf[NNTPBUFSIZE];
229
230         ok = nntp_gen_command(session, buf, "XOVER %d-%d", first, last);
231         if (ok != NN_SUCCESS)
232                 return ok;
233
234         return NN_SUCCESS;
235 }
236
237 gint nntp_xhdr(NNTPSession *session, const gchar *header, gint first, gint last)
238 {
239         gint ok;
240         gchar buf[NNTPBUFSIZE];
241
242         ok = nntp_gen_command(session, buf, "XHDR %s %d-%d",
243                               header, first, last);
244         if (ok != NN_SUCCESS)
245                 return ok;
246
247         return NN_SUCCESS;
248 }
249
250 gint nntp_list(NNTPSession *session)
251 {
252         return nntp_gen_command(session, NULL, "LIST");
253 }
254
255 gint nntp_post(NNTPSession *session, FILE *fp)
256 {
257         gint ok;
258         gchar buf[NNTPBUFSIZE];
259         gchar *msg;
260
261         ok = nntp_gen_command(session, buf, "POST");
262         if (ok != NN_SUCCESS)
263                 return ok;
264
265         msg = get_outgoing_rfc2822_str(fp);
266         if (sock_write_all(SESSION(session)->sock, msg, strlen(msg)) < 0) {
267                 log_warning(_("Error occurred while posting\n"));
268                 g_free(msg);
269                 return NN_SOCKET;
270         }
271         g_free(msg);
272
273         sock_write_all(SESSION(session)->sock, ".\r\n", 3);
274         if ((ok = nntp_ok(SESSION(session)->sock, buf)) != NN_SUCCESS)
275                 return ok;
276
277         return NN_SUCCESS;
278 }
279
280 gint nntp_newgroups(NNTPSession *session)
281 {
282         return NN_SUCCESS;
283 }
284
285 gint nntp_newnews(NNTPSession *session)
286 {
287         return NN_SUCCESS;
288 }
289
290 gint nntp_mode(NNTPSession *session, gboolean stream)
291 {
292         gint ok;
293
294         if (session->auth_failed)
295                 return NN_AUTHREQ;
296
297         ok = nntp_gen_command(session, NULL, "MODE %s",
298                               stream ? "STREAM" : "READER");
299
300         return ok;
301 }
302
303 static gint nntp_ok(SockInfo *sock, gchar *argbuf)
304 {
305         gint ok;
306         gchar buf[NNTPBUFSIZE];
307
308         if ((ok = nntp_gen_recv(sock, buf, sizeof(buf))) == NN_SUCCESS) {
309                 if (strlen(buf) < 3)
310                         return NN_ERROR;
311
312                 if ((buf[0] == '1' || buf[0] == '2' || buf[0] == '3') &&
313                     (buf[3] == ' ' || buf[3] == '\0')) {
314                         if (argbuf)
315                                 strcpy(argbuf, buf);
316
317                         if (!strncmp(buf, "381", 3))
318                                 return NN_AUTHCONT;
319
320                         return NN_SUCCESS;
321                 } else if (!strncmp(buf, "480", 3))
322                         return NN_AUTHREQ;
323                 else
324                         return NN_ERROR;
325         }
326
327         return ok;
328 }
329
330 static void nntp_gen_send(SockInfo *sock, const gchar *format, ...)
331 {
332         gchar buf[NNTPBUFSIZE];
333         va_list args;
334
335         va_start(args, format);
336         g_vsnprintf(buf, sizeof(buf), format, args);
337         va_end(args);
338
339         if (verbose) {
340                 if (!g_strncasecmp(buf, "AUTHINFO PASS", 13))
341                         log_print("NNTP> AUTHINFO PASS ********\n");
342                 else
343                         log_print("NNTP> %s\n", buf);
344         }
345
346         strcat(buf, "\r\n");
347         sock_write_all(sock, buf, strlen(buf));
348 }
349
350 static gint nntp_gen_recv(SockInfo *sock, gchar *buf, gint size)
351 {
352         if (sock_gets(sock, buf, size) == -1)
353                 return NN_SOCKET;
354
355         strretchomp(buf);
356
357         if (verbose)
358                 log_print("NNTP< %s\n", buf);
359
360         return NN_SUCCESS;
361 }
362
363 static gint nntp_gen_command(NNTPSession *session, gchar *argbuf,
364                              const gchar *format, ...)
365 {
366         gchar buf[NNTPBUFSIZE];
367         va_list args;
368         gint ok;
369         SockInfo *sock;
370
371         va_start(args, format);
372         g_vsnprintf(buf, sizeof(buf), format, args);
373         va_end(args);
374
375         sock = SESSION(session)->sock;
376         nntp_gen_send(sock, "%s", buf);
377         ok = nntp_ok(sock, argbuf);
378         if (ok == NN_AUTHREQ) {
379                 if (!session->userid || !session->passwd) {
380                         session->auth_failed = TRUE;
381                         return ok;
382                 }
383
384                 nntp_gen_send(sock, "AUTHINFO USER %s", session->userid);
385                 ok = nntp_ok(sock, NULL);
386                 if (ok == NN_AUTHCONT) {
387                         nntp_gen_send(sock, "AUTHINFO PASS %s",
388                                       session->passwd);
389                         ok = nntp_ok(sock, NULL);
390                 }
391                 if (ok != NN_SUCCESS) {
392                         session->auth_failed = TRUE;
393                         return ok;
394                 }
395
396                 nntp_gen_send(sock, "%s", buf);
397                 ok = nntp_ok(sock, argbuf);
398
399         } else if (ok == NN_AUTHCONT) {
400                 nntp_gen_send(sock, "AUTHINFO PASS %s", session->passwd);
401                 ok = nntp_ok(sock, NULL);
402
403                 if (ok != NN_SUCCESS) {
404                         session->auth_failed = TRUE;
405                         return ok;
406                 }
407         }
408
409         return ok;
410 }