X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=nntp.c;h=4d810124886a4aac4dc022a7d2aa32f547d7e44f;hp=9319c38ce48e8876f68113de5b9683ed0452bffa;hb=6a7c1f87b97c733b63c177d41a5aca3427e9521a;hpb=7768c028980b5fb468decf4fbfbd640082c321b2;ds=sidebyside diff --git a/nntp.c b/nntp.c index 9319c38..4d81012 100644 --- a/nntp.c +++ b/nntp.c @@ -105,41 +105,43 @@ static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line) 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, 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; @@ -159,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!"); @@ -173,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); @@ -225,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, @@ -346,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; @@ -412,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; @@ -736,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; @@ -768,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) @@ -779,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 */ @@ -817,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; @@ -871,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; @@ -1717,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"))) {