claws-mail/src/news.c

1006 lines
23 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2002-01-16 12:48:36 +01:00
* Copyright (C) 1999-2002 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
2001-05-07 08:47:27 +02:00
#include "defs.h"
2001-04-19 14:21:46 +02:00
#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <time.h>
2001-04-19 14:21:46 +02:00
#include "intl.h"
#include "news.h"
#include "nntp.h"
#include "socket.h"
#include "recv.h"
#include "procmsg.h"
#include "procheader.h"
#include "folder.h"
#include "session.h"
#include "statusbar.h"
#include "codeconv.h"
#include "utils.h"
#include "prefs_common.h"
2001-04-26 18:41:29 +02:00
#include "prefs_account.h"
#include "inputdialog.h"
#include "alertpanel.h"
2002-05-31 09:06:28 +02:00
#if USE_SSL
# include "ssl.h"
#endif
2001-04-19 14:21:46 +02:00
2001-06-06 15:05:23 +02:00
#define NNTP_PORT 119
2002-03-25 10:37:38 +01:00
#if USE_SSL
2002-05-31 09:06:28 +02:00
#define NNTPS_PORT 563
2002-03-25 10:37:38 +01:00
#endif
2001-06-06 15:05:23 +02:00
2002-01-16 12:48:36 +01:00
static void news_folder_init (Folder *folder,
const gchar *name,
const gchar *path);
2002-03-25 10:37:38 +01:00
#if USE_SSL
static Session *news_session_new (const gchar *server,
gushort port,
const gchar *userid,
2002-05-31 09:06:28 +02:00
const gchar *passwd,
SSLType ssl_type);
2002-03-25 10:37:38 +01:00
#else
2001-05-22 14:08:39 +02:00
static Session *news_session_new (const gchar *server,
gushort port,
const gchar *userid,
const gchar *passwd);
2002-03-25 10:37:38 +01:00
#endif
2001-05-22 14:08:39 +02:00
static gint news_get_article_cmd (NNTPSession *session,
2001-04-19 14:21:46 +02:00
const gchar *cmd,
gint num,
gchar *filename);
static gint news_get_article (NNTPSession *session,
2001-04-19 14:21:46 +02:00
gint num,
gchar *filename);
static gint news_get_header (NNTPSession *session,
2001-04-19 14:21:46 +02:00
gint num,
gchar *filename);
static gint news_select_group (NNTPSession *session,
2002-02-21 15:56:09 +01:00
const gchar *group,
gint *num,
gint *first,
gint *last);
2001-04-19 14:21:46 +02:00
static GSList *news_get_uncached_articles(NNTPSession *session,
FolderItem *item,
gint cache_last,
gint *rfirst,
gint *rlast);
static MsgInfo *news_parse_xover (const gchar *xover_str);
2001-05-22 14:08:39 +02:00
static gchar *news_parse_xhdr (const gchar *xhdr_str,
MsgInfo *msginfo);
2001-06-19 13:24:32 +02:00
static GSList *news_delete_old_articles (GSList *alist,
FolderItem *item,
2001-04-19 14:21:46 +02:00
gint first);
2001-06-19 13:24:32 +02:00
static void news_delete_all_articles (FolderItem *item);
2001-04-19 14:21:46 +02:00
2002-01-27 15:32:18 +01:00
static gint news_remove_msg (Folder *folder,
FolderItem *item,
gint num);
2001-04-19 14:21:46 +02:00
2002-01-16 12:48:36 +01:00
Folder *news_folder_new(const gchar *name, const gchar *path)
{
Folder *folder;
folder = (Folder *)g_new0(NewsFolder, 1);
news_folder_init(folder, name, path);
return folder;
}
void news_folder_destroy(NewsFolder *folder)
{
folder_remote_folder_destroy(REMOTE_FOLDER(folder));
}
static void news_folder_init(Folder *folder, const gchar *name,
const gchar *path)
{
folder_remote_folder_init(folder, name, path);
folder->type = F_NEWS;
folder->get_msg_list = news_get_article_list;
folder->fetch_msg = news_fetch_msg;
folder->scan = news_scan_group;
2002-01-27 15:32:18 +01:00
folder->remove_msg = news_remove_msg;
2002-01-16 12:48:36 +01:00
}
2002-03-25 10:37:38 +01:00
#if USE_SSL
static Session *news_session_new(const gchar *server, gushort port,
2002-05-31 09:06:28 +02:00
const gchar *userid, const gchar *passwd,
SSLType ssl_type)
2002-03-25 10:37:38 +01:00
#else
2001-04-30 16:58:37 +02:00
static Session *news_session_new(const gchar *server, gushort port,
const gchar *userid, const gchar *passwd)
2002-03-25 10:37:38 +01:00
#endif
2001-04-19 14:21:46 +02:00
{
gchar buf[NNTPBUFSIZE];
NNTPSession *session;
2001-04-30 16:58:37 +02:00
NNTPSockInfo *nntp_sock;
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(server != NULL, NULL);
log_message(_("creating NNTP connection to %s:%d ...\n"), server, port);
2002-03-25 10:37:38 +01:00
#if USE_SSL
2002-05-31 09:06:28 +02:00
if (userid && passwd)
nntp_sock = nntp_open_auth(server, port, buf, userid, passwd,
ssl_type);
2001-04-30 16:58:37 +02:00
else
2002-05-29 10:28:56 +02:00
nntp_sock = nntp_open(server, port, buf, ssl_type);
2002-03-25 10:37:38 +01:00
#else
2002-05-31 09:06:28 +02:00
if (userid && passwd)
nntp_sock = nntp_open_auth(server, port, buf, userid, passwd);
else
2001-04-30 16:58:37 +02:00
nntp_sock = nntp_open(server, port, buf);
2002-03-25 10:37:38 +01:00
#endif
if (nntp_sock == NULL)
2001-04-19 14:21:46 +02:00
return NULL;
session = g_new(NNTPSession, 1);
SESSION(session)->type = SESSION_NEWS;
SESSION(session)->server = g_strdup(server);
session->nntp_sock = nntp_sock;
SESSION(session)->sock = nntp_sock->sock;
SESSION(session)->connected = TRUE;
SESSION(session)->phase = SESSION_READY;
SESSION(session)->last_access_time = time(NULL);
SESSION(session)->data = NULL;
2001-04-19 14:21:46 +02:00
session->group = NULL;
return SESSION(session);
}
void news_session_destroy(NNTPSession *session)
{
2001-04-30 16:58:37 +02:00
nntp_close(session->nntp_sock);
session->nntp_sock = NULL;
2001-04-27 22:27:24 +02:00
SESSION(session)->sock = NULL;
2001-04-19 14:21:46 +02:00
g_free(session->group);
}
2001-04-30 16:58:37 +02:00
static Session *news_session_new_for_folder(Folder *folder)
{
Session *session;
PrefsAccount *ac;
2001-05-22 14:08:39 +02:00
const gchar *userid = NULL;
gchar *passwd = NULL;
2002-05-31 09:06:28 +02:00
gushort port;
2001-04-30 16:58:37 +02:00
2001-07-04 12:14:20 +02:00
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(folder->account != NULL, NULL);
2001-04-30 16:58:37 +02:00
ac = folder->account;
if (ac->use_nntp_auth && ac->userid && ac->userid[0]) {
2001-04-30 16:58:37 +02:00
userid = ac->userid;
if (ac->passwd && ac->passwd[0])
passwd = g_strdup(ac->passwd);
2001-05-22 14:08:39 +02:00
else
2002-01-16 12:48:36 +01:00
passwd = input_dialog_query_password(ac->nntp_server,
userid);
2001-04-30 16:58:37 +02:00
}
2001-05-22 14:08:39 +02:00
2002-03-25 10:37:38 +01:00
#if USE_SSL
2002-05-31 09:06:28 +02:00
port = ac->set_nntpport ? ac->nntpport
: ac->ssl_nntp ? NNTPS_PORT : NNTP_PORT;
session = news_session_new(ac->nntp_server, port, userid, passwd,
ac->ssl_nntp);
2002-03-25 10:37:38 +01:00
#else
2002-05-31 09:06:28 +02:00
port = ac->set_nntpport ? ac->nntpport : NNTP_PORT;
session = news_session_new(ac->nntp_server, port, userid, passwd);
2002-03-25 10:37:38 +01:00
#endif
2002-05-31 09:06:28 +02:00
2001-04-30 16:58:37 +02:00
g_free(passwd);
2001-05-22 14:08:39 +02:00
2001-04-30 16:58:37 +02:00
return session;
}
2001-04-19 14:21:46 +02:00
NNTPSession *news_session_get(Folder *folder)
{
2001-11-16 11:44:43 +01:00
RemoteFolder *rfolder = REMOTE_FOLDER(folder);
2001-04-30 16:58:37 +02:00
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(folder->type == F_NEWS, NULL);
g_return_val_if_fail(folder->account != NULL, NULL);
2001-11-16 11:44:43 +01:00
if (!rfolder->session) {
rfolder->session = news_session_new_for_folder(folder);
2001-07-15 15:42:30 +02:00
statusbar_pop_all();
2001-11-16 11:44:43 +01:00
return NNTP_SESSION(rfolder->session);
2001-06-06 15:05:23 +02:00
}
2001-11-16 11:44:43 +01:00
if (time(NULL) - rfolder->session->last_access_time < SESSION_TIMEOUT) {
rfolder->session->last_access_time = time(NULL);
statusbar_pop_all();
2001-11-16 11:44:43 +01:00
return NNTP_SESSION(rfolder->session);
}
2001-11-16 11:44:43 +01:00
if (nntp_mode(NNTP_SESSION(rfolder->session)->nntp_sock, FALSE)
!= NN_SUCCESS) {
2001-06-06 15:05:23 +02:00
log_warning(_("NNTP connection to %s:%d has been"
" disconnected. Reconnecting...\n"),
2001-07-04 12:14:20 +02:00
folder->account->nntp_server,
folder->account->set_nntpport ?
folder->account->nntpport : NNTP_PORT);
2001-11-16 11:44:43 +01:00
session_destroy(rfolder->session);
rfolder->session = news_session_new_for_folder(folder);
2001-04-19 14:21:46 +02:00
}
2001-11-18 13:23:39 +01:00
if (rfolder->session)
rfolder->session->last_access_time = time(NULL);
2001-07-15 15:42:30 +02:00
statusbar_pop_all();
2001-11-16 11:44:43 +01:00
return NNTP_SESSION(rfolder->session);
2001-04-19 14:21:46 +02:00
}
GSList *news_get_article_list(Folder *folder, FolderItem *item,
gboolean use_cache)
{
GSList *alist;
NNTPSession *session;
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(item != NULL, NULL);
g_return_val_if_fail(folder->type == F_NEWS, NULL);
session = news_session_get(folder);
if (!session) {
alist = procmsg_read_cache(item, FALSE);
item->last_num = procmsg_get_last_num_in_cache(alist);
} else if (use_cache) {
GSList *newlist;
gint cache_last;
gint first, last;
alist = procmsg_read_cache(item, FALSE);
cache_last = procmsg_get_last_num_in_cache(alist);
newlist = news_get_uncached_articles
(session, item, cache_last, &first, &last);
2001-06-19 13:24:32 +02:00
alist = news_delete_old_articles(alist, item, first);
2001-04-19 14:21:46 +02:00
alist = g_slist_concat(alist, newlist);
item->last_num = last;
} else {
gint last;
alist = news_get_uncached_articles
(session, item, 0, NULL, &last);
2001-06-19 13:24:32 +02:00
news_delete_all_articles(item);
2001-04-19 14:21:46 +02:00
item->last_num = last;
}
procmsg_set_flags(alist, item);
statusbar_pop_all();
return alist;
}
gchar *news_fetch_msg(Folder *folder, FolderItem *item, gint num)
{
gchar *path, *filename;
2001-07-15 15:42:30 +02:00
NNTPSession *session;
2001-04-19 14:21:46 +02:00
gint ok;
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(item != NULL, NULL);
path = folder_item_get_path(item);
if (!is_dir_exist(path))
make_dir_hier(path);
2001-04-19 14:21:46 +02:00
filename = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
g_free(path);
if (is_file_exist(filename)) {
debug_print(_("article %d has been already cached.\n"), num);
return filename;
}
2001-07-15 15:42:30 +02:00
session = news_session_get(folder);
if (!session) {
2001-04-19 14:21:46 +02:00
g_free(filename);
return NULL;
}
2002-02-21 15:56:09 +01:00
ok = news_select_group(session, item->path, NULL, NULL, NULL);
2001-07-15 15:42:30 +02:00
statusbar_pop_all();
if (ok != NN_SUCCESS) {
g_warning(_("can't select group %s\n"), item->path);
g_free(filename);
return NULL;
}
2001-04-19 14:21:46 +02:00
debug_print(_("getting article %d...\n"), num);
ok = news_get_article(NNTP_SESSION(REMOTE_FOLDER(folder)->session),
2001-04-19 14:21:46 +02:00
num, filename);
statusbar_pop_all();
if (ok < 0) {
g_warning(_("can't read article %d\n"), num);
g_free(filename);
return NULL;
}
return filename;
}
gint news_scan_group(Folder *folder, FolderItem *item)
2001-04-19 14:21:46 +02:00
{
2002-02-21 15:56:09 +01:00
NNTPSession *session;
gint num = 0, first = 0, last = 0;
gint new = 0, unread = 0, total = 0;
gint min = 0, max = 0;
gchar *path;
gint ok;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(item != NULL, -1);
2002-02-21 15:56:09 +01:00
session = news_session_get(folder);
if (!session) return -1;
2002-02-21 15:56:09 +01:00
ok = news_select_group(session, item->path, &num, &first, &last);
if (ok != NN_SUCCESS) {
log_warning(_("can't set group: %s\n"), item->path);
return -1;
2002-02-21 15:56:09 +01:00
}
if (num == 0) {
item->new = item->unread = item->total = item->last_num = 0;
return 0;
2002-02-21 15:56:09 +01:00
}
path = folder_item_get_path(item);
if (path && is_dir_exist(path)) {
procmsg_get_mark_sum(path, &new, &unread, &total, &min, &max,
first);
}
g_free(path);
2002-05-27 15:14:05 +02:00
if (max < first || last < min)
new = unread = total = num;
2002-05-27 15:14:05 +02:00
else {
if (min < first)
min = first;
else if (first < min) {
num -= min - first;
first = min;
}
if (last < max)
max = last;
else if (max < last) {
new += last - max;
unread += last - max;
}
2002-02-21 15:56:09 +01:00
if (new > num) new = num;
if (unread > num) unread = num;
}
2002-05-27 15:14:05 +02:00
2002-02-21 15:56:09 +01:00
item->new = new;
item->unread = unread;
item->total = num;
item->last_num = last;
return 0;
2001-04-19 14:21:46 +02:00
}
2001-11-16 11:44:43 +01:00
static NewsGroupInfo *news_group_info_new(const gchar *name,
gint first, gint last, gchar type)
2001-10-18 20:51:14 +02:00
{
2001-11-16 11:44:43 +01:00
NewsGroupInfo *ginfo;
2001-10-18 20:51:14 +02:00
2001-11-16 11:44:43 +01:00
ginfo = g_new(NewsGroupInfo, 1);
ginfo->name = g_strdup(name);
ginfo->first = first;
ginfo->last = last;
ginfo->type = type;
2001-10-18 20:51:14 +02:00
2001-11-16 11:44:43 +01:00
return ginfo;
2001-10-18 20:51:14 +02:00
}
2001-11-16 11:44:43 +01:00
static void news_group_info_free(NewsGroupInfo *ginfo)
2001-10-18 20:51:14 +02:00
{
2001-11-16 11:44:43 +01:00
g_free(ginfo->name);
g_free(ginfo);
2001-10-18 20:51:14 +02:00
}
2001-11-16 11:44:43 +01:00
static gint news_group_info_compare(NewsGroupInfo *ginfo1,
NewsGroupInfo *ginfo2)
2001-10-18 20:51:14 +02:00
{
2001-11-16 11:44:43 +01:00
return g_strcasecmp(ginfo1->name, ginfo2->name);
2001-10-18 20:51:14 +02:00
}
2001-07-15 15:42:30 +02:00
GSList *news_get_group_list(Folder *folder)
{
gchar *path, *filename;
FILE *fp;
GSList *list = NULL;
GSList *last = NULL;
gchar buf[NNTPBUFSIZE];
g_return_val_if_fail(folder != NULL, NULL);
g_return_val_if_fail(folder->type == F_NEWS, NULL);
path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
2001-07-25 16:49:48 +02:00
if (!is_dir_exist(path))
make_dir_hier(path);
2001-07-15 15:42:30 +02:00
filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
g_free(path);
2002-03-14 11:17:32 +01:00
if ((fp = fopen(filename, "rb")) == NULL) {
2001-11-16 11:44:43 +01:00
NNTPSession *session;
2001-07-15 15:42:30 +02:00
2001-11-16 11:44:43 +01:00
session = news_session_get(folder);
if (!session) {
g_free(filename);
return NULL;
}
2001-07-15 15:42:30 +02:00
if (nntp_list(session->nntp_sock) != NN_SUCCESS) {
g_free(filename);
statusbar_pop_all();
return NULL;
}
statusbar_pop_all();
if (recv_write_to_file(SESSION(session)->sock, filename) < 0) {
log_warning(_("can't retrieve newsgroup list\n"));
2002-02-23 11:52:54 +01:00
session_destroy(SESSION(session));
REMOTE_FOLDER(folder)->session = NULL;
2001-07-15 15:42:30 +02:00
g_free(filename);
return NULL;
}
2002-03-14 11:17:32 +01:00
if ((fp = fopen(filename, "rb")) == NULL) {
2001-07-15 15:42:30 +02:00
FILE_OP_ERROR(filename, "fopen");
g_free(filename);
return NULL;
}
}
while (fgets(buf, sizeof(buf), fp) != NULL) {
2001-11-16 11:44:43 +01:00
gchar *p = buf;
gchar *name;
gint last_num;
gint first_num;
2001-10-18 20:51:14 +02:00
gchar type;
2001-11-16 11:44:43 +01:00
NewsGroupInfo *ginfo;
2001-10-18 20:51:14 +02:00
2001-11-16 11:44:43 +01:00
p = strchr(p, ' ');
if (!p) continue;
*p = '\0';
p++;
name = buf;
2001-10-18 20:51:14 +02:00
2001-11-16 11:44:43 +01:00
if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3)
2001-10-18 20:51:14 +02:00
continue;
2001-11-16 11:44:43 +01:00
ginfo = news_group_info_new(name, first_num, last_num, type);
2001-10-18 20:51:14 +02:00
2001-07-15 15:42:30 +02:00
if (!last)
2001-11-16 11:44:43 +01:00
last = list = g_slist_append(NULL, ginfo);
2001-07-15 15:42:30 +02:00
else {
2001-11-16 11:44:43 +01:00
last = g_slist_append(last, ginfo);
2001-07-15 15:42:30 +02:00
last = last->next;
}
}
fclose(fp);
g_free(filename);
2001-11-16 11:44:43 +01:00
list = g_slist_sort(list, (GCompareFunc)news_group_info_compare);
2001-10-20 14:22:07 +02:00
2001-07-15 15:42:30 +02:00
statusbar_pop_all();
return list;
}
2001-11-16 11:44:43 +01:00
void news_group_list_free(GSList *group_list)
{
GSList *cur;
if (!group_list) return;
for (cur = group_list; cur != NULL; cur = cur->next)
news_group_info_free((NewsGroupInfo *)cur->data);
g_slist_free(group_list);
}
void news_remove_group_list_cache(Folder *folder)
2001-07-15 15:42:30 +02:00
{
gchar *path, *filename;
g_return_if_fail(folder != NULL);
g_return_if_fail(folder->type == F_NEWS);
path = folder_item_get_path(FOLDER_ITEM(folder->node->data));
filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL);
g_free(path);
if (is_file_exist(filename)) {
if (remove(filename) < 0)
FILE_OP_ERROR(filename, "remove");
}
g_free(filename);
}
2001-04-19 14:21:46 +02:00
gint news_post(Folder *folder, const gchar *file)
{
NNTPSession *session;
FILE *fp;
gint ok;
g_return_val_if_fail(folder != NULL, -1);
g_return_val_if_fail(folder->type == F_NEWS, -1);
g_return_val_if_fail(file != NULL, -1);
session = news_session_get(folder);
if (!session) return -1;
2002-03-14 11:17:32 +01:00
if ((fp = fopen(file, "rb")) == NULL) {
2001-04-19 14:21:46 +02:00
FILE_OP_ERROR(file, "fopen");
return -1;
}
2001-04-30 16:58:37 +02:00
ok = nntp_post(session->nntp_sock, fp);
2001-04-19 14:21:46 +02:00
if (ok != NN_SUCCESS) {
log_warning(_("can't post article.\n"));
return -1;
}
fclose(fp);
statusbar_pop_all();
return 0;
}
static gint news_get_article_cmd(NNTPSession *session, const gchar *cmd,
2001-04-19 14:21:46 +02:00
gint num, gchar *filename)
{
gchar *msgid;
2001-04-30 16:58:37 +02:00
if (nntp_get_article(session->nntp_sock, cmd, num, &msgid)
!= NN_SUCCESS)
2001-04-19 14:21:46 +02:00
return -1;
debug_print("Message-Id = %s, num = %d\n", msgid, num);
g_free(msgid);
2001-04-30 16:58:37 +02:00
if (recv_write_to_file(session->nntp_sock->sock, filename) < 0) {
2001-04-19 14:21:46 +02:00
log_warning(_("can't retrieve article %d\n"), num);
return -1;
}
return 0;
}
2002-01-27 15:32:18 +01:00
static gint news_remove_msg(Folder *folder, FolderItem *item, gint num)
{
2002-03-01 21:27:37 +01:00
MsgInfo * msginfo;
gchar * filename;
MsgFlags msgflags = { 0, 0 };
gint r;
filename = folder_item_fetch_msg(item, num);
if (filename == NULL)
return -1;
2002-06-19 09:31:13 +02:00
msginfo = procheader_parse_file(filename, msgflags, FALSE, FALSE);
2002-03-01 21:27:37 +01:00
if (msginfo == NULL)
return -1;
r = news_cancel_article(folder, msginfo);
procmsg_msginfo_free(msginfo);
return r;
2002-01-27 15:32:18 +01:00
}
static gint news_get_article(NNTPSession *session, gint num, gchar *filename)
2001-04-19 14:21:46 +02:00
{
return news_get_article_cmd(session, "ARTICLE", num, filename);
2001-04-19 14:21:46 +02:00
}
static gint news_get_header(NNTPSession *session, gint num, gchar *filename)
2001-04-19 14:21:46 +02:00
{
return news_get_article_cmd(session, "HEAD", num, filename);
}
/**
* news_select_group:
* @session: Active NNTP session.
* @group: Newsgroup name.
2002-02-21 15:56:09 +01:00
* @num: Estimated number of articles.
* @first: First article number.
* @last: Last article number.
2001-05-22 14:08:39 +02:00
*
* Select newsgroup @group with the GROUP command if it is not already
2002-02-21 15:56:09 +01:00
* selected in @session, or article numbers need to be returned.
2001-05-22 14:08:39 +02:00
*
* Return value: NNTP result code.
**/
2002-02-21 15:56:09 +01:00
static gint news_select_group(NNTPSession *session, const gchar *group,
gint *num, gint *first, gint *last)
{
gint ok;
2002-02-21 15:56:09 +01:00
gint num_, first_, last_;
if (!num || !first || !last) {
if (session->group && g_strcasecmp(session->group, group) == 0)
return NN_SUCCESS;
num = &num_;
first = &first_;
last = &last_;
}
g_free(session->group);
session->group = NULL;
2002-02-21 15:56:09 +01:00
ok = nntp_group(session->nntp_sock, group, num, first, last);
if (ok == NN_SUCCESS)
session->group = g_strdup(group);
return ok;
}
2001-04-19 14:21:46 +02:00
static GSList *news_get_uncached_articles(NNTPSession *session,
FolderItem *item, gint cache_last,
gint *rfirst, gint *rlast)
{
gint ok;
gint num = 0, first = 0, last = 0, begin = 0, end = 0;
gchar buf[NNTPBUFSIZE];
GSList *newlist = NULL;
GSList *llast = NULL;
MsgInfo *msginfo;
if (rfirst) *rfirst = 0;
if (rlast) *rlast = 0;
g_return_val_if_fail(session != NULL, NULL);
g_return_val_if_fail(item != NULL, NULL);
g_return_val_if_fail(item->folder != NULL, NULL);
g_return_val_if_fail(item->folder->type == F_NEWS, NULL);
2002-02-21 15:56:09 +01:00
ok = news_select_group(session, item->path, &num, &first, &last);
2001-04-19 14:21:46 +02:00
if (ok != NN_SUCCESS) {
log_warning(_("can't set group: %s\n"), item->path);
return NULL;
}
2001-04-19 14:21:46 +02:00
/* calculate getting overview range */
if (first > last) {
log_warning(_("invalid article range: %d - %d\n"),
first, last);
return NULL;
}
if (cache_last < first)
begin = first;
else if (last < cache_last)
begin = first;
else if (last == cache_last) {
debug_print(_("no new articles.\n"));
return NULL;
} else
begin = cache_last + 1;
end = last;
2001-05-22 14:08:39 +02:00
if (rfirst) *rfirst = first;
if (rlast) *rlast = last;
2001-04-19 14:21:46 +02:00
if (prefs_common.max_articles > 0 &&
end - begin + 1 > prefs_common.max_articles)
begin = end - prefs_common.max_articles + 1;
log_message(_("getting xover %d - %d in %s...\n"),
begin, end, item->path);
2001-04-30 16:58:37 +02:00
if (nntp_xover(session->nntp_sock, begin, end) != NN_SUCCESS) {
2001-04-19 14:21:46 +02:00
log_warning(_("can't get xover\n"));
return NULL;
}
for (;;) {
if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
2001-04-19 14:21:46 +02:00
log_warning(_("error occurred while getting xover.\n"));
return newlist;
}
if (buf[0] == '.' && buf[1] == '\r') break;
msginfo = news_parse_xover(buf);
if (!msginfo) {
log_warning(_("invalid xover line: %s\n"), buf);
continue;
}
msginfo->folder = item;
msginfo->flags.perm_flags = MSG_NEW|MSG_UNREAD;
msginfo->flags.tmp_flags = MSG_NEWS;
2001-05-11 01:00:27 +02:00
msginfo->newsgroups = g_strdup(item->path);
2001-04-19 14:21:46 +02:00
if (!newlist)
llast = newlist = g_slist_append(newlist, msginfo);
else {
llast = g_slist_append(llast, msginfo);
llast = llast->next;
}
}
2001-05-07 00:31:24 +02:00
if (nntp_xhdr(session->nntp_sock, "to", begin, end) != NN_SUCCESS) {
log_warning(_("can't get xhdr\n"));
2001-05-22 14:08:39 +02:00
return newlist;
2001-05-07 00:31:24 +02:00
}
llast = newlist;
for (;;) {
if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
2001-05-22 14:08:39 +02:00
log_warning(_("error occurred while getting xhdr.\n"));
2001-05-07 00:31:24 +02:00
return newlist;
}
if (buf[0] == '.' && buf[1] == '\r') break;
2001-05-22 14:08:39 +02:00
if (!llast) {
g_warning("llast == NULL\n");
continue;
}
2001-05-07 00:31:24 +02:00
2001-05-22 14:08:39 +02:00
msginfo = (MsgInfo *)llast->data;
msginfo->to = news_parse_xhdr(buf, msginfo);
2001-05-07 00:31:24 +02:00
llast = llast->next;
}
if (nntp_xhdr(session->nntp_sock, "cc", begin, end) != NN_SUCCESS) {
log_warning(_("can't get xhdr\n"));
2001-05-22 14:08:39 +02:00
return newlist;
2001-05-07 00:31:24 +02:00
}
llast = newlist;
for (;;) {
if (sock_gets(SESSION(session)->sock, buf, sizeof(buf)) < 0) {
2001-05-22 14:08:39 +02:00
log_warning(_("error occurred while getting xhdr.\n"));
2001-05-07 00:31:24 +02:00
return newlist;
}
if (buf[0] == '.' && buf[1] == '\r') break;
2001-05-22 14:08:39 +02:00
if (!llast) {
g_warning("llast == NULL\n");
continue;
}
2001-05-07 00:31:24 +02:00
2001-05-22 14:08:39 +02:00
msginfo = (MsgInfo *)llast->data;
msginfo->cc = news_parse_xhdr(buf, msginfo);
2001-05-07 00:31:24 +02:00
llast = llast->next;
}
2001-04-19 14:21:46 +02:00
return newlist;
}
#define PARSE_ONE_PARAM(p, srcp) \
{ \
p = strchr(srcp, '\t'); \
if (!p) return NULL; \
else \
*p++ = '\0'; \
}
static MsgInfo *news_parse_xover(const gchar *xover_str)
{
MsgInfo *msginfo;
gchar buf[NNTPBUFSIZE];
2002-03-17 10:50:46 +01:00
gchar *subject, *sender, *size, *line, *date, *msgid, *ref, *tmp, *xref;
2001-04-19 14:21:46 +02:00
gchar *p;
gint num, size_int, line_int;
gchar *xover_buf;
2001-11-07 11:29:45 +01:00
Xstrdup_a(xover_buf, xover_str, return NULL);
2001-04-19 14:21:46 +02:00
PARSE_ONE_PARAM(subject, xover_buf);
PARSE_ONE_PARAM(sender, subject);
PARSE_ONE_PARAM(date, sender);
PARSE_ONE_PARAM(msgid, date);
PARSE_ONE_PARAM(ref, msgid);
PARSE_ONE_PARAM(size, ref);
PARSE_ONE_PARAM(line, size);
2002-03-17 10:50:46 +01:00
PARSE_ONE_PARAM(xref, line);
2001-04-19 14:21:46 +02:00
2002-03-17 10:50:46 +01:00
tmp = strchr(xref, '\t');
2001-04-19 14:21:46 +02:00
if (!tmp) tmp = strchr(line, '\r');
if (!tmp) tmp = strchr(line, '\n');
if (tmp) *tmp = '\0';
num = atoi(xover_str);
size_int = atoi(size);
line_int = atoi(line);
/* set MsgInfo */
msginfo = g_new0(MsgInfo, 1);
msginfo->msgnum = num;
msginfo->size = size_int;
msginfo->date = g_strdup(date);
msginfo->date_t = procheader_date_parse(NULL, date, 0);
conv_unmime_header(buf, sizeof(buf), sender, NULL);
msginfo->from = g_strdup(buf);
msginfo->fromname = procheader_get_fromname(buf);
conv_unmime_header(buf, sizeof(buf), subject, NULL);
msginfo->subject = g_strdup(buf);
extract_parenthesis(msgid, '<', '>');
remove_space(msgid);
if (*msgid != '\0')
msginfo->msgid = g_strdup(msgid);
2001-05-13 04:57:01 +02:00
msginfo->references = g_strdup(ref);
2001-04-19 14:21:46 +02:00
eliminate_parenthesis(ref, '(', ')');
if ((p = strrchr(ref, '<')) != NULL) {
extract_parenthesis(p, '<', '>');
remove_space(p);
if (*p != '\0')
msginfo->inreplyto = g_strdup(p);
}
2002-03-17 10:50:46 +01:00
msginfo->xref = g_strdup(xref);
p = msginfo->xref+strlen(msginfo->xref) - 1;
while (*p == '\r' || *p == '\n') {
*p = '\0';
p--;
}
2001-04-19 14:21:46 +02:00
return msginfo;
}
2001-05-22 14:08:39 +02:00
static gchar *news_parse_xhdr(const gchar *xhdr_str, MsgInfo *msginfo)
2001-05-07 00:31:24 +02:00
{
gchar *p;
2001-05-22 14:08:39 +02:00
gchar *tmp;
2001-05-07 00:31:24 +02:00
gint num;
2001-05-22 14:08:39 +02:00
p = strchr(xhdr_str, ' ');
2001-05-07 00:31:24 +02:00
if (!p)
return NULL;
else
p++;
2001-05-22 14:08:39 +02:00
num = atoi(xhdr_str);
if (msginfo->msgnum != num) return NULL;
2001-05-07 00:31:24 +02:00
tmp = strchr(p, '\r');
if (!tmp) tmp = strchr(p, '\n');
if (tmp)
return g_strndup(p, tmp - p);
else
return g_strdup(p);
2001-05-07 00:31:24 +02:00
}
2001-06-19 13:24:32 +02:00
static GSList *news_delete_old_articles(GSList *alist, FolderItem *item,
gint first)
2001-04-19 14:21:46 +02:00
{
GSList *cur, *next;
MsgInfo *msginfo;
2001-06-19 13:24:32 +02:00
gchar *dir;
g_return_val_if_fail(item != NULL, alist);
g_return_val_if_fail(item->folder != NULL, alist);
g_return_val_if_fail(item->folder->type == F_NEWS, alist);
2001-04-19 14:21:46 +02:00
if (first < 2) return alist;
2001-06-19 13:24:32 +02:00
debug_print(_("Deleting cached articles 1 - %d ... "), first - 1);
dir = folder_item_get_path(item);
remove_numbered_files(dir, 1, first - 1);
g_free(dir);
2001-04-19 14:21:46 +02:00
for (cur = alist; cur != NULL; ) {
next = cur->next;
msginfo = (MsgInfo *)cur->data;
if (msginfo && msginfo->msgnum < first) {
procmsg_msginfo_free(msginfo);
alist = g_slist_remove(alist, msginfo);
}
cur = next;
}
return alist;
}
2001-06-19 13:24:32 +02:00
static void news_delete_all_articles(FolderItem *item)
2001-04-19 14:21:46 +02:00
{
gchar *dir;
2001-06-19 13:24:32 +02:00
g_return_if_fail(item != NULL);
g_return_if_fail(item->folder != NULL);
g_return_if_fail(item->folder->type == F_NEWS);
2001-04-19 14:21:46 +02:00
debug_print(_("\tDeleting all cached articles... "));
2001-06-19 13:24:32 +02:00
dir = folder_item_get_path(item);
remove_all_numbered_files(dir);
2001-04-19 14:21:46 +02:00
g_free(dir);
debug_print(_("done.\n"));
}
2002-03-01 21:27:37 +01:00
gint news_cancel_article(Folder * folder, MsgInfo * msginfo)
{
gchar * tmp;
FILE * tmpfp;
gchar buf[BUFFSIZE];
tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(),
G_DIR_SEPARATOR, (gint)msginfo);
if (tmp == NULL)
return -1;
2002-03-14 11:17:32 +01:00
if ((tmpfp = fopen(tmp, "wb")) == NULL) {
2002-03-01 21:27:37 +01:00
FILE_OP_ERROR(tmp, "fopen");
return -1;
}
if (change_file_mode_rw(tmpfp, tmp) < 0) {
FILE_OP_ERROR(tmp, "chmod");
g_warning(_("can't change file mode\n"));
}
fprintf(tmpfp, "From: %s\r\n", msginfo->from);
fprintf(tmpfp, "Newsgroups: %s\r\n", msginfo->newsgroups);
fprintf(tmpfp, "Subject: cmsg cancel <%s>\r\n", msginfo->msgid);
fprintf(tmpfp, "Control: cancel <%s>\r\n", msginfo->msgid);
fprintf(tmpfp, "Approved: %s\r\n", msginfo->from);
fprintf(tmpfp, "X-Cancelled-by: %s\r\n", msginfo->from);
get_rfc822_date(buf, sizeof(buf));
fprintf(tmpfp, "Date: %s\r\n", buf);
fprintf(tmpfp, "\r\n");
fprintf(tmpfp, "removed with sylpheed\r\n");
fclose(tmpfp);
news_post(folder, tmp);
remove(tmp);
g_free(tmp);
return 0;
}