+/*
+ * 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, char *group, int article)
+{
+ nntp_data_t *data = (nntp_data_t *) ctx->data;
+ int x;
+
+ if (group)
+ data = (nntp_data_t *) hash_find (data->nserv->newsgroups, group);
+
+ if (!data) {
+ return;
+ }
+
+ for (x = 0; x < data->num; x++) {
+ if ((article >= data->entries[x].first) &&
+ (article <= data->entries[x].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 mutt_newsgroup_stat (nntp_data_t * data)
+{
+ int i, first, last;
+
+ data->unread = 0;
+ if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
+ return;
+
+ data->unread = data->lastMessage - data->firstMessage + 1;
+ for (i = 0; i < data->num; i++) {
+ first = data->entries[i].first;
+ if (first < data->firstMessage)
+ first = data->firstMessage;
+ last = data->entries[i].last;
+ if (last > data->lastMessage)
+ last = data->lastMessage;
+ if (first <= last)
+ data->unread -= last - first + 1;
+ }
+}
+
+static int puti (char *line, int num)
+{
+ char *p, s[32];
+
+ for (p = s; num;) {
+ *p++ = '0' + num % 10;
+ num /= 10;
+ }
+ while (p > s)
+ *line++ = *--p, num++;
+ *line = '\0';
+ return num;
+}
+
+static void nntp_create_newsrc_line (nntp_data_t * data, char **buf,
+ char **pline, ssize_t * buflen)
+{
+ char *line = *pline;
+ ssize_t len = *buflen - (*pline - *buf);
+ int x, i;
+
+ if (len < LONG_STRING * 10) {
+ len += *buflen;
+ *buflen *= 2;
+ line = *buf;
+ p_realloc(buf, *buflen);
+ line = *buf + (*pline - line);
+ }
+ strcpy (line, data->group);
+ len -= m_strlen(line) + 1;
+ line += m_strlen(line);
+ *line++ = data->subscribed ? ':' : '!';
+ *line++ = ' ';
+ *line = '\0';
+
+ for (x = 0; x < data->num; x++) {
+ if (len < LONG_STRING) {
+ len += *buflen;
+ *buflen *= 2;
+ *pline = line;
+ line = *buf;
+ p_realloc(buf, *buflen);
+ line = *buf + (*pline - line);
+ }
+ if (x) {
+ *line++ = ',';
+ len--;
+ }
+
+ i = puti (line, data->entries[x].first);
+ line += i;
+ len -= i;
+ if (data->entries[x].first != data->entries[x].last) {
+ *line++ = '-';
+ len--;
+ i = puti (line, data->entries[x].last);
+ line += i;
+ len -= i;
+ }
+ }
+ *line++ = '\n';
+ *line = '\0';
+ *pline = line;
+}
+
+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->firstMessage && !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, 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 * news)
+{
+ char *buf, *line;
+ nntp_data_t *data;
+ int r = -1;
+ ssize_t len, llen;
+
+ if (!news)
+ return -1;
+ llen = len = 10 * LONG_STRING;
+ line = buf = p_new(char, len);
+ /* we will generate full newsrc here */
+ for (data = news->list; data; data = data->next) {
+ if (!data || !data->rc)
+ continue;
+ nntp_create_newsrc_line (data, &buf, &line, &llen);
+ line += m_strlen(line);
+ }
+ /* newrc being fully rewritten */
+ if (news->newsrc &&
+ (r = mutt_update_list_file(news->newsrc, NULL, "", buf)) == 0) {
+ struct stat st;
+
+ stat (news->newsrc, &st);
+ news->size = st.st_size;
+ news->mtime = st.st_mtime;
+ }
+ p_delete(&buf);
+ return r;
+}
+
+static FILE *mutt_mkname (char *s)
+{
+ char buf[_POSIX_PATH_MAX], *pc;
+ int fd;
+ FILE *fp;
+
+ nntp_cache_expand (buf, s);
+ if ((fp = safe_fopen (buf, "w")))
+ return fp;
+
+ nntp_cache_expand (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 * 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->firstMessage, 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, ".index");
+ return mutt_update_list_file (file, serv->conn->account.host, key, buf);
+}
+
+/* Remove cache files of unsubscribed newsgroups */
+void nntp_clear_cacheindex (NNTP_SERVER * news)
+{
+ nntp_data_t *data;
+
+ if (option (OPTSAVEUNSUB) || !news)
+ return;
+
+ for (data = news->list; data; data = data->next) {
+ if (!data || data->subscribed || !data->cache)
+ continue;
+ nntp_delete_cache (data);
+ }
+ return;
+}
+
+static int nntp_save_cache_index (NNTP_SERVER * news)
+{
+ char buf[HUGE_STRING];
+ char file[_POSIX_PATH_MAX];
+ nntp_data_t *d;
+ FILE *f;
+
+ if (!news || !news->newsgroups)
+ return -1;
+ if (!option (OPTNEWSCACHE))
+ 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);
+ news->cache = m_strdup(buf);
+ nntp_cache_expand (file, 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->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
+ d->desc);
+ else
+ snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
+ d->lastMessage, d->firstMessage, 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 (!option (OPTNEWSCACHE))
+ return 0;
+ if (!ctx || !ctx->data || ctx->magic != M_NNTP)
+ return -1;
+
+ if (((nntp_data_t *) ctx->data)->cache) {
+ nntp_cache_expand (file, ((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);
+ f = mutt_mkname (buf);
+ ((nntp_data_t *) ctx->data)->cache = m_strdup(buf);
+ nntp_cache_expand (file, 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;
+}
+
+static void nntp_delete_cache (nntp_data_t * data)
+{
+ char buf[_POSIX_PATH_MAX];
+
+ if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
+ return;
+
+ nntp_cache_expand (buf, 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_data_t *mutt_newsgroup_subscribe (NNTP_SERVER * 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 = xmalloc(sizeof(nntp_data_t) + m_strlen(group) + 1);
+ data->group = (char *) data + sizeof (nntp_data_t);
+ strcpy (data->group, group);
+ data->nserv = news;
+ data->deleted = 1;
+ if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
+ hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
+ hash_insert (news->newsgroups, data->group, data);
+ nntp_data_list_append(&news->list, data);
+ }
+ if (!data->subscribed) {
+ data->subscribed = 1;
+ data->rc = 1;
+ }
+ return data;
+}
+
+nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER * 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;
+ if (!option (OPTSAVEUNSUB))
+ data->rc = 0;
+ }
+ return data;
+}
+
+nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER * 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->lastMessage;
+ 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 * 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->firstMessage - 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->lastMessage - 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 {{{ */
+
+static unsigned int _checked = 0;
+
+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->firstMessage &&
+ data->lastCached <= data->lastMessage)
+ tmp->msg_unread = data->lastMessage - 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->lastMessage - data->firstMessage;
+}
+
+static int nntp_auth (NNTP_SERVER * serv)