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 static Session *news_session_new(const gchar *server, gushort port,
72 const gchar *userid, const gchar *passwd)
74 gchar buf[NNTPBUFSIZE];
76 NNTPSockInfo *nntp_sock;
78 g_return_val_if_fail(server != NULL, NULL);
80 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
83 nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
85 nntp_sock = nntp_open(server, port, buf);
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;
99 return SESSION(session);
102 void news_session_destroy(NNTPSession *session)
104 nntp_close(session->nntp_sock);
105 session->nntp_sock = NULL;
106 SESSION(session)->sock = NULL;
108 g_free(session->group);
111 static gchar *news_query_password(const gchar *server,
117 message = g_strdup_printf(_("Input password for %s on %s:"),
120 pass = input_dialog_with_invisible(_("Input password"),
123 /* manage_window_focus_in(inc_dialog->mainwin->window, */
128 static Session *news_session_new_for_folder(Folder *folder)
135 ac = folder->account;
136 if (ac->userid && ac->userid[0]) {
138 if (ac->passwd && ac->passwd[0])
139 passwd = g_strdup(ac->passwd);
141 passwd = news_query_password(ac->nntp_server, userid);
143 userid = passwd = NULL;
145 session = news_session_new(ac->nntp_server, 119, userid, passwd);
150 NNTPSession *news_session_get(Folder *folder)
152 NNTPSession *session;
154 g_return_val_if_fail(folder != NULL, NULL);
155 g_return_val_if_fail(folder->type == F_NEWS, NULL);
156 g_return_val_if_fail(folder->account != NULL, NULL);
158 if (!REMOTE_FOLDER(folder)->session) {
159 REMOTE_FOLDER(folder)->session =
160 news_session_new_for_folder(folder);
162 session = NNTP_SESSION(REMOTE_FOLDER(folder)->session);
163 if (nntp_mode(session->nntp_sock, FALSE) != NN_SUCCESS) {
164 log_warning(_("NNTP connection to %s:%d has been"
165 " disconnected. Reconnecting...\n"),
166 folder->account->nntp_server, 119);
167 session_destroy(REMOTE_FOLDER(folder)->session);
168 REMOTE_FOLDER(folder)->session =
169 news_session_new_for_folder(folder);
173 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
176 GSList *news_get_article_list(Folder *folder, FolderItem *item,
180 NNTPSession *session;
182 g_return_val_if_fail(folder != NULL, NULL);
183 g_return_val_if_fail(item != NULL, NULL);
184 g_return_val_if_fail(folder->type == F_NEWS, NULL);
186 session = news_session_get(folder);
189 alist = procmsg_read_cache(item, FALSE);
190 item->last_num = procmsg_get_last_num_in_cache(alist);
191 } else if (use_cache) {
196 alist = procmsg_read_cache(item, FALSE);
198 cache_last = procmsg_get_last_num_in_cache(alist);
199 newlist = news_get_uncached_articles
200 (session, item, cache_last, &first, &last);
201 alist = news_delete_old_article(alist, first);
203 alist = g_slist_concat(alist, newlist);
204 item->last_num = last;
208 alist = news_get_uncached_articles
209 (session, item, 0, NULL, &last);
210 news_delete_all_article(item);
211 item->last_num = last;
214 procmsg_set_flags(alist, item);
221 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
223 gchar *path, *filename;
226 g_return_val_if_fail(folder != NULL, NULL);
227 g_return_val_if_fail(item != NULL, NULL);
229 path = folder_item_get_path(item);
230 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
233 if (is_file_exist(filename)) {
234 debug_print(_("article %d has been already cached.\n"), num);
238 if (!REMOTE_FOLDER(folder)->session) {
243 debug_print(_("getting article %d...\n"), num);
244 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
248 g_warning(_("can't read article %d\n"), num);
256 void news_scan_group(Folder *folder, FolderItem *item)
260 gint news_post(Folder *folder, const gchar *file)
262 NNTPSession *session;
266 g_return_val_if_fail(folder != NULL, -1);
267 g_return_val_if_fail(folder->type == F_NEWS, -1);
268 g_return_val_if_fail(file != NULL, -1);
270 session = news_session_get(folder);
271 if (!session) return -1;
273 if ((fp = fopen(file, "r")) == NULL) {
274 FILE_OP_ERROR(file, "fopen");
278 ok = nntp_post(session->nntp_sock, fp);
279 if (ok != NN_SUCCESS) {
280 log_warning(_("can't post article.\n"));
291 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
292 gint num, gchar *filename)
296 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
300 debug_print("Message-Id = %s, num = %d\n", msgid, num);
303 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
304 log_warning(_("can't retrieve article %d\n"), num);
311 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
313 return news_get_article_cmd(session, "ARTICLE", num, filename);
316 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
318 return news_get_article_cmd(session, "HEAD", num, filename);
321 static GSList *news_get_uncached_articles(NNTPSession *session,
322 FolderItem *item, gint cache_last,
323 gint *rfirst, gint *rlast)
326 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
327 gchar buf[NNTPBUFSIZE];
328 GSList *newlist = NULL;
329 GSList *llast = NULL;
332 if (rfirst) *rfirst = 0;
333 if (rlast) *rlast = 0;
335 g_return_val_if_fail(session != NULL, NULL);
336 g_return_val_if_fail(item != NULL, NULL);
337 g_return_val_if_fail(item->folder != NULL, NULL);
338 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
340 ok = nntp_group(session->nntp_sock, item->path,
341 &num, &first, &last);
342 if (ok != NN_SUCCESS) {
343 log_warning(_("can't set group: %s\n"), item->path);
347 /* calculate getting overview range */
349 log_warning(_("invalid article range: %d - %d\n"),
353 if (cache_last < first)
355 else if (last < cache_last)
357 else if (last == cache_last) {
358 debug_print(_("no new articles.\n"));
361 begin = cache_last + 1;
364 if (prefs_common.max_articles > 0 &&
365 end - begin + 1 > prefs_common.max_articles)
366 begin = end - prefs_common.max_articles + 1;
368 log_message(_("getting xover %d - %d in %s...\n"),
369 begin, end, item->path);
370 if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
371 log_warning(_("can't get xover\n"));
376 if (sock_read(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
377 log_warning(_("error occurred while getting xover.\n"));
381 if (buf[0] == '.' && buf[1] == '\r') break;
383 msginfo = news_parse_xover(buf);
385 log_warning(_("invalid xover line: %s\n"), buf);
389 msginfo->folder = item;
390 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
393 llast = newlist = g_slist_append(newlist, msginfo);
395 llast = g_slist_append(llast, msginfo);
400 if (rfirst) *rfirst = first;
401 if (rlast) *rlast = last;
405 #define PARSE_ONE_PARAM(p, srcp) \
407 p = strchr(srcp, '\t'); \
408 if (!p) return NULL; \
413 static MsgInfo *news_parse_xover(const gchar *xover_str)
416 gchar buf[NNTPBUFSIZE];
417 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
419 gint num, size_int, line_int;
422 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
423 strcpy(xover_buf, xover_str);
425 PARSE_ONE_PARAM(subject, xover_buf);
426 PARSE_ONE_PARAM(sender, subject);
427 PARSE_ONE_PARAM(date, sender);
428 PARSE_ONE_PARAM(msgid, date);
429 PARSE_ONE_PARAM(ref, msgid);
430 PARSE_ONE_PARAM(size, ref);
431 PARSE_ONE_PARAM(line, size);
433 tmp = strchr(line, '\t');
434 if (!tmp) tmp = strchr(line, '\r');
435 if (!tmp) tmp = strchr(line, '\n');
436 if (tmp) *tmp = '\0';
438 num = atoi(xover_str);
439 size_int = atoi(size);
440 line_int = atoi(line);
443 msginfo = g_new0(MsgInfo, 1);
444 msginfo->msgnum = num;
445 msginfo->size = size_int;
447 msginfo->date = g_strdup(date);
448 msginfo->date_t = procheader_date_parse(NULL, date, 0);
450 conv_unmime_header(buf, sizeof(buf), sender, NULL);
451 msginfo->from = g_strdup(buf);
452 msginfo->fromname = procheader_get_fromname(buf);
454 conv_unmime_header(buf, sizeof(buf), subject, NULL);
455 msginfo->subject = g_strdup(buf);
457 extract_parenthesis(msgid, '<', '>');
460 msginfo->msgid = g_strdup(msgid);
462 eliminate_parenthesis(ref, '(', ')');
463 if ((p = strrchr(ref, '<')) != NULL) {
464 extract_parenthesis(p, '<', '>');
467 msginfo->inreplyto = g_strdup(p);
473 static GSList *news_delete_old_article(GSList *alist, gint first)
479 if (first < 2) return alist;
481 for (cur = alist; cur != NULL; ) {
484 msginfo = (MsgInfo *)cur->data;
485 if (msginfo && msginfo->msgnum < first) {
486 debug_print(_("deleting article %d...\n"),
489 cache_file = procmsg_get_message_file_path(msginfo);
490 if (is_file_exist(cache_file)) unlink(cache_file);
493 procmsg_msginfo_free(msginfo);
494 alist = g_slist_remove(alist, msginfo);
503 static void news_delete_all_article(FolderItem *item)
510 dir = folder_item_get_path(item);
511 if ((dp = opendir(dir)) == NULL) {
512 FILE_OP_ERROR(dir, "opendir");
517 debug_print(_("\tDeleting all cached articles... "));
519 while ((d = readdir(dp)) != NULL) {
520 if (to_number(d->d_name) < 0) continue;
522 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
524 if (is_file_exist(file)) {
525 if (unlink(file) < 0)
526 FILE_OP_ERROR(file, "unlink");
535 debug_print(_("done.\n"));
539 news_get_group_list returns a strings list.
540 These strings are the names of the newsgroups of a server.
541 item is the FolderItem of the news server.
542 The names of the newsgroups are cached into a file so that
543 when the function is called again, there is no need to make
544 a request to the server.
547 GSList * news_get_group_list(FolderItem *item)
549 gchar *path, *filename;
551 NNTPSession *session;
552 GSList * group_list = NULL;
554 gchar buf[NNTPBUFSIZE];
560 path = folder_item_get_path(item);
562 if (!is_dir_exist(path))
565 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
568 session = news_session_get(item->folder);
573 if (is_file_exist(filename)) {
574 debug_print(_("group list has been already cached.\n"));
577 ok = nntp_list(session->nntp_sock);
578 if (ok != NN_SUCCESS)
581 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
582 log_warning(_("can't retrieve group list\n"));
587 f = fopen(filename, "r");
588 while (fgets(buf, NNTPBUFSIZE, f)) {
592 while ((buf[len] != 0) && (buf[len] != ' '))
597 group_list = g_slist_append(group_list, s);
602 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
608 remove the cache file of the names of the newsgroups.
611 void news_reset_group_list(FolderItem *item)
613 gchar *path, *filename;
615 debug_print(_("\tDeleting cached group list... "));
616 path = folder_item_get_path(item);
617 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
619 if (remove(filename) != 0)
620 log_warning(_("can't delete cached group list %s\n"), filename);