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)
112 news->stat = stat (news->newsrc, &sb);
113 news->size = sb.st_size;
114 news->mtime = sb.st_mtime;
116 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
118 /* hmm, should we use dotlock? */
119 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1)) {
124 buf = p_new(char, sb.st_size + 1);
125 while (fgets(buf, sb.st_size + 1, fp))
126 nntp_parse_newsrc_line(news, buf);
129 mx_unlock_file (news->newsrc, fileno (fp), 0);
134 static void nntp_cache_expand (char *dst, const char *src)
136 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
137 mutt_expand_path (dst, _POSIX_PATH_MAX);
140 /* Loads $news_cache_dir/.index into memory, loads newsserver data
141 * and newsgroup cache names */
142 static int nntp_parse_cacheindex (nntp_server_t * news)
145 char buf[HUGE_STRING], *cp;
146 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
151 /* check is server name defined or not */
152 if (!news || !news->conn || !news->conn->account.host)
154 unset_option (OPTNEWSCACHE);
155 if (m_strisempty(NewsCacheDir))
158 m_strcpy(dir, sizeof(dir), NewsCacheDir);
159 mutt_expand_path (dir, sizeof (dir));
161 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
162 snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"),
164 if (mutt_yesorno (buf, M_YES) != M_YES
165 || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) {
166 mutt_error _("Cache directory not created!");
173 set_option (OPTNEWSCACHE);
175 p_delete(&news->cache);
176 snprintf (buf, sizeof (buf), "%s/.index", dir);
177 if (!(idx = safe_fopen (buf, "a+")))
180 while (fgets (buf, sizeof (buf), idx)) {
181 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
182 if (!m_strncmp(buf, "#: ", 3) &&
183 !m_strcasecmp(buf + 3, news->conn->account.host))
186 while (fgets (buf, sizeof (buf), idx)) {
188 while (*cp && *cp != ' ')
193 if (!m_strcmp(buf, "#:"))
195 sscanf (cp + 1, "%s %d %d", file, &l, &m);
196 if (!m_strcmp(buf, "ALL")) {
197 news->cache = m_strdup(file);
198 news->newgroups_time = m;
200 else if (news->newsgroups) {
201 if ((data = hash_find (news->newsgroups, buf)) == NULL) {
202 data = nntp_data_new();
203 data->group = m_strdup(buf);
206 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
207 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
208 hash_insert (news->newsgroups, data->group, data);
209 nntp_data_list_append(&news->list, data);
211 data->cache = m_strdup(file);
213 if (!data->firstMessage || data->lastMessage < m)
215 if (!data->firstMessage)
216 data->firstMessage = l;
217 if (data->lastMessage < m)
218 data->lastMessage = m;
219 data->lastCached = m;
220 if (t || !data->unread)
221 mutt_newsgroup_stat (data);
228 const char *nntp_format_str(char *dest, ssize_t destlen, char op,
229 const char *src, const char *fmt,
230 const char *ifstr __attribute__((unused)),
231 const char *elstr __attribute__((unused)),
232 anytype data __attribute__((unused)),
233 format_flag flags __attribute__((unused)))
235 char fn[STRING], tmp[STRING];
239 m_strcpy(fn, sizeof (fn), NewsServer);
241 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
242 snprintf (dest, destlen, tmp, fn);
248 /* nntp_parse_url: given an NNPT URL, return host, port,
249 * username, password and newsgroup will recognise. */
250 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
259 act->port = NNTP_PORT;
260 act->type = M_ACCT_TYPE_NNTP;
262 c = m_strdup(server);
263 url_parse_ciss (&url, c);
265 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
266 if (url.scheme == U_NNTPS) {
268 act->port = NNTP_SSL_PORT;
273 m_strcpy(group, group_len, url.path);
275 ret = mutt_account_fromurl (act, &url);
282 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
286 url.path = m_strdup(line);
287 mutt_account_tourl (act, &url);
288 url_ciss_tostring (&url, line, len, 0);
292 static int add_group (char *buf, void *serv)
294 nntp_server_t *s = serv;
295 char group[LONG_STRING], mod, desc[HUGE_STRING];
297 nntp_data_t *nntp_data;
300 _checked = n; /* _checked have N, where N = number of groups */
301 if (!buf) /* at EOF must be zerouth */
308 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
311 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
313 nntp_data = nntp_data_new();
314 nntp_data->group = m_strdup(group);
315 nntp_data->nserv = s;
316 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
317 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
318 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
319 nntp_data_list_append(&s->list, nntp_data);
321 nntp_data->deleted = 0;
322 nntp_data->firstMessage = first;
323 nntp_data->lastMessage = last;
325 nntp_data->allowed = 1;
327 nntp_data->allowed = 0;
329 p_delete(&nntp_data->desc);
331 nntp_data->desc = m_strdup(desc);
332 if (nntp_data->rc || nntp_data->lastCached)
333 mutt_newsgroup_stat (nntp_data);
334 else if (nntp_data->lastMessage &&
335 nntp_data->firstMessage <= nntp_data->lastMessage)
336 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
338 nntp_data->unread = 0;
343 /* Load list of all newsgroups from cache ALL */
344 static int nntp_get_cache_all (nntp_server_t * serv)
346 char buf[HUGE_STRING];
349 nntp_cache_expand (buf, serv->cache);
350 if ((f = safe_fopen (buf, "r"))) {
353 while (fgets (buf, sizeof (buf), f) != NULL) {
354 if (ReadInc && (i % ReadInc == 0))
355 mutt_message (_("Loading list from cache... %d"), i);
356 add_group (buf, serv);
359 add_group (NULL, NULL);
365 p_delete(&serv->cache);
371 * Automatically loads a newsrc into memory, if necessary.
372 * Checks the size/mtime of a newsrc file, if it doesn't match, load
373 * again. Hmm, if a system has broken mtimes, this might mean the file
374 * is reloaded every time, which we'd have to fix.
376 * a newsrc file is a line per newsgroup, with the newsgroup, then a
377 * ':' denoting subscribed or '!' denoting unsubscribed, then a
378 * comma separated list of article numbers and ranges.
380 nntp_server_t *mutt_select_newsserver (char *server)
382 char file[_POSIX_PATH_MAX];
391 if (!server || !*server) {
392 mutt_error _("No newsserver defined!");
397 buf = p = p_new(char, m_strlen(server) + 10);
398 if (url_check_scheme (server) == U_UNKNOWN) {
399 strcpy (buf, "nntp://");
400 p = strchr (buf, '\0');
404 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
406 mutt_error (_("%s is an invalid newsserver specification!"), server);
411 conn = mutt_conn_find (NULL, &act);
415 m_strformat(file, sizeof(file), 0, NewsRc, nntp_format_str, NULL, 0);
416 mutt_expand_path(file, sizeof(file));
418 serv = (nntp_server_t *) conn->data;
422 /* externally modified? */
423 if (serv->stat != stat(file, &sb) || (!serv->stat &&
424 (serv->size != sb.st_size
425 || serv->mtime != sb.st_mtime)))
427 for (list = serv->list; list; list = list->next) {
428 list->subscribed = list->rc = list->num = 0;
431 nntp_clear_cacheindex (serv);
434 if (serv->status == NNTP_BYE)
435 serv->status = NNTP_NONE;
436 nntp_check_newgroups (serv, 0);
441 serv = p_new(nntp_server_t, 1);
443 serv->newsrc = m_strdup(file);
444 serv->newsgroups = hash_new(1009, false);
445 slurp_newsrc (serv); /* load .newsrc */
446 nntp_parse_cacheindex (serv); /* load .index */
447 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
448 nntp_check_newgroups (serv, 1);
449 else if (nntp_get_active (serv) < 0) {
450 hash_delete(&serv->newsgroups, NULL);
451 nntp_data_list_wipe(&serv->list);
452 p_delete(&serv->newsrc);
453 p_delete(&serv->cache);
457 nntp_clear_cacheindex (serv);
458 conn->data = (void *) serv;
464 * full status flags are not supported by nntp, but we can fake some
465 * of them. This is how:
466 * Read = a read message number is in the .newsrc
467 * New = a message is new since we last read this newsgroup
468 * Old = anything else
469 * So, Read is marked as such in the newsrc, old is anything that is
470 * "skipped" in the newsrc, and new is anything not in the newsrc nor
471 * in the cache. By skipped, I mean before the last unread message
474 nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article)
476 nntp_data_t *data = ctx->data;
479 data = hash_find(data->nserv->newsgroups, group);
483 for (int i = 0; i < data->num; i++) {
484 if ((article >= data->entries[i].first) &&
485 (article <= data->entries[i].last))
487 /* we cannot use mutt_set_flag() because mx_update_context()
494 /* If article was not cached yet, it is new! :) */
495 if (!data->cache || article > data->lastCached)
498 /* Old articles are articles which aren't read but an article after them
500 if (option(OPTMARKOLD))
504 static void nntp_create_newsrc_line(nntp_data_t *data, buffer_t *buf)
506 buffer_addstr(buf, data->group);
507 buffer_addch(buf, data->subscribed ? ':' : '!');
508 buffer_addch(buf, ' ');
510 for (int x = 0; x < data->num; x++) {
512 buffer_addch(buf, ',');
515 buffer_addf(buf, "%d-%d", data->entries[x].first,
516 data->entries[x].last);
518 buffer_addch(buf, '\n');
521 static void newsrc_gen_entries (CONTEXT * ctx)
523 nntp_data_t *data = (nntp_data_t *) ctx->data;
525 int last = 0, first = 1;
526 int save_sort = SORT_ORDER;
528 if (Sort != SORT_ORDER) {
531 mutt_sort_headers (ctx, 0);
535 data->entries = p_new(NEWSRC_ENTRY, 5);
540 * Set up to fake initial sequence from 1 to the article before the
541 * first article in our list
546 for (x = 0; x < ctx->msgcount; x++) {
547 if (series) { /* search for first unread */
549 * We don't actually check sequential order, since we mark
550 * "missing" entries as read/deleted
552 last = ctx->hdrs[x]->article_num;
553 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
554 !ctx->hdrs[x]->read) {
555 if (data->num >= data->max) {
556 data->max = data->max * 2;
557 p_realloc(&data->entries, data->max);
559 data->entries[data->num].first = first;
560 data->entries[data->num].last = last - 1;
565 else { /* search for first read */
567 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
571 last = ctx->hdrs[x]->article_num;
574 if (series && first <= data->lastLoaded) {
575 if (data->num >= data->max) {
576 data->max = data->max * 2;
577 p_realloc(&data->entries, data->max);
579 data->entries[data->num].first = first;
580 data->entries[data->num].last = data->lastLoaded;
584 if (save_sort != Sort) {
586 mutt_sort_headers (ctx, 0);
590 static int mutt_update_list_file (char *filename, char *section,
591 const char *key, const char *line)
595 char buf[HUGE_STRING];
596 char tmpf[_POSIX_PATH_MAX], lnk[_POSIX_PATH_MAX];
598 int ext = 0, done = 0, r = 0, l = 0;
600 /* if file not exist, create it */
601 if ((ifp = safe_fopen (filename, "a")))
603 if (!(ifp = safe_fopen (filename, "r"))) {
604 mutt_error (_("Unable to open %s for reading"), filename);
607 if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) {
609 mutt_error (_("Unable to lock %s"), filename);
613 /* use m_tempfile() to get a tempfile in the same
614 * directory as filename is so that we can follow symlinks
615 * via rename(2); as dirname(2) may modify its argument,
616 * temporarily use buf as copy of it
618 m_strcpy(buf, sizeof(buf), filename);
619 ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
622 mutt_error (_("Unable to open %s for writing"), tmpf);
627 while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) {
628 r = fputs (buf, ofp);
630 while (*c && *c != '\n') c++;
631 c[0] = 0; /* strip EOL */
632 if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
635 if (r != EOF && !done) {
636 snprintf (buf, sizeof(buf), "#: %s\n", section);
637 r = fputs (buf, ofp);
642 while (r != EOF && fgets (buf, sizeof (buf), ifp)) {
645 while (*c && (*c != '\r') && (*c != '\n')) c++;
647 if (*c != '\\') ext = 0;
648 } else if ((section && !strncmp (buf, "#: ", 3))) {
653 r = fputs (buf, ofp);
656 } else if (key && !strncmp (buf, key, strlen(key)) &&
657 (!*key || buf[strlen(key)] == ' ')) {
660 while (*c && (*c != '\r') && (*c != '\n')) c++;
662 if (*c == '\\') ext = 1;
664 r = fputs (line, ofp);
666 r = fputc ('\n', ofp);
670 r = fputs (buf, ofp);
674 while (r != EOF && fgets (buf, sizeof (buf), ifp))
675 r = fputs (buf, ofp);
677 /* If there wasn't a line to replace, put it on the end of the file */
678 if (r != EOF && !done && line) {
680 r = fputc ('\n', ofp);
682 mx_unlock_file (filename, fileno (ifp), 0);
687 mutt_error (_("Can't write %s"), tmpf);
691 if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
693 if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
695 mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
701 int mutt_newsrc_update (nntp_server_t * news)
712 /* we will generate full newsrc here */
713 for (data = news->list; data; data = data->next) {
714 if (!data || !data->rc)
716 nntp_create_newsrc_line(data, &buf);
718 /* newrc being fully rewritten */
720 && (r = mutt_update_list_file(news->newsrc, NULL, "", buf.data)) == 0)
724 stat (news->newsrc, &st);
725 news->size = st.st_size;
726 news->mtime = st.st_mtime;
733 static FILE *mutt_mkname (char *s)
735 char buf[_POSIX_PATH_MAX], *pc;
739 nntp_cache_expand (buf, s);
740 if ((fp = safe_fopen (buf, "w")))
743 nntp_cache_expand (buf, "cache-XXXXXX");
744 pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
745 if ((fd = mkstemp (buf)) == -1)
747 strcpy (s, pc); /* generated name */
748 return fdopen (fd, "w");
751 /* Updates info into .index file: ALL or about selected newsgroup */
752 static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
754 char buf[LONG_STRING];
755 char file[_POSIX_PATH_MAX];
756 const char *key = "ALL";
758 if (!serv || !serv->conn || !serv->conn->account.host)
761 if (data && data->group) {
763 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
764 data->firstMessage, data->lastLoaded);
767 m_strcpy(file, sizeof(file), serv->cache);
768 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
769 (int) serv->newgroups_time);
771 nntp_cache_expand (file, ".index");
772 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
775 static void nntp_delete_cache (nntp_data_t * data)
777 char buf[_POSIX_PATH_MAX];
779 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
782 nntp_cache_expand (buf, data->cache);
784 p_delete(&data->cache);
785 data->lastCached = 0;
786 nntp_cache_expand (buf, ".index");
787 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
791 /* Remove cache files of unsubscribed newsgroups */
792 void nntp_clear_cacheindex (nntp_server_t * news)
796 if (option (OPTSAVEUNSUB) || !news)
799 for (data = news->list; data; data = data->next) {
800 if (!data || data->subscribed || !data->cache)
802 nntp_delete_cache (data);
807 static int nntp_save_cache_index (nntp_server_t * news)
809 char buf[HUGE_STRING];
810 char file[_POSIX_PATH_MAX];
814 if (!news || !news->newsgroups)
816 if (!option (OPTNEWSCACHE))
820 nntp_cache_expand (file, news->cache);
822 f = safe_fopen (file, "w");
825 m_strcpy(buf, sizeof(buf), news->conn->account.host);
826 f = mutt_mkname (buf);
827 news->cache = m_strdup(buf);
828 nntp_cache_expand (file, buf);
833 for (d = news->list; d; d = d->next) {
836 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
837 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
840 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
841 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
842 if (fputs (buf, f) == EOF) {
851 if (nntp_update_cacheindex (news, NULL)) {
858 static int nntp_save_cache_group (CONTEXT * ctx)
860 char buf[HUGE_STRING], addr[STRING];
861 char file[_POSIX_PATH_MAX];
865 int i = 0, save = SORT_ORDER;
868 if (!option (OPTNEWSCACHE))
870 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
873 if (((nntp_data_t *) ctx->data)->cache) {
874 nntp_cache_expand (file, ((nntp_data_t *) ctx->data)->cache);
876 f = safe_fopen (file, "w");
879 snprintf (buf, sizeof (buf), "%s-%s",
880 ((nntp_data_t *) ctx->data)->nserv->conn->account.host,
881 ((nntp_data_t *) ctx->data)->group);
882 f = mutt_mkname (buf);
883 ((nntp_data_t *) ctx->data)->cache = m_strdup(buf);
884 nntp_cache_expand (file, buf);
889 if (Sort != SORT_ORDER) {
892 mutt_sort_headers (ctx, 0);
895 /* Save only $nntp_context messages... */
896 ((nntp_data_t *) ctx->data)->lastCached = 0;
897 if (NntpContext && ctx->msgcount > NntpContext)
898 i = ctx->msgcount - NntpContext;
899 for (; i < ctx->msgcount; i++) {
900 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
903 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
904 tm = gmtime (&h->date_sent);
905 snprintf (buf, sizeof (buf),
906 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
907 h->article_num, h->env->subject, addr, tm->tm_mday,
908 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
909 tm->tm_min, tm->tm_sec, h->env->message_id);
911 if (h->env->references)
912 mutt_write_references (h->env->references, f);
913 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
914 h->content->length, h->lines, NONULL (h->env->xref));
915 if (fputs (buf, f) == EOF) {
921 prev = ctx->hdrs[i]->article_num;
926 mutt_sort_headers (ctx, 0);
930 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
931 (nntp_data_t *) ctx->data)) {
935 ((nntp_data_t *) ctx->data)->lastCached =
936 ((nntp_data_t *) ctx->data)->lastLoaded;
940 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
944 if (!news || !news->newsgroups || !group || !*group)
946 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
947 data = nntp_data_new();
948 data->group = m_strdup(group);
951 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
952 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
953 hash_insert (news->newsgroups, data->group, data);
954 nntp_data_list_append(&news->list, data);
956 if (!data->subscribed) {
957 data->subscribed = 1;
963 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
967 if (!news || !news->newsgroups || !group || !*group ||
968 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
970 if (data->subscribed) {
971 data->subscribed = 0;
972 if (!option (OPTSAVEUNSUB))
978 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
982 if (!news || !news->newsgroups || !group || !*group ||
983 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
986 data->entries = p_new(NEWSRC_ENTRY, 5);
990 data->entries[0].first = 1;
992 data->entries[0].last = data->lastMessage;
993 if (Context && Context->data == data) {
996 for (x = 0; x < Context->msgcount; x++)
997 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
1002 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
1006 if (!news || !news->newsgroups || !group || !*group ||
1007 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
1010 data->entries = p_new(NEWSRC_ENTRY, 5);
1014 data->entries[0].first = 1;
1015 data->entries[0].last = data->firstMessage - 1;
1016 if (Context && Context->data == data) {
1019 data->unread = Context->msgcount;
1020 for (x = 0; x < Context->msgcount; x++)
1021 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
1024 data->unread = data->lastMessage - data->entries[0].last;
1028 /* this routine gives the first newsgroup with new messages */
1029 void nntp_buffy (char* dst, ssize_t dstlen) {
1033 /* forward to current group */
1034 for (list = CurrentNewsSrv->list; list; list = list->next) {
1035 if (list->subscribed && list->unread
1036 && Context && Context->magic == M_NNTP
1037 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
1049 list = CurrentNewsSrv->list;
1051 for (; list; list = list->next) {
1052 if (list->subscribed && list->unread) {
1053 if (Context && Context->magic == M_NNTP &&
1054 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1057 for (i = 0; i < Context->msgcount; i++)
1058 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1063 m_strcpy(dst, dstlen, list->group);
1076 /* nntp protocol {{{ */
1078 void nntp_sync_sidebar (nntp_data_t* data) {
1086 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1087 data->nserv->conn->account.has_ssl ? "s" : "",
1088 NONULL(data->nserv->conn->account.user),
1089 *data->nserv->conn->account.pass ? ":" : "",
1090 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1091 data->nserv->conn->account.host,
1094 /* bail out if group not found via mailboxes */
1095 if ((i = buffy_lookup (buf)) < 0)
1098 tmp = Incoming.arr[i];
1099 /* copied from browser.c */
1100 if (option (OPTMARKOLD) &&
1101 data->lastCached >= data->firstMessage &&
1102 data->lastCached <= data->lastMessage)
1103 tmp->msg_unread = data->lastMessage - data->lastCached;
1105 tmp->msg_unread = data->unread;
1106 tmp->new = data->unread > 0;
1107 /* this is closest to a "total" count we can get */
1108 tmp->msgcount = data->lastMessage - data->firstMessage;
1111 static int nntp_auth (nntp_server_t * serv)
1113 CONNECTION *conn = serv->conn;
1115 unsigned char flags = conn->account.flags;
1117 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1118 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1119 conn->account.flags = flags;
1123 mutt_message _("Logging in...");
1125 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1126 mutt_socket_write (conn, buf);
1127 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1128 conn->account.flags = flags;
1132 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1133 mutt_socket_write(conn, buf);
1134 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1135 conn->account.flags = flags;
1139 if (m_strncmp("281", buf, 3)) {
1140 conn->account.flags = flags;
1141 mutt_error _("Login failed.");
1150 static int nntp_connect_error (nntp_server_t * serv)
1152 serv->status = NNTP_NONE;
1153 mutt_socket_close (serv->conn);
1154 mutt_error _("Server closed connection!");
1160 static int nntp_connect_and_auth (nntp_server_t * serv)
1162 CONNECTION *conn = serv->conn;
1166 serv->status = NNTP_NONE;
1168 if (mutt_socket_open (conn) < 0)
1171 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1172 return nntp_connect_error (serv);
1174 if (!m_strncmp("200", buf, 3))
1175 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1176 else if (!m_strncmp("201", buf, 3))
1177 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1179 mutt_socket_close(conn);
1181 mutt_error("%s", buf);
1186 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1187 returned codes and messages. */
1188 mutt_socket_write (conn, "MODE READER\r\n");
1189 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1190 return nntp_connect_error (serv);
1192 mutt_socket_write (conn, "STAT\r\n");
1193 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1194 return nntp_connect_error (serv);
1196 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1197 serv->status = NNTP_OK;
1201 rc = nntp_auth (serv);
1203 return nntp_connect_error (serv);
1205 mutt_socket_close (conn);
1206 serv->status = NNTP_BYE;
1210 mutt_socket_close (conn);
1211 mutt_error _("Login failed.");
1216 serv->status = NNTP_OK;
1220 static int nntp_attempt_features (nntp_server_t * serv)
1222 char buf[LONG_STRING];
1223 CONNECTION *conn = serv->conn;
1225 if (serv->feat_known)
1228 mutt_socket_write (conn, "XOVER\r\n");
1229 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1230 return nntp_connect_error (serv);
1231 if (m_strncmp("500", buf, 3))
1234 mutt_socket_write (conn, "XPAT\r\n");
1235 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1236 return nntp_connect_error (serv);
1237 if (m_strncmp("500", buf, 3))
1240 mutt_socket_write (conn, "LISTGROUP\r\n");
1241 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1242 return (nntp_connect_error (serv));
1243 if (m_strncmp("500", buf, 3))
1244 serv->hasLISTGROUP = 1;
1246 mutt_socket_write (conn, "XGTITLE +\r\n");
1247 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1248 return nntp_connect_error (serv);
1249 if (m_strncmp("500", buf, 3))
1250 serv->hasXGTITLE = 1;
1252 if (!m_strncmp("282", buf, 3)) {
1254 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1255 return nntp_connect_error (serv);
1256 } while (!(buf[0] == '.' && buf[1] == '\0'));
1259 serv->feat_known = 1;
1263 static int nntp_open_connection (nntp_server_t * serv)
1265 if (serv->status == NNTP_OK)
1267 if (serv->status == NNTP_BYE)
1269 if (nntp_connect_and_auth (serv) < 0)
1271 if (nntp_attempt_features (serv) < 0)
1276 static int nntp_reconnect (nntp_server_t * serv)
1280 mutt_socket_close (serv->conn);
1283 if (nntp_connect_and_auth (serv) == 0)
1286 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1287 serv->conn->account.host);
1288 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1289 serv->status = NNTP_BYE;
1295 /* Send data from line[LONG_STRING] and receive answer to same line */
1296 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1298 char buf[LONG_STRING];
1301 if (data->nserv->status == NNTP_BYE)
1306 mutt_socket_write (data->nserv->conn, line);
1308 else if (data->group) {
1309 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1310 mutt_socket_write (data->nserv->conn, buf);
1314 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1315 if (nntp_reconnect (data->nserv) < 0)
1319 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1320 mutt_socket_write (data->nserv->conn, buf);
1321 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1327 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1331 m_strcpy(line, linelen, buf);
1336 * This function calls funct(*line, *data) for each received line,
1337 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1340 * 1 - correct but not performed (may be, have to be continued),
1341 * -1 - conection lost,
1342 * -2 - invalid command or execution error,
1343 * -3 - error in funct(*line, *data).
1345 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1346 const char *msg, progress_t* bar,
1347 int (*funct) (char *, void *),
1348 void *data, int tagged)
1350 char buf[LONG_STRING];
1359 m_strcpy(buf, sizeof(buf), query);
1360 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1369 inbuf = p_new(char, sizeof(buf));
1372 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1377 if (!lenbuf && buf[0] == '.') {
1378 if (buf[1] == '\0') {
1386 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1389 if (chunk >= ssizeof (buf)) {
1390 lenbuf += m_strlen(p);
1394 mutt_progress_bar (bar, pos);
1397 if (ReadInc && (line % ReadInc == 0)) {
1399 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1401 mutt_message ("%s %d", msg, line);
1405 if (ret == 0 && funct (inbuf, data) < 0)
1410 p_realloc(&inbuf, lenbuf + sizeof (buf));
1419 static int nntp_read_tempfile (char *line, void *file)
1421 FILE *f = (FILE *) file;
1427 if (fputc ('\n', f) == EOF)
1433 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1436 register char *p, *b;
1437 register char *colon = NULL;
1441 /* skip to next word */
1443 while (*b && ((*b == ' ') || (*b == '\t')))
1447 /* skip to end of word */
1448 while (*p && (*p != ' ') && (*p != '\t')) {
1460 nntp_get_status (ctx, h, b, atoi (colon));
1461 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1462 h->article_num = atoi (colon);
1470 * 1 if article not found
1471 * -1 if read or write error on tempfile or socket
1473 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1476 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1478 char buf[LONG_STRING];
1479 char tempfile[_POSIX_PATH_MAX];
1481 HEADER *h = ctx->hdrs[ctx->msgcount];
1483 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1488 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1490 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1492 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1496 return (ret == -1 ? -1 : 1);
1499 h->article_num = article_num;
1500 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1504 if (h->env->xref != NULL)
1505 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1506 else if (h->article_num == 0 && msgid) {
1507 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1508 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1509 h->article_num = atoi (buf + 4);
1515 static int parse_description (char *line, void *n)
1517 register char *d = line;
1519 nntp_server_t *news = n;
1523 while (*d && *d != '\t' && *d != ' ')
1527 while (*d && (*d == '\t' || *d == ' '))
1529 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1530 m_strcmp(d, data->desc)) {
1531 p_delete(&data->desc);
1532 data->desc = m_strdup(d);
1537 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1541 if (!option (OPTLOADDESC) || !data || !data->nserv)
1544 /* Get newsgroup description, if we can */
1545 if (data->nserv->hasXGTITLE)
1546 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1548 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1549 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
1555 * XOVER returns a tab separated list of:
1556 * id|subject|from|date|Msgid|references|bytes|lines|xref
1558 * This has to duplicate some of the functionality of
1559 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1560 * a limited number of headers which are "parsed" by placement in the list)
1562 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1564 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1568 hdr->env = envelope_new();
1569 hdr->env->newsgroups = m_strdup(nntp_data->group);
1570 hdr->content = body_new();
1571 hdr->content->type = TYPETEXT;
1572 hdr->content->subtype = m_strdup("plain");
1573 hdr->content->encoding = ENC7BIT;
1574 hdr->content->disposition = DISPINLINE;
1575 hdr->content->length = -1;
1578 for (x = 0; !done && x < 9; x++) {
1579 /* if from file, need to skip newline character */
1580 while (*p && *p != '\n' && *p != '\t')
1588 hdr->article_num = atoi (b);
1589 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1592 hdr->env->subject = m_strdup(b);
1595 address_list_wipe(&hdr->env->from);
1596 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1597 /* same as for mutt_parse_rfc822_line():
1598 * don't leave from info NULL if there's an invalid address (or
1599 * whatever) in From: field; mutt would just display it as empty
1600 * and mark mail/(esp.) news article as your own. aaargh! this
1601 * bothered me for _years_ */
1602 if (!hdr->env->from) {
1603 hdr->env->from = address_new ();
1604 hdr->env->from->personal = m_strdup(b);
1608 hdr->date_sent = mutt_parse_date (b, hdr);
1609 hdr->received = hdr->date_sent;
1612 p_delete(&hdr->env->message_id);
1613 hdr->env->message_id = m_strdup(b);
1616 string_list_wipe(&hdr->env->references);
1617 hdr->env->references = mutt_parse_references (b, 0);
1620 hdr->content->length = atoi (b);
1623 hdr->lines = atoi (b);
1627 p_delete(&hdr->env->xref);
1628 b = b + 6; /* skips the "Xref: " */
1629 hdr->env->xref = m_strdup(b);
1630 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1632 rfc2047_decode_envelope(hdr->env);
1648 static int nntp_fetch_numbers (char *line, void *c)
1650 FETCH_CONTEXT *fc = c;
1653 int num = atoi(line);
1654 if (num < fc->first || num > fc->last)
1657 bit_set(&fc->messages, num);
1662 static int add_xover_line (char *line, void *c)
1665 FETCH_CONTEXT *fc = c;
1666 CONTEXT *ctx = fc->ctx;
1667 nntp_data_t *data = (nntp_data_t *) ctx->data;
1672 if (ctx->msgcount >= ctx->hdrmax)
1673 mx_alloc_memory (ctx);
1674 ctx->hdrs[ctx->msgcount] = header_new();
1675 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1677 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1678 num = ctx->hdrs[ctx->msgcount]->article_num;
1680 if (num >= fc->first && num <= fc->last) {
1682 if (num > data->lastLoaded)
1683 data->lastLoaded = num;
1684 num = num - fc->first + 1;
1685 total = fc->last - fc->first + 1;
1686 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1687 mutt_message ("%s %d/%d", fc->msg, num, total);
1689 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1696 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1698 char buf[HUGE_STRING];
1699 const char *msg = _("Fetching message headers...");
1700 const char *msg2 = _("Fetching headers from cache...");
1701 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1702 int ret, num, oldmsgcount, current;
1706 /* if empty group or nothing to do */
1707 if (!last || first > last)
1710 /* fetch list of articles */
1711 mutt_message _("Fetching list of articles...");
1717 /* CACHE: must be loaded xover cache here */
1718 num = nntp_data->lastCached - first + 1;
1719 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
1720 nntp_cache_expand (buf, nntp_data->cache);
1721 mutt_message (msg2);
1723 if ((f = safe_fopen (buf, "r"))) {
1726 /* counting number of lines */
1727 while (fgets (buf, sizeof (buf), f) != NULL)
1730 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1732 oldmsgcount = ctx->msgcount;
1734 fc.last = first + num - 1;
1736 while (fgets (buf, sizeof (buf), f) != NULL) {
1737 if (ReadInc && ((++c) % ReadInc == 0))
1738 mutt_message ("%s %d/%d", msg2, c, r);
1739 add_xover_line (buf, &fc);
1742 nntp_data->lastLoaded = fc.last;
1743 first = fc.last + 1;
1744 if (ctx->msgcount > oldmsgcount)
1745 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1748 nntp_delete_cache (nntp_data);
1750 num = last - first + 1;
1756 * Without XOVER, we have to fetch each article header and parse
1757 * it. With XOVER, we ask for all of them
1760 if (nntp_data->nserv->hasXOVER) {
1761 oldmsgcount = ctx->msgcount;
1765 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1766 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1767 if (ctx->msgcount > oldmsgcount)
1768 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1770 mutt_error (_("XOVER command failed: %s"), buf);
1774 bits_init(&fc.messages);
1776 if (nntp_data->nserv->hasLISTGROUP) {
1777 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1778 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1779 nntp_fetch_numbers, &fc, 0))
1781 mutt_error (_("LISTGROUP command failed: %s"), buf);
1783 bits_wipe(&fc.messages);
1787 for (num = first; num <= last; num++)
1788 bit_set(&fc.messages, num);
1791 for (current = first; current <= last; current++) {
1794 ret = current - first + 1;
1795 mutt_message ("%s %d/%d", msg, ret, num);
1797 if (!bit_isset(&fc.messages, current))
1800 if (ctx->msgcount >= ctx->hdrmax)
1801 mx_alloc_memory (ctx);
1802 h = ctx->hdrs[ctx->msgcount] = header_new();
1803 h->index = ctx->msgcount;
1805 ret = nntp_read_header (ctx, NULL, current);
1806 if (ret == 0) { /* Got article. Fetch next header */
1807 nntp_get_status (ctx, h, NULL, h->article_num);
1809 mx_update_context (ctx, 1);
1812 header_delete(&h); /* skip it */
1814 bits_wipe(&fc.messages);
1818 if (current > nntp_data->lastLoaded)
1819 nntp_data->lastLoaded = current;
1821 bits_wipe(&fc.messages);
1824 nntp_data->lastLoaded = last;
1825 mutt_clear_error ();
1830 * currently, nntp "mailbox" is "newsgroup"
1832 static int nntp_open_mailbox (CONTEXT * ctx)
1834 nntp_data_t *nntp_data;
1835 nntp_server_t *serv;
1836 char buf[HUGE_STRING];
1837 char server[LONG_STRING];
1838 int count = 0, first;
1843 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1844 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1850 nntp_expand_path (server, sizeof (server), &act);
1851 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1854 CurrentNewsSrv = serv;
1856 /* create NNTP-specific state struct if nof found in list */
1857 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1858 nntp_data = nntp_data_new();
1859 nntp_data->group = m_strdup(buf);
1860 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1861 nntp_data_list_append(&serv->list, nntp_data);
1863 ctx->data = nntp_data;
1864 nntp_data->nserv = serv;
1866 mutt_message (_("Selecting %s..."), nntp_data->group);
1868 if (!nntp_data->desc) {
1869 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1870 if (nntp_data->desc)
1871 nntp_save_cache_index (serv);
1875 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1879 if (m_strncmp("211", buf, 3)) {
1882 /* GROUP command failed */
1883 if (!m_strncmp("411", buf, 3)) {
1884 mutt_error (_("Newsgroup %s not found on server %s"),
1885 nntp_data->group, serv->conn->account.host);
1887 /* CACHE: delete cache and line from .index */
1888 nntp_delete_cache (nntp_data);
1889 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1890 for (l = &serv->list; *l; l = &(*l)->next) {
1891 if ((*l) == nntp_data) {
1892 nntp_data_list_pop(l);
1893 nntp_data_delete(&nntp_data);
1897 nntp_data_delete(&nntp_data);
1904 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1905 &nntp_data->lastMessage, buf);
1907 nntp_data->deleted = 0;
1909 time (&serv->check_time);
1912 * Check for max adding context. If it is greater than $nntp_context,
1913 * strip off extra articles
1915 first = nntp_data->firstMessage;
1916 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1917 first = nntp_data->lastMessage - NntpContext + 1;
1919 nntp_data->lastLoaded = first - 1;
1920 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1923 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1925 char buf[LONG_STRING];
1926 char path[_POSIX_PATH_MAX];
1931 /* see if we already have the message in our cache */
1933 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1936 /* if everything is fine, assign msg->fp and return */
1937 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1938 (msg->fp = fopen (cache->path, "r")))
1941 /* clear the previous entry */
1942 unlink (cache->path);
1943 p_delete(&cache->path);
1945 cache->index = ctx->hdrs[msgno]->index;
1946 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1950 cache->path = m_strdup(path);
1952 if (ctx->hdrs[msgno]->article_num == 0)
1953 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1954 ctx->hdrs[msgno]->env->message_id);
1956 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1957 ctx->hdrs[msgno]->article_num);
1959 bar.msg = _("Fetching message...");
1961 mutt_progress_bar (&bar, 0);
1963 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1964 msg->fp, ctx->tagged);
1966 mutt_error (_("Article %d not found on server"),
1967 ctx->hdrs[msgno]->article_num);
1973 p_delete(&cache->path);
1977 envelope_delete(&ctx->hdrs[msgno]->env);
1978 ctx->hdrs[msgno]->env =
1979 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1980 /* fix content length */
1981 fseeko (msg->fp, 0, SEEK_END);
1982 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1983 ctx->hdrs[msgno]->content->offset;
1985 /* this is called in mutt before the open which fetches the message,
1986 * which is probably wrong, but we just call it again here to handle
1987 * the problem instead of fixing it.
1989 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1991 /* These would normally be updated in mx_update_context(), but the
1992 * full headers aren't parsed with XOVER, so the information wasn't
1995 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1997 mutt_clear_error ();
2004 int nntp_post (const char *msg)
2006 char buf[LONG_STRING];
2009 nntp_data_t *nntp_data;
2011 if (Context && Context->magic == M_NNTP)
2012 nntp_data = (nntp_data_t *)Context->data;
2014 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
2015 !CurrentNewsSrv->list)
2017 mutt_error (_("Can't post article. No connection to news server."));
2020 nntp_data = CurrentNewsSrv->list;
2023 if (!(f = safe_fopen (msg, "r"))) {
2024 mutt_error (_("Can't post article. Unable to open %s"), msg);
2028 m_strcpy(buf, sizeof(buf), "POST\r\n");
2029 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2030 mutt_error (_("Can't post article. Connection to %s lost."),
2031 nntp_data->nserv->conn->account.host);
2034 if (buf[0] != '3') {
2035 mutt_error (_("Can't post article: %s"), buf);
2041 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
2042 len = m_strlen(buf);
2043 if (buf[len - 1] == '\n') {
2044 buf[len - 1] = '\r';
2050 mutt_socket_write(nntp_data->nserv->conn, buf);
2052 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2056 if (buf[m_strlen(buf) - 1] != '\n')
2057 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2058 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2059 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2060 mutt_error (_("Can't post article. Connection to %s lost."),
2061 nntp_data->nserv->conn->account.host);
2064 if (buf[0] != '2') {
2065 mutt_error (_("Can't post article: %s"), buf);
2072 /* nntp_logout_all: close all open connections. */
2073 void nntp_logout_all (void)
2075 char buf[LONG_STRING];
2078 conn = mutt_socket_head ();
2081 CONNECTION* next = conn->next;
2082 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2083 mutt_message (_("Closing connection to %s..."), conn->account.host);
2084 mutt_socket_write (conn, "QUIT\r\n");
2085 mutt_socket_readln (buf, sizeof (buf), conn);
2086 mutt_clear_error ();
2087 mutt_socket_close (conn);
2088 mutt_socket_free (conn);
2094 static void nntp_free_acache (nntp_data_t * data)
2098 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2099 if (data->acache[i].path) {
2100 unlink (data->acache[i].path);
2101 p_delete(&data->acache[i].path);
2106 void nntp_data_wipe(nntp_data_t *data)
2108 p_delete(&data->entries);
2109 p_delete(&data->desc);
2110 p_delete(&data->cache);
2111 p_delete(&data->group);
2112 nntp_free_acache(data);
2115 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2117 nntp_data_t *data = ctx->data;
2119 /* CACHE: update cache and .index files */
2120 if ((option (OPTSAVEUNSUB) || data->subscribed))
2121 nntp_save_cache_group (ctx);
2122 nntp_free_acache (data);
2124 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2128 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2130 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2134 nntp_free_acache (data);
2135 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2137 nntp_save_cache_index (data->nserv);
2138 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2140 nntp_data_delete(&data);
2142 nntp_sync_sidebar (data);
2145 /* commit changes and terminate connection */
2146 int nntp_close_mailbox (CONTEXT * ctx)
2150 mutt_message _("Quitting newsgroup...");
2153 nntp_data_t *data = (nntp_data_t *) ctx->data;
2156 if (data->nserv && data->nserv->conn && ctx->unread) {
2157 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2159 mutt_newsgroup_catchup (data->nserv, data->group);
2164 nntp_sync_mailbox (ctx, 0, NULL);
2165 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2166 nntp_server_t *news;
2168 news = ((nntp_data_t *) ctx->data)->nserv;
2169 newsrc_gen_entries (ctx);
2170 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2171 mutt_newsrc_update (news);
2173 mutt_clear_error ();
2177 /* use the GROUP command to poll for new mail */
2178 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2180 char buf[LONG_STRING];
2183 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2187 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2190 if (m_strncmp("211", buf, 3)) {
2192 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2196 if (!m_strncmp("211", buf, 3)) {
2200 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2201 nntp_data->firstMessage = first;
2202 nntp_data->lastMessage = last;
2203 if (ctx && last > nntp_data->lastLoaded) {
2204 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2205 time (&nntp_data->nserv->check_time);
2208 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2209 nntp_data->unread = count;
2211 mutt_newsgroup_stat (nntp_data);
2212 /* active was renumbered? */
2213 if (last < nntp_data->lastLoaded) {
2214 if (!nntp_data->max) {
2215 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2218 nntp_data->lastCached = 0;
2220 nntp_data->entries[0].first = 1;
2221 nntp_data->entries[0].last = 0;
2223 nntp_sync_sidebar (nntp_data);
2226 time (&nntp_data->nserv->check_time);
2230 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2232 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2235 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2237 char buf[LONG_STRING];
2238 nntp_data_t nntp_data;
2239 nntp_data_t *l, **emp;
2243 if (!serv || !serv->newgroups_time)
2246 if (nntp_open_connection (serv) < 0)
2249 /* check subscribed groups for new news */
2250 if (option (OPTSHOWNEWNEWS)) {
2251 mutt_message _("Checking for new messages...");
2253 for (l = serv->list; l; l = l->next) {
2254 serv->check_time = 0; /* really check! */
2256 _nntp_check_mailbox (NULL, l);
2263 mutt_message _("Checking for new newsgroups...");
2265 now = serv->newgroups_time;
2266 time (&serv->newgroups_time);
2268 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2269 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2270 t->tm_min, t->tm_sec);
2271 nntp_data.nserv = serv;
2272 if (Context && Context->magic == M_NNTP)
2273 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2275 nntp_data.group = NULL;
2277 emp = nntp_data_list_last(&serv->list);
2278 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2279 add_group, serv, 0) != 0) {
2283 mutt_message _("Loading descriptions...");
2285 for (l = *emp; l; l = l->next) {
2287 nntp_get_desc(l, l->group, NULL, NULL);
2290 nntp_save_cache_index(serv);
2295 /* Load list of all newsgroups from active */
2296 int nntp_get_active (nntp_server_t * serv)
2299 nntp_data_t nntp_data, **tmp = &serv->list;
2301 if (nntp_open_connection (serv) < 0)
2304 snprintf (msg, sizeof (msg),
2305 _("Loading list of all newsgroups on server %s..."),
2306 serv->conn->account.host);
2308 time (&serv->newgroups_time);
2309 nntp_data.nserv = serv;
2310 nntp_data.group = NULL;
2312 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2316 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2318 nntp_get_desc (&nntp_data, "*", msg, NULL);
2321 if ((*tmp)->deleted && !(*tmp)->rc) {
2322 nntp_data_t *d = nntp_data_list_pop(tmp);
2323 nntp_delete_cache(d);
2324 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2325 nntp_data_delete(&d);
2327 tmp = &(*tmp)->next;
2330 nntp_save_cache_index (serv);
2332 mutt_clear_error ();
2337 * returns -1 if error ocurred while retrieving header,
2338 * number of articles which ones exist in context on success.
2340 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2344 /* if msgid is already in context, don't reload them */
2345 if (hash_find (ctx->id_hash, msgid))
2347 if (ctx->msgcount == ctx->hdrmax)
2348 mx_alloc_memory (ctx);
2349 ctx->hdrs[ctx->msgcount] = header_new();
2350 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2352 mutt_message (_("Fetching %s from server..."), msgid);
2353 ret = nntp_read_header (ctx, msgid, 0);
2354 /* since nntp_read_header() may set read flag, we must reset it */
2355 ctx->hdrs[ctx->msgcount]->read = 0;
2357 header_delete(&ctx->hdrs[ctx->msgcount]);
2360 mx_update_context (ctx, 1);
2373 static int check_children (char *s, void *c)
2375 CHILD_CONTEXT *cc = c;
2378 if (!s || (n = atoi (s)) == 0)
2380 for (i = 0; i < cc->ctx->msgcount; i++)
2381 if (cc->ctx->hdrs[i]->article_num == n)
2383 if (cc->num >= cc->max)
2384 p_realloc(&cc->child, cc->max += 25);
2385 cc->child[cc->num++] = n;
2390 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2392 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2394 int i, ret = 0, tmp = 0;
2395 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2397 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2398 !nntp_data->nserv->conn->account.host)
2400 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2402 if (!nntp_data->nserv->hasXPAT) {
2403 mutt_error (_("Server %s does not support this operation!"),
2404 nntp_data->nserv->conn->account.host);
2408 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2409 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2411 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2412 p_delete(&cc.child);
2416 /* dont try to read the xover cache. check_children() already
2417 * made sure that we dont have the article, so we need to visit
2418 * the server. Reading the cache at this point is also bad
2419 * because it would duplicate messages */
2420 if (option (OPTNEWSCACHE)) {
2422 unset_option (OPTNEWSCACHE);
2424 for (i = 0; i < cc.num; i++) {
2425 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2427 if (ctx->msgcount &&
2428 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2429 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2432 set_option (OPTNEWSCACHE);
2433 p_delete(&cc.child);
2437 static int nntp_is_magic (const char* path, struct stat* st) {
2438 url_scheme_t s = url_check_scheme(NONULL(path));
2439 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2442 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2444 case ACL_INSERT: /* editing messages */
2445 case ACL_WRITE: /* change importance */
2447 case ACL_DELETE: /* (un)deletion */
2448 case ACL_SEEN: /* mark as read */
2455 mx_t const nntp_mx = {
2465 nntp_fastclose_mailbox,