+#include "buffy.h"
+#include "crypt.h"
+
+#define NNTP_PORT 119
+#define NNTP_SSL_PORT 563
+
+static struct {
+ unsigned use_cache : 1;
+
+ int checked;
+} nntp = { true, 0 };
+
+static int nntp_check_newgroups (nntp_server_t *, int);
+
+static void nntp_free_acache(nntp_data_t * data)
+{
+ 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);
+ }
+ }
+}
+
+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);
+}
+
+/* newsrc {{{ */
+
+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);
+ }
+}
+
+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 == ',';
+ }
+
+ 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);
+ }
+
+ 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;
+ }
+
+ 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_t * news)
+{
+ 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;
+
+ 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;
+ }
+
+ while (getline(&buf, &n, fp) >= 0) {
+ nntp_parse_newsrc_line(news, buf);
+ }
+ p_delete(&buf);
+
+ 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_t *news)
+{
+ 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;
+ }
+ }
+
+ while (fgets(buf, sizeof(buf), idx)) {
+ char *p, *q;
+ int l, m, t;
+
+ if (m_strstart(buf, "#:", NULL))
+ break;
+
+ 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);
+
+ if (!m_strcmp(buf, "ALL")) {
+ m_strreplace(&news->cache, m_strdup(p));
+ news->newgroups_time = m;
+ continue;
+ }
+
+ 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;
+}
+
+/* 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,
+ ssize_t group_len)
+{
+ ciss_url_t url;
+ char s[STRING];
+
+ act->flags = 0;
+ act->port = 0;
+ act->type = M_ACCT_TYPE_NNTP;
+
+ m_strcpy(s, sizeof(s), server);
+ url_parse_ciss(&url, s);
+
+ if (url.scheme != U_NNTP && url.scheme != U_NNTPS)
+ return -1;
+
+ 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);
+
+ return mutt_account_fromurl(act, &url);
+}
+
+void nntp_expand_path(char *line, ssize_t len, ACCOUNT *act)
+{
+ ciss_url_t url = { .path = alloca(len) };
+
+ m_strcpy(url.path, len, line);
+ mutt_account_tourl (act, &url);
+ url_ciss_tostring(&url, line, len, 0);
+}
+
+static int add_group(char *buf, void *serv)
+{
+ 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);
+ }
+
+ 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;
+}
+
+/*
+ * Automatically loads a newsrc into memory, if necessary.
+ * Checks the size/mtime of a newsrc file, if it doesn't match, load
+ * again. Hmm, if a system has broken mtimes, this might mean the file
+ * is reloaded every time, which we'd have to fix.
+ *
+ * a newsrc file is a line per newsgroup, with the newsgroup, then a
+ * ':' denoting subscribed or '!' denoting unsubscribed, then a
+ * comma separated list of article numbers and ranges.
+ */
+nntp_server_t *mutt_select_newsserver (char *server)
+{
+ char file[_POSIX_PATH_MAX];
+ char *buf, *p;
+ nntp_data_t *list;
+ ACCOUNT act;
+ nntp_server_t *serv;
+ CONNECTION *conn;
+
+ p_clear(&act, 1);
+
+ if (m_strisempty(server)) {
+ mutt_error _("No newsserver defined!");
+ return NULL;
+ }
+
+ buf = p = p_new(char, m_strlen(server) + 10);
+ if (url_check_scheme (server) == U_UNKNOWN) {
+ strcpy (buf, "nntp://");
+ p = strchr (buf, '\0');
+ }
+ strcpy (p, server);
+
+ 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;
+ }
+ p_delete(&buf);
+
+ conn = mutt_conn_find (NULL, &act);
+ if (!conn)
+ return NULL;
+
+ 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)))
+ {
+ for (list = serv->list; list; list = list->next) {
+ list->subscribed = list->rc = list->num = 0;
+ }
+ slurp_newsrc(serv);
+ }
+
+ if (serv->status == NNTP_BYE)
+ serv->status = NNTP_NONE;
+ nntp_check_newgroups(serv, 0);
+ return serv;
+ }
+
+ /* New newsserver */
+ serv = p_new(nntp_server_t, 1);
+ serv->tail = &serv->list;
+ serv->conn = conn;
+ serv->newsrc = m_strdup(file);
+ 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, NULL);
+ nntp_data_list_wipe(&serv->list);
+ p_delete(&serv->newsrc);
+ p_delete(&serv->cache);
+ p_delete(&serv);
+ return NULL;
+ }
+
+ conn->data = serv;
+ return serv;
+}
+
+/*
+ * full status flags are not supported by nntp, but we can fake some
+ * of them. This is how:
+ * Read = a read message number is in the .newsrc
+ * New = a message is new since we last read this newsgroup
+ * Old = anything else
+ * So, Read is marked as such in the newsrc, old is anything that is
+ * "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, const char *group, int article)
+{
+ 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 newsrc_gen_entries (CONTEXT * ctx)
+{
+ nntp_data_t *data = (nntp_data_t *) ctx->data;
+ int series, x;
+ int last = 0, first = 1;
+ int save_sort = SORT_ORDER;
+
+ if (Sort != SORT_ORDER) {
+ save_sort = Sort;
+ Sort = SORT_ORDER;
+ mutt_sort_headers (ctx, 0);
+ }
+
+ if (!data->max) {
+ data->entries = p_new(NEWSRC_ENTRY, 5);
+ data->max = 5;
+ }
+
+ /*
+ * Set up to fake initial sequence from 1 to the article before the
+ * first article in our list
+ */
+ data->num = 0;
+ series = 1;
+
+ for (x = 0; x < ctx->msgcount; x++) {
+ if (series) { /* search for first unread */
+ /*
+ * We don't actually check sequential order, since we mark
+ * "missing" entries as read/deleted
+ */
+ last = ctx->hdrs[x]->article_num;
+ if (last >= data->first && !ctx->hdrs[x]->deleted &&
+ !ctx->hdrs[x]->read) {
+ if (data->num >= data->max) {
+ data->max = data->max * 2;
+ p_realloc(&data->entries, data->max);
+ }
+ data->entries[data->num].first = first;
+ data->entries[data->num].last = last - 1;
+ data->num++;
+ series = 0;
+ }
+ }
+ else { /* search for first read */
+
+ if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
+ first = last + 1;
+ series = 1;
+ }
+ last = ctx->hdrs[x]->article_num;
+ }
+ }
+ if (series && first <= data->lastLoaded) {
+ if (data->num >= data->max) {
+ data->max = data->max * 2;
+ p_realloc(&data->entries, data->max);
+ }
+ data->entries[data->num].first = first;
+ data->entries[data->num].last = data->lastLoaded;
+ data->num++;
+ }
+
+ if (save_sort != Sort) {
+ Sort = save_sort;
+ mutt_sort_headers (ctx, 0);
+ }
+}
+
+static int mutt_update_list_file (char *filename, char *section,
+ const char *key, const char *line)
+{
+ FILE *ifp;
+ FILE *ofp;
+ char buf[HUGE_STRING];
+ char tmpf[_POSIX_PATH_MAX], lnk[_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")))
+ m_fclose(&ifp);
+ 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)) {
+ m_fclose(&ifp);
+ mutt_error (_("Unable to lock %s"), filename);
+ return -1;
+ }
+
+ /* use m_tempfile() 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
+ */
+ m_strcpy(buf, sizeof(buf), filename);
+ ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
+ if (!ofp) {
+ m_fclose(&ifp);
+ mutt_error (_("Unable to open %s for writing"), tmpf);
+ 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) && !m_strcasecmp(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);
+ m_fclose(&ofp);
+ m_fclose(&ifp);
+ if (r == EOF) {
+ unlink (tmpf);
+ mutt_error (_("Can't write %s"), tmpf);
+ return -1;
+ }
+ lnk[0] = '\0';
+ if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
+ lnk[l] = '\0';
+ if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
+ unlink (tmpf);
+ mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
+ return -1;
+ }
+ return 0;
+}
+
+int mutt_newsrc_update (nntp_server_t * news)
+{
+ buffer_t buf;
+ nntp_data_t *data;
+ int r = -1;
+
+ 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)
+{
+ char buf[_POSIX_PATH_MAX], *pc;
+ int fd;
+ FILE *fp;
+
+ nntp_cache_expand(buf, ssizeof(buf), "%s", s);
+ if ((fp = safe_fopen (buf, "w")))
+ return fp;
+
+ 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;
+ strcpy (s, pc); /* generated name */
+ return fdopen (fd, "w");
+}
+
+/* Updates info into .index file: ALL or about selected newsgroup */
+static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
+{
+ char buf[LONG_STRING];
+ char file[_POSIX_PATH_MAX];
+ const char *key = "ALL";
+
+ if (!serv || !serv->conn || !serv->conn->account.host)
+ return -1;
+
+ if (data && data->group) {
+ key = data->group;
+ 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);
+ }
+ 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)
+{
+ char buf[_POSIX_PATH_MAX];
+
+ if (!nntp.use_cache || !data || !data->cache || !data->nserv)
+ 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_t * news)
+{
+ char buf[HUGE_STRING];
+ char file[_POSIX_PATH_MAX];
+ nntp_data_t *d;
+ FILE *f;
+
+ if (!news || !news->newsgroups)
+ return -1;
+ if (!nntp.use_cache)
+ return 0;