X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=blobdiff_plain;f=nntp%2Fnewsrc.c;h=898b1481de9618a2ce4a4ad381ae112d44479cf1;hp=22ff5a2262c60d14586b135ee90da92c99352da9;hb=ef7b80006ebb47372d69c64e8b2b2f0764333d55;hpb=203e950e3d3c76795fa49895d040f732adad2049 diff --git a/nntp/newsrc.c b/nntp/newsrc.c index 22ff5a2..898b148 100644 --- a/nntp/newsrc.c +++ b/nntp/newsrc.c @@ -18,7 +18,6 @@ #include "sort.h" #include "mx.h" #include "mime.h" -#include "mailbox.h" #include "nntp.h" #include "rfc822.h" #include "rfc1524.h" @@ -27,11 +26,13 @@ #include "lib/mem.h" #include "lib/str.h" #include "lib/intl.h" +#include "lib/debug.h" #include #include #include #include +#include #include void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d) @@ -41,7 +42,7 @@ void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d) if (!s || !d) return; - l = safe_calloc (1, sizeof (LIST)); + l = mem_calloc (1, sizeof (LIST)); if (s->list) s->tail->next = l; else @@ -74,7 +75,7 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line) strfcpy (group, line, len); if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) { data = - (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + mutt_strlen (group) + 1); + (NNTP_DATA *) mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1); data->group = (char *) data + sizeof (NNTP_DATA); strcpy (data->group, group); data->nserv = news; @@ -86,10 +87,10 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line) nntp_add_to_list (news, data); } else - FREE ((void **) &data->entries); + mem_free (&data->entries); data->rc = 1; - data->entries = safe_calloc (x * 2, sizeof (NEWSRC_ENTRY)); + data->entries = mem_calloc (x * 2, sizeof (NEWSRC_ENTRY)); data->max = x * 2; if (*p == ':') @@ -125,7 +126,7 @@ static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line) data->lastMessage = data->entries[x - 1].last; data->num = x; mutt_newsgroup_stat (data); - dprint (2, (debugfile, "parse_line: Newsgroup %s\n", data->group)); + debug_print (2, ("Newsgroup %s\n", data->group)); return 0; } @@ -148,10 +149,10 @@ static int slurp_newsrc (NNTP_SERVER * news) return -1; } - buf = safe_malloc (sb.st_size + 1); + buf = mem_malloc (sb.st_size + 1); while (fgets (buf, sb.st_size + 1, fp)) nntp_parse_newsrc_line (news, buf); - FREE (&buf); + mem_free (&buf); mx_unlock_file (news->newsrc, fileno (fp), 0); fclose (fp); @@ -199,15 +200,15 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news) set_option (OPTNEWSCACHE); - FREE (&news->cache); + mem_free (&news->cache); snprintf (buf, sizeof (buf), "%s/.index", dir); if (!(index = safe_fopen (buf, "a+"))) return 0; rewind (index); while (fgets (buf, sizeof (buf), index)) { - buf[mutt_strlen (buf) - 1] = 0; /* strip ending '\n' */ - if (!mutt_strncmp (buf, "#: ", 3) && - !mutt_strcasecmp (buf + 3, news->conn->account.host)) + buf[str_len (buf) - 1] = 0; /* strip ending '\n' */ + if (!str_ncmp (buf, "#: ", 3) && + !str_casecmp (buf + 3, news->conn->account.host)) break; } while (fgets (buf, sizeof (buf), index)) { @@ -217,18 +218,18 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news) if (!*cp) continue; cp[0] = 0; - if (!mutt_strcmp (buf, "#:")) + if (!str_cmp (buf, "#:")) break; sscanf (cp + 1, "%s %d %d", file, &l, &m); - if (!mutt_strcmp (buf, "ALL")) { - news->cache = safe_strdup (file); + if (!str_cmp (buf, "ALL")) { + news->cache = str_dup (file); news->newgroups_time = m; } else if (news->newsgroups) { if ((data = (NNTP_DATA *) hash_find (news->newsgroups, buf)) == NULL) { data = - (NNTP_DATA *) safe_calloc (1, - sizeof (NNTP_DATA) + mutt_strlen (buf) + 1); + (NNTP_DATA *) mem_calloc (1, + sizeof (NNTP_DATA) + str_len (buf) + 1); data->group = (char *) data + sizeof (NNTP_DATA); strcpy (data->group, buf); data->nserv = news; @@ -239,7 +240,7 @@ static int nntp_parse_cacheindex (NNTP_SERVER * news) hash_insert (news->newsgroups, data->group, data, 0); nntp_add_to_list (news, data); } - data->cache = safe_strdup (file); + data->cache = str_dup (file); t = 0; if (!data->firstMessage || data->lastMessage < m) t = 1; @@ -266,7 +267,7 @@ const char *nntp_format_str (char *dest, size_t destlen, char op, switch (op) { case 's': strncpy (fn, NewsServer, sizeof (fn) - 1); - mutt_strlower (fn); + str_tolower (fn); snprintf (tmp, sizeof (tmp), "%%%ss", fmt); snprintf (dest, destlen, tmp, fn); break; @@ -288,7 +289,7 @@ int nntp_parse_url (const char *server, ACCOUNT * acct, acct->port = NNTP_PORT; acct->type = M_ACCT_TYPE_NNTP; - c = safe_strdup (server); + c = str_dup (server); url_parse_ciss (&url, c); if (url.scheme == U_NNTP || url.scheme == U_NNTPS) { @@ -304,7 +305,7 @@ int nntp_parse_url (const char *server, ACCOUNT * acct, ret = mutt_account_fromurl (acct, &url); } - FREE (&c); + mem_free (&c); return ret; } @@ -312,10 +313,10 @@ void nntp_expand_path (char *line, size_t len, ACCOUNT * acct) { ciss_url_t url; - url.path = safe_strdup (line); + url.path = str_dup (line); mutt_account_tourl (acct, &url); url_ciss_tostring (&url, line, len, 0); - FREE (&url.path); + mem_free (&url.path); } /* @@ -337,13 +338,15 @@ NNTP_SERVER *mutt_select_newsserver (char *server) NNTP_SERVER *serv; CONNECTION *conn; + memset (&acct, 0, sizeof (ACCOUNT)); + if (!server || !*server) { mutt_error _("No newsserver defined!"); return NULL; } - buf = p = safe_calloc (mutt_strlen (server) + 10, sizeof (char)); + buf = p = mem_calloc (str_len (server) + 10, sizeof (char)); if (url_check_scheme (server) == U_UNKNOWN) { strcpy (buf, "nntp://"); p = strchr (buf, '\0'); @@ -351,11 +354,11 @@ NNTP_SERVER *mutt_select_newsserver (char *server) strcpy (p, server); if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file) { - FREE (&buf); + mem_free (&buf); mutt_error (_("%s is an invalid newsserver specification!"), server); return NULL; } - FREE (&buf); + mem_free (&buf); conn = mutt_conn_find (NULL, &acct); if (!conn) @@ -393,9 +396,9 @@ NNTP_SERVER *mutt_select_newsserver (char *server) } /* New newsserver */ - serv = safe_calloc (1, sizeof (NNTP_SERVER)); + serv = mem_calloc (1, sizeof (NNTP_SERVER)); serv->conn = conn; - serv->newsrc = safe_strdup (file); + serv->newsrc = str_dup (file); serv->newsgroups = hash_create (1009); slurp_newsrc (serv); /* load .newsrc */ nntp_parse_cacheindex (serv); /* load .index */ @@ -406,9 +409,9 @@ NNTP_SERVER *mutt_select_newsserver (char *server) for (list = serv->list; list; list = list->next) list->data = NULL; mutt_free_list (&serv->list); - FREE (&serv->newsrc); - FREE (&serv->cache); - FREE (&serv); + mem_free (&serv->newsrc); + mem_free (&serv->cache); + mem_free (&serv); return NULL; } nntp_clear_cacheindex (serv); @@ -438,7 +441,7 @@ void nntp_get_status (CONTEXT * ctx, HEADER * h, char *group, int article) if (!data) { #ifdef DEBUG if (group) - dprint (3, (debugfile, "newsgroup %s not found\n", group)); + debug_print (3, ("newsgroup %s not found\n", group)); #endif return; } @@ -508,12 +511,12 @@ static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf, len += *buflen; *buflen *= 2; line = *buf; - safe_realloc (buf, *buflen); + mem_realloc (buf, *buflen); line = *buf + (*pline - line); } strcpy (line, data->group); - len -= mutt_strlen (line) + 1; - line += mutt_strlen (line); + len -= str_len (line) + 1; + line += str_len (line); *line++ = data->subscribed ? ':' : '!'; *line++ = ' '; *line = '\0'; @@ -524,7 +527,7 @@ static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf, *buflen *= 2; *pline = line; line = *buf; - safe_realloc (buf, *buflen); + mem_realloc (buf, *buflen); line = *buf + (*pline - line); } if (x) { @@ -572,7 +575,7 @@ void newsrc_gen_entries (CONTEXT * ctx) } if (!data->max) { - data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY)); + data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY)); data->max = 5; } @@ -594,7 +597,7 @@ void newsrc_gen_entries (CONTEXT * ctx) !ctx->hdrs[x]->read) { if (data->num >= data->max) { data->max = data->max * 2; - safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY)); + mem_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY)); } data->entries[data->num].first = first; data->entries[data->num].last = last - 1; @@ -614,7 +617,7 @@ void newsrc_gen_entries (CONTEXT * ctx) if (series && first <= data->lastLoaded) { if (data->num >= data->max) { data->max = data->max * 2; - safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY)); + mem_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY)); } data->entries[data->num].first = first; data->entries[data->num].last = data->lastLoaded; @@ -627,6 +630,119 @@ void newsrc_gen_entries (CONTEXT * ctx) } } +static int mutt_update_list_file (char *filename, char *section, + char *key, char *line) { + FILE *ifp; + FILE *ofp; + char buf[HUGE_STRING]; + char tmpfile[_POSIX_PATH_MAX], link[_POSIX_PATH_MAX]; + char *c; + int ext = 0, done = 0, r = 0, l = 0; + + /* if file not exist, create it */ + if ((ifp = safe_fopen (filename, "a"))) + fclose (ifp); + debug_print (1, ("Opening %s\n", filename)); + if (!(ifp = safe_fopen (filename, "r"))) { + mutt_error (_("Unable to open %s for reading"), filename); + return -1; + } + if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) { + fclose (ifp); + mutt_error (_("Unable to lock %s"), filename); + return -1; + } + /* use mutt_adv_mktemp() to get a tempfile in the same + * directory as filename is so that we can follow symlinks + * via rename(2); as dirname(2) may modify its argument, + * temporarily use buf as copy of it + */ + strncpy (buf, filename, sizeof (buf)); + strncpy (tmpfile, basename (filename), sizeof (tmpfile)); + mutt_adv_mktemp ((const char*) dirname (buf), tmpfile, sizeof (tmpfile)); + debug_print (1, ("Opening %s\n", tmpfile)); + if (!(ofp = fopen (tmpfile, "w"))) { + fclose (ifp); + mutt_error (_("Unable to open %s for writing"), tmpfile); + return -1; + } + + if (section) { + while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) { + r = fputs (buf, ofp); + c = buf; + while (*c && *c != '\n') c++; + c[0] = 0; /* strip EOL */ + if (!strncmp (buf, "#: ", 3) && !str_casecmp (buf+3, section)) + done++; + } + if (r != EOF && !done) { + snprintf (buf, sizeof(buf), "#: %s\n", section); + r = fputs (buf, ofp); + } + done = 0; + } + + while (r != EOF && fgets (buf, sizeof (buf), ifp)) { + if (ext) { + c = buf; + while (*c && (*c != '\r') && (*c != '\n')) c++; + c--; + if (*c != '\\') ext = 0; + } else if ((section && !strncmp (buf, "#: ", 3))) { + if (!done && line) { + fputs (line, ofp); + fputc ('\n', ofp); + } + r = fputs (buf, ofp); + done++; + break; + } else if (key && !strncmp (buf, key, strlen(key)) && + (!*key || buf[strlen(key)] == ' ')) { + c = buf; + ext = 0; + while (*c && (*c != '\r') && (*c != '\n')) c++; + c--; + if (*c == '\\') ext = 1; + if (!done && line) { + r = fputs (line, ofp); + if (*key) + r = fputc ('\n', ofp); + done++; + } + } else { + r = fputs (buf, ofp); + } + } + + while (r != EOF && fgets (buf, sizeof (buf), ifp)) + r = fputs (buf, ofp); + + /* If there wasn't a line to replace, put it on the end of the file */ + if (r != EOF && !done && line) { + fputs (line, ofp); + r = fputc ('\n', ofp); + } + mx_unlock_file (filename, fileno (ifp), 0); + fclose (ofp); + fclose (ifp); + if (r == EOF) { + unlink (tmpfile); + mutt_error (_("Can't write %s"), tmpfile); + return -1; + } + link[0] = '\0'; + if ((l = readlink (filename, link, sizeof (link)-1)) > 0) + link[l] = '\0'; + debug_print (1, ("Renaming %s to %s\n",tmpfile, l > 0 ? link : filename)); + if (rename (tmpfile, l > 0 ? link : filename) < 0) { + unlink (tmpfile); + mutt_error (_("Can't rename %s to %s"), tmpfile, l > 0 ? link : filename); + return -1; + } + return 0; +} + int mutt_newsrc_update (NNTP_SERVER * news) { char *buf, *line; @@ -638,15 +754,15 @@ int mutt_newsrc_update (NNTP_SERVER * news) if (!news) return -1; llen = len = 10 * LONG_STRING; - line = buf = safe_calloc (1, len); + line = buf = mem_calloc (1, 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); - dprint (2, (debugfile, "Added to newsrc: %s", line)); - line += mutt_strlen (line); + debug_print (2, ("Added to newsrc: %s\n", line)); + line += str_len (line); } /* newrc being fully rewritten */ if (news->newsrc && @@ -657,7 +773,7 @@ int mutt_newsrc_update (NNTP_SERVER * news) news->size = st.st_size; news->mtime = st.st_mtime; } - FREE (&buf); + mem_free (&buf); return r; } @@ -672,7 +788,7 @@ static FILE *mutt_mkname (char *s) return fp; nntp_cache_expand (buf, "cache-XXXXXX"); - pc = buf + mutt_strlen (buf) - 12; /* positioning to "cache-XXXXXX" */ + pc = buf + str_len (buf) - 12; /* positioning to "cache-XXXXXX" */ if ((fd = mkstemp (buf)) == -1) return NULL; strcpy (s, pc); /* generated name */ @@ -716,7 +832,7 @@ void nntp_clear_cacheindex (NNTP_SERVER * news) if (!data || data->subscribed || !data->cache) continue; nntp_delete_cache (data); - dprint (2, (debugfile, "Removed from .index: %s\n", data->group)); + debug_print (2, ("Removed from .index: %s\n", data->group)); } return; } @@ -742,7 +858,7 @@ int nntp_save_cache_index (NNTP_SERVER * news) else { strfcpy (buf, news->conn->account.host, sizeof (buf)); f = mutt_mkname (buf); - news->cache = safe_strdup (buf); + news->cache = str_dup (buf); nntp_cache_expand (file, buf); } if (!f) @@ -798,7 +914,7 @@ int nntp_save_cache_group (CONTEXT * ctx) ((NNTP_DATA *) ctx->data)->nserv->conn->account.host, ((NNTP_DATA *) ctx->data)->group); f = mutt_mkname (buf); - ((NNTP_DATA *) ctx->data)->cache = safe_strdup (buf); + ((NNTP_DATA *) ctx->data)->cache = str_dup (buf); nntp_cache_expand (file, buf); } if (!f) @@ -828,7 +944,7 @@ int nntp_save_cache_group (CONTEXT * ctx) fputs (buf, f); if (h->env->references) mutt_write_references (h->env->references, f); - snprintf (buf, sizeof (buf), "\t%ld\t%d\tXref: %s\n", + snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n", h->content->length, h->lines, NONULL (h->env->xref)); if (fputs (buf, f) == EOF) { fclose (f); @@ -864,7 +980,7 @@ void nntp_delete_cache (NNTP_DATA * data) nntp_cache_expand (buf, data->cache); unlink (buf); - FREE (&data->cache); + mem_free (&data->cache); data->lastCached = 0; nntp_cache_expand (buf, ".index"); mutt_update_list_file (buf, data->nserv->conn->account.host, data->group, @@ -879,7 +995,7 @@ NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group) return NULL; if (!(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) { data = - (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + mutt_strlen (group) + 1); + (NNTP_DATA *) mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1); data->group = (char *) data + sizeof (NNTP_DATA); strcpy (data->group, group); data->nserv = news; @@ -920,7 +1036,7 @@ NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group) !(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) return NULL; if (!data->max) { - data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY)); + data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY)); data->max = 5; } data->num = 1; @@ -944,7 +1060,7 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group) !(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) return NULL; if (!data->max) { - data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY)); + data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY)); data->max = 5; } data->num = 1; @@ -963,26 +1079,50 @@ NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group) } /* this routine gives the first newsgroup with new messages */ -void nntp_buffy (char *s) -{ +void nntp_buffy (char* dst, size_t dstlen) { LIST *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 && + str_cmp (data->group, ((NNTP_DATA *) Context->data)->group) == 0) { + list = list->next; + break; + } + } + + *dst = '\0'; + + while (count < 2) { - if (data && data->subscribed && data->unread) { - if (Context && Context->magic == M_NNTP && - !mutt_strcmp (data->group, ((NNTP_DATA *) Context->data)->group)) { - unsigned int i, unread = 0; + if (!list) + list = CurrentNewsSrv->list; - for (i = 0; i < Context->msgcount; i++) - if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted) - unread++; - if (!unread) - continue; + for (; list; list = list->next) { + NNTP_DATA *data = (NNTP_DATA *) list->data; + + if (data && data->subscribed && data->unread) { + if (Context && Context->magic == M_NNTP && + !str_cmp (data->group, ((NNTP_DATA *) Context->data)->group)) { + unsigned int i, unread = 0; + + for (i = 0; i < Context->msgcount; i++) + if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted) + unread++; + if (!unread) + continue; + } + strncpy (dst, data->group, dstlen); + break; } - strcpy (s, data->group); - break; } + /* done if found */ + if (dst && *dst) + return; + count++; } + *dst = '\0'; }