2 * Copyright notice from original mutt:
3 * Copyright (C) 1998 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
5 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 #include <lib-lib/lib-lib.h>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/sidebar.h>
19 #include <lib-mx/mx.h>
28 #define NNTP_SSL_PORT 563
30 static int nntp_get_cache_all (NNTP_SERVER *);
31 static int nntp_check_newgroups (NNTP_SERVER *, int);
32 static void mutt_newsgroup_stat (nntp_data_t *);
33 static void nntp_delete_cache (nntp_data_t *);
37 static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
40 char group[LONG_STRING];
44 for (p = line; p; p = strchr(p, ',')) {
48 p = strpbrk(line, ":!");
52 m_strncpy(group, ssizeof(group), line, p - line);
53 if ((data = hash_find(news->newsgroups, group)) == NULL) {
54 data = nntp_data_new();
55 data->group = m_strdup(group);
58 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
59 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
60 hash_insert(news->newsgroups, data->group, data);
61 nntp_data_list_append(&news->list, data);
63 p_delete(&data->entries);
67 data->entries = p_new(NEWSRC_ENTRY, x * 2);
70 data->subscribed = (*p++ == ':');
75 while (*p && *p != ',' && *p != '\n')
81 if ((h = strchr (b, '-'))) {
84 data->entries[x].first = atoi (b);
85 data->entries[x].last = atoi (h);
88 data->entries[x].first = atoi (b);
89 data->entries[x].last = data->entries[x].first;
92 if (data->entries[x].last != 0)
95 if (x && !data->lastMessage)
96 data->lastMessage = data->entries[x - 1].last;
98 mutt_newsgroup_stat (data);
103 static int slurp_newsrc (NNTP_SERVER * news)
109 news->stat = stat (news->newsrc, &sb);
110 news->size = sb.st_size;
111 news->mtime = sb.st_mtime;
113 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
115 /* hmm, should we use dotlock? */
116 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1)) {
121 buf = p_new(char, sb.st_size + 1);
122 while (fgets (buf, sb.st_size + 1, fp))
123 nntp_parse_newsrc_line (news, buf);
126 mx_unlock_file (news->newsrc, fileno (fp), 0);
131 static void nntp_cache_expand (char *dst, const char *src)
133 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
134 mutt_expand_path (dst, _POSIX_PATH_MAX);
137 /* Loads $news_cache_dir/.index into memory, loads newsserver data
138 * and newsgroup cache names */
139 static int nntp_parse_cacheindex (NNTP_SERVER * news)
142 char buf[HUGE_STRING], *cp;
143 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
148 /* check is server name defined or not */
149 if (!news || !news->conn || !news->conn->account.host)
151 unset_option (OPTNEWSCACHE);
152 if (m_strisempty(NewsCacheDir))
155 m_strcpy(dir, sizeof(dir), NewsCacheDir);
156 mutt_expand_path (dir, sizeof (dir));
158 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
159 snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"),
161 if (mutt_yesorno (buf, M_YES) != M_YES
162 || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) {
163 mutt_error _("Cache directory not created!");
170 set_option (OPTNEWSCACHE);
172 p_delete(&news->cache);
173 snprintf (buf, sizeof (buf), "%s/.index", dir);
174 if (!(idx = safe_fopen (buf, "a+")))
177 while (fgets (buf, sizeof (buf), idx)) {
178 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
179 if (!m_strncmp(buf, "#: ", 3) &&
180 !m_strcasecmp(buf + 3, news->conn->account.host))
183 while (fgets (buf, sizeof (buf), idx)) {
185 while (*cp && *cp != ' ')
190 if (!m_strcmp(buf, "#:"))
192 sscanf (cp + 1, "%s %d %d", file, &l, &m);
193 if (!m_strcmp(buf, "ALL")) {
194 news->cache = m_strdup(file);
195 news->newgroups_time = m;
197 else if (news->newsgroups) {
198 if ((data = hash_find (news->newsgroups, buf)) == NULL) {
199 data = nntp_data_new();
200 data->group = m_strdup(buf);
203 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
204 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
205 hash_insert (news->newsgroups, data->group, data);
206 nntp_data_list_append(&news->list, data);
208 data->cache = m_strdup(file);
210 if (!data->firstMessage || data->lastMessage < m)
212 if (!data->firstMessage)
213 data->firstMessage = l;
214 if (data->lastMessage < m)
215 data->lastMessage = m;
216 data->lastCached = m;
217 if (t || !data->unread)
218 mutt_newsgroup_stat (data);
225 const char *nntp_format_str(char *dest, ssize_t destlen, char op,
226 const char *src, const char *fmt,
227 const char *ifstr __attribute__((unused)),
228 const char *elstr __attribute__((unused)),
229 anytype data __attribute__((unused)),
230 format_flag flags __attribute__((unused)))
232 char fn[STRING], tmp[STRING];
236 m_strcpy(fn, sizeof (fn), NewsServer);
238 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
239 snprintf (dest, destlen, tmp, fn);
245 /* nntp_parse_url: given an NNPT URL, return host, port,
246 * username, password and newsgroup will recognise. */
247 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
256 act->port = NNTP_PORT;
257 act->type = M_ACCT_TYPE_NNTP;
259 c = m_strdup(server);
260 url_parse_ciss (&url, c);
262 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
263 if (url.scheme == U_NNTPS) {
265 act->port = NNTP_SSL_PORT;
270 m_strcpy(group, group_len, url.path);
272 ret = mutt_account_fromurl (act, &url);
279 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
283 url.path = m_strdup(line);
284 mutt_account_tourl (act, &url);
285 url_ciss_tostring (&url, line, len, 0);
290 * Automatically loads a newsrc into memory, if necessary.
291 * Checks the size/mtime of a newsrc file, if it doesn't match, load
292 * again. Hmm, if a system has broken mtimes, this might mean the file
293 * is reloaded every time, which we'd have to fix.
295 * a newsrc file is a line per newsgroup, with the newsgroup, then a
296 * ':' denoting subscribed or '!' denoting unsubscribed, then a
297 * comma separated list of article numbers and ranges.
299 NNTP_SERVER *mutt_select_newsserver (char *server)
301 char file[_POSIX_PATH_MAX];
310 if (!server || !*server) {
311 mutt_error _("No newsserver defined!");
316 buf = p = p_new(char, m_strlen(server) + 10);
317 if (url_check_scheme (server) == U_UNKNOWN) {
318 strcpy (buf, "nntp://");
319 p = strchr (buf, '\0');
323 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
325 mutt_error (_("%s is an invalid newsserver specification!"), server);
330 conn = mutt_conn_find (NULL, &act);
334 m_strformat(file, sizeof(file), 0, NewsRc, nntp_format_str, NULL, 0);
335 mutt_expand_path(file, sizeof(file));
337 serv = (NNTP_SERVER *) conn->data;
341 /* externally modified? */
342 if (serv->stat != stat(file, &sb) || (!serv->stat &&
343 (serv->size != sb.st_size
344 || serv->mtime != sb.st_mtime)))
346 for (list = serv->list; list; list = list->next) {
347 list->subscribed = 0;
352 nntp_clear_cacheindex (serv);
355 if (serv->status == NNTP_BYE)
356 serv->status = NNTP_NONE;
357 nntp_check_newgroups (serv, 0);
362 serv = p_new(NNTP_SERVER, 1);
364 serv->newsrc = m_strdup(file);
365 serv->newsgroups = hash_new(1009, false);
366 slurp_newsrc (serv); /* load .newsrc */
367 nntp_parse_cacheindex (serv); /* load .index */
368 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
369 nntp_check_newgroups (serv, 1);
370 else if (nntp_get_active (serv) < 0) {
371 hash_delete(&serv->newsgroups, NULL);
372 nntp_data_list_wipe(&serv->list);
373 p_delete(&serv->newsrc);
374 p_delete(&serv->cache);
378 nntp_clear_cacheindex (serv);
379 conn->data = (void *) serv;
385 * full status flags are not supported by nntp, but we can fake some
386 * of them. This is how:
387 * Read = a read message number is in the .newsrc
388 * New = a message is new since we last read this newsgroup
389 * Old = anything else
390 * So, Read is marked as such in the newsrc, old is anything that is
391 * "skipped" in the newsrc, and new is anything not in the newsrc nor
392 * in the cache. By skipped, I mean before the last unread message
394 static void nntp_get_status(CONTEXT *ctx, HEADER *h, char *group, int article)
396 nntp_data_t *data = (nntp_data_t *) ctx->data;
400 data = (nntp_data_t *) hash_find (data->nserv->newsgroups, group);
406 for (x = 0; x < data->num; x++) {
407 if ((article >= data->entries[x].first) &&
408 (article <= data->entries[x].last)) {
409 /* we cannot use mutt_set_flag() because mx_update_context()
415 /* If article was not cached yet, it is new! :) */
416 if (!data->cache || article > data->lastCached)
418 /* Old articles are articles which aren't read but an article after them
420 if (option (OPTMARKOLD))
424 static void mutt_newsgroup_stat (nntp_data_t * data)
429 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
432 data->unread = data->lastMessage - data->firstMessage + 1;
433 for (i = 0; i < data->num; i++) {
434 first = data->entries[i].first;
435 if (first < data->firstMessage)
436 first = data->firstMessage;
437 last = data->entries[i].last;
438 if (last > data->lastMessage)
439 last = data->lastMessage;
441 data->unread -= last - first + 1;
445 static int puti (char *line, int num)
450 *p++ = '0' + num % 10;
454 *line++ = *--p, num++;
459 static void nntp_create_newsrc_line (nntp_data_t * data, char **buf,
460 char **pline, ssize_t * buflen)
463 ssize_t len = *buflen - (*pline - *buf);
466 if (len < LONG_STRING * 10) {
470 p_realloc(buf, *buflen);
471 line = *buf + (*pline - line);
473 strcpy (line, data->group);
474 len -= m_strlen(line) + 1;
475 line += m_strlen(line);
476 *line++ = data->subscribed ? ':' : '!';
480 for (x = 0; x < data->num; x++) {
481 if (len < LONG_STRING) {
486 p_realloc(buf, *buflen);
487 line = *buf + (*pline - line);
494 i = puti (line, data->entries[x].first);
497 if (data->entries[x].first != data->entries[x].last) {
500 i = puti (line, data->entries[x].last);
510 static void newsrc_gen_entries (CONTEXT * ctx)
512 nntp_data_t *data = (nntp_data_t *) ctx->data;
514 int last = 0, first = 1;
515 int save_sort = SORT_ORDER;
517 if (Sort != SORT_ORDER) {
520 mutt_sort_headers (ctx, 0);
524 data->entries = p_new(NEWSRC_ENTRY, 5);
529 * Set up to fake initial sequence from 1 to the article before the
530 * first article in our list
535 for (x = 0; x < ctx->msgcount; x++) {
536 if (series) { /* search for first unread */
538 * We don't actually check sequential order, since we mark
539 * "missing" entries as read/deleted
541 last = ctx->hdrs[x]->article_num;
542 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
543 !ctx->hdrs[x]->read) {
544 if (data->num >= data->max) {
545 data->max = data->max * 2;
546 p_realloc(&data->entries, data->max);
548 data->entries[data->num].first = first;
549 data->entries[data->num].last = last - 1;
554 else { /* search for first read */
556 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
560 last = ctx->hdrs[x]->article_num;
563 if (series && first <= data->lastLoaded) {
564 if (data->num >= data->max) {
565 data->max = data->max * 2;
566 p_realloc(&data->entries, data->max);
568 data->entries[data->num].first = first;
569 data->entries[data->num].last = data->lastLoaded;
573 if (save_sort != Sort) {
575 mutt_sort_headers (ctx, 0);
579 static int mutt_update_list_file (char *filename, char *section,
580 const char *key, char *line) {
583 char buf[HUGE_STRING];
584 char tmpf[_POSIX_PATH_MAX], lnk[_POSIX_PATH_MAX];
586 int ext = 0, done = 0, r = 0, l = 0;
588 /* if file not exist, create it */
589 if ((ifp = safe_fopen (filename, "a")))
591 if (!(ifp = safe_fopen (filename, "r"))) {
592 mutt_error (_("Unable to open %s for reading"), filename);
595 if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) {
597 mutt_error (_("Unable to lock %s"), filename);
601 /* use m_tempfile() to get a tempfile in the same
602 * directory as filename is so that we can follow symlinks
603 * via rename(2); as dirname(2) may modify its argument,
604 * temporarily use buf as copy of it
606 m_strcpy(buf, sizeof(buf), filename);
607 ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
610 mutt_error (_("Unable to open %s for writing"), tmpf);
615 while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) {
616 r = fputs (buf, ofp);
618 while (*c && *c != '\n') c++;
619 c[0] = 0; /* strip EOL */
620 if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
623 if (r != EOF && !done) {
624 snprintf (buf, sizeof(buf), "#: %s\n", section);
625 r = fputs (buf, ofp);
630 while (r != EOF && fgets (buf, sizeof (buf), ifp)) {
633 while (*c && (*c != '\r') && (*c != '\n')) c++;
635 if (*c != '\\') ext = 0;
636 } else if ((section && !strncmp (buf, "#: ", 3))) {
641 r = fputs (buf, ofp);
644 } else if (key && !strncmp (buf, key, strlen(key)) &&
645 (!*key || buf[strlen(key)] == ' ')) {
648 while (*c && (*c != '\r') && (*c != '\n')) c++;
650 if (*c == '\\') ext = 1;
652 r = fputs (line, ofp);
654 r = fputc ('\n', ofp);
658 r = fputs (buf, ofp);
662 while (r != EOF && fgets (buf, sizeof (buf), ifp))
663 r = fputs (buf, ofp);
665 /* If there wasn't a line to replace, put it on the end of the file */
666 if (r != EOF && !done && line) {
668 r = fputc ('\n', ofp);
670 mx_unlock_file (filename, fileno (ifp), 0);
675 mutt_error (_("Can't write %s"), tmpf);
679 if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
681 if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
683 mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
689 int mutt_newsrc_update (NNTP_SERVER * news)
698 llen = len = 10 * LONG_STRING;
699 line = buf = p_new(char, len);
700 /* we will generate full newsrc here */
701 for (data = news->list; data; data = data->next) {
702 if (!data || !data->rc)
704 nntp_create_newsrc_line (data, &buf, &line, &llen);
705 line += m_strlen(line);
707 /* newrc being fully rewritten */
709 (r = mutt_update_list_file(news->newsrc, NULL, "", buf)) == 0) {
712 stat (news->newsrc, &st);
713 news->size = st.st_size;
714 news->mtime = st.st_mtime;
720 static FILE *mutt_mkname (char *s)
722 char buf[_POSIX_PATH_MAX], *pc;
726 nntp_cache_expand (buf, s);
727 if ((fp = safe_fopen (buf, "w")))
730 nntp_cache_expand (buf, "cache-XXXXXX");
731 pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
732 if ((fd = mkstemp (buf)) == -1)
734 strcpy (s, pc); /* generated name */
735 return fdopen (fd, "w");
738 /* Updates info into .index file: ALL or about selected newsgroup */
739 static int nntp_update_cacheindex (NNTP_SERVER * serv, nntp_data_t * data)
741 char buf[LONG_STRING];
742 char file[_POSIX_PATH_MAX];
743 const char *key = "ALL";
745 if (!serv || !serv->conn || !serv->conn->account.host)
748 if (data && data->group) {
750 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
751 data->firstMessage, data->lastLoaded);
754 m_strcpy(file, sizeof(file), serv->cache);
755 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
756 (int) serv->newgroups_time);
758 nntp_cache_expand (file, ".index");
759 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
762 /* Remove cache files of unsubscribed newsgroups */
763 void nntp_clear_cacheindex (NNTP_SERVER * news)
767 if (option (OPTSAVEUNSUB) || !news)
770 for (data = news->list; data; data = data->next) {
771 if (!data || data->subscribed || !data->cache)
773 nntp_delete_cache (data);
778 static int nntp_save_cache_index (NNTP_SERVER * news)
780 char buf[HUGE_STRING];
781 char file[_POSIX_PATH_MAX];
785 if (!news || !news->newsgroups)
787 if (!option (OPTNEWSCACHE))
791 nntp_cache_expand (file, news->cache);
793 f = safe_fopen (file, "w");
796 m_strcpy(buf, sizeof(buf), news->conn->account.host);
797 f = mutt_mkname (buf);
798 news->cache = m_strdup(buf);
799 nntp_cache_expand (file, buf);
804 for (d = news->list; d; d = d->next) {
807 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
808 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
811 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
812 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
813 if (fputs (buf, f) == EOF) {
822 if (nntp_update_cacheindex (news, NULL)) {
829 static int nntp_save_cache_group (CONTEXT * ctx)
831 char buf[HUGE_STRING], addr[STRING];
832 char file[_POSIX_PATH_MAX];
836 int i = 0, save = SORT_ORDER;
839 if (!option (OPTNEWSCACHE))
841 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
844 if (((nntp_data_t *) ctx->data)->cache) {
845 nntp_cache_expand (file, ((nntp_data_t *) ctx->data)->cache);
847 f = safe_fopen (file, "w");
850 snprintf (buf, sizeof (buf), "%s-%s",
851 ((nntp_data_t *) ctx->data)->nserv->conn->account.host,
852 ((nntp_data_t *) ctx->data)->group);
853 f = mutt_mkname (buf);
854 ((nntp_data_t *) ctx->data)->cache = m_strdup(buf);
855 nntp_cache_expand (file, buf);
860 if (Sort != SORT_ORDER) {
863 mutt_sort_headers (ctx, 0);
866 /* Save only $nntp_context messages... */
867 ((nntp_data_t *) ctx->data)->lastCached = 0;
868 if (NntpContext && ctx->msgcount > NntpContext)
869 i = ctx->msgcount - NntpContext;
870 for (; i < ctx->msgcount; i++) {
871 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
874 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
875 tm = gmtime (&h->date_sent);
876 snprintf (buf, sizeof (buf),
877 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
878 h->article_num, h->env->subject, addr, tm->tm_mday,
879 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
880 tm->tm_min, tm->tm_sec, h->env->message_id);
882 if (h->env->references)
883 mutt_write_references (h->env->references, f);
884 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
885 h->content->length, h->lines, NONULL (h->env->xref));
886 if (fputs (buf, f) == EOF) {
892 prev = ctx->hdrs[i]->article_num;
897 mutt_sort_headers (ctx, 0);
901 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
902 (nntp_data_t *) ctx->data)) {
906 ((nntp_data_t *) ctx->data)->lastCached =
907 ((nntp_data_t *) ctx->data)->lastLoaded;
911 static void nntp_delete_cache (nntp_data_t * data)
913 char buf[_POSIX_PATH_MAX];
915 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
918 nntp_cache_expand (buf, data->cache);
920 p_delete(&data->cache);
921 data->lastCached = 0;
922 nntp_cache_expand (buf, ".index");
923 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
927 nntp_data_t *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
931 if (!news || !news->newsgroups || !group || !*group)
933 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
934 data = nntp_data_new();
935 data->group = m_strdup(group);
938 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
939 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
940 hash_insert (news->newsgroups, data->group, data);
941 nntp_data_list_append(&news->list, data);
943 if (!data->subscribed) {
944 data->subscribed = 1;
950 nntp_data_t *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group)
954 if (!news || !news->newsgroups || !group || !*group ||
955 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
957 if (data->subscribed) {
958 data->subscribed = 0;
959 if (!option (OPTSAVEUNSUB))
965 nntp_data_t *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
969 if (!news || !news->newsgroups || !group || !*group ||
970 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
973 data->entries = p_new(NEWSRC_ENTRY, 5);
977 data->entries[0].first = 1;
979 data->entries[0].last = data->lastMessage;
980 if (Context && Context->data == data) {
983 for (x = 0; x < Context->msgcount; x++)
984 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
989 nntp_data_t *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
993 if (!news || !news->newsgroups || !group || !*group ||
994 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
997 data->entries = p_new(NEWSRC_ENTRY, 5);
1001 data->entries[0].first = 1;
1002 data->entries[0].last = data->firstMessage - 1;
1003 if (Context && Context->data == data) {
1006 data->unread = Context->msgcount;
1007 for (x = 0; x < Context->msgcount; x++)
1008 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
1011 data->unread = data->lastMessage - data->entries[0].last;
1015 /* this routine gives the first newsgroup with new messages */
1016 void nntp_buffy (char* dst, ssize_t dstlen) {
1020 /* forward to current group */
1021 for (list = CurrentNewsSrv->list; list; list = list->next) {
1022 if (list->subscribed && list->unread
1023 && Context && Context->magic == M_NNTP
1024 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
1036 list = CurrentNewsSrv->list;
1038 for (; list; list = list->next) {
1039 if (list->subscribed && list->unread) {
1040 if (Context && Context->magic == M_NNTP &&
1041 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1044 for (i = 0; i < Context->msgcount; i++)
1045 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1050 m_strcpy(dst, dstlen, list->group);
1063 /* nntp protocol {{{ */
1065 static unsigned int _checked = 0;
1067 void nntp_sync_sidebar (nntp_data_t* data) {
1075 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1076 data->nserv->conn->account.has_ssl ? "s" : "",
1077 NONULL(data->nserv->conn->account.user),
1078 *data->nserv->conn->account.pass ? ":" : "",
1079 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1080 data->nserv->conn->account.host,
1083 /* bail out if group not found via mailboxes */
1084 if ((i = buffy_lookup (buf)) < 0)
1087 tmp = Incoming.arr[i];
1088 /* copied from browser.c */
1089 if (option (OPTMARKOLD) &&
1090 data->lastCached >= data->firstMessage &&
1091 data->lastCached <= data->lastMessage)
1092 tmp->msg_unread = data->lastMessage - data->lastCached;
1094 tmp->msg_unread = data->unread;
1095 tmp->new = data->unread > 0;
1096 /* this is closest to a "total" count we can get */
1097 tmp->msgcount = data->lastMessage - data->firstMessage;
1100 static int nntp_auth (NNTP_SERVER * serv)
1102 CONNECTION *conn = serv->conn;
1104 unsigned char flags = conn->account.flags;
1106 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1107 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1108 conn->account.flags = flags;
1112 mutt_message _("Logging in...");
1114 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1115 mutt_socket_write (conn, buf);
1116 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1117 conn->account.flags = flags;
1121 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1122 mutt_socket_write(conn, buf);
1123 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1124 conn->account.flags = flags;
1128 if (m_strncmp("281", buf, 3)) {
1129 conn->account.flags = flags;
1130 mutt_error _("Login failed.");
1139 static int nntp_connect_error (NNTP_SERVER * serv)
1141 serv->status = NNTP_NONE;
1142 mutt_socket_close (serv->conn);
1143 mutt_error _("Server closed connection!");
1149 static int nntp_connect_and_auth (NNTP_SERVER * serv)
1151 CONNECTION *conn = serv->conn;
1155 serv->status = NNTP_NONE;
1157 if (mutt_socket_open (conn) < 0)
1160 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1161 return nntp_connect_error (serv);
1163 if (!m_strncmp("200", buf, 3))
1164 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1165 else if (!m_strncmp("201", buf, 3))
1166 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1168 mutt_socket_close(conn);
1170 mutt_error("%s", buf);
1175 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1176 returned codes and messages. */
1177 mutt_socket_write (conn, "MODE READER\r\n");
1178 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1179 return nntp_connect_error (serv);
1181 mutt_socket_write (conn, "STAT\r\n");
1182 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1183 return nntp_connect_error (serv);
1185 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1186 serv->status = NNTP_OK;
1190 rc = nntp_auth (serv);
1192 return nntp_connect_error (serv);
1194 mutt_socket_close (conn);
1195 serv->status = NNTP_BYE;
1199 mutt_socket_close (conn);
1200 mutt_error _("Login failed.");
1205 serv->status = NNTP_OK;
1209 static int nntp_attempt_features (NNTP_SERVER * serv)
1211 char buf[LONG_STRING];
1212 CONNECTION *conn = serv->conn;
1214 if (serv->feat_known)
1217 mutt_socket_write (conn, "XOVER\r\n");
1218 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1219 return nntp_connect_error (serv);
1220 if (m_strncmp("500", buf, 3))
1223 mutt_socket_write (conn, "XPAT\r\n");
1224 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1225 return nntp_connect_error (serv);
1226 if (m_strncmp("500", buf, 3))
1229 mutt_socket_write (conn, "LISTGROUP\r\n");
1230 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1231 return (nntp_connect_error (serv));
1232 if (m_strncmp("500", buf, 3))
1233 serv->hasLISTGROUP = 1;
1235 mutt_socket_write (conn, "XGTITLE +\r\n");
1236 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1237 return nntp_connect_error (serv);
1238 if (m_strncmp("500", buf, 3))
1239 serv->hasXGTITLE = 1;
1241 if (!m_strncmp("282", buf, 3)) {
1243 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1244 return nntp_connect_error (serv);
1245 } while (!(buf[0] == '.' && buf[1] == '\0'));
1248 serv->feat_known = 1;
1252 static int nntp_open_connection (NNTP_SERVER * serv)
1254 if (serv->status == NNTP_OK)
1256 if (serv->status == NNTP_BYE)
1258 if (nntp_connect_and_auth (serv) < 0)
1260 if (nntp_attempt_features (serv) < 0)
1265 static int nntp_reconnect (NNTP_SERVER * serv)
1269 mutt_socket_close (serv->conn);
1272 if (nntp_connect_and_auth (serv) == 0)
1275 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1276 serv->conn->account.host);
1277 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1278 serv->status = NNTP_BYE;
1284 /* Send data from line[LONG_STRING] and receive answer to same line */
1285 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1287 char buf[LONG_STRING];
1290 if (data->nserv->status == NNTP_BYE)
1295 mutt_socket_write (data->nserv->conn, line);
1297 else if (data->group) {
1298 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1299 mutt_socket_write (data->nserv->conn, buf);
1303 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1304 if (nntp_reconnect (data->nserv) < 0)
1308 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1309 mutt_socket_write (data->nserv->conn, buf);
1310 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1316 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1320 m_strcpy(line, linelen, buf);
1325 * This function calls funct(*line, *data) for each received line,
1326 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1329 * 1 - correct but not performed (may be, have to be continued),
1330 * -1 - conection lost,
1331 * -2 - invalid command or execution error,
1332 * -3 - error in funct(*line, *data).
1334 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1335 const char *msg, progress_t* bar,
1336 int (*funct) (char *, void *),
1337 void *data, int tagged)
1339 char buf[LONG_STRING];
1348 m_strcpy(buf, sizeof(buf), query);
1349 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1358 inbuf = p_new(char, sizeof(buf));
1361 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1366 if (!lenbuf && buf[0] == '.') {
1367 if (buf[1] == '\0') {
1375 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1378 if (chunk >= ssizeof (buf)) {
1379 lenbuf += m_strlen(p);
1383 mutt_progress_bar (bar, pos);
1386 if (ReadInc && (line % ReadInc == 0)) {
1388 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1390 mutt_message ("%s %d", msg, line);
1394 if (ret == 0 && funct (inbuf, data) < 0)
1399 p_realloc(&inbuf, lenbuf + sizeof (buf));
1408 static int nntp_read_tempfile (char *line, void *file)
1410 FILE *f = (FILE *) file;
1416 if (fputc ('\n', f) == EOF)
1422 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1425 register char *p, *b;
1426 register char *colon = NULL;
1430 /* skip to next word */
1432 while (*b && ((*b == ' ') || (*b == '\t')))
1436 /* skip to end of word */
1437 while (*p && (*p != ' ') && (*p != '\t')) {
1449 nntp_get_status (ctx, h, b, atoi (colon));
1450 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1451 h->article_num = atoi (colon);
1459 * 1 if article not found
1460 * -1 if read or write error on tempfile or socket
1462 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1465 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1467 char buf[LONG_STRING];
1468 char tempfile[_POSIX_PATH_MAX];
1470 HEADER *h = ctx->hdrs[ctx->msgcount];
1472 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1477 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1479 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1481 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1485 return (ret == -1 ? -1 : 1);
1488 h->article_num = article_num;
1489 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1493 if (h->env->xref != NULL)
1494 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1495 else if (h->article_num == 0 && msgid) {
1496 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1497 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1498 h->article_num = atoi (buf + 4);
1504 static int parse_description (char *line, void *n)
1506 register char *d = line;
1508 NNTP_SERVER *news = n;
1512 while (*d && *d != '\t' && *d != ' ')
1516 while (*d && (*d == '\t' || *d == ' '))
1518 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1519 m_strcmp(d, data->desc)) {
1520 p_delete(&data->desc);
1521 data->desc = m_strdup(d);
1526 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1530 if (!option (OPTLOADDESC) || !data || !data->nserv)
1533 /* Get newsgroup description, if we can */
1534 if (data->nserv->hasXGTITLE)
1535 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1537 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1538 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
1544 * XOVER returns a tab separated list of:
1545 * id|subject|from|date|Msgid|references|bytes|lines|xref
1547 * This has to duplicate some of the functionality of
1548 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1549 * a limited number of headers which are "parsed" by placement in the list)
1551 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1553 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1557 hdr->env = envelope_new();
1558 hdr->env->newsgroups = m_strdup(nntp_data->group);
1559 hdr->content = body_new();
1560 hdr->content->type = TYPETEXT;
1561 hdr->content->subtype = m_strdup("plain");
1562 hdr->content->encoding = ENC7BIT;
1563 hdr->content->disposition = DISPINLINE;
1564 hdr->content->length = -1;
1567 for (x = 0; !done && x < 9; x++) {
1568 /* if from file, need to skip newline character */
1569 while (*p && *p != '\n' && *p != '\t')
1578 hdr->article_num = atoi (b);
1579 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1582 hdr->env->subject = m_strdup(b);
1585 address_list_wipe(&hdr->env->from);
1586 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1587 /* same as for mutt_parse_rfc822_line():
1588 * don't leave from info NULL if there's an invalid address (or
1589 * whatever) in From: field; mutt would just display it as empty
1590 * and mark mail/(esp.) news article as your own. aaargh! this
1591 * bothered me for _years_ */
1592 if (!hdr->env->from) {
1593 hdr->env->from = address_new ();
1594 hdr->env->from->personal = m_strdup(b);
1598 hdr->date_sent = mutt_parse_date (b, hdr);
1599 hdr->received = hdr->date_sent;
1602 p_delete(&hdr->env->message_id);
1603 hdr->env->message_id = m_strdup(b);
1606 string_list_wipe(&hdr->env->references);
1607 hdr->env->references = mutt_parse_references (b, 0);
1610 hdr->content->length = atoi (b);
1613 hdr->lines = atoi (b);
1617 p_delete(&hdr->env->xref);
1618 b = b + 6; /* skips the "Xref: " */
1619 hdr->env->xref = m_strdup(b);
1620 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1622 rfc2047_decode_envelope(hdr->env);
1638 static int nntp_fetch_numbers (char *line, void *c)
1640 FETCH_CONTEXT *fc = c;
1643 int num = atoi(line);
1644 if (num < fc->first || num > fc->last)
1647 bit_set(&fc->messages, num);
1652 static int add_xover_line (char *line, void *c)
1655 FETCH_CONTEXT *fc = c;
1656 CONTEXT *ctx = fc->ctx;
1657 nntp_data_t *data = (nntp_data_t *) ctx->data;
1662 if (ctx->msgcount >= ctx->hdrmax)
1663 mx_alloc_memory (ctx);
1664 ctx->hdrs[ctx->msgcount] = header_new();
1665 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1667 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1668 num = ctx->hdrs[ctx->msgcount]->article_num;
1670 if (num >= fc->first && num <= fc->last) {
1672 if (num > data->lastLoaded)
1673 data->lastLoaded = num;
1674 num = num - fc->first + 1;
1675 total = fc->last - fc->first + 1;
1676 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1677 mutt_message ("%s %d/%d", fc->msg, num, total);
1679 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1686 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1688 char buf[HUGE_STRING];
1689 const char *msg = _("Fetching message headers...");
1690 const char *msg2 = _("Fetching headers from cache...");
1691 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1692 int ret, num, oldmsgcount, current;
1696 /* if empty group or nothing to do */
1697 if (!last || first > last)
1700 /* fetch list of articles */
1701 mutt_message _("Fetching list of articles...");
1707 /* CACHE: must be loaded xover cache here */
1708 num = nntp_data->lastCached - first + 1;
1709 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
1710 nntp_cache_expand (buf, nntp_data->cache);
1711 mutt_message (msg2);
1713 if ((f = safe_fopen (buf, "r"))) {
1716 /* counting number of lines */
1717 while (fgets (buf, sizeof (buf), f) != NULL)
1720 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1722 oldmsgcount = ctx->msgcount;
1724 fc.last = first + num - 1;
1726 while (fgets (buf, sizeof (buf), f) != NULL) {
1727 if (ReadInc && ((++c) % ReadInc == 0))
1728 mutt_message ("%s %d/%d", msg2, c, r);
1729 add_xover_line (buf, &fc);
1732 nntp_data->lastLoaded = fc.last;
1733 first = fc.last + 1;
1734 if (ctx->msgcount > oldmsgcount)
1735 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1738 nntp_delete_cache (nntp_data);
1740 num = last - first + 1;
1746 * Without XOVER, we have to fetch each article header and parse
1747 * it. With XOVER, we ask for all of them
1750 if (nntp_data->nserv->hasXOVER) {
1751 oldmsgcount = ctx->msgcount;
1755 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1756 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1757 if (ctx->msgcount > oldmsgcount)
1758 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1760 mutt_error (_("XOVER command failed: %s"), buf);
1764 bits_init(&fc.messages);
1766 if (nntp_data->nserv->hasLISTGROUP) {
1767 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1768 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1769 nntp_fetch_numbers, &fc, 0))
1771 mutt_error (_("LISTGROUP command failed: %s"), buf);
1773 bits_wipe(&fc.messages);
1777 for (num = first; num <= last; num++)
1778 bit_set(&fc.messages, num);
1781 for (current = first; current <= last; current++) {
1784 ret = current - first + 1;
1785 mutt_message ("%s %d/%d", msg, ret, num);
1787 if (!bit_isset(&fc.messages, current))
1790 if (ctx->msgcount >= ctx->hdrmax)
1791 mx_alloc_memory (ctx);
1792 h = ctx->hdrs[ctx->msgcount] = header_new();
1793 h->index = ctx->msgcount;
1795 ret = nntp_read_header (ctx, NULL, current);
1796 if (ret == 0) { /* Got article. Fetch next header */
1797 nntp_get_status (ctx, h, NULL, h->article_num);
1799 mx_update_context (ctx, 1);
1802 header_delete(&h); /* skip it */
1804 bits_wipe(&fc.messages);
1808 if (current > nntp_data->lastLoaded)
1809 nntp_data->lastLoaded = current;
1811 bits_wipe(&fc.messages);
1814 nntp_data->lastLoaded = last;
1815 mutt_clear_error ();
1820 * currently, nntp "mailbox" is "newsgroup"
1822 static int nntp_open_mailbox (CONTEXT * ctx)
1824 nntp_data_t *nntp_data;
1826 char buf[HUGE_STRING];
1827 char server[LONG_STRING];
1828 int count = 0, first;
1833 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1834 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1840 nntp_expand_path (server, sizeof (server), &act);
1841 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1844 CurrentNewsSrv = serv;
1846 /* create NNTP-specific state struct if nof found in list */
1847 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1848 nntp_data = nntp_data_new();
1849 nntp_data->group = m_strdup(buf);
1850 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1851 nntp_data_list_append(&serv->list, nntp_data);
1853 ctx->data = nntp_data;
1854 nntp_data->nserv = serv;
1856 mutt_message (_("Selecting %s..."), nntp_data->group);
1858 if (!nntp_data->desc) {
1859 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1860 if (nntp_data->desc)
1861 nntp_save_cache_index (serv);
1865 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1869 if (m_strncmp("211", buf, 3)) {
1872 /* GROUP command failed */
1873 if (!m_strncmp("411", buf, 3)) {
1874 mutt_error (_("Newsgroup %s not found on server %s"),
1875 nntp_data->group, serv->conn->account.host);
1877 /* CACHE: delete cache and line from .index */
1878 nntp_delete_cache (nntp_data);
1879 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1880 for (l = &serv->list; *l; l = &(*l)->next) {
1881 if ((*l) == nntp_data) {
1882 nntp_data_list_pop(l);
1883 nntp_data_delete(&nntp_data);
1887 nntp_data_delete(&nntp_data);
1894 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1895 &nntp_data->lastMessage, buf);
1897 nntp_data->deleted = 0;
1899 time (&serv->check_time);
1902 * Check for max adding context. If it is greater than $nntp_context,
1903 * strip off extra articles
1905 first = nntp_data->firstMessage;
1906 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1907 first = nntp_data->lastMessage - NntpContext + 1;
1909 nntp_data->lastLoaded = first - 1;
1910 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1913 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1915 char buf[LONG_STRING];
1916 char path[_POSIX_PATH_MAX];
1921 /* see if we already have the message in our cache */
1923 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1926 /* if everything is fine, assign msg->fp and return */
1927 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1928 (msg->fp = fopen (cache->path, "r")))
1931 /* clear the previous entry */
1932 unlink (cache->path);
1933 p_delete(&cache->path);
1935 cache->index = ctx->hdrs[msgno]->index;
1936 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1940 cache->path = m_strdup(path);
1942 if (ctx->hdrs[msgno]->article_num == 0)
1943 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1944 ctx->hdrs[msgno]->env->message_id);
1946 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1947 ctx->hdrs[msgno]->article_num);
1949 bar.msg = _("Fetching message...");
1951 mutt_progress_bar (&bar, 0);
1953 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1954 msg->fp, ctx->tagged);
1956 mutt_error (_("Article %d not found on server"),
1957 ctx->hdrs[msgno]->article_num);
1963 p_delete(&cache->path);
1967 envelope_delete(&ctx->hdrs[msgno]->env);
1968 ctx->hdrs[msgno]->env =
1969 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1970 /* fix content length */
1971 fseeko (msg->fp, 0, SEEK_END);
1972 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1973 ctx->hdrs[msgno]->content->offset;
1975 /* this is called in mutt before the open which fetches the message,
1976 * which is probably wrong, but we just call it again here to handle
1977 * the problem instead of fixing it.
1979 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1981 /* These would normally be updated in mx_update_context(), but the
1982 * full headers aren't parsed with XOVER, so the information wasn't
1985 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1987 mutt_clear_error ();
1994 int nntp_post (const char *msg)
1996 char buf[LONG_STRING];
1999 nntp_data_t *nntp_data;
2001 if (Context && Context->magic == M_NNTP)
2002 nntp_data = (nntp_data_t *)Context->data;
2004 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
2005 !CurrentNewsSrv->list)
2007 mutt_error (_("Can't post article. No connection to news server."));
2010 nntp_data = CurrentNewsSrv->list;
2013 if (!(f = safe_fopen (msg, "r"))) {
2014 mutt_error (_("Can't post article. Unable to open %s"), msg);
2018 m_strcpy(buf, sizeof(buf), "POST\r\n");
2019 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2020 mutt_error (_("Can't post article. Connection to %s lost."),
2021 nntp_data->nserv->conn->account.host);
2024 if (buf[0] != '3') {
2025 mutt_error (_("Can't post article: %s"), buf);
2031 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
2032 len = m_strlen(buf);
2033 if (buf[len - 1] == '\n') {
2034 buf[len - 1] = '\r';
2040 mutt_socket_write(nntp_data->nserv->conn, buf);
2042 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2046 if (buf[m_strlen(buf) - 1] != '\n')
2047 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2048 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2049 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2050 mutt_error (_("Can't post article. Connection to %s lost."),
2051 nntp_data->nserv->conn->account.host);
2054 if (buf[0] != '2') {
2055 mutt_error (_("Can't post article: %s"), buf);
2062 /* nntp_logout_all: close all open connections. */
2063 void nntp_logout_all (void)
2065 char buf[LONG_STRING];
2068 conn = mutt_socket_head ();
2071 CONNECTION* next = conn->next;
2072 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2073 mutt_message (_("Closing connection to %s..."), conn->account.host);
2074 mutt_socket_write (conn, "QUIT\r\n");
2075 mutt_socket_readln (buf, sizeof (buf), conn);
2076 mutt_clear_error ();
2077 mutt_socket_close (conn);
2078 mutt_socket_free (conn);
2084 static void nntp_free_acache (nntp_data_t * data)
2088 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2089 if (data->acache[i].path) {
2090 unlink (data->acache[i].path);
2091 p_delete(&data->acache[i].path);
2096 void nntp_data_wipe(nntp_data_t *data)
2098 p_delete(&data->entries);
2099 p_delete(&data->desc);
2100 p_delete(&data->cache);
2101 p_delete(&data->group);
2102 nntp_free_acache(data);
2105 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2107 nntp_data_t *data = ctx->data;
2109 /* CACHE: update cache and .index files */
2110 if ((option (OPTSAVEUNSUB) || data->subscribed))
2111 nntp_save_cache_group (ctx);
2112 nntp_free_acache (data);
2114 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2118 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2120 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2124 nntp_free_acache (data);
2125 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2127 nntp_save_cache_index (data->nserv);
2128 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2130 nntp_data_delete(&data);
2132 nntp_sync_sidebar (data);
2135 /* commit changes and terminate connection */
2136 int nntp_close_mailbox (CONTEXT * ctx)
2140 mutt_message _("Quitting newsgroup...");
2143 nntp_data_t *data = (nntp_data_t *) ctx->data;
2146 if (data->nserv && data->nserv->conn && ctx->unread) {
2147 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2149 mutt_newsgroup_catchup (data->nserv, data->group);
2154 nntp_sync_mailbox (ctx, 0, NULL);
2155 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2158 news = ((nntp_data_t *) ctx->data)->nserv;
2159 newsrc_gen_entries (ctx);
2160 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2161 mutt_newsrc_update (news);
2163 mutt_clear_error ();
2167 /* use the GROUP command to poll for new mail */
2168 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2170 char buf[LONG_STRING];
2173 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2177 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2180 if (m_strncmp("211", buf, 3)) {
2182 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2186 if (!m_strncmp("211", buf, 3)) {
2190 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2191 nntp_data->firstMessage = first;
2192 nntp_data->lastMessage = last;
2193 if (ctx && last > nntp_data->lastLoaded) {
2194 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2195 time (&nntp_data->nserv->check_time);
2198 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2199 nntp_data->unread = count;
2201 mutt_newsgroup_stat (nntp_data);
2202 /* active was renumbered? */
2203 if (last < nntp_data->lastLoaded) {
2204 if (!nntp_data->max) {
2205 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2208 nntp_data->lastCached = 0;
2210 nntp_data->entries[0].first = 1;
2211 nntp_data->entries[0].last = 0;
2213 nntp_sync_sidebar (nntp_data);
2216 time (&nntp_data->nserv->check_time);
2220 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2222 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2225 static int add_group (char *buf, void *serv)
2227 NNTP_SERVER *s = serv;
2228 char group[LONG_STRING], mod, desc[HUGE_STRING];
2230 nntp_data_t *nntp_data;
2233 _checked = n; /* _checked have N, where N = number of groups */
2234 if (!buf) /* at EOF must be zerouth */
2241 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
2244 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
2246 nntp_data = nntp_data_new();
2247 nntp_data->group = m_strdup(group);
2248 nntp_data->nserv = s;
2249 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
2250 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
2251 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
2252 nntp_data_list_append(&s->list, nntp_data);
2254 nntp_data->deleted = 0;
2255 nntp_data->firstMessage = first;
2256 nntp_data->lastMessage = last;
2258 nntp_data->allowed = 1;
2260 nntp_data->allowed = 0;
2261 if (nntp_data->desc)
2262 p_delete(&nntp_data->desc);
2264 nntp_data->desc = m_strdup(desc);
2265 if (nntp_data->rc || nntp_data->lastCached)
2266 mutt_newsgroup_stat (nntp_data);
2267 else if (nntp_data->lastMessage &&
2268 nntp_data->firstMessage <= nntp_data->lastMessage)
2269 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
2271 nntp_data->unread = 0;
2276 static int nntp_check_newgroups (NNTP_SERVER * serv, int force)
2278 char buf[LONG_STRING];
2279 nntp_data_t nntp_data;
2280 nntp_data_t *l, **emp;
2284 if (!serv || !serv->newgroups_time)
2287 if (nntp_open_connection (serv) < 0)
2290 /* check subscribed groups for new news */
2291 if (option (OPTSHOWNEWNEWS)) {
2292 mutt_message _("Checking for new messages...");
2294 for (l = serv->list; l; l = l->next) {
2295 serv->check_time = 0; /* really check! */
2297 _nntp_check_mailbox (NULL, l);
2304 mutt_message _("Checking for new newsgroups...");
2306 now = serv->newgroups_time;
2307 time (&serv->newgroups_time);
2309 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2310 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2311 t->tm_min, t->tm_sec);
2312 nntp_data.nserv = serv;
2313 if (Context && Context->magic == M_NNTP)
2314 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2316 nntp_data.group = NULL;
2318 emp = nntp_data_list_last(&serv->list);
2319 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2320 add_group, serv, 0) != 0) {
2324 mutt_message _("Loading descriptions...");
2326 for (l = *emp; l; l = l->next) {
2328 nntp_get_desc(l, l->group, NULL, NULL);
2331 nntp_save_cache_index(serv);
2336 /* Load list of all newsgroups from cache ALL */
2337 static int nntp_get_cache_all (NNTP_SERVER * serv)
2339 char buf[HUGE_STRING];
2342 nntp_cache_expand (buf, serv->cache);
2343 if ((f = safe_fopen (buf, "r"))) {
2346 while (fgets (buf, sizeof (buf), f) != NULL) {
2347 if (ReadInc && (i % ReadInc == 0))
2348 mutt_message (_("Loading list from cache... %d"), i);
2349 add_group (buf, serv);
2352 add_group (NULL, NULL);
2354 mutt_clear_error ();
2358 p_delete(&serv->cache);
2363 /* Load list of all newsgroups from active */
2364 int nntp_get_active (NNTP_SERVER * serv)
2367 nntp_data_t nntp_data, **tmp = &serv->list;
2369 if (nntp_open_connection (serv) < 0)
2372 snprintf (msg, sizeof (msg),
2373 _("Loading list of all newsgroups on server %s..."),
2374 serv->conn->account.host);
2376 time (&serv->newgroups_time);
2377 nntp_data.nserv = serv;
2378 nntp_data.group = NULL;
2380 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2384 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2386 nntp_get_desc (&nntp_data, "*", msg, NULL);
2389 if ((*tmp)->deleted && !(*tmp)->rc) {
2390 nntp_data_t *d = nntp_data_list_pop(tmp);
2391 nntp_delete_cache(d);
2392 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2393 nntp_data_delete(&d);
2395 tmp = &(*tmp)->next;
2398 nntp_save_cache_index (serv);
2400 mutt_clear_error ();
2405 * returns -1 if error ocurred while retrieving header,
2406 * number of articles which ones exist in context on success.
2408 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2412 /* if msgid is already in context, don't reload them */
2413 if (hash_find (ctx->id_hash, msgid))
2415 if (ctx->msgcount == ctx->hdrmax)
2416 mx_alloc_memory (ctx);
2417 ctx->hdrs[ctx->msgcount] = header_new();
2418 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2420 mutt_message (_("Fetching %s from server..."), msgid);
2421 ret = nntp_read_header (ctx, msgid, 0);
2422 /* since nntp_read_header() may set read flag, we must reset it */
2423 ctx->hdrs[ctx->msgcount]->read = 0;
2425 header_delete(&ctx->hdrs[ctx->msgcount]);
2428 mx_update_context (ctx, 1);
2441 static int check_children (char *s, void *c)
2443 CHILD_CONTEXT *cc = c;
2446 if (!s || (n = atoi (s)) == 0)
2448 for (i = 0; i < cc->ctx->msgcount; i++)
2449 if (cc->ctx->hdrs[i]->article_num == n)
2451 if (cc->num >= cc->max)
2452 p_realloc(&cc->child, cc->max += 25);
2453 cc->child[cc->num++] = n;
2458 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2460 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2462 int i, ret = 0, tmp = 0;
2463 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2465 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2466 !nntp_data->nserv->conn->account.host)
2468 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2470 if (!nntp_data->nserv->hasXPAT) {
2471 mutt_error (_("Server %s does not support this operation!"),
2472 nntp_data->nserv->conn->account.host);
2476 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2477 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2479 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2480 p_delete(&cc.child);
2484 /* dont try to read the xover cache. check_children() already
2485 * made sure that we dont have the article, so we need to visit
2486 * the server. Reading the cache at this point is also bad
2487 * because it would duplicate messages */
2488 if (option (OPTNEWSCACHE)) {
2490 unset_option (OPTNEWSCACHE);
2492 for (i = 0; i < cc.num; i++) {
2493 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2495 if (ctx->msgcount &&
2496 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2497 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2500 set_option (OPTNEWSCACHE);
2501 p_delete(&cc.child);
2505 static int nntp_is_magic (const char* path, struct stat* st) {
2506 url_scheme_t s = url_check_scheme(NONULL(path));
2507 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2510 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2512 case ACL_INSERT: /* editing messages */
2513 case ACL_WRITE: /* change importance */
2515 case ACL_DELETE: /* (un)deletion */
2516 case ACL_SEEN: /* mark as read */
2523 mx_t const nntp_mx = {
2533 nntp_fastclose_mailbox,