Getting rid of useless settings.
[apps/madmutt.git] / nntp.c
diff --git a/nntp.c b/nntp.c
index 78935ce..4d81012 100644 (file)
--- a/nntp.c
+++ b/nntp.c
@@ -123,7 +123,7 @@ static int slurp_newsrc (nntp_server_t * news)
         return -1;
     }
 
-    while (getline(&buf, &n, fp)) {
+    while (getline(&buf, &n, fp) >= 0) {
         nntp_parse_newsrc_line(news, buf);
     }
     p_delete(&buf);
@@ -133,15 +133,15 @@ static int slurp_newsrc (nntp_server_t * news)
     return 0;
 }
 
-static void nntp_cache_expand (char *dst, const char *src)
-{
-  snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
-  mutt_expand_path (dst, _POSIX_PATH_MAX);
-}
+#define nntp_cache_expand(dst, dlen, fmt, ...)     \
+    do {                                                                   \
+        snprintf((dst), (dlen), "%s/" fmt, NewsCacheDir, ##__VA_ARGS__);   \
+        mutt_expand_path((dst), (dlen));                                   \
+    } while (0)
 
 /* Loads $news_cache_dir/.index into memory, loads newsserver data
  * and newsgroup cache names */
-static int nntp_parse_cacheindex (nntp_server_t * news)
+static int nntp_parse_cacheindex(nntp_server_t *news)
 {
   struct stat st;
   char buf[HUGE_STRING], *cp;
@@ -161,8 +161,7 @@ static int nntp_parse_cacheindex (nntp_server_t * news)
   mutt_expand_path (dir, sizeof (dir));
 
   if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
-    snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"),
-              dir);
+    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!");
@@ -175,7 +174,7 @@ static int nntp_parse_cacheindex (nntp_server_t * news)
   set_option (OPTNEWSCACHE);
 
   p_delete(&news->cache);
-  snprintf (buf, sizeof (buf), "%s/.index", dir);
+  snprintf(buf, sizeof (buf), "%s/%s.index", dir, news->conn->account.host);
   if (!(idx = safe_fopen (buf, "a+")))
     return 0;
   rewind (idx);
@@ -227,26 +226,6 @@ static int nntp_parse_cacheindex (nntp_server_t * news)
   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);
-}
-
 /* 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,
@@ -348,7 +327,7 @@ static int nntp_get_cache_all (nntp_server_t * serv)
   char buf[HUGE_STRING];
   FILE *f;
 
-  nntp_cache_expand (buf, serv->cache);
+  nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
   if ((f = safe_fopen (buf, "r"))) {
     int i = 0;
 
@@ -414,9 +393,7 @@ nntp_server_t *mutt_select_newsserver (char *server)
   if (!conn)
     return NULL;
 
-  m_strformat(file, sizeof(file), 0, NewsRc, nntp_format_str, NULL, 0);
-  mutt_expand_path(file, sizeof(file));
-
+  nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
   serv = (nntp_server_t *) conn->data;
   if (serv) {
     struct stat sb;
@@ -738,11 +715,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;
@@ -770,8 +747,8 @@ static int nntp_update_cacheindex (nntp_server_t * 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);
 }
 
 static void nntp_delete_cache (nntp_data_t * data)
@@ -781,13 +758,14 @@ static void nntp_delete_cache (nntp_data_t * data)
   if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
     return;
 
-  nntp_cache_expand (buf, data->cache);
+  nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache);
   unlink (buf);
   p_delete(&data->cache);
   data->lastCached = 0;
-  nntp_cache_expand (buf, ".index");
-  mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
-                         NULL);
+  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);
 }
 
 /* Remove cache files of unsubscribed newsgroups */
@@ -819,15 +797,14 @@ static int nntp_save_cache_index (nntp_server_t * news)
     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;
@@ -873,17 +850,18 @@ static int nntp_save_cache_group (CONTEXT * ctx)
     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;
@@ -1719,7 +1697,7 @@ static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
   /* CACHE: must be loaded xover cache here */
   num = nntp_data->lastCached - first + 1;
   if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
-  nntp_cache_expand (buf, nntp_data->cache);
+  nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
   mutt_message (msg2);
 
   if ((f = safe_fopen (buf, "r"))) {