+/*
+ * 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;
+
+ if (news->cache) {
+ 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, ssizeof(file), "%s", buf);
+ }
+ if (!f)
+ return -1;
+
+ for (d = news->list; d; d = d->next) {
+ if (!d->deleted) {
+ if (d->desc)
+ snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
+ d->last, d->first, d->allowed ? 'y' : 'n',
+ d->desc);
+ else
+ snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
+ d->last, d->first, d->allowed ? 'y' : 'n');
+ if (fputs (buf, f) == EOF) {
+ m_fclose(&f);
+ unlink (file);
+ return -1;
+ }
+ }
+ }
+ m_fclose(&f);
+
+ if (nntp_update_cacheindex (news, NULL)) {
+ unlink (file);
+ return -1;
+ }
+ return 0;
+}
+
+static int nntp_save_cache_group (CONTEXT * ctx)
+{
+ char buf[HUGE_STRING], addr[STRING];
+ char file[_POSIX_PATH_MAX];
+ FILE *f;
+ HEADER *h;
+ struct tm *tm;
+ int i = 0, save = SORT_ORDER;
+ int prev = 0;
+
+ 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, ssizeof(file), "%s",
+ ((nntp_data_t *)ctx->data)->cache);
+ unlink (file);
+ f = safe_fopen (file, "w");
+ }
+ else {
+ 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, ssizeof(file), "%s", buf);
+ }
+ if (!f)
+ return -1;
+
+ if (Sort != SORT_ORDER) {
+ save = Sort;
+ Sort = SORT_ORDER;
+ mutt_sort_headers (ctx, 0);
+ }
+
+ /* Save only $nntp_context messages... */
+ ((nntp_data_t *) ctx->data)->lastCached = 0;
+ if (NntpContext && ctx->msgcount > NntpContext)
+ i = ctx->msgcount - NntpContext;
+ for (; i < ctx->msgcount; i++) {
+ if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
+ h = ctx->hdrs[i];
+ addr[0] = 0;
+ rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
+ tm = gmtime (&h->date_sent);
+ snprintf (buf, sizeof (buf),
+ "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
+ h->article_num, h->env->subject, addr, tm->tm_mday,
+ Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
+ tm->tm_min, tm->tm_sec, h->env->message_id);
+ fputs (buf, f);
+ if (h->env->references)
+ mutt_write_references (h->env->references, f);
+ 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) {
+ m_fclose(&f);
+ unlink (file);
+ return -1;
+ }
+ }
+ prev = ctx->hdrs[i]->article_num;
+ }
+
+ if (save != Sort) {
+ Sort = save;
+ mutt_sort_headers (ctx, 0);
+ }
+ m_fclose(&f);
+
+ if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
+ (nntp_data_t *) ctx->data)) {
+ unlink (file);
+ return -1;
+ }
+ ((nntp_data_t *) ctx->data)->lastCached =
+ ((nntp_data_t *) ctx->data)->lastLoaded;
+ return 0;
+}
+
+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 = nntp_data_new();
+ data->group = m_strdup(group);
+ data->nserv = news;
+ data->deleted = 1;
+ hash_insert (news->newsgroups, data->group, data);
+ news->tail = nntp_data_list_append(news->tail, data);
+ }
+ if (!data->subscribed) {
+ data->subscribed = 1;
+ data->rc = 1;
+ }
+ return data;
+}
+
+nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
+{
+ nntp_data_t *data;
+
+ if (!news || !news->newsgroups || !group || !*group ||
+ !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
+ return NULL;
+ if (data->subscribed) {
+ data->subscribed = 0;
+ }
+ return data;
+}
+
+nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
+{
+ nntp_data_t *data;
+
+ if (!news || !news->newsgroups || !group || !*group ||
+ !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
+ return NULL;
+ if (!data->max) {
+ data->entries = p_new(NEWSRC_ENTRY, 5);
+ data->max = 5;
+ }
+ data->num = 1;
+ data->entries[0].first = 1;
+ data->unread = 0;
+ data->entries[0].last = data->last;
+ if (Context && Context->data == data) {
+ int x;
+
+ for (x = 0; x < Context->msgcount; x++)
+ mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
+ }
+ return data;
+}
+
+nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
+{
+ nntp_data_t *data;
+
+ if (!news || !news->newsgroups || !group || !*group ||
+ !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
+ return NULL;
+ if (!data->max) {
+ data->entries = p_new(NEWSRC_ENTRY, 5);
+ data->max = 5;
+ }
+ data->num = 1;
+ data->entries[0].first = 1;
+ data->entries[0].last = data->first - 1;
+ if (Context && Context->data == data) {
+ int x;
+
+ data->unread = Context->msgcount;
+ for (x = 0; x < Context->msgcount; x++)
+ mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
+ }
+ else
+ data->unread = data->last - data->entries[0].last;
+ return data;
+}
+
+/* this routine gives the first newsgroup with new messages */
+void nntp_buffy (char* dst, ssize_t dstlen) {
+ nntp_data_t *list;
+ int count = 0;
+
+ /* forward to current group */
+ for (list = CurrentNewsSrv->list; list; list = list->next) {
+ if (list->subscribed && list->unread
+ && Context && Context->magic == M_NNTP
+ && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
+ {
+ list = list->next;
+ break;
+ }
+ }
+
+ *dst = '\0';
+
+ while (count < 2) {
+
+ if (!list)
+ list = CurrentNewsSrv->list;
+
+ for (; list; list = list->next) {
+ if (list->subscribed && list->unread) {
+ if (Context && Context->magic == M_NNTP &&
+ !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
+ int i, unread = 0;
+
+ for (i = 0; i < Context->msgcount; i++)
+ if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
+ unread++;
+ if (!unread)
+ continue;
+ }
+ m_strcpy(dst, dstlen, list->group);
+ break;
+ }
+ }
+ /* done if found */
+ if (dst && *dst)
+ return;
+ count++;
+ }
+ *dst = '\0';
+}
+
+/* }}} */
+/* nntp protocol {{{ */
+
+void nntp_sync_sidebar (nntp_data_t* data) {
+ int i = 0;
+ BUFFY* tmp = NULL;
+ char buf[STRING];
+
+ if (!Incoming.len)
+ return;
+
+ snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
+ data->nserv->conn->account.has_ssl ? "s" : "",
+ NONULL(data->nserv->conn->account.user),
+ *data->nserv->conn->account.pass ? ":" : "",
+ *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
+ data->nserv->conn->account.host,
+ data->group);
+
+ /* bail out if group not found via mailboxes */
+ if ((i = buffy_lookup (buf)) < 0)
+ return;
+
+ tmp = Incoming.arr[i];
+ /* copied from browser.c */
+ if (option (OPTMARKOLD) &&
+ data->lastCached >= data->first &&
+ data->lastCached <= data->last)
+ tmp->msg_unread = data->last - data->lastCached;
+ else
+ tmp->msg_unread = data->unread;
+ tmp->new = data->unread > 0;
+ /* this is closest to a "total" count we can get */
+ tmp->msgcount = data->last - data->first;
+}
+
+static int nntp_auth (nntp_server_t * serv)