X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=nntp.c;h=914df3968bb66cf50f407054d1b79598550761b7;hp=78935ce01d78092ebb4fc31fe16a5febc1495f1d;hb=1cd799f26007d33d10b1ae04aa443f572d84ab20;hpb=e3c03b4252d4d86369538dd2fab3f023639dfb35 diff --git a/nntp.c b/nntp.c index 78935ce..914df39 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); @@ -47,7 +51,7 @@ static void mutt_newsgroup_stat(nntp_data_t *data) } } -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]; @@ -69,10 +73,8 @@ static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line) data->group = p_dupstr(line, p - line); 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); + news->tail = nntp_data_list_append(news->tail, data); } else { p_delete(&data->entries); } @@ -81,18 +83,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; } @@ -123,7 +126,7 @@ static int slurp_newsrc (nntp_server_t * news) return -1; } - while (getline(&buf, &n, fp)) { + while (getline(&buf, &n, fp) >= 0) { nntp_parse_newsrc_line(news, buf); } p_delete(&buf); @@ -133,152 +136,114 @@ static int slurp_newsrc (nntp_server_t * news) return 0; } -static void nntp_cache_expand (char *dst, const char *src) -{ - snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src); - mutt_expand_path (dst, _POSIX_PATH_MAX); -} +#define nntp_cache_expand(dst, dlen, fmt, ...) \ + do { \ + snprintf((dst), (dlen), "%s/" fmt, mod_core.cachedir, ##__VA_ARGS__);\ + mutt_expand_path((dst), (dlen)); \ + } while (0) /* Loads $news_cache_dir/.index into memory, loads newsserver data * and newsgroup cache names */ -static int nntp_parse_cacheindex (nntp_server_t * news) +static int nntp_parse_cacheindex(nntp_server_t *news) { - struct stat st; - char buf[HUGE_STRING], *cp; - char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX]; - FILE *idx; - nntp_data_t *data; - int l, m, t; - - /* check is server name defined or not */ - if (!news || !news->conn || !news->conn->account.host) - return -1; - unset_option (OPTNEWSCACHE); - if (m_strisempty(NewsCacheDir)) - return 0; + 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; + } + } - m_strcpy(dir, sizeof(dir), NewsCacheDir); - mutt_expand_path (dir, sizeof (dir)); + while (fgets(buf, sizeof(buf), idx)) { + char *p, *q; + int l, m, t; - if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) { - snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"), - dir); - if (mutt_yesorno (buf, M_YES) != M_YES - || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) { - mutt_error _("Cache directory not created!"); + if (m_strstart(buf, "#:", NULL)) + break; - return -1; - } - mutt_clear_error (); - } + 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); - set_option (OPTNEWSCACHE); + if (!m_strcmp(buf, "ALL")) { + m_strreplace(&news->cache, m_strdup(p)); + news->newgroups_time = m; + continue; + } - p_delete(&news->cache); - snprintf (buf, sizeof (buf), "%s/.index", dir); - 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 (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; + 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; -} - -const char *nntp_format_str(char *dest, ssize_t destlen, char op, - const char *src, const char *fmt, - const char *ifstr __attribute__((unused)), - const char *elstr __attribute__((unused)), - anytype data __attribute__((unused)), - format_flag flags __attribute__((unused))) -{ - char fn[STRING], tmp[STRING]; - switch (op) { - case 's': - m_strcpy(fn, sizeof (fn), NewsServer); - m_strtolower(fn); - snprintf (tmp, sizeof (tmp), "%%%ss", fmt); - snprintf (dest, destlen, tmp, fn); - break; - } - return (src); + m_fclose(&idx); + return 0; } /* nntp_parse_url: given an NNPT URL, return host, port, * username, password and newsgroup will recognise. */ -static int nntp_parse_url(const char *server, ACCOUNT * act, char *group, +static int nntp_parse_url(const char *server, ACCOUNT *act, char *group, ssize_t group_len) { - ciss_url_t url; - char *c; - int ret = -1; + ciss_url_t url; + char s[STRING]; - /* Defaults */ - act->flags = 0; - act->port = NNTP_PORT; - act->type = M_ACCT_TYPE_NNTP; + act->flags = 0; + act->port = 0; + act->type = M_ACCT_TYPE_NNTP; - c = m_strdup(server); - url_parse_ciss (&url, c); + m_strcpy(s, sizeof(s), server); + url_parse_ciss(&url, s); - if (url.scheme == U_NNTP || url.scheme == U_NNTPS) { - if (url.scheme == U_NNTPS) { - act->has_ssl = 1; - act->port = NNTP_SSL_PORT; - } - - *group = '\0'; - if (url.path) - m_strcpy(group, group_len, url.path); + if (url.scheme != U_NNTP || url.scheme != U_NNTPS) + return -1; - ret = mutt_account_fromurl (act, &url); - } + act->has_ssl = (url.scheme == U_NNTPS); + if (!act->port) { + act->port = act->has_ssl ? NNTP_PORT : NNTP_SSL_PORT; + } + m_strcpy(group, group_len, url.path); - p_delete(&c); - return ret; + return mutt_account_fromurl(act, &url); } void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act) @@ -299,8 +264,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) @@ -315,10 +280,8 @@ static int add_group (char *buf, void *serv) nntp_data = nntp_data_new(); nntp_data->group = m_strdup(group); nntp_data->nserv = s; - 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; @@ -348,7 +311,7 @@ static int nntp_get_cache_all (nntp_server_t * serv) char buf[HUGE_STRING]; FILE *f; - nntp_cache_expand (buf, serv->cache); + nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache); if ((f = safe_fopen (buf, "r"))) { int i = 0; @@ -414,9 +377,7 @@ nntp_server_t *mutt_select_newsserver (char *server) if (!conn) return NULL; - m_strformat(file, sizeof(file), 0, NewsRc, nntp_format_str, NULL, 0); - mutt_expand_path(file, sizeof(file)); - + nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host); serv = (nntp_server_t *) conn->data; if (serv) { struct stat sb; @@ -430,7 +391,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) @@ -441,12 +401,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); @@ -456,7 +417,6 @@ nntp_server_t *mutt_select_newsserver (char *server) p_delete(&serv); return NULL; } - nntp_clear_cacheindex (serv); conn->data = (void *) serv; return serv; @@ -503,23 +463,6 @@ nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article) h->old = 1; } -static void nntp_create_newsrc_line(nntp_data_t *data, buffer_t *buf) -{ - buffer_addstr(buf, data->group); - buffer_addch(buf, data->subscribed ? ':' : '!'); - buffer_addch(buf, ' '); - - for (int x = 0; x < data->num; x++) { - if (x) { - buffer_addch(buf, ','); - } - - buffer_addf(buf, "%d-%d", data->entries[x].first, - data->entries[x].last); - } - buffer_addch(buf, '\n'); -} - static void newsrc_gen_entries (CONTEXT * ctx) { nntp_data_t *data = (nntp_data_t *) ctx->data; @@ -715,8 +658,18 @@ int mutt_newsrc_update (nntp_server_t * news) for (data = news->list; data; data = data->next) { if (!data || !data->rc) continue; - nntp_create_newsrc_line(data, &buf); + + buffer_addstr(&buf, data->group); + buffer_addch(&buf, data->subscribed ? ':' : '!'); + + for (int x = 0; x < data->num; x++) { + buffer_addch(&buf, x ? ',' : ' '); + buffer_addf(&buf, "%d-%d", data->entries[x].first, + data->entries[x].last); + } + buffer_addch(&buf, '\n'); } + /* newrc being fully rewritten */ if (news->newsrc && (r = mutt_update_list_file(news->newsrc, NULL, "", buf.data)) == 0) @@ -738,11 +691,11 @@ static FILE *mutt_mkname (char *s) int fd; FILE *fp; - nntp_cache_expand (buf, s); + nntp_cache_expand(buf, ssizeof(buf), "%s", s); if ((fp = safe_fopen (buf, "w"))) return fp; - nntp_cache_expand (buf, "cache-XXXXXX"); + nntp_cache_expand(buf, ssizeof(buf), "cache-XXXXXX"); pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */ if ((fd = mkstemp (buf)) == -1) return NULL; @@ -762,48 +715,32 @@ static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data) if (data && data->group) { key = data->group; - snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache, - data->firstMessage, data->lastLoaded); - } - else { + snprintf(buf, sizeof(buf), "%s %s %d %d", key, data->cache, + data->firstMessage, data->lastLoaded); + } else { m_strcpy(file, sizeof(file), serv->cache); - snprintf (buf, sizeof (buf), "ALL %s 0 %d", file, - (int) serv->newgroups_time); + snprintf(buf, sizeof (buf), "ALL %s 0 %d", file, + (int)serv->newgroups_time); } - nntp_cache_expand (file, ".index"); - return mutt_update_list_file (file, serv->conn->account.host, key, buf); + nntp_cache_expand(file, ssizeof(file), "%s.index", serv->conn->account.host); + return mutt_update_list_file(file, serv->conn->account.host, key, buf); } 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, data->cache); + nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache); unlink (buf); p_delete(&data->cache); data->lastCached = 0; - nntp_cache_expand (buf, ".index"); - mutt_update_list_file (buf, data->nserv->conn->account.host, data->group, - 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; + nntp_cache_expand(buf, ssizeof(buf), "%s.index", + data->nserv->conn->account.host); + mutt_update_list_file(buf, data->nserv->conn->account.host, data->group, + NULL); } static int nntp_save_cache_index (nntp_server_t * news) @@ -815,19 +752,18 @@ 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) { - nntp_cache_expand (file, news->cache); - unlink (file); - f = safe_fopen (file, "w"); - } - else { - m_strcpy(buf, sizeof(buf), news->conn->account.host); - f = mutt_mkname (buf); + nntp_cache_expand(file, ssizeof(file), "%s", news->cache); + unlink(file); + f = safe_fopen(file, "w"); + } else { + m_strcpy(buf, ssizeof(buf), news->conn->account.host); + f = mutt_mkname(buf); news->cache = m_strdup(buf); - nntp_cache_expand (file, buf); + nntp_cache_expand(file, ssizeof(file), "%s", buf); } if (!f) return -1; @@ -867,23 +803,24 @@ 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; if (((nntp_data_t *) ctx->data)->cache) { - nntp_cache_expand (file, ((nntp_data_t *) ctx->data)->cache); + nntp_cache_expand(file, ssizeof(file), "%s", + ((nntp_data_t *)ctx->data)->cache); unlink (file); f = safe_fopen (file, "w"); } else { - snprintf (buf, sizeof (buf), "%s-%s", - ((nntp_data_t *) ctx->data)->nserv->conn->account.host, - ((nntp_data_t *) ctx->data)->group); + snprintf(buf, ssizeof(buf), "%s-%s", + ((nntp_data_t *)ctx->data)->nserv->conn->account.host, + ((nntp_data_t *)ctx->data)->group); f = mutt_mkname (buf); - ((nntp_data_t *) ctx->data)->cache = m_strdup(buf); - nntp_cache_expand (file, buf); + ((nntp_data_t *)ctx->data)->cache = m_strdup(buf); + nntp_cache_expand(file, ssizeof(file), "%s", buf); } if (!f) return -1; @@ -950,10 +887,8 @@ nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group) data->group = m_strdup(group); 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); + news->tail = nntp_data_list_append(news->tail, data); } if (!data->subscribed) { data->subscribed = 1; @@ -971,8 +906,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; } @@ -1548,9 +1481,7 @@ static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, prog snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask); else snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask); - if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) != - 0) { - } + mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0); } /* @@ -1718,8 +1649,8 @@ 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) { - nntp_cache_expand (buf, nntp_data->cache); + if (nntp.use_cache && nntp_data->cache && num > 0) { + nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache); mutt_message (msg2); if ((f = safe_fopen (buf, "r"))) { @@ -1860,7 +1791,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; @@ -1893,6 +1824,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; } } @@ -2118,10 +2051,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; @@ -2291,7 +2222,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 */ @@ -2329,10 +2260,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; } /* @@ -2419,9 +2351,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]))) @@ -2431,7 +2363,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; }