X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=nntp.c;h=4e79ffc2a0f8449261270391b08834d5ffc9a47e;hp=73c254bef2e012d1162e7d51f4fbab969547f9f6;hb=c9b049f668148dab6ae90f32de6a4981f673bfa1;hpb=546a6855b7d44c415cb397e3a62f6a854132a0dd diff --git a/nntp.c b/nntp.c index 73c254b..4e79ffc 100644 --- a/nntp.c +++ b/nntp.c @@ -27,281 +27,312 @@ #define NNTP_PORT 119 #define NNTP_SSL_PORT 563 -static int nntp_get_cache_all (NNTP_SERVER *); -static int nntp_check_newgroups (NNTP_SERVER *, int); -static void mutt_newsgroup_stat (NNTP_DATA *); -static void nntp_delete_cache (NNTP_DATA *); -static void nntp_delete_data (void *p); +static struct { + unsigned use_cache : 1; -/* newsrc {{{ */ + int checked; +} nntp = { true, 0 }; + +static int nntp_check_newgroups (nntp_server_t *, int); -static void nntp_add_to_list(NNTP_SERVER *s, NNTP_DATA *d) +static void nntp_free_acache(nntp_data_t * data) { - *s->tail = p_new(string_list_t, 1); - (*s->tail)->data = (void *)d; - s->tail = &(*s->tail)->next; + for (int i = 0; i < countof(data->acache); i++) { + if (data->acache[i].path) { + unlink(data->acache[i].path); + p_delete(&data->acache[i].path); + } + } } -static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line) +void nntp_data_wipe(nntp_data_t *data) { - NNTP_DATA *data; - char group[LONG_STRING]; - int x = 1; - char *p = line, *b, *h; - ssize_t len; - - while (*p) { - if (*p++ == ',') - x++; - } - - p = line; - while (*p && (*p != ':' && *p != '!')) - p++; - if (!*p) - return -1; - len = MIN(p + 1 - line, ssizeof(group)); - m_strcpy(group, len, line); - if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) { - data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1); - data->group = (char *) data + sizeof (NNTP_DATA); - strcpy (data->group, 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_add_to_list (news, data); - } - else p_delete(&data->entries); + p_delete(&data->desc); + p_delete(&data->cache); + p_delete(&data->group); + nntp_free_acache(data); +} - data->rc = 1; - data->entries = p_new(NEWSRC_ENTRY, x * 2); - data->max = x * 2; +/* newsrc {{{ */ - if (*p == ':') - data->subscribed = 1; - else - data->subscribed = 0; +static void mutt_newsgroup_stat(nntp_data_t *data) +{ + data->unread = 0; + if (data->last == 0 || data->first > data->last) + return; + + data->unread = data->last - data->first + 1; + for (int i = 0; i < data->num; i++) { + int first = MAX(data->entries[i].first, data->first); + int last = MIN(data->entries[i].last, data->last); + data->unread -= MAX(0, last - first + 1); + } +} - p++; - b = p; - x = 0; - while (*b) { - while (*p && *p != ',' && *p != '\n') - p++; - if (*p) { - *p = '\0'; - p++; +static int nntp_parse_newsrc_line(nntp_server_t *news, const char *line) +{ + nntp_data_t *data; + char group[LONG_STRING]; + const char *p; + int x = 1; + + for (p = line; *p; p++) { + x += *p == ','; } - if ((h = strchr (b, '-'))) { - *h = '\0'; - h++; - data->entries[x].first = atoi (b); - data->entries[x].last = atoi (h); + + p = strpbrk(line, ":!"); + if (!p) + return -1; + + m_strncpy(group, ssizeof(group), line, p - line); + data = hash_find(news->newsgroups, group); + if (!data) { + data = nntp_data_new(); + data->group = p_dupstr(line, p - line); + data->nserv = news; + data->deleted = 1; + hash_insert(news->newsgroups, data->group, data); + news->tail = nntp_data_list_append(news->tail, data); + } else { + p_delete(&data->entries); } - else { - data->entries[x].first = atoi (b); - data->entries[x].last = data->entries[x].first; + + data->rc = 1; + data->entries = p_new(NEWSRC_ENTRY, x * 2); + data->max = x * 2; + data->subscribed = (*p++ == ':'); + p = skipspaces(p); + + for (x = 0; *p; p++) { + data->entries[x].first = strtol(p, (char **)&p, 10); + 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, ','); + } + x += data->entries[x].last != 0; } - b = p; - if (data->entries[x].last != 0) - x++; - } - if (x && !data->lastMessage) - data->lastMessage = data->entries[x - 1].last; - data->num = x; - mutt_newsgroup_stat (data); - return 0; + if (x && !data->last) + data->last = data->entries[x - 1].last; + data->num = x; + mutt_newsgroup_stat(data); + return 0; } -static int slurp_newsrc (NNTP_SERVER * news) +static int slurp_newsrc (nntp_server_t * news) { - FILE *fp; - char *buf; - struct stat sb; + FILE *fp; + char *buf = NULL; + size_t n = 0; + struct stat sb; - news->stat = stat (news->newsrc, &sb); - news->size = sb.st_size; - news->mtime = sb.st_mtime; + news->stat = stat(news->newsrc, &sb); + news->size = sb.st_size; + news->mtime = sb.st_mtime; - if ((fp = safe_fopen (news->newsrc, "r")) == NULL) - return -1; - /* hmm, should we use dotlock? */ - if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1)) { - m_fclose(&fp); - return -1; - } + if ((fp = safe_fopen(news->newsrc, "r")) == NULL) + return -1; - buf = p_new(char, sb.st_size + 1); - while (fgets (buf, sb.st_size + 1, fp)) - nntp_parse_newsrc_line (news, buf); - p_delete(&buf); + /* hmm, should we use dotlock? */ + if (mx_lock_file(news->newsrc, fileno (fp), 0, 0, 1)) { + m_fclose(&fp); + return -1; + } - mx_unlock_file (news->newsrc, fileno (fp), 0); - m_fclose(&fp); - return 0; -} + while (getline(&buf, &n, fp) >= 0) { + nntp_parse_newsrc_line(news, buf); + } + p_delete(&buf); -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); + mx_unlock_file (news->newsrc, fileno (fp), 0); + m_fclose(&fp); + return 0; } +#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 * 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 *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 = (NNTP_DATA *) hash_find (news->newsgroups, buf)) == NULL) { - data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1); - data->group = (char *) data + sizeof (NNTP_DATA); - strcpy (data->group, 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_add_to_list (news, 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->first || data->last < m; + if (!data->first) + data->first = l; + if (data->last < m) + data->last = 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) + return -1; - if (url.scheme == U_NNTP || url.scheme == U_NNTPS) { - if (url.scheme == U_NNTPS) { - act->has_ssl = 1; - act->port = NNTP_SSL_PORT; + act->has_ssl = (url.scheme == U_NNTPS); + if (!act->port) { + act->port = act->has_ssl ? NNTP_SSL_PORT : NNTP_PORT; } + m_strcpy(group, group_len, url.path); - *group = '\0'; - if (url.path) - m_strcpy(group, group_len, url.path); + return mutt_account_fromurl(act, &url); +} - ret = mutt_account_fromurl (act, &url); - } +void nntp_expand_path(char *line, ssize_t len, ACCOUNT *act) +{ + ciss_url_t url = { .path = alloca(len) }; - p_delete(&c); - return ret; + m_strcpy(url.path, len, line); + mutt_account_tourl (act, &url); + url_ciss_tostring(&url, line, len, 0); } -void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act) +static int add_group(char *buf, void *serv) { - ciss_url_t url; + char mod, group[HUGE_STRING], desc[HUGE_STRING]; + nntp_server_t *srv = serv; + nntp_data_t *data; + static int n = 0; + int first, last; + + nntp.checked = n; /* nntp.checked have N, where N = number of groups */ + if (!buf) /* at EOF must be zerouth */ + n = 0; + + if (!srv || !buf) + return 0; + + *desc = '\0'; + sscanf(buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc); + + if (!(data = hash_find(srv->newsgroups, group))) { + n++; + data = nntp_data_new(); + data->group = m_strdup(group); + data->nserv = srv; + hash_insert(srv->newsgroups, data->group, data); + srv->tail = nntp_data_list_append(srv->tail, data); + } - url.path = m_strdup(line); - mutt_account_tourl (act, &url); - url_ciss_tostring (&url, line, len, 0); - p_delete(&url.path); + data->deleted = 0; + data->first = first; + data->last = last; + data->allowed = mod == 'y'; + m_strreplace(&data->desc, desc); + mutt_newsgroup_stat(data); + return 0; +} + +static int nntp_get_cache_all(nntp_server_t * serv) +{ + char buf[HUGE_STRING]; + FILE *f; + + nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache); + if ((f = fopen(buf, "r"))) { + int i = 0; + + while (fgets(buf, sizeof(buf), f)) { + if (ReadInc && (i % ReadInc == 0)) + mutt_message (_("Loading list from cache... %d"), i); + add_group(buf, serv); + i++; + } + add_group (NULL, NULL); + m_fclose(&f); + mutt_clear_error(); + return 0; + } + + p_delete(&serv->cache); + return -1; } /* @@ -314,20 +345,19 @@ void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act) * ':' denoting subscribed or '!' denoting unsubscribed, then a * comma separated list of article numbers and ranges. */ -NNTP_SERVER *mutt_select_newsserver (char *server) +nntp_server_t *mutt_select_newsserver (char *server) { char file[_POSIX_PATH_MAX]; char *buf, *p; - string_list_t *list; + nntp_data_t *list; ACCOUNT act; - NNTP_SERVER *serv; + nntp_server_t *serv; CONNECTION *conn; p_clear(&act, 1); - if (!server || !*server) { + if (m_strisempty(server)) { mutt_error _("No newsserver defined!"); - return NULL; } @@ -338,7 +368,7 @@ NNTP_SERVER *mutt_select_newsserver (char *server) } strcpy (p, server); - if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) { + if ((nntp_parse_url(buf, &act, file, sizeof(file))) < 0 || *file) { p_delete(&buf); mutt_error (_("%s is an invalid newsserver specification!"), server); return NULL; @@ -349,59 +379,48 @@ NNTP_SERVER *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)); - - serv = (NNTP_SERVER *) conn->data; + nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host); + serv = (nntp_server_t *) conn->data; if (serv) { struct stat sb; /* externally modified? */ - if (serv->stat != stat (file, &sb) || (!serv->stat && - (serv->size != sb.st_size - || serv->mtime != sb.st_mtime))) { + if (serv->stat != stat(file, &sb) + || (!serv->stat && (serv->size != sb.st_size + || serv->mtime != sb.st_mtime))) + { for (list = serv->list; list; list = list->next) { - NNTP_DATA *data = (NNTP_DATA *) list->data; - - if (data) { - data->subscribed = 0; - data->rc = 0; - data->num = 0; - } + list->subscribed = list->rc = list->num = 0; } - slurp_newsrc (serv); - nntp_clear_cacheindex (serv); + slurp_newsrc(serv); } if (serv->status == NNTP_BYE) serv->status = NNTP_NONE; - nntp_check_newgroups (serv, 0); + nntp_check_newgroups(serv, 0); return serv; } /* New newsserver */ - serv = p_new(NNTP_SERVER, 1); + 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); - slurp_newsrc (serv); /* load .newsrc */ - nntp_parse_cacheindex (serv); /* load .index */ - if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0) + serv->newsgroups = hash_new(SHRT_MAX, false); + slurp_newsrc(serv); /* load .newsrc */ + nntp_parse_cacheindex(serv); /* load .index */ + 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, nntp_delete_data); - for (list = serv->list; list; list = list->next) - list->data = NULL; - string_list_wipe(&serv->list); + } else if (nntp_get_active(serv) < 0) { + hash_delete(&serv->newsgroups, NULL); + nntp_data_list_wipe(&serv->list); p_delete(&serv->newsrc); p_delete(&serv->cache); p_delete(&serv); return NULL; } - nntp_clear_cacheindex (serv); - conn->data = (void *) serv; + conn->data = serv; return serv; } @@ -415,125 +434,40 @@ NNTP_SERVER *mutt_select_newsserver (char *server) * "skipped" in the newsrc, and new is anything not in the newsrc nor * in the cache. By skipped, I mean before the last unread message */ -static void nntp_get_status(CONTEXT *ctx, HEADER *h, char *group, int article) +static void +nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article) { - NNTP_DATA *data = (NNTP_DATA *) ctx->data; - int x; - - if (group) - data = (NNTP_DATA *) hash_find (data->nserv->newsgroups, group); - - if (!data) { - return; - } - - for (x = 0; x < data->num; x++) { - if ((article >= data->entries[x].first) && - (article <= data->entries[x].last)) { - /* we cannot use mutt_set_flag() because mx_update_context() - didn't called yet */ - h->read = 1; - return; + nntp_data_t *data = ctx->data; + + if (group) + data = hash_find(data->nserv->newsgroups, group); + if (!data) + return; + + for (int i = 0; i < data->num; i++) { + if ((article >= data->entries[i].first) && + (article <= data->entries[i].last)) + { + /* we cannot use mutt_set_flag() because mx_update_context() + didn't called yet */ + h->read = 1; + return; + } } - } - /* If article was not cached yet, it is new! :) */ - if (!data->cache || article > data->lastCached) - return; - /* Old articles are articles which aren't read but an article after them - * has been cached */ - if (option (OPTMARKOLD)) - h->old = 1; -} -static void mutt_newsgroup_stat (NNTP_DATA * data) -{ - int i, first, last; - - data->unread = 0; - if (data->lastMessage == 0 || data->firstMessage > data->lastMessage) - return; + /* If article was not cached yet, it is new! :) */ + if (!data->cache || article > data->lastCached) + return; - data->unread = data->lastMessage - data->firstMessage + 1; - for (i = 0; i < data->num; i++) { - first = data->entries[i].first; - if (first < data->firstMessage) - first = data->firstMessage; - last = data->entries[i].last; - if (last > data->lastMessage) - last = data->lastMessage; - if (first <= last) - data->unread -= last - first + 1; - } -} - -static int puti (char *line, int num) -{ - char *p, s[32]; - - for (p = s; num;) { - *p++ = '0' + num % 10; - num /= 10; - } - while (p > s) - *line++ = *--p, num++; - *line = '\0'; - return num; -} - -static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf, - char **pline, ssize_t * buflen) -{ - char *line = *pline; - ssize_t len = *buflen - (*pline - *buf); - int x, i; - - if (len < LONG_STRING * 10) { - len += *buflen; - *buflen *= 2; - line = *buf; - p_realloc(buf, *buflen); - line = *buf + (*pline - line); - } - strcpy (line, data->group); - len -= m_strlen(line) + 1; - line += m_strlen(line); - *line++ = data->subscribed ? ':' : '!'; - *line++ = ' '; - *line = '\0'; - - for (x = 0; x < data->num; x++) { - if (len < LONG_STRING) { - len += *buflen; - *buflen *= 2; - *pline = line; - line = *buf; - p_realloc(buf, *buflen); - line = *buf + (*pline - line); - } - if (x) { - *line++ = ','; - len--; - } - - i = puti (line, data->entries[x].first); - line += i; - len -= i; - if (data->entries[x].first != data->entries[x].last) { - *line++ = '-'; - len--; - i = puti (line, data->entries[x].last); - line += i; - len -= i; - } - } - *line++ = '\n'; - *line = '\0'; - *pline = line; + /* Old articles are articles which aren't read but an article after them + * has been cached */ + if (option(OPTMARKOLD)) + h->old = 1; } static void newsrc_gen_entries (CONTEXT * ctx) { - NNTP_DATA *data = (NNTP_DATA *) ctx->data; + nntp_data_t *data = (nntp_data_t *) ctx->data; int series, x; int last = 0, first = 1; int save_sort = SORT_ORDER; @@ -563,7 +497,7 @@ static void newsrc_gen_entries (CONTEXT * ctx) * "missing" entries as read/deleted */ last = ctx->hdrs[x]->article_num; - if (last >= data->firstMessage && !ctx->hdrs[x]->deleted && + if (last >= data->first && !ctx->hdrs[x]->deleted && !ctx->hdrs[x]->read) { if (data->num >= data->max) { data->max = data->max * 2; @@ -601,7 +535,8 @@ static void newsrc_gen_entries (CONTEXT * ctx) } static int mutt_update_list_file (char *filename, char *section, - const char *key, char *line) { + const char *key, const char *line) +{ FILE *ifp; FILE *ofp; char buf[HUGE_STRING]; @@ -710,37 +645,46 @@ static int mutt_update_list_file (char *filename, char *section, return 0; } -int mutt_newsrc_update (NNTP_SERVER * news) +int mutt_newsrc_update (nntp_server_t * news) { - char *buf, *line; - NNTP_DATA *data; - string_list_t *tmp; - int r = -1; - ssize_t len, llen; + buffer_t buf; + nntp_data_t *data; + int r = -1; - if (!news) - return -1; - llen = len = 10 * LONG_STRING; - line = buf = p_new(char, len); - /* we will generate full newsrc here */ - for (tmp = news->list; tmp; tmp = tmp->next) { - data = (NNTP_DATA *) tmp->data; - if (!data || !data->rc) - continue; - nntp_create_newsrc_line (data, &buf, &line, &llen); - line += m_strlen(line); - } - /* newrc being fully rewritten */ - if (news->newsrc && - (r = mutt_update_list_file(news->newsrc, NULL, "", buf)) == 0) { - struct stat st; - - stat (news->newsrc, &st); - news->size = st.st_size; - news->mtime = st.st_mtime; - } - p_delete(&buf); - return r; + if (!news) + return -1; + + buffer_init(&buf); + + /* we will generate full newsrc here */ + for (data = news->list; data; data = data->next) { + if (!data || !data->rc) + continue; + + 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) + { + struct stat st; + + stat (news->newsrc, &st); + news->size = st.st_size; + news->mtime = st.st_mtime; + } + + buffer_wipe(&buf); + return r; } static FILE *mutt_mkname (char *s) @@ -749,11 +693,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,7 +706,7 @@ static FILE *mutt_mkname (char *s) } /* Updates info into .index file: ALL or about selected newsgroup */ -static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data) +static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data) { char buf[LONG_STRING]; char file[_POSIX_PATH_MAX]; @@ -773,72 +717,68 @@ static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * 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->first, 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); } -/* Remove cache files of unsubscribed newsgroups */ -void nntp_clear_cacheindex (NNTP_SERVER * news) +static void nntp_delete_cache (nntp_data_t * data) { - NNTP_DATA *data; - string_list_t *tmp; + char buf[_POSIX_PATH_MAX]; - if (option (OPTSAVEUNSUB) || !news) + if (!nntp.use_cache || !data || !data->cache || !data->nserv) return; - for (tmp = news->list; tmp; tmp = tmp->next) { - data = (NNTP_DATA *) tmp->data; - if (!data || data->subscribed || !data->cache) - continue; - nntp_delete_cache (data); - } - return; + nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache); + unlink (buf); + p_delete(&data->cache); + data->lastCached = 0; + 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 * news) +static int nntp_save_cache_index (nntp_server_t * news) { char buf[HUGE_STRING]; char file[_POSIX_PATH_MAX]; - NNTP_DATA *d; + nntp_data_t *d; FILE *f; - string_list_t *l; 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; - for (l = news->list; l; l = l->next) { - if ((d = (NNTP_DATA *) l->data) && !d->deleted) { + for (d = news->list; d; d = d->next) { + if (!d->deleted) { if (d->desc) snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group, - d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n', + d->last, d->first, d->allowed ? 'y' : 'n', d->desc); else snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group, - d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n'); + d->last, d->first, d->allowed ? 'y' : 'n'); if (fputs (buf, f) == EOF) { m_fclose(&f); unlink (file); @@ -865,23 +805,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 *) ctx->data)->cache) { - nntp_cache_expand (file, ((NNTP_DATA *) ctx->data)->cache); + if (((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 *) ctx->data)->nserv->conn->account.host, - ((NNTP_DATA *) 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 *) 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; @@ -893,7 +834,7 @@ static int nntp_save_cache_group (CONTEXT * ctx) } /* Save only $nntp_context messages... */ - ((NNTP_DATA *) ctx->data)->lastCached = 0; + ((nntp_data_t *) ctx->data)->lastCached = 0; if (NntpContext && ctx->msgcount > NntpContext) i = ctx->msgcount - NntpContext; for (; i < ctx->msgcount; i++) { @@ -927,48 +868,29 @@ static int nntp_save_cache_group (CONTEXT * ctx) } m_fclose(&f); - if (nntp_update_cacheindex (((NNTP_DATA *) ctx->data)->nserv, - (NNTP_DATA *) ctx->data)) { + if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv, + (nntp_data_t *) ctx->data)) { unlink (file); return -1; } - ((NNTP_DATA *) ctx->data)->lastCached = - ((NNTP_DATA *) ctx->data)->lastLoaded; + ((nntp_data_t *) ctx->data)->lastCached = + ((nntp_data_t *) ctx->data)->lastLoaded; return 0; } -static void nntp_delete_cache (NNTP_DATA * data) -{ - char buf[_POSIX_PATH_MAX]; - - if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv) - return; - - nntp_cache_expand (buf, 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); -} - -NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group) { - NNTP_DATA *data; + nntp_data_t *data; if (!news || !news->newsgroups || !group || !*group) return NULL; - if (!(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) { - data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1); - data->group = (char *) data + sizeof (NNTP_DATA); - strcpy (data->group, group); + if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) { + data = nntp_data_new(); + 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_add_to_list (news, data); + news->tail = nntp_data_list_append(news->tail, data); } if (!data->subscribed) { data->subscribed = 1; @@ -977,27 +899,25 @@ NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group) return data; } -NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group) { - NNTP_DATA *data; + nntp_data_t *data; if (!news || !news->newsgroups || !group || !*group || - !(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) + !(data = (nntp_data_t *) hash_find (news->newsgroups, group))) return NULL; if (data->subscribed) { data->subscribed = 0; - if (!option (OPTSAVEUNSUB)) - data->rc = 0; } return data; } -NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group) { - NNTP_DATA *data; + nntp_data_t *data; if (!news || !news->newsgroups || !group || !*group || - !(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) + !(data = (nntp_data_t *) hash_find (news->newsgroups, group))) return NULL; if (!data->max) { data->entries = p_new(NEWSRC_ENTRY, 5); @@ -1006,7 +926,7 @@ NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group) data->num = 1; data->entries[0].first = 1; data->unread = 0; - data->entries[0].last = data->lastMessage; + data->entries[0].last = data->last; if (Context && Context->data == data) { int x; @@ -1016,12 +936,12 @@ NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group) return data; } -NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group) +nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group) { - NNTP_DATA *data; + nntp_data_t *data; if (!news || !news->newsgroups || !group || !*group || - !(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) + !(data = (nntp_data_t *) hash_find (news->newsgroups, group))) return NULL; if (!data->max) { data->entries = p_new(NEWSRC_ENTRY, 5); @@ -1029,7 +949,7 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group) } data->num = 1; data->entries[0].first = 1; - data->entries[0].last = data->firstMessage - 1; + data->entries[0].last = data->first - 1; if (Context && Context->data == data) { int x; @@ -1038,21 +958,21 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group) mutt_set_flag (Context, Context->hdrs[x], M_READ, 0); } else - data->unread = data->lastMessage - data->entries[0].last; + data->unread = data->last - data->entries[0].last; return data; } /* this routine gives the first newsgroup with new messages */ void nntp_buffy (char* dst, ssize_t dstlen) { - string_list_t *list; + nntp_data_t *list; int count = 0; /* forward to current group */ for (list = CurrentNewsSrv->list; list; list = list->next) { - NNTP_DATA *data = (NNTP_DATA *) list->data; - if (data && data->subscribed && data->unread && - Context && Context->magic == M_NNTP && - m_strcmp(data->group, ((NNTP_DATA *) Context->data)->group) == 0) { + if (list->subscribed && list->unread + && Context && Context->magic == M_NNTP + && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0) + { list = list->next; break; } @@ -1066,11 +986,9 @@ void nntp_buffy (char* dst, ssize_t dstlen) { list = CurrentNewsSrv->list; for (; list; list = list->next) { - NNTP_DATA *data = (NNTP_DATA *) list->data; - - if (data && data->subscribed && data->unread) { + if (list->subscribed && list->unread) { if (Context && Context->magic == M_NNTP && - !m_strcmp(data->group, ((NNTP_DATA *) Context->data)->group)) { + !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) { int i, unread = 0; for (i = 0; i < Context->msgcount; i++) @@ -1079,7 +997,7 @@ void nntp_buffy (char* dst, ssize_t dstlen) { if (!unread) continue; } - m_strcpy(dst, dstlen, data->group); + m_strcpy(dst, dstlen, list->group); break; } } @@ -1094,9 +1012,7 @@ void nntp_buffy (char* dst, ssize_t dstlen) { /* }}} */ /* nntp protocol {{{ */ -static unsigned int _checked = 0; - -void nntp_sync_sidebar (NNTP_DATA* data) { +void nntp_sync_sidebar (nntp_data_t* data) { int i = 0; BUFFY* tmp = NULL; char buf[STRING]; @@ -1119,21 +1035,21 @@ void nntp_sync_sidebar (NNTP_DATA* data) { tmp = Incoming.arr[i]; /* copied from browser.c */ if (option (OPTMARKOLD) && - data->lastCached >= data->firstMessage && - data->lastCached <= data->lastMessage) - tmp->msg_unread = data->lastMessage - data->lastCached; + data->lastCached >= data->first && + data->lastCached <= data->last) + tmp->msg_unread = data->last - data->lastCached; else tmp->msg_unread = data->unread; tmp->new = data->unread > 0; /* this is closest to a "total" count we can get */ - tmp->msgcount = data->lastMessage - data->firstMessage; + tmp->msgcount = data->last - data->first; } -static int nntp_auth (NNTP_SERVER * serv) +static int nntp_auth (nntp_server_t * serv) { CONNECTION *conn = serv->conn; char buf[STRING]; - unsigned char flags = conn->account.flags; + int flags = conn->account.flags; if (mutt_account_getuser(&conn->account) || !conn->account.user[0] || mutt_account_getpass(&conn->account) || !conn->account.pass[0]) { @@ -1168,7 +1084,7 @@ static int nntp_auth (NNTP_SERVER * serv) return 0; } -static int nntp_connect_error (NNTP_SERVER * serv) +static int nntp_connect_error (nntp_server_t * serv) { serv->status = NNTP_NONE; mutt_socket_close (serv->conn); @@ -1178,7 +1094,7 @@ static int nntp_connect_error (NNTP_SERVER * serv) return -1; } -static int nntp_connect_and_auth (NNTP_SERVER * serv) +static int nntp_connect_and_auth (nntp_server_t * serv) { CONNECTION *conn = serv->conn; char buf[STRING]; @@ -1238,7 +1154,7 @@ static int nntp_connect_and_auth (NNTP_SERVER * serv) return 0; } -static int nntp_attempt_features (NNTP_SERVER * serv) +static int nntp_attempt_features (nntp_server_t * serv) { char buf[LONG_STRING]; CONNECTION *conn = serv->conn; @@ -1281,7 +1197,7 @@ static int nntp_attempt_features (NNTP_SERVER * serv) return 0; } -static int nntp_open_connection (NNTP_SERVER * serv) +static int nntp_open_connection (nntp_server_t * serv) { if (serv->status == NNTP_OK) return 0; @@ -1294,7 +1210,7 @@ static int nntp_open_connection (NNTP_SERVER * serv) return 0; } -static int nntp_reconnect (NNTP_SERVER * serv) +static int nntp_reconnect (nntp_server_t * serv) { char buf[STRING]; @@ -1314,7 +1230,7 @@ static int nntp_reconnect (NNTP_SERVER * serv) } /* Send data from line[LONG_STRING] and receive answer to same line */ -static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen) +static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen) { char buf[LONG_STRING]; int done = TRUE; @@ -1363,7 +1279,7 @@ static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen) * -2 - invalid command or execution error, * -3 - error in funct(*line, *data). */ -static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, +static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query, const char *msg, progress_t* bar, int (*funct) (char *, void *), void *data, int tagged) @@ -1494,7 +1410,7 @@ static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref, static int nntp_read_header (CONTEXT * ctx, const char *msgid, int article_num) { - NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data); + nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data); FILE *f; char buf[LONG_STRING]; char tempfile[_POSIX_PATH_MAX]; @@ -1536,8 +1452,8 @@ static int nntp_read_header (CONTEXT * ctx, const char *msgid, static int parse_description (char *line, void *n) { register char *d = line; - NNTP_DATA *data; - NNTP_SERVER *news = n; + nntp_data_t *data; + nntp_server_t *news = n; if (!line) return 0; @@ -1547,7 +1463,7 @@ static int parse_description (char *line, void *n) d++; while (*d && (*d == '\t' || *d == ' ')) d++; - if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL && + if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL && m_strcmp(d, data->desc)) { p_delete(&data->desc); data->desc = m_strdup(d); @@ -1555,7 +1471,7 @@ static int parse_description (char *line, void *n) return 0; } -static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar) +static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar) { char buf[STRING]; @@ -1567,9 +1483,7 @@ static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progre 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); } /* @@ -1582,7 +1496,7 @@ static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progre */ static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr) { - NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data; + nntp_data_t *nntp_data = (nntp_data_t *) ctx->data; char *p, *b; int x, done = 0; @@ -1606,7 +1520,6 @@ static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr) p++; switch (x) { case 0: - hdr->article_num = atoi (b); nntp_get_status (ctx, hdr, NULL, hdr->article_num); break; @@ -1686,7 +1599,7 @@ static int add_xover_line (char *line, void *c) int num, total; FETCH_CONTEXT *fc = c; CONTEXT *ctx = fc->ctx; - NNTP_DATA *data = (NNTP_DATA *) ctx->data; + nntp_data_t *data = (nntp_data_t *) ctx->data; if (!line) return 0; @@ -1720,7 +1633,7 @@ static int nntp_fetch_headers(CONTEXT * ctx, int first, int last) char buf[HUGE_STRING]; const char *msg = _("Fetching message headers..."); const char *msg2 = _("Fetching headers from cache..."); - NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data); + nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data); int ret, num, oldmsgcount, current; FILE *f; FETCH_CONTEXT fc; @@ -1738,8 +1651,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"))) { @@ -1853,8 +1766,8 @@ static int nntp_fetch_headers(CONTEXT * ctx, int first, int last) */ static int nntp_open_mailbox (CONTEXT * ctx) { - NNTP_DATA *nntp_data; - NNTP_SERVER *serv; + nntp_data_t *nntp_data; + nntp_server_t *serv; char buf[HUGE_STRING]; char server[LONG_STRING]; int count = 0, first; @@ -1876,12 +1789,11 @@ static int nntp_open_mailbox (CONTEXT * ctx) CurrentNewsSrv = serv; /* create NNTP-specific state struct if nof found in list */ - if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) { - nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1); - nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA); - strcpy (nntp_data->group, buf); - hash_insert (serv->newsgroups, nntp_data->group, nntp_data); - nntp_add_to_list (serv, nntp_data); + if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) { + nntp_data = nntp_data_new(); + nntp_data->group = m_strdup(buf); + hash_insert(serv->newsgroups, nntp_data->group, nntp_data); + serv->tail = nntp_data_list_append(serv->tail, nntp_data); } ctx->data = nntp_data; nntp_data->nserv = serv; @@ -1900,7 +1812,7 @@ static int nntp_open_mailbox (CONTEXT * ctx) } if (m_strncmp("211", buf, 3)) { - string_list_t *l = serv->list; + nntp_data_t **l; /* GROUP command failed */ if (!m_strncmp("411", buf, 3)) { @@ -1909,21 +1821,25 @@ static int nntp_open_mailbox (CONTEXT * ctx) /* CACHE: delete cache and line from .index */ nntp_delete_cache (nntp_data); - hash_remove (serv->newsgroups, nntp_data->group, NULL, - nntp_delete_data); - while (l && l->data != (void *) nntp_data) - l = l->next; - if (l) - l->data = NULL; - + hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL); + for (l = &serv->list; *l; l = &(*l)->next) { + if ((*l) == nntp_data) { + nntp_data_list_pop(l); + nntp_data_delete(&nntp_data); + if (!*l) + serv->tail = l; + break; + } + } + nntp_data_delete(&nntp_data); sleep (2); } return -1; } - sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage, - &nntp_data->lastMessage, buf); + sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->first, + &nntp_data->last, buf); nntp_data->deleted = 0; @@ -1933,12 +1849,12 @@ static int nntp_open_mailbox (CONTEXT * ctx) * Check for max adding context. If it is greater than $nntp_context, * strip off extra articles */ - first = nntp_data->firstMessage; - if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext) - first = nntp_data->lastMessage - NntpContext + 1; + first = nntp_data->first; + if (NntpContext && nntp_data->last - first + 1 > NntpContext) + first = nntp_data->last - NntpContext + 1; if (first) nntp_data->lastLoaded = first - 1; - return nntp_fetch_headers (ctx, first, nntp_data->lastMessage); + return nntp_fetch_headers (ctx, first, nntp_data->last); } int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) @@ -1951,7 +1867,7 @@ int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) /* see if we already have the message in our cache */ cache = - &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index % + &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index % NNTP_CACHE_LEN]; /* if everything is fine, assign msg->fp and return */ @@ -1981,7 +1897,7 @@ int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno) bar.size = 0; mutt_progress_bar (&bar, 0); - ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile, + ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile, msg->fp, ctx->tagged); if (ret == 1) { mutt_error (_("Article %d not found on server"), @@ -2027,17 +1943,18 @@ int nntp_post (const char *msg) char buf[LONG_STRING]; size_t len; FILE *f; - NNTP_DATA *nntp_data; + nntp_data_t *nntp_data; if (Context && Context->magic == M_NNTP) - nntp_data = (NNTP_DATA *) Context->data; + nntp_data = (nntp_data_t *)Context->data; else { - if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) || - !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) { + if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) || + !CurrentNewsSrv->list) + { mutt_error (_("Can't post article. No connection to news server.")); return -1; } - nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data; + nntp_data = CurrentNewsSrv->list; } if (!(f = safe_fopen (msg, "r"))) { @@ -2111,39 +2028,12 @@ void nntp_logout_all (void) } } -static void nntp_free_acache (NNTP_DATA * data) -{ - int i; - - for (i = 0; i < NNTP_CACHE_LEN; i++) { - if (data->acache[i].path) { - unlink (data->acache[i].path); - p_delete(&data->acache[i].path); - } - } -} - -static void nntp_delete_data (void *p) -{ - NNTP_DATA *data = (NNTP_DATA *)p; - - if (!p) - return; - p_delete(&data->entries); - p_delete(&data->desc); - p_delete(&data->cache); - nntp_free_acache (data); - p_delete(&data); -} - static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2) { - NNTP_DATA *data = ctx->data; + 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; @@ -2151,7 +2041,7 @@ static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2) static void nntp_fastclose_mailbox (CONTEXT * ctx) { - NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp; + nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp; if (!data) return; @@ -2161,7 +2051,7 @@ static void nntp_fastclose_mailbox (CONTEXT * ctx) nntp_save_cache_index (data->nserv); if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL || tmp != data) - nntp_delete_data (data); + nntp_data_delete(&data); else nntp_sync_sidebar (data); } @@ -2174,7 +2064,7 @@ int nntp_close_mailbox (CONTEXT * ctx) mutt_message _("Quitting newsgroup..."); if (ctx->data) { - NNTP_DATA *data = (NNTP_DATA *) ctx->data; + nntp_data_t *data = (nntp_data_t *) ctx->data; int ret; if (data->nserv && data->nserv->conn && ctx->unread) { @@ -2186,12 +2076,12 @@ int nntp_close_mailbox (CONTEXT * ctx) } } nntp_sync_mailbox (ctx, 0, NULL); - if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) { - NNTP_SERVER *news; + if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) { + nntp_server_t *news; - news = ((NNTP_DATA *) ctx->data)->nserv; + news = ((nntp_data_t *) ctx->data)->nserv; newsrc_gen_entries (ctx); - ((NNTP_DATA *) ctx->data)->unread = ctx->unread; + ((nntp_data_t *) ctx->data)->unread = ctx->unread; mutt_newsrc_update (news); } mutt_clear_error (); @@ -2199,7 +2089,7 @@ int nntp_close_mailbox (CONTEXT * ctx) } /* use the GROUP command to poll for new mail */ -static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data) +static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data) { char buf[LONG_STRING]; int count = 0; @@ -2222,8 +2112,8 @@ static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data) int last; sscanf (buf + 4, "%d %d %d", &count, &first, &last); - nntp_data->firstMessage = first; - nntp_data->lastMessage = last; + nntp_data->first = first; + nntp_data->last = last; if (ctx && last > nntp_data->lastLoaded) { nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last); time (&nntp_data->nserv->check_time); @@ -2253,66 +2143,14 @@ static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data) static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2) { - return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data); -} - -static int add_group (char *buf, void *serv) -{ - NNTP_SERVER *s = serv; - char group[LONG_STRING], mod, desc[HUGE_STRING]; - int first, last; - NNTP_DATA *nntp_data; - static int n = 0; - - _checked = n; /* _checked have N, where N = number of groups */ - if (!buf) /* at EOF must be zerouth */ - n = 0; - - if (!s || !buf) - return 0; - - *desc = 0; - sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc); - if (!group) - return 0; - if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) { - n++; - nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1); - nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA); - strcpy (nntp_data->group, 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_add_to_list (s, nntp_data); - } - nntp_data->deleted = 0; - nntp_data->firstMessage = first; - nntp_data->lastMessage = last; - if (mod == 'y') - nntp_data->allowed = 1; - else - nntp_data->allowed = 0; - if (nntp_data->desc) - p_delete(&nntp_data->desc); - if (*desc) - nntp_data->desc = m_strdup(desc); - if (nntp_data->rc || nntp_data->lastCached) - mutt_newsgroup_stat (nntp_data); - else if (nntp_data->lastMessage && - nntp_data->firstMessage <= nntp_data->lastMessage) - nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1; - else - nntp_data->unread = 0; - - return 0; + return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data); } -static int nntp_check_newgroups (NNTP_SERVER * serv, int force) +static int nntp_check_newgroups (nntp_server_t * serv, int force) { char buf[LONG_STRING]; - NNTP_DATA nntp_data; - string_list_t *l, **emp; + nntp_data_t nntp_data; + nntp_data_t *l, **emp; time_t now; struct tm *t; @@ -2328,8 +2166,8 @@ static int nntp_check_newgroups (NNTP_SERVER * serv, int force) for (l = serv->list; l; l = l->next) { serv->check_time = 0; /* really check! */ - if (l->data && ((NNTP_DATA *) l->data)->subscribed) - _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data); + if (l->subscribed) + _nntp_check_mailbox (NULL, l); } sidebar_draw (); } @@ -2346,11 +2184,11 @@ static int nntp_check_newgroups (NNTP_SERVER * serv, int force) t->tm_min, t->tm_sec); nntp_data.nserv = serv; if (Context && Context->magic == M_NNTP) - nntp_data.group = ((NNTP_DATA *) Context->data)->group; + nntp_data.group = ((nntp_data_t *) Context->data)->group; else nntp_data.group = NULL; - emp = serv->tail; + emp = nntp_data_list_last(&serv->list); if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL, add_group, serv, 0) != 0) { return -1; @@ -2359,49 +2197,20 @@ static int nntp_check_newgroups (NNTP_SERVER * serv, int force) mutt_message _("Loading descriptions..."); for (l = *emp; l; l = l->next) { - ((NNTP_DATA *)l->data)->new = 1; - nntp_get_desc((NNTP_DATA *) l->data, ((NNTP_DATA *)l->data)->group, - NULL, NULL); + l->new = 1; + nntp_get_desc(l, l->group, NULL, NULL); } if (*emp) nntp_save_cache_index(serv); mutt_clear_error(); - return _checked; -} - -/* Load list of all newsgroups from cache ALL */ -static int nntp_get_cache_all (NNTP_SERVER * serv) -{ - char buf[HUGE_STRING]; - FILE *f; - - nntp_cache_expand (buf, serv->cache); - if ((f = safe_fopen (buf, "r"))) { - int i = 0; - - while (fgets (buf, sizeof (buf), f) != NULL) { - if (ReadInc && (i % ReadInc == 0)) - mutt_message (_("Loading list from cache... %d"), i); - add_group (buf, serv); - i++; - } - add_group (NULL, NULL); - m_fclose(&f); - mutt_clear_error (); - return 0; - } - else { - p_delete(&serv->cache); - return -1; - } + return nntp.checked; } /* Load list of all newsgroups from active */ -int nntp_get_active (NNTP_SERVER * serv) +int nntp_get_active (nntp_server_t * serv) { char msg[STRING]; - NNTP_DATA nntp_data; - string_list_t *tmp; + nntp_data_t nntp_data, **tmp = &serv->list; if (nntp_open_connection (serv) < 0) return -1; @@ -2422,19 +2231,21 @@ int nntp_get_active (NNTP_SERVER * serv) mutt_message (msg); nntp_get_desc (&nntp_data, "*", msg, NULL); - for (tmp = serv->list; tmp; tmp = tmp->next) { - NNTP_DATA *data = (NNTP_DATA *) tmp->data; - - if (data && data->deleted && !data->rc) { - nntp_delete_cache (data); - hash_remove (serv->newsgroups, data->group, NULL, nntp_delete_data); - tmp->data = NULL; + while (*tmp) { + if ((*tmp)->deleted && !(*tmp)->rc) { + nntp_data_t *d = nntp_data_list_pop(tmp); + nntp_delete_cache(d); + hash_remove(serv->newsgroups, d->group, NULL, NULL); + nntp_data_delete(&d); + } else { + tmp = &(*tmp)->next; } } - nntp_save_cache_index (serv); + serv->tail = tmp; + nntp_save_cache_index(serv); mutt_clear_error (); - return _checked; + return nntp.checked; } /* @@ -2493,7 +2304,7 @@ static int check_children (char *s, void *c) int nntp_check_children (CONTEXT * ctx, const char *msgid) { - NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data; + nntp_data_t *nntp_data = (nntp_data_t *) ctx->data; char buf[STRING]; int i, ret = 0, tmp = 0; CHILD_CONTEXT cc = { ctx, 0, 0, NULL }; @@ -2501,7 +2312,7 @@ int nntp_check_children (CONTEXT * ctx, const char *msgid) if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn || !nntp_data->nserv->conn->account.host) return -1; - if (nntp_data->firstMessage > nntp_data->lastLoaded) + if (nntp_data->first > nntp_data->lastLoaded) return 0; if (!nntp_data->nserv->hasXPAT) { mutt_error (_("Server %s does not support this operation!"), @@ -2510,7 +2321,7 @@ int nntp_check_children (CONTEXT * ctx, const char *msgid) } snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n", - nntp_data->firstMessage, nntp_data->lastLoaded, msgid); + nntp_data->first, nntp_data->lastLoaded, msgid); if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) { p_delete(&cc.child); @@ -2521,9 +2332,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]))) @@ -2533,7 +2344,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; }