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++ == ':');
91 for (x = 0; *p; p++) {
92 data->entries[x].first = strtol(p, (char **)&p, 10);
93 p += strcspn(p, "-,");
97 data->entries[x].last = strtol(p + 1, (char **)&p, 10);
99 data->entries[x].last = data->entries[x].first;
100 p = strchrnul(p, ',');
102 x += data->entries[x].last != 0;
105 if (x && !data->lastMessage)
106 data->lastMessage = data->entries[x - 1].last;
108 mutt_newsgroup_stat(data);
112 static int slurp_newsrc (nntp_server_t * news)
119 news->stat = stat(news->newsrc, &sb);
120 news->size = sb.st_size;
121 news->mtime = sb.st_mtime;
123 if ((fp = safe_fopen(news->newsrc, "r")) == NULL)
126 /* hmm, should we use dotlock? */
127 if (mx_lock_file(news->newsrc, fileno (fp), 0, 0, 1)) {
132 while (getline(&buf, &n, fp) >= 0) {
133 nntp_parse_newsrc_line(news, buf);
137 mx_unlock_file (news->newsrc, fileno (fp), 0);
142 #define nntp_cache_expand(dst, dlen, fmt, ...) \
144 snprintf((dst), (dlen), "%s/" fmt, mod_core.cachedir, ##__VA_ARGS__);\
145 mutt_expand_path((dst), (dlen)); \
148 /* Loads $news_cache_dir/.index into memory, loads newsserver data
149 * and newsgroup cache names */
150 static int nntp_parse_cacheindex(nntp_server_t *news)
152 char buf[HUGE_STRING];
155 p_delete(&news->cache);
156 if (m_strisempty(mod_core.cachedir))
159 nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host);
160 if (!(idx = safe_fopen(buf, "a+")))
164 while (fgets(buf, sizeof(buf), idx)) {
165 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
166 if (!m_strncmp(buf, "#: ", 3)
167 && !m_strcasecmp(buf + 3, news->conn->account.host)) {
172 while (fgets(buf, sizeof(buf), idx)) {
176 if (m_strstart(buf, "#:", NULL))
179 p = strchrnul(buf, ' ');
184 q = strchrnul(p, ' ');
188 l = strtol(q, &q, 10);
189 m = strtol(q, &q, 10);
191 if (!m_strcmp(buf, "ALL")) {
192 m_strreplace(&news->cache, m_strdup(p));
193 news->newgroups_time = m;
197 if (news->newsgroups) {
198 nntp_data_t *data = hash_find(news->newsgroups, buf);
201 data = nntp_data_new();
202 data->group = m_strdup(buf);
205 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
206 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
207 hash_insert (news->newsgroups, data->group, data);
208 news->tail = nntp_data_list_append(news->tail, data);
210 m_strreplace(&data->cache, p);
212 t = !data->firstMessage || data->lastMessage < m;
213 if (!data->firstMessage)
214 data->firstMessage = l;
215 if (data->lastMessage < m)
216 data->lastMessage = m;
217 data->lastCached = m;
218 if (t || !data->unread)
219 mutt_newsgroup_stat(data);
227 /* nntp_parse_url: given an NNPT URL, return host, port,
228 * username, password and newsgroup will recognise. */
229 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
238 act->port = NNTP_PORT;
239 act->type = M_ACCT_TYPE_NNTP;
241 c = m_strdup(server);
242 url_parse_ciss (&url, c);
244 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
245 if (url.scheme == U_NNTPS) {
247 act->port = NNTP_SSL_PORT;
252 m_strcpy(group, group_len, url.path);
254 ret = mutt_account_fromurl (act, &url);
261 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
265 url.path = m_strdup(line);
266 mutt_account_tourl (act, &url);
267 url_ciss_tostring (&url, line, len, 0);
271 static int add_group (char *buf, void *serv)
273 nntp_server_t *s = serv;
274 char group[LONG_STRING], mod, desc[HUGE_STRING];
276 nntp_data_t *nntp_data;
279 nntp.checked = n; /* nntp.checked have N, where N = number of groups */
280 if (!buf) /* at EOF must be zerouth */
287 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
290 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
292 nntp_data = nntp_data_new();
293 nntp_data->group = m_strdup(group);
294 nntp_data->nserv = s;
295 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
296 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
297 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
298 s->tail = nntp_data_list_append(s->tail, nntp_data);
300 nntp_data->deleted = 0;
301 nntp_data->firstMessage = first;
302 nntp_data->lastMessage = last;
304 nntp_data->allowed = 1;
306 nntp_data->allowed = 0;
308 p_delete(&nntp_data->desc);
310 nntp_data->desc = m_strdup(desc);
311 if (nntp_data->rc || nntp_data->lastCached)
312 mutt_newsgroup_stat (nntp_data);
313 else if (nntp_data->lastMessage &&
314 nntp_data->firstMessage <= nntp_data->lastMessage)
315 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
317 nntp_data->unread = 0;
322 /* Load list of all newsgroups from cache ALL */
323 static int nntp_get_cache_all (nntp_server_t * serv)
325 char buf[HUGE_STRING];
328 nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
329 if ((f = safe_fopen (buf, "r"))) {
332 while (fgets (buf, sizeof (buf), f) != NULL) {
333 if (ReadInc && (i % ReadInc == 0))
334 mutt_message (_("Loading list from cache... %d"), i);
335 add_group (buf, serv);
338 add_group (NULL, NULL);
344 p_delete(&serv->cache);
350 * Automatically loads a newsrc into memory, if necessary.
351 * Checks the size/mtime of a newsrc file, if it doesn't match, load
352 * again. Hmm, if a system has broken mtimes, this might mean the file
353 * is reloaded every time, which we'd have to fix.
355 * a newsrc file is a line per newsgroup, with the newsgroup, then a
356 * ':' denoting subscribed or '!' denoting unsubscribed, then a
357 * comma separated list of article numbers and ranges.
359 nntp_server_t *mutt_select_newsserver (char *server)
361 char file[_POSIX_PATH_MAX];
370 if (!server || !*server) {
371 mutt_error _("No newsserver defined!");
376 buf = p = p_new(char, m_strlen(server) + 10);
377 if (url_check_scheme (server) == U_UNKNOWN) {
378 strcpy (buf, "nntp://");
379 p = strchr (buf, '\0');
383 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
385 mutt_error (_("%s is an invalid newsserver specification!"), server);
390 conn = mutt_conn_find (NULL, &act);
394 nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
395 serv = (nntp_server_t *) conn->data;
399 /* externally modified? */
400 if (serv->stat != stat(file, &sb) || (!serv->stat &&
401 (serv->size != sb.st_size
402 || serv->mtime != sb.st_mtime)))
404 for (list = serv->list; list; list = list->next) {
405 list->subscribed = list->rc = list->num = 0;
410 if (serv->status == NNTP_BYE)
411 serv->status = NNTP_NONE;
412 nntp_check_newgroups (serv, 0);
417 serv = p_new(nntp_server_t, 1);
418 serv->tail = &serv->list;
420 serv->newsrc = m_strdup(file);
421 serv->newsgroups = hash_new(SHRT_MAX, false);
422 slurp_newsrc (serv); /* load .newsrc */
423 nntp_parse_cacheindex (serv); /* load .index */
424 if (nntp.use_cache && serv->cache && nntp_get_cache_all (serv) >= 0)
425 nntp_check_newgroups (serv, 1);
426 else if (nntp_get_active (serv) < 0) {
427 hash_delete(&serv->newsgroups, NULL);
428 nntp_data_list_wipe(&serv->list);
429 p_delete(&serv->newsrc);
430 p_delete(&serv->cache);
434 conn->data = (void *) serv;
440 * full status flags are not supported by nntp, but we can fake some
441 * of them. This is how:
442 * Read = a read message number is in the .newsrc
443 * New = a message is new since we last read this newsgroup
444 * Old = anything else
445 * So, Read is marked as such in the newsrc, old is anything that is
446 * "skipped" in the newsrc, and new is anything not in the newsrc nor
447 * in the cache. By skipped, I mean before the last unread message
450 nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article)
452 nntp_data_t *data = ctx->data;
455 data = hash_find(data->nserv->newsgroups, group);
459 for (int i = 0; i < data->num; i++) {
460 if ((article >= data->entries[i].first) &&
461 (article <= data->entries[i].last))
463 /* we cannot use mutt_set_flag() because mx_update_context()
470 /* If article was not cached yet, it is new! :) */
471 if (!data->cache || article > data->lastCached)
474 /* Old articles are articles which aren't read but an article after them
476 if (option(OPTMARKOLD))
480 static void nntp_create_newsrc_line(nntp_data_t *data, buffer_t *buf)
482 buffer_addstr(buf, data->group);
483 buffer_addch(buf, data->subscribed ? ':' : '!');
484 buffer_addch(buf, ' ');
486 for (int x = 0; x < data->num; x++) {
488 buffer_addch(buf, ',');
491 buffer_addf(buf, "%d-%d", data->entries[x].first,
492 data->entries[x].last);
494 buffer_addch(buf, '\n');
497 static void newsrc_gen_entries (CONTEXT * ctx)
499 nntp_data_t *data = (nntp_data_t *) ctx->data;
501 int last = 0, first = 1;
502 int save_sort = SORT_ORDER;
504 if (Sort != SORT_ORDER) {
507 mutt_sort_headers (ctx, 0);
511 data->entries = p_new(NEWSRC_ENTRY, 5);
516 * Set up to fake initial sequence from 1 to the article before the
517 * first article in our list
522 for (x = 0; x < ctx->msgcount; x++) {
523 if (series) { /* search for first unread */
525 * We don't actually check sequential order, since we mark
526 * "missing" entries as read/deleted
528 last = ctx->hdrs[x]->article_num;
529 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
530 !ctx->hdrs[x]->read) {
531 if (data->num >= data->max) {
532 data->max = data->max * 2;
533 p_realloc(&data->entries, data->max);
535 data->entries[data->num].first = first;
536 data->entries[data->num].last = last - 1;
541 else { /* search for first read */
543 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
547 last = ctx->hdrs[x]->article_num;
550 if (series && first <= data->lastLoaded) {
551 if (data->num >= data->max) {
552 data->max = data->max * 2;
553 p_realloc(&data->entries, data->max);
555 data->entries[data->num].first = first;
556 data->entries[data->num].last = data->lastLoaded;
560 if (save_sort != Sort) {
562 mutt_sort_headers (ctx, 0);
566 static int mutt_update_list_file (char *filename, char *section,
567 const char *key, const char *line)
571 char buf[HUGE_STRING];
572 char tmpf[_POSIX_PATH_MAX], lnk[_POSIX_PATH_MAX];
574 int ext = 0, done = 0, r = 0, l = 0;
576 /* if file not exist, create it */
577 if ((ifp = safe_fopen (filename, "a")))
579 if (!(ifp = safe_fopen (filename, "r"))) {
580 mutt_error (_("Unable to open %s for reading"), filename);
583 if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) {
585 mutt_error (_("Unable to lock %s"), filename);
589 /* use m_tempfile() to get a tempfile in the same
590 * directory as filename is so that we can follow symlinks
591 * via rename(2); as dirname(2) may modify its argument,
592 * temporarily use buf as copy of it
594 m_strcpy(buf, sizeof(buf), filename);
595 ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
598 mutt_error (_("Unable to open %s for writing"), tmpf);
603 while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) {
604 r = fputs (buf, ofp);
606 while (*c && *c != '\n') c++;
607 c[0] = 0; /* strip EOL */
608 if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
611 if (r != EOF && !done) {
612 snprintf (buf, sizeof(buf), "#: %s\n", section);
613 r = fputs (buf, ofp);
618 while (r != EOF && fgets (buf, sizeof (buf), ifp)) {
621 while (*c && (*c != '\r') && (*c != '\n')) c++;
623 if (*c != '\\') ext = 0;
624 } else if ((section && !strncmp (buf, "#: ", 3))) {
629 r = fputs (buf, ofp);
632 } else if (key && !strncmp (buf, key, strlen(key)) &&
633 (!*key || buf[strlen(key)] == ' ')) {
636 while (*c && (*c != '\r') && (*c != '\n')) c++;
638 if (*c == '\\') ext = 1;
640 r = fputs (line, ofp);
642 r = fputc ('\n', ofp);
646 r = fputs (buf, ofp);
650 while (r != EOF && fgets (buf, sizeof (buf), ifp))
651 r = fputs (buf, ofp);
653 /* If there wasn't a line to replace, put it on the end of the file */
654 if (r != EOF && !done && line) {
656 r = fputc ('\n', ofp);
658 mx_unlock_file (filename, fileno (ifp), 0);
663 mutt_error (_("Can't write %s"), tmpf);
667 if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
669 if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
671 mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
677 int mutt_newsrc_update (nntp_server_t * news)
688 /* we will generate full newsrc here */
689 for (data = news->list; data; data = data->next) {
690 if (!data || !data->rc)
692 nntp_create_newsrc_line(data, &buf);
694 /* newrc being fully rewritten */
696 && (r = mutt_update_list_file(news->newsrc, NULL, "", buf.data)) == 0)
700 stat (news->newsrc, &st);
701 news->size = st.st_size;
702 news->mtime = st.st_mtime;
709 static FILE *mutt_mkname (char *s)
711 char buf[_POSIX_PATH_MAX], *pc;
715 nntp_cache_expand(buf, ssizeof(buf), "%s", s);
716 if ((fp = safe_fopen (buf, "w")))
719 nntp_cache_expand(buf, ssizeof(buf), "cache-XXXXXX");
720 pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
721 if ((fd = mkstemp (buf)) == -1)
723 strcpy (s, pc); /* generated name */
724 return fdopen (fd, "w");
727 /* Updates info into .index file: ALL or about selected newsgroup */
728 static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
730 char buf[LONG_STRING];
731 char file[_POSIX_PATH_MAX];
732 const char *key = "ALL";
734 if (!serv || !serv->conn || !serv->conn->account.host)
737 if (data && data->group) {
739 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
740 data->firstMessage, data->lastLoaded);
743 m_strcpy(file, sizeof(file), serv->cache);
744 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
745 (int) serv->newgroups_time);
747 nntp_cache_expand(file, ssizeof(file), "%s.index", serv->conn->account.host);
748 return mutt_update_list_file(file, serv->conn->account.host, key, buf);
751 static void nntp_delete_cache (nntp_data_t * data)
753 char buf[_POSIX_PATH_MAX];
755 if (!nntp.use_cache || !data || !data->cache || !data->nserv)
758 nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache);
760 p_delete(&data->cache);
761 data->lastCached = 0;
762 nntp_cache_expand(buf, ssizeof(buf), "%s.index",
763 data->nserv->conn->account.host);
764 mutt_update_list_file(buf, data->nserv->conn->account.host, data->group,
768 static int nntp_save_cache_index (nntp_server_t * news)
770 char buf[HUGE_STRING];
771 char file[_POSIX_PATH_MAX];
775 if (!news || !news->newsgroups)
781 nntp_cache_expand(file, ssizeof(file), "%s", news->cache);
783 f = safe_fopen(file, "w");
785 m_strcpy(buf, ssizeof(buf), news->conn->account.host);
786 f = mutt_mkname(buf);
787 news->cache = m_strdup(buf);
788 nntp_cache_expand(file, ssizeof(file), "%s", buf);
793 for (d = news->list; d; d = d->next) {
796 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
797 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
800 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
801 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
802 if (fputs (buf, f) == EOF) {
811 if (nntp_update_cacheindex (news, NULL)) {
818 static int nntp_save_cache_group (CONTEXT * ctx)
820 char buf[HUGE_STRING], addr[STRING];
821 char file[_POSIX_PATH_MAX];
825 int i = 0, save = SORT_ORDER;
830 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
833 if (((nntp_data_t *) ctx->data)->cache) {
834 nntp_cache_expand(file, ssizeof(file), "%s",
835 ((nntp_data_t *)ctx->data)->cache);
837 f = safe_fopen (file, "w");
840 snprintf(buf, ssizeof(buf), "%s-%s",
841 ((nntp_data_t *)ctx->data)->nserv->conn->account.host,
842 ((nntp_data_t *)ctx->data)->group);
843 f = mutt_mkname (buf);
844 ((nntp_data_t *)ctx->data)->cache = m_strdup(buf);
845 nntp_cache_expand(file, ssizeof(file), "%s", buf);
850 if (Sort != SORT_ORDER) {
853 mutt_sort_headers (ctx, 0);
856 /* Save only $nntp_context messages... */
857 ((nntp_data_t *) ctx->data)->lastCached = 0;
858 if (NntpContext && ctx->msgcount > NntpContext)
859 i = ctx->msgcount - NntpContext;
860 for (; i < ctx->msgcount; i++) {
861 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
864 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
865 tm = gmtime (&h->date_sent);
866 snprintf (buf, sizeof (buf),
867 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
868 h->article_num, h->env->subject, addr, tm->tm_mday,
869 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
870 tm->tm_min, tm->tm_sec, h->env->message_id);
872 if (h->env->references)
873 mutt_write_references (h->env->references, f);
874 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
875 h->content->length, h->lines, NONULL (h->env->xref));
876 if (fputs (buf, f) == EOF) {
882 prev = ctx->hdrs[i]->article_num;
887 mutt_sort_headers (ctx, 0);
891 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
892 (nntp_data_t *) ctx->data)) {
896 ((nntp_data_t *) ctx->data)->lastCached =
897 ((nntp_data_t *) ctx->data)->lastLoaded;
901 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
905 if (!news || !news->newsgroups || !group || !*group)
907 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
908 data = nntp_data_new();
909 data->group = m_strdup(group);
912 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
913 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
914 hash_insert (news->newsgroups, data->group, data);
915 news->tail = nntp_data_list_append(news->tail, data);
917 if (!data->subscribed) {
918 data->subscribed = 1;
924 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
928 if (!news || !news->newsgroups || !group || !*group ||
929 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
931 if (data->subscribed) {
932 data->subscribed = 0;
937 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
941 if (!news || !news->newsgroups || !group || !*group ||
942 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
945 data->entries = p_new(NEWSRC_ENTRY, 5);
949 data->entries[0].first = 1;
951 data->entries[0].last = data->lastMessage;
952 if (Context && Context->data == data) {
955 for (x = 0; x < Context->msgcount; x++)
956 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
961 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
965 if (!news || !news->newsgroups || !group || !*group ||
966 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
969 data->entries = p_new(NEWSRC_ENTRY, 5);
973 data->entries[0].first = 1;
974 data->entries[0].last = data->firstMessage - 1;
975 if (Context && Context->data == data) {
978 data->unread = Context->msgcount;
979 for (x = 0; x < Context->msgcount; x++)
980 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
983 data->unread = data->lastMessage - data->entries[0].last;
987 /* this routine gives the first newsgroup with new messages */
988 void nntp_buffy (char* dst, ssize_t dstlen) {
992 /* forward to current group */
993 for (list = CurrentNewsSrv->list; list; list = list->next) {
994 if (list->subscribed && list->unread
995 && Context && Context->magic == M_NNTP
996 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
1008 list = CurrentNewsSrv->list;
1010 for (; list; list = list->next) {
1011 if (list->subscribed && list->unread) {
1012 if (Context && Context->magic == M_NNTP &&
1013 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1016 for (i = 0; i < Context->msgcount; i++)
1017 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1022 m_strcpy(dst, dstlen, list->group);
1035 /* nntp protocol {{{ */
1037 void nntp_sync_sidebar (nntp_data_t* data) {
1045 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1046 data->nserv->conn->account.has_ssl ? "s" : "",
1047 NONULL(data->nserv->conn->account.user),
1048 *data->nserv->conn->account.pass ? ":" : "",
1049 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1050 data->nserv->conn->account.host,
1053 /* bail out if group not found via mailboxes */
1054 if ((i = buffy_lookup (buf)) < 0)
1057 tmp = Incoming.arr[i];
1058 /* copied from browser.c */
1059 if (option (OPTMARKOLD) &&
1060 data->lastCached >= data->firstMessage &&
1061 data->lastCached <= data->lastMessage)
1062 tmp->msg_unread = data->lastMessage - data->lastCached;
1064 tmp->msg_unread = data->unread;
1065 tmp->new = data->unread > 0;
1066 /* this is closest to a "total" count we can get */
1067 tmp->msgcount = data->lastMessage - data->firstMessage;
1070 static int nntp_auth (nntp_server_t * serv)
1072 CONNECTION *conn = serv->conn;
1074 unsigned char flags = conn->account.flags;
1076 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1077 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1078 conn->account.flags = flags;
1082 mutt_message _("Logging in...");
1084 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1085 mutt_socket_write (conn, buf);
1086 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1087 conn->account.flags = flags;
1091 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1092 mutt_socket_write(conn, buf);
1093 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1094 conn->account.flags = flags;
1098 if (m_strncmp("281", buf, 3)) {
1099 conn->account.flags = flags;
1100 mutt_error _("Login failed.");
1109 static int nntp_connect_error (nntp_server_t * serv)
1111 serv->status = NNTP_NONE;
1112 mutt_socket_close (serv->conn);
1113 mutt_error _("Server closed connection!");
1119 static int nntp_connect_and_auth (nntp_server_t * serv)
1121 CONNECTION *conn = serv->conn;
1125 serv->status = NNTP_NONE;
1127 if (mutt_socket_open (conn) < 0)
1130 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1131 return nntp_connect_error (serv);
1133 if (!m_strncmp("200", buf, 3))
1134 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1135 else if (!m_strncmp("201", buf, 3))
1136 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1138 mutt_socket_close(conn);
1140 mutt_error("%s", buf);
1145 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1146 returned codes and messages. */
1147 mutt_socket_write (conn, "MODE READER\r\n");
1148 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1149 return nntp_connect_error (serv);
1151 mutt_socket_write (conn, "STAT\r\n");
1152 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1153 return nntp_connect_error (serv);
1155 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1156 serv->status = NNTP_OK;
1160 rc = nntp_auth (serv);
1162 return nntp_connect_error (serv);
1164 mutt_socket_close (conn);
1165 serv->status = NNTP_BYE;
1169 mutt_socket_close (conn);
1170 mutt_error _("Login failed.");
1175 serv->status = NNTP_OK;
1179 static int nntp_attempt_features (nntp_server_t * serv)
1181 char buf[LONG_STRING];
1182 CONNECTION *conn = serv->conn;
1184 if (serv->feat_known)
1187 mutt_socket_write (conn, "XOVER\r\n");
1188 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1189 return nntp_connect_error (serv);
1190 if (m_strncmp("500", buf, 3))
1193 mutt_socket_write (conn, "XPAT\r\n");
1194 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1195 return nntp_connect_error (serv);
1196 if (m_strncmp("500", buf, 3))
1199 mutt_socket_write (conn, "LISTGROUP\r\n");
1200 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1201 return (nntp_connect_error (serv));
1202 if (m_strncmp("500", buf, 3))
1203 serv->hasLISTGROUP = 1;
1205 mutt_socket_write (conn, "XGTITLE +\r\n");
1206 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1207 return nntp_connect_error (serv);
1208 if (m_strncmp("500", buf, 3))
1209 serv->hasXGTITLE = 1;
1211 if (!m_strncmp("282", buf, 3)) {
1213 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1214 return nntp_connect_error (serv);
1215 } while (!(buf[0] == '.' && buf[1] == '\0'));
1218 serv->feat_known = 1;
1222 static int nntp_open_connection (nntp_server_t * serv)
1224 if (serv->status == NNTP_OK)
1226 if (serv->status == NNTP_BYE)
1228 if (nntp_connect_and_auth (serv) < 0)
1230 if (nntp_attempt_features (serv) < 0)
1235 static int nntp_reconnect (nntp_server_t * serv)
1239 mutt_socket_close (serv->conn);
1242 if (nntp_connect_and_auth (serv) == 0)
1245 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1246 serv->conn->account.host);
1247 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1248 serv->status = NNTP_BYE;
1254 /* Send data from line[LONG_STRING] and receive answer to same line */
1255 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1257 char buf[LONG_STRING];
1260 if (data->nserv->status == NNTP_BYE)
1265 mutt_socket_write (data->nserv->conn, line);
1267 else if (data->group) {
1268 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1269 mutt_socket_write (data->nserv->conn, buf);
1273 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1274 if (nntp_reconnect (data->nserv) < 0)
1278 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1279 mutt_socket_write (data->nserv->conn, buf);
1280 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1286 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1290 m_strcpy(line, linelen, buf);
1295 * This function calls funct(*line, *data) for each received line,
1296 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1299 * 1 - correct but not performed (may be, have to be continued),
1300 * -1 - conection lost,
1301 * -2 - invalid command or execution error,
1302 * -3 - error in funct(*line, *data).
1304 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1305 const char *msg, progress_t* bar,
1306 int (*funct) (char *, void *),
1307 void *data, int tagged)
1309 char buf[LONG_STRING];
1318 m_strcpy(buf, sizeof(buf), query);
1319 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1328 inbuf = p_new(char, sizeof(buf));
1331 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1336 if (!lenbuf && buf[0] == '.') {
1337 if (buf[1] == '\0') {
1345 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1348 if (chunk >= ssizeof (buf)) {
1349 lenbuf += m_strlen(p);
1353 mutt_progress_bar (bar, pos);
1356 if (ReadInc && (line % ReadInc == 0)) {
1358 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1360 mutt_message ("%s %d", msg, line);
1364 if (ret == 0 && funct (inbuf, data) < 0)
1369 p_realloc(&inbuf, lenbuf + sizeof (buf));
1378 static int nntp_read_tempfile (char *line, void *file)
1380 FILE *f = (FILE *) file;
1386 if (fputc ('\n', f) == EOF)
1392 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1395 register char *p, *b;
1396 register char *colon = NULL;
1400 /* skip to next word */
1402 while (*b && ((*b == ' ') || (*b == '\t')))
1406 /* skip to end of word */
1407 while (*p && (*p != ' ') && (*p != '\t')) {
1419 nntp_get_status (ctx, h, b, atoi (colon));
1420 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1421 h->article_num = atoi (colon);
1429 * 1 if article not found
1430 * -1 if read or write error on tempfile or socket
1432 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1435 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1437 char buf[LONG_STRING];
1438 char tempfile[_POSIX_PATH_MAX];
1440 HEADER *h = ctx->hdrs[ctx->msgcount];
1442 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1447 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1449 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1451 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1455 return (ret == -1 ? -1 : 1);
1458 h->article_num = article_num;
1459 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1463 if (h->env->xref != NULL)
1464 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1465 else if (h->article_num == 0 && msgid) {
1466 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1467 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1468 h->article_num = atoi (buf + 4);
1474 static int parse_description (char *line, void *n)
1476 register char *d = line;
1478 nntp_server_t *news = n;
1482 while (*d && *d != '\t' && *d != ' ')
1486 while (*d && (*d == '\t' || *d == ' '))
1488 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1489 m_strcmp(d, data->desc)) {
1490 p_delete(&data->desc);
1491 data->desc = m_strdup(d);
1496 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1500 if (!option (OPTLOADDESC) || !data || !data->nserv)
1503 /* Get newsgroup description, if we can */
1504 if (data->nserv->hasXGTITLE)
1505 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1507 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1508 mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0);
1512 * XOVER returns a tab separated list of:
1513 * id|subject|from|date|Msgid|references|bytes|lines|xref
1515 * This has to duplicate some of the functionality of
1516 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1517 * a limited number of headers which are "parsed" by placement in the list)
1519 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1521 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1525 hdr->env = envelope_new();
1526 hdr->env->newsgroups = m_strdup(nntp_data->group);
1527 hdr->content = body_new();
1528 hdr->content->type = TYPETEXT;
1529 hdr->content->subtype = m_strdup("plain");
1530 hdr->content->encoding = ENC7BIT;
1531 hdr->content->disposition = DISPINLINE;
1532 hdr->content->length = -1;
1535 for (x = 0; !done && x < 9; x++) {
1536 /* if from file, need to skip newline character */
1537 while (*p && *p != '\n' && *p != '\t')
1545 hdr->article_num = atoi (b);
1546 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1549 hdr->env->subject = m_strdup(b);
1552 address_list_wipe(&hdr->env->from);
1553 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1554 /* same as for mutt_parse_rfc822_line():
1555 * don't leave from info NULL if there's an invalid address (or
1556 * whatever) in From: field; mutt would just display it as empty
1557 * and mark mail/(esp.) news article as your own. aaargh! this
1558 * bothered me for _years_ */
1559 if (!hdr->env->from) {
1560 hdr->env->from = address_new ();
1561 hdr->env->from->personal = m_strdup(b);
1565 hdr->date_sent = mutt_parse_date (b, hdr);
1566 hdr->received = hdr->date_sent;
1569 p_delete(&hdr->env->message_id);
1570 hdr->env->message_id = m_strdup(b);
1573 string_list_wipe(&hdr->env->references);
1574 hdr->env->references = mutt_parse_references (b, 0);
1577 hdr->content->length = atoi (b);
1580 hdr->lines = atoi (b);
1584 p_delete(&hdr->env->xref);
1585 b = b + 6; /* skips the "Xref: " */
1586 hdr->env->xref = m_strdup(b);
1587 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1589 rfc2047_decode_envelope(hdr->env);
1605 static int nntp_fetch_numbers (char *line, void *c)
1607 FETCH_CONTEXT *fc = c;
1610 int num = atoi(line);
1611 if (num < fc->first || num > fc->last)
1614 bit_set(&fc->messages, num);
1619 static int add_xover_line (char *line, void *c)
1622 FETCH_CONTEXT *fc = c;
1623 CONTEXT *ctx = fc->ctx;
1624 nntp_data_t *data = (nntp_data_t *) ctx->data;
1629 if (ctx->msgcount >= ctx->hdrmax)
1630 mx_alloc_memory (ctx);
1631 ctx->hdrs[ctx->msgcount] = header_new();
1632 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1634 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1635 num = ctx->hdrs[ctx->msgcount]->article_num;
1637 if (num >= fc->first && num <= fc->last) {
1639 if (num > data->lastLoaded)
1640 data->lastLoaded = num;
1641 num = num - fc->first + 1;
1642 total = fc->last - fc->first + 1;
1643 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1644 mutt_message ("%s %d/%d", fc->msg, num, total);
1646 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1653 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1655 char buf[HUGE_STRING];
1656 const char *msg = _("Fetching message headers...");
1657 const char *msg2 = _("Fetching headers from cache...");
1658 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1659 int ret, num, oldmsgcount, current;
1663 /* if empty group or nothing to do */
1664 if (!last || first > last)
1667 /* fetch list of articles */
1668 mutt_message _("Fetching list of articles...");
1674 /* CACHE: must be loaded xover cache here */
1675 num = nntp_data->lastCached - first + 1;
1676 if (nntp.use_cache && nntp_data->cache && num > 0) {
1677 nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
1678 mutt_message (msg2);
1680 if ((f = safe_fopen (buf, "r"))) {
1683 /* counting number of lines */
1684 while (fgets (buf, sizeof (buf), f) != NULL)
1687 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1689 oldmsgcount = ctx->msgcount;
1691 fc.last = first + num - 1;
1693 while (fgets (buf, sizeof (buf), f) != NULL) {
1694 if (ReadInc && ((++c) % ReadInc == 0))
1695 mutt_message ("%s %d/%d", msg2, c, r);
1696 add_xover_line (buf, &fc);
1699 nntp_data->lastLoaded = fc.last;
1700 first = fc.last + 1;
1701 if (ctx->msgcount > oldmsgcount)
1702 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1705 nntp_delete_cache (nntp_data);
1707 num = last - first + 1;
1713 * Without XOVER, we have to fetch each article header and parse
1714 * it. With XOVER, we ask for all of them
1717 if (nntp_data->nserv->hasXOVER) {
1718 oldmsgcount = ctx->msgcount;
1722 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1723 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1724 if (ctx->msgcount > oldmsgcount)
1725 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1727 mutt_error (_("XOVER command failed: %s"), buf);
1731 bits_init(&fc.messages);
1733 if (nntp_data->nserv->hasLISTGROUP) {
1734 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1735 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1736 nntp_fetch_numbers, &fc, 0))
1738 mutt_error (_("LISTGROUP command failed: %s"), buf);
1740 bits_wipe(&fc.messages);
1744 for (num = first; num <= last; num++)
1745 bit_set(&fc.messages, num);
1748 for (current = first; current <= last; current++) {
1751 ret = current - first + 1;
1752 mutt_message ("%s %d/%d", msg, ret, num);
1754 if (!bit_isset(&fc.messages, current))
1757 if (ctx->msgcount >= ctx->hdrmax)
1758 mx_alloc_memory (ctx);
1759 h = ctx->hdrs[ctx->msgcount] = header_new();
1760 h->index = ctx->msgcount;
1762 ret = nntp_read_header (ctx, NULL, current);
1763 if (ret == 0) { /* Got article. Fetch next header */
1764 nntp_get_status (ctx, h, NULL, h->article_num);
1766 mx_update_context (ctx, 1);
1769 header_delete(&h); /* skip it */
1771 bits_wipe(&fc.messages);
1775 if (current > nntp_data->lastLoaded)
1776 nntp_data->lastLoaded = current;
1778 bits_wipe(&fc.messages);
1781 nntp_data->lastLoaded = last;
1782 mutt_clear_error ();
1787 * currently, nntp "mailbox" is "newsgroup"
1789 static int nntp_open_mailbox (CONTEXT * ctx)
1791 nntp_data_t *nntp_data;
1792 nntp_server_t *serv;
1793 char buf[HUGE_STRING];
1794 char server[LONG_STRING];
1795 int count = 0, first;
1800 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1801 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1807 nntp_expand_path (server, sizeof (server), &act);
1808 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1811 CurrentNewsSrv = serv;
1813 /* create NNTP-specific state struct if nof found in list */
1814 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1815 nntp_data = nntp_data_new();
1816 nntp_data->group = m_strdup(buf);
1817 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1818 serv->tail = nntp_data_list_append(serv->tail, nntp_data);
1820 ctx->data = nntp_data;
1821 nntp_data->nserv = serv;
1823 mutt_message (_("Selecting %s..."), nntp_data->group);
1825 if (!nntp_data->desc) {
1826 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1827 if (nntp_data->desc)
1828 nntp_save_cache_index (serv);
1832 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1836 if (m_strncmp("211", buf, 3)) {
1839 /* GROUP command failed */
1840 if (!m_strncmp("411", buf, 3)) {
1841 mutt_error (_("Newsgroup %s not found on server %s"),
1842 nntp_data->group, serv->conn->account.host);
1844 /* CACHE: delete cache and line from .index */
1845 nntp_delete_cache (nntp_data);
1846 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1847 for (l = &serv->list; *l; l = &(*l)->next) {
1848 if ((*l) == nntp_data) {
1849 nntp_data_list_pop(l);
1850 nntp_data_delete(&nntp_data);
1856 nntp_data_delete(&nntp_data);
1863 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1864 &nntp_data->lastMessage, buf);
1866 nntp_data->deleted = 0;
1868 time (&serv->check_time);
1871 * Check for max adding context. If it is greater than $nntp_context,
1872 * strip off extra articles
1874 first = nntp_data->firstMessage;
1875 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1876 first = nntp_data->lastMessage - NntpContext + 1;
1878 nntp_data->lastLoaded = first - 1;
1879 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1882 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1884 char buf[LONG_STRING];
1885 char path[_POSIX_PATH_MAX];
1890 /* see if we already have the message in our cache */
1892 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1895 /* if everything is fine, assign msg->fp and return */
1896 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1897 (msg->fp = fopen (cache->path, "r")))
1900 /* clear the previous entry */
1901 unlink (cache->path);
1902 p_delete(&cache->path);
1904 cache->index = ctx->hdrs[msgno]->index;
1905 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1909 cache->path = m_strdup(path);
1911 if (ctx->hdrs[msgno]->article_num == 0)
1912 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1913 ctx->hdrs[msgno]->env->message_id);
1915 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1916 ctx->hdrs[msgno]->article_num);
1918 bar.msg = _("Fetching message...");
1920 mutt_progress_bar (&bar, 0);
1922 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1923 msg->fp, ctx->tagged);
1925 mutt_error (_("Article %d not found on server"),
1926 ctx->hdrs[msgno]->article_num);
1932 p_delete(&cache->path);
1936 envelope_delete(&ctx->hdrs[msgno]->env);
1937 ctx->hdrs[msgno]->env =
1938 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1939 /* fix content length */
1940 fseeko (msg->fp, 0, SEEK_END);
1941 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1942 ctx->hdrs[msgno]->content->offset;
1944 /* this is called in mutt before the open which fetches the message,
1945 * which is probably wrong, but we just call it again here to handle
1946 * the problem instead of fixing it.
1948 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1950 /* These would normally be updated in mx_update_context(), but the
1951 * full headers aren't parsed with XOVER, so the information wasn't
1954 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1956 mutt_clear_error ();
1963 int nntp_post (const char *msg)
1965 char buf[LONG_STRING];
1968 nntp_data_t *nntp_data;
1970 if (Context && Context->magic == M_NNTP)
1971 nntp_data = (nntp_data_t *)Context->data;
1973 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
1974 !CurrentNewsSrv->list)
1976 mutt_error (_("Can't post article. No connection to news server."));
1979 nntp_data = CurrentNewsSrv->list;
1982 if (!(f = safe_fopen (msg, "r"))) {
1983 mutt_error (_("Can't post article. Unable to open %s"), msg);
1987 m_strcpy(buf, sizeof(buf), "POST\r\n");
1988 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1989 mutt_error (_("Can't post article. Connection to %s lost."),
1990 nntp_data->nserv->conn->account.host);
1993 if (buf[0] != '3') {
1994 mutt_error (_("Can't post article: %s"), buf);
2000 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
2001 len = m_strlen(buf);
2002 if (buf[len - 1] == '\n') {
2003 buf[len - 1] = '\r';
2009 mutt_socket_write(nntp_data->nserv->conn, buf);
2011 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2015 if (buf[m_strlen(buf) - 1] != '\n')
2016 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2017 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2018 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2019 mutt_error (_("Can't post article. Connection to %s lost."),
2020 nntp_data->nserv->conn->account.host);
2023 if (buf[0] != '2') {
2024 mutt_error (_("Can't post article: %s"), buf);
2031 /* nntp_logout_all: close all open connections. */
2032 void nntp_logout_all (void)
2034 char buf[LONG_STRING];
2037 conn = mutt_socket_head ();
2040 CONNECTION* next = conn->next;
2041 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2042 mutt_message (_("Closing connection to %s..."), conn->account.host);
2043 mutt_socket_write (conn, "QUIT\r\n");
2044 mutt_socket_readln (buf, sizeof (buf), conn);
2045 mutt_clear_error ();
2046 mutt_socket_close (conn);
2047 mutt_socket_free (conn);
2053 static void nntp_free_acache (nntp_data_t * data)
2057 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2058 if (data->acache[i].path) {
2059 unlink (data->acache[i].path);
2060 p_delete(&data->acache[i].path);
2065 void nntp_data_wipe(nntp_data_t *data)
2067 p_delete(&data->entries);
2068 p_delete(&data->desc);
2069 p_delete(&data->cache);
2070 p_delete(&data->group);
2071 nntp_free_acache(data);
2074 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2076 nntp_data_t *data = ctx->data;
2078 nntp_save_cache_group(ctx);
2079 nntp_free_acache(data);
2081 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2085 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2087 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2091 nntp_free_acache (data);
2092 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2094 nntp_save_cache_index (data->nserv);
2095 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2097 nntp_data_delete(&data);
2099 nntp_sync_sidebar (data);
2102 /* commit changes and terminate connection */
2103 int nntp_close_mailbox (CONTEXT * ctx)
2107 mutt_message _("Quitting newsgroup...");
2110 nntp_data_t *data = (nntp_data_t *) ctx->data;
2113 if (data->nserv && data->nserv->conn && ctx->unread) {
2114 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2116 mutt_newsgroup_catchup (data->nserv, data->group);
2121 nntp_sync_mailbox (ctx, 0, NULL);
2122 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2123 nntp_server_t *news;
2125 news = ((nntp_data_t *) ctx->data)->nserv;
2126 newsrc_gen_entries (ctx);
2127 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2128 mutt_newsrc_update (news);
2130 mutt_clear_error ();
2134 /* use the GROUP command to poll for new mail */
2135 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2137 char buf[LONG_STRING];
2140 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2144 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2147 if (m_strncmp("211", buf, 3)) {
2149 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2153 if (!m_strncmp("211", buf, 3)) {
2157 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2158 nntp_data->firstMessage = first;
2159 nntp_data->lastMessage = last;
2160 if (ctx && last > nntp_data->lastLoaded) {
2161 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2162 time (&nntp_data->nserv->check_time);
2165 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2166 nntp_data->unread = count;
2168 mutt_newsgroup_stat (nntp_data);
2169 /* active was renumbered? */
2170 if (last < nntp_data->lastLoaded) {
2171 if (!nntp_data->max) {
2172 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2175 nntp_data->lastCached = 0;
2177 nntp_data->entries[0].first = 1;
2178 nntp_data->entries[0].last = 0;
2180 nntp_sync_sidebar (nntp_data);
2183 time (&nntp_data->nserv->check_time);
2187 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2189 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2192 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2194 char buf[LONG_STRING];
2195 nntp_data_t nntp_data;
2196 nntp_data_t *l, **emp;
2200 if (!serv || !serv->newgroups_time)
2203 if (nntp_open_connection (serv) < 0)
2206 /* check subscribed groups for new news */
2207 if (option (OPTSHOWNEWNEWS)) {
2208 mutt_message _("Checking for new messages...");
2210 for (l = serv->list; l; l = l->next) {
2211 serv->check_time = 0; /* really check! */
2213 _nntp_check_mailbox (NULL, l);
2220 mutt_message _("Checking for new newsgroups...");
2222 now = serv->newgroups_time;
2223 time (&serv->newgroups_time);
2225 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2226 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2227 t->tm_min, t->tm_sec);
2228 nntp_data.nserv = serv;
2229 if (Context && Context->magic == M_NNTP)
2230 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2232 nntp_data.group = NULL;
2234 emp = nntp_data_list_last(&serv->list);
2235 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2236 add_group, serv, 0) != 0) {
2240 mutt_message _("Loading descriptions...");
2242 for (l = *emp; l; l = l->next) {
2244 nntp_get_desc(l, l->group, NULL, NULL);
2247 nntp_save_cache_index(serv);
2249 return nntp.checked;
2252 /* Load list of all newsgroups from active */
2253 int nntp_get_active (nntp_server_t * serv)
2256 nntp_data_t nntp_data, **tmp = &serv->list;
2258 if (nntp_open_connection (serv) < 0)
2261 snprintf (msg, sizeof (msg),
2262 _("Loading list of all newsgroups on server %s..."),
2263 serv->conn->account.host);
2265 time (&serv->newgroups_time);
2266 nntp_data.nserv = serv;
2267 nntp_data.group = NULL;
2269 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2273 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2275 nntp_get_desc (&nntp_data, "*", msg, NULL);
2278 if ((*tmp)->deleted && !(*tmp)->rc) {
2279 nntp_data_t *d = nntp_data_list_pop(tmp);
2280 nntp_delete_cache(d);
2281 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2282 nntp_data_delete(&d);
2284 tmp = &(*tmp)->next;
2288 nntp_save_cache_index(serv);
2290 mutt_clear_error ();
2291 return nntp.checked;
2295 * returns -1 if error ocurred while retrieving header,
2296 * number of articles which ones exist in context on success.
2298 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2302 /* if msgid is already in context, don't reload them */
2303 if (hash_find (ctx->id_hash, msgid))
2305 if (ctx->msgcount == ctx->hdrmax)
2306 mx_alloc_memory (ctx);
2307 ctx->hdrs[ctx->msgcount] = header_new();
2308 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2310 mutt_message (_("Fetching %s from server..."), msgid);
2311 ret = nntp_read_header (ctx, msgid, 0);
2312 /* since nntp_read_header() may set read flag, we must reset it */
2313 ctx->hdrs[ctx->msgcount]->read = 0;
2315 header_delete(&ctx->hdrs[ctx->msgcount]);
2318 mx_update_context (ctx, 1);
2331 static int check_children (char *s, void *c)
2333 CHILD_CONTEXT *cc = c;
2336 if (!s || (n = atoi (s)) == 0)
2338 for (i = 0; i < cc->ctx->msgcount; i++)
2339 if (cc->ctx->hdrs[i]->article_num == n)
2341 if (cc->num >= cc->max)
2342 p_realloc(&cc->child, cc->max += 25);
2343 cc->child[cc->num++] = n;
2348 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2350 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2352 int i, ret = 0, tmp = 0;
2353 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2355 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2356 !nntp_data->nserv->conn->account.host)
2358 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2360 if (!nntp_data->nserv->hasXPAT) {
2361 mutt_error (_("Server %s does not support this operation!"),
2362 nntp_data->nserv->conn->account.host);
2366 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2367 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2369 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2370 p_delete(&cc.child);
2374 /* dont try to read the xover cache. check_children() already
2375 * made sure that we dont have the article, so we need to visit
2376 * the server. Reading the cache at this point is also bad
2377 * because it would duplicate messages */
2378 if (nntp.use_cache) {
2380 nntp.use_cache = false;
2382 for (i = 0; i < cc.num; i++) {
2383 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2385 if (ctx->msgcount &&
2386 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2387 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2390 nntp.use_cache = true;
2391 p_delete(&cc.child);
2395 static int nntp_is_magic (const char* path, struct stat* st) {
2396 url_scheme_t s = url_check_scheme(NONULL(path));
2397 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2400 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2402 case ACL_INSERT: /* editing messages */
2403 case ACL_WRITE: /* change importance */
2405 case ACL_DELETE: /* (un)deletion */
2406 case ACL_SEEN: /* mark as read */
2413 mx_t const nntp_mx = {
2423 nntp_fastclose_mailbox,