don't resize the hash, 32k elements are way enough.
[apps/madmutt.git] / nntp.c
diff --git a/nntp.c b/nntp.c
index d6054a3..a61bcf8 100644 (file)
--- a/nntp.c
+++ b/nntp.c
 #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_t *);
-static void nntp_delete_cache (nntp_data_t *);
+static struct {
+    unsigned use_cache : 1;
 
-/* newsrc {{{ */
+    int  checked;
+} nntp = { true, 0 };
 
-static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
-{
-  nntp_data_t *data;
-  char group[LONG_STRING];
-  int x = 1;
-  char *p, *b, *h;
+static int nntp_check_newgroups (nntp_server_t *, int);
 
-  for (p = line; p; p = strchr(p, ',')) {
-    x++;
-  }
+/* newsrc {{{ */
 
-  p = strpbrk(line, ":!");
-  if (!p)
-    return -1;
+static void mutt_newsgroup_stat(nntp_data_t *data)
+{
+    data->unread = 0;
+    if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
+        return;
+
+    data->unread = data->lastMessage - data->firstMessage + 1;
+    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);
+    }
+}
 
-  m_strncpy(group, ssizeof(group), line, p - line);
-  if ((data = (nntp_data_t *) hash_find (news->newsgroups, group)) == NULL) {
-    data = xmalloc(sizeof(nntp_data_t) + m_strlen(group) + 1);
-    data->group = (char *) data + sizeof (nntp_data_t);
-    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_data_list_append(&news->list, data);
-  } else {
-    p_delete(&data->entries);
-  }
+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;
 
-  data->rc = 1;
-  data->entries = p_new(NEWSRC_ENTRY, x * 2);
-  data->max = x * 2;
+    for (p = line; *p; p++) {
+        x += *p == ',';
+    }
 
-  data->subscribed = (*p++ == ':');
+    p = strpbrk(line, ":!");
+    if (!p)
+        return -1;
 
-  b = p;
-  x = 0;
-  while (*b) {
-    while (*p && *p != ',' && *p != '\n')
-      p++;
-    if (*p) {
-      *p = '\0';
-      p++;
-    }
-    if ((h = strchr (b, '-'))) {
-      *h = '\0';
-      h++;
-      data->entries[x].first = atoi (b);
-      data->entries[x].last = atoi (h);
+    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->lastMessage)
+        data->lastMessage = 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_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 = (nntp_data_t *) hash_find (news->newsgroups, buf)) == NULL) {
-        data = xmalloc(sizeof(nntp_data_t) + m_strlen(buf) + 1);
-        data->group = (char *) data + sizeof (nntp_data_t);
-        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_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,
@@ -288,6 +263,82 @@ void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
   p_delete(&url.path);
 }
 
+static int add_group (char *buf, void *serv)
+{
+  nntp_server_t *s = serv;
+  char group[LONG_STRING], mod, desc[HUGE_STRING];
+  int first, last;
+  nntp_data_t *nntp_data;
+  static int n = 0;
+
+  nntp.checked = n;       /* nntp.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 = hash_find(s->newsgroups, group)) == NULL) {
+    n++;
+    nntp_data = nntp_data_new();
+    nntp_data->group = m_strdup(group);
+    nntp_data->nserv = s;
+    hash_insert(s->newsgroups, nntp_data->group, nntp_data);
+    s->tail = nntp_data_list_append(s->tail, 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;
+}
+
+/* Load list of all newsgroups from cache ALL */
+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 = 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;
+  }
+}
+
 /*
  * Automatically loads a newsrc into memory, if necessary.
  * Checks the size/mtime of a newsrc file, if it doesn't match, load
@@ -298,13 +349,13 @@ 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;
   nntp_data_t *list;
   ACCOUNT act;
-  NNTP_SERVER *serv;
+  nntp_server_t *serv;
   CONNECTION *conn;
 
   p_clear(&act, 1);
@@ -333,10 +384,8 @@ 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;
 
@@ -346,12 +395,9 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
                                             || serv->mtime != sb.st_mtime)))
     {
       for (list = serv->list; list; list = list->next) {
-        list->subscribed = 0;
-        list->rc = 0;
-        list->num = 0;
+        list->subscribed = list->rc = list->num = 0;
       }
       slurp_newsrc (serv);
-      nntp_clear_cacheindex (serv);
     }
 
     if (serv->status == NNTP_BYE)
@@ -361,13 +407,14 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
   }
 
   /* 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);
+  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);
@@ -377,7 +424,6 @@ NNTP_SERVER *mutt_select_newsserver (char *server)
     p_delete(&serv);
     return NULL;
   }
-  nntp_clear_cacheindex (serv);
   conn->data = (void *) serv;
 
   return serv;
@@ -393,120 +439,52 @@ 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_t *data = (nntp_data_t *) ctx->data;
-  int x;
-
-  if (group)
-    data = (nntp_data_t *) 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_t * 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;
-  }
+    /* Old articles are articles which aren't read but an article after them
+     * has been cached */
+    if (option(OPTMARKOLD))
+        h->old = 1;
 }
 
