changed sock_read() in some files to sock_gets().
[claws.git] / src / news.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999,2000 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 #include <stdlib.h>
28 #include <dirent.h>
29 #include <unistd.h>
30
31 #include "defs.h"
32 #include "intl.h"
33 #include "news.h"
34 #include "nntp.h"
35 #include "socket.h"
36 #include "recv.h"
37 #include "procmsg.h"
38 #include "procheader.h"
39 #include "folder.h"
40 #include "session.h"
41 #include "statusbar.h"
42 #include "codeconv.h"
43 #include "utils.h"
44 #include "prefs_common.h"
45 #include "prefs_account.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
48
49 static gint news_get_article_cmd         (NNTPSession   *session,
50                                           const gchar   *cmd,
51                                           gint           num,
52                                           gchar         *filename);
53 static gint news_get_article             (NNTPSession   *session,
54                                           gint           num,
55                                           gchar         *filename);
56 static gint news_get_header              (NNTPSession   *session,
57                                           gint           num,
58                                           gchar         *filename);
59
60 static GSList *news_get_uncached_articles(NNTPSession   *session,
61                                           FolderItem    *item,
62                                           gint           cache_last,
63                                           gint          *rfirst,
64                                           gint          *rlast);
65 static MsgInfo *news_parse_xover         (const gchar   *xover_str);
66 static GSList *news_delete_old_article   (GSList        *alist,
67                                           gint           first);
68 static void news_delete_all_article      (FolderItem    *item);
69
70
71 static Session *news_session_new(const gchar *server, gushort port,
72                                  const gchar *userid, const gchar *passwd)
73 {
74         gchar buf[NNTPBUFSIZE];
75         NNTPSession *session;
76         NNTPSockInfo *nntp_sock;
77
78         g_return_val_if_fail(server != NULL, NULL);
79
80         log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
81
82         if (userid && passwd)
83                 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
84         else
85                 nntp_sock = nntp_open(server, port, buf);
86         if (nntp_sock < 0)
87                 return NULL;
88
89         session = g_new(NNTPSession, 1);
90         SESSION(session)->type      = SESSION_NEWS;
91         SESSION(session)->server    = g_strdup(server);
92         session->nntp_sock          = nntp_sock;
93         SESSION(session)->sock      = nntp_sock->sock;
94         SESSION(session)->connected = TRUE;
95         SESSION(session)->phase     = SESSION_READY;
96         SESSION(session)->data      = NULL;
97         session->group = NULL;
98
99         return SESSION(session);
100 }
101
102 void news_session_destroy(NNTPSession *session)
103 {
104         nntp_close(session->nntp_sock);
105         session->nntp_sock = NULL;
106         SESSION(session)->sock = NULL;
107
108         g_free(session->group);
109 }
110
111 static gchar *news_query_password(const gchar *server,
112                                   const gchar *user)
113 {
114         gchar *message;
115         gchar *pass;
116
117         message = g_strdup_printf(_("Input password for %s on %s:"),
118                                   user, server);
119
120         pass = input_dialog_with_invisible(_("Input password"),
121                                            message, NULL);
122         g_free(message);
123 /*      manage_window_focus_in(inc_dialog->mainwin->window, */
124 /*                             NULL, NULL); */
125         return pass;
126 }
127
128 static Session *news_session_new_for_folder(Folder *folder)
129 {
130         Session *session;
131         PrefsAccount *ac;
132         const gchar *userid;
133         gchar *passwd;
134
135         ac = folder->account;
136         if (ac->userid && ac->userid[0]) {
137                 userid = ac->userid;
138                 if (ac->passwd && ac->passwd[0])
139                         passwd = g_strdup(ac->passwd);
140                 else {
141                         passwd = news_query_password(ac->nntp_server, userid);
142                         if (!passwd)
143                                 userid = NULL;
144                 }
145         } else {
146                 userid = passwd = NULL;
147         }
148         session = news_session_new(ac->nntp_server, 119, userid, passwd);
149         g_free(passwd);
150         return session;
151 }
152
153 NNTPSession *news_session_get(Folder *folder)
154 {
155         NNTPSession *session;
156
157         g_return_val_if_fail(folder != NULL, NULL);
158         g_return_val_if_fail(folder->type == F_NEWS, NULL);
159         g_return_val_if_fail(folder->account != NULL, NULL);
160
161         if (!REMOTE_FOLDER(folder)->session) {
162                 REMOTE_FOLDER(folder)->session =
163                         news_session_new_for_folder(folder);
164         } else {
165                 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
166                 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
167                         log_warning(_("NNTP connection to %s:%d has been"
168                                       " disconnected. Reconnecting...\n"),
169                                     folder->account->nntp_server, 119);
170                         session_destroy(REMOTE_FOLDER(folder)->session);
171                         REMOTE_FOLDER(folder)->session =
172                                 news_session_new_for_folder(folder);
173                 }
174         }
175
176         return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
177 }
178
179 GSList *news_get_article_list(Folder *folder, FolderItem *item,
180                               gboolean use_cache)
181 {
182         GSList *alist;
183         NNTPSession *session;
184
185         g_return_val_if_fail(folder != NULL, NULL);
186         g_return_val_if_fail(item != NULL, NULL);
187         g_return_val_if_fail(folder->type == F_NEWS, NULL);
188
189         session = news_session_get(folder);
190
191         if (!session) {
192                 alist = procmsg_read_cache(item, FALSE);
193                 item->last_num = procmsg_get_last_num_in_cache(alist);
194         } else if (use_cache) {
195                 GSList *newlist;
196                 gint cache_last;
197                 gint first, last;
198
199                 alist = procmsg_read_cache(item, FALSE);
200
201                 cache_last = procmsg_get_last_num_in_cache(alist);
202                 newlist = news_get_uncached_articles
203                         (session, item, cache_last, &first, &last);
204                 alist = news_delete_old_article(alist, first);
205
206                 alist = g_slist_concat(alist, newlist);
207                 item->last_num = last;
208         } else {
209                 gint last;
210
211                 alist = news_get_uncached_articles
212                         (session, item, 0, NULL, &last);
213                 news_delete_all_article(item);
214                 item->last_num = last;
215         }
216
217         procmsg_set_flags(alist, item);
218
219         statusbar_pop_all();
220
221         return alist;
222 }
223
224 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
225 {
226         gchar *path, *filename;
227         gint ok;
228
229         g_return_val_if_fail(folder != NULL, NULL);
230         g_return_val_if_fail(item != NULL, NULL);
231
232         path = folder_item_get_path(item);
233         filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
234         g_free(path);
235
236         if (is_file_exist(filename)) {
237                 debug_print(_("article %d has been already cached.\n"), num);
238                 return filename;
239         }
240
241         if (!REMOTE_FOLDER(folder)->session) {
242                 g_free(filename);
243                 return NULL;
244         }
245
246         debug_print(_("getting article %d...\n"), num);
247         ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
248                               num, filename);
249         statusbar_pop_all();
250         if (ok < 0) {
251                 g_warning(_("can't read article %d\n"), num);
252                 g_free(filename);
253                 return NULL;
254         }
255
256         return filename;
257 }
258
259 void news_scan_group(Folder *folder, FolderItem *item)
260 {
261 }
262
263 gint news_post(Folder *folder, const gchar *file)
264 {
265         NNTPSession *session;
266         FILE *fp;
267         gint ok;
268
269         g_return_val_if_fail(folder != NULL, -1);
270         g_return_val_if_fail(folder->type == F_NEWS, -1);
271         g_return_val_if_fail(file != NULL, -1);
272
273         session = news_session_get(folder);
274         if (!session) return -1;
275
276         if ((fp = fopen(file, "r")) == NULL) {
277                 FILE_OP_ERROR(file, "fopen");
278                 return -1;
279         }
280
281         ok = nntp_post(session->nntp_sock, fp);
282         if (ok != NN_SUCCESS) {
283                 log_warning(_("can't post article.\n"));
284                 return -1;
285         }
286
287         fclose(fp);
288
289         statusbar_pop_all();
290
291         return 0;
292 }
293
294 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
295                                  gint num, gchar *filename)
296 {
297         gchar *msgid;
298
299         if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
300             != NN_SUCCESS)
301                 return -1;
302
303         debug_print("Message-Id = %s, num = %d\n", msgid, num);
304         g_free(msgid);
305
306         if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
307                 log_warning(_("can't retrieve article %d\n"), num);
308                 return -1;
309         }
310
311         return 0;
312 }
313
314 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
315 {
316         return news_get_article_cmd(session, "ARTICLE", num, filename);
317 }
318
319 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
320 {
321         return news_get_article_cmd(session, "HEAD", num, filename);
322 }
323
324 static GSList *news_get_uncached_articles(NNTPSession *session,
325                                           FolderItem *item, gint cache_last,
326                                           gint *rfirst, gint *rlast)
327 {
328         gint ok;
329         gint num = 0, first = 0, last = 0, begin = 0, end = 0;
330         gchar buf[NNTPBUFSIZE];
331         GSList *newlist = NULL;
332         GSList *llast = NULL;
333         MsgInfo *msginfo;
334
335         if (rfirst) *rfirst = 0;
336         if (rlast)  *rlast  = 0;
337
338         g_return_val_if_fail(session != NULL, NULL);
339         g_return_val_if_fail(item != NULL, NULL);
340         g_return_val_if_fail(item->folder != NULL, NULL);
341         g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
342
343         ok = nntp_group(session->nntp_sock, item->path,
344                         &num, &first, &last);
345         if (ok != NN_SUCCESS) {
346                 log_warning(_("can't set group: %s\n"), item->path);
347                 return NULL;
348         }
349
350         /* calculate getting overview range */
351         if (first > last) {
352                 log_warning(_("invalid article range: %d - %d\n"),
353                             first, last);
354                 return NULL;
355         }
356         if (cache_last < first)
357                 begin = first;
358         else if (last < cache_last)
359                 begin = first;
360         else if (last == cache_last) {
361                 debug_print(_("no new articles.\n"));
362                 return NULL;
363         } else
364                 begin = cache_last + 1;
365         end = last;
366
367         if (prefs_common.max_articles > 0 &&
368             end - begin + 1 > prefs_common.max_articles)
369                 begin = end - prefs_common.max_articles + 1;
370
371         log_message(_("getting xover %d - %d in %s...\n"),
372                     begin, end, item->path);
373         if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
374                 log_warning(_("can't get xover\n"));
375                 return NULL;
376         }
377
378         for (;;) {
379                 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
380                         log_warning(_("error occurred while getting xover.\n"));
381                         return newlist;
382                 }
383
384                 if (buf[0] == '.' && buf[1] == '\r') break;
385
386                 msginfo = news_parse_xover(buf);
387                 if (!msginfo) {
388                         log_warning(_("invalid xover line: %s\n"), buf);
389                         continue;
390                 }
391
392                 msginfo->folder = item;
393                 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
394
395                 if (!newlist)
396                         llast = newlist = g_slist_append(newlist, msginfo);
397                 else {
398                         llast = g_slist_append(llast, msginfo);
399                         llast = llast->next;
400                 }
401         }
402
403         if (rfirst) *rfirst = first;
404         if (rlast)  *rlast  = last;
405         return newlist;
406 }
407
408 #define PARSE_ONE_PARAM(p, srcp) \
409 { \
410         p = strchr(srcp, '\t'); \
411         if (!p) return NULL; \
412         else \
413                 *p++ = '\0'; \
414 }
415
416 static MsgInfo *news_parse_xover(const gchar *xover_str)
417 {
418         MsgInfo *msginfo;
419         gchar buf[NNTPBUFSIZE];
420         gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
421         gchar *p;
422         gint num, size_int, line_int;
423         gchar *xover_buf;
424
425         Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
426         strcpy(xover_buf, xover_str);
427
428         PARSE_ONE_PARAM(subject, xover_buf);
429         PARSE_ONE_PARAM(sender, subject);
430         PARSE_ONE_PARAM(date, sender);
431         PARSE_ONE_PARAM(msgid, date);
432         PARSE_ONE_PARAM(ref, msgid);
433         PARSE_ONE_PARAM(size, ref);
434         PARSE_ONE_PARAM(line, size);
435
436         tmp = strchr(line, '\t');
437         if (!tmp) tmp = strchr(line, '\r');
438         if (!tmp) tmp = strchr(line, '\n');
439         if (tmp) *tmp = '\0';
440
441         num = atoi(xover_str);
442         size_int = atoi(size);
443         line_int = atoi(line);
444
445         /* set MsgInfo */
446         msginfo = g_new0(MsgInfo, 1);
447         msginfo->msgnum = num;
448         msginfo->size = size_int;
449
450         msginfo->date = g_strdup(date);
451         msginfo->date_t = procheader_date_parse(NULL, date, 0);
452
453         conv_unmime_header(buf, sizeof(buf), sender, NULL);
454         msginfo->from = g_strdup(buf);
455         msginfo->fromname = procheader_get_fromname(buf);
456
457         conv_unmime_header(buf, sizeof(buf), subject, NULL);
458         msginfo->subject = g_strdup(buf);
459
460         extract_parenthesis(msgid, '<', '>');
461         remove_space(msgid);
462         if (*msgid != '\0')
463                 msginfo->msgid = g_strdup(msgid);
464
465         eliminate_parenthesis(ref, '(', ')');
466         if ((p = strrchr(ref, '<')) != NULL) {
467                 extract_parenthesis(p, '<', '>');
468                 remove_space(p);
469                 if (*p != '\0')
470                         msginfo->inreplyto = g_strdup(p);
471         }
472
473         return msginfo;
474 }
475
476 static GSList *news_delete_old_article(GSList *alist, gint first)
477 {
478         GSList *cur, *next;
479         MsgInfo *msginfo;
480         gchar *cache_file;
481
482         if (first < 2) return alist;
483
484         for (cur = alist; cur != NULL; ) {
485                 next = cur->next;
486
487                 msginfo = (MsgInfo *)cur->data;
488                 if (msginfo && msginfo->msgnum < first) {
489                         debug_print(_("deleting article %d...\n"),
490                                     msginfo->msgnum);
491
492                         cache_file = procmsg_get_message_file_path(msginfo);
493                         if (is_file_exist(cache_file)) unlink(cache_file);
494                         g_free(cache_file);
495
496                         procmsg_msginfo_free(msginfo);
497                         alist = g_slist_remove(alist, msginfo);
498                 }
499
500                 cur = next;
501         }
502
503         return alist;
504 }
505
506 static void news_delete_all_article(FolderItem *item)
507 {
508         DIR *dp;
509         struct dirent *d;
510         gchar *dir;
511         gchar *file;
512
513         dir = folder_item_get_path(item);
514         if ((dp = opendir(dir)) == NULL) {
515                 FILE_OP_ERROR(dir, "opendir");
516                 g_free(dir);
517                 return;
518         }
519
520         debug_print(_("\tDeleting all cached articles... "));
521
522         while ((d = readdir(dp)) != NULL) {
523                 if (to_number(d->d_name) < 0) continue;
524
525                 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
526
527                 if (is_file_exist(file)) {
528                         if (unlink(file) < 0)
529                                 FILE_OP_ERROR(file, "unlink");
530                 }
531
532                 g_free(file);
533         }
534
535         closedir(dp);
536         g_free(dir);
537
538         debug_print(_("done.\n"));
539 }
540
541 /*
542   news_get_group_list returns a strings list.
543   These strings are the names of the newsgroups of a server.
544   item is the FolderItem of the news server.
545   The names of the newsgroups are cached into a file so that
546   when the function is called again, there is no need to make
547   a request to the server.
548  */
549
550 GSList * news_get_group_list(FolderItem *item)
551 {
552         gchar *path, *filename;
553         gint ok;
554         NNTPSession *session;
555         GSList * group_list = NULL;
556         FILE * f;
557         gchar buf[NNTPBUFSIZE];
558         int len;
559
560         if (item == NULL)
561           return NULL;
562
563         path = folder_item_get_path(item);
564
565         if (!is_dir_exist(path))
566                 make_dir_hier(path);
567
568         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
569         g_free(path);
570
571         session = news_session_get(item->folder);
572
573         if (session == NULL)
574           return NULL;
575
576         if (is_file_exist(filename)) {
577                 debug_print(_("group list has been already cached.\n"));
578         }
579         else {
580             ok = nntp_list(session->nntp_sock);
581             if (ok != NN_SUCCESS)
582               return NULL;
583             
584             if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
585               log_warning(_("can't retrieve group list\n"));
586               return NULL;
587             }
588         }
589
590         f = fopen(filename, "r");
591         while (fgets(buf, NNTPBUFSIZE, f)) {
592             char * s;
593
594             len = 0;
595             while ((buf[len] != 0) && (buf[len] != ' '))
596               len++;
597             buf[len] = 0;
598             s = g_strdup(buf);
599
600             group_list = g_slist_append(group_list, s);
601         }
602         fclose(f);
603         g_free(filename);
604
605         group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
606
607         return group_list;
608 }
609
610 /*
611   remove the cache file of the names of the newsgroups.
612  */
613
614 void news_reset_group_list(FolderItem *item)
615 {
616         gchar *path, *filename;
617
618         debug_print(_("\tDeleting cached group list... "));
619         path = folder_item_get_path(item);
620         filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
621         g_free(path);
622         if (remove(filename) != 0)
623           log_warning(_("can't delete cached group list %s\n"), filename);
624         g_free(filename);
625 }