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>
14 #include <lib-mime/mime.h>
15 #include <lib-ui/curses.h>
16 #include <lib-ui/sidebar.h>
17 #include <lib-mx/mx.h>
25 #define WANT_LISTGROUP_COMMAND 0
27 static unsigned int _checked = 0;
29 void nntp_sync_sidebar (NNTP_DATA* data) {
37 /* unfortunately, NNTP_DATA::group only is the plain
38 * group name, so for every single update, we need to
39 * compose the full string which must be defined via
40 * mailboxes command ;-((( FIXME
43 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
44 data->nserv->conn->account.has_ssl ? "s" : "",
45 NONULL(data->nserv->conn->account.user),
46 *data->nserv->conn->account.pass ? ":" : "",
47 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
48 data->nserv->conn->account.host,
51 /* bail out if group not found via mailboxes */
52 if ((i = buffy_lookup (buf)) < 0)
55 tmp = Incoming.arr[i];
56 /* copied from browser.c */
57 if (option (OPTMARKOLD) &&
58 data->lastCached >= data->firstMessage &&
59 data->lastCached <= data->lastMessage)
60 tmp->msg_unread = data->lastMessage - data->lastCached;
62 tmp->msg_unread = data->unread;
63 tmp->new = data->unread > 0;
64 /* this is closest to a "total" count we can get */
65 tmp->msgcount = data->lastMessage - data->firstMessage;
68 static int nntp_auth (NNTP_SERVER * serv)
70 CONNECTION *conn = serv->conn;
72 unsigned char flags = conn->account.flags;
74 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
75 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
76 conn->account.flags = flags;
80 mutt_message _("Logging in...");
82 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
83 mutt_socket_write (conn, buf);
84 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
85 conn->account.flags = flags;
89 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
90 mutt_socket_write(conn, buf);
91 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
92 conn->account.flags = flags;
96 if (m_strncmp("281", buf, 3)) {
97 conn->account.flags = flags;
98 mutt_error _("Login failed.");
107 static int nntp_connect_error (NNTP_SERVER * serv)
109 serv->status = NNTP_NONE;
110 mutt_socket_close (serv->conn);
111 mutt_error _("Server closed connection!");
117 static int nntp_connect_and_auth (NNTP_SERVER * serv)
119 CONNECTION *conn = serv->conn;
123 serv->status = NNTP_NONE;
125 if (mutt_socket_open (conn) < 0)
128 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
129 return nntp_connect_error (serv);
131 if (!m_strncmp("200", buf, 3))
132 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
133 else if (!m_strncmp("201", buf, 3))
134 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
136 mutt_socket_close(conn);
138 mutt_error("%s", buf);
143 /* Tell INN to switch to mode reader if it isn't so. Ignore all
144 returned codes and messages. */
145 mutt_socket_write (conn, "MODE READER\r\n");
146 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
147 return nntp_connect_error (serv);
149 mutt_socket_write (conn, "STAT\r\n");
150 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
151 return nntp_connect_error (serv);
153 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
154 serv->status = NNTP_OK;
158 rc = nntp_auth (serv);
160 return nntp_connect_error (serv);
162 mutt_socket_close (conn);
163 serv->status = NNTP_BYE;
167 mutt_socket_close (conn);
168 mutt_error _("Login failed.");
173 serv->status = NNTP_OK;
177 static int nntp_attempt_features (NNTP_SERVER * serv)
179 char buf[LONG_STRING];
180 CONNECTION *conn = serv->conn;
182 mutt_socket_write (conn, "XOVER\r\n");
183 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
184 return nntp_connect_error (serv);
185 if (m_strncmp("500", buf, 3))
188 mutt_socket_write (conn, "XPAT\r\n");
189 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
190 return nntp_connect_error (serv);
191 if (m_strncmp("500", buf, 3))
194 #if WANT_LISTGROUP_COMMAND
195 mutt_socket_write (conn, "LISTGROUP\r\n");
196 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
197 return (nntp_connect_error (serv));
198 if (m_strncmp("500", buf, 3))
199 serv->hasLISTGROUP = 1;
202 mutt_socket_write (conn, "XGTITLE +\r\n");
203 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
204 return nntp_connect_error (serv);
205 if (m_strncmp("500", buf, 3))
206 serv->hasXGTITLE = 1;
208 if (!m_strncmp("282", buf, 3)) {
210 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
211 return nntp_connect_error (serv);
212 } while (!(buf[0] == '.' && buf[1] == '\0'));
218 static int nntp_open_connection (NNTP_SERVER * serv)
220 if (serv->status == NNTP_OK)
222 if (serv->status == NNTP_BYE)
224 if (nntp_connect_and_auth (serv) < 0)
226 if (nntp_attempt_features (serv) < 0)
231 static int nntp_reconnect (NNTP_SERVER * serv)
235 mutt_socket_close (serv->conn);
238 if (nntp_connect_and_auth (serv) == 0)
241 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
242 serv->conn->account.host);
243 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
244 serv->status = NNTP_BYE;
250 /* Send data from line[LONG_STRING] and receive answer to same line */
251 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
253 char buf[LONG_STRING];
256 if (data->nserv->status == NNTP_BYE)
261 mutt_socket_write (data->nserv->conn, line);
263 else if (data->group) {
264 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
265 mutt_socket_write (data->nserv->conn, buf);
269 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
270 if (nntp_reconnect (data->nserv) < 0)
274 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
275 mutt_socket_write (data->nserv->conn, buf);
276 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
282 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
286 m_strcpy(line, linelen, buf);
291 * This function calls funct(*line, *data) for each received line,
292 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
295 * 1 - correct but not performed (may be, have to be continued),
296 * -1 - conection lost,
297 * -2 - invalid command or execution error,
298 * -3 - error in funct(*line, *data).
300 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query,
301 const char *msg, progress_t* bar,
302 int (*funct) (char *, void *),
303 void *data, int tagged)
305 char buf[LONG_STRING];
314 m_strcpy(buf, sizeof(buf), query);
315 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
324 inbuf = p_new(char, sizeof(buf));
327 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
332 if (!lenbuf && buf[0] == '.') {
333 if (buf[1] == '\0') {
341 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
344 if (chunk >= ssizeof (buf)) {
345 lenbuf += m_strlen(p);
349 mutt_progress_bar (bar, pos);
352 if (ReadInc && (line % ReadInc == 0)) {
354 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
356 mutt_message ("%s %d", msg, line);
360 if (ret == 0 && funct (inbuf, data) < 0)
365 p_realloc(&inbuf, lenbuf + sizeof (buf));
374 static int nntp_read_tempfile (char *line, void *file)
376 FILE *f = (FILE *) file;
382 if (fputc ('\n', f) == EOF)
388 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
391 register char *p, *b;
392 register char *colon = NULL;
396 /* skip to next word */
398 while (*b && ((*b == ' ') || (*b == '\t')))
402 /* skip to end of word */
403 while (*p && (*p != ' ') && (*p != '\t')) {
415 nntp_get_status (ctx, h, b, atoi (colon));
416 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
417 h->article_num = atoi (colon);
425 * 1 if article not found
426 * -1 if read or write error on tempfile or socket
428 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
431 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
433 char buf[LONG_STRING];
434 char tempfile[_POSIX_PATH_MAX];
436 HEADER *h = ctx->hdrs[ctx->msgcount];
438 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
443 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
445 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
447 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
451 return (ret == -1 ? -1 : 1);
454 h->article_num = article_num;
455 h->env = mutt_read_rfc822_header (f, h, 0, 0);
459 if (h->env->xref != NULL)
460 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
461 else if (h->article_num == 0 && msgid) {
462 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
463 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
464 h->article_num = atoi (buf + 4);
470 static int parse_description (char *line, void *n)
472 #define news ((NNTP_SERVER *) n)
473 register char *d = line;
478 while (*d && *d != '\t' && *d != ' ')
482 while (*d && (*d == '\t' || *d == ' '))
484 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
485 m_strcmp(d, data->desc)) {
486 p_delete(&data->desc);
487 data->desc = m_strdup(d);
493 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
497 if (!option (OPTLOADDESC) || !data || !data->nserv)
500 /* Get newsgroup description, if we can */
501 if (data->nserv->hasXGTITLE)
502 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
504 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
505 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
511 * XOVER returns a tab separated list of:
512 * id|subject|from|date|Msgid|references|bytes|lines|xref
514 * This has to duplicate some of the functionality of
515 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
516 * a limited number of headers which are "parsed" by placement in the list)
518 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
520 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
524 hdr->env = envelope_new();
525 hdr->env->newsgroups = m_strdup(nntp_data->group);
526 hdr->content = body_new();
527 hdr->content->type = TYPETEXT;
528 hdr->content->subtype = m_strdup("plain");
529 hdr->content->encoding = ENC7BIT;
530 hdr->content->disposition = DISPINLINE;
531 hdr->content->length = -1;
534 for (x = 0; !done && x < 9; x++) {
535 /* if from file, need to skip newline character */
536 while (*p && *p != '\n' && *p != '\t')
545 hdr->article_num = atoi (b);
546 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
549 hdr->env->subject = m_strdup(b);
552 address_list_wipe(&hdr->env->from);
553 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
554 /* same as for mutt_parse_rfc822_line():
555 * don't leave from info NULL if there's an invalid address (or
556 * whatever) in From: field; mutt would just display it as empty
557 * and mark mail/(esp.) news article as your own. aaargh! this
558 * bothered me for _years_ */
559 if (!hdr->env->from) {
560 hdr->env->from = address_new ();
561 hdr->env->from->personal = m_strdup(b);
565 hdr->date_sent = mutt_parse_date (b, hdr);
566 hdr->received = hdr->date_sent;
569 p_delete(&hdr->env->message_id);
570 hdr->env->message_id = m_strdup(b);
573 string_list_wipe(&hdr->env->references);
574 hdr->env->references = mutt_parse_references (b, 0);
577 hdr->content->length = atoi (b);
580 hdr->lines = atoi (b);
584 p_delete(&hdr->env->xref);
585 b = b + 6; /* skips the "Xref: " */
586 hdr->env->xref = m_strdup(b);
587 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
589 rfc2047_decode_envelope(hdr->env);
602 unsigned short *messages;
606 #define fc ((FETCH_CONTEXT *) c)
608 #if WANT_LISTGROUP_COMMAND
609 static int _nntp_fetch_numbers (unsigned int num, void *c)
611 if (num < fc->base || num > fc->last)
613 fc->messages[num - fc->base] = 1;
616 static int nntp_fetch_numbers (char *line, void *c)
620 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
624 static int add_xover_line (char *line, void *c)
626 unsigned int num, total;
627 CONTEXT *ctx = fc->ctx;
628 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
633 if (ctx->msgcount >= ctx->hdrmax)
634 mx_alloc_memory (ctx);
635 ctx->hdrs[ctx->msgcount] = header_new();
636 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
638 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
639 num = ctx->hdrs[ctx->msgcount]->article_num;
641 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
643 if (num > data->lastLoaded)
644 data->lastLoaded = num;
645 num = num - fc->first + 1;
646 total = fc->last - fc->first + 1;
647 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
648 mutt_message ("%s %d/%d", fc->msg, num, total);
651 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
658 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
661 char buf[HUGE_STRING];
662 const char *msg = _("Fetching message headers...");
663 const char *msg2 = _("Fetching headers from cache...");
664 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
668 unsigned int current;
672 /* if empty group or nothing to do */
673 if (!last || first > last)
676 /* fetch list of articles */
677 mutt_message _("Fetching list of articles...");
682 fc.messages = p_new(unsigned short, last - first + 1);
683 #if WANT_LISTGROUP_COMMAND
684 if (nntp_data->nserv->hasLISTGROUP) {
685 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
686 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
688 mutt_error (_("LISTGROUP command failed: %s"), buf);
690 p_delete(&fc.messages);
696 for (num = 0; num < last - first + 1; num++)
697 fc.messages[num] = 1;
698 #if WANT_LISTGROUP_COMMAND
702 /* CACHE: must be loaded xover cache here */
703 num = nntp_data->lastCached - first + 1;
704 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
705 nntp_cache_expand (buf, nntp_data->cache);
708 if ((f = safe_fopen (buf, "r"))) {
711 /* counting number of lines */
712 while (fgets (buf, sizeof (buf), f) != NULL)
715 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
717 oldmsgcount = ctx->msgcount;
719 fc.last = first + num - 1;
721 while (fgets (buf, sizeof (buf), f) != NULL) {
722 if (ReadInc && ((++c) % ReadInc == 0))
723 mutt_message ("%s %d/%d", msg2, c, r);
724 add_xover_line (buf, &fc);
727 nntp_data->lastLoaded = fc.last;
729 if (ctx->msgcount > oldmsgcount)
730 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
733 nntp_delete_cache (nntp_data);
735 num = last - first + 1;
737 p_delete(&fc.messages);
742 * Without XOVER, we have to fetch each article header and parse
743 * it. With XOVER, we ask for all of them
746 if (nntp_data->nserv->hasXOVER) {
747 oldmsgcount = ctx->msgcount;
751 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
752 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
753 if (ctx->msgcount > oldmsgcount)
754 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
756 mutt_error (_("XOVER command failed: %s"), buf);
757 p_delete(&fc.messages);
763 for (current = first; current <= last; current++) {
766 ret = current - first + 1;
767 mutt_message ("%s %d/%d", msg, ret, num);
769 if (!fc.messages[current - fc.base])
772 if (ctx->msgcount >= ctx->hdrmax)
773 mx_alloc_memory (ctx);
774 h = ctx->hdrs[ctx->msgcount] = header_new();
775 h->index = ctx->msgcount;
777 ret = nntp_read_header (ctx, NULL, current);
778 if (ret == 0) { /* Got article. Fetch next header */
779 nntp_get_status (ctx, h, NULL, h->article_num);
781 mx_update_context (ctx, 1);
784 header_delete(&h); /* skip it */
786 p_delete(&fc.messages);
790 if (current > nntp_data->lastLoaded)
791 nntp_data->lastLoaded = current;
793 p_delete(&fc.messages);
794 nntp_data->lastLoaded = last;
800 * currently, nntp "mailbox" is "newsgroup"
802 static int nntp_open_mailbox (CONTEXT * ctx)
804 NNTP_DATA *nntp_data;
806 char buf[HUGE_STRING];
807 char server[LONG_STRING];
814 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
815 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
821 nntp_expand_path (server, sizeof (server), &act);
822 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
825 CurrentNewsSrv = serv;
827 /* create NNTP-specific state struct if nof found in list */
828 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
829 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
830 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
831 strcpy (nntp_data->group, buf);
832 hash_insert (serv->newsgroups, nntp_data->group, nntp_data);
833 nntp_add_to_list (serv, nntp_data);
835 ctx->data = nntp_data;
836 nntp_data->nserv = serv;
838 mutt_message (_("Selecting %s..."), nntp_data->group);
840 if (!nntp_data->desc) {
841 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
843 nntp_save_cache_index (serv);
847 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
851 if (m_strncmp("211", buf, 3)) {
852 string_list_t *l = serv->list;
854 /* GROUP command failed */
855 if (!m_strncmp("411", buf, 3)) {
856 mutt_error (_("Newsgroup %s not found on server %s"),
857 nntp_data->group, serv->conn->account.host);
859 /* CACHE: delete cache and line from .index */
860 nntp_delete_cache (nntp_data);
861 hash_remove (serv->newsgroups, nntp_data->group, NULL,
863 while (l && l->data != (void *) nntp_data)
874 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
875 &nntp_data->lastMessage, buf);
877 nntp_data->deleted = 0;
879 time (&serv->check_time);
882 * Check for max adding context. If it is greater than $nntp_context,
883 * strip off extra articles
885 first = nntp_data->firstMessage;
886 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
887 first = nntp_data->lastMessage - NntpContext + 1;
889 nntp_data->lastLoaded = first - 1;
890 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
893 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
895 char buf[LONG_STRING];
896 char path[_POSIX_PATH_MAX];
901 /* see if we already have the message in our cache */
903 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
906 /* if everything is fine, assign msg->fp and return */
907 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
908 (msg->fp = fopen (cache->path, "r")))
911 /* clear the previous entry */
912 unlink (cache->path);
913 p_delete(&cache->path);
915 cache->index = ctx->hdrs[msgno]->index;
916 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
920 cache->path = m_strdup(path);
922 if (ctx->hdrs[msgno]->article_num == 0)
923 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
924 ctx->hdrs[msgno]->env->message_id);
926 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
927 ctx->hdrs[msgno]->article_num);
929 bar.msg = _("Fetching message...");
931 mutt_progress_bar (&bar, 0);
933 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
934 msg->fp, ctx->tagged);
936 mutt_error (_("Article %d not found on server"),
937 ctx->hdrs[msgno]->article_num);
943 p_delete(&cache->path);
947 envelope_delete(&ctx->hdrs[msgno]->env);
948 ctx->hdrs[msgno]->env =
949 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
950 /* fix content length */
951 fseeko (msg->fp, 0, SEEK_END);
952 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
953 ctx->hdrs[msgno]->content->offset;
955 /* this is called in mutt before the open which fetches the message,
956 * which is probably wrong, but we just call it again here to handle
957 * the problem instead of fixing it.
959 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
961 /* These would normally be updated in mx_update_context(), but the
962 * full headers aren't parsed with XOVER, so the information wasn't
965 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
974 int nntp_post (const char *msg)
976 char buf[LONG_STRING];
979 NNTP_DATA *nntp_data;
981 if (Context && Context->magic == M_NNTP)
982 nntp_data = (NNTP_DATA *) Context->data;
984 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
985 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
986 mutt_error (_("Can't post article. No connection to news server."));
989 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
992 if (!(f = safe_fopen (msg, "r"))) {
993 mutt_error (_("Can't post article. Unable to open %s"), msg);
997 m_strcpy(buf, sizeof(buf), "POST\r\n");
998 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
999 mutt_error (_("Can't post article. Connection to %s lost."),
1000 nntp_data->nserv->conn->account.host);
1003 if (buf[0] != '3') {
1004 mutt_error (_("Can't post article: %s"), buf);
1010 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1011 len = m_strlen(buf);
1012 if (buf[len - 1] == '\n') {
1013 buf[len - 1] = '\r';
1019 mutt_socket_write(nntp_data->nserv->conn, buf);
1021 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
1025 if (buf[m_strlen(buf) - 1] != '\n')
1026 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
1027 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
1028 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1029 mutt_error (_("Can't post article. Connection to %s lost."),
1030 nntp_data->nserv->conn->account.host);
1033 if (buf[0] != '2') {
1034 mutt_error (_("Can't post article: %s"), buf);
1041 /* nntp_logout_all: close all open connections. */
1042 void nntp_logout_all (void)
1044 char buf[LONG_STRING];
1047 conn = mutt_socket_head ();
1050 CONNECTION* next = conn->next;
1051 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1052 mutt_message (_("Closing connection to %s..."), conn->account.host);
1053 mutt_socket_write (conn, "QUIT\r\n");
1054 mutt_socket_readln (buf, sizeof (buf), conn);
1055 mutt_clear_error ();
1056 mutt_socket_close (conn);
1057 mutt_socket_free (conn);
1063 static void nntp_free_acache (NNTP_DATA * data)
1067 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1068 if (data->acache[i].path) {
1069 unlink (data->acache[i].path);
1070 p_delete(&data->acache[i].path);
1075 void nntp_delete_data (void *p)
1077 NNTP_DATA *data = (NNTP_DATA *)p;
1081 p_delete(&data->entries);
1082 p_delete(&data->desc);
1083 p_delete(&data->cache);
1084 nntp_free_acache (data);
1088 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1090 NNTP_DATA *data = ctx->data;
1092 /* CACHE: update cache and .index files */
1093 if ((option (OPTSAVEUNSUB) || data->subscribed))
1094 nntp_save_cache_group (ctx);
1095 nntp_free_acache (data);
1097 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1101 static void nntp_fastclose_mailbox (CONTEXT * ctx)
1103 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1107 nntp_free_acache (data);
1108 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1110 nntp_save_cache_index (data->nserv);
1111 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1113 nntp_delete_data (data);
1115 nntp_sync_sidebar (data);
1118 /* commit changes and terminate connection */
1119 int nntp_close_mailbox (CONTEXT * ctx)
1123 mutt_message _("Quitting newsgroup...");
1126 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1129 if (data->nserv && data->nserv->conn && ctx->unread) {
1130 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1132 mutt_newsgroup_catchup (data->nserv, data->group);
1137 nntp_sync_mailbox (ctx, 0, NULL);
1138 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1141 news = ((NNTP_DATA *) ctx->data)->nserv;
1142 newsrc_gen_entries (ctx);
1143 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1144 mutt_newsrc_update (news);
1146 mutt_clear_error ();
1150 /* use the GROUP command to poll for new mail */
1151 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1153 char buf[LONG_STRING];
1156 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1160 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1163 if (m_strncmp("211", buf, 3)) {
1165 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1169 if (!m_strncmp("211", buf, 3)) {
1173 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1174 nntp_data->firstMessage = first;
1175 nntp_data->lastMessage = last;
1176 if (ctx && last > nntp_data->lastLoaded) {
1177 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1178 time (&nntp_data->nserv->check_time);
1181 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1182 nntp_data->unread = count;
1184 mutt_newsgroup_stat (nntp_data);
1185 /* active was renumbered? */
1186 if (last < nntp_data->lastLoaded) {
1187 if (!nntp_data->max) {
1188 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1191 nntp_data->lastCached = 0;
1193 nntp_data->entries[0].first = 1;
1194 nntp_data->entries[0].last = 0;
1196 nntp_sync_sidebar (nntp_data);
1199 time (&nntp_data->nserv->check_time);
1203 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1205 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1208 static int add_group (char *buf, void *serv)
1210 #define s ((NNTP_SERVER *) serv)
1211 char group[LONG_STRING], mod, desc[HUGE_STRING];
1213 NNTP_DATA *nntp_data;
1216 _checked = n; /* _checked have N, where N = number of groups */
1217 if (!buf) /* at EOF must be zerouth */
1224 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1227 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1229 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1230 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1231 strcpy (nntp_data->group, group);
1232 nntp_data->nserv = s;
1233 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1234 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1235 hash_insert (s->newsgroups, nntp_data->group, nntp_data);
1236 nntp_add_to_list (s, nntp_data);
1238 nntp_data->deleted = 0;
1239 nntp_data->firstMessage = first;
1240 nntp_data->lastMessage = last;
1242 nntp_data->allowed = 1;
1244 nntp_data->allowed = 0;
1245 if (nntp_data->desc)
1246 p_delete(&nntp_data->desc);
1248 nntp_data->desc = m_strdup(desc);
1249 if (nntp_data->rc || nntp_data->lastCached)
1250 mutt_newsgroup_stat (nntp_data);
1251 else if (nntp_data->lastMessage &&
1252 nntp_data->firstMessage <= nntp_data->lastMessage)
1253 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1255 nntp_data->unread = 0;
1261 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1263 char buf[LONG_STRING];
1264 NNTP_DATA nntp_data;
1270 if (!serv || !serv->newgroups_time)
1273 if (nntp_open_connection (serv) < 0)
1276 /* check subscribed groups for new news */
1277 if (option (OPTSHOWNEWNEWS)) {
1278 mutt_message _("Checking for new messages...");
1280 for (l = serv->list; l; l = l->next) {
1281 serv->check_time = 0; /* really check! */
1282 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1283 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1290 mutt_message _("Checking for new newsgroups...");
1292 now = serv->newgroups_time;
1293 time (&serv->newgroups_time);
1295 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1296 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1297 t->tm_min, t->tm_sec);
1298 nntp_data.nserv = serv;
1299 if (Context && Context->magic == M_NNTP)
1300 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1302 nntp_data.group = NULL;
1304 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1305 add_group, serv, 0) != 0) {
1309 mutt_message _("Loading descriptions...");
1314 emp.next = serv->list;
1318 ((NNTP_DATA *) l->data)->new = 1;
1319 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1323 nntp_save_cache_index (serv);
1324 mutt_clear_error ();
1328 /* Load list of all newsgroups from cache ALL */
1329 int nntp_get_cache_all (NNTP_SERVER * serv)
1331 char buf[HUGE_STRING];
1334 nntp_cache_expand (buf, serv->cache);
1335 if ((f = safe_fopen (buf, "r"))) {
1338 while (fgets (buf, sizeof (buf), f) != NULL) {
1339 if (ReadInc && (i % ReadInc == 0))
1340 mutt_message (_("Loading list from cache... %d"), i);
1341 add_group (buf, serv);
1344 add_group (NULL, NULL);
1346 mutt_clear_error ();
1350 p_delete(&serv->cache);
1355 /* Load list of all newsgroups from active */
1356 int nntp_get_active (NNTP_SERVER * serv)
1359 NNTP_DATA nntp_data;
1362 if (nntp_open_connection (serv) < 0)
1365 snprintf (msg, sizeof (msg),
1366 _("Loading list of all newsgroups on server %s..."),
1367 serv->conn->account.host);
1369 time (&serv->newgroups_time);
1370 nntp_data.nserv = serv;
1371 nntp_data.group = NULL;
1373 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
1377 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1379 nntp_get_desc (&nntp_data, "*", msg, NULL);
1381 for (tmp = serv->list; tmp; tmp = tmp->next) {
1382 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1384 if (data && data->deleted && !data->rc) {
1385 nntp_delete_cache (data);
1386 hash_remove (serv->newsgroups, data->group, NULL, nntp_delete_data);
1390 nntp_save_cache_index (serv);
1392 mutt_clear_error ();
1397 * returns -1 if error ocurred while retrieving header,
1398 * number of articles which ones exist in context on success.
1400 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1404 /* if msgid is already in context, don't reload them */
1405 if (hash_find (ctx->id_hash, msgid))
1407 if (ctx->msgcount == ctx->hdrmax)
1408 mx_alloc_memory (ctx);
1409 ctx->hdrs[ctx->msgcount] = header_new();
1410 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1412 mutt_message (_("Fetching %s from server..."), msgid);
1413 ret = nntp_read_header (ctx, msgid, 0);
1414 /* since nntp_read_header() may set read flag, we must reset it */
1415 ctx->hdrs[ctx->msgcount]->read = 0;
1417 header_delete(&ctx->hdrs[ctx->msgcount]);
1420 mx_update_context (ctx, 1);
1430 unsigned int *child;
1433 static int check_children (char *s, void *c)
1435 #define cc ((CHILD_CONTEXT *) c)
1438 if (!s || (n = atoi (s)) == 0)
1440 for (i = 0; i < cc->ctx->msgcount; i++)
1441 if (cc->ctx->hdrs[i]->article_num == n)
1443 if (cc->num >= cc->max)
1444 p_realloc(&cc->child, cc->max += 25);
1445 cc->child[cc->num++] = n;
1451 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1453 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1455 int i, ret = 0, tmp = 0;
1458 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1459 !nntp_data->nserv->conn->account.host)
1461 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1463 if (!nntp_data->nserv->hasXPAT) {
1464 mutt_error (_("Server %s does not support this operation!"),
1465 nntp_data->nserv->conn->account.host);
1469 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1470 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1475 cc.child = p_new(unsigned int, 25);
1476 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1477 p_delete(&cc.child);
1480 /* dont try to read the xover cache. check_children() already
1481 * made sure that we dont have the article, so we need to visit
1482 * the server. Reading the cache at this point is also bad
1483 * because it would duplicate messages */
1484 if (option (OPTNEWSCACHE)) {
1486 unset_option (OPTNEWSCACHE);
1488 for (i = 0; i < cc.num; i++) {
1489 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1491 if (ctx->msgcount &&
1492 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1493 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1496 set_option (OPTNEWSCACHE);
1497 p_delete(&cc.child);
1501 static int nntp_is_magic (const char* path, struct stat* st) {
1502 url_scheme_t s = url_check_scheme (NONULL (path));
1503 return ((s == U_NNTP || s == U_NNTPS) ? M_NNTP : -1);
1506 static int acl_check_nntp (CONTEXT* ctx, int bit) {
1508 case ACL_INSERT: /* editing messages */
1509 case ACL_WRITE: /* change importance */
1511 case ACL_DELETE: /* (un)deletion */
1512 case ACL_SEEN: /* mark as read */
1519 mx_t const nntp_mx = {
1529 nntp_fastclose_mailbox,