-static int puti (char *line, int num)
+static void nntp_create_newsrc_line(nntp_data_t *data, buffer_t *buf)
 {
-  char *p, s[32];
-
-  for (p = s; num;) {
-    *p++ = '0' + num % 10;
-    num /= 10;
-  }
-  while (p > s)
-    *line++ = *--p, num++;
-  *line = '\0';
-  return num;
-}
+    buffer_addstr(buf, data->group);
+    buffer_addch(buf, data->subscribed ? ':' : '!');
+    buffer_addch(buf, ' ');
 
-static void nntp_create_newsrc_line (nntp_data_t * 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--;
-    }
+    for (int x = 0; x < data->num; x++) {
+        if (x) {
+            buffer_addch(buf, ',');
+        }
 
-    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;
+        buffer_addf(buf, "%d-%d", data->entries[x].first,
+                    data->entries[x].last);
     }
-  }
-  *line++ = '\n';
-  *line = '\0';
-  *pline = line;
+    buffer_addch(buf, '\n');
 }
 
 static void newsrc_gen_entries (CONTEXT * ctx)
@@ -579,7 +557,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];
@@ -688,35 +667,36 @@ 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_t *data;
-  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 (data = news->list; data; data = data->next) {
-    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;
+        nntp_create_newsrc_line(data, &buf);
+    }
+    /* 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)
@@ -725,11 +705,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;
@@ -738,7 +718,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_t * data)
+static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
 {
   char buf[LONG_STRING];
   char file[_POSIX_PATH_MAX];
@@ -757,27 +737,28 @@ static int nntp_update_cacheindex (NNTP_SERVER * serv, nntp_data_t * data)
     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_t *data;
+  char buf[_POSIX_PATH_MAX];
 
-  if (option (OPTSAVEUNSUB) || !news)
+  if (!nntp.use_cache || !data || !data->cache || !data->nserv)
     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", 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];
@@ -786,19 +767,18 @@ static int nntp_save_cache_index (NNTP_SERVER * 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;
@@ -838,23 +818,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;
@@ -910,38 +891,19 @@ static int nntp_save_cache_group (CONTEXT * ctx)
   return 0;
 }
 
-static void nntp_delete_cache (nntp_data_t * 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_t *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
+nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
 {
   nntp_data_t *data;
 
   if (!news || !news->newsgroups || !group || !*group)
     return NULL;
   if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
-    data = xmalloc(sizeof(nntp_data_t) + m_strlen(group) + 1);
-    data->group = (char *) data + sizeof (nntp_data_t);
-    strcpy (data->group, 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_data_list_append(&news->list, data);
+    news->tail = nntp_data_list_append(news->tail, data);
   }
   if (!data->subscribed) {
     data->subscribed = 1;
@@ -950,7 +912,7 @@ nntp_data_t *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
   return data;
 }
 
-nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group)
+nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
 {
   nntp_data_t *data;
 
@@ -959,13 +921,11 @@ nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group)
     return NULL;
   if (data->subscribed) {
     data->subscribed = 0;
-    if (!option (OPTSAVEUNSUB))
-      data->rc = 0;
   }
   return data;
 }
 
-nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
+nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
 {
   nntp_data_t *data;
 
@@ -989,7 +949,7 @@ nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
   return data;
 }
 
-nntp_data_t *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
+nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
 {
   nntp_data_t *data;
 
@@ -1065,8 +1025,6 @@ void nntp_buffy (char* dst, ssize_t dstlen) {
 /* }}} */
 /* nntp protocol {{{ */
 
