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"
46 static gint news_get_article_cmd (NNTPSession *session,
50 static gint news_get_article (NNTPSession *session,
53 static gint news_get_header (NNTPSession *session,
57 static GSList *news_get_uncached_articles(NNTPSession *session,
62 static MsgInfo *news_parse_xover (const gchar *xover_str);
63 static GSList *news_delete_old_article (GSList *alist,
65 static void news_delete_all_article (FolderItem *item);
68 Session *news_session_new(const gchar *server, gushort port)
70 gchar buf[NNTPBUFSIZE];
74 g_return_val_if_fail(server != NULL, NULL);
76 log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
78 if ((nntp_sock = nntp_open(server, port, buf)) < 0)
81 session = g_new(NNTPSession, 1);
82 SESSION(session)->type = SESSION_NEWS;
83 SESSION(session)->server = g_strdup(server);
84 SESSION(session)->sock = nntp_sock;
85 SESSION(session)->connected = TRUE;
86 SESSION(session)->phase = SESSION_READY;
87 SESSION(session)->data = NULL;
88 session->group = NULL;
90 return SESSION(session);
93 void news_session_destroy(NNTPSession *session)
95 close(SESSION(session)->sock);
97 g_free(session->group);
100 NNTPSession *news_session_get(Folder *folder)
102 g_return_val_if_fail(folder != NULL, NULL);
103 g_return_val_if_fail(folder->type == F_NEWS, NULL);
104 g_return_val_if_fail(folder->account != NULL, NULL);
106 if (!REMOTE_FOLDER(folder)->session) {
107 REMOTE_FOLDER(folder)->session =
108 news_session_new(folder->account->nntp_server, 119);
110 if (nntp_mode(REMOTE_FOLDER(folder)->session->sock, FALSE)
112 log_warning(_("NNTP connection to %s:%d has been"
113 " disconnected. Reconnecting...\n"),
114 folder->account->nntp_server, 119);
115 session_destroy(REMOTE_FOLDER(folder)->session);
116 REMOTE_FOLDER(folder)->session =
117 news_session_new(folder->account->nntp_server,
122 return NNTP_SESSION(REMOTE_FOLDER(folder)->session);
125 GSList *news_get_article_list(Folder *folder, FolderItem *item,
129 NNTPSession *session;
131 g_return_val_if_fail(folder != NULL, NULL);
132 g_return_val_if_fail(item != NULL, NULL);
133 g_return_val_if_fail(folder->type == F_NEWS, NULL);
135 session = news_session_get(folder);
138 alist = procmsg_read_cache(item, FALSE);
139 item->last_num = procmsg_get_last_num_in_cache(alist);
140 } else if (use_cache) {
145 alist = procmsg_read_cache(item, FALSE);
147 cache_last = procmsg_get_last_num_in_cache(alist);
148 newlist = news_get_uncached_articles
149 (session, item, cache_last, &first, &last);
150 alist = news_delete_old_article(alist, first);
152 alist = g_slist_concat(alist, newlist);
153 item->last_num = last;
157 alist = news_get_uncached_articles
158 (session, item, 0, NULL, &last);
159 news_delete_all_article(item);
160 item->last_num = last;
163 procmsg_set_flags(alist, item);
170 gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
172 gchar *path, *filename;
175 g_return_val_if_fail(folder != NULL, NULL);
176 g_return_val_if_fail(item != NULL, NULL);
178 path = folder_item_get_path(item);
179 filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
182 if (is_file_exist(filename)) {
183 debug_print(_("article %d has been already cached.\n"), num);
187 if (!REMOTE_FOLDER(folder)->session) {
192 debug_print(_("getting article %d...\n"), num);
193 ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
197 g_warning(_("can't read article %d\n"), num);
205 void news_scan_group(Folder *folder, FolderItem *item)
209 gint news_post(Folder *folder, const gchar *file)
211 NNTPSession *session;
215 g_return_val_if_fail(folder != NULL, -1);
216 g_return_val_if_fail(folder->type == F_NEWS, -1);
217 g_return_val_if_fail(file != NULL, -1);
219 session = news_session_get(folder);
220 if (!session) return -1;
222 if ((fp = fopen(file, "r")) == NULL) {
223 FILE_OP_ERROR(file, "fopen");
227 ok = nntp_post(SESSION(session)->sock, fp);
228 if (ok != NN_SUCCESS) {
229 log_warning(_("can't post article.\n"));
240 static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
241 gint num, gchar *filename)
245 if (nntp_get_article(SESSION(session)->sock, cmd, num, &msgid)
249 debug_print("Message-Id = %s, num = %d\n", msgid, num);
252 if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
253 log_warning(_("can't retrieve article %d\n"), num);
260 static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
262 return news_get_article_cmd(session, "ARTICLE", num, filename);
265 static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
267 return news_get_article_cmd(session, "HEAD", num, filename);
270 static GSList *news_get_uncached_articles(NNTPSession *session,
271 FolderItem *item, gint cache_last,
272 gint *rfirst, gint *rlast)
275 gint num = 0, first = 0, last = 0, begin = 0, end = 0;
276 gchar buf[NNTPBUFSIZE];
277 GSList *newlist = NULL;
278 GSList *llast = NULL;
281 if (rfirst) *rfirst = 0;
282 if (rlast) *rlast = 0;
284 g_return_val_if_fail(session != NULL, NULL);
285 g_return_val_if_fail(item != NULL, NULL);
286 g_return_val_if_fail(item->folder != NULL, NULL);
287 g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
289 ok = nntp_group(SESSION(session)->sock, item->path,
290 &num, &first, &last);
291 if (ok != NN_SUCCESS) {
292 log_warning(_("can't set group: %s\n"), item->path);
296 /* calculate getting overview range */
298 log_warning(_("invalid article range: %d - %d\n"),
302 if (cache_last < first)
304 else if (last < cache_last)
306 else if (last == cache_last) {
307 debug_print(_("no new articles.\n"));
310 begin = cache_last + 1;
313 if (prefs_common.max_articles > 0 &&
314 end - begin + 1 > prefs_common.max_articles)
315 begin = end - prefs_common.max_articles + 1;
317 log_message(_("getting xover %d - %d in %s...\n"),
318 begin, end, item->path);
319 if (nntp_xover(SESSION(session)->sock, begin, end) != NN_SUCCESS) {
320 log_warning(_("can't get xover\n"));
325 if (sock_read(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
326 log_warning(_("error occurred while getting xover.\n"));
330 if (buf[0] == '.' && buf[1] == '\r') break;
332 msginfo = news_parse_xover(buf);
334 log_warning(_("invalid xover line: %s\n"), buf);
338 msginfo->folder = item;
339 msginfo->flags = MSG_NEW|MSG_UNREAD|MSG_NEWS;
342 llast = newlist = g_slist_append(newlist, msginfo);
344 llast = g_slist_append(llast, msginfo);
349 if (rfirst) *rfirst = first;
350 if (rlast) *rlast = last;
354 #define PARSE_ONE_PARAM(p, srcp) \
356 p = strchr(srcp, '\t'); \
357 if (!p) return NULL; \
362 static MsgInfo *news_parse_xover(const gchar *xover_str)
365 gchar buf[NNTPBUFSIZE];
366 gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp;
368 gint num, size_int, line_int;
371 Xalloca(xover_buf, strlen(xover_str) + 1, return NULL);
372 strcpy(xover_buf, xover_str);
374 PARSE_ONE_PARAM(subject, xover_buf);
375 PARSE_ONE_PARAM(sender, subject);
376 PARSE_ONE_PARAM(date, sender);
377 PARSE_ONE_PARAM(msgid, date);
378 PARSE_ONE_PARAM(ref, msgid);
379 PARSE_ONE_PARAM(size, ref);
380 PARSE_ONE_PARAM(line, size);
382 tmp = strchr(line, '\t');
383 if (!tmp) tmp = strchr(line, '\r');
384 if (!tmp) tmp = strchr(line, '\n');
385 if (tmp) *tmp = '\0';
387 num = atoi(xover_str);
388 size_int = atoi(size);
389 line_int = atoi(line);
392 msginfo = g_new0(MsgInfo, 1);
393 msginfo->msgnum = num;
394 msginfo->size = size_int;
396 msginfo->date = g_strdup(date);
397 msginfo->date_t = procheader_date_parse(NULL, date, 0);
399 conv_unmime_header(buf, sizeof(buf), sender, NULL);
400 msginfo->from = g_strdup(buf);
401 msginfo->fromname = procheader_get_fromname(buf);
403 conv_unmime_header(buf, sizeof(buf), subject, NULL);
404 msginfo->subject = g_strdup(buf);
406 extract_parenthesis(msgid, '<', '>');
409 msginfo->msgid = g_strdup(msgid);
411 eliminate_parenthesis(ref, '(', ')');
412 if ((p = strrchr(ref, '<')) != NULL) {
413 extract_parenthesis(p, '<', '>');
416 msginfo->inreplyto = g_strdup(p);
422 static GSList *news_delete_old_article(GSList *alist, gint first)
428 if (first < 2) return alist;
430 for (cur = alist; cur != NULL; ) {
433 msginfo = (MsgInfo *)cur->data;
434 if (msginfo && msginfo->msgnum < first) {
435 debug_print(_("deleting article %d...\n"),
438 cache_file = procmsg_get_message_file_path(msginfo);
439 if (is_file_exist(cache_file)) unlink(cache_file);
442 procmsg_msginfo_free(msginfo);
443 alist = g_slist_remove(alist, msginfo);
452 static void news_delete_all_article(FolderItem *item)
459 dir = folder_item_get_path(item);
460 if ((dp = opendir(dir)) == NULL) {
461 FILE_OP_ERROR(dir, "opendir");
466 debug_print(_("\tDeleting all cached articles... "));
468 while ((d = readdir(dp)) != NULL) {
469 if (to_number(d->d_name) < 0) continue;
471 file = g_strconcat(dir, G_DIR_SEPARATOR_S, d->d_name, NULL);
473 if (is_file_exist(file)) {
474 if (unlink(file) < 0)
475 FILE_OP_ERROR(file, "unlink");
484 debug_print(_("done.\n"));
487 GSList * news_get_group_list(FolderItem *item)
489 gchar *path, *filename;
491 NNTPSession *session;
492 GSList * group_list = NULL;
494 gchar buf[NNTPBUFSIZE];
500 path = folder_item_get_path(item);
502 if (!is_dir_exist(path))
505 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
508 session = news_session_get(item->folder);
513 if (is_file_exist(filename)) {
514 debug_print(_("group list has been already cached.\n"));
518 ok = nntp_list(SESSION(session)->sock);
519 if (ok != NN_SUCCESS)
522 if (recv_write_to_file(SESSION(session)->sock, filename)
524 log_warning(_("can't retrieve group list\n"));
529 f = fopen(filename, "r");
530 while (fgets(buf, NNTPBUFSIZE, f))
535 while ((buf[len] != 0) && (buf[len] != ' '))
540 group_list = g_slist_append(group_list, s);
545 group_list = g_slist_sort(group_list, (GCompareFunc) g_strcasecmp);
550 void news_reset_group_list(FolderItem *item)
552 gchar *path, *filename;
554 debug_print(_("\tDeleting cached group list... "));
555 path = folder_item_get_path(item);
556 filename = g_strconcat(path, G_DIR_SEPARATOR_S, GROUPLIST_FILE, NULL);
558 if (remove(filename) != 0)
559 log_warning(_("can't delete cached group list %s\n"), filename);