2 * Copyright notice from original mutt:
3 * Copyright (C) 1998 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
5 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 #include <lib-lib/lib-lib.h>
16 #include <lib-mime/mime.h>
17 #include <lib-ui/curses.h>
18 #include <lib-ui/sidebar.h>
19 #include <lib-mx/mx.h>
28 #define NNTP_SSL_PORT 563
31 unsigned use_cache : 1;
36 static int nntp_check_newgroups (nntp_server_t *, int);
40 static void mutt_newsgroup_stat(nntp_data_t *data)
43 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
46 data->unread = data->lastMessage - data->firstMessage + 1;
47 for (int i = 0; i < data->num; i++) {
48 int first = MAX(data->entries[i].first, data->firstMessage);
49 int last = MIN(data->entries[i].last, data->lastMessage);
50 data->unread -= MAX(0, last - first + 1);
54 static int nntp_parse_newsrc_line(nntp_server_t *news, const char *line)
57 char group[LONG_STRING];
61 for (p = line; *p; p++) {
65 p = strpbrk(line, ":!");
69 m_strncpy(group, ssizeof(group), line, p - line);
70 data = hash_find(news->newsgroups, group);
72 data = nntp_data_new();
73 data->group = p_dupstr(line, p - line);
76 hash_insert(news->newsgroups, data->group, data);
77 news->tail = nntp_data_list_append(news->tail, data);
79 p_delete(&data->entries);
83 data->entries = p_new(NEWSRC_ENTRY, x * 2);
85 data->subscribed = (*p++ == ':');
88 for (x = 0; *p; p++) {
89 data->entries[x].first = strtol(p, (char **)&p, 10);
90 p += strcspn(p, "-,");
94 data->entries[x].last = strtol(p + 1, (char **)&p, 10);
96 data->entries[x].last = data->entries[x].first;
97 p = strchrnul(p, ',');
99 x += data->entries[x].last != 0;
102 if (x && !data->lastMessage)
103 data->lastMessage = data->entries[x - 1].last;
105 mutt_newsgroup_stat(data);
109 static int slurp_newsrc (nntp_server_t * news)
116 news->stat = stat(news->newsrc, &sb);
117 news->size = sb.st_size;
118 news->mtime = sb.st_mtime;
120 if ((fp = safe_fopen(news->newsrc, "r")) == NULL)
123 /* hmm, should we use dotlock? */
124 if (mx_lock_file(news->newsrc, fileno (fp), 0, 0, 1)) {
129 while (getline(&buf, &n, fp) >= 0) {
130 nntp_parse_newsrc_line(news, buf);
134 mx_unlock_file (news->newsrc, fileno (fp), 0);
139 #define nntp_cache_expand(dst, dlen, fmt, ...) \
141 snprintf((dst), (dlen), "%s/" fmt, mod_core.cachedir, ##__VA_ARGS__);\
142 mutt_expand_path((dst), (dlen)); \
145 /* Loads $news_cache_dir/.index into memory, loads newsserver data
146 * and newsgroup cache names */
147 static int nntp_parse_cacheindex(nntp_server_t *news)
149 char buf[HUGE_STRING];
152 p_delete(&news->cache);
153 if (m_strisempty(mod_core.cachedir))
156 nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host);
157 if (!(idx = safe_fopen(buf, "a+")))
161 while (fgets(buf, sizeof(buf), idx)) {
162 buf[m_strlen(buf) - 1] = 0; /* strip ending '\n' */
163 if (!m_strncmp(buf, "#: ", 3)
164 && !m_strcasecmp(buf + 3, news->conn->account.host)) {
169 while (fgets(buf, sizeof(buf), idx)) {
173 if (m_strstart(buf, "#:", NULL))
176 p = strchrnul(buf, ' ');
181 q = strchrnul(p, ' ');
185 l = strtol(q, &q, 10);
186 m = strtol(q, &q, 10);
188 if (!m_strcmp(buf, "ALL")) {
189 m_strreplace(&news->cache, m_strdup(p));
190 news->newgroups_time = m;
194 if (news->newsgroups) {
195 nntp_data_t *data = hash_find(news->newsgroups, buf);
198 data = nntp_data_new();
199 data->group = m_strdup(buf);
202 hash_insert (news->newsgroups, data->group, data);
203 news->tail = nntp_data_list_append(news->tail, data);
205 m_strreplace(&data->cache, p);
207 t = !data->firstMessage || data->lastMessage < m;
208 if (!data->firstMessage)
209 data->firstMessage = l;
210 if (data->lastMessage < m)
211 data->lastMessage = m;
212 data->lastCached = m;
213 if (t || !data->unread)
214 mutt_newsgroup_stat(data);
222 /* nntp_parse_url: given an NNPT URL, return host, port,
223 * username, password and newsgroup will recognise. */
224 static int nntp_parse_url(const char *server, ACCOUNT * act, char *group,
233 act->port = NNTP_PORT;
234 act->type = M_ACCT_TYPE_NNTP;
236 c = m_strdup(server);
237 url_parse_ciss (&url, c);
239 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
240 if (url.scheme == U_NNTPS) {
242 act->port = NNTP_SSL_PORT;
247 m_strcpy(group, group_len, url.path);
249 ret = mutt_account_fromurl (act, &url);
256 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
260 url.path = m_strdup(line);
261 mutt_account_tourl (act, &url);
262 url_ciss_tostring (&url, line, len, 0);
266 static int add_group (char *buf, void *serv)
268 nntp_server_t *s = serv;
269 char group[LONG_STRING], mod, desc[HUGE_STRING];
271 nntp_data_t *nntp_data;
274 nntp.checked = n; /* nntp.checked have N, where N = number of groups */
275 if (!buf) /* at EOF must be zerouth */
282 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
285 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
287 nntp_data = nntp_data_new();
288 nntp_data->group = m_strdup(group);
289 nntp_data->nserv = s;
290 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
291 s->tail = nntp_data_list_append(s->tail, nntp_data);
293 nntp_data->deleted = 0;
294 nntp_data->firstMessage = first;
295 nntp_data->lastMessage = last;
297 nntp_data->allowed = 1;
299 nntp_data->allowed = 0;
301 p_delete(&nntp_data->desc);
303 nntp_data->desc = m_strdup(desc);
304 if (nntp_data->rc || nntp_data->lastCached)
305 mutt_newsgroup_stat (nntp_data);
306 else if (nntp_data->lastMessage &&
307 nntp_data->firstMessage <= nntp_data->lastMessage)
308 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
310 nntp_data->unread = 0;
315 /* Load list of all newsgroups from cache ALL */
316 static int nntp_get_cache_all (nntp_server_t * serv)
318 char buf[HUGE_STRING];
321 nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
322 if ((f = safe_fopen (buf, "r"))) {
325 while (fgets (buf, sizeof (buf), f) != NULL) {
326 if (ReadInc && (i % ReadInc == 0))
327 mutt_message (_("Loading list from cache... %d"), i);
328 add_group (buf, serv);
331 add_group (NULL, NULL);
337 p_delete(&serv->cache);
343 * Automatically loads a newsrc into memory, if necessary.
344 * Checks the size/mtime of a newsrc file, if it doesn't match, load
345 * again. Hmm, if a system has broken mtimes, this might mean the file
346 * is reloaded every time, which we'd have to fix.
348 * a newsrc file is a line per newsgroup, with the newsgroup, then a
349 * ':' denoting subscribed or '!' denoting unsubscribed, then a
350 * comma separated list of article numbers and ranges.
352 nntp_server_t *mutt_select_newsserver (char *server)
354 char file[_POSIX_PATH_MAX];
363 if (!server || !*server) {
364 mutt_error _("No newsserver defined!");
369 buf = p = p_new(char, m_strlen(server) + 10);
370 if (url_check_scheme (server) == U_UNKNOWN) {
371 strcpy (buf, "nntp://");
372 p = strchr (buf, '\0');
376 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
378 mutt_error (_("%s is an invalid newsserver specification!"), server);
383 conn = mutt_conn_find (NULL, &act);
387 nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
388 serv = (nntp_server_t *) conn->data;
392 /* externally modified? */
393 if (serv->stat != stat(file, &sb) || (!serv->stat &&
394 (serv->size != sb.st_size
395 || serv->mtime != sb.st_mtime)))
397 for (list = serv->list; list; list = list->next) {
398 list->subscribed = list->rc = list->num = 0;
403 if (serv->status == NNTP_BYE)
404 serv->status = NNTP_NONE;
405 nntp_check_newgroups (serv, 0);
410 serv = p_new(nntp_server_t, 1);
411 serv->tail = &serv->list;
413 serv->newsrc = m_strdup(file);
414 serv->newsgroups = hash_new(SHRT_MAX, false);
415 slurp_newsrc (serv); /* load .newsrc */
416 nntp_parse_cacheindex (serv); /* load .index */
417 if (nntp.use_cache && serv->cache && nntp_get_cache_all (serv) >= 0)
418 nntp_check_newgroups (serv, 1);
419 else if (nntp_get_active (serv) < 0) {
420 hash_delete(&serv->newsgroups, NULL);
421 nntp_data_list_wipe(&serv->list);
422 p_delete(&serv->newsrc);
423 p_delete(&serv->cache);
427 conn->data = (void *) serv;
433 * full status flags are not supported by nntp, but we can fake some
434 * of them. This is how:
435 * Read = a read message number is in the .newsrc
436 * New = a message is new since we last read this newsgroup
437 * Old = anything else
438 * So, Read is marked as such in the newsrc, old is anything that is
439 * "skipped" in the newsrc, and new is anything not in the newsrc nor
440 * in the cache. By skipped, I mean before the last unread message
443 nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article)
445 nntp_data_t *data = ctx->data;
448 data = hash_find(data->nserv->newsgroups, group);
452 for (int i = 0; i < data->num; i++) {
453 if ((article >= data->entries[i].first) &&
454 (article <= data->entries[i].last))
456 /* we cannot use mutt_set_flag() because mx_update_context()
463 /* If article was not cached yet, it is new! :) */
464 if (!data->cache || article > data->lastCached)
467 /* Old articles are articles which aren't read but an article after them
469 if (option(OPTMARKOLD))
473 static void nntp_create_newsrc_line(nntp_data_t *data, buffer_t *buf)
475 buffer_addstr(buf, data->group);
476 buffer_addch(buf, data->subscribed ? ':' : '!');
477 buffer_addch(buf, ' ');
479 for (int x = 0; x < data->num; x++) {
481 buffer_addch(buf, ',');
484 buffer_addf(buf, "%d-%d", data->entries[x].first,
485 data->entries[x].last);
487 buffer_addch(buf, '\n');
490 static void newsrc_gen_entries (CONTEXT * ctx)
492 nntp_data_t *data = (nntp_data_t *) ctx->data;
494 int last = 0, first = 1;
495 int save_sort = SORT_ORDER;
497 if (Sort != SORT_ORDER) {
500 mutt_sort_headers (ctx, 0);
504 data->entries = p_new(NEWSRC_ENTRY, 5);
509 * Set up to fake initial sequence from 1 to the article before the
510 * first article in our list
515 for (x = 0; x < ctx->msgcount; x++) {
516 if (series) { /* search for first unread */
518 * We don't actually check sequential order, since we mark
519 * "missing" entries as read/deleted
521 last = ctx->hdrs[x]->article_num;
522 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
523 !ctx->hdrs[x]->read) {
524 if (data->num >= data->max) {
525 data->max = data->max * 2;
526 p_realloc(&data->entries, data->max);
528 data->entries[data->num].first = first;
529 data->entries[data->num].last = last - 1;
534 else { /* search for first read */
536 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
540 last = ctx->hdrs[x]->article_num;
543 if (series && first <= data->lastLoaded) {
544 if (data->num >= data->max) {
545 data->max = data->max * 2;
546 p_realloc(&data->entries, data->max);
548 data->entries[data->num].first = first;
549 data->entries[data->num].last = data->lastLoaded;
553 if (save_sort != Sort) {
555 mutt_sort_headers (ctx, 0);
559 static int mutt_update_list_file (char *filename, char *section,
560 const char *key, const char *line)
564 char buf[HUGE_STRING];
565 char tmpf[_POSIX_PATH_MAX], lnk[_POSIX_PATH_MAX];
567 int ext = 0, done = 0, r = 0, l = 0;
569 /* if file not exist, create it */
570 if ((ifp = safe_fopen (filename, "a")))
572 if (!(ifp = safe_fopen (filename, "r"))) {
573 mutt_error (_("Unable to open %s for reading"), filename);
576 if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) {
578 mutt_error (_("Unable to lock %s"), filename);
582 /* use m_tempfile() to get a tempfile in the same
583 * directory as filename is so that we can follow symlinks
584 * via rename(2); as dirname(2) may modify its argument,
585 * temporarily use buf as copy of it
587 m_strcpy(buf, sizeof(buf), filename);
588 ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
591 mutt_error (_("Unable to open %s for writing"), tmpf);
596 while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) {
597 r = fputs (buf, ofp);
599 while (*c && *c != '\n') c++;
600 c[0] = 0; /* strip EOL */
601 if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
604 if (r != EOF && !done) {
605 snprintf (buf, sizeof(buf), "#: %s\n", section);
606 r = fputs (buf, ofp);
611 while (r != EOF && fgets (buf, sizeof (buf), ifp)) {
614 while (*c && (*c != '\r') && (*c != '\n')) c++;
616 if (*c != '\\') ext = 0;
617 } else if ((section && !strncmp (buf, "#: ", 3))) {
622 r = fputs (buf, ofp);
625 } else if (key && !strncmp (buf, key, strlen(key)) &&
626 (!*key || buf[strlen(key)] == ' ')) {
629 while (*c && (*c != '\r') && (*c != '\n')) c++;
631 if (*c == '\\') ext = 1;
633 r = fputs (line, ofp);
635 r = fputc ('\n', ofp);
639 r = fputs (buf, ofp);
643 while (r != EOF && fgets (buf, sizeof (buf), ifp))
644 r = fputs (buf, ofp);
646 /* If there wasn't a line to replace, put it on the end of the file */
647 if (r != EOF && !done && line) {
649 r = fputc ('\n', ofp);
651 mx_unlock_file (filename, fileno (ifp), 0);
656 mutt_error (_("Can't write %s"), tmpf);
660 if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
662 if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
664 mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
670 int mutt_newsrc_update (nntp_server_t * news)
681 /* we will generate full newsrc here */
682 for (data = news->list; data; data = data->next) {
683 if (!data || !data->rc)
685 nntp_create_newsrc_line(data, &buf);
687 /* newrc being fully rewritten */
689 && (r = mutt_update_list_file(news->newsrc, NULL, "", buf.data)) == 0)
693 stat (news->newsrc, &st);
694 news->size = st.st_size;
695 news->mtime = st.st_mtime;
702 static FILE *mutt_mkname (char *s)
704 char buf[_POSIX_PATH_MAX], *pc;
708 nntp_cache_expand(buf, ssizeof(buf), "%s", s);
709 if ((fp = safe_fopen (buf, "w")))
712 nntp_cache_expand(buf, ssizeof(buf), "cache-XXXXXX");
713 pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
714 if ((fd = mkstemp (buf)) == -1)
716 strcpy (s, pc); /* generated name */
717 return fdopen (fd, "w");
720 /* Updates info into .index file: ALL or about selected newsgroup */
721 static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
723 char buf[LONG_STRING];
724 char file[_POSIX_PATH_MAX];
725 const char *key = "ALL";
727 if (!serv || !serv->conn || !serv->conn->account.host)
730 if (data && data->group) {
732 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
733 data->firstMessage, data->lastLoaded);
736 m_strcpy(file, sizeof(file), serv->cache);
737 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
738 (int) serv->newgroups_time);
740 nntp_cache_expand(file, ssizeof(file), "%s.index", serv->conn->account.host);
741 return mutt_update_list_file(file, serv->conn->account.host, key, buf);
744 static void nntp_delete_cache (nntp_data_t * data)
746 char buf[_POSIX_PATH_MAX];
748 if (!nntp.use_cache || !data || !data->cache || !data->nserv)
751 nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache);
753 p_delete(&data->cache);
754 data->lastCached = 0;
755 nntp_cache_expand(buf, ssizeof(buf), "%s.index",
756 data->nserv->conn->account.host);
757 mutt_update_list_file(buf, data->nserv->conn->account.host, data->group,
761 static int nntp_save_cache_index (nntp_server_t * news)
763 char buf[HUGE_STRING];
764 char file[_POSIX_PATH_MAX];
768 if (!news || !news->newsgroups)
774 nntp_cache_expand(file, ssizeof(file), "%s", news->cache);
776 f = safe_fopen(file, "w");
778 m_strcpy(buf, ssizeof(buf), news->conn->account.host);
779 f = mutt_mkname(buf);
780 news->cache = m_strdup(buf);
781 nntp_cache_expand(file, ssizeof(file), "%s", buf);
786 for (d = news->list; d; d = d->next) {
789 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
790 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
793 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
794 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
795 if (fputs (buf, f) == EOF) {
804 if (nntp_update_cacheindex (news, NULL)) {
811 static int nntp_save_cache_group (CONTEXT * ctx)
813 char buf[HUGE_STRING], addr[STRING];
814 char file[_POSIX_PATH_MAX];
818 int i = 0, save = SORT_ORDER;
823 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
826 if (((nntp_data_t *) ctx->data)->cache) {
827 nntp_cache_expand(file, ssizeof(file), "%s",
828 ((nntp_data_t *)ctx->data)->cache);
830 f = safe_fopen (file, "w");
833 snprintf(buf, ssizeof(buf), "%s-%s",
834 ((nntp_data_t *)ctx->data)->nserv->conn->account.host,
835 ((nntp_data_t *)ctx->data)->group);
836 f = mutt_mkname (buf);
837 ((nntp_data_t *)ctx->data)->cache = m_strdup(buf);
838 nntp_cache_expand(file, ssizeof(file), "%s", buf);
843 if (Sort != SORT_ORDER) {
846 mutt_sort_headers (ctx, 0);
849 /* Save only $nntp_context messages... */
850 ((nntp_data_t *) ctx->data)->lastCached = 0;
851 if (NntpContext && ctx->msgcount > NntpContext)
852 i = ctx->msgcount - NntpContext;
853 for (; i < ctx->msgcount; i++) {
854 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
857 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
858 tm = gmtime (&h->date_sent);
859 snprintf (buf, sizeof (buf),
860 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
861 h->article_num, h->env->subject, addr, tm->tm_mday,
862 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
863 tm->tm_min, tm->tm_sec, h->env->message_id);
865 if (h->env->references)
866 mutt_write_references (h->env->references, f);
867 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
868 h->content->length, h->lines, NONULL (h->env->xref));
869 if (fputs (buf, f) == EOF) {
875 prev = ctx->hdrs[i]->article_num;
880 mutt_sort_headers (ctx, 0);
884 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
885 (nntp_data_t *) ctx->data)) {
889 ((nntp_data_t *) ctx->data)->lastCached =
890 ((nntp_data_t *) ctx->data)->lastLoaded;
894 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
898 if (!news || !news->newsgroups || !group || !*group)
900 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
901 data = nntp_data_new();
902 data->group = m_strdup(group);
905 hash_insert (news->newsgroups, data->group, data);
906 news->tail = nntp_data_list_append(news->tail, data);
908 if (!data->subscribed) {
909 data->subscribed = 1;
915 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
919 if (!news || !news->newsgroups || !group || !*group ||
920 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
922 if (data->subscribed) {
923 data->subscribed = 0;
928 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
932 if (!news || !news->newsgroups || !group || !*group ||
933 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
936 data->entries = p_new(NEWSRC_ENTRY, 5);
940 data->entries[0].first = 1;
942 data->entries[0].last = data->lastMessage;
943 if (Context && Context->data == data) {
946 for (x = 0; x < Context->msgcount; x++)
947 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
952 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
956 if (!news || !news->newsgroups || !group || !*group ||
957 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
960 data->entries = p_new(NEWSRC_ENTRY, 5);
964 data->entries[0].first = 1;
965 data->entries[0].last = data->firstMessage - 1;
966 if (Context && Context->data == data) {
969 data->unread = Context->msgcount;
970 for (x = 0; x < Context->msgcount; x++)
971 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
974 data->unread = data->lastMessage - data->entries[0].last;
978 /* this routine gives the first newsgroup with new messages */
979 void nntp_buffy (char* dst, ssize_t dstlen) {
983 /* forward to current group */
984 for (list = CurrentNewsSrv->list; list; list = list->next) {
985 if (list->subscribed && list->unread
986 && Context && Context->magic == M_NNTP
987 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
999 list = CurrentNewsSrv->list;
1001 for (; list; list = list->next) {
1002 if (list->subscribed && list->unread) {
1003 if (Context && Context->magic == M_NNTP &&
1004 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
1007 for (i = 0; i < Context->msgcount; i++)
1008 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1013 m_strcpy(dst, dstlen, list->group);
1026 /* nntp protocol {{{ */
1028 void nntp_sync_sidebar (nntp_data_t* data) {
1036 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1037 data->nserv->conn->account.has_ssl ? "s" : "",
1038 NONULL(data->nserv->conn->account.user),
1039 *data->nserv->conn->account.pass ? ":" : "",
1040 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1041 data->nserv->conn->account.host,
1044 /* bail out if group not found via mailboxes */
1045 if ((i = buffy_lookup (buf)) < 0)
1048 tmp = Incoming.arr[i];
1049 /* copied from browser.c */
1050 if (option (OPTMARKOLD) &&
1051 data->lastCached >= data->firstMessage &&
1052 data->lastCached <= data->lastMessage)
1053 tmp->msg_unread = data->lastMessage - data->lastCached;
1055 tmp->msg_unread = data->unread;
1056 tmp->new = data->unread > 0;
1057 /* this is closest to a "total" count we can get */
1058 tmp->msgcount = data->lastMessage - data->firstMessage;
1061 static int nntp_auth (nntp_server_t * serv)
1063 CONNECTION *conn = serv->conn;
1065 unsigned char flags = conn->account.flags;
1067 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1068 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1069 conn->account.flags = flags;
1073 mutt_message _("Logging in...");
1075 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1076 mutt_socket_write (conn, buf);
1077 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1078 conn->account.flags = flags;
1082 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1083 mutt_socket_write(conn, buf);
1084 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1085 conn->account.flags = flags;
1089 if (m_strncmp("281", buf, 3)) {
1090 conn->account.flags = flags;
1091 mutt_error _("Login failed.");
1100 static int nntp_connect_error (nntp_server_t * serv)
1102 serv->status = NNTP_NONE;
1103 mutt_socket_close (serv->conn);
1104 mutt_error _("Server closed connection!");
1110 static int nntp_connect_and_auth (nntp_server_t * serv)
1112 CONNECTION *conn = serv->conn;
1116 serv->status = NNTP_NONE;
1118 if (mutt_socket_open (conn) < 0)
1121 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1122 return nntp_connect_error (serv);
1124 if (!m_strncmp("200", buf, 3))
1125 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1126 else if (!m_strncmp("201", buf, 3))
1127 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1129 mutt_socket_close(conn);
1131 mutt_error("%s", buf);
1136 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1137 returned codes and messages. */
1138 mutt_socket_write (conn, "MODE READER\r\n");
1139 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1140 return nntp_connect_error (serv);
1142 mutt_socket_write (conn, "STAT\r\n");
1143 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1144 return nntp_connect_error (serv);
1146 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1147 serv->status = NNTP_OK;
1151 rc = nntp_auth (serv);
1153 return nntp_connect_error (serv);
1155 mutt_socket_close (conn);
1156 serv->status = NNTP_BYE;
1160 mutt_socket_close (conn);
1161 mutt_error _("Login failed.");
1166 serv->status = NNTP_OK;
1170 static int nntp_attempt_features (nntp_server_t * serv)
1172 char buf[LONG_STRING];
1173 CONNECTION *conn = serv->conn;
1175 if (serv->feat_known)
1178 mutt_socket_write (conn, "XOVER\r\n");
1179 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1180 return nntp_connect_error (serv);
1181 if (m_strncmp("500", buf, 3))
1184 mutt_socket_write (conn, "XPAT\r\n");
1185 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1186 return nntp_connect_error (serv);
1187 if (m_strncmp("500", buf, 3))
1190 mutt_socket_write (conn, "LISTGROUP\r\n");
1191 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1192 return (nntp_connect_error (serv));
1193 if (m_strncmp("500", buf, 3))
1194 serv->hasLISTGROUP = 1;
1196 mutt_socket_write (conn, "XGTITLE +\r\n");
1197 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1198 return nntp_connect_error (serv);
1199 if (m_strncmp("500", buf, 3))
1200 serv->hasXGTITLE = 1;
1202 if (!m_strncmp("282", buf, 3)) {
1204 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1205 return nntp_connect_error (serv);
1206 } while (!(buf[0] == '.' && buf[1] == '\0'));
1209 serv->feat_known = 1;
1213 static int nntp_open_connection (nntp_server_t * serv)
1215 if (serv->status == NNTP_OK)
1217 if (serv->status == NNTP_BYE)
1219 if (nntp_connect_and_auth (serv) < 0)
1221 if (nntp_attempt_features (serv) < 0)
1226 static int nntp_reconnect (nntp_server_t * serv)
1230 mutt_socket_close (serv->conn);
1233 if (nntp_connect_and_auth (serv) == 0)
1236 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1237 serv->conn->account.host);
1238 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1239 serv->status = NNTP_BYE;
1245 /* Send data from line[LONG_STRING] and receive answer to same line */
1246 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1248 char buf[LONG_STRING];
1251 if (data->nserv->status == NNTP_BYE)
1256 mutt_socket_write (data->nserv->conn, line);
1258 else if (data->group) {
1259 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1260 mutt_socket_write (data->nserv->conn, buf);
1264 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1265 if (nntp_reconnect (data->nserv) < 0)
1269 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1270 mutt_socket_write (data->nserv->conn, buf);
1271 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1277 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1281 m_strcpy(line, linelen, buf);
1286 * This function calls funct(*line, *data) for each received line,
1287 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1290 * 1 - correct but not performed (may be, have to be continued),
1291 * -1 - conection lost,
1292 * -2 - invalid command or execution error,
1293 * -3 - error in funct(*line, *data).
1295 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1296 const char *msg, progress_t* bar,
1297 int (*funct) (char *, void *),
1298 void *data, int tagged)
1300 char buf[LONG_STRING];
1309 m_strcpy(buf, sizeof(buf), query);
1310 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1319 inbuf = p_new(char, sizeof(buf));
1322 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1327 if (!lenbuf && buf[0] == '.') {
1328 if (buf[1] == '\0') {
1336 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1339 if (chunk >= ssizeof (buf)) {
1340 lenbuf += m_strlen(p);
1344 mutt_progress_bar (bar, pos);
1347 if (ReadInc && (line % ReadInc == 0)) {
1349 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1351 mutt_message ("%s %d", msg, line);
1355 if (ret == 0 && funct (inbuf, data) < 0)
1360 p_realloc(&inbuf, lenbuf + sizeof (buf));
1369 static int nntp_read_tempfile (char *line, void *file)
1371 FILE *f = (FILE *) file;
1377 if (fputc ('\n', f) == EOF)
1383 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1386 register char *p, *b;
1387 register char *colon = NULL;
1391 /* skip to next word */
1393 while (*b && ((*b == ' ') || (*b == '\t')))
1397 /* skip to end of word */
1398 while (*p && (*p != ' ') && (*p != '\t')) {
1410 nntp_get_status (ctx, h, b, atoi (colon));
1411 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1412 h->article_num = atoi (colon);
1420 * 1 if article not found
1421 * -1 if read or write error on tempfile or socket
1423 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1426 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1428 char buf[LONG_STRING];
1429 char tempfile[_POSIX_PATH_MAX];
1431 HEADER *h = ctx->hdrs[ctx->msgcount];
1433 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1438 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1440 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1442 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1446 return (ret == -1 ? -1 : 1);
1449 h->article_num = article_num;
1450 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1454 if (h->env->xref != NULL)
1455 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1456 else if (h->article_num == 0 && msgid) {
1457 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1458 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1459 h->article_num = atoi (buf + 4);
1465 static int parse_description (char *line, void *n)
1467 register char *d = line;
1469 nntp_server_t *news = n;
1473 while (*d && *d != '\t' && *d != ' ')
1477 while (*d && (*d == '\t' || *d == ' '))
1479 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1480 m_strcmp(d, data->desc)) {
1481 p_delete(&data->desc);
1482 data->desc = m_strdup(d);
1487 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1491 if (!option (OPTLOADDESC) || !data || !data->nserv)
1494 /* Get newsgroup description, if we can */
1495 if (data->nserv->hasXGTITLE)
1496 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1498 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1499 mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0);
1503 * XOVER returns a tab separated list of:
1504 * id|subject|from|date|Msgid|references|bytes|lines|xref
1506 * This has to duplicate some of the functionality of
1507 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1508 * a limited number of headers which are "parsed" by placement in the list)
1510 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1512 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1516 hdr->env = envelope_new();
1517 hdr->env->newsgroups = m_strdup(nntp_data->group);
1518 hdr->content = body_new();
1519 hdr->content->type = TYPETEXT;
1520 hdr->content->subtype = m_strdup("plain");
1521 hdr->content->encoding = ENC7BIT;
1522 hdr->content->disposition = DISPINLINE;
1523 hdr->content->length = -1;
1526 for (x = 0; !done && x < 9; x++) {
1527 /* if from file, need to skip newline character */
1528 while (*p && *p != '\n' && *p != '\t')
1536 hdr->article_num = atoi (b);
1537 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1540 hdr->env->subject = m_strdup(b);
1543 address_list_wipe(&hdr->env->from);
1544 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1545 /* same as for mutt_parse_rfc822_line():
1546 * don't leave from info NULL if there's an invalid address (or
1547 * whatever) in From: field; mutt would just display it as empty
1548 * and mark mail/(esp.) news article as your own. aaargh! this
1549 * bothered me for _years_ */
1550 if (!hdr->env->from) {
1551 hdr->env->from = address_new ();
1552 hdr->env->from->personal = m_strdup(b);
1556 hdr->date_sent = mutt_parse_date (b, hdr);
1557 hdr->received = hdr->date_sent;
1560 p_delete(&hdr->env->message_id);
1561 hdr->env->message_id = m_strdup(b);
1564 string_list_wipe(&hdr->env->references);
1565 hdr->env->references = mutt_parse_references (b, 0);
1568 hdr->content->length = atoi (b);
1571 hdr->lines = atoi (b);
1575 p_delete(&hdr->env->xref);
1576 b = b + 6; /* skips the "Xref: " */
1577 hdr->env->xref = m_strdup(b);
1578 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1580 rfc2047_decode_envelope(hdr->env);
1596 static int nntp_fetch_numbers (char *line, void *c)
1598 FETCH_CONTEXT *fc = c;
1601 int num = atoi(line);
1602 if (num < fc->first || num > fc->last)
1605 bit_set(&fc->messages, num);
1610 static int add_xover_line (char *line, void *c)
1613 FETCH_CONTEXT *fc = c;
1614 CONTEXT *ctx = fc->ctx;
1615 nntp_data_t *data = (nntp_data_t *) ctx->data;
1620 if (ctx->msgcount >= ctx->hdrmax)
1621 mx_alloc_memory (ctx);
1622 ctx->hdrs[ctx->msgcount] = header_new();
1623 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1625 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1626 num = ctx->hdrs[ctx->msgcount]->article_num;
1628 if (num >= fc->first && num <= fc->last) {
1630 if (num > data->lastLoaded)
1631 data->lastLoaded = num;
1632 num = num - fc->first + 1;
1633 total = fc->last - fc->first + 1;
1634 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1635 mutt_message ("%s %d/%d", fc->msg, num, total);
1637 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1644 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1646 char buf[HUGE_STRING];
1647 const char *msg = _("Fetching message headers...");
1648 const char *msg2 = _("Fetching headers from cache...");
1649 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1650 int ret, num, oldmsgcount, current;
1654 /* if empty group or nothing to do */
1655 if (!last || first > last)
1658 /* fetch list of articles */
1659 mutt_message _("Fetching list of articles...");
1665 /* CACHE: must be loaded xover cache here */
1666 num = nntp_data->lastCached - first + 1;
1667 if (nntp.use_cache && nntp_data->cache && num > 0) {
1668 nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
1669 mutt_message (msg2);
1671 if ((f = safe_fopen (buf, "r"))) {
1674 /* counting number of lines */
1675 while (fgets (buf, sizeof (buf), f) != NULL)
1678 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1680 oldmsgcount = ctx->msgcount;
1682 fc.last = first + num - 1;
1684 while (fgets (buf, sizeof (buf), f) != NULL) {
1685 if (ReadInc && ((++c) % ReadInc == 0))
1686 mutt_message ("%s %d/%d", msg2, c, r);
1687 add_xover_line (buf, &fc);
1690 nntp_data->lastLoaded = fc.last;
1691 first = fc.last + 1;
1692 if (ctx->msgcount > oldmsgcount)
1693 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1696 nntp_delete_cache (nntp_data);
1698 num = last - first + 1;
1704 * Without XOVER, we have to fetch each article header and parse
1705 * it. With XOVER, we ask for all of them
1708 if (nntp_data->nserv->hasXOVER) {
1709 oldmsgcount = ctx->msgcount;
1713 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1714 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1715 if (ctx->msgcount > oldmsgcount)
1716 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1718 mutt_error (_("XOVER command failed: %s"), buf);
1722 bits_init(&fc.messages);
1724 if (nntp_data->nserv->hasLISTGROUP) {
1725 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1726 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1727 nntp_fetch_numbers, &fc, 0))
1729 mutt_error (_("LISTGROUP command failed: %s"), buf);
1731 bits_wipe(&fc.messages);
1735 for (num = first; num <= last; num++)
1736 bit_set(&fc.messages, num);
1739 for (current = first; current <= last; current++) {
1742 ret = current - first + 1;
1743 mutt_message ("%s %d/%d", msg, ret, num);
1745 if (!bit_isset(&fc.messages, current))
1748 if (ctx->msgcount >= ctx->hdrmax)
1749 mx_alloc_memory (ctx);
1750 h = ctx->hdrs[ctx->msgcount] = header_new();
1751 h->index = ctx->msgcount;
1753 ret = nntp_read_header (ctx, NULL, current);
1754 if (ret == 0) { /* Got article. Fetch next header */
1755 nntp_get_status (ctx, h, NULL, h->article_num);
1757 mx_update_context (ctx, 1);
1760 header_delete(&h); /* skip it */
1762 bits_wipe(&fc.messages);
1766 if (current > nntp_data->lastLoaded)
1767 nntp_data->lastLoaded = current;
1769 bits_wipe(&fc.messages);
1772 nntp_data->lastLoaded = last;
1773 mutt_clear_error ();
1778 * currently, nntp "mailbox" is "newsgroup"
1780 static int nntp_open_mailbox (CONTEXT * ctx)
1782 nntp_data_t *nntp_data;
1783 nntp_server_t *serv;
1784 char buf[HUGE_STRING];
1785 char server[LONG_STRING];
1786 int count = 0, first;
1791 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1792 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1798 nntp_expand_path (server, sizeof (server), &act);
1799 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1802 CurrentNewsSrv = serv;
1804 /* create NNTP-specific state struct if nof found in list */
1805 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1806 nntp_data = nntp_data_new();
1807 nntp_data->group = m_strdup(buf);
1808 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1809 serv->tail = nntp_data_list_append(serv->tail, nntp_data);
1811 ctx->data = nntp_data;
1812 nntp_data->nserv = serv;
1814 mutt_message (_("Selecting %s..."), nntp_data->group);
1816 if (!nntp_data->desc) {
1817 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1818 if (nntp_data->desc)
1819 nntp_save_cache_index (serv);
1823 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1827 if (m_strncmp("211", buf, 3)) {
1830 /* GROUP command failed */
1831 if (!m_strncmp("411", buf, 3)) {
1832 mutt_error (_("Newsgroup %s not found on server %s"),
1833 nntp_data->group, serv->conn->account.host);
1835 /* CACHE: delete cache and line from .index */
1836 nntp_delete_cache (nntp_data);
1837 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1838 for (l = &serv->list; *l; l = &(*l)->next) {
1839 if ((*l) == nntp_data) {
1840 nntp_data_list_pop(l);
1841 nntp_data_delete(&nntp_data);
1847 nntp_data_delete(&nntp_data);
1854 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1855 &nntp_data->lastMessage, buf);
1857 nntp_data->deleted = 0;
1859 time (&serv->check_time);
1862 * Check for max adding context. If it is greater than $nntp_context,
1863 * strip off extra articles
1865 first = nntp_data->firstMessage;
1866 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1867 first = nntp_data->lastMessage - NntpContext + 1;
1869 nntp_data->lastLoaded = first - 1;
1870 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1873 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1875 char buf[LONG_STRING];
1876 char path[_POSIX_PATH_MAX];
1881 /* see if we already have the message in our cache */
1883 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1886 /* if everything is fine, assign msg->fp and return */
1887 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1888 (msg->fp = fopen (cache->path, "r")))
1891 /* clear the previous entry */
1892 unlink (cache->path);
1893 p_delete(&cache->path);
1895 cache->index = ctx->hdrs[msgno]->index;
1896 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1900 cache->path = m_strdup(path);
1902 if (ctx->hdrs[msgno]->article_num == 0)
1903 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1904 ctx->hdrs[msgno]->env->message_id);
1906 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1907 ctx->hdrs[msgno]->article_num);
1909 bar.msg = _("Fetching message...");
1911 mutt_progress_bar (&bar, 0);
1913 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1914 msg->fp, ctx->tagged);
1916 mutt_error (_("Article %d not found on server"),
1917 ctx->hdrs[msgno]->article_num);
1923 p_delete(&cache->path);
1927 envelope_delete(&ctx->hdrs[msgno]->env);
1928 ctx->hdrs[msgno]->env =
1929 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1930 /* fix content length */
1931 fseeko (msg->fp, 0, SEEK_END);
1932 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1933 ctx->hdrs[msgno]->content->offset;
1935 /* this is called in mutt before the open which fetches the message,
1936 * which is probably wrong, but we just call it again here to handle
1937 * the problem instead of fixing it.
1939 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1941 /* These would normally be updated in mx_update_context(), but the
1942 * full headers aren't parsed with XOVER, so the information wasn't
1945 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1947 mutt_clear_error ();
1954 int nntp_post (const char *msg)
1956 char buf[LONG_STRING];
1959 nntp_data_t *nntp_data;
1961 if (Context && Context->magic == M_NNTP)
1962 nntp_data = (nntp_data_t *)Context->data;
1964 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
1965 !CurrentNewsSrv->list)
1967 mutt_error (_("Can't post article. No connection to news server."));
1970 nntp_data = CurrentNewsSrv->list;
1973 if (!(f = safe_fopen (msg, "r"))) {
1974 mutt_error (_("Can't post article. Unable to open %s"), msg);
1978 m_strcpy(buf, sizeof(buf), "POST\r\n");
1979 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1980 mutt_error (_("Can't post article. Connection to %s lost."),
1981 nntp_data->nserv->conn->account.host);
1984 if (buf[0] != '3') {
1985 mutt_error (_("Can't post article: %s"), buf);
1991 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1992 len = m_strlen(buf);
1993 if (buf[len - 1] == '\n') {
1994 buf[len - 1] = '\r';
2000 mutt_socket_write(nntp_data->nserv->conn, buf);
2002 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
2006 if (buf[m_strlen(buf) - 1] != '\n')
2007 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
2008 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
2009 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
2010 mutt_error (_("Can't post article. Connection to %s lost."),
2011 nntp_data->nserv->conn->account.host);
2014 if (buf[0] != '2') {
2015 mutt_error (_("Can't post article: %s"), buf);
2022 /* nntp_logout_all: close all open connections. */
2023 void nntp_logout_all (void)
2025 char buf[LONG_STRING];
2028 conn = mutt_socket_head ();
2031 CONNECTION* next = conn->next;
2032 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2033 mutt_message (_("Closing connection to %s..."), conn->account.host);
2034 mutt_socket_write (conn, "QUIT\r\n");
2035 mutt_socket_readln (buf, sizeof (buf), conn);
2036 mutt_clear_error ();
2037 mutt_socket_close (conn);
2038 mutt_socket_free (conn);
2044 static void nntp_free_acache (nntp_data_t * data)
2048 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2049 if (data->acache[i].path) {
2050 unlink (data->acache[i].path);
2051 p_delete(&data->acache[i].path);
2056 void nntp_data_wipe(nntp_data_t *data)
2058 p_delete(&data->entries);
2059 p_delete(&data->desc);
2060 p_delete(&data->cache);
2061 p_delete(&data->group);
2062 nntp_free_acache(data);
2065 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2067 nntp_data_t *data = ctx->data;
2069 nntp_save_cache_group(ctx);
2070 nntp_free_acache(data);
2072 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2076 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2078 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2082 nntp_free_acache (data);
2083 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2085 nntp_save_cache_index (data->nserv);
2086 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2088 nntp_data_delete(&data);
2090 nntp_sync_sidebar (data);
2093 /* commit changes and terminate connection */
2094 int nntp_close_mailbox (CONTEXT * ctx)
2098 mutt_message _("Quitting newsgroup...");
2101 nntp_data_t *data = (nntp_data_t *) ctx->data;
2104 if (data->nserv && data->nserv->conn && ctx->unread) {
2105 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2107 mutt_newsgroup_catchup (data->nserv, data->group);
2112 nntp_sync_mailbox (ctx, 0, NULL);
2113 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2114 nntp_server_t *news;
2116 news = ((nntp_data_t *) ctx->data)->nserv;
2117 newsrc_gen_entries (ctx);
2118 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2119 mutt_newsrc_update (news);
2121 mutt_clear_error ();
2125 /* use the GROUP command to poll for new mail */
2126 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2128 char buf[LONG_STRING];
2131 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2135 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2138 if (m_strncmp("211", buf, 3)) {
2140 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2144 if (!m_strncmp("211", buf, 3)) {
2148 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2149 nntp_data->firstMessage = first;
2150 nntp_data->lastMessage = last;
2151 if (ctx && last > nntp_data->lastLoaded) {
2152 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2153 time (&nntp_data->nserv->check_time);
2156 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2157 nntp_data->unread = count;
2159 mutt_newsgroup_stat (nntp_data);
2160 /* active was renumbered? */
2161 if (last < nntp_data->lastLoaded) {
2162 if (!nntp_data->max) {
2163 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2166 nntp_data->lastCached = 0;
2168 nntp_data->entries[0].first = 1;
2169 nntp_data->entries[0].last = 0;
2171 nntp_sync_sidebar (nntp_data);
2174 time (&nntp_data->nserv->check_time);
2178 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2180 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2183 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2185 char buf[LONG_STRING];
2186 nntp_data_t nntp_data;
2187 nntp_data_t *l, **emp;
2191 if (!serv || !serv->newgroups_time)
2194 if (nntp_open_connection (serv) < 0)
2197 /* check subscribed groups for new news */
2198 if (option (OPTSHOWNEWNEWS)) {
2199 mutt_message _("Checking for new messages...");
2201 for (l = serv->list; l; l = l->next) {
2202 serv->check_time = 0; /* really check! */
2204 _nntp_check_mailbox (NULL, l);
2211 mutt_message _("Checking for new newsgroups...");
2213 now = serv->newgroups_time;
2214 time (&serv->newgroups_time);
2216 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2217 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2218 t->tm_min, t->tm_sec);
2219 nntp_data.nserv = serv;
2220 if (Context && Context->magic == M_NNTP)
2221 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2223 nntp_data.group = NULL;
2225 emp = nntp_data_list_last(&serv->list);
2226 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2227 add_group, serv, 0) != 0) {
2231 mutt_message _("Loading descriptions...");
2233 for (l = *emp; l; l = l->next) {
2235 nntp_get_desc(l, l->group, NULL, NULL);
2238 nntp_save_cache_index(serv);
2240 return nntp.checked;
2243 /* Load list of all newsgroups from active */
2244 int nntp_get_active (nntp_server_t * serv)
2247 nntp_data_t nntp_data, **tmp = &serv->list;
2249 if (nntp_open_connection (serv) < 0)
2252 snprintf (msg, sizeof (msg),
2253 _("Loading list of all newsgroups on server %s..."),
2254 serv->conn->account.host);
2256 time (&serv->newgroups_time);
2257 nntp_data.nserv = serv;
2258 nntp_data.group = NULL;
2260 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2264 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2266 nntp_get_desc (&nntp_data, "*", msg, NULL);
2269 if ((*tmp)->deleted && !(*tmp)->rc) {
2270 nntp_data_t *d = nntp_data_list_pop(tmp);
2271 nntp_delete_cache(d);
2272 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2273 nntp_data_delete(&d);
2275 tmp = &(*tmp)->next;
2279 nntp_save_cache_index(serv);
2281 mutt_clear_error ();
2282 return nntp.checked;
2286 * returns -1 if error ocurred while retrieving header,
2287 * number of articles which ones exist in context on success.
2289 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2293 /* if msgid is already in context, don't reload them */
2294 if (hash_find (ctx->id_hash, msgid))
2296 if (ctx->msgcount == ctx->hdrmax)
2297 mx_alloc_memory (ctx);
2298 ctx->hdrs[ctx->msgcount] = header_new();
2299 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2301 mutt_message (_("Fetching %s from server..."), msgid);
2302 ret = nntp_read_header (ctx, msgid, 0);
2303 /* since nntp_read_header() may set read flag, we must reset it */
2304 ctx->hdrs[ctx->msgcount]->read = 0;
2306 header_delete(&ctx->hdrs[ctx->msgcount]);
2309 mx_update_context (ctx, 1);
2322 static int check_children (char *s, void *c)
2324 CHILD_CONTEXT *cc = c;
2327 if (!s || (n = atoi (s)) == 0)
2329 for (i = 0; i < cc->ctx->msgcount; i++)
2330 if (cc->ctx->hdrs[i]->article_num == n)
2332 if (cc->num >= cc->max)
2333 p_realloc(&cc->child, cc->max += 25);
2334 cc->child[cc->num++] = n;
2339 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2341 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2343 int i, ret = 0, tmp = 0;
2344 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2346 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2347 !nntp_data->nserv->conn->account.host)
2349 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2351 if (!nntp_data->nserv->hasXPAT) {
2352 mutt_error (_("Server %s does not support this operation!"),
2353 nntp_data->nserv->conn->account.host);
2357 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2358 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2360 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2361 p_delete(&cc.child);
2365 /* dont try to read the xover cache. check_children() already
2366 * made sure that we dont have the article, so we need to visit
2367 * the server. Reading the cache at this point is also bad
2368 * because it would duplicate messages */
2369 if (nntp.use_cache) {
2371 nntp.use_cache = false;
2373 for (i = 0; i < cc.num; i++) {
2374 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2376 if (ctx->msgcount &&
2377 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2378 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2381 nntp.use_cache = true;
2382 p_delete(&cc.child);
2386 static int nntp_is_magic (const char* path, struct stat* st) {
2387 url_scheme_t s = url_check_scheme(NONULL(path));
2388 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2391 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2393 case ACL_INSERT: /* editing messages */
2394 case ACL_WRITE: /* change importance */
2396 case ACL_DELETE: /* (un)deletion */
2397 case ACL_SEEN: /* mark as read */
2404 mx_t const nntp_mx = {
2414 nntp_fastclose_mailbox,