-static unsigned int _checked = 0;
-
 void nntp_sync_sidebar (nntp_data_t* data) {
   int i = 0;
   BUFFY* tmp = NULL;
@@ -1100,7 +1058,7 @@ void nntp_sync_sidebar (nntp_data_t* data) {
   tmp->msgcount = data->lastMessage - data->firstMessage;
 }
 
-static int nntp_auth (NNTP_SERVER * serv)
+static int nntp_auth (nntp_server_t * serv)
 {
   CONNECTION *conn = serv->conn;
   char buf[STRING];
@@ -1139,7 +1097,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);
@@ -1149,7 +1107,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];
@@ -1209,7 +1167,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;
@@ -1252,7 +1210,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;
@@ -1265,7 +1223,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];
 
@@ -1508,7 +1466,7 @@ static int parse_description (char *line, void *n)
 {
   register char *d = line;
   nntp_data_t *data;
-  NNTP_SERVER *news = n;
+  nntp_server_t *news = n;
 
   if (!line)
     return 0;
@@ -1538,9 +1496,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);
 }
 
 /*
@@ -1577,7 +1533,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;
@@ -1709,8 +1664,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"))) {
@@ -1825,7 +1780,7 @@ static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
 static int nntp_open_mailbox (CONTEXT * ctx)
 {
   nntp_data_t *nntp_data;
-  NNTP_SERVER *serv;
+  nntp_server_t *serv;
   char buf[HUGE_STRING];
   char server[LONG_STRING];
   int count = 0, first;
@@ -1847,12 +1802,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_t *) hash_find (serv->newsgroups, buf)) == NULL) {
-    nntp_data = xmalloc(sizeof(nntp_data_t) + m_strlen(buf) + 1);
-    nntp_data->group = (char *) nntp_data + sizeof (nntp_data_t);
-    strcpy (nntp_data->group, buf);
-    hash_insert (serv->newsgroups, nntp_data->group, nntp_data);
-    nntp_data_list_append(&serv->list, 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;
@@ -1885,6 +1839,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;
           }
       }
@@ -2102,6 +2058,7 @@ void nntp_data_wipe(nntp_data_t *data)
     p_delete(&data->entries);
     p_delete(&data->desc);
     p_delete(&data->cache);
+    p_delete(&data->group);
     nntp_free_acache(data);
 }
 
@@ -2109,10 +2066,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;
@@ -2156,7 +2111,7 @@ int nntp_close_mailbox (CONTEXT * ctx)
   }
   nntp_sync_mailbox (ctx, 0, NULL);
   if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
-    NNTP_SERVER *news;
+    nntp_server_t *news;
 
     news = ((nntp_data_t *) ctx->data)->nserv;
     newsrc_gen_entries (ctx);
@@ -2225,59 +2180,7 @@ static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
   return _nntp_check_mailbox (ctx, (nntp_data_t *) 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_t *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_t *) hash_find (s->newsgroups, group)) == NULL) {
-    n++;
-    nntp_data = xmalloc(sizeof(nntp_data_t) + m_strlen(group) + 1);
-    nntp_data->group = (char *) nntp_data + sizeof (nntp_data_t);
-    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_data_list_append(&s->list, 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;
-}
-
-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_t nntp_data;
@@ -2334,38 +2237,11 @@ static int nntp_check_newgroups (NNTP_SERVER * serv, int force)
   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_t nntp_data, **tmp = &serv->list;
@@ -2399,10 +2275,11 @@ int nntp_get_active (NNTP_SERVER * 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;
 }
 
 /*
@@ -2489,9 +2366,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])))
@@ -2501,7 +2378,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;
 }