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);
86 if (nntp_sock == NULL)
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->use_nntp_auth && 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);
146 userid = passwd = NULL;
148 session = news_session_new(ac->nntp_server, 119, userid, passwd);
153 NNTPSession *news_session_get(Folder *folder)
155 NNTPSession *session;
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);
161 if (!REMOTE_FOLDER(folder)->session) {
162 REMOTE_FOLDER(folder)->session =
163 news_session_new_for_folder(folder);
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);
176 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
179 GSList *news_get_article_list(Folder *folder, FolderItem *item,
183 NNTPSession *session;
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);
189 session = news_session_get(folder);
192 alist = procmsg_read_cache(item, FALSE);
193 item->last_num = procmsg_get_last_num_in_cache(alist);
194 } else if (use_cache) {
199 alist = procmsg_read_cache(item, FALSE);
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);
206 alist = g_slist_concat(alist, newlist);
207 item->last_num = last;
211 alist = news_get_uncached_articles
212 (session, item, 0, NULL, &last);
213 news_delete_all_article(item);
214 item->last_num = last;
217 procmsg_set_flags(alist, item);
224 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
226 gchar *path, *filename;
229 g_return_val_if_fail(folder != NULL, NULL);
230 g_return_val_if_fail(item != NULL, NULL);
232 path = folder_item_get_path(item);
233 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
236 if (is_file_exist(filename)) {
237 debug_print(_("article %d has been already cached.\n"), num);
241 if (!REMOTE_FOLDER(folder)->session) {
246 debug_print(_("getting article %d...\n"), num);
247 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
251 g_warning(_("can't read article %d\n"), num);
259 void news_scan_group(Folder *folder, FolderItem *item)
263 gint news_post(Folder *folder, const gchar *file)
265 NNTPSession *session;
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);
273 session = news_session_get(folder);
274 if (!session) return -1;
276 if ((fp = fopen(file, "r")) == NULL) {
277 FILE_OP_ERROR(file, "fopen");
281 ok = nntp_post(session->nntp_sock, fp);
282 if (ok != NN_SUCCESS) {
283 log_warning(_("can't post article.\n"));
294 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
295 gint num, gchar *filename)
299 if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
303 debug_print("Message-Id = %s, num = %d\n", msgid, num);
306 if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
307 log_warning(_("can't retrieve article %d\n"), num);
314 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
316 return news_get_article_cmd(session, "ARTICLE", num, filename);
319 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
321 return news_get_article_cmd(session, "HEAD", num, filename);
324 static GSList *news_get_uncached_articles(NNTPSession *session,
325 FolderItem *item, gint cache_last,
326 gint *rfirst, gint *rlast)
329 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
330 gchar buf[NNTPBUFSIZE];
331 GSList *newlist = NULL;
332 GSList *llast = NULL;
335 if (rfirst) *rfirst = 0;
336 if (rlast) *rlast = 0;
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);
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);
350 /* calculate getting overview range */
352 log_warning(_("invalid article range: %d - %d\n"),
356 if (cache_last < first)
358 else if (last < cache_last)
360 else if (last == cache_last) {
361 debug_print(_("no new articles.\n"));
364 begin = cache_last + 1;
367 if (prefs_common.max_articles > 0 &&
368 end - begin + 1 > prefs_common.max_articles)
369 begin = end - prefs_common.max_articles + 1;
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"));
379 if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
380 log_warning(_("error occurred while getting xover.\n"));
384 if (buf[0] == '.' && buf[1] == '\r') break;
386 msginfo = news_parse_xover(buf);
388 log_warning(_("invalid xover line: %s\n"), buf);
392 msginfo->folder = item;
393 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
396 llast = newlist = g_slist_append(newlist, msginfo);
398 llast = g_slist_append(llast, msginfo);
403 if (rfirst) *rfirst = first;
404 if (rlast) *rlast = last;
408 #define PARSE_ONE_PARAM(p, srcp) \
410 p = strchr(srcp, '\t'); \
411 if (!p) return NULL; \
416 static MsgInfo *news_parse_xover(const gchar *xover_str)
419 gchar buf[NNTPBUFSIZE];
420 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
422 gint num, size_int, line_int;
425 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
426 strcpy(xover_buf, xover_str);
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);
436 tmp = strchr(line, '\t');
437 if (!tmp) tmp = strchr(line, '\r');
438 if (!tmp) tmp = strchr(line, '\n');
439 if (tmp) *tmp = '\0';
441 num = atoi(xover_str);
442 size_int = atoi(size);
443 line_int = atoi(line);
446 msginfo = g_new0(MsgInfo, 1);
447 msginfo->msgnum = num;
448 msginfo->size = size_int;
450 msginfo->date = g_strdup(date);
451 msginfo->date_t = procheader_date_parse(NULL, date, 0);
453 conv_unmime_header(buf, sizeof(buf), sender, NULL);
454 msginfo->from = g_strdup(buf);
455 msginfo->fromname = procheader_get_fromname(buf);
457 conv_unmime_header(buf, sizeof(buf), subject, NULL);
458 msginfo->subject = g_strdup(buf);
460 extract_parenthesis(msgid, '<', '>');
463 msginfo->msgid = g_strdup(msgid);
465 eliminate_parenthesis(ref, '(', ')');
466 if ((p = strrchr(ref, '<')) != NULL) {
467 extract_parenthesis(p, '<', '>');
470 msginfo->inreplyto = g_strdup(p);
476 static GSList *news_delete_old_article(GSList *alist, gint first)
482 if (first < 2) return alist;
484 for (cur = alist; cur != NULL; ) {
487 msginfo = (MsgInfo *)cur->data;
488 if (msginfo && msginfo->msgnum < first) {
489 debug_print(_("deleting article %d...\n"),
492 cache_file = procmsg_get_message_file_path(msginfo);
493 if (is_file_exist(cache_file)) unlink(cache_file);
496 procmsg_msginfo_free(msginfo);
497 alist = g_slist_remove(alist, msginfo);
506 static void news_delete_all_article(FolderItem *item)
513 dir = folder_item_get_path(item);
514 if ((dp = opendir(dir)) == NULL) {
515 FILE_OP_ERROR(dir, "opendir");
520 debug_print(_("\tDeleting all cached articles... "));
522 while ((d = readdir(dp)) != NULL) {
523 if (to_number(d->d_name) < 0) continue;
525 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
527 if (is_file_exist(file)) {
528 if (unlink(file) < 0)
529 FILE_OP_ERROR(file, "unlink");
538 debug_print(_("done.\n"));
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.
550 GSList * news_get_group_list(FolderItem *item)
552 gchar *path, *filename;
554 NNTPSession *session;
555 GSList * group_list = NULL;
557 gchar buf[NNTPBUFSIZE];
563 path = folder_item_get_path(item);
565 if (!is_dir_exist(path))
568 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
571 session = news_session_get(item->folder);
576 if (is_file_exist(filename)) {
577 debug_print(_("group list has been already cached.\n"));
580 ok = nntp_list(session->nntp_sock);
581 if (ok != NN_SUCCESS)
584 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
585 log_warning(_("can't retrieve group list\n"));
590 f = fopen(filename, "r");
591 while (fgets(buf, NNTPBUFSIZE, f)) {
595 while ((buf[len] != 0) && (buf[len] != ' '))
600 group_list = g_slist_append(group_list, s);
605 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
611 remove the cache file of the names of the newsgroups.
614 void news_reset_group_list(FolderItem *item)
616 gchar *path, *filename;
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);
622 if (remove(filename) != 0)
623 log_warning(_("can't delete cached group list %s\n"), filename);