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);
50 data->unread -= MAX(0, last - first + 1);
54 static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line)
57 char group[LONG_STRING];
61 for (p = line; *p; p++) {
65 p = strpbrk(line, ":!");
69 m_strncpy(group, ssizeof(group), line, p - line);
70 data = hash_find(news->newsgroups, group);
72 data = nntp_data_new();
73 data->group = p_dupstr(line, p - line);
76 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
77 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
78 hash_insert(news->newsgroups, data->group, data);
79 nntp_data_list_append(&news->list, data);
81 p_delete(&data->entries);
85 data->entries = p_new(NEWSRC_ENTRY, x * 2);
87 data->subscribed = (*p++ == ':');
89 for (x = 0; *p; p++) {
91 data->entries[x].first = strtol(p, (char **)&p, 10);
94 data->entries[x].last = strtol(p + 1, (char **)&p, 10);
96 data->entries[x].last = data->entries[x].first;
99 p = strchrnul(p, ',');
100 x += data->entries[x].last != 0;
103 if (x && !data->lastMessage)
104 data->lastMessage = data->entries[x - 1].last;
106 mutt_newsgroup_stat(data);
110 static int slurp_newsrc (nntp_server_t * news)
117 news->stat = stat(news->newsrc, &sb);
118 news->size = sb.st_size;
119 news->mtime = sb.st_mtime;
121 if ((fp = safe_fopen(news->newsrc, "r")) == NULL)
124 /* hmm, should we use dotlock? */
125 if (mx_lock_file(news->newsrc, fileno (fp), 0, 0, 1)) {
130 while (getline(&buf, &n, fp) >= 0) {
131 nntp_parse_newsrc_line(news, buf);
135 mx_unlock_file (news->newsrc, fileno (fp), 0);
140 #define nntp_cache_expand(dst, dlen, fmt, ...) \
142 snprintf((dst), (dlen), "%s/" fmt, mod_core.cachedir, ##__VA_ARGS__);\
143 mutt_expand_path((dst), (dlen)); \
146 /* Loads $news_cache_dir/.index into memory, loads newsserver data
147 * and newsgroup cache names */
148 static int nntp_parse_cacheindex(nntp_server_t *news)
150 char buf[HUGE_STRING];
153 p_delete(&news->cache);
154 if (m_strisempty(mod_core.cachedir))
157 nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host);
158 if (!(idx = safe_fopen(buf, "a+")))
162 while (fgets(buf, sizeof(buf), idx)) {
163 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
164 if (!m_strncmp(buf, "#: ", 3)
165 && !m_strcasecmp(buf + 3, news->conn->account.host)) {
170 while (fgets(buf, sizeof(buf), idx)) {
174 if (m_strstart(buf, "#:", NULL))
177 p = strchrnul(buf, ' ');
182 q = strchrnul(p, ' ');
186 l = strtol(q, &q, 10);
187 m = strtol(q, &q, 10);
189 if (!m_strcmp(buf, "ALL")) {
190 m_strreplace(&news->cache, m_strdup(p));
191 news->newgroups_time = m;
195 if (news->newsgroups) {
196 nntp_data_t *data = hash_find(news->newsgroups, buf);
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 m_strreplace(&data->cache, p);
210 t = !data->firstMessage || data->lastMessage < m;
211 if (!data->firstMessage)
212 data->firstMessage = l;
213 if (data->lastMessage < m)
214 data->lastMessage = m;
215 data->lastCached = m;
216 if (t || !data->unread)
217 mutt_newsgroup_stat(data);
225 /* nntp_parse_url: given an NNPT URL, return host, port,
226 * username, password and newsgroup will recognise. */
227 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
236 act->port = NNTP_PORT;
237 act->type = M_ACCT_TYPE_NNTP;
239 c = m_strdup(server);
240 url_parse_ciss (&url, c);
242 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
243 if (url.scheme == U_NNTPS) {
245 act->port = NNTP_SSL_PORT;
250 m_strcpy(group, group_len, url.path);
252 ret = mutt_account_fromurl (act, &url);
259 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
263 url.path = m_strdup(line);
264 mutt_account_tourl (act, &url);
265 url_ciss_tostring (&url, line, len, 0);
269 static int add_group (char *buf, void *serv)
271 nntp_server_t *s = serv;
272 char group[LONG_STRING], mod, desc[HUGE_STRING];
274 nntp_data_t *nntp_data;
277 nntp.checked = n; /* nntp.checked have N, where N = number of groups */
278 if (!buf) /* at EOF must be zerouth */
285 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
288 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
290 nntp_data = nntp_data_new();
291 nntp_data->group = m_strdup(group);
292 nntp_data->nserv = s;
293 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
294 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
295 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
296 nntp_data_list_append(&s->list, nntp_data);
298 nntp_data->deleted = 0;
299 nntp_data->firstMessage = first;
300 nntp_data->lastMessage = last;
302 nntp_data->allowed = 1;
304 nntp_data->allowed = 0;
306 p_delete(&nntp_data->desc);
308 nntp_data->desc = m_strdup(desc);
309 if (nntp_data->rc || nntp_data->lastCached)
310 mutt_newsgroup_stat (nntp_data);
311 else if (nntp_data->lastMessage &&
312 nntp_data->firstMessage <= nntp_data->lastMessage)
313 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
315 nntp_data->unread = 0;
320 /* Load list of all newsgroups from cache ALL */
321 static int nntp_get_cache_all (nntp_server_t * serv)
323 char buf[HUGE_STRING];
326 nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
327 if ((f = safe_fopen (buf, "r"))) {
330 while (fgets (buf, sizeof (buf), f) != NULL) {
331 if (ReadInc && (i % ReadInc == 0))
332 mutt_message (_("Loading list from cache... %d"), i);
333 add_group (buf, serv);
336 add_group (NULL, NULL);
342 p_delete(&serv->cache);
348 * Automatically loads a newsrc into memory, if necessary.
349 * Checks the size/mtime of a newsrc file, if it doesn't match, load
350 * again. Hmm, if a system has broken mtimes, this might mean the file
351 * is reloaded every time, which we'd have to fix.
353 * a newsrc file is a line per newsgroup, with the newsgroup, then a
354 * ':' denoting subscribed or '!' denoting unsubscribed, then a
355 * comma separated list of article numbers and ranges.
357 nntp_server_t *mutt_select_newsserver (char *server)
359 char file[_POSIX_PATH_MAX];
368 if (!server || !*server) {
369 mutt_error _("No newsserver defined!");
374 buf = p = p_new(char, m_strlen(server) + 10);
375 if (url_check_scheme (server) == U_UNKNOWN) {
376 strcpy (buf, "nntp://");
377 p = strchr (buf, '\0');
381 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
383 mutt_error (_("%s is an invalid newsserver specification!"), server);
388 conn = mutt_conn_find (NULL, &act);
392 nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
393 serv = (nntp_server_t *) conn->data;
397 /* externally modified? */
398 if (serv->stat != stat(file, &sb) || (!serv->stat &&
399 (serv->size != sb.st_size
400 || serv->mtime != sb.st_mtime)))
402 for (list = serv->list; list; list = list->next) {
403 list->subscribed = list->rc = list->num = 0;
406 nntp_clear_cacheindex (serv);
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);
418 serv->newsrc = m_strdup(file);
419 serv->newsgroups = hash_new(1009, false);
420 slurp_newsrc (serv); /* load .newsrc */
421 nntp_parse_cacheindex (serv); /* load .index */
422 if (nntp.use_cache && serv->cache && nntp_get_cache_all (serv) >= 0)
423 nntp_check_newgroups (serv, 1);
424 else if (nntp_get_active (serv) < 0) {
425 hash_delete(&serv->newsgroups, NULL);
426 nntp_data_list_wipe(&serv->list);
427 p_delete(&serv->newsrc);
428 p_delete(&serv->cache);
432 nntp_clear_cacheindex (serv);
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 /* Remove cache files of unsubscribed newsgroups */
768 void nntp_clear_cacheindex (nntp_server_t * news)
772 if (option (OPTSAVEUNSUB) || !news)
775 for (data = news->list; data; data = data->next) {
776 if (!data || data->subscribed || !data->cache)
778 nntp_delete_cache (data);
783 static int nntp_save_cache_index (nntp_server_t * news)
785 char buf[HUGE_STRING];
786 char file[_POSIX_PATH_MAX];
790 if (!news || !news->newsgroups)
796 nntp_cache_expand(file, ssizeof(file), "%s", news->cache);
798 f = safe_fopen(file, "w");
800 m_strcpy(buf, ssizeof(buf), news->conn->account.host);
801 f = mutt_mkname(buf);
802 news->cache = m_strdup(buf);
803 nntp_cache_expand(file, ssizeof(file), "%s", buf);
808 for (d = news->list; d; d = d->next) {
811 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
812 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
815 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
816 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
817 if (fputs (buf, f) == EOF) {
826 if (nntp_update_cacheindex (news, NULL)) {
833 static int nntp_save_cache_group (CONTEXT * ctx)
835 char buf[HUGE_STRING], addr[STRING];
836 char file[_POSIX_PATH_MAX];
840 int i = 0, save = SORT_ORDER;
845 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
848 if (((nntp_data_t *) ctx->data)->cache) {
849 nntp_cache_expand(file, ssizeof(file), "%s",
850 ((nntp_data_t *)ctx->data)->cache);
852 f = safe_fopen (file, "w");
855 snprintf(buf, ssizeof(buf), "%s-%s",
856 ((nntp_data_t *)ctx->data)->nserv->conn->account.host,
857 ((nntp_data_t *)ctx->data)->group);
858 f = mutt_mkname (buf);
859 ((nntp_data_t *)ctx->data)->cache = m_strdup(buf);
860 nntp_cache_expand(file, ssizeof(file), "%s", buf);
865 if (Sort != SORT_ORDER) {
868 mutt_sort_headers (ctx, 0);
871 /* Save only $nntp_context messages... */
872 ((nntp_data_t *) ctx->data)->lastCached = 0;
873 if (NntpContext && ctx->msgcount > NntpContext)
874 i = ctx->msgcount - NntpContext;
875 for (; i < ctx->msgcount; i++) {
876 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
879 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
880 tm = gmtime (&h->date_sent);
881 snprintf (buf, sizeof (buf),
882 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
883 h->article_num, h->env->subject, addr, tm->tm_mday,
884 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
885 tm->tm_min, tm->tm_sec, h->env->message_id);
887 if (h->env->references)
888 mutt_write_references (h->env->references, f);
889 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
890 h->content->length, h->lines, NONULL (h->env->xref));
891 if (fputs (buf, f) == EOF) {
897 prev = ctx->hdrs[i]->article_num;
902 mutt_sort_headers (ctx, 0);
906 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
907 (nntp_data_t *) ctx->data)) {
911 ((nntp_data_t *) ctx->data)->lastCached =
912 ((nntp_data_t *) ctx->data)->lastLoaded;
916 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
920 if (!news || !news->newsgroups || !group || !*group)
922 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
923 data = nntp_data_new();
924 data->group = m_strdup(group);
927 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
928 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
929 hash_insert (news->newsgroups, data->group, data);
930 nntp_data_list_append(&news->list, data);
932 if (!data->subscribed) {
933 data->subscribed = 1;
939 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
943 if (!news || !news->newsgroups || !group || !*group ||
944 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
946 if (data->subscribed) {
947 data->subscribed = 0;
948 if (!option (OPTSAVEUNSUB))
954 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
958 if (!news || !news->newsgroups || !group || !*group ||
959 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
962 data->entries = p_new(NEWSRC_ENTRY, 5);
966 data->entries[0].first = 1;
968 data->entries[0].last = data->lastMessage;
969 if (Context && Context->data == data) {
972 for (x = 0; x < Context->msgcount; x++)
973 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
978 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
982 if (!news || !news->newsgroups || !group || !*group ||
983 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
986 data->entries = p_new(NEWSRC_ENTRY, 5);
990 data->entries[0].first = 1;
991 data->entries[0].last = data->firstMessage - 1;
992 if (Context && Context->data == data) {
995 data->unread = Context->msgcount;
996 for (x = 0; x < Context->msgcount; x++)
997 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
1000 data->unread = data->lastMessage - data->entries[0].last;
1004 /* this routine gives the first newsgroup with new messages */
1005 void nntp_buffy (char* dst, ssize_t dstlen) {
1009 /* forward to current group */
1010 for (list = CurrentNewsSrv->list; list; list = list->next) {
1011 if (list->subscribed && list->unread
1012 && Context && Context->magic == M_NNTP
1013 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
1025 list = CurrentNewsSrv->list;
1027 for (; list; list = list->next) {
1028 if (list->subscribed && list->unread) {
1029 if (Context && Context->magic == M_NNTP &&
1030 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1033 for (i = 0; i < Context->msgcount; i++)
1034 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1039 m_strcpy(dst, dstlen, list->group);
1052 /* nntp protocol {{{ */
1054 void nntp_sync_sidebar (nntp_data_t* data) {
1062 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1063 data->nserv->conn->account.has_ssl ? "s" : "",
1064 NONULL(data->nserv->conn->account.user),
1065 *data->nserv->conn->account.pass ? ":" : "",
1066 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1067 data->nserv->conn->account.host,
1070 /* bail out if group not found via mailboxes */
1071 if ((i = buffy_lookup (buf)) < 0)
1074 tmp = Incoming.arr[i];
1075 /* copied from browser.c */
1076 if (option (OPTMARKOLD) &&
1077 data->lastCached >= data->firstMessage &&
1078 data->lastCached <= data->lastMessage)
1079 tmp->msg_unread = data->lastMessage - data->lastCached;
1081 tmp->msg_unread = data->unread;
1082 tmp->new = data->unread > 0;
1083 /* this is closest to a "total" count we can get */
1084 tmp->msgcount = data->lastMessage - data->firstMessage;
1087 static int nntp_auth (nntp_server_t * serv)
1089 CONNECTION *conn = serv->conn;
1091 unsigned char flags = conn->account.flags;
1093 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1094 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1095 conn->account.flags = flags;
1099 mutt_message _("Logging in...");
1101 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1102 mutt_socket_write (conn, buf);
1103 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1104 conn->account.flags = flags;
1108 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1109 mutt_socket_write(conn, buf);
1110 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1111 conn->account.flags = flags;
1115 if (m_strncmp("281", buf, 3)) {
1116 conn->account.flags = flags;
1117 mutt_error _("Login failed.");
1126 static int nntp_connect_error (nntp_server_t * serv)
1128 serv->status = NNTP_NONE;
1129 mutt_socket_close (serv->conn);
1130 mutt_error _("Server closed connection!");
1136 static int nntp_connect_and_auth (nntp_server_t * serv)
1138 CONNECTION *conn = serv->conn;
1142 serv->status = NNTP_NONE;
1144 if (mutt_socket_open (conn) < 0)
1147 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1148 return nntp_connect_error (serv);
1150 if (!m_strncmp("200", buf, 3))
1151 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1152 else if (!m_strncmp("201", buf, 3))
1153 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1155 mutt_socket_close(conn);
1157 mutt_error("%s", buf);
1162 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1163 returned codes and messages. */
1164 mutt_socket_write (conn, "MODE READER\r\n");
1165 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1166 return nntp_connect_error (serv);
1168 mutt_socket_write (conn, "STAT\r\n");
1169 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1170 return nntp_connect_error (serv);
1172 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1173 serv->status = NNTP_OK;
1177 rc = nntp_auth (serv);
1179 return nntp_connect_error (serv);
1181 mutt_socket_close (conn);
1182 serv->status = NNTP_BYE;
1186 mutt_socket_close (conn);
1187 mutt_error _("Login failed.");
1192 serv->status = NNTP_OK;
1196 static int nntp_attempt_features (nntp_server_t * serv)
1198 char buf[LONG_STRING];
1199 CONNECTION *conn = serv->conn;
1201 if (serv->feat_known)
1204 mutt_socket_write (conn, "XOVER\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))
1210 mutt_socket_write (conn, "XPAT\r\n");
1211 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1212 return nntp_connect_error (serv);
1213 if (m_strncmp("500", buf, 3))
1216 mutt_socket_write (conn, "LISTGROUP\r\n");
1217 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1218 return (nntp_connect_error (serv));
1219 if (m_strncmp("500", buf, 3))
1220 serv->hasLISTGROUP = 1;
1222 mutt_socket_write (conn, "XGTITLE +\r\n");
1223 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1224 return nntp_connect_error (serv);
1225 if (m_strncmp("500", buf, 3))
1226 serv->hasXGTITLE = 1;
1228 if (!m_strncmp("282", buf, 3)) {
1230 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1231 return nntp_connect_error (serv);
1232 } while (!(buf[0] == '.' && buf[1] == '\0'));
1235 serv->feat_known = 1;
1239 static int nntp_open_connection (nntp_server_t * serv)
1241 if (serv->status == NNTP_OK)
1243 if (serv->status == NNTP_BYE)
1245 if (nntp_connect_and_auth (serv) < 0)
1247 if (nntp_attempt_features (serv) < 0)
1252 static int nntp_reconnect (nntp_server_t * serv)
1256 mutt_socket_close (serv->conn);
1259 if (nntp_connect_and_auth (serv) == 0)
1262 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1263 serv->conn->account.host);
1264 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1265 serv->status = NNTP_BYE;
1271 /* Send data from line[LONG_STRING] and receive answer to same line */
1272 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1274 char buf[LONG_STRING];
1277 if (data->nserv->status == NNTP_BYE)
1282 mutt_socket_write (data->nserv->conn, line);
1284 else if (data->group) {
1285 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1286 mutt_socket_write (data->nserv->conn, buf);
1290 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1291 if (nntp_reconnect (data->nserv) < 0)
1295 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1296 mutt_socket_write (data->nserv->conn, buf);
1297 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1303 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1307 m_strcpy(line, linelen, buf);
1312 * This function calls funct(*line, *data) for each received line,
1313 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1316 * 1 - correct but not performed (may be, have to be continued),
1317 * -1 - conection lost,
1318 * -2 - invalid command or execution error,
1319 * -3 - error in funct(*line, *data).
1321 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1322 const char *msg, progress_t* bar,
1323 int (*funct) (char *, void *),
1324 void *data, int tagged)
1326 char buf[LONG_STRING];
1335 m_strcpy(buf, sizeof(buf), query);
1336 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1345 inbuf = p_new(char, sizeof(buf));
1348 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1353 if (!lenbuf && buf[0] == '.') {
1354 if (buf[1] == '\0') {
1362 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1365 if (chunk >= ssizeof (buf)) {
1366 lenbuf += m_strlen(p);
1370 mutt_progress_bar (bar, pos);
1373 if (ReadInc && (line % ReadInc == 0)) {
1375 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1377 mutt_message ("%s %d", msg, line);
1381 if (ret == 0 && funct (inbuf, data) < 0)
1386 p_realloc(&inbuf, lenbuf + sizeof (buf));
1395 static int nntp_read_tempfile (char *line, void *file)
1397 FILE *f = (FILE *) file;
1403 if (fputc ('\n', f) == EOF)
1409 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1412 register char *p, *b;
1413 register char *colon = NULL;
1417 /* skip to next word */
1419 while (*b && ((*b == ' ') || (*b == '\t')))
1423 /* skip to end of word */
1424 while (*p && (*p != ' ') && (*p != '\t')) {
1436 nntp_get_status (ctx, h, b, atoi (colon));
1437 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1438 h->article_num = atoi (colon);
1446 * 1 if article not found
1447 * -1 if read or write error on tempfile or socket
1449 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1452 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1454 char buf[LONG_STRING];
1455 char tempfile[_POSIX_PATH_MAX];
1457 HEADER *h = ctx->hdrs[ctx->msgcount];
1459 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1464 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1466 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1468 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1472 return (ret == -1 ? -1 : 1);
1475 h->article_num = article_num;
1476 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1480 if (h->env->xref != NULL)
1481 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1482 else if (h->article_num == 0 && msgid) {
1483 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1484 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1485 h->article_num = atoi (buf + 4);
1491 static int parse_description (char *line, void *n)
1493 register char *d = line;
1495 nntp_server_t *news = n;
1499 while (*d && *d != '\t' && *d != ' ')
1503 while (*d && (*d == '\t' || *d == ' '))
1505 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1506 m_strcmp(d, data->desc)) {
1507 p_delete(&data->desc);
1508 data->desc = m_strdup(d);
1513 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1517 if (!option (OPTLOADDESC) || !data || !data->nserv)
1520 /* Get newsgroup description, if we can */
1521 if (data->nserv->hasXGTITLE)
1522 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1524 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1525 mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0);
1529 * XOVER returns a tab separated list of:
1530 * id|subject|from|date|Msgid|references|bytes|lines|xref
1532 * This has to duplicate some of the functionality of
1533 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1534 * a limited number of headers which are "parsed" by placement in the list)
1536 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1538 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1542 hdr->env = envelope_new();
1543 hdr->env->newsgroups = m_strdup(nntp_data->group);
1544 hdr->content = body_new();
1545 hdr->content->type = TYPETEXT;
1546 hdr->content->subtype = m_strdup("plain");
1547 hdr->content->encoding = ENC7BIT;
1548 hdr->content->disposition = DISPINLINE;
1549 hdr->content->length = -1;
1552 for (x = 0; !done && x < 9; x++) {
1553 /* if from file, need to skip newline character */
1554 while (*p && *p != '\n' && *p != '\t')
1562 hdr->article_num = atoi (b);
1563 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1566 hdr->env->subject = m_strdup(b);
1569 address_list_wipe(&hdr->env->from);
1570 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1571 /* same as for mutt_parse_rfc822_line():
1572 * don't leave from info NULL if there's an invalid address (or
1573 * whatever) in From: field; mutt would just display it as empty
1574 * and mark mail/(esp.) news article as your own. aaargh! this
1575 * bothered me for _years_ */
1576 if (!hdr->env->from) {
1577 hdr->env->from = address_new ();
1578 hdr->env->from->personal = m_strdup(b);
1582 hdr->date_sent = mutt_parse_date (b, hdr);
1583 hdr->received = hdr->date_sent;
1586 p_delete(&hdr->env->message_id);
1587 hdr->env->message_id = m_strdup(b);
1590 string_list_wipe(&hdr->env->references);
1591 hdr->env->references = mutt_parse_references (b, 0);
1594 hdr->content->length = atoi (b);
1597 hdr->lines = atoi (b);
1601 p_delete(&hdr->env->xref);
1602 b = b + 6; /* skips the "Xref: " */
1603 hdr->env->xref = m_strdup(b);
1604 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1606 rfc2047_decode_envelope(hdr->env);
1622 static int nntp_fetch_numbers (char *line, void *c)
1624 FETCH_CONTEXT *fc = c;
1627 int num = atoi(line);
1628 if (num < fc->first || num > fc->last)
1631 bit_set(&fc->messages, num);
1636 static int add_xover_line (char *line, void *c)
1639 FETCH_CONTEXT *fc = c;
1640 CONTEXT *ctx = fc->ctx;
1641 nntp_data_t *data = (nntp_data_t *) ctx->data;
1646 if (ctx->msgcount >= ctx->hdrmax)
1647 mx_alloc_memory (ctx);
1648 ctx->hdrs[ctx->msgcount] = header_new();
1649 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1651 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1652 num = ctx->hdrs[ctx->msgcount]->article_num;
1654 if (num >= fc->first && num <= fc->last) {
1656 if (num > data->lastLoaded)
1657 data->lastLoaded = num;
1658 num = num - fc->first + 1;
1659 total = fc->last - fc->first + 1;
1660 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1661 mutt_message ("%s %d/%d", fc->msg, num, total);
1663 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1670 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1672 char buf[HUGE_STRING];
1673 const char *msg = _("Fetching message headers...");
1674 const char *msg2 = _("Fetching headers from cache...");
1675 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1676 int ret, num, oldmsgcount, current;
1680 /* if empty group or nothing to do */
1681 if (!last || first > last)
1684 /* fetch list of articles */
1685 mutt_message _("Fetching list of articles...");
1691 /* CACHE: must be loaded xover cache here */
1692 num = nntp_data->lastCached - first + 1;
1693 if (nntp.use_cache && nntp_data->cache && num > 0) {
1694 nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
1695 mutt_message (msg2);
1697 if ((f = safe_fopen (buf, "r"))) {
1700 /* counting number of lines */
1701 while (fgets (buf, sizeof (buf), f) != NULL)
1704 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1706 oldmsgcount = ctx->msgcount;
1708 fc.last = first + num - 1;
1710 while (fgets (buf, sizeof (buf), f) != NULL) {
1711 if (ReadInc && ((++c) % ReadInc == 0))
1712 mutt_message ("%s %d/%d", msg2, c, r);
1713 add_xover_line (buf, &fc);
1716 nntp_data->lastLoaded = fc.last;
1717 first = fc.last + 1;
1718 if (ctx->msgcount > oldmsgcount)
1719 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1722 nntp_delete_cache (nntp_data);
1724 num = last - first + 1;
1730 * Without XOVER, we have to fetch each article header and parse
1731 * it. With XOVER, we ask for all of them
1734 if (nntp_data->nserv->hasXOVER) {
1735 oldmsgcount = ctx->msgcount;
1739 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1740 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1741 if (ctx->msgcount > oldmsgcount)
1742 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1744 mutt_error (_("XOVER command failed: %s"), buf);
1748 bits_init(&fc.messages);
1750 if (nntp_data->nserv->hasLISTGROUP) {
1751 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1752 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1753 nntp_fetch_numbers, &fc, 0))
1755 mutt_error (_("LISTGROUP command failed: %s"), buf);
1757 bits_wipe(&fc.messages);
1761 for (num = first; num <= last; num++)
1762 bit_set(&fc.messages, num);
1765 for (current = first; current <= last; current++) {
1768 ret = current - first + 1;
1769 mutt_message ("%s %d/%d", msg, ret, num);
1771 if (!bit_isset(&fc.messages, current))
1774 if (ctx->msgcount >= ctx->hdrmax)
1775 mx_alloc_memory (ctx);
1776 h = ctx->hdrs[ctx->msgcount] = header_new();
1777 h->index = ctx->msgcount;
1779 ret = nntp_read_header (ctx, NULL, current);
1780 if (ret == 0) { /* Got article. Fetch next header */
1781 nntp_get_status (ctx, h, NULL, h->article_num);
1783 mx_update_context (ctx, 1);
1786 header_delete(&h); /* skip it */
1788 bits_wipe(&fc.messages);
1792 if (current > nntp_data->lastLoaded)
1793 nntp_data->lastLoaded = current;
1795 bits_wipe(&fc.messages);
1798 nntp_data->lastLoaded = last;
1799 mutt_clear_error ();
1804 * currently, nntp "mailbox" is "newsgroup"
1806 static int nntp_open_mailbox (CONTEXT * ctx)
1808 nntp_data_t *nntp_data;
1809 nntp_server_t *serv;
1810 char buf[HUGE_STRING];
1811 char server[LONG_STRING];
1812 int count = 0, first;
1817 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1818 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1824 nntp_expand_path (server, sizeof (server), &act);
1825 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1828 CurrentNewsSrv = serv;
1830 /* create NNTP-specific state struct if nof found in list */
1831 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1832 nntp_data = nntp_data_new();
1833 nntp_data->group = m_strdup(buf);
1834 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1835 nntp_data_list_append(&serv->list, nntp_data);
1837 ctx->data = nntp_data;
1838 nntp_data->nserv = serv;
1840 mutt_message (_("Selecting %s..."), nntp_data->group);
1842 if (!nntp_data->desc) {
1843 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1844 if (nntp_data->desc)
1845 nntp_save_cache_index (serv);
1849 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1853 if (m_strncmp("211", buf, 3)) {
1856 /* GROUP command failed */
1857 if (!m_strncmp("411", buf, 3)) {
1858 mutt_error (_("Newsgroup %s not found on server %s"),
1859 nntp_data->group, serv->conn->account.host);
1861 /* CACHE: delete cache and line from .index */
1862 nntp_delete_cache (nntp_data);
1863 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1864 for (l = &serv->list; *l; l = &(*l)->next) {
1865 if ((*l) == nntp_data) {
1866 nntp_data_list_pop(l);
1867 nntp_data_delete(&nntp_data);
1871 nntp_data_delete(&nntp_data);
1878 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1879 &nntp_data->lastMessage, buf);
1881 nntp_data->deleted = 0;
1883 time (&serv->check_time);
1886 * Check for max adding context. If it is greater than $nntp_context,
1887 * strip off extra articles
1889 first = nntp_data->firstMessage;
1890 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1891 first = nntp_data->lastMessage - NntpContext + 1;
1893 nntp_data->lastLoaded = first - 1;
1894 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1897 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1899 char buf[LONG_STRING];
1900 char path[_POSIX_PATH_MAX];
1905 /* see if we already have the message in our cache */
1907 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1910 /* if everything is fine, assign msg->fp and return */
1911 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1912 (msg->fp = fopen (cache->path, "r")))
1915 /* clear the previous entry */
1916 unlink (cache->path);
1917 p_delete(&cache->path);
1919 cache->index = ctx->hdrs[msgno]->index;
1920 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1924 cache->path = m_strdup(path);
1926 if (ctx->hdrs[msgno]->article_num == 0)
1927 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1928 ctx->hdrs[msgno]->env->message_id);
1930 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1931 ctx->hdrs[msgno]->article_num);
1933 bar.msg = _("Fetching message...");
1935 mutt_progress_bar (&bar, 0);
1937 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1938 msg->fp, ctx->tagged);
1940 mutt_error (_("Article %d not found on server"),
1941 ctx->hdrs[msgno]->article_num);
1947 p_delete(&cache->path);
1951 envelope_delete(&ctx->hdrs[msgno]->env);
1952 ctx->hdrs[msgno]->env =
1953 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1954 /* fix content length */
1955 fseeko (msg->fp, 0, SEEK_END);
1956 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1957 ctx->hdrs[msgno]->content->offset;
1959 /* this is called in mutt before the open which fetches the message,
1960 * which is probably wrong, but we just call it again here to handle
1961 * the problem instead of fixing it.
1963 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1965 /* These would normally be updated in mx_update_context(), but the
1966 * full headers aren't parsed with XOVER, so the information wasn't
1969 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1971 mutt_clear_error ();
1978 int nntp_post (const char *msg)
1980 char buf[LONG_STRING];
1983 nntp_data_t *nntp_data;
1985 if (Context && Context->magic == M_NNTP)
1986 nntp_data = (nntp_data_t *)Context->data;
1988 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
1989 !CurrentNewsSrv->list)
1991 mutt_error (_("Can't post article. No connection to news server."));
1994 nntp_data = CurrentNewsSrv->list;
1997 if (!(f = safe_fopen (msg, "r"))) {
1998 mutt_error (_("Can't post article. Unable to open %s"), msg);
2002 m_strcpy(buf, sizeof(buf), "POST\r\n");
2003 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2004 mutt_error (_("Can't post article. Connection to %s lost."),
2005 nntp_data->nserv->conn->account.host);
2008 if (buf[0] != '3') {
2009 mutt_error (_("Can't post article: %s"), buf);
2015 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
2016 len = m_strlen(buf);
2017 if (buf[len - 1] == '\n') {
2018 buf[len - 1] = '\r';
2024 mutt_socket_write(nntp_data->nserv->conn, buf);
2026 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2030 if (buf[m_strlen(buf) - 1] != '\n')
2031 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2032 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2033 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2034 mutt_error (_("Can't post article. Connection to %s lost."),
2035 nntp_data->nserv->conn->account.host);
2038 if (buf[0] != '2') {
2039 mutt_error (_("Can't post article: %s"), buf);
2046 /* nntp_logout_all: close all open connections. */
2047 void nntp_logout_all (void)
2049 char buf[LONG_STRING];
2052 conn = mutt_socket_head ();
2055 CONNECTION* next = conn->next;
2056 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2057 mutt_message (_("Closing connection to %s..."), conn->account.host);
2058 mutt_socket_write (conn, "QUIT\r\n");
2059 mutt_socket_readln (buf, sizeof (buf), conn);
2060 mutt_clear_error ();
2061 mutt_socket_close (conn);
2062 mutt_socket_free (conn);
2068 static void nntp_free_acache (nntp_data_t * data)
2072 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2073 if (data->acache[i].path) {
2074 unlink (data->acache[i].path);
2075 p_delete(&data->acache[i].path);
2080 void nntp_data_wipe(nntp_data_t *data)
2082 p_delete(&data->entries);
2083 p_delete(&data->desc);
2084 p_delete(&data->cache);
2085 p_delete(&data->group);
2086 nntp_free_acache(data);
2089 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2091 nntp_data_t *data = ctx->data;
2093 /* CACHE: update cache and .index files */
2094 if ((option (OPTSAVEUNSUB) || data->subscribed))
2095 nntp_save_cache_group (ctx);
2096 nntp_free_acache (data);
2098 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2102 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2104 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2108 nntp_free_acache (data);
2109 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2111 nntp_save_cache_index (data->nserv);
2112 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2114 nntp_data_delete(&data);
2116 nntp_sync_sidebar (data);
2119 /* commit changes and terminate connection */
2120 int nntp_close_mailbox (CONTEXT * ctx)
2124 mutt_message _("Quitting newsgroup...");
2127 nntp_data_t *data = (nntp_data_t *) ctx->data;
2130 if (data->nserv && data->nserv->conn && ctx->unread) {
2131 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2133 mutt_newsgroup_catchup (data->nserv, data->group);
2138 nntp_sync_mailbox (ctx, 0, NULL);
2139 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2140 nntp_server_t *news;
2142 news = ((nntp_data_t *) ctx->data)->nserv;
2143 newsrc_gen_entries (ctx);
2144 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2145 mutt_newsrc_update (news);
2147 mutt_clear_error ();
2151 /* use the GROUP command to poll for new mail */
2152 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2154 char buf[LONG_STRING];
2157 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2161 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2164 if (m_strncmp("211", buf, 3)) {
2166 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2170 if (!m_strncmp("211", buf, 3)) {
2174 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2175 nntp_data->firstMessage = first;
2176 nntp_data->lastMessage = last;
2177 if (ctx && last > nntp_data->lastLoaded) {
2178 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2179 time (&nntp_data->nserv->check_time);
2182 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2183 nntp_data->unread = count;
2185 mutt_newsgroup_stat (nntp_data);
2186 /* active was renumbered? */
2187 if (last < nntp_data->lastLoaded) {
2188 if (!nntp_data->max) {
2189 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2192 nntp_data->lastCached = 0;
2194 nntp_data->entries[0].first = 1;
2195 nntp_data->entries[0].last = 0;
2197 nntp_sync_sidebar (nntp_data);
2200 time (&nntp_data->nserv->check_time);
2204 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2206 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2209 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2211 char buf[LONG_STRING];
2212 nntp_data_t nntp_data;
2213 nntp_data_t *l, **emp;
2217 if (!serv || !serv->newgroups_time)
2220 if (nntp_open_connection (serv) < 0)
2223 /* check subscribed groups for new news */
2224 if (option (OPTSHOWNEWNEWS)) {
2225 mutt_message _("Checking for new messages...");
2227 for (l = serv->list; l; l = l->next) {
2228 serv->check_time = 0; /* really check! */
2230 _nntp_check_mailbox (NULL, l);
2237 mutt_message _("Checking for new newsgroups...");
2239 now = serv->newgroups_time;
2240 time (&serv->newgroups_time);
2242 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2243 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2244 t->tm_min, t->tm_sec);
2245 nntp_data.nserv = serv;
2246 if (Context && Context->magic == M_NNTP)
2247 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2249 nntp_data.group = NULL;
2251 emp = nntp_data_list_last(&serv->list);
2252 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2253 add_group, serv, 0) != 0) {
2257 mutt_message _("Loading descriptions...");
2259 for (l = *emp; l; l = l->next) {
2261 nntp_get_desc(l, l->group, NULL, NULL);
2264 nntp_save_cache_index(serv);
2266 return nntp.checked;
2269 /* Load list of all newsgroups from active */
2270 int nntp_get_active (nntp_server_t * serv)
2273 nntp_data_t nntp_data, **tmp = &serv->list;
2275 if (nntp_open_connection (serv) < 0)
2278 snprintf (msg, sizeof (msg),
2279 _("Loading list of all newsgroups on server %s..."),
2280 serv->conn->account.host);
2282 time (&serv->newgroups_time);
2283 nntp_data.nserv = serv;
2284 nntp_data.group = NULL;
2286 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2290 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2292 nntp_get_desc (&nntp_data, "*", msg, NULL);
2295 if ((*tmp)->deleted && !(*tmp)->rc) {
2296 nntp_data_t *d = nntp_data_list_pop(tmp);
2297 nntp_delete_cache(d);
2298 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2299 nntp_data_delete(&d);
2301 tmp = &(*tmp)->next;
2304 nntp_save_cache_index (serv);
2306 mutt_clear_error ();
2307 return nntp.checked;
2311 * returns -1 if error ocurred while retrieving header,
2312 * number of articles which ones exist in context on success.
2314 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2318 /* if msgid is already in context, don't reload them */
2319 if (hash_find (ctx->id_hash, msgid))
2321 if (ctx->msgcount == ctx->hdrmax)
2322 mx_alloc_memory (ctx);
2323 ctx->hdrs[ctx->msgcount] = header_new();
2324 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2326 mutt_message (_("Fetching %s from server..."), msgid);
2327 ret = nntp_read_header (ctx, msgid, 0);
2328 /* since nntp_read_header() may set read flag, we must reset it */
2329 ctx->hdrs[ctx->msgcount]->read = 0;
2331 header_delete(&ctx->hdrs[ctx->msgcount]);
2334 mx_update_context (ctx, 1);
2347 static int check_children (char *s, void *c)
2349 CHILD_CONTEXT *cc = c;
2352 if (!s || (n = atoi (s)) == 0)
2354 for (i = 0; i < cc->ctx->msgcount; i++)
2355 if (cc->ctx->hdrs[i]->article_num == n)
2357 if (cc->num >= cc->max)
2358 p_realloc(&cc->child, cc->max += 25);
2359 cc->child[cc->num++] = n;
2364 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2366 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2368 int i, ret = 0, tmp = 0;
2369 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2371 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2372 !nntp_data->nserv->conn->account.host)
2374 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2376 if (!nntp_data->nserv->hasXPAT) {
2377 mutt_error (_("Server %s does not support this operation!"),
2378 nntp_data->nserv->conn->account.host);
2382 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2383 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2385 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2386 p_delete(&cc.child);
2390 /* dont try to read the xover cache. check_children() already
2391 * made sure that we dont have the article, so we need to visit
2392 * the server. Reading the cache at this point is also bad
2393 * because it would duplicate messages */
2394 if (nntp.use_cache) {
2396 nntp.use_cache = false;
2398 for (i = 0; i < cc.num; i++) {
2399 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2401 if (ctx->msgcount &&
2402 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2403 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2406 nntp.use_cache = true;
2407 p_delete(&cc.child);
2411 static int nntp_is_magic (const char* path, struct stat* st) {
2412 url_scheme_t s = url_check_scheme(NONULL(path));
2413 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2416 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2418 case ACL_INSERT: /* editing messages */
2419 case ACL_WRITE: /* change importance */
2421 case ACL_DELETE: /* (un)deletion */
2422 case ACL_SEEN: /* mark as read */
2429 mx_t const nntp_mx = {
2439 nntp_fastclose_mailbox,