2 * Copyright notice from original mutt:
3 * Copyright (C) 1998 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
5 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 #include <lib-lib/lib-lib.h>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/sidebar.h>
19 #include <lib-mx/mx.h>
28 #define NNTP_SSL_PORT 563
30 static unsigned int _checked = 0;
32 static int nntp_check_newgroups (nntp_server_t *, int);
36 static void mutt_newsgroup_stat(nntp_data_t *data)
39 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
42 data->unread = data->lastMessage - data->firstMessage + 1;
43 for (int i = 0; i < data->num; i++) {
44 int first = MAX(data->entries[i].first, data->firstMessage);
45 int last = MIN(data->entries[i].last, data->lastMessage);
46 data->unread -= MAX(0, last - first + 1);
50 static int nntp_parse_newsrc_line(nntp_server_t * news, const char *line)
53 char group[LONG_STRING];
57 for (p = line; *p; p++) {
61 p = strpbrk(line, ":!");
65 m_strncpy(group, ssizeof(group), line, p - line);
66 data = hash_find(news->newsgroups, group);
68 data = nntp_data_new();
69 data->group = p_dupstr(line, p - line);
72 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
73 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
74 hash_insert(news->newsgroups, data->group, data);
75 nntp_data_list_append(&news->list, data);
77 p_delete(&data->entries);
81 data->entries = p_new(NEWSRC_ENTRY, x * 2);
83 data->subscribed = (*p++ == ':');
85 for (x = 0; *p; p++) {
87 data->entries[x].first = strtol(p, (char **)&p, 10);
90 data->entries[x].last = strtol(p + 1, (char **)&p, 10);
92 data->entries[x].last = data->entries[x].first;
95 p = strchrnul(p, ',');
96 x += data->entries[x].last != 0;
99 if (x && !data->lastMessage)
100 data->lastMessage = data->entries[x - 1].last;
102 mutt_newsgroup_stat(data);
106 static int slurp_newsrc (nntp_server_t * news)
113 news->stat = stat(news->newsrc, &sb);
114 news->size = sb.st_size;
115 news->mtime = sb.st_mtime;
117 if ((fp = safe_fopen(news->newsrc, "r")) == NULL)
120 /* hmm, should we use dotlock? */
121 if (mx_lock_file(news->newsrc, fileno (fp), 0, 0, 1)) {
126 while (getline(&buf, &n, fp) >= 0) {
127 nntp_parse_newsrc_line(news, buf);
131 mx_unlock_file (news->newsrc, fileno (fp), 0);
136 #define nntp_cache_expand(dst, dlen, fmt, ...) \
138 snprintf((dst), (dlen), "%s/" fmt, NewsCacheDir, ##__VA_ARGS__); \
139 mutt_expand_path((dst), (dlen)); \
142 /* Loads $news_cache_dir/.index into memory, loads newsserver data
143 * and newsgroup cache names */
144 static int nntp_parse_cacheindex(nntp_server_t *news)
147 char buf[HUGE_STRING], *cp;
148 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
153 unset_option (OPTNEWSCACHE);
154 if (m_strisempty(NewsCacheDir))
157 m_strcpy(dir, sizeof(dir), NewsCacheDir);
158 mutt_expand_path(dir, sizeof(dir));
160 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
161 snprintf(buf, sizeof(buf), _("Directory %s not exist. Create it?"), dir);
162 if (mutt_yesorno (buf, M_YES) != M_YES
163 || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) {
164 mutt_error _("Cache directory not created!");
171 set_option (OPTNEWSCACHE);
173 p_delete(&news->cache);
174 snprintf(buf, sizeof (buf), "%s/%s.index", dir, news->conn->account.host);
175 if (!(idx = safe_fopen (buf, "a+")))
178 while (fgets (buf, sizeof (buf), idx)) {
179 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
180 if (!m_strncmp(buf, "#: ", 3) &&
181 !m_strcasecmp(buf + 3, news->conn->account.host))
184 while (fgets (buf, sizeof (buf), idx)) {
186 while (*cp && *cp != ' ')
191 if (!m_strcmp(buf, "#:"))
193 sscanf (cp + 1, "%s %d %d", file, &l, &m);
194 if (!m_strcmp(buf, "ALL")) {
195 news->cache = m_strdup(file);
196 news->newgroups_time = m;
198 else if (news->newsgroups) {
199 if ((data = hash_find (news->newsgroups, buf)) == NULL) {
200 data = nntp_data_new();
201 data->group = m_strdup(buf);
204 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
205 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
206 hash_insert (news->newsgroups, data->group, data);
207 nntp_data_list_append(&news->list, data);
209 data->cache = m_strdup(file);
211 if (!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);
226 /* nntp_parse_url: given an NNPT URL, return host, port,
227 * username, password and newsgroup will recognise. */
228 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
237 act->port = NNTP_PORT;
238 act->type = M_ACCT_TYPE_NNTP;
240 c = m_strdup(server);
241 url_parse_ciss (&url, c);
243 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
244 if (url.scheme == U_NNTPS) {
246 act->port = NNTP_SSL_PORT;
251 m_strcpy(group, group_len, url.path);
253 ret = mutt_account_fromurl (act, &url);
260 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
264 url.path = m_strdup(line);
265 mutt_account_tourl (act, &url);
266 url_ciss_tostring (&url, line, len, 0);
270 static int add_group (char *buf, void *serv)
272 nntp_server_t *s = serv;
273 char group[LONG_STRING], mod, desc[HUGE_STRING];
275 nntp_data_t *nntp_data;
278 _checked = n; /* _checked have N, where N = number of groups */
279 if (!buf) /* at EOF must be zerouth */
286 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
289 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
291 nntp_data = nntp_data_new();
292 nntp_data->group = m_strdup(group);
293 nntp_data->nserv = s;
294 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
295 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
296 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
297 nntp_data_list_append(&s->list, nntp_data);
299 nntp_data->deleted = 0;
300 nntp_data->firstMessage = first;
301 nntp_data->lastMessage = last;
303 nntp_data->allowed = 1;
305 nntp_data->allowed = 0;
307 p_delete(&nntp_data->desc);
309 nntp_data->desc = m_strdup(desc);
310 if (nntp_data->rc || nntp_data->lastCached)
311 mutt_newsgroup_stat (nntp_data);
312 else if (nntp_data->lastMessage &&
313 nntp_data->firstMessage <= nntp_data->lastMessage)
314 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
316 nntp_data->unread = 0;
321 /* Load list of all newsgroups from cache ALL */
322 static int nntp_get_cache_all (nntp_server_t * serv)
324 char buf[HUGE_STRING];
327 nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
328 if ((f = safe_fopen (buf, "r"))) {
331 while (fgets (buf, sizeof (buf), f) != NULL) {
332 if (ReadInc && (i % ReadInc == 0))
333 mutt_message (_("Loading list from cache... %d"), i);
334 add_group (buf, serv);
337 add_group (NULL, NULL);
343 p_delete(&serv->cache);
349 * Automatically loads a newsrc into memory, if necessary.
350 * Checks the size/mtime of a newsrc file, if it doesn't match, load
351 * again. Hmm, if a system has broken mtimes, this might mean the file
352 * is reloaded every time, which we'd have to fix.
354 * a newsrc file is a line per newsgroup, with the newsgroup, then a
355 * ':' denoting subscribed or '!' denoting unsubscribed, then a
356 * comma separated list of article numbers and ranges.
358 nntp_server_t *mutt_select_newsserver (char *server)
360 char file[_POSIX_PATH_MAX];
369 if (!server || !*server) {
370 mutt_error _("No newsserver defined!");
375 buf = p = p_new(char, m_strlen(server) + 10);
376 if (url_check_scheme (server) == U_UNKNOWN) {
377 strcpy (buf, "nntp://");
378 p = strchr (buf, '\0');
382 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
384 mutt_error (_("%s is an invalid newsserver specification!"), server);
389 conn = mutt_conn_find (NULL, &act);
393 nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
394 serv = (nntp_server_t *) conn->data;
398 /* externally modified? */
399 if (serv->stat != stat(file, &sb) || (!serv->stat &&
400 (serv->size != sb.st_size
401 || serv->mtime != sb.st_mtime)))
403 for (list = serv->list; list; list = list->next) {
404 list->subscribed = list->rc = list->num = 0;
407 nntp_clear_cacheindex (serv);
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);
419 serv->newsrc = m_strdup(file);
420 serv->newsgroups = hash_new(1009, false);
421 slurp_newsrc (serv); /* load .newsrc */
422 nntp_parse_cacheindex (serv); /* load .index */
423 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
424 nntp_check_newgroups (serv, 1);
425 else if (nntp_get_active (serv) < 0) {
426 hash_delete(&serv->newsgroups, NULL);
427 nntp_data_list_wipe(&serv->list);
428 p_delete(&serv->newsrc);
429 p_delete(&serv->cache);
433 nntp_clear_cacheindex (serv);
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 (!option (OPTNEWSCACHE) || !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 /* Remove cache files of unsubscribed newsgroups */
769 void nntp_clear_cacheindex (nntp_server_t * news)
773 if (option (OPTSAVEUNSUB) || !news)
776 for (data = news->list; data; data = data->next) {
777 if (!data || data->subscribed || !data->cache)
779 nntp_delete_cache (data);
784 static int nntp_save_cache_index (nntp_server_t * news)
786 char buf[HUGE_STRING];
787 char file[_POSIX_PATH_MAX];
791 if (!news || !news->newsgroups)
793 if (!option (OPTNEWSCACHE))
797 nntp_cache_expand(file, ssizeof(file), "%s", news->cache);
799 f = safe_fopen(file, "w");
801 m_strcpy(buf, ssizeof(buf), news->conn->account.host);
802 f = mutt_mkname(buf);
803 news->cache = m_strdup(buf);
804 nntp_cache_expand(file, ssizeof(file), "%s", buf);
809 for (d = news->list; d; d = d->next) {
812 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
813 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
816 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
817 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
818 if (fputs (buf, f) == EOF) {
827 if (nntp_update_cacheindex (news, NULL)) {
834 static int nntp_save_cache_group (CONTEXT * ctx)
836 char buf[HUGE_STRING], addr[STRING];
837 char file[_POSIX_PATH_MAX];
841 int i = 0, save = SORT_ORDER;
844 if (!option (OPTNEWSCACHE))
846 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
849 if (((nntp_data_t *) ctx->data)->cache) {
850 nntp_cache_expand(file, ssizeof(file), "%s",
851 ((nntp_data_t *)ctx->data)->cache);
853 f = safe_fopen (file, "w");
856 snprintf(buf, ssizeof(buf), "%s-%s",
857 ((nntp_data_t *)ctx->data)->nserv->conn->account.host,
858 ((nntp_data_t *)ctx->data)->group);
859 f = mutt_mkname (buf);
860 ((nntp_data_t *)ctx->data)->cache = m_strdup(buf);
861 nntp_cache_expand(file, ssizeof(file), "%s", buf);
866 if (Sort != SORT_ORDER) {
869 mutt_sort_headers (ctx, 0);
872 /* Save only $nntp_context messages... */
873 ((nntp_data_t *) ctx->data)->lastCached = 0;
874 if (NntpContext && ctx->msgcount > NntpContext)
875 i = ctx->msgcount - NntpContext;
876 for (; i < ctx->msgcount; i++) {
877 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
880 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
881 tm = gmtime (&h->date_sent);
882 snprintf (buf, sizeof (buf),
883 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
884 h->article_num, h->env->subject, addr, tm->tm_mday,
885 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
886 tm->tm_min, tm->tm_sec, h->env->message_id);
888 if (h->env->references)
889 mutt_write_references (h->env->references, f);
890 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
891 h->content->length, h->lines, NONULL (h->env->xref));
892 if (fputs (buf, f) == EOF) {
898 prev = ctx->hdrs[i]->article_num;
903 mutt_sort_headers (ctx, 0);
907 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
908 (nntp_data_t *) ctx->data)) {
912 ((nntp_data_t *) ctx->data)->lastCached =
913 ((nntp_data_t *) ctx->data)->lastLoaded;
917 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
921 if (!news || !news->newsgroups || !group || !*group)
923 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
924 data = nntp_data_new();
925 data->group = m_strdup(group);
928 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
929 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
930 hash_insert (news->newsgroups, data->group, data);
931 nntp_data_list_append(&news->list, data);
933 if (!data->subscribed) {
934 data->subscribed = 1;
940 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
944 if (!news || !news->newsgroups || !group || !*group ||
945 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
947 if (data->subscribed) {
948 data->subscribed = 0;
949 if (!option (OPTSAVEUNSUB))
955 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
959 if (!news || !news->newsgroups || !group || !*group ||
960 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
963 data->entries = p_new(NEWSRC_ENTRY, 5);
967 data->entries[0].first = 1;
969 data->entries[0].last = data->lastMessage;
970 if (Context && Context->data == data) {
973 for (x = 0; x < Context->msgcount; x++)
974 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
979 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
983 if (!news || !news->newsgroups || !group || !*group ||
984 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
987 data->entries = p_new(NEWSRC_ENTRY, 5);
991 data->entries[0].first = 1;
992 data->entries[0].last = data->firstMessage - 1;
993 if (Context && Context->data == data) {
996 data->unread = Context->msgcount;
997 for (x = 0; x < Context->msgcount; x++)
998 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
1001 data->unread = data->lastMessage - data->entries[0].last;
1005 /* this routine gives the first newsgroup with new messages */
1006 void nntp_buffy (char* dst, ssize_t dstlen) {
1010 /* forward to current group */
1011 for (list = CurrentNewsSrv->list; list; list = list->next) {
1012 if (list->subscribed && list->unread
1013 && Context && Context->magic == M_NNTP
1014 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
1026 list = CurrentNewsSrv->list;
1028 for (; list; list = list->next) {
1029 if (list->subscribed && list->unread) {
1030 if (Context && Context->magic == M_NNTP &&
1031 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1034 for (i = 0; i < Context->msgcount; i++)
1035 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1040 m_strcpy(dst, dstlen, list->group);
1053 /* nntp protocol {{{ */
1055 void nntp_sync_sidebar (nntp_data_t* data) {
1063 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1064 data->nserv->conn->account.has_ssl ? "s" : "",
1065 NONULL(data->nserv->conn->account.user),
1066 *data->nserv->conn->account.pass ? ":" : "",
1067 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1068 data->nserv->conn->account.host,
1071 /* bail out if group not found via mailboxes */
1072 if ((i = buffy_lookup (buf)) < 0)
1075 tmp = Incoming.arr[i];
1076 /* copied from browser.c */
1077 if (option (OPTMARKOLD) &&
1078 data->lastCached >= data->firstMessage &&
1079 data->lastCached <= data->lastMessage)
1080 tmp->msg_unread = data->lastMessage - data->lastCached;
1082 tmp->msg_unread = data->unread;
1083 tmp->new = data->unread > 0;
1084 /* this is closest to a "total" count we can get */
1085 tmp->msgcount = data->lastMessage - data->firstMessage;
1088 static int nntp_auth (nntp_server_t * serv)
1090 CONNECTION *conn = serv->conn;
1092 unsigned char flags = conn->account.flags;
1094 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1095 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1096 conn->account.flags = flags;
1100 mutt_message _("Logging in...");
1102 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1103 mutt_socket_write (conn, buf);
1104 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1105 conn->account.flags = flags;
1109 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1110 mutt_socket_write(conn, buf);
1111 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1112 conn->account.flags = flags;
1116 if (m_strncmp("281", buf, 3)) {
1117 conn->account.flags = flags;
1118 mutt_error _("Login failed.");
1127 static int nntp_connect_error (nntp_server_t * serv)
1129 serv->status = NNTP_NONE;
1130 mutt_socket_close (serv->conn);
1131 mutt_error _("Server closed connection!");
1137 static int nntp_connect_and_auth (nntp_server_t * serv)
1139 CONNECTION *conn = serv->conn;
1143 serv->status = NNTP_NONE;
1145 if (mutt_socket_open (conn) < 0)
1148 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1149 return nntp_connect_error (serv);
1151 if (!m_strncmp("200", buf, 3))
1152 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1153 else if (!m_strncmp("201", buf, 3))
1154 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1156 mutt_socket_close(conn);
1158 mutt_error("%s", buf);
1163 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1164 returned codes and messages. */
1165 mutt_socket_write (conn, "MODE READER\r\n");
1166 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1167 return nntp_connect_error (serv);
1169 mutt_socket_write (conn, "STAT\r\n");
1170 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1171 return nntp_connect_error (serv);
1173 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1174 serv->status = NNTP_OK;
1178 rc = nntp_auth (serv);
1180 return nntp_connect_error (serv);
1182 mutt_socket_close (conn);
1183 serv->status = NNTP_BYE;
1187 mutt_socket_close (conn);
1188 mutt_error _("Login failed.");
1193 serv->status = NNTP_OK;
1197 static int nntp_attempt_features (nntp_server_t * serv)
1199 char buf[LONG_STRING];
1200 CONNECTION *conn = serv->conn;
1202 if (serv->feat_known)
1205 mutt_socket_write (conn, "XOVER\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))
1211 mutt_socket_write (conn, "XPAT\r\n");
1212 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1213 return nntp_connect_error (serv);
1214 if (m_strncmp("500", buf, 3))
1217 mutt_socket_write (conn, "LISTGROUP\r\n");
1218 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1219 return (nntp_connect_error (serv));
1220 if (m_strncmp("500", buf, 3))
1221 serv->hasLISTGROUP = 1;
1223 mutt_socket_write (conn, "XGTITLE +\r\n");
1224 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1225 return nntp_connect_error (serv);
1226 if (m_strncmp("500", buf, 3))
1227 serv->hasXGTITLE = 1;
1229 if (!m_strncmp("282", buf, 3)) {
1231 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1232 return nntp_connect_error (serv);
1233 } while (!(buf[0] == '.' && buf[1] == '\0'));
1236 serv->feat_known = 1;
1240 static int nntp_open_connection (nntp_server_t * serv)
1242 if (serv->status == NNTP_OK)
1244 if (serv->status == NNTP_BYE)
1246 if (nntp_connect_and_auth (serv) < 0)
1248 if (nntp_attempt_features (serv) < 0)
1253 static int nntp_reconnect (nntp_server_t * serv)
1257 mutt_socket_close (serv->conn);
1260 if (nntp_connect_and_auth (serv) == 0)
1263 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1264 serv->conn->account.host);
1265 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1266 serv->status = NNTP_BYE;
1272 /* Send data from line[LONG_STRING] and receive answer to same line */
1273 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1275 char buf[LONG_STRING];
1278 if (data->nserv->status == NNTP_BYE)
1283 mutt_socket_write (data->nserv->conn, line);
1285 else if (data->group) {
1286 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1287 mutt_socket_write (data->nserv->conn, buf);
1291 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1292 if (nntp_reconnect (data->nserv) < 0)
1296 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1297 mutt_socket_write (data->nserv->conn, buf);
1298 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1304 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1308 m_strcpy(line, linelen, buf);
1313 * This function calls funct(*line, *data) for each received line,
1314 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1317 * 1 - correct but not performed (may be, have to be continued),
1318 * -1 - conection lost,
1319 * -2 - invalid command or execution error,
1320 * -3 - error in funct(*line, *data).
1322 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1323 const char *msg, progress_t* bar,
1324 int (*funct) (char *, void *),
1325 void *data, int tagged)
1327 char buf[LONG_STRING];
1336 m_strcpy(buf, sizeof(buf), query);
1337 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1346 inbuf = p_new(char, sizeof(buf));
1349 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1354 if (!lenbuf && buf[0] == '.') {
1355 if (buf[1] == '\0') {
1363 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1366 if (chunk >= ssizeof (buf)) {
1367 lenbuf += m_strlen(p);
1371 mutt_progress_bar (bar, pos);
1374 if (ReadInc && (line % ReadInc == 0)) {
1376 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1378 mutt_message ("%s %d", msg, line);
1382 if (ret == 0 && funct (inbuf, data) < 0)
1387 p_realloc(&inbuf, lenbuf + sizeof (buf));
1396 static int nntp_read_tempfile (char *line, void *file)
1398 FILE *f = (FILE *) file;
1404 if (fputc ('\n', f) == EOF)
1410 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1413 register char *p, *b;
1414 register char *colon = NULL;
1418 /* skip to next word */
1420 while (*b && ((*b == ' ') || (*b == '\t')))
1424 /* skip to end of word */
1425 while (*p && (*p != ' ') && (*p != '\t')) {
1437 nntp_get_status (ctx, h, b, atoi (colon));
1438 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1439 h->article_num = atoi (colon);
1447 * 1 if article not found
1448 * -1 if read or write error on tempfile or socket
1450 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1453 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1455 char buf[LONG_STRING];
1456 char tempfile[_POSIX_PATH_MAX];
1458 HEADER *h = ctx->hdrs[ctx->msgcount];
1460 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1465 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1467 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1469 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1473 return (ret == -1 ? -1 : 1);
1476 h->article_num = article_num;
1477 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1481 if (h->env->xref != NULL)
1482 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1483 else if (h->article_num == 0 && msgid) {
1484 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1485 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1486 h->article_num = atoi (buf + 4);
1492 static int parse_description (char *line, void *n)
1494 register char *d = line;
1496 nntp_server_t *news = n;
1500 while (*d && *d != '\t' && *d != ' ')
1504 while (*d && (*d == '\t' || *d == ' '))
1506 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1507 m_strcmp(d, data->desc)) {
1508 p_delete(&data->desc);
1509 data->desc = m_strdup(d);
1514 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1518 if (!option (OPTLOADDESC) || !data || !data->nserv)
1521 /* Get newsgroup description, if we can */
1522 if (data->nserv->hasXGTITLE)
1523 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1525 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1526 mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0);
1530 * XOVER returns a tab separated list of:
1531 * id|subject|from|date|Msgid|references|bytes|lines|xref
1533 * This has to duplicate some of the functionality of
1534 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1535 * a limited number of headers which are "parsed" by placement in the list)
1537 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1539 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1543 hdr->env = envelope_new();
1544 hdr->env->newsgroups = m_strdup(nntp_data->group);
1545 hdr->content = body_new();
1546 hdr->content->type = TYPETEXT;
1547 hdr->content->subtype = m_strdup("plain");
1548 hdr->content->encoding = ENC7BIT;
1549 hdr->content->disposition = DISPINLINE;
1550 hdr->content->length = -1;
1553 for (x = 0; !done && x < 9; x++) {
1554 /* if from file, need to skip newline character */
1555 while (*p && *p != '\n' && *p != '\t')
1563 hdr->article_num = atoi (b);
1564 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1567 hdr->env->subject = m_strdup(b);
1570 address_list_wipe(&hdr->env->from);
1571 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1572 /* same as for mutt_parse_rfc822_line():
1573 * don't leave from info NULL if there's an invalid address (or
1574 * whatever) in From: field; mutt would just display it as empty
1575 * and mark mail/(esp.) news article as your own. aaargh! this
1576 * bothered me for _years_ */
1577 if (!hdr->env->from) {
1578 hdr->env->from = address_new ();
1579 hdr->env->from->personal = m_strdup(b);
1583 hdr->date_sent = mutt_parse_date (b, hdr);
1584 hdr->received = hdr->date_sent;
1587 p_delete(&hdr->env->message_id);
1588 hdr->env->message_id = m_strdup(b);
1591 string_list_wipe(&hdr->env->references);
1592 hdr->env->references = mutt_parse_references (b, 0);
1595 hdr->content->length = atoi (b);
1598 hdr->lines = atoi (b);
1602 p_delete(&hdr->env->xref);
1603 b = b + 6; /* skips the "Xref: " */
1604 hdr->env->xref = m_strdup(b);
1605 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1607 rfc2047_decode_envelope(hdr->env);
1623 static int nntp_fetch_numbers (char *line, void *c)
1625 FETCH_CONTEXT *fc = c;
1628 int num = atoi(line);
1629 if (num < fc->first || num > fc->last)
1632 bit_set(&fc->messages, num);
1637 static int add_xover_line (char *line, void *c)
1640 FETCH_CONTEXT *fc = c;
1641 CONTEXT *ctx = fc->ctx;
1642 nntp_data_t *data = (nntp_data_t *) ctx->data;
1647 if (ctx->msgcount >= ctx->hdrmax)
1648 mx_alloc_memory (ctx);
1649 ctx->hdrs[ctx->msgcount] = header_new();
1650 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1652 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1653 num = ctx->hdrs[ctx->msgcount]->article_num;
1655 if (num >= fc->first && num <= fc->last) {
1657 if (num > data->lastLoaded)
1658 data->lastLoaded = num;
1659 num = num - fc->first + 1;
1660 total = fc->last - fc->first + 1;
1661 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1662 mutt_message ("%s %d/%d", fc->msg, num, total);
1664 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1671 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1673 char buf[HUGE_STRING];
1674 const char *msg = _("Fetching message headers...");
1675 const char *msg2 = _("Fetching headers from cache...");
1676 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1677 int ret, num, oldmsgcount, current;
1681 /* if empty group or nothing to do */
1682 if (!last || first > last)
1685 /* fetch list of articles */
1686 mutt_message _("Fetching list of articles...");
1692 /* CACHE: must be loaded xover cache here */
1693 num = nntp_data->lastCached - first + 1;
1694 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
1695 nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
1696 mutt_message (msg2);
1698 if ((f = safe_fopen (buf, "r"))) {
1701 /* counting number of lines */
1702 while (fgets (buf, sizeof (buf), f) != NULL)
1705 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1707 oldmsgcount = ctx->msgcount;
1709 fc.last = first + num - 1;
1711 while (fgets (buf, sizeof (buf), f) != NULL) {
1712 if (ReadInc && ((++c) % ReadInc == 0))
1713 mutt_message ("%s %d/%d", msg2, c, r);
1714 add_xover_line (buf, &fc);
1717 nntp_data->lastLoaded = fc.last;
1718 first = fc.last + 1;
1719 if (ctx->msgcount > oldmsgcount)
1720 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1723 nntp_delete_cache (nntp_data);
1725 num = last - first + 1;
1731 * Without XOVER, we have to fetch each article header and parse
1732 * it. With XOVER, we ask for all of them
1735 if (nntp_data->nserv->hasXOVER) {
1736 oldmsgcount = ctx->msgcount;
1740 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1741 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1742 if (ctx->msgcount > oldmsgcount)
1743 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1745 mutt_error (_("XOVER command failed: %s"), buf);
1749 bits_init(&fc.messages);
1751 if (nntp_data->nserv->hasLISTGROUP) {
1752 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1753 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1754 nntp_fetch_numbers, &fc, 0))
1756 mutt_error (_("LISTGROUP command failed: %s"), buf);
1758 bits_wipe(&fc.messages);
1762 for (num = first; num <= last; num++)
1763 bit_set(&fc.messages, num);
1766 for (current = first; current <= last; current++) {
1769 ret = current - first + 1;
1770 mutt_message ("%s %d/%d", msg, ret, num);
1772 if (!bit_isset(&fc.messages, current))
1775 if (ctx->msgcount >= ctx->hdrmax)
1776 mx_alloc_memory (ctx);
1777 h = ctx->hdrs[ctx->msgcount] = header_new();
1778 h->index = ctx->msgcount;
1780 ret = nntp_read_header (ctx, NULL, current);
1781 if (ret == 0) { /* Got article. Fetch next header */
1782 nntp_get_status (ctx, h, NULL, h->article_num);
1784 mx_update_context (ctx, 1);
1787 header_delete(&h); /* skip it */
1789 bits_wipe(&fc.messages);
1793 if (current > nntp_data->lastLoaded)
1794 nntp_data->lastLoaded = current;
1796 bits_wipe(&fc.messages);
1799 nntp_data->lastLoaded = last;
1800 mutt_clear_error ();
1805 * currently, nntp "mailbox" is "newsgroup"
1807 static int nntp_open_mailbox (CONTEXT * ctx)
1809 nntp_data_t *nntp_data;
1810 nntp_server_t *serv;
1811 char buf[HUGE_STRING];
1812 char server[LONG_STRING];
1813 int count = 0, first;
1818 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1819 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1825 nntp_expand_path (server, sizeof (server), &act);
1826 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1829 CurrentNewsSrv = serv;
1831 /* create NNTP-specific state struct if nof found in list */
1832 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1833 nntp_data = nntp_data_new();
1834 nntp_data->group = m_strdup(buf);
1835 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1836 nntp_data_list_append(&serv->list, nntp_data);
1838 ctx->data = nntp_data;
1839 nntp_data->nserv = serv;
1841 mutt_message (_("Selecting %s..."), nntp_data->group);
1843 if (!nntp_data->desc) {
1844 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1845 if (nntp_data->desc)
1846 nntp_save_cache_index (serv);
1850 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1854 if (m_strncmp("211", buf, 3)) {
1857 /* GROUP command failed */
1858 if (!m_strncmp("411", buf, 3)) {
1859 mutt_error (_("Newsgroup %s not found on server %s"),
1860 nntp_data->group, serv->conn->account.host);
1862 /* CACHE: delete cache and line from .index */
1863 nntp_delete_cache (nntp_data);
1864 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1865 for (l = &serv->list; *l; l = &(*l)->next) {
1866 if ((*l) == nntp_data) {
1867 nntp_data_list_pop(l);
1868 nntp_data_delete(&nntp_data);
1872 nntp_data_delete(&nntp_data);
1879 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1880 &nntp_data->lastMessage, buf);
1882 nntp_data->deleted = 0;
1884 time (&serv->check_time);
1887 * Check for max adding context. If it is greater than $nntp_context,
1888 * strip off extra articles
1890 first = nntp_data->firstMessage;
1891 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1892 first = nntp_data->lastMessage - NntpContext + 1;
1894 nntp_data->lastLoaded = first - 1;
1895 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1898 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1900 char buf[LONG_STRING];
1901 char path[_POSIX_PATH_MAX];
1906 /* see if we already have the message in our cache */
1908 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1911 /* if everything is fine, assign msg->fp and return */
1912 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1913 (msg->fp = fopen (cache->path, "r")))
1916 /* clear the previous entry */
1917 unlink (cache->path);
1918 p_delete(&cache->path);
1920 cache->index = ctx->hdrs[msgno]->index;
1921 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1925 cache->path = m_strdup(path);
1927 if (ctx->hdrs[msgno]->article_num == 0)
1928 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1929 ctx->hdrs[msgno]->env->message_id);
1931 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1932 ctx->hdrs[msgno]->article_num);
1934 bar.msg = _("Fetching message...");
1936 mutt_progress_bar (&bar, 0);
1938 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1939 msg->fp, ctx->tagged);
1941 mutt_error (_("Article %d not found on server"),
1942 ctx->hdrs[msgno]->article_num);
1948 p_delete(&cache->path);
1952 envelope_delete(&ctx->hdrs[msgno]->env);
1953 ctx->hdrs[msgno]->env =
1954 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1955 /* fix content length */
1956 fseeko (msg->fp, 0, SEEK_END);
1957 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1958 ctx->hdrs[msgno]->content->offset;
1960 /* this is called in mutt before the open which fetches the message,
1961 * which is probably wrong, but we just call it again here to handle
1962 * the problem instead of fixing it.
1964 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1966 /* These would normally be updated in mx_update_context(), but the
1967 * full headers aren't parsed with XOVER, so the information wasn't
1970 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1972 mutt_clear_error ();
1979 int nntp_post (const char *msg)
1981 char buf[LONG_STRING];
1984 nntp_data_t *nntp_data;
1986 if (Context && Context->magic == M_NNTP)
1987 nntp_data = (nntp_data_t *)Context->data;
1989 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
1990 !CurrentNewsSrv->list)
1992 mutt_error (_("Can't post article. No connection to news server."));
1995 nntp_data = CurrentNewsSrv->list;
1998 if (!(f = safe_fopen (msg, "r"))) {
1999 mutt_error (_("Can't post article. Unable to open %s"), msg);
2003 m_strcpy(buf, sizeof(buf), "POST\r\n");
2004 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2005 mutt_error (_("Can't post article. Connection to %s lost."),
2006 nntp_data->nserv->conn->account.host);
2009 if (buf[0] != '3') {
2010 mutt_error (_("Can't post article: %s"), buf);
2016 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
2017 len = m_strlen(buf);
2018 if (buf[len - 1] == '\n') {
2019 buf[len - 1] = '\r';
2025 mutt_socket_write(nntp_data->nserv->conn, buf);
2027 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2031 if (buf[m_strlen(buf) - 1] != '\n')
2032 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2033 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2034 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2035 mutt_error (_("Can't post article. Connection to %s lost."),
2036 nntp_data->nserv->conn->account.host);
2039 if (buf[0] != '2') {
2040 mutt_error (_("Can't post article: %s"), buf);
2047 /* nntp_logout_all: close all open connections. */
2048 void nntp_logout_all (void)
2050 char buf[LONG_STRING];
2053 conn = mutt_socket_head ();
2056 CONNECTION* next = conn->next;
2057 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2058 mutt_message (_("Closing connection to %s..."), conn->account.host);
2059 mutt_socket_write (conn, "QUIT\r\n");
2060 mutt_socket_readln (buf, sizeof (buf), conn);
2061 mutt_clear_error ();
2062 mutt_socket_close (conn);
2063 mutt_socket_free (conn);
2069 static void nntp_free_acache (nntp_data_t * data)
2073 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2074 if (data->acache[i].path) {
2075 unlink (data->acache[i].path);
2076 p_delete(&data->acache[i].path);
2081 void nntp_data_wipe(nntp_data_t *data)
2083 p_delete(&data->entries);
2084 p_delete(&data->desc);
2085 p_delete(&data->cache);
2086 p_delete(&data->group);
2087 nntp_free_acache(data);
2090 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2092 nntp_data_t *data = ctx->data;
2094 /* CACHE: update cache and .index files */
2095 if ((option (OPTSAVEUNSUB) || data->subscribed))
2096 nntp_save_cache_group (ctx);
2097 nntp_free_acache (data);
2099 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2103 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2105 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2109 nntp_free_acache (data);
2110 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2112 nntp_save_cache_index (data->nserv);
2113 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2115 nntp_data_delete(&data);
2117 nntp_sync_sidebar (data);
2120 /* commit changes and terminate connection */
2121 int nntp_close_mailbox (CONTEXT * ctx)
2125 mutt_message _("Quitting newsgroup...");
2128 nntp_data_t *data = (nntp_data_t *) ctx->data;
2131 if (data->nserv && data->nserv->conn && ctx->unread) {
2132 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2134 mutt_newsgroup_catchup (data->nserv, data->group);
2139 nntp_sync_mailbox (ctx, 0, NULL);
2140 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2141 nntp_server_t *news;
2143 news = ((nntp_data_t *) ctx->data)->nserv;
2144 newsrc_gen_entries (ctx);
2145 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2146 mutt_newsrc_update (news);
2148 mutt_clear_error ();
2152 /* use the GROUP command to poll for new mail */
2153 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2155 char buf[LONG_STRING];
2158 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2162 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2165 if (m_strncmp("211", buf, 3)) {
2167 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2171 if (!m_strncmp("211", buf, 3)) {
2175 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2176 nntp_data->firstMessage = first;
2177 nntp_data->lastMessage = last;
2178 if (ctx && last > nntp_data->lastLoaded) {
2179 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2180 time (&nntp_data->nserv->check_time);
2183 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2184 nntp_data->unread = count;
2186 mutt_newsgroup_stat (nntp_data);
2187 /* active was renumbered? */
2188 if (last < nntp_data->lastLoaded) {
2189 if (!nntp_data->max) {
2190 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2193 nntp_data->lastCached = 0;
2195 nntp_data->entries[0].first = 1;
2196 nntp_data->entries[0].last = 0;
2198 nntp_sync_sidebar (nntp_data);
2201 time (&nntp_data->nserv->check_time);
2205 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2207 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2210 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2212 char buf[LONG_STRING];
2213 nntp_data_t nntp_data;
2214 nntp_data_t *l, **emp;
2218 if (!serv || !serv->newgroups_time)
2221 if (nntp_open_connection (serv) < 0)
2224 /* check subscribed groups for new news */
2225 if (option (OPTSHOWNEWNEWS)) {
2226 mutt_message _("Checking for new messages...");
2228 for (l = serv->list; l; l = l->next) {
2229 serv->check_time = 0; /* really check! */
2231 _nntp_check_mailbox (NULL, l);
2238 mutt_message _("Checking for new newsgroups...");
2240 now = serv->newgroups_time;
2241 time (&serv->newgroups_time);
2243 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2244 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2245 t->tm_min, t->tm_sec);
2246 nntp_data.nserv = serv;
2247 if (Context && Context->magic == M_NNTP)
2248 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2250 nntp_data.group = NULL;
2252 emp = nntp_data_list_last(&serv->list);
2253 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2254 add_group, serv, 0) != 0) {
2258 mutt_message _("Loading descriptions...");
2260 for (l = *emp; l; l = l->next) {
2262 nntp_get_desc(l, l->group, NULL, NULL);
2265 nntp_save_cache_index(serv);
2270 /* Load list of all newsgroups from active */
2271 int nntp_get_active (nntp_server_t * serv)
2274 nntp_data_t nntp_data, **tmp = &serv->list;
2276 if (nntp_open_connection (serv) < 0)
2279 snprintf (msg, sizeof (msg),
2280 _("Loading list of all newsgroups on server %s..."),
2281 serv->conn->account.host);
2283 time (&serv->newgroups_time);
2284 nntp_data.nserv = serv;
2285 nntp_data.group = NULL;
2287 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2291 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2293 nntp_get_desc (&nntp_data, "*", msg, NULL);
2296 if ((*tmp)->deleted && !(*tmp)->rc) {
2297 nntp_data_t *d = nntp_data_list_pop(tmp);
2298 nntp_delete_cache(d);
2299 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2300 nntp_data_delete(&d);
2302 tmp = &(*tmp)->next;
2305 nntp_save_cache_index (serv);
2307 mutt_clear_error ();
2312 * returns -1 if error ocurred while retrieving header,
2313 * number of articles which ones exist in context on success.
2315 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2319 /* if msgid is already in context, don't reload them */
2320 if (hash_find (ctx->id_hash, msgid))
2322 if (ctx->msgcount == ctx->hdrmax)
2323 mx_alloc_memory (ctx);
2324 ctx->hdrs[ctx->msgcount] = header_new();
2325 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2327 mutt_message (_("Fetching %s from server..."), msgid);
2328 ret = nntp_read_header (ctx, msgid, 0);
2329 /* since nntp_read_header() may set read flag, we must reset it */
2330 ctx->hdrs[ctx->msgcount]->read = 0;
2332 header_delete(&ctx->hdrs[ctx->msgcount]);
2335 mx_update_context (ctx, 1);
2348 static int check_children (char *s, void *c)
2350 CHILD_CONTEXT *cc = c;
2353 if (!s || (n = atoi (s)) == 0)
2355 for (i = 0; i < cc->ctx->msgcount; i++)
2356 if (cc->ctx->hdrs[i]->article_num == n)
2358 if (cc->num >= cc->max)
2359 p_realloc(&cc->child, cc->max += 25);
2360 cc->child[cc->num++] = n;
2365 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2367 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2369 int i, ret = 0, tmp = 0;
2370 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2372 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2373 !nntp_data->nserv->conn->account.host)
2375 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2377 if (!nntp_data->nserv->hasXPAT) {
2378 mutt_error (_("Server %s does not support this operation!"),
2379 nntp_data->nserv->conn->account.host);
2383 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2384 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2386 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2387 p_delete(&cc.child);
2391 /* dont try to read the xover cache. check_children() already
2392 * made sure that we dont have the article, so we need to visit
2393 * the server. Reading the cache at this point is also bad
2394 * because it would duplicate messages */
2395 if (option (OPTNEWSCACHE)) {
2397 unset_option (OPTNEWSCACHE);
2399 for (i = 0; i < cc.num; i++) {
2400 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2402 if (ctx->msgcount &&
2403 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2404 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2407 set_option (OPTNEWSCACHE);
2408 p_delete(&cc.child);
2412 static int nntp_is_magic (const char* path, struct stat* st) {
2413 url_scheme_t s = url_check_scheme(NONULL(path));
2414 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2417 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2419 case ACL_INSERT: /* editing messages */
2420 case ACL_WRITE: /* change importance */
2422 case ACL_DELETE: /* (un)deletion */
2423 case ACL_SEEN: /* mark as read */
2430 mx_t const nntp_mx = {
2440 nntp_fastclose_mailbox,