X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=nntp.c;h=d3bf60f15bfcb4f38722abd7b54b5cd2e493f3ac;hp=604b5d1eec1f642ff8cb8833c60ef620bcebbc44;hb=2dd75819b174c796ebd7e93602a36305b5346c8f;hpb=da04d632587a221112a0f065e4802e1ea5393e21 diff --git a/nntp.c b/nntp.c index 604b5d1..d3bf60f 100644 --- a/nntp.c +++ b/nntp.c @@ -27,7 +27,11 @@ #define NNTP_PORT 119 #define NNTP_SSL_PORT 563 -static unsigned int _checked = 0; +static struct { + unsigned use_cache : 1; + + int checked; +} nntp = { true, 0 }; static int nntp_check_newgroups (nntp_server_t *, int); @@ -43,11 +47,12 @@ static void mutt_newsgroup_stat(nntp_data_t *data) for (int i = 0; i < data->num; i++) { int first = MAX(data->entries[i].first, data->firstMessage); int last = MIN(data->entries[i].last, data->lastMessage); - data->unread -= MAX(0, last - first + 1); + if (first <= last) + data->unread -= last - first + 1; } } -static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line) +static int nntp_parse_newsrc_line(nntp_server_t *news, const char *line) { nntp_data_t *data; char group[LONG_STRING]; @@ -72,7 +77,7 @@ static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line) if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) hash_resize (news->newsgroups, news->newsgroups->nelem * 2); hash_insert(news->newsgroups, data->group, data); - nntp_data_list_append(&news->list, data); + news->tail = nntp_data_list_append(news->tail, data); } else { p_delete(&data->entries); } @@ -81,18 +86,19 @@ static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line) data->entries = p_new(NEWSRC_ENTRY, x * 2); data->max = x * 2; data->subscribed = (*p++ == ':'); + p = skipspaces(p); for (x = 0; *p; p++) { - p = skipspaces(p); data->entries[x].first = strtol(p, (char **)&p, 10); - p = skipspaces(p); + p += strcspn(p, "-,"); if (*p == '-') { + if (!*p) + break; data->entries[x].last = strtol(p + 1, (char **)&p, 10); } else { data->entries[x].last = data->entries[x].first; + p = strchrnul(p, ','); } - - p = strchrnul(p, ','); x += data->entries[x].last != 0; } @@ -143,69 +149,79 @@ static int slurp_newsrc (nntp_server_t * news) * and newsgroup cache names */ static int nntp_parse_cacheindex(nntp_server_t *news) { - nntp_data_t *data; - char file[_POSIX_PATH_MAX], buf[HUGE_STRING], *cp; - FILE *idx; - int l, m, t; + char buf[HUGE_STRING]; + FILE *idx; + + p_delete(&news->cache); + if (m_strisempty(mod_core.cachedir)) + return 0; + + nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host); + if (!(idx = safe_fopen(buf, "a+"))) + return 0; + rewind(idx); + + while (fgets(buf, sizeof(buf), idx)) { + buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */ + if (!m_strncmp(buf, "#: ", 3) + && !m_strcasecmp(buf + 3, news->conn->account.host)) { + break; + } + } - if (m_strisempty(mod_core.cachedir)) { - unset_option(OPTNEWSCACHE); - return 0; - } else { - set_option(OPTNEWSCACHE); - } + while (fgets(buf, sizeof(buf), idx)) { + char *p, *q; + int l, m, t; - p_delete(&news->cache); - nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host); - if (!(idx = safe_fopen (buf, "a+"))) - return 0; - rewind (idx); - while (fgets (buf, sizeof (buf), idx)) { - buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */ - if (!m_strncmp(buf, "#: ", 3) && - !m_strcasecmp(buf + 3, news->conn->account.host)) - break; - } - while (fgets (buf, sizeof (buf), idx)) { - cp = buf; - while (*cp && *cp != ' ') - cp++; - if (!*cp) - continue; - cp[0] = 0; - if (!m_strcmp(buf, "#:")) - break; - sscanf (cp + 1, "%s %d %d", file, &l, &m); - if (!m_strcmp(buf, "ALL")) { - news->cache = m_strdup(file); - news->newgroups_time = m; - } - else if (news->newsgroups) { - if ((data = hash_find (news->newsgroups, buf)) == NULL) { - data = nntp_data_new(); - data->group = m_strdup(buf); - data->nserv = news; - data->deleted = 1; - if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) - hash_resize (news->newsgroups, news->newsgroups->nelem * 2); - hash_insert (news->newsgroups, data->group, data); - nntp_data_list_append(&news->list, data); - } - data->cache = m_strdup(file); - t = 0; - if (!data->firstMessage || data->lastMessage < m) - t = 1; - if (!data->firstMessage) - data->firstMessage = l; - if (data->lastMessage < m) - data->lastMessage = m; - data->lastCached = m; - if (t || !data->unread) - mutt_newsgroup_stat (data); + if (m_strstart(buf, "#:", NULL)) + break; + + p = strchrnul(buf, ' '); + if (!*p) + continue; + *p++ = '\0'; + p = vskipspaces(p); + q = strchrnul(p, ' '); + if (!*q) + continue; + *q++ = '\0'; + l = strtol(q, &q, 10); + m = strtol(q, &q, 10); + + if (!m_strcmp(buf, "ALL")) { + m_strreplace(&news->cache, m_strdup(p)); + news->newgroups_time = m; + continue; + } + + if (news->newsgroups) { + nntp_data_t *data = hash_find(news->newsgroups, buf); + + if (!data) { + data = nntp_data_new(); + data->group = m_strdup(buf); + data->nserv = news; + data->deleted = 1; + if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) + hash_resize (news->newsgroups, news->newsgroups->nelem * 2); + hash_insert (news->newsgroups, data->group, data); + news->tail = nntp_data_list_append(news->tail, data); + } + m_strreplace(&data->cache, p); + + t = !data->firstMessage || data->lastMessage < m; + if (!data->firstMessage) + data->firstMessage = l; + if (data->lastMessage < m) + data->lastMessage = m; + data->lastCached = m; + if (t || !data->unread) + mutt_newsgroup_stat(data); + } } - } - m_fclose(&idx); - return 0; + + m_fclose(&idx); + return 0; } /* nntp_parse_url: given an NNPT URL, return host, port, @@ -260,8 +276,8 @@ static int add_group (char *buf, void *serv) nntp_data_t *nntp_data; static int n = 0; - _checked = n; /* _checked have N, where N = number of groups */ - if (!buf) /* at EOF must be zerouth */ + nntp.checked = n; /* nntp.checked have N, where N = number of groups */ + if (!buf) /* at EOF must be zerouth */ n = 0; if (!s || !buf) @@ -279,7 +295,7 @@ static int add_group (char *buf, void *serv) if (s->newsgroups->nelem < s->newsgroups->curnelem * 2) hash_resize (s->newsgroups, s->newsgroups->nelem * 2); hash_insert(s->newsgroups, nntp_data->group, nntp_data); - nntp_data_list_append(&s->list, nntp_data); + s->tail = nntp_data_list_append(s->tail, nntp_data); } nntp_data->deleted = 0; nntp_data->firstMessage = first; @@ -389,7 +405,6 @@ nntp_server_t *mutt_select_newsserver (char *server) list->subscribed = list->rc = list->num = 0; } slurp_newsrc (serv); - nntp_clear_cacheindex (serv); } if (serv->status == NNTP_BYE) @@ -400,12 +415,13 @@ nntp_server_t *mutt_select_newsserver (char *server) /* New newsserver */ serv = p_new(nntp_server_t, 1); + serv->tail = &serv->list; serv->conn = conn; serv->newsrc = m_strdup(file); - serv->newsgroups = hash_new(1009, false); + serv->newsgroups = hash_new(SHRT_MAX, false); slurp_newsrc (serv); /* load .newsrc */ nntp_parse_cacheindex (serv); /* load .index */ - if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0) + if (nntp.use_cache && serv->cache && nntp_get_cache_all (serv) >= 0) nntp_check_newgroups (serv, 1); else if (nntp_get_active (serv) < 0) { hash_delete(&serv->newsgroups, NULL); @@ -415,7 +431,6 @@ nntp_server_t *mutt_select_newsserver (char *server) p_delete(&serv); return NULL; } - nntp_clear_cacheindex (serv); conn->data = (void *) serv; return serv; @@ -737,7 +752,7 @@ static void nntp_delete_cache (nntp_data_t * data) { char buf[_POSIX_PATH_MAX]; - if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv) + if (!nntp.use_cache || !data || !data->cache || !data->nserv) return; nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache); @@ -750,22 +765,6 @@ static void nntp_delete_cache (nntp_data_t * data) NULL); } -/* Remove cache files of unsubscribed newsgroups */ -void nntp_clear_cacheindex (nntp_server_t * news) -{ - nntp_data_t *data; - - if (option (OPTSAVEUNSUB) || !news) - return; - - for (data = news->list; data; data = data->next) { - if (!data || data->subscribed || !data->cache) - continue; - nntp_delete_cache (data); - } - return; -} - static int nntp_save_cache_index (nntp_server_t * news) { char buf[HUGE_STRING]; @@ -775,7 +774,7 @@ static int nntp_save_cache_index (nntp_server_t * news) if (!news || !news->newsgroups) return -1; - if (!option (OPTNEWSCACHE)) + if (!nntp.use_cache) return 0; if (news->cache) { @@ -826,7 +825,7 @@ static int nntp_save_cache_group (CONTEXT * ctx) int i = 0, save = SORT_ORDER; int prev = 0; - if (!option (OPTNEWSCACHE)) + if (!nntp.use_cache) return 0; if (!ctx || !ctx->data || ctx->magic != M_NNTP) return -1; @@ -913,7 +912,7 @@ nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group) if (news->newsgroups->nelem < news->newsgroups->curnelem * 2) hash_resize (news->newsgroups, news->newsgroups->nelem * 2); hash_insert (news->newsgroups, data->group, data); - nntp_data_list_append(&news->list, data); + news->tail = nntp_data_list_append(news->tail, data); } if (!data->subscribed) { data->subscribed = 1; @@ -931,8 +930,6 @@ nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group) return NULL; if (data->subscribed) { data->subscribed = 0; - if (!option (OPTSAVEUNSUB)) - data->rc = 0; } return data; } @@ -1676,7 +1673,7 @@ static int nntp_fetch_headers(CONTEXT * ctx, int first, int last) /* CACHE: must be loaded xover cache here */ num = nntp_data->lastCached - first + 1; - if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) { + if (nntp.use_cache && nntp_data->cache && num > 0) { nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache); mutt_message (msg2); @@ -1818,7 +1815,7 @@ static int nntp_open_mailbox (CONTEXT * ctx) nntp_data = nntp_data_new(); nntp_data->group = m_strdup(buf); hash_insert(serv->newsgroups, nntp_data->group, nntp_data); - nntp_data_list_append(&serv->list, nntp_data); + serv->tail = nntp_data_list_append(serv->tail, nntp_data); } ctx->data = nntp_data; nntp_data->nserv = serv; @@ -1851,6 +1848,8 @@ static int nntp_open_mailbox (CONTEXT * ctx) if ((*l) == nntp_data) { nntp_data_list_pop(l); nntp_data_delete(&nntp_data); + if (!*l) + serv->tail = l; break; } } @@ -2076,10 +2075,8 @@ static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2) { nntp_data_t *data = ctx->data; - /* CACHE: update cache and .index files */ - if ((option (OPTSAVEUNSUB) || data->subscribed)) - nntp_save_cache_group (ctx); - nntp_free_acache (data); + nntp_save_cache_group(ctx); + nntp_free_acache(data); data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */ return 0; @@ -2249,7 +2246,7 @@ static int nntp_check_newgroups (nntp_server_t * serv, int force) if (*emp) nntp_save_cache_index(serv); mutt_clear_error(); - return _checked; + return nntp.checked; } /* Load list of all newsgroups from active */ @@ -2287,10 +2284,11 @@ int nntp_get_active (nntp_server_t * serv) tmp = &(*tmp)->next; } } - nntp_save_cache_index (serv); + serv->tail = tmp; + nntp_save_cache_index(serv); mutt_clear_error (); - return _checked; + return nntp.checked; } /* @@ -2377,9 +2375,9 @@ int nntp_check_children (CONTEXT * ctx, const char *msgid) * made sure that we dont have the article, so we need to visit * the server. Reading the cache at this point is also bad * because it would duplicate messages */ - if (option (OPTNEWSCACHE)) { + if (nntp.use_cache) { tmp++; - unset_option (OPTNEWSCACHE); + nntp.use_cache = false; } for (i = 0; i < cc.num; i++) { if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i]))) @@ -2389,7 +2387,7 @@ int nntp_check_children (CONTEXT * ctx, const char *msgid) ctx->hdrs[ctx->msgcount - 1]->read = 0; } if (tmp) - set_option (OPTNEWSCACHE); + nntp.use_cache = true; p_delete(&cc.child); return ret; }