2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 Hiroyuki Yamamoto
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.
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.
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.
38 #include "procheader.h"
41 #include "statusbar.h"
44 #include "prefs_common.h"
45 #include "prefs_account.h"
46 #include "inputdialog.h"
47 #include "alertpanel.h"
49 static gint news_get_article_cmd (NNTPSession *session,
53 static gint news_get_article (NNTPSession *session,
56 static gint news_get_header (NNTPSession *session,
60 static GSList *news_get_uncached_articles(NNTPSession *session,
65 static MsgInfo *news_parse_xover (const gchar *xover_str);
66 static GSList *news_delete_old_article (GSList *alist,
68 static void news_delete_all_article (FolderItem *item);
71 Session *news_session_new(const gchar *server, gushort port)
73 gchar buf[NNTPBUFSIZE];
77 g_return_val_if_fail(server != NULL, NULL);
79 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
81 if ((nntp_sock = nntp_open(server, port, buf)) < 0)
84 session = g_new(NNTPSession, 1);
85 SESSION(session)->type = SESSION_NEWS;
86 SESSION(session)->server = g_strdup(server);
87 SESSION(session)->sock = nntp_sock;
88 SESSION(session)->connected = TRUE;
89 SESSION(session)->phase = SESSION_READY;
90 SESSION(session)->data = NULL;
91 session->group = NULL;
93 return SESSION(session);
96 void news_session_destroy(NNTPSession *session)
98 sock_close(SESSION(session)->sock);
99 SESSION(session)->sock = NULL;
101 g_free(session->group);
104 NNTPSession *news_session_get(Folder *folder)
106 g_return_val_if_fail(folder != NULL, NULL);
107 g_return_val_if_fail(folder->type == F_NEWS, NULL);
108 g_return_val_if_fail(folder->account != NULL, NULL);
110 if (!REMOTE_FOLDER(folder)->session) {
111 REMOTE_FOLDER(folder)->session =
112 news_session_new(folder->account->nntp_server, 119);
114 if (nntp_mode(REMOTE_FOLDER(folder)->session->sock, FALSE)
116 log_warning(_("NNTP connection to %s:%d has been"
117 " disconnected. Reconnecting...\n"),
118 folder->account->nntp_server, 119);
119 session_destroy(REMOTE_FOLDER(folder)->session);
120 REMOTE_FOLDER(folder)->session =
121 news_session_new(folder->account->nntp_server,
126 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
129 GSList *news_get_article_list(Folder *folder, FolderItem *item,
133 NNTPSession *session;
135 g_return_val_if_fail(folder != NULL, NULL);
136 g_return_val_if_fail(item != NULL, NULL);
137 g_return_val_if_fail(folder->type == F_NEWS, NULL);
139 session = news_session_get(folder);
142 alist = procmsg_read_cache(item, FALSE);
143 item->last_num = procmsg_get_last_num_in_cache(alist);
144 } else if (use_cache) {
149 alist = procmsg_read_cache(item, FALSE);
151 cache_last = procmsg_get_last_num_in_cache(alist);
152 newlist = news_get_uncached_articles
153 (session, item, cache_last, &first, &last);
154 alist = news_delete_old_article(alist, first);
156 alist = g_slist_concat(alist, newlist);
157 item->last_num = last;
161 alist = news_get_uncached_articles
162 (session, item, 0, NULL, &last);
163 news_delete_all_article(item);
164 item->last_num = last;
167 procmsg_set_flags(alist, item);
174 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
176 gchar *path, *filename;
179 g_return_val_if_fail(folder != NULL, NULL);
180 g_return_val_if_fail(item != NULL, NULL);
182 path = folder_item_get_path(item);
183 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
186 if (is_file_exist(filename)) {
187 debug_print(_("article %d has been already cached.\n"), num);
191 if (!REMOTE_FOLDER(folder)->session) {
196 debug_print(_("getting article %d...\n"), num);
197 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
201 g_warning(_("can't read article %d\n"), num);
209 void news_scan_group(Folder *folder, FolderItem *item)
213 gint news_post(Folder *folder, const gchar *file)
215 NNTPSession *session;
219 g_return_val_if_fail(folder != NULL, -1);
220 g_return_val_if_fail(folder->type == F_NEWS, -1);
221 g_return_val_if_fail(file != NULL, -1);
223 session = news_session_get(folder);
224 if (!session) return -1;
226 if ((fp = fopen(file, "r")) == NULL) {
227 FILE_OP_ERROR(file, "fopen");
231 ok = nntp_post(SESSION(session)->sock, fp);
232 if (ok != NN_SUCCESS) {
233 log_warning(_("can't post article.\n"));
244 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
245 gint num, gchar *filename)
249 if (nntp_get_article(SESSION(session)->sock, cmd, num, &msgid)
253 debug_print("Message-Id = %s, num = %d\n", msgid, num);
256 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
257 log_warning(_("can't retrieve article %d\n"), num);
264 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
266 return news_get_article_cmd(session, "ARTICLE", num, filename);
269 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
271 return news_get_article_cmd(session, "HEAD", num, filename);
274 static gchar *news_query_password(NNTPSession *session,
280 message = g_strdup_printf
281 (_("Input password for %s on %s:"),
283 SESSION(session)->server);
285 pass = input_dialog_with_invisible(_("Input password"),
288 /* manage_window_focus_in(inc_dialog->mainwin->window, */
293 static gint news_authenticate(NNTPSession *session,
299 gboolean need_free_pass = FALSE;
301 debug_print(_("news server requested authentication\n"));
302 if (!item || !item->folder || !item->folder->account)
304 user = item->folder->account->userid;
307 ok = nntp_authinfo_user(SESSION(session)->sock, user);
308 if (ok == NN_AUTHCONT) {
309 pass = item->folder->account->passwd;
310 if (!pass || !pass[0]) {
311 pass = news_query_password(session, user);
312 need_free_pass = TRUE;
314 ok = nntp_authinfo_pass(SESSION(session)->sock, pass);
318 if (ok != NN_SUCCESS) {
319 log_warning(_("NNTP authentication failed\n"));
320 alertpanel_error(_("Authentication for %s on %s failed."),
321 user, SESSION(session)->server);
326 static GSList *news_get_uncached_articles(NNTPSession *session,
327 FolderItem *item, gint cache_last,
328 gint *rfirst, gint *rlast)
331 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
332 gchar buf[NNTPBUFSIZE];
333 GSList *newlist = NULL;
334 GSList *llast = NULL;
337 if (rfirst) *rfirst = 0;
338 if (rlast) *rlast = 0;
340 g_return_val_if_fail(session != NULL, NULL);
341 g_return_val_if_fail(item != NULL, NULL);
342 g_return_val_if_fail(item->folder != NULL, NULL);
343 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
345 ok = nntp_group(SESSION(session)->sock, item->path,
346 &num, &first, &last);
347 if (ok == NN_AUTHREQ) {
348 ok = news_authenticate(session, item);
349 if (ok != NN_SUCCESS)
351 ok = nntp_group(SESSION(session)->sock, item->path,
352 &num, &first, &last);
354 if (ok != NN_SUCCESS) {
355 log_warning(_("can't set group: %s\n"), item->path);
359 /* calculate getting overview range */
361 log_warning(_("invalid article range: %d - %d\n"),
365 if (cache_last < first)
367 else if (last < cache_last)
369 else if (last == cache_last) {
370 debug_print(_("no new articles.\n"));
373 begin = cache_last + 1;
376 if (prefs_common.max_articles > 0 &&
377 end - begin + 1 > prefs_common.max_articles)
378 begin = end - prefs_common.max_articles + 1;
380 log_message(_("getting xover %d - %d in %s...\n"),
381 begin, end, item->path);
382 if (nntp_xover(SESSION(session)->sock, begin, end) != NN_SUCCESS) {
383 log_warning(_("can't get xover\n"));
388 if (sock_read(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
389 log_warning(_("error occurred while getting xover.\n"));
393 if (buf[0] == '.' && buf[1] == '\r') break;
395 msginfo = news_parse_xover(buf);
397 log_warning(_("invalid xover line: %s\n"), buf);
401 msginfo->folder = item;
402 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
405 llast = newlist = g_slist_append(newlist, msginfo);
407 llast = g_slist_append(llast, msginfo);
412 if (rfirst) *rfirst = first;
413 if (rlast) *rlast = last;
417 #define PARSE_ONE_PARAM(p, srcp) \
419 p = strchr(srcp, '\t'); \
420 if (!p) return NULL; \
425 static MsgInfo *news_parse_xover(const gchar *xover_str)
428 gchar buf[NNTPBUFSIZE];
429 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
431 gint num, size_int, line_int;
434 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
435 strcpy(xover_buf, xover_str);
437 PARSE_ONE_PARAM(subject, xover_buf);
438 PARSE_ONE_PARAM(sender, subject);
439 PARSE_ONE_PARAM(date, sender);
440 PARSE_ONE_PARAM(msgid, date);
441 PARSE_ONE_PARAM(ref, msgid);
442 PARSE_ONE_PARAM(size, ref);
443 PARSE_ONE_PARAM(line, size);
445 tmp = strchr(line, '\t');
446 if (!tmp) tmp = strchr(line, '\r');
447 if (!tmp) tmp = strchr(line, '\n');
448 if (tmp) *tmp = '\0';
450 num = atoi(xover_str);
451 size_int = atoi(size);
452 line_int = atoi(line);
455 msginfo = g_new0(MsgInfo, 1);
456 msginfo->msgnum = num;
457 msginfo->size = size_int;
459 msginfo->date = g_strdup(date);
460 msginfo->date_t = procheader_date_parse(NULL, date, 0);
462 conv_unmime_header(buf, sizeof(buf), sender, NULL);
463 msginfo->from = g_strdup(buf);
464 msginfo->fromname = procheader_get_fromname(buf);
466 conv_unmime_header(buf, sizeof(buf), subject, NULL);
467 msginfo->subject = g_strdup(buf);
469 extract_parenthesis(msgid, '<', '>');
472 msginfo->msgid = g_strdup(msgid);
474 eliminate_parenthesis(ref, '(', ')');
475 if ((p = strrchr(ref, '<')) != NULL) {
476 extract_parenthesis(p, '<', '>');
479 msginfo->inreplyto = g_strdup(p);
485 static GSList *news_delete_old_article(GSList *alist, gint first)
491 if (first < 2) return alist;
493 for (cur = alist; cur != NULL; ) {
496 msginfo = (MsgInfo *)cur->data;
497 if (msginfo && msginfo->msgnum < first) {
498 debug_print(_("deleting article %d...\n"),
501 cache_file = procmsg_get_message_file_path(msginfo);
502 if (is_file_exist(cache_file)) unlink(cache_file);
505 procmsg_msginfo_free(msginfo);
506 alist = g_slist_remove(alist, msginfo);
515 static void news_delete_all_article(FolderItem *item)
522 dir = folder_item_get_path(item);
523 if ((dp = opendir(dir)) == NULL) {
524 FILE_OP_ERROR(dir, "opendir");
529 debug_print(_("\tDeleting all cached articles... "));
531 while ((d = readdir(dp)) != NULL) {
532 if (to_number(d->d_name) < 0) continue;
534 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
536 if (is_file_exist(file)) {
537 if (unlink(file) < 0)
538 FILE_OP_ERROR(file, "unlink");
547 debug_print(_("done.\n"));
551 news_get_group_list returns a strings list.
552 These strings are the names of the newsgroups of a server.
553 item is the FolderItem of the news server.
554 The names of the newsgroups are cached into a file so that
555 when the function is called again, there is no need to make
556 a request to the server.
559 GSList * news_get_group_list(FolderItem *item)
561 gchar *path, *filename;
563 NNTPSession *session;
564 GSList * group_list = NULL;
566 gchar buf[NNTPBUFSIZE];
572 path = folder_item_get_path(item);
574 if (!is_dir_exist(path))
577 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
580 session = news_session_get(item->folder);
585 if (is_file_exist(filename)) {
586 debug_print(_("group list has been already cached.\n"));
589 ok = nntp_list(SESSION(session)->sock);
590 if (ok != NN_SUCCESS)
593 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
594 log_warning(_("can't retrieve group list\n"));
599 f = fopen(filename, "r");
600 while (fgets(buf, NNTPBUFSIZE, f)) {
604 while ((buf[len] != 0) && (buf[len] != ' '))
609 group_list = g_slist_append(group_list, s);
614 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
620 remove the cache file of the names of the newsgroups.
623 void news_reset_group_list(FolderItem *item)
625 gchar *path, *filename;
627 debug_print(_("\tDeleting cached group list... "));
628 path = folder_item_get_path(item);
629 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
631 if (remove(filename) != 0)
632 log_warning(_("can't delete cached group list %s\n"), filename);