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>
24 #include <lib-crypt/crypt.h>
26 #define WANT_LISTGROUP_COMMAND 0
28 static unsigned int _checked = 0;
30 void nntp_sync_sidebar (NNTP_DATA* data) {
38 /* unfortunately, NNTP_DATA::group only is the plain
39 * group name, so for every single update, we need to
40 * compose the full string which must be defined via
41 * mailboxes command ;-((( FIXME
44 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
45 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
46 NONULL (data->nserv->conn->account.user),
47 *data->nserv->conn->account.pass ? ":" : "",
48 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
49 data->nserv->conn->account.host,
52 /* bail out if group not found via mailboxes */
53 if ((i = buffy_lookup (buf)) < 0)
56 tmp = Incoming.arr[i];
57 /* copied from browser.c */
58 if (option (OPTMARKOLD) &&
59 data->lastCached >= data->firstMessage &&
60 data->lastCached <= data->lastMessage)
61 tmp->msg_unread = data->lastMessage - data->lastCached;
63 tmp->msg_unread = data->unread;
64 tmp->new = data->unread > 0;
65 /* this is closest to a "total" count we can get */
66 tmp->msgcount = data->lastMessage - data->firstMessage;
69 static int nntp_auth (NNTP_SERVER * serv)
71 CONNECTION *conn = serv->conn;
73 unsigned char flags = conn->account.flags;
75 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
76 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
77 conn->account.flags = flags;
81 mutt_message _("Logging in...");
83 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
84 mutt_socket_write (conn, buf);
85 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
86 conn->account.flags = flags;
90 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
91 mutt_socket_write(conn, buf);
92 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
93 conn->account.flags = flags;
97 if (m_strncmp("281", buf, 3)) {
98 conn->account.flags = flags;
99 mutt_error _("Login failed.");
108 static int nntp_connect_error (NNTP_SERVER * serv)
110 serv->status = NNTP_NONE;
111 mutt_socket_close (serv->conn);
112 mutt_error _("Server closed connection!");
118 static int nntp_connect_and_auth (NNTP_SERVER * serv)
120 CONNECTION *conn = serv->conn;
124 serv->status = NNTP_NONE;
126 if (mutt_socket_open (conn) < 0)
129 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
130 return nntp_connect_error (serv);
132 if (!m_strncmp("200", buf, 3))
133 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
134 else if (!m_strncmp("201", buf, 3))
135 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
137 mutt_socket_close(conn);
139 mutt_error("%s", buf);
146 /* Tell INN to switch to mode reader if it isn't so. Ignore all
147 returned codes and messages. */
148 mutt_socket_write (conn, "MODE READER\r\n");
149 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
150 return nntp_connect_error (serv);
152 mutt_socket_write (conn, "STAT\r\n");
153 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
154 return nntp_connect_error (serv);
156 if (!(conn->account.flags & M_ACCT_USER) && m_strncmp("480", buf, 3)) {
157 serv->status = NNTP_OK;
161 rc = nntp_auth (serv);
163 return nntp_connect_error (serv);
165 mutt_socket_close (conn);
166 serv->status = NNTP_BYE;
170 mutt_socket_close (conn);
171 mutt_error _("Login failed.");
176 serv->status = NNTP_OK;
180 static int nntp_attempt_features (NNTP_SERVER * serv)
182 char buf[LONG_STRING];
183 CONNECTION *conn = serv->conn;
185 mutt_socket_write (conn, "XOVER\r\n");
186 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
187 return nntp_connect_error (serv);
188 if (m_strncmp("500", buf, 3))
191 mutt_socket_write (conn, "XPAT\r\n");
192 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
193 return nntp_connect_error (serv);
194 if (m_strncmp("500", buf, 3))
197 #if WANT_LISTGROUP_COMMAND
198 mutt_socket_write (conn, "LISTGROUP\r\n");
199 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
200 return (nntp_connect_error (serv));
201 if (m_strncmp("500", buf, 3))
202 serv->hasLISTGROUP = 1;
205 mutt_socket_write (conn, "XGTITLE +\r\n");
206 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
207 return nntp_connect_error (serv);
208 if (m_strncmp("500", buf, 3))
209 serv->hasXGTITLE = 1;
211 if (!m_strncmp("282", buf, 3)) {
213 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
214 return nntp_connect_error (serv);
215 } while (!(buf[0] == '.' && buf[1] == '\0'));
221 static int nntp_open_connection (NNTP_SERVER * serv)
223 if (serv->status == NNTP_OK)
225 if (serv->status == NNTP_BYE)
227 if (nntp_connect_and_auth (serv) < 0)
229 if (nntp_attempt_features (serv) < 0)
234 static int nntp_reconnect (NNTP_SERVER * serv)
236 char buf[SHORT_STRING];
238 mutt_socket_close (serv->conn);
241 if (nntp_connect_and_auth (serv) == 0)
244 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
245 serv->conn->account.host);
246 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
247 serv->status = NNTP_BYE;
253 /* Send data from line[LONG_STRING] and receive answer to same line */
254 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
256 char buf[LONG_STRING];
259 if (data->nserv->status == NNTP_BYE)
264 mutt_socket_write (data->nserv->conn, line);
266 else if (data->group) {
267 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
268 mutt_socket_write (data->nserv->conn, buf);
272 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
273 if (nntp_reconnect (data->nserv) < 0)
277 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
278 mutt_socket_write (data->nserv->conn, buf);
279 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
285 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
289 m_strcpy(line, linelen, buf);
294 * This function calls funct(*line, *data) for each received line,
295 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
298 * 1 - correct but not performed (may be, have to be continued),
299 * -1 - conection lost,
300 * -2 - invalid command or execution error,
301 * -3 - error in funct(*line, *data).
303 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg,
304 progress_t* bar, int (*funct) (char *, void *),
305 void *data, int tagged)
307 char buf[LONG_STRING];
316 m_strcpy(buf, sizeof(buf), query);
317 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
326 inbuf = p_new(char, sizeof(buf));
329 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
334 if (!lenbuf && buf[0] == '.') {
335 if (buf[1] == '\0') {
343 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
346 if (chunk >= ssizeof (buf)) {
347 lenbuf += m_strlen(p);
351 mutt_progress_bar (bar, pos);
354 if (ReadInc && (line % ReadInc == 0)) {
356 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
358 mutt_message ("%s %d", msg, line);
362 if (ret == 0 && funct (inbuf, data) < 0)
367 p_realloc(&inbuf, lenbuf + sizeof (buf));
376 static int nntp_read_tempfile (char *line, void *file)
378 FILE *f = (FILE *) file;
384 if (fputc ('\n', f) == EOF)
390 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
393 register char *p, *b;
394 register char *colon = NULL;
398 /* skip to next word */
400 while (*b && ((*b == ' ') || (*b == '\t')))
404 /* skip to end of word */
405 while (*p && (*p != ' ') && (*p != '\t')) {
417 nntp_get_status (ctx, h, b, atoi (colon));
418 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
419 h->article_num = atoi (colon);
427 * 1 if article not found
428 * -1 if read or write error on tempfile or socket
430 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
433 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
435 char buf[LONG_STRING];
436 char tempfile[_POSIX_PATH_MAX];
438 HEADER *h = ctx->hdrs[ctx->msgcount];
440 mutt_mktemp (tempfile);
441 if (!(f = safe_fopen (tempfile, "w+")))
445 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
447 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
449 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
453 return (ret == -1 ? -1 : 1);
456 h->article_num = article_num;
457 h->env = mutt_read_rfc822_header (f, h, 0, 0);
461 if (h->env->xref != NULL)
462 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
463 else if (h->article_num == 0 && msgid) {
464 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
465 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
466 h->article_num = atoi (buf + 4);
472 static int parse_description (char *line, void *n)
474 #define news ((NNTP_SERVER *) n)
475 register char *d = line;
480 while (*d && *d != '\t' && *d != ' ')
484 while (*d && (*d == '\t' || *d == ' '))
486 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
487 m_strcmp(d, data->desc)) {
488 p_delete(&data->desc);
489 data->desc = m_strdup(d);
495 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
499 if (!option (OPTLOADDESC) || !data || !data->nserv)
502 /* Get newsgroup description, if we can */
503 if (data->nserv->hasXGTITLE)
504 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
506 snprintf (buf, sizeof (buf), "string_list_t NEWSGROUPS %s\r\n", mask);
507 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
513 * XOVER returns a tab separated list of:
514 * id|subject|from|date|Msgid|references|bytes|lines|xref
516 * This has to duplicate some of the functionality of
517 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
518 * a limited number of headers which are "parsed" by placement in the list)
520 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
522 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
526 hdr->env = envelope_new();
527 hdr->env->newsgroups = m_strdup(nntp_data->group);
528 hdr->content = body_new();
529 hdr->content->type = TYPETEXT;
530 hdr->content->subtype = m_strdup("plain");
531 hdr->content->encoding = ENC7BIT;
532 hdr->content->disposition = DISPINLINE;
533 hdr->content->length = -1;
536 for (x = 0; !done && x < 9; x++) {
537 /* if from file, need to skip newline character */
538 while (*p && *p != '\n' && *p != '\t')
547 hdr->article_num = atoi (b);
548 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
551 hdr->env->subject = m_strdup(b);
554 address_list_wipe(&hdr->env->from);
555 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
556 /* same as for mutt_parse_rfc822_line():
557 * don't leave from info NULL if there's an invalid address (or
558 * whatever) in From: field; mutt would just display it as empty
559 * and mark mail/(esp.) news article as your own. aaargh! this
560 * bothered me for _years_ */
561 if (!hdr->env->from) {
562 hdr->env->from = address_new ();
563 hdr->env->from->personal = m_strdup(b);
567 hdr->date_sent = mutt_parse_date (b, hdr);
568 hdr->received = hdr->date_sent;
571 p_delete(&hdr->env->message_id);
572 hdr->env->message_id = m_strdup(b);
575 string_list_wipe(&hdr->env->references);
576 hdr->env->references = mutt_parse_references (b, 0);
579 hdr->content->length = atoi (b);
582 hdr->lines = atoi (b);
586 p_delete(&hdr->env->xref);
587 b = b + 6; /* skips the "Xref: " */
588 hdr->env->xref = m_strdup(b);
589 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
591 rfc2047_decode_envelope(hdr->env);
604 unsigned short *messages;
608 #define fc ((FETCH_CONTEXT *) c)
610 #if WANT_LISTGROUP_COMMAND
611 static int _nntp_fetch_numbers (unsigned int num, void *c)
613 if (num < fc->base || num > fc->last)
615 fc->messages[num - fc->base] = 1;
618 static int nntp_fetch_numbers (char *line, void *c)
622 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
626 static int add_xover_line (char *line, void *c)
628 unsigned int num, total;
629 CONTEXT *ctx = fc->ctx;
630 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
635 if (ctx->msgcount >= ctx->hdrmax)
636 mx_alloc_memory (ctx);
637 ctx->hdrs[ctx->msgcount] = header_new();
638 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
640 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
641 num = ctx->hdrs[ctx->msgcount]->article_num;
643 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
645 if (num > data->lastLoaded)
646 data->lastLoaded = num;
647 num = num - fc->first + 1;
648 total = fc->last - fc->first + 1;
649 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
650 mutt_message ("%s %d/%d", fc->msg, num, total);
653 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
660 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
663 char buf[HUGE_STRING];
664 char *msg = _("Fetching message headers...");
665 char *msg2 = _("Fetching headers from cache...");
666 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
670 unsigned int current;
674 /* if empty group or nothing to do */
675 if (!last || first > last)
678 /* fetch list of articles */
679 mutt_message _("Fetching list of articles...");
684 fc.messages = p_new(unsigned short, last - first + 1);
685 #if WANT_LISTGROUP_COMMAND
686 if (nntp_data->nserv->hasLISTGROUP) {
687 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
688 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
690 mutt_error (_("LISTGROUP command failed: %s"), buf);
692 p_delete(&fc.messages);
698 for (num = 0; num < last - first + 1; num++)
699 fc.messages[num] = 1;
700 #if WANT_LISTGROUP_COMMAND
704 /* CACHE: must be loaded xover cache here */
705 num = nntp_data->lastCached - first + 1;
706 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
707 nntp_cache_expand (buf, nntp_data->cache);
710 if ((f = safe_fopen (buf, "r"))) {
713 /* counting number of lines */
714 while (fgets (buf, sizeof (buf), f) != NULL)
717 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
719 oldmsgcount = ctx->msgcount;
721 fc.last = first + num - 1;
723 while (fgets (buf, sizeof (buf), f) != NULL) {
724 if (ReadInc && ((++c) % ReadInc == 0))
725 mutt_message ("%s %d/%d", msg2, c, r);
726 add_xover_line (buf, &fc);
729 nntp_data->lastLoaded = fc.last;
731 if (ctx->msgcount > oldmsgcount)
732 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
735 nntp_delete_cache (nntp_data);
737 num = last - first + 1;
739 p_delete(&fc.messages);
744 * Without XOVER, we have to fetch each article header and parse
745 * it. With XOVER, we ask for all of them
748 if (nntp_data->nserv->hasXOVER) {
749 oldmsgcount = ctx->msgcount;
753 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
754 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
755 if (ctx->msgcount > oldmsgcount)
756 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
758 mutt_error (_("XOVER command failed: %s"), buf);
759 p_delete(&fc.messages);
765 for (current = first; current <= last; current++) {
768 ret = current - first + 1;
769 mutt_message ("%s %d/%d", msg, ret, num);
771 if (!fc.messages[current - fc.base])
774 if (ctx->msgcount >= ctx->hdrmax)
775 mx_alloc_memory (ctx);
776 h = ctx->hdrs[ctx->msgcount] = header_new();
777 h->index = ctx->msgcount;
779 ret = nntp_read_header (ctx, NULL, current);
780 if (ret == 0) { /* Got article. Fetch next header */
781 nntp_get_status (ctx, h, NULL, h->article_num);
783 mx_update_context (ctx, 1);
786 header_delete(&h); /* skip it */
788 p_delete(&fc.messages);
792 if (current > nntp_data->lastLoaded)
793 nntp_data->lastLoaded = current;
795 p_delete(&fc.messages);
796 nntp_data->lastLoaded = last;
802 * currently, nntp "mailbox" is "newsgroup"
804 static int nntp_open_mailbox (CONTEXT * ctx)
806 NNTP_DATA *nntp_data;
808 char buf[HUGE_STRING];
809 char server[LONG_STRING];
816 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
817 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
823 nntp_expand_path (server, sizeof (server), &act);
824 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
827 CurrentNewsSrv = serv;
829 /* create NNTP-specific state struct if nof found in list */
830 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
831 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
832 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
833 strcpy (nntp_data->group, buf);
834 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
835 nntp_add_to_list (serv, nntp_data);
837 ctx->data = nntp_data;
838 nntp_data->nserv = serv;
840 mutt_message (_("Selecting %s..."), nntp_data->group);
842 if (!nntp_data->desc) {
843 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
845 nntp_save_cache_index (serv);
849 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
853 if (m_strncmp("211", buf, 3)) {
854 string_list_t *l = serv->list;
856 /* GROUP command failed */
857 if (!m_strncmp("411", buf, 3)) {
858 mutt_error (_("Newsgroup %s not found on server %s"),
859 nntp_data->group, serv->conn->account.host);
861 /* CACHE: delete cache and line from .index */
862 nntp_delete_cache (nntp_data);
863 hash_delete (serv->newsgroups, nntp_data->group, NULL,
865 while (l && l->data != (void *) nntp_data)
876 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
877 &nntp_data->lastMessage, buf);
879 nntp_data->deleted = 0;
881 time (&serv->check_time);
884 * Check for max adding context. If it is greater than $nntp_context,
885 * strip off extra articles
887 first = nntp_data->firstMessage;
888 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
889 first = nntp_data->lastMessage - NntpContext + 1;
891 nntp_data->lastLoaded = first - 1;
892 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
895 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
897 char buf[LONG_STRING];
898 char path[_POSIX_PATH_MAX];
903 /* see if we already have the message in our cache */
905 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
908 /* if everything is fine, assign msg->fp and return */
909 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
910 (msg->fp = fopen (cache->path, "r")))
913 /* clear the previous entry */
914 unlink (cache->path);
915 p_delete(&cache->path);
917 cache->index = ctx->hdrs[msgno]->index;
919 cache->path = m_strdup(path);
920 if (!(msg->fp = safe_fopen (path, "w+"))) {
921 p_delete(&cache->path);
925 if (ctx->hdrs[msgno]->article_num == 0)
926 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
927 ctx->hdrs[msgno]->env->message_id);
929 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
930 ctx->hdrs[msgno]->article_num);
932 bar.msg = _("Fetching message...");
934 mutt_progress_bar (&bar, 0);
936 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
937 msg->fp, ctx->tagged);
939 mutt_error (_("Article %d not found on server"),
940 ctx->hdrs[msgno]->article_num);
946 p_delete(&cache->path);
950 envelope_delete(&ctx->hdrs[msgno]->env);
951 ctx->hdrs[msgno]->env =
952 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
953 /* fix content length */
954 fseeko (msg->fp, 0, SEEK_END);
955 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
956 ctx->hdrs[msgno]->content->offset;
958 /* this is called in mutt before the open which fetches the message,
959 * which is probably wrong, but we just call it again here to handle
960 * the problem instead of fixing it.
962 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
964 /* These would normally be updated in mx_update_context(), but the
965 * full headers aren't parsed with XOVER, so the information wasn't
968 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
977 int nntp_post (const char *msg)
979 char buf[LONG_STRING];
982 NNTP_DATA *nntp_data;
984 if (Context && Context->magic == M_NNTP)
985 nntp_data = (NNTP_DATA *) Context->data;
987 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
988 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
989 mutt_error (_("Can't post article. No connection to news server."));
992 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
995 if (!(f = safe_fopen (msg, "r"))) {
996 mutt_error (_("Can't post article. Unable to open %s"), msg);
1000 m_strcpy(buf, sizeof(buf), "POST\r\n");
1001 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1002 mutt_error (_("Can't post article. Connection to %s lost."),
1003 nntp_data->nserv->conn->account.host);
1006 if (buf[0] != '3') {
1007 mutt_error (_("Can't post article: %s"), buf);
1013 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1014 len = m_strlen(buf);
1015 if (buf[len - 1] == '\n') {
1016 buf[len - 1] = '\r';
1022 mutt_socket_write(nntp_data->nserv->conn, buf);
1024 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
1028 if (buf[m_strlen(buf) - 1] != '\n')
1029 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
1030 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
1031 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1032 mutt_error (_("Can't post article. Connection to %s lost."),
1033 nntp_data->nserv->conn->account.host);
1036 if (buf[0] != '2') {
1037 mutt_error (_("Can't post article: %s"), buf);
1044 /* nntp_logout_all: close all open connections. */
1045 void nntp_logout_all (void)
1047 char buf[LONG_STRING];
1050 conn = mutt_socket_head ();
1053 CONNECTION* next = conn->next;
1054 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1055 mutt_message (_("Closing connection to %s..."), conn->account.host);
1056 mutt_socket_write (conn, "QUIT\r\n");
1057 mutt_socket_readln (buf, sizeof (buf), conn);
1058 mutt_clear_error ();
1059 mutt_socket_close (conn);
1060 mutt_socket_free (conn);
1066 static void nntp_free_acache (NNTP_DATA * data)
1070 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1071 if (data->acache[i].path) {
1072 unlink (data->acache[i].path);
1073 p_delete(&data->acache[i].path);
1078 void nntp_delete_data (void *p)
1080 NNTP_DATA *data = (NNTP_DATA *)p;
1084 p_delete(&data->entries);
1085 p_delete(&data->desc);
1086 p_delete(&data->cache);
1087 nntp_free_acache (data);
1091 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1093 NNTP_DATA *data = ctx->data;
1095 /* CACHE: update cache and .index files */
1096 if ((option (OPTSAVEUNSUB) || data->subscribed))
1097 nntp_save_cache_group (ctx);
1098 nntp_free_acache (data);
1100 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1104 static void nntp_fastclose_mailbox (CONTEXT * ctx)
1106 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1110 nntp_free_acache (data);
1111 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1113 nntp_save_cache_index (data->nserv);
1114 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1116 nntp_delete_data (data);
1118 nntp_sync_sidebar (data);
1121 /* commit changes and terminate connection */
1122 int nntp_close_mailbox (CONTEXT * ctx)
1126 mutt_message _("Quitting newsgroup...");
1129 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1132 if (data->nserv && data->nserv->conn && ctx->unread) {
1133 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1135 mutt_newsgroup_catchup (data->nserv, data->group);
1140 nntp_sync_mailbox (ctx, 0, NULL);
1141 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1144 news = ((NNTP_DATA *) ctx->data)->nserv;
1145 newsrc_gen_entries (ctx);
1146 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1147 mutt_newsrc_update (news);
1149 mutt_clear_error ();
1153 /* use the GROUP command to poll for new mail */
1154 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1156 char buf[LONG_STRING];
1159 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1163 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1166 if (m_strncmp("211", buf, 3)) {
1168 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1172 if (!m_strncmp("211", buf, 3)) {
1176 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1177 nntp_data->firstMessage = first;
1178 nntp_data->lastMessage = last;
1179 if (ctx && last > nntp_data->lastLoaded) {
1180 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1181 time (&nntp_data->nserv->check_time);
1184 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1185 nntp_data->unread = count;
1187 mutt_newsgroup_stat (nntp_data);
1188 /* active was renumbered? */
1189 if (last < nntp_data->lastLoaded) {
1190 if (!nntp_data->max) {
1191 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1194 nntp_data->lastCached = 0;
1196 nntp_data->entries[0].first = 1;
1197 nntp_data->entries[0].last = 0;
1199 nntp_sync_sidebar (nntp_data);
1202 time (&nntp_data->nserv->check_time);
1206 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1208 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1211 static int add_group (char *buf, void *serv)
1213 #define s ((NNTP_SERVER *) serv)
1214 char group[LONG_STRING], mod, desc[HUGE_STRING];
1216 NNTP_DATA *nntp_data;
1219 _checked = n; /* _checked have N, where N = number of groups */
1220 if (!buf) /* at EOF must be zerouth */
1227 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1230 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1232 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1233 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1234 strcpy (nntp_data->group, group);
1235 nntp_data->nserv = s;
1236 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1237 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1238 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1239 nntp_add_to_list (s, nntp_data);
1241 nntp_data->deleted = 0;
1242 nntp_data->firstMessage = first;
1243 nntp_data->lastMessage = last;
1245 nntp_data->allowed = 1;
1247 nntp_data->allowed = 0;
1248 if (nntp_data->desc)
1249 p_delete(&nntp_data->desc);
1251 nntp_data->desc = m_strdup(desc);
1252 if (nntp_data->rc || nntp_data->lastCached)
1253 mutt_newsgroup_stat (nntp_data);
1254 else if (nntp_data->lastMessage &&
1255 nntp_data->firstMessage <= nntp_data->lastMessage)
1256 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1258 nntp_data->unread = 0;
1264 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1266 char buf[LONG_STRING];
1267 NNTP_DATA nntp_data;
1273 if (!serv || !serv->newgroups_time)
1276 if (nntp_open_connection (serv) < 0)
1279 /* check subscribed groups for new news */
1280 if (option (OPTSHOWNEWNEWS)) {
1281 mutt_message _("Checking for new messages...");
1283 for (l = serv->list; l; l = l->next) {
1284 serv->check_time = 0; /* really check! */
1285 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1286 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1293 mutt_message _("Checking for new newsgroups...");
1295 now = serv->newgroups_time;
1296 time (&serv->newgroups_time);
1298 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1299 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1300 t->tm_min, t->tm_sec);
1301 nntp_data.nserv = serv;
1302 if (Context && Context->magic == M_NNTP)
1303 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1305 nntp_data.group = NULL;
1307 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1308 add_group, serv, 0) != 0) {
1312 mutt_message _("Loading descriptions...");
1317 emp.next = serv->list;
1321 ((NNTP_DATA *) l->data)->new = 1;
1322 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1326 nntp_save_cache_index (serv);
1327 mutt_clear_error ();
1331 /* Load list of all newsgroups from cache ALL */
1332 int nntp_get_cache_all (NNTP_SERVER * serv)
1334 char buf[HUGE_STRING];
1337 nntp_cache_expand (buf, serv->cache);
1338 if ((f = safe_fopen (buf, "r"))) {
1341 while (fgets (buf, sizeof (buf), f) != NULL) {
1342 if (ReadInc && (i % ReadInc == 0))
1343 mutt_message (_("Loading list from cache... %d"), i);
1344 add_group (buf, serv);
1347 add_group (NULL, NULL);
1349 mutt_clear_error ();
1353 p_delete(&serv->cache);
1358 /* Load list of all newsgroups from active */
1359 int nntp_get_active (NNTP_SERVER * serv)
1361 char msg[SHORT_STRING];
1362 NNTP_DATA nntp_data;
1365 if (nntp_open_connection (serv) < 0)
1368 snprintf (msg, sizeof (msg),
1369 _("Loading list of all newsgroups on server %s..."),
1370 serv->conn->account.host);
1372 time (&serv->newgroups_time);
1373 nntp_data.nserv = serv;
1374 nntp_data.group = NULL;
1376 if (mutt_nntp_fetch (&nntp_data, "string_list_t\r\n", msg, NULL, add_group, serv, 0) < 0) {
1380 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1382 nntp_get_desc (&nntp_data, "*", msg, NULL);
1384 for (tmp = serv->list; tmp; tmp = tmp->next) {
1385 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1387 if (data && data->deleted && !data->rc) {
1388 nntp_delete_cache (data);
1389 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1393 nntp_save_cache_index (serv);
1395 mutt_clear_error ();
1400 * returns -1 if error ocurred while retrieving header,
1401 * number of articles which ones exist in context on success.
1403 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1407 /* if msgid is already in context, don't reload them */
1408 if (hash_find (ctx->id_hash, msgid))
1410 if (ctx->msgcount == ctx->hdrmax)
1411 mx_alloc_memory (ctx);
1412 ctx->hdrs[ctx->msgcount] = header_new();
1413 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1415 mutt_message (_("Fetching %s from server..."), msgid);
1416 ret = nntp_read_header (ctx, msgid, 0);
1417 /* since nntp_read_header() may set read flag, we must reset it */
1418 ctx->hdrs[ctx->msgcount]->read = 0;
1420 header_delete(&ctx->hdrs[ctx->msgcount]);
1423 mx_update_context (ctx, 1);
1433 unsigned int *child;
1436 static int check_children (char *s, void *c)
1438 #define cc ((CHILD_CONTEXT *) c)
1441 if (!s || (n = atoi (s)) == 0)
1443 for (i = 0; i < cc->ctx->msgcount; i++)
1444 if (cc->ctx->hdrs[i]->article_num == n)
1446 if (cc->num >= cc->max)
1447 p_realloc(&cc->child, cc->max += 25);
1448 cc->child[cc->num++] = n;
1454 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1456 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1458 int i, ret = 0, tmp = 0;
1461 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1462 !nntp_data->nserv->conn->account.host)
1464 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1466 if (!nntp_data->nserv->hasXPAT) {
1467 mutt_error (_("Server %s does not support this operation!"),
1468 nntp_data->nserv->conn->account.host);
1472 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1473 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1478 cc.child = p_new(unsigned int, 25);
1479 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1480 p_delete(&cc.child);
1483 /* dont try to read the xover cache. check_children() already
1484 * made sure that we dont have the article, so we need to visit
1485 * the server. Reading the cache at this point is also bad
1486 * because it would duplicate messages */
1487 if (option (OPTNEWSCACHE)) {
1489 unset_option (OPTNEWSCACHE);
1491 for (i = 0; i < cc.num; i++) {
1492 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1494 if (ctx->msgcount &&
1495 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1496 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1499 set_option (OPTNEWSCACHE);
1500 p_delete(&cc.child);
1504 static int nntp_is_magic (const char* path, struct stat* st) {
1505 url_scheme_t s = url_check_scheme (NONULL (path));
1506 return ((s == U_NNTP || s == U_NNTPS) ? M_NNTP : -1);
1509 static int acl_check_nntp (CONTEXT* ctx, int bit) {
1511 case ACL_INSERT: /* editing messages */
1512 case ACL_WRITE: /* change importance */
1514 case ACL_DELETE: /* (un)deletion */
1515 case ACL_SEEN: /* mark as read */
1522 mx_t const nntp_mx = {
1532 nntp_fastclose_mailbox,