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
31 unsigned use_cache : 1;
36 static int nntp_check_newgroups (nntp_server_t *, int);
40 static void mutt_newsgroup_stat(nntp_data_t *data)
43 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
46 data->unread = data->lastMessage - data->firstMessage + 1;
47 for (int i = 0; i < data->num; i++) {
48 int first = MAX(data->entries[i].first, data->firstMessage);
49 int last = MIN(data->entries[i].last, data->lastMessage);
51 data->unread -= last - first + 1;
55 static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line)
58 char group[LONG_STRING];
62 for (p = line; *p; p++) {
66 p = strpbrk(line, ":!");
70 m_strncpy(group, ssizeof(group), line, p - line);
71 data = hash_find(news->newsgroups, group);
73 data = nntp_data_new();
74 data->group = p_dupstr(line, p - line);
77 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
78 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
79 hash_insert(news->newsgroups, data->group, data);
80 news->tail = nntp_data_list_append(news->tail, data);
82 p_delete(&data->entries);
86 data->entries = p_new(NEWSRC_ENTRY, x * 2);
88 data->subscribed = (*p++ == ':');
90 for (x = 0; *p; p++) {
92 data->entries[x].first = strtol(p, (char **)&p, 10);
95 data->entries[x].last = strtol(p + 1, (char **)&p, 10);
97 data->entries[x].last = data->entries[x].first;
100 p = strchrnul(p, ',');
101 x += data->entries[x].last != 0;
104 if (x && !data->lastMessage)
105 data->lastMessage = data->entries[x - 1].last;
107 mutt_newsgroup_stat(data);
111 static int slurp_newsrc (nntp_server_t * news)
118 news->stat = stat(news->newsrc, &sb);
119 news->size = sb.st_size;
120 news->mtime = sb.st_mtime;
122 if ((fp = safe_fopen(news->newsrc, "r")) == NULL)
125 /* hmm, should we use dotlock? */
126 if (mx_lock_file(news->newsrc, fileno (fp), 0, 0, 1)) {
131 while (getline(&buf, &n, fp) >= 0) {
132 nntp_parse_newsrc_line(news, buf);
136 mx_unlock_file (news->newsrc, fileno (fp), 0);
141 #define nntp_cache_expand(dst, dlen, fmt, ...) \
143 snprintf((dst), (dlen), "%s/" fmt, mod_core.cachedir, ##__VA_ARGS__);\
144 mutt_expand_path((dst), (dlen)); \
147 /* Loads $news_cache_dir/.index into memory, loads newsserver data
148 * and newsgroup cache names */
149 static int nntp_parse_cacheindex(nntp_server_t *news)
151 char buf[HUGE_STRING];
154 p_delete(&news->cache);
155 if (m_strisempty(mod_core.cachedir))
158 nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host);
159 if (!(idx = safe_fopen(buf, "a+")))
163 while (fgets(buf, sizeof(buf), idx)) {
164 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
165 if (!m_strncmp(buf, "#: ", 3)
166 && !m_strcasecmp(buf + 3, news->conn->account.host)) {
171 while (fgets(buf, sizeof(buf), idx)) {
175 if (m_strstart(buf, "#:", NULL))
178 p = strchrnul(buf, ' ');
183 q = strchrnul(p, ' ');
187 l = strtol(q, &q, 10);
188 m = strtol(q, &q, 10);
190 if (!m_strcmp(buf, "ALL")) {
191 m_strreplace(&news->cache, m_strdup(p));
192 news->newgroups_time = m;
196 if (news->newsgroups) {
197 nntp_data_t *data = hash_find(news->newsgroups, buf);
200 data = nntp_data_new();
201 data->group = m_strdup(buf);
204 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
205 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
206 hash_insert (news->newsgroups, data->group, data);
207 news->tail = nntp_data_list_append(news->tail, data);
209 m_strreplace(&data->cache, p);
211 t = !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);
226 /* nntp_parse_url: given an NNPT URL, return host, port,
227 * username, password and newsgroup will recognise. */
228 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
237 act->port = NNTP_PORT;
238 act->type = M_ACCT_TYPE_NNTP;
240 c = m_strdup(server);
241 url_parse_ciss (&url, c);
243 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
244 if (url.scheme == U_NNTPS) {
246 act->port = NNTP_SSL_PORT;
251 m_strcpy(group, group_len, url.path);
253 ret = mutt_account_fromurl (act, &url);
260 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
264 url.path = m_strdup(line);
265 mutt_account_tourl (act, &url);
266 url_ciss_tostring (&url, line, len, 0);
270 static int add_group (char *buf, void *serv)
272 nntp_server_t *s = serv;
273 char group[LONG_STRING], mod, desc[HUGE_STRING];
275 nntp_data_t *nntp_data;
278 nntp.checked = n; /* nntp.checked have N, where N = number of groups */
279 if (!buf) /* at EOF must be zerouth */
286 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
289 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
291 nntp_data = nntp_data_new();
292 nntp_data->group = m_strdup(group);
293 nntp_data->nserv = s;
294 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
295 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
296 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
297 s->tail = nntp_data_list_append(s->tail, nntp_data);
299 nntp_data->deleted = 0;
300 nntp_data->firstMessage = first;
301 nntp_data->lastMessage = last;
303 nntp_data->allowed = 1;
305 nntp_data->allowed = 0;
307 p_delete(&nntp_data->desc);
309 nntp_data->desc = m_strdup(desc);
310 if (nntp_data->rc || nntp_data->lastCached)
311 mutt_newsgroup_stat (nntp_data);
312 else if (nntp_data->lastMessage &&
313 nntp_data->firstMessage <= nntp_data->lastMessage)
314 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
316 nntp_data->unread = 0;
321 /* Load list of all newsgroups from cache ALL */
322 static int nntp_get_cache_all (nntp_server_t * serv)
324 char buf[HUGE_STRING];
327 nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
328 if ((f = safe_fopen (buf, "r"))) {
331 while (fgets (buf, sizeof (buf), f) != NULL) {
332 if (ReadInc && (i % ReadInc == 0))
333 mutt_message (_("Loading list from cache... %d"), i);
334 add_group (buf, serv);
337 add_group (NULL, NULL);
343 p_delete(&serv->cache);
349 * Automatically loads a newsrc into memory, if necessary.
350 * Checks the size/mtime of a newsrc file, if it doesn't match, load
351 * again. Hmm, if a system has broken mtimes, this might mean the file
352 * is reloaded every time, which we'd have to fix.
354 * a newsrc file is a line per newsgroup, with the newsgroup, then a
355 * ':' denoting subscribed or '!' denoting unsubscribed, then a
356 * comma separated list of article numbers and ranges.
358 nntp_server_t *mutt_select_newsserver (char *server)
360 char file[_POSIX_PATH_MAX];
369 if (!server || !*server) {
370 mutt_error _("No newsserver defined!");
375 buf = p = p_new(char, m_strlen(server) + 10);
376 if (url_check_scheme (server) == U_UNKNOWN) {
377 strcpy (buf, "nntp://");
378 p = strchr (buf, '\0');
382 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
384 mutt_error (_("%s is an invalid newsserver specification!"), server);
389 conn = mutt_conn_find (NULL, &act);
393 nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
394 serv = (nntp_server_t *) conn->data;
398 /* externally modified? */
399 if (serv->stat != stat(file, &sb) || (!serv->stat &&
400 (serv->size != sb.st_size
401 || serv->mtime != sb.st_mtime)))
403 for (list = serv->list; list; list = list->next) {
404 list->subscribed = list->rc = list->num = 0;
409 if (serv->status == NNTP_BYE)
410 serv->status = NNTP_NONE;
411 nntp_check_newgroups (serv, 0);
416 serv = p_new(nntp_server_t, 1);
417 serv->tail = &serv->list;
419 serv->newsrc = m_strdup(file);
420 serv->newsgroups = hash_new(SHRT_MAX, false);
421 slurp_newsrc (serv); /* load .newsrc */
422 nntp_parse_cacheindex (serv); /* load .index */
423 if (nntp.use_cache && serv->cache && nntp_get_cache_all (serv) >= 0)
424 nntp_check_newgroups (serv, 1);
425 else if (nntp_get_active (serv) < 0) {
426 hash_delete(&serv->newsgroups, NULL);
427 nntp_data_list_wipe(&serv->list);
428 p_delete(&serv->newsrc);
429 p_delete(&serv->cache);
433 conn->data = (void *) serv;
439 * full status flags are not supported by nntp, but we can fake some
440 * of them. This is how:
441 * Read = a read message number is in the .newsrc
442 * New = a message is new since we last read this newsgroup
443 * Old = anything else
444 * So, Read is marked as such in the newsrc, old is anything that is
445 * "skipped" in the newsrc, and new is anything not in the newsrc nor
446 * in the cache. By skipped, I mean before the last unread message
449 nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article)
451 nntp_data_t *data = ctx->data;
454 data = hash_find(data->nserv->newsgroups, group);
458 for (int i = 0; i < data->num; i++) {
459 if ((article >= data->entries[i].first) &&
460 (article <= data->entries[i].last))
462 /* we cannot use mutt_set_flag() because mx_update_context()
469 /* If article was not cached yet, it is new! :) */
470 if (!data->cache || article > data->lastCached)
473 /* Old articles are articles which aren't read but an article after them
475 if (option(OPTMARKOLD))
479 static void nntp_create_newsrc_line(nntp_data_t *data, buffer_t *buf)
481 buffer_addstr(buf, data->group);
482 buffer_addch(buf, data->subscribed ? ':' : '!');
483 buffer_addch(buf, ' ');
485 for (int x = 0; x < data->num; x++) {
487 buffer_addch(buf, ',');
490 buffer_addf(buf, "%d-%d", data->entries[x].first,
491 data->entries[x].last);
493 buffer_addch(buf, '\n');
496 static void newsrc_gen_entries (CONTEXT * ctx)
498 nntp_data_t *data = (nntp_data_t *) ctx->data;
500 int last = 0, first = 1;
501 int save_sort = SORT_ORDER;
503 if (Sort != SORT_ORDER) {
506 mutt_sort_headers (ctx, 0);
510 data->entries = p_new(NEWSRC_ENTRY, 5);
515 * Set up to fake initial sequence from 1 to the article before the
516 * first article in our list
521 for (x = 0; x < ctx->msgcount; x++) {
522 if (series) { /* search for first unread */
524 * We don't actually check sequential order, since we mark
525 * "missing" entries as read/deleted
527 last = ctx->hdrs[x]->article_num;
528 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
529 !ctx->hdrs[x]->read) {
530 if (data->num >= data->max) {
531 data->max = data->max * 2;
532 p_realloc(&data->entries, data->max);
534 data->entries[data->num].first = first;
535 data->entries[data->num].last = last - 1;
540 else { /* search for first read */
542 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
546 last = ctx->hdrs[x]->article_num;
549 if (series && first <= data->lastLoaded) {
550 if (data->num >= data->max) {
551 data->max = data->max * 2;
552 p_realloc(&data->entries, data->max);
554 data->entries[data->num].first = first;
555 data->entries[data->num].last = data->lastLoaded;
559 if (save_sort != Sort) {
561 mutt_sort_headers (ctx, 0);
565 static int mutt_update_list_file (char *filename, char *section,
566 const char *key, const char *line)
570 char buf[HUGE_STRING];
571 char tmpf[_POSIX_PATH_MAX], lnk[_POSIX_PATH_MAX];
573 int ext = 0, done = 0, r = 0, l = 0;
575 /* if file not exist, create it */
576 if ((ifp = safe_fopen (filename, "a")))
578 if (!(ifp = safe_fopen (filename, "r"))) {
579 mutt_error (_("Unable to open %s for reading"), filename);
582 if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) {
584 mutt_error (_("Unable to lock %s"), filename);
588 /* use m_tempfile() to get a tempfile in the same
589 * directory as filename is so that we can follow symlinks
590 * via rename(2); as dirname(2) may modify its argument,
591 * temporarily use buf as copy of it
593 m_strcpy(buf, sizeof(buf), filename);
594 ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
597 mutt_error (_("Unable to open %s for writing"), tmpf);
602 while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) {
603 r = fputs (buf, ofp);
605 while (*c && *c != '\n') c++;
606 c[0] = 0; /* strip EOL */
607 if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
610 if (r != EOF && !done) {
611 snprintf (buf, sizeof(buf), "#: %s\n", section);
612 r = fputs (buf, ofp);
617 while (r != EOF && fgets (buf, sizeof (buf), ifp)) {
620 while (*c && (*c != '\r') && (*c != '\n')) c++;
622 if (*c != '\\') ext = 0;
623 } else if ((section && !strncmp (buf, "#: ", 3))) {
628 r = fputs (buf, ofp);
631 } else if (key && !strncmp (buf, key, strlen(key)) &&
632 (!*key || buf[strlen(key)] == ' ')) {
635 while (*c && (*c != '\r') && (*c != '\n')) c++;
637 if (*c == '\\') ext = 1;
639 r = fputs (line, ofp);
641 r = fputc ('\n', ofp);
645 r = fputs (buf, ofp);
649 while (r != EOF && fgets (buf, sizeof (buf), ifp))
650 r = fputs (buf, ofp);
652 /* If there wasn't a line to replace, put it on the end of the file */
653 if (r != EOF && !done && line) {
655 r = fputc ('\n', ofp);
657 mx_unlock_file (filename, fileno (ifp), 0);
662 mutt_error (_("Can't write %s"), tmpf);
666 if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
668 if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
670 mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
676 int mutt_newsrc_update (nntp_server_t * news)
687 /* we will generate full newsrc here */
688 for (data = news->list; data; data = data->next) {
689 if (!data || !data->rc)
691 nntp_create_newsrc_line(data, &buf);
693 /* newrc being fully rewritten */
695 && (r = mutt_update_list_file(news->newsrc, NULL, "", buf.data)) == 0)
699 stat (news->newsrc, &st);
700 news->size = st.st_size;
701 news->mtime = st.st_mtime;
708 static FILE *mutt_mkname (char *s)
710 char buf[_POSIX_PATH_MAX], *pc;
714 nntp_cache_expand(buf, ssizeof(buf), "%s", s);
715 if ((fp = safe_fopen (buf, "w")))
718 nntp_cache_expand(buf, ssizeof(buf), "cache-XXXXXX");
719 pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
720 if ((fd = mkstemp (buf)) == -1)
722 strcpy (s, pc); /* generated name */
723 return fdopen (fd, "w");
726 /* Updates info into .index file: ALL or about selected newsgroup */
727 static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
729 char buf[LONG_STRING];
730 char file[_POSIX_PATH_MAX];
731 const char *key = "ALL";
733 if (!serv || !serv->conn || !serv->conn->account.host)
736 if (data && data->group) {
738 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
739 data->firstMessage, data->lastLoaded);
742 m_strcpy(file, sizeof(file), serv->cache);
743 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
744 (int) serv->newgroups_time);
746 nntp_cache_expand(file, ssizeof(file), "%s.index", serv->conn->account.host);
747 return mutt_update_list_file(file, serv->conn->account.host, key, buf);
750 static void nntp_delete_cache (nntp_data_t * data)
752 char buf[_POSIX_PATH_MAX];
754 if (!nntp.use_cache || !data || !data->cache || !data->nserv)
757 nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache);
759 p_delete(&data->cache);
760 data->lastCached = 0;
761 nntp_cache_expand(buf, ssizeof(buf), "%s.index",
762 data->nserv->conn->account.host);
763 mutt_update_list_file(buf, data->nserv->conn->account.host, data->group,
767 static int nntp_save_cache_index (nntp_server_t * news)
769 char buf[HUGE_STRING];
770 char file[_POSIX_PATH_MAX];
774 if (!news || !news->newsgroups)
780 nntp_cache_expand(file, ssizeof(file), "%s", news->cache);
782 f = safe_fopen(file, "w");
784 m_strcpy(buf, ssizeof(buf), news->conn->account.host);
785 f = mutt_mkname(buf);
786 news->cache = m_strdup(buf);
787 nntp_cache_expand(file, ssizeof(file), "%s", buf);
792 for (d = news->list; d; d = d->next) {
795 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
796 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
799 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
800 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
801 if (fputs (buf, f) == EOF) {
810 if (nntp_update_cacheindex (news, NULL)) {
817 static int nntp_save_cache_group (CONTEXT * ctx)
819 char buf[HUGE_STRING], addr[STRING];
820 char file[_POSIX_PATH_MAX];
824 int i = 0, save = SORT_ORDER;
829 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
832 if (((nntp_data_t *) ctx->data)->cache) {
833 nntp_cache_expand(file, ssizeof(file), "%s",
834 ((nntp_data_t *)ctx->data)->cache);
836 f = safe_fopen (file, "w");
839 snprintf(buf, ssizeof(buf), "%s-%s",
840 ((nntp_data_t *)ctx->data)->nserv->conn->account.host,
841 ((nntp_data_t *)ctx->data)->group);
842 f = mutt_mkname (buf);
843 ((nntp_data_t *)ctx->data)->cache = m_strdup(buf);
844 nntp_cache_expand(file, ssizeof(file), "%s", buf);
849 if (Sort != SORT_ORDER) {
852 mutt_sort_headers (ctx, 0);
855 /* Save only $nntp_context messages... */
856 ((nntp_data_t *) ctx->data)->lastCached = 0;
857 if (NntpContext && ctx->msgcount > NntpContext)
858 i = ctx->msgcount - NntpContext;
859 for (; i < ctx->msgcount; i++) {
860 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
863 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
864 tm = gmtime (&h->date_sent);
865 snprintf (buf, sizeof (buf),
866 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
867 h->article_num, h->env->subject, addr, tm->tm_mday,
868 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
869 tm->tm_min, tm->tm_sec, h->env->message_id);
871 if (h->env->references)
872 mutt_write_references (h->env->references, f);
873 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
874 h->content->length, h->lines, NONULL (h->env->xref));
875 if (fputs (buf, f) == EOF) {
881 prev = ctx->hdrs[i]->article_num;
886 mutt_sort_headers (ctx, 0);
890 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
891 (nntp_data_t *) ctx->data)) {
895 ((nntp_data_t *) ctx->data)->lastCached =
896 ((nntp_data_t *) ctx->data)->lastLoaded;
900 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
904 if (!news || !news->newsgroups || !group || !*group)
906 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
907 data = nntp_data_new();
908 data->group = m_strdup(group);
911 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
912 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
913 hash_insert (news->newsgroups, data->group, data);
914 news->tail = nntp_data_list_append(news->tail, data);
916 if (!data->subscribed) {
917 data->subscribed = 1;
923 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
927 if (!news || !news->newsgroups || !group || !*group ||
928 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
930 if (data->subscribed) {
931 data->subscribed = 0;
936 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
940 if (!news || !news->newsgroups || !group || !*group ||
941 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
944 data->entries = p_new(NEWSRC_ENTRY, 5);
948 data->entries[0].first = 1;
950 data->entries[0].last = data->lastMessage;
951 if (Context && Context->data == data) {
954 for (x = 0; x < Context->msgcount; x++)
955 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
960 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
964 if (!news || !news->newsgroups || !group || !*group ||
965 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
968 data->entries = p_new(NEWSRC_ENTRY, 5);
972 data->entries[0].first = 1;
973 data->entries[0].last = data->firstMessage - 1;
974 if (Context && Context->data == data) {
977 data->unread = Context->msgcount;
978 for (x = 0; x < Context->msgcount; x++)
979 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
982 data->unread = data->lastMessage - data->entries[0].last;
986 /* this routine gives the first newsgroup with new messages */
987 void nntp_buffy (char* dst, ssize_t dstlen) {
991 /* forward to current group */
992 for (list = CurrentNewsSrv->list; list; list = list->next) {
993 if (list->subscribed && list->unread
994 && Context && Context->magic == M_NNTP
995 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
1007 list = CurrentNewsSrv->list;
1009 for (; list; list = list->next) {
1010 if (list->subscribed && list->unread) {
1011 if (Context && Context->magic == M_NNTP &&
1012 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1015 for (i = 0; i < Context->msgcount; i++)
1016 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1021 m_strcpy(dst, dstlen, list->group);
1034 /* nntp protocol {{{ */
1036 void nntp_sync_sidebar (nntp_data_t* data) {
1044 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1045 data->nserv->conn->account.has_ssl ? "s" : "",
1046 NONULL(data->nserv->conn->account.user),
1047 *data->nserv->conn->account.pass ? ":" : "",
1048 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1049 data->nserv->conn->account.host,
1052 /* bail out if group not found via mailboxes */
1053 if ((i = buffy_lookup (buf)) < 0)
1056 tmp = Incoming.arr[i];
1057 /* copied from browser.c */
1058 if (option (OPTMARKOLD) &&
1059 data->lastCached >= data->firstMessage &&
1060 data->lastCached <= data->lastMessage)
1061 tmp->msg_unread = data->lastMessage - data->lastCached;
1063 tmp->msg_unread = data->unread;
1064 tmp->new = data->unread > 0;
1065 /* this is closest to a "total" count we can get */
1066 tmp->msgcount = data->lastMessage - data->firstMessage;
1069 static int nntp_auth (nntp_server_t * serv)
1071 CONNECTION *conn = serv->conn;
1073 unsigned char flags = conn->account.flags;
1075 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1076 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1077 conn->account.flags = flags;
1081 mutt_message _("Logging in...");
1083 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1084 mutt_socket_write (conn, buf);
1085 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1086 conn->account.flags = flags;
1090 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1091 mutt_socket_write(conn, buf);
1092 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1093 conn->account.flags = flags;
1097 if (m_strncmp("281", buf, 3)) {
1098 conn->account.flags = flags;
1099 mutt_error _("Login failed.");
1108 static int nntp_connect_error (nntp_server_t * serv)
1110 serv->status = NNTP_NONE;
1111 mutt_socket_close (serv->conn);
1112 mutt_error _("Server closed connection!");
1118 static int nntp_connect_and_auth (nntp_server_t * serv)
1120 CONNECTION *conn = serv->conn;
1124 serv->status = NNTP_NONE;
1126 if (mutt_socket_open (conn) < 0)
1129 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1130 return nntp_connect_error (serv);
1132 if (!m_strncmp("200", buf, 3))
1133 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1134 else if (!m_strncmp("201", buf, 3))
1135 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1137 mutt_socket_close(conn);
1139 mutt_error("%s", buf);
1144 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1145 returned codes and messages. */
1146 mutt_socket_write (conn, "MODE READER\r\n");
1147 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1148 return nntp_connect_error (serv);
1150 mutt_socket_write (conn, "STAT\r\n");
1151 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1152 return nntp_connect_error (serv);
1154 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1155 serv->status = NNTP_OK;
1159 rc = nntp_auth (serv);
1161 return nntp_connect_error (serv);
1163 mutt_socket_close (conn);
1164 serv->status = NNTP_BYE;
1168 mutt_socket_close (conn);
1169 mutt_error _("Login failed.");
1174 serv->status = NNTP_OK;
1178 static int nntp_attempt_features (nntp_server_t * serv)
1180 char buf[LONG_STRING];
1181 CONNECTION *conn = serv->conn;
1183 if (serv->feat_known)
1186 mutt_socket_write (conn, "XOVER\r\n");
1187 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1188 return nntp_connect_error (serv);
1189 if (m_strncmp("500", buf, 3))
1192 mutt_socket_write (conn, "XPAT\r\n");
1193 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1194 return nntp_connect_error (serv);
1195 if (m_strncmp("500", buf, 3))
1198 mutt_socket_write (conn, "LISTGROUP\r\n");
1199 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1200 return (nntp_connect_error (serv));
1201 if (m_strncmp("500", buf, 3))
1202 serv->hasLISTGROUP = 1;
1204 mutt_socket_write (conn, "XGTITLE +\r\n");
1205 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1206 return nntp_connect_error (serv);
1207 if (m_strncmp("500", buf, 3))
1208 serv->hasXGTITLE = 1;
1210 if (!m_strncmp("282", buf, 3)) {
1212 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1213 return nntp_connect_error (serv);
1214 } while (!(buf[0] == '.' && buf[1] == '\0'));
1217 serv->feat_known = 1;
1221 static int nntp_open_connection (nntp_server_t * serv)
1223 if (serv->status == NNTP_OK)
1225 if (serv->status == NNTP_BYE)
1227 if (nntp_connect_and_auth (serv) < 0)
1229 if (nntp_attempt_features (serv) < 0)
1234 static int nntp_reconnect (nntp_server_t * serv)
1238 mutt_socket_close (serv->conn);
1241 if (nntp_connect_and_auth (serv) == 0)
1244 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1245 serv->conn->account.host);
1246 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1247 serv->status = NNTP_BYE;
1253 /* Send data from line[LONG_STRING] and receive answer to same line */
1254 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1256 char buf[LONG_STRING];
1259 if (data->nserv->status == NNTP_BYE)
1264 mutt_socket_write (data->nserv->conn, line);
1266 else if (data->group) {
1267 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1268 mutt_socket_write (data->nserv->conn, buf);
1272 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1273 if (nntp_reconnect (data->nserv) < 0)
1277 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1278 mutt_socket_write (data->nserv->conn, buf);
1279 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1285 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1289 m_strcpy(line, linelen, buf);
1294 * This function calls funct(*line, *data) for each received line,
1295 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1298 * 1 - correct but not performed (may be, have to be continued),
1299 * -1 - conection lost,
1300 * -2 - invalid command or execution error,
1301 * -3 - error in funct(*line, *data).
1303 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1304 const char *msg, progress_t* bar,
1305 int (*funct) (char *, void *),
1306 void *data, int tagged)
1308 char buf[LONG_STRING];
1317 m_strcpy(buf, sizeof(buf), query);
1318 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1327 inbuf = p_new(char, sizeof(buf));
1330 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1335 if (!lenbuf && buf[0] == '.') {
1336 if (buf[1] == '\0') {
1344 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1347 if (chunk >= ssizeof (buf)) {
1348 lenbuf += m_strlen(p);
1352 mutt_progress_bar (bar, pos);
1355 if (ReadInc && (line % ReadInc == 0)) {
1357 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1359 mutt_message ("%s %d", msg, line);
1363 if (ret == 0 && funct (inbuf, data) < 0)
1368 p_realloc(&inbuf, lenbuf + sizeof (buf));
1377 static int nntp_read_tempfile (char *line, void *file)
1379 FILE *f = (FILE *) file;
1385 if (fputc ('\n', f) == EOF)
1391 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1394 register char *p, *b;
1395 register char *colon = NULL;
1399 /* skip to next word */
1401 while (*b && ((*b == ' ') || (*b == '\t')))
1405 /* skip to end of word */
1406 while (*p && (*p != ' ') && (*p != '\t')) {
1418 nntp_get_status (ctx, h, b, atoi (colon));
1419 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1420 h->article_num = atoi (colon);
1428 * 1 if article not found
1429 * -1 if read or write error on tempfile or socket
1431 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1434 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1436 char buf[LONG_STRING];
1437 char tempfile[_POSIX_PATH_MAX];
1439 HEADER *h = ctx->hdrs[ctx->msgcount];
1441 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1446 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1448 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1450 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1454 return (ret == -1 ? -1 : 1);
1457 h->article_num = article_num;
1458 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1462 if (h->env->xref != NULL)
1463 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1464 else if (h->article_num == 0 && msgid) {
1465 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1466 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1467 h->article_num = atoi (buf + 4);
1473 static int parse_description (char *line, void *n)
1475 register char *d = line;
1477 nntp_server_t *news = n;
1481 while (*d && *d != '\t' && *d != ' ')
1485 while (*d && (*d == '\t' || *d == ' '))
1487 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1488 m_strcmp(d, data->desc)) {
1489 p_delete(&data->desc);
1490 data->desc = m_strdup(d);
1495 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1499 if (!option (OPTLOADDESC) || !data || !data->nserv)
1502 /* Get newsgroup description, if we can */
1503 if (data->nserv->hasXGTITLE)
1504 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1506 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1507 mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0);
1511 * XOVER returns a tab separated list of:
1512 * id|subject|from|date|Msgid|references|bytes|lines|xref
1514 * This has to duplicate some of the functionality of
1515 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1516 * a limited number of headers which are "parsed" by placement in the list)
1518 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1520 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1524 hdr->env = envelope_new();
1525 hdr->env->newsgroups = m_strdup(nntp_data->group);
1526 hdr->content = body_new();
1527 hdr->content->type = TYPETEXT;
1528 hdr->content->subtype = m_strdup("plain");
1529 hdr->content->encoding = ENC7BIT;
1530 hdr->content->disposition = DISPINLINE;
1531 hdr->content->length = -1;
1534 for (x = 0; !done && x < 9; x++) {
1535 /* if from file, need to skip newline character */
1536 while (*p && *p != '\n' && *p != '\t')
1544 hdr->article_num = atoi (b);
1545 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1548 hdr->env->subject = m_strdup(b);
1551 address_list_wipe(&hdr->env->from);
1552 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1553 /* same as for mutt_parse_rfc822_line():
1554 * don't leave from info NULL if there's an invalid address (or
1555 * whatever) in From: field; mutt would just display it as empty
1556 * and mark mail/(esp.) news article as your own. aaargh! this
1557 * bothered me for _years_ */
1558 if (!hdr->env->from) {
1559 hdr->env->from = address_new ();
1560 hdr->env->from->personal = m_strdup(b);
1564 hdr->date_sent = mutt_parse_date (b, hdr);
1565 hdr->received = hdr->date_sent;
1568 p_delete(&hdr->env->message_id);
1569 hdr->env->message_id = m_strdup(b);
1572 string_list_wipe(&hdr->env->references);
1573 hdr->env->references = mutt_parse_references (b, 0);
1576 hdr->content->length = atoi (b);
1579 hdr->lines = atoi (b);
1583 p_delete(&hdr->env->xref);
1584 b = b + 6; /* skips the "Xref: " */
1585 hdr->env->xref = m_strdup(b);
1586 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1588 rfc2047_decode_envelope(hdr->env);
1604 static int nntp_fetch_numbers (char *line, void *c)
1606 FETCH_CONTEXT *fc = c;
1609 int num = atoi(line);
1610 if (num < fc->first || num > fc->last)
1613 bit_set(&fc->messages, num);
1618 static int add_xover_line (char *line, void *c)
1621 FETCH_CONTEXT *fc = c;
1622 CONTEXT *ctx = fc->ctx;
1623 nntp_data_t *data = (nntp_data_t *) ctx->data;
1628 if (ctx->msgcount >= ctx->hdrmax)
1629 mx_alloc_memory (ctx);
1630 ctx->hdrs[ctx->msgcount] = header_new();
1631 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1633 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1634 num = ctx->hdrs[ctx->msgcount]->article_num;
1636 if (num >= fc->first && num <= fc->last) {
1638 if (num > data->lastLoaded)
1639 data->lastLoaded = num;
1640 num = num - fc->first + 1;
1641 total = fc->last - fc->first + 1;
1642 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1643 mutt_message ("%s %d/%d", fc->msg, num, total);
1645 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1652 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1654 char buf[HUGE_STRING];
1655 const char *msg = _("Fetching message headers...");
1656 const char *msg2 = _("Fetching headers from cache...");
1657 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1658 int ret, num, oldmsgcount, current;
1662 /* if empty group or nothing to do */
1663 if (!last || first > last)
1666 /* fetch list of articles */
1667 mutt_message _("Fetching list of articles...");
1673 /* CACHE: must be loaded xover cache here */
1674 num = nntp_data->lastCached - first + 1;
1675 if (nntp.use_cache && nntp_data->cache && num > 0) {
1676 nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
1677 mutt_message (msg2);
1679 if ((f = safe_fopen (buf, "r"))) {
1682 /* counting number of lines */
1683 while (fgets (buf, sizeof (buf), f) != NULL)
1686 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1688 oldmsgcount = ctx->msgcount;
1690 fc.last = first + num - 1;
1692 while (fgets (buf, sizeof (buf), f) != NULL) {
1693 if (ReadInc && ((++c) % ReadInc == 0))
1694 mutt_message ("%s %d/%d", msg2, c, r);
1695 add_xover_line (buf, &fc);
1698 nntp_data->lastLoaded = fc.last;
1699 first = fc.last + 1;
1700 if (ctx->msgcount > oldmsgcount)
1701 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1704 nntp_delete_cache (nntp_data);
1706 num = last - first + 1;
1712 * Without XOVER, we have to fetch each article header and parse
1713 * it. With XOVER, we ask for all of them
1716 if (nntp_data->nserv->hasXOVER) {
1717 oldmsgcount = ctx->msgcount;
1721 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1722 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1723 if (ctx->msgcount > oldmsgcount)
1724 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1726 mutt_error (_("XOVER command failed: %s"), buf);
1730 bits_init(&fc.messages);
1732 if (nntp_data->nserv->hasLISTGROUP) {
1733 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1734 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1735 nntp_fetch_numbers, &fc, 0))
1737 mutt_error (_("LISTGROUP command failed: %s"), buf);
1739 bits_wipe(&fc.messages);
1743 for (num = first; num <= last; num++)
1744 bit_set(&fc.messages, num);
1747 for (current = first; current <= last; current++) {
1750 ret = current - first + 1;
1751 mutt_message ("%s %d/%d", msg, ret, num);
1753 if (!bit_isset(&fc.messages, current))
1756 if (ctx->msgcount >= ctx->hdrmax)
1757 mx_alloc_memory (ctx);
1758 h = ctx->hdrs[ctx->msgcount] = header_new();
1759 h->index = ctx->msgcount;
1761 ret = nntp_read_header (ctx, NULL, current);
1762 if (ret == 0) { /* Got article. Fetch next header */
1763 nntp_get_status (ctx, h, NULL, h->article_num);
1765 mx_update_context (ctx, 1);
1768 header_delete(&h); /* skip it */
1770 bits_wipe(&fc.messages);
1774 if (current > nntp_data->lastLoaded)
1775 nntp_data->lastLoaded = current;
1777 bits_wipe(&fc.messages);
1780 nntp_data->lastLoaded = last;
1781 mutt_clear_error ();
1786 * currently, nntp "mailbox" is "newsgroup"
1788 static int nntp_open_mailbox (CONTEXT * ctx)
1790 nntp_data_t *nntp_data;
1791 nntp_server_t *serv;
1792 char buf[HUGE_STRING];
1793 char server[LONG_STRING];
1794 int count = 0, first;
1799 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1800 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1806 nntp_expand_path (server, sizeof (server), &act);
1807 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1810 CurrentNewsSrv = serv;
1812 /* create NNTP-specific state struct if nof found in list */
1813 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1814 nntp_data = nntp_data_new();
1815 nntp_data->group = m_strdup(buf);
1816 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1817 serv->tail = nntp_data_list_append(serv->tail, nntp_data);
1819 ctx->data = nntp_data;
1820 nntp_data->nserv = serv;
1822 mutt_message (_("Selecting %s..."), nntp_data->group);
1824 if (!nntp_data->desc) {
1825 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1826 if (nntp_data->desc)
1827 nntp_save_cache_index (serv);
1831 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1835 if (m_strncmp("211", buf, 3)) {
1838 /* GROUP command failed */
1839 if (!m_strncmp("411", buf, 3)) {
1840 mutt_error (_("Newsgroup %s not found on server %s"),
1841 nntp_data->group, serv->conn->account.host);
1843 /* CACHE: delete cache and line from .index */
1844 nntp_delete_cache (nntp_data);
1845 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1846 for (l = &serv->list; *l; l = &(*l)->next) {
1847 if ((*l) == nntp_data) {
1848 nntp_data_list_pop(l);
1849 nntp_data_delete(&nntp_data);
1855 nntp_data_delete(&nntp_data);
1862 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1863 &nntp_data->lastMessage, buf);
1865 nntp_data->deleted = 0;
1867 time (&serv->check_time);
1870 * Check for max adding context. If it is greater than $nntp_context,
1871 * strip off extra articles
1873 first = nntp_data->firstMessage;
1874 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1875 first = nntp_data->lastMessage - NntpContext + 1;
1877 nntp_data->lastLoaded = first - 1;
1878 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1881 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1883 char buf[LONG_STRING];
1884 char path[_POSIX_PATH_MAX];
1889 /* see if we already have the message in our cache */
1891 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1894 /* if everything is fine, assign msg->fp and return */
1895 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1896 (msg->fp = fopen (cache->path, "r")))
1899 /* clear the previous entry */
1900 unlink (cache->path);
1901 p_delete(&cache->path);
1903 cache->index = ctx->hdrs[msgno]->index;
1904 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1908 cache->path = m_strdup(path);
1910 if (ctx->hdrs[msgno]->article_num == 0)
1911 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1912 ctx->hdrs[msgno]->env->message_id);
1914 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1915 ctx->hdrs[msgno]->article_num);
1917 bar.msg = _("Fetching message...");
1919 mutt_progress_bar (&bar, 0);
1921 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1922 msg->fp, ctx->tagged);
1924 mutt_error (_("Article %d not found on server"),
1925 ctx->hdrs[msgno]->article_num);
1931 p_delete(&cache->path);
1935 envelope_delete(&ctx->hdrs[msgno]->env);
1936 ctx->hdrs[msgno]->env =
1937 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1938 /* fix content length */
1939 fseeko (msg->fp, 0, SEEK_END);
1940 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1941 ctx->hdrs[msgno]->content->offset;
1943 /* this is called in mutt before the open which fetches the message,
1944 * which is probably wrong, but we just call it again here to handle
1945 * the problem instead of fixing it.
1947 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1949 /* These would normally be updated in mx_update_context(), but the
1950 * full headers aren't parsed with XOVER, so the information wasn't
1953 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1955 mutt_clear_error ();
1962 int nntp_post (const char *msg)
1964 char buf[LONG_STRING];
1967 nntp_data_t *nntp_data;
1969 if (Context && Context->magic == M_NNTP)
1970 nntp_data = (nntp_data_t *)Context->data;
1972 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
1973 !CurrentNewsSrv->list)
1975 mutt_error (_("Can't post article. No connection to news server."));
1978 nntp_data = CurrentNewsSrv->list;
1981 if (!(f = safe_fopen (msg, "r"))) {
1982 mutt_error (_("Can't post article. Unable to open %s"), msg);
1986 m_strcpy(buf, sizeof(buf), "POST\r\n");
1987 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1988 mutt_error (_("Can't post article. Connection to %s lost."),
1989 nntp_data->nserv->conn->account.host);
1992 if (buf[0] != '3') {
1993 mutt_error (_("Can't post article: %s"), buf);
1999 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
2000 len = m_strlen(buf);
2001 if (buf[len - 1] == '\n') {
2002 buf[len - 1] = '\r';
2008 mutt_socket_write(nntp_data->nserv->conn, buf);
2010 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2014 if (buf[m_strlen(buf) - 1] != '\n')
2015 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2016 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2017 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2018 mutt_error (_("Can't post article. Connection to %s lost."),
2019 nntp_data->nserv->conn->account.host);
2022 if (buf[0] != '2') {
2023 mutt_error (_("Can't post article: %s"), buf);
2030 /* nntp_logout_all: close all open connections. */
2031 void nntp_logout_all (void)
2033 char buf[LONG_STRING];
2036 conn = mutt_socket_head ();
2039 CONNECTION* next = conn->next;
2040 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2041 mutt_message (_("Closing connection to %s..."), conn->account.host);
2042 mutt_socket_write (conn, "QUIT\r\n");
2043 mutt_socket_readln (buf, sizeof (buf), conn);
2044 mutt_clear_error ();
2045 mutt_socket_close (conn);
2046 mutt_socket_free (conn);
2052 static void nntp_free_acache (nntp_data_t * data)
2056 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2057 if (data->acache[i].path) {
2058 unlink (data->acache[i].path);
2059 p_delete(&data->acache[i].path);
2064 void nntp_data_wipe(nntp_data_t *data)
2066 p_delete(&data->entries);
2067 p_delete(&data->desc);
2068 p_delete(&data->cache);
2069 p_delete(&data->group);
2070 nntp_free_acache(data);
2073 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2075 nntp_data_t *data = ctx->data;
2077 nntp_save_cache_group(ctx);
2078 nntp_free_acache(data);
2080 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2084 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2086 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2090 nntp_free_acache (data);
2091 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2093 nntp_save_cache_index (data->nserv);
2094 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2096 nntp_data_delete(&data);
2098 nntp_sync_sidebar (data);
2101 /* commit changes and terminate connection */
2102 int nntp_close_mailbox (CONTEXT * ctx)
2106 mutt_message _("Quitting newsgroup...");
2109 nntp_data_t *data = (nntp_data_t *) ctx->data;
2112 if (data->nserv && data->nserv->conn && ctx->unread) {
2113 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2115 mutt_newsgroup_catchup (data->nserv, data->group);
2120 nntp_sync_mailbox (ctx, 0, NULL);
2121 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2122 nntp_server_t *news;
2124 news = ((nntp_data_t *) ctx->data)->nserv;
2125 newsrc_gen_entries (ctx);
2126 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2127 mutt_newsrc_update (news);
2129 mutt_clear_error ();
2133 /* use the GROUP command to poll for new mail */
2134 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2136 char buf[LONG_STRING];
2139 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2143 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2146 if (m_strncmp("211", buf, 3)) {
2148 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2152 if (!m_strncmp("211", buf, 3)) {
2156 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2157 nntp_data->firstMessage = first;
2158 nntp_data->lastMessage = last;
2159 if (ctx && last > nntp_data->lastLoaded) {
2160 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2161 time (&nntp_data->nserv->check_time);
2164 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2165 nntp_data->unread = count;
2167 mutt_newsgroup_stat (nntp_data);
2168 /* active was renumbered? */
2169 if (last < nntp_data->lastLoaded) {
2170 if (!nntp_data->max) {
2171 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2174 nntp_data->lastCached = 0;
2176 nntp_data->entries[0].first = 1;
2177 nntp_data->entries[0].last = 0;
2179 nntp_sync_sidebar (nntp_data);
2182 time (&nntp_data->nserv->check_time);
2186 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2188 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2191 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2193 char buf[LONG_STRING];
2194 nntp_data_t nntp_data;
2195 nntp_data_t *l, **emp;
2199 if (!serv || !serv->newgroups_time)
2202 if (nntp_open_connection (serv) < 0)
2205 /* check subscribed groups for new news */
2206 if (option (OPTSHOWNEWNEWS)) {
2207 mutt_message _("Checking for new messages...");
2209 for (l = serv->list; l; l = l->next) {
2210 serv->check_time = 0; /* really check! */
2212 _nntp_check_mailbox (NULL, l);
2219 mutt_message _("Checking for new newsgroups...");
2221 now = serv->newgroups_time;
2222 time (&serv->newgroups_time);
2224 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2225 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2226 t->tm_min, t->tm_sec);
2227 nntp_data.nserv = serv;
2228 if (Context && Context->magic == M_NNTP)
2229 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2231 nntp_data.group = NULL;
2233 emp = nntp_data_list_last(&serv->list);
2234 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2235 add_group, serv, 0) != 0) {
2239 mutt_message _("Loading descriptions...");
2241 for (l = *emp; l; l = l->next) {
2243 nntp_get_desc(l, l->group, NULL, NULL);
2246 nntp_save_cache_index(serv);
2248 return nntp.checked;
2251 /* Load list of all newsgroups from active */
2252 int nntp_get_active (nntp_server_t * serv)
2255 nntp_data_t nntp_data, **tmp = &serv->list;
2257 if (nntp_open_connection (serv) < 0)
2260 snprintf (msg, sizeof (msg),
2261 _("Loading list of all newsgroups on server %s..."),
2262 serv->conn->account.host);
2264 time (&serv->newgroups_time);
2265 nntp_data.nserv = serv;
2266 nntp_data.group = NULL;
2268 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2272 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2274 nntp_get_desc (&nntp_data, "*", msg, NULL);
2277 if ((*tmp)->deleted && !(*tmp)->rc) {
2278 nntp_data_t *d = nntp_data_list_pop(tmp);
2279 nntp_delete_cache(d);
2280 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2281 nntp_data_delete(&d);
2283 tmp = &(*tmp)->next;
2287 nntp_save_cache_index(serv);
2289 mutt_clear_error ();
2290 return nntp.checked;
2294 * returns -1 if error ocurred while retrieving header,
2295 * number of articles which ones exist in context on success.
2297 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2301 /* if msgid is already in context, don't reload them */
2302 if (hash_find (ctx->id_hash, msgid))
2304 if (ctx->msgcount == ctx->hdrmax)
2305 mx_alloc_memory (ctx);
2306 ctx->hdrs[ctx->msgcount] = header_new();
2307 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2309 mutt_message (_("Fetching %s from server..."), msgid);
2310 ret = nntp_read_header (ctx, msgid, 0);
2311 /* since nntp_read_header() may set read flag, we must reset it */
2312 ctx->hdrs[ctx->msgcount]->read = 0;
2314 header_delete(&ctx->hdrs[ctx->msgcount]);
2317 mx_update_context (ctx, 1);
2330 static int check_children (char *s, void *c)
2332 CHILD_CONTEXT *cc = c;
2335 if (!s || (n = atoi (s)) == 0)
2337 for (i = 0; i < cc->ctx->msgcount; i++)
2338 if (cc->ctx->hdrs[i]->article_num == n)
2340 if (cc->num >= cc->max)
2341 p_realloc(&cc->child, cc->max += 25);
2342 cc->child[cc->num++] = n;
2347 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2349 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2351 int i, ret = 0, tmp = 0;
2352 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2354 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2355 !nntp_data->nserv->conn->account.host)
2357 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2359 if (!nntp_data->nserv->hasXPAT) {
2360 mutt_error (_("Server %s does not support this operation!"),
2361 nntp_data->nserv->conn->account.host);
2365 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2366 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2368 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2369 p_delete(&cc.child);
2373 /* dont try to read the xover cache. check_children() already
2374 * made sure that we dont have the article, so we need to visit
2375 * the server. Reading the cache at this point is also bad
2376 * because it would duplicate messages */
2377 if (nntp.use_cache) {
2379 nntp.use_cache = false;
2381 for (i = 0; i < cc.num; i++) {
2382 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2384 if (ctx->msgcount &&
2385 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2386 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2389 nntp.use_cache = true;
2390 p_delete(&cc.child);
2394 static int nntp_is_magic (const char* path, struct stat* st) {
2395 url_scheme_t s = url_check_scheme(NONULL(path));
2396 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2399 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2401 case ACL_INSERT: /* editing messages */
2402 case ACL_WRITE: /* change importance */
2404 case ACL_DELETE: /* (un)deletion */
2405 case ACL_SEEN: /* mark as read */
2412 mx_t const nntp_mx = {
2422 nntp_fastclose_mailbox,