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,
232 act->type = M_ACCT_TYPE_NNTP;
234 m_strcpy(s, sizeof(s), server);
235 url_parse_ciss(&url, s);
237 if (url.scheme != U_NNTP && url.scheme != U_NNTPS)
240 act->has_ssl = (url.scheme == U_NNTPS);
242 act->port = act->has_ssl ? NNTP_SSL_PORT : NNTP_PORT;
244 m_strcpy(group, group_len, url.path);
246 return mutt_account_fromurl(act, &url);
249 void nntp_expand_path (char *line, ssize_t len, ACCOUNT * act)
253 url.path = m_strdup(line);
254 mutt_account_tourl (act, &url);
255 url_ciss_tostring (&url, line, len, 0);
259 static int add_group (char *buf, void *serv)
261 nntp_server_t *s = serv;
262 char group[LONG_STRING], mod, desc[HUGE_STRING];
264 nntp_data_t *nntp_data;
267 nntp.checked = n; /* nntp.checked have N, where N = number of groups */
268 if (!buf) /* at EOF must be zerouth */
275 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
278 if ((nntp_data = hash_find(s->newsgroups, group)) == NULL) {
280 nntp_data = nntp_data_new();
281 nntp_data->group = m_strdup(group);
282 nntp_data->nserv = s;
283 hash_insert(s->newsgroups, nntp_data->group, nntp_data);
284 s->tail = nntp_data_list_append(s->tail, nntp_data);
286 nntp_data->deleted = 0;
287 nntp_data->firstMessage = first;
288 nntp_data->lastMessage = last;
290 nntp_data->allowed = 1;
292 nntp_data->allowed = 0;
294 p_delete(&nntp_data->desc);
296 nntp_data->desc = m_strdup(desc);
297 if (nntp_data->rc || nntp_data->lastCached)
298 mutt_newsgroup_stat (nntp_data);
299 else if (nntp_data->lastMessage &&
300 nntp_data->firstMessage <= nntp_data->lastMessage)
301 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
303 nntp_data->unread = 0;
308 /* Load list of all newsgroups from cache ALL */
309 static int nntp_get_cache_all (nntp_server_t * serv)
311 char buf[HUGE_STRING];
314 nntp_cache_expand(buf, ssizeof(buf), "%s", serv->cache);
315 if ((f = safe_fopen (buf, "r"))) {
318 while (fgets (buf, sizeof (buf), f) != NULL) {
319 if (ReadInc && (i % ReadInc == 0))
320 mutt_message (_("Loading list from cache... %d"), i);
321 add_group (buf, serv);
324 add_group (NULL, NULL);
330 p_delete(&serv->cache);
336 * Automatically loads a newsrc into memory, if necessary.
337 * Checks the size/mtime of a newsrc file, if it doesn't match, load
338 * again. Hmm, if a system has broken mtimes, this might mean the file
339 * is reloaded every time, which we'd have to fix.
341 * a newsrc file is a line per newsgroup, with the newsgroup, then a
342 * ':' denoting subscribed or '!' denoting unsubscribed, then a
343 * comma separated list of article numbers and ranges.
345 nntp_server_t *mutt_select_newsserver (char *server)
347 char file[_POSIX_PATH_MAX];
356 if (!server || !*server) {
357 mutt_error _("No newsserver defined!");
362 buf = p = p_new(char, m_strlen(server) + 10);
363 if (url_check_scheme (server) == U_UNKNOWN) {
364 strcpy (buf, "nntp://");
365 p = strchr (buf, '\0');
369 if ((nntp_parse_url (buf, &act, file, sizeof (file))) < 0 || *file) {
371 mutt_error (_("%s is an invalid newsserver specification!"), server);
376 conn = mutt_conn_find (NULL, &act);
380 nntp_cache_expand(file, sizeof(file), "%s.newsrc", conn->account.host);
381 serv = (nntp_server_t *) conn->data;
385 /* externally modified? */
386 if (serv->stat != stat(file, &sb) || (!serv->stat &&
387 (serv->size != sb.st_size
388 || serv->mtime != sb.st_mtime)))
390 for (list = serv->list; list; list = list->next) {
391 list->subscribed = list->rc = list->num = 0;
396 if (serv->status == NNTP_BYE)
397 serv->status = NNTP_NONE;
398 nntp_check_newgroups (serv, 0);
403 serv = p_new(nntp_server_t, 1);
404 serv->tail = &serv->list;
406 serv->newsrc = m_strdup(file);
407 serv->newsgroups = hash_new(SHRT_MAX, false);
408 slurp_newsrc (serv); /* load .newsrc */
409 nntp_parse_cacheindex (serv); /* load .index */
410 if (nntp.use_cache && serv->cache && nntp_get_cache_all (serv) >= 0)
411 nntp_check_newgroups (serv, 1);
412 else if (nntp_get_active (serv) < 0) {
413 hash_delete(&serv->newsgroups, NULL);
414 nntp_data_list_wipe(&serv->list);
415 p_delete(&serv->newsrc);
416 p_delete(&serv->cache);
420 conn->data = (void *) serv;
426 * full status flags are not supported by nntp, but we can fake some
427 * of them. This is how:
428 * Read = a read message number is in the .newsrc
429 * New = a message is new since we last read this newsgroup
430 * Old = anything else
431 * So, Read is marked as such in the newsrc, old is anything that is
432 * "skipped" in the newsrc, and new is anything not in the newsrc nor
433 * in the cache. By skipped, I mean before the last unread message
436 nntp_get_status(CONTEXT *ctx, HEADER *h, const char *group, int article)
438 nntp_data_t *data = ctx->data;
441 data = hash_find(data->nserv->newsgroups, group);
445 for (int i = 0; i < data->num; i++) {
446 if ((article >= data->entries[i].first) &&
447 (article <= data->entries[i].last))
449 /* we cannot use mutt_set_flag() because mx_update_context()
456 /* If article was not cached yet, it is new! :) */
457 if (!data->cache || article > data->lastCached)
460 /* Old articles are articles which aren't read but an article after them
462 if (option(OPTMARKOLD))
466 static void newsrc_gen_entries (CONTEXT * ctx)
468 nntp_data_t *data = (nntp_data_t *) ctx->data;
470 int last = 0, first = 1;
471 int save_sort = SORT_ORDER;
473 if (Sort != SORT_ORDER) {
476 mutt_sort_headers (ctx, 0);
480 data->entries = p_new(NEWSRC_ENTRY, 5);
485 * Set up to fake initial sequence from 1 to the article before the
486 * first article in our list
491 for (x = 0; x < ctx->msgcount; x++) {
492 if (series) { /* search for first unread */
494 * We don't actually check sequential order, since we mark
495 * "missing" entries as read/deleted
497 last = ctx->hdrs[x]->article_num;
498 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
499 !ctx->hdrs[x]->read) {
500 if (data->num >= data->max) {
501 data->max = data->max * 2;
502 p_realloc(&data->entries, data->max);
504 data->entries[data->num].first = first;
505 data->entries[data->num].last = last - 1;
510 else { /* search for first read */
512 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
516 last = ctx->hdrs[x]->article_num;
519 if (series && first <= data->lastLoaded) {
520 if (data->num >= data->max) {
521 data->max = data->max * 2;
522 p_realloc(&data->entries, data->max);
524 data->entries[data->num].first = first;
525 data->entries[data->num].last = data->lastLoaded;
529 if (save_sort != Sort) {
531 mutt_sort_headers (ctx, 0);
535 static int mutt_update_list_file (char *filename, char *section,
536 const char *key, const char *line)
540 char buf[HUGE_STRING];
541 char tmpf[_POSIX_PATH_MAX], lnk[_POSIX_PATH_MAX];
543 int ext = 0, done = 0, r = 0, l = 0;
545 /* if file not exist, create it */
546 if ((ifp = safe_fopen (filename, "a")))
548 if (!(ifp = safe_fopen (filename, "r"))) {
549 mutt_error (_("Unable to open %s for reading"), filename);
552 if (mx_lock_file (filename, fileno (ifp), 0, 0, 1)) {
554 mutt_error (_("Unable to lock %s"), filename);
558 /* use m_tempfile() to get a tempfile in the same
559 * directory as filename is so that we can follow symlinks
560 * via rename(2); as dirname(2) may modify its argument,
561 * temporarily use buf as copy of it
563 m_strcpy(buf, sizeof(buf), filename);
564 ofp = m_tempfile(tmpf, sizeof(tmpf), dirname(buf), filename);
567 mutt_error (_("Unable to open %s for writing"), tmpf);
572 while (r != EOF && !done && fgets (buf, sizeof (buf), ifp)) {
573 r = fputs (buf, ofp);
575 while (*c && *c != '\n') c++;
576 c[0] = 0; /* strip EOL */
577 if (!strncmp (buf, "#: ", 3) && !m_strcasecmp(buf+3, section))
580 if (r != EOF && !done) {
581 snprintf (buf, sizeof(buf), "#: %s\n", section);
582 r = fputs (buf, ofp);
587 while (r != EOF && fgets (buf, sizeof (buf), ifp)) {
590 while (*c && (*c != '\r') && (*c != '\n')) c++;
592 if (*c != '\\') ext = 0;
593 } else if ((section && !strncmp (buf, "#: ", 3))) {
598 r = fputs (buf, ofp);
601 } else if (key && !strncmp (buf, key, strlen(key)) &&
602 (!*key || buf[strlen(key)] == ' ')) {
605 while (*c && (*c != '\r') && (*c != '\n')) c++;
607 if (*c == '\\') ext = 1;
609 r = fputs (line, ofp);
611 r = fputc ('\n', ofp);
615 r = fputs (buf, ofp);
619 while (r != EOF && fgets (buf, sizeof (buf), ifp))
620 r = fputs (buf, ofp);
622 /* If there wasn't a line to replace, put it on the end of the file */
623 if (r != EOF && !done && line) {
625 r = fputc ('\n', ofp);
627 mx_unlock_file (filename, fileno (ifp), 0);
632 mutt_error (_("Can't write %s"), tmpf);
636 if ((l = readlink (filename, lnk, sizeof(lnk)-1)) > 0)
638 if (rename (tmpf, l > 0 ? lnk : filename) < 0) {
640 mutt_error (_("Can't rename %s to %s"), tmpf, l > 0 ? lnk : filename);
646 int mutt_newsrc_update (nntp_server_t * news)
657 /* we will generate full newsrc here */
658 for (data = news->list; data; data = data->next) {
659 if (!data || !data->rc)
662 buffer_addstr(&buf, data->group);
663 buffer_addch(&buf, data->subscribed ? ':' : '!');
665 for (int x = 0; x < data->num; x++) {
666 buffer_addch(&buf, x ? ',' : ' ');
667 buffer_addf(&buf, "%d-%d", data->entries[x].first,
668 data->entries[x].last);
670 buffer_addch(&buf, '\n');
673 /* newrc being fully rewritten */
675 && (r = mutt_update_list_file(news->newsrc, NULL, "", buf.data)) == 0)
679 stat (news->newsrc, &st);
680 news->size = st.st_size;
681 news->mtime = st.st_mtime;
688 static FILE *mutt_mkname (char *s)
690 char buf[_POSIX_PATH_MAX], *pc;
694 nntp_cache_expand(buf, ssizeof(buf), "%s", s);
695 if ((fp = safe_fopen (buf, "w")))
698 nntp_cache_expand(buf, ssizeof(buf), "cache-XXXXXX");
699 pc = buf + m_strlen(buf) - 12; /* positioning to "cache-XXXXXX" */
700 if ((fd = mkstemp (buf)) == -1)
702 strcpy (s, pc); /* generated name */
703 return fdopen (fd, "w");
706 /* Updates info into .index file: ALL or about selected newsgroup */
707 static int nntp_update_cacheindex (nntp_server_t * serv, nntp_data_t * data)
709 char buf[LONG_STRING];
710 char file[_POSIX_PATH_MAX];
711 const char *key = "ALL";
713 if (!serv || !serv->conn || !serv->conn->account.host)
716 if (data && data->group) {
718 snprintf(buf, sizeof(buf), "%s %s %d %d", key, data->cache,
719 data->firstMessage, data->lastLoaded);
721 m_strcpy(file, sizeof(file), serv->cache);
722 snprintf(buf, sizeof (buf), "ALL %s 0 %d", file,
723 (int)serv->newgroups_time);
725 nntp_cache_expand(file, ssizeof(file), "%s.index", serv->conn->account.host);
726 return mutt_update_list_file(file, serv->conn->account.host, key, buf);
729 static void nntp_delete_cache (nntp_data_t * data)
731 char buf[_POSIX_PATH_MAX];
733 if (!nntp.use_cache || !data || !data->cache || !data->nserv)
736 nntp_cache_expand(buf, ssizeof(buf), "%s", data->cache);
738 p_delete(&data->cache);
739 data->lastCached = 0;
740 nntp_cache_expand(buf, ssizeof(buf), "%s.index",
741 data->nserv->conn->account.host);
742 mutt_update_list_file(buf, data->nserv->conn->account.host, data->group,
746 static int nntp_save_cache_index (nntp_server_t * news)
748 char buf[HUGE_STRING];
749 char file[_POSIX_PATH_MAX];
753 if (!news || !news->newsgroups)
759 nntp_cache_expand(file, ssizeof(file), "%s", news->cache);
761 f = safe_fopen(file, "w");
763 m_strcpy(buf, ssizeof(buf), news->conn->account.host);
764 f = mutt_mkname(buf);
765 news->cache = m_strdup(buf);
766 nntp_cache_expand(file, ssizeof(file), "%s", buf);
771 for (d = news->list; d; d = d->next) {
774 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
775 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
778 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
779 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
780 if (fputs (buf, f) == EOF) {
789 if (nntp_update_cacheindex (news, NULL)) {
796 static int nntp_save_cache_group (CONTEXT * ctx)
798 char buf[HUGE_STRING], addr[STRING];
799 char file[_POSIX_PATH_MAX];
803 int i = 0, save = SORT_ORDER;
808 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
811 if (((nntp_data_t *) ctx->data)->cache) {
812 nntp_cache_expand(file, ssizeof(file), "%s",
813 ((nntp_data_t *)ctx->data)->cache);
815 f = safe_fopen (file, "w");
818 snprintf(buf, ssizeof(buf), "%s-%s",
819 ((nntp_data_t *)ctx->data)->nserv->conn->account.host,
820 ((nntp_data_t *)ctx->data)->group);
821 f = mutt_mkname (buf);
822 ((nntp_data_t *)ctx->data)->cache = m_strdup(buf);
823 nntp_cache_expand(file, ssizeof(file), "%s", buf);
828 if (Sort != SORT_ORDER) {
831 mutt_sort_headers (ctx, 0);
834 /* Save only $nntp_context messages... */
835 ((nntp_data_t *) ctx->data)->lastCached = 0;
836 if (NntpContext && ctx->msgcount > NntpContext)
837 i = ctx->msgcount - NntpContext;
838 for (; i < ctx->msgcount; i++) {
839 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
842 rfc822_addrcat(addr, sizeof(addr), h->env->from, 0);
843 tm = gmtime (&h->date_sent);
844 snprintf (buf, sizeof (buf),
845 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
846 h->article_num, h->env->subject, addr, tm->tm_mday,
847 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
848 tm->tm_min, tm->tm_sec, h->env->message_id);
850 if (h->env->references)
851 mutt_write_references (h->env->references, f);
852 snprintf (buf, sizeof (buf), "\t%zd\t%d\tXref: %s\n",
853 h->content->length, h->lines, NONULL (h->env->xref));
854 if (fputs (buf, f) == EOF) {
860 prev = ctx->hdrs[i]->article_num;
865 mutt_sort_headers (ctx, 0);
869 if (nntp_update_cacheindex (((nntp_data_t *) ctx->data)->nserv,
870 (nntp_data_t *) ctx->data)) {
874 ((nntp_data_t *) ctx->data)->lastCached =
875 ((nntp_data_t *) ctx->data)->lastLoaded;
879 nntp_data_t *mutt_newsgroup_subscribe (nntp_server_t * news, char *group)
883 if (!news || !news->newsgroups || !group || !*group)
885 if (!(data = (nntp_data_t *) hash_find (news->newsgroups, group))) {
886 data = nntp_data_new();
887 data->group = m_strdup(group);
890 hash_insert (news->newsgroups, data->group, data);
891 news->tail = nntp_data_list_append(news->tail, data);
893 if (!data->subscribed) {
894 data->subscribed = 1;
900 nntp_data_t *mutt_newsgroup_unsubscribe (nntp_server_t * news, char *group)
904 if (!news || !news->newsgroups || !group || !*group ||
905 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
907 if (data->subscribed) {
908 data->subscribed = 0;
913 nntp_data_t *mutt_newsgroup_catchup (nntp_server_t * news, char *group)
917 if (!news || !news->newsgroups || !group || !*group ||
918 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
921 data->entries = p_new(NEWSRC_ENTRY, 5);
925 data->entries[0].first = 1;
927 data->entries[0].last = data->lastMessage;
928 if (Context && Context->data == data) {
931 for (x = 0; x < Context->msgcount; x++)
932 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
937 nntp_data_t *mutt_newsgroup_uncatchup (nntp_server_t * news, char *group)
941 if (!news || !news->newsgroups || !group || !*group ||
942 !(data = (nntp_data_t *) hash_find (news->newsgroups, group)))
945 data->entries = p_new(NEWSRC_ENTRY, 5);
949 data->entries[0].first = 1;
950 data->entries[0].last = data->firstMessage - 1;
951 if (Context && Context->data == data) {
954 data->unread = Context->msgcount;
955 for (x = 0; x < Context->msgcount; x++)
956 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
959 data->unread = data->lastMessage - data->entries[0].last;
963 /* this routine gives the first newsgroup with new messages */
964 void nntp_buffy (char* dst, ssize_t dstlen) {
968 /* forward to current group */
969 for (list = CurrentNewsSrv->list; list; list = list->next) {
970 if (list->subscribed && list->unread
971 && Context && Context->magic == M_NNTP
972 && m_strcmp(list->group, ((nntp_data_t *)Context->data)->group) == 0)
984 list = CurrentNewsSrv->list;
986 for (; list; list = list->next) {
987 if (list->subscribed && list->unread) {
988 if (Context && Context->magic == M_NNTP &&
989 !m_strcmp(list->group, ((nntp_data_t *)Context->data)->group)) {
992 for (i = 0; i < Context->msgcount; i++)
993 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
998 m_strcpy(dst, dstlen, list->group);
1011 /* nntp protocol {{{ */
1013 void nntp_sync_sidebar (nntp_data_t* data) {
1021 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
1022 data->nserv->conn->account.has_ssl ? "s" : "",
1023 NONULL(data->nserv->conn->account.user),
1024 *data->nserv->conn->account.pass ? ":" : "",
1025 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
1026 data->nserv->conn->account.host,
1029 /* bail out if group not found via mailboxes */
1030 if ((i = buffy_lookup (buf)) < 0)
1033 tmp = Incoming.arr[i];
1034 /* copied from browser.c */
1035 if (option (OPTMARKOLD) &&
1036 data->lastCached >= data->firstMessage &&
1037 data->lastCached <= data->lastMessage)
1038 tmp->msg_unread = data->lastMessage - data->lastCached;
1040 tmp->msg_unread = data->unread;
1041 tmp->new = data->unread > 0;
1042 /* this is closest to a "total" count we can get */
1043 tmp->msgcount = data->lastMessage - data->firstMessage;
1046 static int nntp_auth (nntp_server_t * serv)
1048 CONNECTION *conn = serv->conn;
1050 int flags = conn->account.flags;
1052 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
1053 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
1054 conn->account.flags = flags;
1058 mutt_message _("Logging in...");
1060 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
1061 mutt_socket_write (conn, buf);
1062 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1063 conn->account.flags = flags;
1067 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
1068 mutt_socket_write(conn, buf);
1069 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
1070 conn->account.flags = flags;
1074 if (m_strncmp("281", buf, 3)) {
1075 conn->account.flags = flags;
1076 mutt_error _("Login failed.");
1085 static int nntp_connect_error (nntp_server_t * serv)
1087 serv->status = NNTP_NONE;
1088 mutt_socket_close (serv->conn);
1089 mutt_error _("Server closed connection!");
1095 static int nntp_connect_and_auth (nntp_server_t * serv)
1097 CONNECTION *conn = serv->conn;
1101 serv->status = NNTP_NONE;
1103 if (mutt_socket_open (conn) < 0)
1106 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1107 return nntp_connect_error (serv);
1109 if (!m_strncmp("200", buf, 3))
1110 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
1111 else if (!m_strncmp("201", buf, 3))
1112 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
1114 mutt_socket_close(conn);
1116 mutt_error("%s", buf);
1121 /* Tell INN to switch to mode reader if it isn't so. Ignore all
1122 returned codes and messages. */
1123 mutt_socket_write (conn, "MODE READER\r\n");
1124 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1125 return nntp_connect_error (serv);
1127 mutt_socket_write (conn, "STAT\r\n");
1128 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1129 return nntp_connect_error (serv);
1131 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
1132 serv->status = NNTP_OK;
1136 rc = nntp_auth (serv);
1138 return nntp_connect_error (serv);
1140 mutt_socket_close (conn);
1141 serv->status = NNTP_BYE;
1145 mutt_socket_close (conn);
1146 mutt_error _("Login failed.");
1151 serv->status = NNTP_OK;
1155 static int nntp_attempt_features (nntp_server_t * serv)
1157 char buf[LONG_STRING];
1158 CONNECTION *conn = serv->conn;
1160 if (serv->feat_known)
1163 mutt_socket_write (conn, "XOVER\r\n");
1164 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1165 return nntp_connect_error (serv);
1166 if (m_strncmp("500", buf, 3))
1169 mutt_socket_write (conn, "XPAT\r\n");
1170 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1171 return nntp_connect_error (serv);
1172 if (m_strncmp("500", buf, 3))
1175 mutt_socket_write (conn, "LISTGROUP\r\n");
1176 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1177 return (nntp_connect_error (serv));
1178 if (m_strncmp("500", buf, 3))
1179 serv->hasLISTGROUP = 1;
1181 mutt_socket_write (conn, "XGTITLE +\r\n");
1182 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1183 return nntp_connect_error (serv);
1184 if (m_strncmp("500", buf, 3))
1185 serv->hasXGTITLE = 1;
1187 if (!m_strncmp("282", buf, 3)) {
1189 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
1190 return nntp_connect_error (serv);
1191 } while (!(buf[0] == '.' && buf[1] == '\0'));
1194 serv->feat_known = 1;
1198 static int nntp_open_connection (nntp_server_t * serv)
1200 if (serv->status == NNTP_OK)
1202 if (serv->status == NNTP_BYE)
1204 if (nntp_connect_and_auth (serv) < 0)
1206 if (nntp_attempt_features (serv) < 0)
1211 static int nntp_reconnect (nntp_server_t * serv)
1215 mutt_socket_close (serv->conn);
1218 if (nntp_connect_and_auth (serv) == 0)
1221 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
1222 serv->conn->account.host);
1223 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
1224 serv->status = NNTP_BYE;
1230 /* Send data from line[LONG_STRING] and receive answer to same line */
1231 static int mutt_nntp_query (nntp_data_t * data, char *line, size_t linelen)
1233 char buf[LONG_STRING];
1236 if (data->nserv->status == NNTP_BYE)
1241 mutt_socket_write (data->nserv->conn, line);
1243 else if (data->group) {
1244 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1245 mutt_socket_write (data->nserv->conn, buf);
1249 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
1250 if (nntp_reconnect (data->nserv) < 0)
1254 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
1255 mutt_socket_write (data->nserv->conn, buf);
1256 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
1262 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
1266 m_strcpy(line, linelen, buf);
1271 * This function calls funct(*line, *data) for each received line,
1272 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
1275 * 1 - correct but not performed (may be, have to be continued),
1276 * -1 - conection lost,
1277 * -2 - invalid command or execution error,
1278 * -3 - error in funct(*line, *data).
1280 static int mutt_nntp_fetch (nntp_data_t * nntp_data, const char *query,
1281 const char *msg, progress_t* bar,
1282 int (*funct) (char *, void *),
1283 void *data, int tagged)
1285 char buf[LONG_STRING];
1294 m_strcpy(buf, sizeof(buf), query);
1295 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1304 inbuf = p_new(char, sizeof(buf));
1307 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
1312 if (!lenbuf && buf[0] == '.') {
1313 if (buf[1] == '\0') {
1321 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
1324 if (chunk >= ssizeof (buf)) {
1325 lenbuf += m_strlen(p);
1329 mutt_progress_bar (bar, pos);
1332 if (ReadInc && (line % ReadInc == 0)) {
1334 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
1336 mutt_message ("%s %d", msg, line);
1340 if (ret == 0 && funct (inbuf, data) < 0)
1345 p_realloc(&inbuf, lenbuf + sizeof (buf));
1354 static int nntp_read_tempfile (char *line, void *file)
1356 FILE *f = (FILE *) file;
1362 if (fputc ('\n', f) == EOF)
1368 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
1371 register char *p, *b;
1372 register char *colon = NULL;
1376 /* skip to next word */
1378 while (*b && ((*b == ' ') || (*b == '\t')))
1382 /* skip to end of word */
1383 while (*p && (*p != ' ') && (*p != '\t')) {
1395 nntp_get_status (ctx, h, b, atoi (colon));
1396 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
1397 h->article_num = atoi (colon);
1405 * 1 if article not found
1406 * -1 if read or write error on tempfile or socket
1408 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
1411 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1413 char buf[LONG_STRING];
1414 char tempfile[_POSIX_PATH_MAX];
1416 HEADER *h = ctx->hdrs[ctx->msgcount];
1418 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
1423 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
1425 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
1427 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
1431 return (ret == -1 ? -1 : 1);
1434 h->article_num = article_num;
1435 h->env = mutt_read_rfc822_header (f, h, 0, 0);
1439 if (h->env->xref != NULL)
1440 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
1441 else if (h->article_num == 0 && msgid) {
1442 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
1443 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
1444 h->article_num = atoi (buf + 4);
1450 static int parse_description (char *line, void *n)
1452 register char *d = line;
1454 nntp_server_t *news = n;
1458 while (*d && *d != '\t' && *d != ' ')
1462 while (*d && (*d == '\t' || *d == ' '))
1464 if ((data = (nntp_data_t *) hash_find (news->newsgroups, line)) != NULL &&
1465 m_strcmp(d, data->desc)) {
1466 p_delete(&data->desc);
1467 data->desc = m_strdup(d);
1472 static void nntp_get_desc (nntp_data_t * data, const char *mask, char *msg, progress_t* bar)
1476 if (!option (OPTLOADDESC) || !data || !data->nserv)
1479 /* Get newsgroup description, if we can */
1480 if (data->nserv->hasXGTITLE)
1481 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
1483 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
1484 mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0);
1488 * XOVER returns a tab separated list of:
1489 * id|subject|from|date|Msgid|references|bytes|lines|xref
1491 * This has to duplicate some of the functionality of
1492 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
1493 * a limited number of headers which are "parsed" by placement in the list)
1495 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
1497 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
1501 hdr->env = envelope_new();
1502 hdr->env->newsgroups = m_strdup(nntp_data->group);
1503 hdr->content = body_new();
1504 hdr->content->type = TYPETEXT;
1505 hdr->content->subtype = m_strdup("plain");
1506 hdr->content->encoding = ENC7BIT;
1507 hdr->content->disposition = DISPINLINE;
1508 hdr->content->length = -1;
1511 for (x = 0; !done && x < 9; x++) {
1512 /* if from file, need to skip newline character */
1513 while (*p && *p != '\n' && *p != '\t')
1521 hdr->article_num = atoi (b);
1522 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
1525 hdr->env->subject = m_strdup(b);
1528 address_list_wipe(&hdr->env->from);
1529 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
1530 /* same as for mutt_parse_rfc822_line():
1531 * don't leave from info NULL if there's an invalid address (or
1532 * whatever) in From: field; mutt would just display it as empty
1533 * and mark mail/(esp.) news article as your own. aaargh! this
1534 * bothered me for _years_ */
1535 if (!hdr->env->from) {
1536 hdr->env->from = address_new ();
1537 hdr->env->from->personal = m_strdup(b);
1541 hdr->date_sent = mutt_parse_date (b, hdr);
1542 hdr->received = hdr->date_sent;
1545 p_delete(&hdr->env->message_id);
1546 hdr->env->message_id = m_strdup(b);
1549 string_list_wipe(&hdr->env->references);
1550 hdr->env->references = mutt_parse_references (b, 0);
1553 hdr->content->length = atoi (b);
1556 hdr->lines = atoi (b);
1560 p_delete(&hdr->env->xref);
1561 b = b + 6; /* skips the "Xref: " */
1562 hdr->env->xref = m_strdup(b);
1563 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
1565 rfc2047_decode_envelope(hdr->env);
1581 static int nntp_fetch_numbers (char *line, void *c)
1583 FETCH_CONTEXT *fc = c;
1586 int num = atoi(line);
1587 if (num < fc->first || num > fc->last)
1590 bit_set(&fc->messages, num);
1595 static int add_xover_line (char *line, void *c)
1598 FETCH_CONTEXT *fc = c;
1599 CONTEXT *ctx = fc->ctx;
1600 nntp_data_t *data = (nntp_data_t *) ctx->data;
1605 if (ctx->msgcount >= ctx->hdrmax)
1606 mx_alloc_memory (ctx);
1607 ctx->hdrs[ctx->msgcount] = header_new();
1608 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1610 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
1611 num = ctx->hdrs[ctx->msgcount]->article_num;
1613 if (num >= fc->first && num <= fc->last) {
1615 if (num > data->lastLoaded)
1616 data->lastLoaded = num;
1617 num = num - fc->first + 1;
1618 total = fc->last - fc->first + 1;
1619 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
1620 mutt_message ("%s %d/%d", fc->msg, num, total);
1622 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
1629 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
1631 char buf[HUGE_STRING];
1632 const char *msg = _("Fetching message headers...");
1633 const char *msg2 = _("Fetching headers from cache...");
1634 nntp_data_t *nntp_data = ((nntp_data_t *) ctx->data);
1635 int ret, num, oldmsgcount, current;
1639 /* if empty group or nothing to do */
1640 if (!last || first > last)
1643 /* fetch list of articles */
1644 mutt_message _("Fetching list of articles...");
1650 /* CACHE: must be loaded xover cache here */
1651 num = nntp_data->lastCached - first + 1;
1652 if (nntp.use_cache && nntp_data->cache && num > 0) {
1653 nntp_cache_expand(buf, ssizeof(buf), "%s", nntp_data->cache);
1654 mutt_message (msg2);
1656 if ((f = safe_fopen (buf, "r"))) {
1659 /* counting number of lines */
1660 while (fgets (buf, sizeof (buf), f) != NULL)
1663 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
1665 oldmsgcount = ctx->msgcount;
1667 fc.last = first + num - 1;
1669 while (fgets (buf, sizeof (buf), f) != NULL) {
1670 if (ReadInc && ((++c) % ReadInc == 0))
1671 mutt_message ("%s %d/%d", msg2, c, r);
1672 add_xover_line (buf, &fc);
1675 nntp_data->lastLoaded = fc.last;
1676 first = fc.last + 1;
1677 if (ctx->msgcount > oldmsgcount)
1678 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1681 nntp_delete_cache (nntp_data);
1683 num = last - first + 1;
1689 * Without XOVER, we have to fetch each article header and parse
1690 * it. With XOVER, we ask for all of them
1693 if (nntp_data->nserv->hasXOVER) {
1694 oldmsgcount = ctx->msgcount;
1698 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
1699 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
1700 if (ctx->msgcount > oldmsgcount)
1701 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
1703 mutt_error (_("XOVER command failed: %s"), buf);
1707 bits_init(&fc.messages);
1709 if (nntp_data->nserv->hasLISTGROUP) {
1710 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
1711 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
1712 nntp_fetch_numbers, &fc, 0))
1714 mutt_error (_("LISTGROUP command failed: %s"), buf);
1716 bits_wipe(&fc.messages);
1720 for (num = first; num <= last; num++)
1721 bit_set(&fc.messages, num);
1724 for (current = first; current <= last; current++) {
1727 ret = current - first + 1;
1728 mutt_message ("%s %d/%d", msg, ret, num);
1730 if (!bit_isset(&fc.messages, current))
1733 if (ctx->msgcount >= ctx->hdrmax)
1734 mx_alloc_memory (ctx);
1735 h = ctx->hdrs[ctx->msgcount] = header_new();
1736 h->index = ctx->msgcount;
1738 ret = nntp_read_header (ctx, NULL, current);
1739 if (ret == 0) { /* Got article. Fetch next header */
1740 nntp_get_status (ctx, h, NULL, h->article_num);
1742 mx_update_context (ctx, 1);
1745 header_delete(&h); /* skip it */
1747 bits_wipe(&fc.messages);
1751 if (current > nntp_data->lastLoaded)
1752 nntp_data->lastLoaded = current;
1754 bits_wipe(&fc.messages);
1757 nntp_data->lastLoaded = last;
1758 mutt_clear_error ();
1763 * currently, nntp "mailbox" is "newsgroup"
1765 static int nntp_open_mailbox (CONTEXT * ctx)
1767 nntp_data_t *nntp_data;
1768 nntp_server_t *serv;
1769 char buf[HUGE_STRING];
1770 char server[LONG_STRING];
1771 int count = 0, first;
1776 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
1777 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
1783 nntp_expand_path (server, sizeof (server), &act);
1784 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
1787 CurrentNewsSrv = serv;
1789 /* create NNTP-specific state struct if nof found in list */
1790 if ((nntp_data = hash_find(serv->newsgroups, buf)) == NULL) {
1791 nntp_data = nntp_data_new();
1792 nntp_data->group = m_strdup(buf);
1793 hash_insert(serv->newsgroups, nntp_data->group, nntp_data);
1794 serv->tail = nntp_data_list_append(serv->tail, nntp_data);
1796 ctx->data = nntp_data;
1797 nntp_data->nserv = serv;
1799 mutt_message (_("Selecting %s..."), nntp_data->group);
1801 if (!nntp_data->desc) {
1802 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
1803 if (nntp_data->desc)
1804 nntp_save_cache_index (serv);
1808 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1812 if (m_strncmp("211", buf, 3)) {
1815 /* GROUP command failed */
1816 if (!m_strncmp("411", buf, 3)) {
1817 mutt_error (_("Newsgroup %s not found on server %s"),
1818 nntp_data->group, serv->conn->account.host);
1820 /* CACHE: delete cache and line from .index */
1821 nntp_delete_cache (nntp_data);
1822 hash_remove(serv->newsgroups, nntp_data->group, NULL, NULL);
1823 for (l = &serv->list; *l; l = &(*l)->next) {
1824 if ((*l) == nntp_data) {
1825 nntp_data_list_pop(l);
1826 nntp_data_delete(&nntp_data);
1832 nntp_data_delete(&nntp_data);
1839 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
1840 &nntp_data->lastMessage, buf);
1842 nntp_data->deleted = 0;
1844 time (&serv->check_time);
1847 * Check for max adding context. If it is greater than $nntp_context,
1848 * strip off extra articles
1850 first = nntp_data->firstMessage;
1851 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
1852 first = nntp_data->lastMessage - NntpContext + 1;
1854 nntp_data->lastLoaded = first - 1;
1855 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
1858 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
1860 char buf[LONG_STRING];
1861 char path[_POSIX_PATH_MAX];
1866 /* see if we already have the message in our cache */
1868 &((nntp_data_t *) ctx->data)->acache[ctx->hdrs[msgno]->index %
1871 /* if everything is fine, assign msg->fp and return */
1872 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
1873 (msg->fp = fopen (cache->path, "r")))
1876 /* clear the previous entry */
1877 unlink (cache->path);
1878 p_delete(&cache->path);
1880 cache->index = ctx->hdrs[msgno]->index;
1881 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
1885 cache->path = m_strdup(path);
1887 if (ctx->hdrs[msgno]->article_num == 0)
1888 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
1889 ctx->hdrs[msgno]->env->message_id);
1891 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
1892 ctx->hdrs[msgno]->article_num);
1894 bar.msg = _("Fetching message...");
1896 mutt_progress_bar (&bar, 0);
1898 ret = mutt_nntp_fetch ((nntp_data_t *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
1899 msg->fp, ctx->tagged);
1901 mutt_error (_("Article %d not found on server"),
1902 ctx->hdrs[msgno]->article_num);
1908 p_delete(&cache->path);
1912 envelope_delete(&ctx->hdrs[msgno]->env);
1913 ctx->hdrs[msgno]->env =
1914 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
1915 /* fix content length */
1916 fseeko (msg->fp, 0, SEEK_END);
1917 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
1918 ctx->hdrs[msgno]->content->offset;
1920 /* this is called in mutt before the open which fetches the message,
1921 * which is probably wrong, but we just call it again here to handle
1922 * the problem instead of fixing it.
1924 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1926 /* These would normally be updated in mx_update_context(), but the
1927 * full headers aren't parsed with XOVER, so the information wasn't
1930 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1932 mutt_clear_error ();
1939 int nntp_post (const char *msg)
1941 char buf[LONG_STRING];
1944 nntp_data_t *nntp_data;
1946 if (Context && Context->magic == M_NNTP)
1947 nntp_data = (nntp_data_t *)Context->data;
1949 if (!(CurrentNewsSrv = mutt_select_newsserver(NewsServer)) ||
1950 !CurrentNewsSrv->list)
1952 mutt_error (_("Can't post article. No connection to news server."));
1955 nntp_data = CurrentNewsSrv->list;
1958 if (!(f = safe_fopen (msg, "r"))) {
1959 mutt_error (_("Can't post article. Unable to open %s"), msg);
1963 m_strcpy(buf, sizeof(buf), "POST\r\n");
1964 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1965 mutt_error (_("Can't post article. Connection to %s lost."),
1966 nntp_data->nserv->conn->account.host);
1969 if (buf[0] != '3') {
1970 mutt_error (_("Can't post article: %s"), buf);
1976 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1977 len = m_strlen(buf);
1978 if (buf[len - 1] == '\n') {
1979 buf[len - 1] = '\r';
1985 mutt_socket_write(nntp_data->nserv->conn, buf);
1987 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
1991 if (buf[m_strlen(buf) - 1] != '\n')
1992 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
1993 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
1994 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1995 mutt_error (_("Can't post article. Connection to %s lost."),
1996 nntp_data->nserv->conn->account.host);
1999 if (buf[0] != '2') {
2000 mutt_error (_("Can't post article: %s"), buf);
2007 /* nntp_logout_all: close all open connections. */
2008 void nntp_logout_all (void)
2010 char buf[LONG_STRING];
2013 conn = mutt_socket_head ();
2016 CONNECTION* next = conn->next;
2017 if (conn->account.type == M_ACCT_TYPE_NNTP) {
2018 mutt_message (_("Closing connection to %s..."), conn->account.host);
2019 mutt_socket_write (conn, "QUIT\r\n");
2020 mutt_socket_readln (buf, sizeof (buf), conn);
2021 mutt_clear_error ();
2022 mutt_socket_close (conn);
2023 mutt_socket_free (conn);
2029 static void nntp_free_acache (nntp_data_t * data)
2033 for (i = 0; i < NNTP_CACHE_LEN; i++) {
2034 if (data->acache[i].path) {
2035 unlink (data->acache[i].path);
2036 p_delete(&data->acache[i].path);
2041 void nntp_data_wipe(nntp_data_t *data)
2043 p_delete(&data->entries);
2044 p_delete(&data->desc);
2045 p_delete(&data->cache);
2046 p_delete(&data->group);
2047 nntp_free_acache(data);
2050 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
2052 nntp_data_t *data = ctx->data;
2054 nntp_save_cache_group(ctx);
2055 nntp_free_acache(data);
2057 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
2061 static void nntp_fastclose_mailbox (CONTEXT * ctx)
2063 nntp_data_t *data = (nntp_data_t *) ctx->data, *tmp;
2067 nntp_free_acache (data);
2068 if (!data->nserv || !data->nserv->newsgroups || !data->group)
2070 nntp_save_cache_index (data->nserv);
2071 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
2073 nntp_data_delete(&data);
2075 nntp_sync_sidebar (data);
2078 /* commit changes and terminate connection */
2079 int nntp_close_mailbox (CONTEXT * ctx)
2083 mutt_message _("Quitting newsgroup...");
2086 nntp_data_t *data = (nntp_data_t *) ctx->data;
2089 if (data->nserv && data->nserv->conn && ctx->unread) {
2090 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
2092 mutt_newsgroup_catchup (data->nserv, data->group);
2097 nntp_sync_mailbox (ctx, 0, NULL);
2098 if (ctx->data && ((nntp_data_t *) ctx->data)->nserv) {
2099 nntp_server_t *news;
2101 news = ((nntp_data_t *) ctx->data)->nserv;
2102 newsrc_gen_entries (ctx);
2103 ((nntp_data_t *) ctx->data)->unread = ctx->unread;
2104 mutt_newsrc_update (news);
2106 mutt_clear_error ();
2110 /* use the GROUP command to poll for new mail */
2111 static int _nntp_check_mailbox (CONTEXT * ctx, nntp_data_t * nntp_data)
2113 char buf[LONG_STRING];
2116 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
2120 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2123 if (m_strncmp("211", buf, 3)) {
2125 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
2129 if (!m_strncmp("211", buf, 3)) {
2133 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
2134 nntp_data->firstMessage = first;
2135 nntp_data->lastMessage = last;
2136 if (ctx && last > nntp_data->lastLoaded) {
2137 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
2138 time (&nntp_data->nserv->check_time);
2141 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
2142 nntp_data->unread = count;
2144 mutt_newsgroup_stat (nntp_data);
2145 /* active was renumbered? */
2146 if (last < nntp_data->lastLoaded) {
2147 if (!nntp_data->max) {
2148 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
2151 nntp_data->lastCached = 0;
2153 nntp_data->entries[0].first = 1;
2154 nntp_data->entries[0].last = 0;
2156 nntp_sync_sidebar (nntp_data);
2159 time (&nntp_data->nserv->check_time);
2163 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
2165 return _nntp_check_mailbox (ctx, (nntp_data_t *) ctx->data);
2168 static int nntp_check_newgroups (nntp_server_t * serv, int force)
2170 char buf[LONG_STRING];
2171 nntp_data_t nntp_data;
2172 nntp_data_t *l, **emp;
2176 if (!serv || !serv->newgroups_time)
2179 if (nntp_open_connection (serv) < 0)
2182 /* check subscribed groups for new news */
2183 if (option (OPTSHOWNEWNEWS)) {
2184 mutt_message _("Checking for new messages...");
2186 for (l = serv->list; l; l = l->next) {
2187 serv->check_time = 0; /* really check! */
2189 _nntp_check_mailbox (NULL, l);
2196 mutt_message _("Checking for new newsgroups...");
2198 now = serv->newgroups_time;
2199 time (&serv->newgroups_time);
2201 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
2202 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
2203 t->tm_min, t->tm_sec);
2204 nntp_data.nserv = serv;
2205 if (Context && Context->magic == M_NNTP)
2206 nntp_data.group = ((nntp_data_t *) Context->data)->group;
2208 nntp_data.group = NULL;
2210 emp = nntp_data_list_last(&serv->list);
2211 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
2212 add_group, serv, 0) != 0) {
2216 mutt_message _("Loading descriptions...");
2218 for (l = *emp; l; l = l->next) {
2220 nntp_get_desc(l, l->group, NULL, NULL);
2223 nntp_save_cache_index(serv);
2225 return nntp.checked;
2228 /* Load list of all newsgroups from active */
2229 int nntp_get_active (nntp_server_t * serv)
2232 nntp_data_t nntp_data, **tmp = &serv->list;
2234 if (nntp_open_connection (serv) < 0)
2237 snprintf (msg, sizeof (msg),
2238 _("Loading list of all newsgroups on server %s..."),
2239 serv->conn->account.host);
2241 time (&serv->newgroups_time);
2242 nntp_data.nserv = serv;
2243 nntp_data.group = NULL;
2245 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
2249 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
2251 nntp_get_desc (&nntp_data, "*", msg, NULL);
2254 if ((*tmp)->deleted && !(*tmp)->rc) {
2255 nntp_data_t *d = nntp_data_list_pop(tmp);
2256 nntp_delete_cache(d);
2257 hash_remove(serv->newsgroups, d->group, NULL, NULL);
2258 nntp_data_delete(&d);
2260 tmp = &(*tmp)->next;
2264 nntp_save_cache_index(serv);
2266 mutt_clear_error ();
2267 return nntp.checked;
2271 * returns -1 if error ocurred while retrieving header,
2272 * number of articles which ones exist in context on success.
2274 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
2278 /* if msgid is already in context, don't reload them */
2279 if (hash_find (ctx->id_hash, msgid))
2281 if (ctx->msgcount == ctx->hdrmax)
2282 mx_alloc_memory (ctx);
2283 ctx->hdrs[ctx->msgcount] = header_new();
2284 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
2286 mutt_message (_("Fetching %s from server..."), msgid);
2287 ret = nntp_read_header (ctx, msgid, 0);
2288 /* since nntp_read_header() may set read flag, we must reset it */
2289 ctx->hdrs[ctx->msgcount]->read = 0;
2291 header_delete(&ctx->hdrs[ctx->msgcount]);
2294 mx_update_context (ctx, 1);
2307 static int check_children (char *s, void *c)
2309 CHILD_CONTEXT *cc = c;
2312 if (!s || (n = atoi (s)) == 0)
2314 for (i = 0; i < cc->ctx->msgcount; i++)
2315 if (cc->ctx->hdrs[i]->article_num == n)
2317 if (cc->num >= cc->max)
2318 p_realloc(&cc->child, cc->max += 25);
2319 cc->child[cc->num++] = n;
2324 int nntp_check_children (CONTEXT * ctx, const char *msgid)
2326 nntp_data_t *nntp_data = (nntp_data_t *) ctx->data;
2328 int i, ret = 0, tmp = 0;
2329 CHILD_CONTEXT cc = { ctx, 0, 0, NULL };
2331 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
2332 !nntp_data->nserv->conn->account.host)
2334 if (nntp_data->firstMessage > nntp_data->lastLoaded)
2336 if (!nntp_data->nserv->hasXPAT) {
2337 mutt_error (_("Server %s does not support this operation!"),
2338 nntp_data->nserv->conn->account.host);
2342 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
2343 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
2345 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
2346 p_delete(&cc.child);
2350 /* dont try to read the xover cache. check_children() already
2351 * made sure that we dont have the article, so we need to visit
2352 * the server. Reading the cache at this point is also bad
2353 * because it would duplicate messages */
2354 if (nntp.use_cache) {
2356 nntp.use_cache = false;
2358 for (i = 0; i < cc.num; i++) {
2359 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
2361 if (ctx->msgcount &&
2362 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
2363 ctx->hdrs[ctx->msgcount - 1]->read = 0;
2366 nntp.use_cache = true;
2367 p_delete(&cc.child);
2371 static int nntp_is_magic (const char* path, struct stat* st) {
2372 url_scheme_t s = url_check_scheme(NONULL(path));
2373 return s == U_NNTP || s == U_NNTPS ? M_NNTP : -1;
2376 static int acl_check_nntp (CONTEXT* ctx, int bit) {
2378 case ACL_INSERT: /* editing messages */
2379 case ACL_WRITE: /* change importance */
2381 case ACL_DELETE: /* (un)deletion */
2382 case ACL_SEEN: /* mark as read */
2389 mx_t const nntp_mx = {
2399 nntp_fastclose_mailbox,