2 * Copyright (C) 1998 Brandon Long <blong@fiction.net>
3 * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
4 * Copyright (C) 2000-2002 Vsevolod Volkov <vvv@mutt.org.ua>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "mutt_curses.h"
48 static unsigned int _checked = 0;
51 static void nntp_error (const char *where, const char *msg)
54 (debugfile, "nntp_error(): unexpected response in %s: %s\n", where,
59 static int nntp_auth (NNTP_SERVER * serv)
61 CONNECTION *conn = serv->conn;
63 unsigned char flags = conn->account.flags;
65 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
66 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
67 conn->account.flags = flags;
71 mutt_message _("Logging in...");
73 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
74 mutt_socket_write (conn, buf);
75 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
76 conn->account.flags = flags;
81 /* don't print the password unless we're at the ungodly debugging level */
82 if (debuglevel < M_SOCK_LOG_FULL)
83 dprint (M_SOCK_LOG_CMD, (debugfile, "> AUTHINFO PASS *\n"));
85 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
86 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
87 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
88 conn->account.flags = flags;
92 if (mutt_strncmp ("281", buf, 3)) {
93 conn->account.flags = flags;
94 mutt_error _("Login failed.");
103 static int nntp_connect_error (NNTP_SERVER * serv)
105 serv->status = NNTP_NONE;
106 mutt_socket_close (serv->conn);
107 mutt_error _("Server closed connection!");
113 static int nntp_connect_and_auth (NNTP_SERVER * serv)
115 CONNECTION *conn = serv->conn;
119 serv->status = NNTP_NONE;
121 if (mutt_socket_open (conn) < 0)
124 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
125 return nntp_connect_error (serv);
127 if (!mutt_strncmp ("200", buf, 3))
128 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
129 else if (!mutt_strncmp ("201", buf, 3))
130 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
132 mutt_socket_close (conn);
133 mutt_remove_trailing_ws (buf);
134 mutt_error ("%s", buf);
141 /* Tell INN to switch to mode reader if it isn't so. Ignore all
142 returned codes and messages. */
143 mutt_socket_write (conn, "MODE READER\r\n");
144 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
145 return nntp_connect_error (serv);
147 mutt_socket_write (conn, "STAT\r\n");
148 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
149 return nntp_connect_error (serv);
151 if (!(conn->account.flags & M_ACCT_USER) && mutt_strncmp ("480", buf, 3)) {
152 serv->status = NNTP_OK;
156 rc = nntp_auth (serv);
158 return nntp_connect_error (serv);
160 mutt_socket_close (conn);
161 serv->status = NNTP_BYE;
165 mutt_socket_close (conn);
166 mutt_error _("Login failed.");
171 serv->status = NNTP_OK;
175 static int nntp_attempt_features (NNTP_SERVER * serv)
177 char buf[LONG_STRING];
178 CONNECTION *conn = serv->conn;
180 mutt_socket_write (conn, "LISTGROUP\r\n");
181 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
182 return (nntp_connect_error (serv));
183 if (mutt_strncmp ("500", buf, 3))
184 serv->hasLISTGROUP = 1;
186 mutt_socket_write (conn, "XOVER\r\n");
187 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
188 return nntp_connect_error (serv);
189 if (mutt_strncmp ("500", buf, 3))
192 mutt_socket_write (conn, "XPAT\r\n");
193 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
194 return nntp_connect_error (serv);
195 if (mutt_strncmp ("500", buf, 3))
198 mutt_socket_write (conn, "XGTITLE +\r\n");
199 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
200 return nntp_connect_error (serv);
201 if (mutt_strncmp ("500", buf, 3))
202 serv->hasXGTITLE = 1;
204 if (!mutt_strncmp ("282", buf, 3)) {
206 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
207 return nntp_connect_error (serv);
208 } while (!(buf[0] == '.' && buf[1] == '\0'));
214 static int nntp_open_connection (NNTP_SERVER * serv)
216 if (serv->status == NNTP_OK)
218 if (serv->status == NNTP_BYE)
220 if (nntp_connect_and_auth (serv) < 0)
222 if (nntp_attempt_features (serv) < 0)
227 static int nntp_reconnect (NNTP_SERVER * serv)
229 char buf[SHORT_STRING];
231 mutt_socket_close (serv->conn);
234 if (nntp_connect_and_auth (serv) == 0)
237 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
238 serv->conn->account.host);
239 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
240 serv->status = NNTP_BYE;
246 /* Send data from line[LONG_STRING] and receive answer to same line */
247 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
249 char buf[LONG_STRING];
252 if (data->nserv->status == NNTP_BYE)
257 mutt_socket_write (data->nserv->conn, line);
259 else if (data->group) {
260 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
261 mutt_socket_write (data->nserv->conn, buf);
265 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
266 if (nntp_reconnect (data->nserv) < 0)
270 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
271 mutt_socket_write (data->nserv->conn, buf);
272 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
278 else if ((!mutt_strncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
282 strfcpy (line, buf, linelen);
287 * This function calls funct(*line, *data) for each received line,
288 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
291 * 1 - correct but not performed (may be, have to be continued),
292 * -1 - conection lost,
293 * -2 - invalid command or execution error,
294 * -3 - error in funct(*line, *data).
296 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, char *query, char *msg,
297 int (*funct) (char *, void *), void *data,
300 char buf[LONG_STRING];
308 strfcpy (buf, query, sizeof (buf));
309 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
318 inbuf = safe_malloc (sizeof (buf));
321 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
327 if (!lenbuf && buf[0] == '.') {
328 if (buf[1] == '\0') {
336 strfcpy (inbuf + lenbuf, p, sizeof (buf));
338 if (chunk >= sizeof (buf)) {
339 lenbuf += strlen (p);
343 if (msg && ReadInc && (line % ReadInc == 0)) {
345 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
347 mutt_message ("%s %d", msg, line);
350 if (ret == 0 && funct (inbuf, data) < 0)
355 safe_realloc (&inbuf, lenbuf + sizeof (buf));
364 static int nntp_read_tempfile (char *line, void *file)
366 FILE *f = (FILE *) file;
372 if (fputc ('\n', f) == EOF)
378 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
381 register char *p, *b;
382 register char *colon = NULL;
386 /* skip to next word */
388 while (*b && ((*b == ' ') || (*b == '\t')))
392 /* skip to end of word */
393 while (*p && (*p != ' ') && (*p != '\t')) {
405 nntp_get_status (ctx, h, p, atoi (colon));
406 if (h && h->article_num == 0 && mutt_strcmp (group, b) == 0)
407 h->article_num = atoi (colon);
415 * 1 if article not found
416 * -1 if read or write error on tempfile or socket
418 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
421 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
423 char buf[LONG_STRING];
424 char tempfile[_POSIX_PATH_MAX];
426 HEADER *h = ctx->hdrs[ctx->msgcount];
428 mutt_mktemp (tempfile);
429 if (!(f = safe_fopen (tempfile, "w+")))
433 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
435 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
437 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
441 dprint (1, (debugfile, "nntp_read_header: %s\n", buf));
445 return (ret == -1 ? -1 : 1);
448 h->article_num = article_num;
449 h->env = mutt_read_rfc822_header (f, h, 0, 0);
453 if (h->env->xref != NULL)
454 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
455 else if (h->article_num == 0 && msgid) {
456 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
457 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
458 h->article_num = atoi (buf + 4);
464 static int parse_description (char *line, void *n)
466 #define news ((NNTP_SERVER *) n)
467 register char *d = line;
472 while (*d && *d != '\t' && *d != ' ')
476 while (*d && (*d == '\t' || *d == ' '))
478 dprint (2, (debugfile, "group: %s, desc: %s\n", line, d));
479 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
480 mutt_strcmp (d, data->desc)) {
482 data->desc = safe_strdup (d);
488 static void nntp_get_desc (NNTP_DATA * data, char *mask, char *msg)
492 if (!option (OPTLOADDESC) || !data || !data->nserv)
495 /* Get newsgroup description, if we can */
496 if (data->nserv->hasXGTITLE)
497 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
499 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
500 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) !=
503 nntp_error ("nntp_get_desc()", buf);
509 * XOVER returns a tab separated list of:
510 * id|subject|from|date|Msgid|references|bytes|lines|xref
512 * This has to duplicate some of the functionality of
513 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
514 * a limited number of headers which are "parsed" by placement in the list)
516 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
518 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
522 hdr->env = mutt_new_envelope ();
523 hdr->env->newsgroups = safe_strdup (nntp_data->group);
524 hdr->content = mutt_new_body ();
525 hdr->content->type = TYPETEXT;
526 hdr->content->subtype = safe_strdup ("plain");
527 hdr->content->encoding = ENC7BIT;
528 hdr->content->disposition = DISPINLINE;
529 hdr->content->length = -1;
532 for (x = 0; !done && x < 9; x++) {
533 /* if from file, need to skip newline character */
534 while (*p && *p != '\n' && *p != '\t')
543 hdr->article_num = atoi (b);
544 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
547 hdr->env->subject = safe_strdup (b);
548 /* Now we need to do the things which would normally be done in
549 * mutt_read_rfc822_header() */
550 if (hdr->env->subject) {
551 regmatch_t pmatch[1];
553 rfc2047_decode (&hdr->env->subject);
555 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
556 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
558 hdr->env->real_subj = hdr->env->subject;
562 rfc822_free_address (&hdr->env->from);
563 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
564 rfc2047_decode_adrlist (hdr->env->from);
567 hdr->date_sent = mutt_parse_date (b, hdr);
568 hdr->received = hdr->date_sent;
571 FREE (&hdr->env->message_id);
572 hdr->env->message_id = safe_strdup (b);
575 mutt_free_list (&hdr->env->references);
576 hdr->env->references = mutt_parse_references (b, 0);
579 hdr->content->length = atoi (b);
582 hdr->lines = atoi (b);
586 FREE (&hdr->env->xref);
587 b = b + 6; /* skips the "Xref: " */
588 hdr->env->xref = safe_strdup (b);
589 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
603 unsigned short *messages;
607 #define fc ((FETCH_CONTEXT *) c)
608 static int _nntp_fetch_numbers (unsigned int num, void *c)
610 if (num < fc->base || num > fc->last)
612 fc->messages[num - fc->base] = 1;
615 static int nntp_fetch_numbers (char *line, void *c)
619 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
622 static int add_xover_line (char *line, void *c)
624 unsigned int num, total;
625 CONTEXT *ctx = fc->ctx;
626 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
631 if (ctx->msgcount >= ctx->hdrmax)
632 mx_alloc_memory (ctx);
633 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
634 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
636 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
637 num = ctx->hdrs[ctx->msgcount]->article_num;
639 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
641 if (num > data->lastLoaded)
642 data->lastLoaded = num;
643 num = num - fc->first + 1;
644 total = fc->last - fc->first + 1;
645 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
646 mutt_message ("%s %d/%d", fc->msg, num, total);
649 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
656 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
659 char buf[HUGE_STRING];
660 char *msg = _("Fetching message headers...");
661 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
665 unsigned int current;
669 /* if empty group or nothing to do */
670 if (!last || first > last)
673 /* fetch list of articles */
674 mutt_message _("Fetching list of articles...");
679 fc.messages = safe_calloc (last - first + 1, sizeof (unsigned short));
680 if (nntp_data->nserv->hasLISTGROUP) {
681 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
682 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) !=
684 mutt_error (_("LISTGROUP command failed: %s"), buf);
687 nntp_error ("nntp_fetch_headers()", buf);
694 /* mutt_nntp_query() issues a 'GROUP nntp_data->group'
695 * command on its own if !*buf */
697 mutt_nntp_query (nntp_data, buf, sizeof (buf));
698 if (sscanf (buf + 4, "%d %u %u %s", &num, &fc.first, &fc.last, buf) != 4) {
699 mutt_error (_("GROUP command failed: %s"), buf);
704 for (num = fc.first; num < fc.last; num++)
705 _nntp_fetch_numbers (num, &fc);
709 /* CACHE: must be loaded xover cache here */
710 num = nntp_data->lastCached - first + 1;
711 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
712 nntp_cache_expand (buf, nntp_data->cache);
713 mutt_message _("Fetching headers from cache...");
715 if ((f = safe_fopen (buf, "r"))) {
718 /* counting number of lines */
719 while (fgets (buf, sizeof (buf), f) != NULL)
722 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
724 oldmsgcount = ctx->msgcount;
726 fc.last = first + num - 1;
728 while (fgets (buf, sizeof (buf), f) != NULL)
729 add_xover_line (buf, &fc);
731 nntp_data->lastLoaded = fc.last;
733 if (ctx->msgcount > oldmsgcount)
734 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
737 nntp_delete_cache (nntp_data);
739 num = last - first + 1;
746 * Without XOVER, we have to fetch each article header and parse
747 * it. With XOVER, we ask for all of them
750 if (nntp_data->nserv->hasXOVER) {
751 oldmsgcount = ctx->msgcount;
755 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
756 ret = mutt_nntp_fetch (nntp_data, buf, NULL, add_xover_line, &fc, 0);
757 if (ctx->msgcount > oldmsgcount)
758 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
760 mutt_error (_("XOVER command failed: %s"), buf);
762 nntp_error ("nntp_fetch_headers()", buf);
770 for (current = first; current <= last; current++) {
773 ret = current - first + 1;
774 mutt_message ("%s %d/%d", msg, ret, num);
776 if (!fc.messages[current - fc.base])
779 if (ctx->msgcount >= ctx->hdrmax)
780 mx_alloc_memory (ctx);
781 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
782 h->index = ctx->msgcount;
784 ret = nntp_read_header (ctx, NULL, current);
785 if (ret == 0) { /* Got article. Fetch next header */
786 nntp_get_status (ctx, h, NULL, h->article_num);
788 mx_update_context (ctx, 1);
791 mutt_free_header (&h); /* skip it */
797 if (current > nntp_data->lastLoaded)
798 nntp_data->lastLoaded = current;
801 nntp_data->lastLoaded = last;
807 * currently, nntp "mailbox" is "newsgroup"
809 int nntp_open_mailbox (CONTEXT * ctx)
811 NNTP_DATA *nntp_data;
813 char buf[HUGE_STRING];
814 char server[LONG_STRING];
819 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
820 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
826 nntp_expand_path (server, sizeof (server), &acct);
827 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
830 CurrentNewsSrv = serv;
832 /* create NNTP-specific state struct if nof found in list */
833 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
834 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (buf) + 1);
835 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
836 strcpy (nntp_data->group, buf);
837 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
838 nntp_add_to_list (serv, nntp_data);
840 ctx->data = nntp_data;
841 nntp_data->nserv = serv;
843 mutt_message (_("Selecting %s..."), nntp_data->group);
845 if (!nntp_data->desc) {
846 nntp_get_desc (nntp_data, nntp_data->group, NULL);
848 nntp_save_cache_index (serv);
852 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
854 nntp_error ("nntp_open_mailbox()", buf);
859 if (mutt_strncmp ("211", buf, 3)) {
860 LIST *l = serv->list;
862 /* GROUP command failed */
863 if (!mutt_strncmp ("411", buf, 3)) {
864 mutt_error (_("Newsgroup %s not found on server %s"),
865 nntp_data->group, serv->conn->account.host);
867 /* CACHE: delete cache and line from .index */
868 nntp_delete_cache (nntp_data);
869 hash_delete (serv->newsgroups, nntp_data->group, NULL,
871 while (l && l->data != (void *) nntp_data)
882 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
883 &nntp_data->lastMessage, buf);
885 nntp_data->deleted = 0;
887 time (&serv->check_time);
890 * Check for max adding context. If it is greater than $nntp_context,
891 * strip off extra articles
893 first = nntp_data->firstMessage;
894 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
895 first = nntp_data->lastMessage - NntpContext + 1;
897 nntp_data->lastLoaded = first - 1;
898 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
901 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
903 char buf[LONG_STRING];
904 char path[_POSIX_PATH_MAX];
906 char *m = _("Fetching message...");
909 /* see if we already have the message in our cache */
911 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
914 /* if everything is fine, assign msg->fp and return */
915 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
916 (msg->fp = fopen (cache->path, "r")))
919 /* clear the previous entry */
920 unlink (cache->path);
925 cache->index = ctx->hdrs[msgno]->index;
927 cache->path = safe_strdup (path);
928 if (!(msg->fp = safe_fopen (path, "w+"))) {
933 if (ctx->hdrs[msgno]->article_num == 0)
934 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
935 ctx->hdrs[msgno]->env->message_id);
937 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
938 ctx->hdrs[msgno]->article_num);
940 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, m, nntp_read_tempfile,
941 msg->fp, ctx->tagged);
943 mutt_error (_("Article %d not found on server"),
944 ctx->hdrs[msgno]->article_num);
945 dprint (1, (debugfile, "nntp_fetch_message: %s\n", buf));
955 mutt_free_envelope (&ctx->hdrs[msgno]->env);
956 ctx->hdrs[msgno]->env =
957 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
958 /* fix content length */
959 fseek (msg->fp, 0, SEEK_END);
960 ctx->hdrs[msgno]->content->length = ftell (msg->fp) -
961 ctx->hdrs[msgno]->content->offset;
963 /* this is called in mutt before the open which fetches the message,
964 * which is probably wrong, but we just call it again here to handle
965 * the problem instead of fixing it.
967 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
969 /* These would normally be updated in mx_update_context(), but the
970 * full headers aren't parsed with XOVER, so the information wasn't
973 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
974 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
975 #endif /* HAVE_PGP || HAVE_SMIME */
984 int nntp_post (const char *msg)
986 char buf[LONG_STRING];
989 NNTP_DATA *nntp_data;
991 if (Context && Context->magic == M_NNTP)
992 nntp_data = (NNTP_DATA *) Context->data;
994 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
995 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
996 mutt_error (_("Can't post article. No connection to news server."));
999 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1002 if (!(f = safe_fopen (msg, "r"))) {
1003 mutt_error (_("Can't post article. Unable to open %s"), msg);
1007 strfcpy (buf, "POST\r\n", sizeof (buf));
1008 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1009 mutt_error (_("Can't post article. Connection to %s lost."),
1010 nntp_data->nserv->conn->account.host);
1013 if (buf[0] != '3') {
1014 mutt_error (_("Can't post article: %s"), buf);
1020 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1022 if (buf[len - 1] == '\n') {
1023 buf[len - 1] = '\r';
1029 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1031 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1035 if (buf[strlen (buf) - 1] != '\n')
1036 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1037 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1038 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1039 mutt_error (_("Can't post article. Connection to %s lost."),
1040 nntp_data->nserv->conn->account.host);
1043 if (buf[0] != '2') {
1044 mutt_error (_("Can't post article: %s"), buf);
1051 /* nntp_logout_all: close all open connections. */
1052 void nntp_logout_all (void)
1054 char buf[LONG_STRING];
1058 conn = mutt_socket_head ();
1063 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1064 mutt_message (_("Closing connection to %s..."), conn->account.host);
1065 mutt_socket_write (conn, "QUIT\r\n");
1066 mutt_socket_readln (buf, sizeof (buf), conn);
1067 mutt_clear_error ();
1068 mutt_socket_close (conn);
1070 mutt_socket_free (tmp);
1077 static void nntp_free_acache (NNTP_DATA * data)
1081 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1082 if (data->acache[i].path) {
1083 unlink (data->acache[i].path);
1084 FREE (&data->acache[i].path);
1089 void nntp_delete_data (void *p)
1091 NNTP_DATA *data = (NNTP_DATA *) p;
1095 FREE (&data->entries);
1097 FREE (&data->cache);
1098 nntp_free_acache (data);
1102 int nntp_sync_mailbox (CONTEXT * ctx)
1104 NNTP_DATA *data = ctx->data;
1106 /* CACHE: update cache and .index files */
1107 if ((option (OPTSAVEUNSUB) || data->subscribed))
1108 nntp_save_cache_group (ctx);
1109 nntp_free_acache (data);
1111 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1115 void nntp_fastclose_mailbox (CONTEXT * ctx)
1117 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1121 nntp_free_acache (data);
1122 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1124 nntp_save_cache_index (data->nserv);
1125 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1127 nntp_delete_data (data);
1130 /* commit changes and terminate connection */
1131 int nntp_close_mailbox (CONTEXT * ctx)
1135 mutt_message _("Quitting newsgroup...");
1138 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1141 if (data->nserv && data->nserv->conn && ctx->unread) {
1142 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1144 mutt_newsgroup_catchup (data->nserv, data->group);
1149 nntp_sync_mailbox (ctx);
1150 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1153 news = ((NNTP_DATA *) ctx->data)->nserv;
1154 newsrc_gen_entries (ctx);
1155 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1156 mutt_newsrc_update (news);
1158 mutt_clear_error ();
1162 /* use the GROUP command to poll for new mail */
1163 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1165 char buf[LONG_STRING];
1168 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1172 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1174 nntp_error ("nntp_check_mailbox()", buf);
1178 if (mutt_strncmp ("211", buf, 3)) {
1180 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1182 nntp_error ("nntp_check_mailbox()", buf);
1187 if (!mutt_strncmp ("211", buf, 3)) {
1191 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1192 nntp_data->firstMessage = first;
1193 nntp_data->lastMessage = last;
1194 if (ctx && last > nntp_data->lastLoaded) {
1195 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1196 time (&nntp_data->nserv->check_time);
1199 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1200 nntp_data->unread = count;
1202 mutt_newsgroup_stat (nntp_data);
1203 /* active was renumbered? */
1204 if (last < nntp_data->lastLoaded) {
1205 if (!nntp_data->max) {
1206 nntp_data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1209 nntp_data->lastCached = 0;
1211 nntp_data->entries[0].first = 1;
1212 nntp_data->entries[0].last = 0;
1216 time (&nntp_data->nserv->check_time);
1220 int nntp_check_mailbox (CONTEXT * ctx)
1222 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1225 static int add_group (char *buf, void *serv)
1227 #define s ((NNTP_SERVER *) serv)
1228 char group[LONG_STRING], mod, desc[HUGE_STRING];
1230 NNTP_DATA *nntp_data;
1233 _checked = n; /* _checked have N, where N = number of groups */
1234 if (!buf) /* at EOF must be zerouth */
1241 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1244 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1246 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
1247 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1248 strcpy (nntp_data->group, group);
1249 nntp_data->nserv = s;
1250 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1251 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1252 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1253 nntp_add_to_list (s, nntp_data);
1255 nntp_data->deleted = 0;
1256 nntp_data->firstMessage = first;
1257 nntp_data->lastMessage = last;
1259 nntp_data->allowed = 1;
1261 nntp_data->allowed = 0;
1262 if (nntp_data->desc)
1263 FREE (&nntp_data->desc);
1265 nntp_data->desc = safe_strdup (desc);
1266 if (nntp_data->rc || nntp_data->lastCached)
1267 mutt_newsgroup_stat (nntp_data);
1268 else if (nntp_data->lastMessage &&
1269 nntp_data->firstMessage <= nntp_data->lastMessage)
1270 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1272 nntp_data->unread = 0;
1278 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1280 char buf[LONG_STRING];
1281 NNTP_DATA nntp_data;
1287 if (!serv || !serv->newgroups_time)
1290 if (nntp_open_connection (serv) < 0)
1293 /* check subscribed groups for new news */
1294 if (option (OPTSHOWNEWNEWS)) {
1295 mutt_message _("Checking for new messages...");
1297 for (l = serv->list; l; l = l->next) {
1298 serv->check_time = 0; /* really check! */
1299 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1300 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1306 mutt_message _("Checking for new newsgroups...");
1308 now = serv->newgroups_time;
1309 time (&serv->newgroups_time);
1311 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1312 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1313 t->tm_min, t->tm_sec);
1314 nntp_data.nserv = serv;
1315 if (Context && Context->magic == M_NNTP)
1316 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1318 nntp_data.group = NULL;
1320 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1321 add_group, serv, 0) != 0) {
1323 nntp_error ("nntp_check_newgroups()", buf);
1328 mutt_message _("Loading descriptions...");
1333 emp.next = serv->list;
1337 ((NNTP_DATA *) l->data)->new = 1;
1338 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1342 nntp_save_cache_index (serv);
1343 mutt_clear_error ();
1347 /* Load list of all newsgroups from cache ALL */
1348 int nntp_get_cache_all (NNTP_SERVER * serv)
1350 char buf[HUGE_STRING];
1353 nntp_cache_expand (buf, serv->cache);
1354 if ((f = safe_fopen (buf, "r"))) {
1357 while (fgets (buf, sizeof (buf), f) != NULL) {
1358 if (ReadInc && (i % ReadInc == 0))
1359 mutt_message (_("Loading list from cache... %d"), i);
1360 add_group (buf, serv);
1363 add_group (NULL, NULL);
1365 mutt_clear_error ();
1369 FREE (&serv->cache);
1374 /* Load list of all newsgroups from active */
1375 int nntp_get_active (NNTP_SERVER * serv)
1377 char msg[SHORT_STRING];
1378 NNTP_DATA nntp_data;
1381 if (nntp_open_connection (serv) < 0)
1384 snprintf (msg, sizeof (msg),
1385 _("Loading list of all newsgroups on server %s..."),
1386 serv->conn->account.host);
1388 time (&serv->newgroups_time);
1389 nntp_data.nserv = serv;
1390 nntp_data.group = NULL;
1392 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0) {
1394 nntp_error ("nntp_get_active()", "LIST\r\n");
1399 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1401 nntp_get_desc (&nntp_data, "*", msg);
1403 for (tmp = serv->list; tmp; tmp = tmp->next) {
1404 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1406 if (data && data->deleted && !data->rc) {
1407 nntp_delete_cache (data);
1408 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1412 nntp_save_cache_index (serv);
1414 mutt_clear_error ();
1419 * returns -1 if error ocurred while retrieving header,
1420 * number of articles which ones exist in context on success.
1422 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1426 /* if msgid is already in context, don't reload them */
1427 if (hash_find (ctx->id_hash, msgid))
1429 if (ctx->msgcount == ctx->hdrmax)
1430 mx_alloc_memory (ctx);
1431 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1432 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1434 mutt_message (_("Fetching %s from server..."), msgid);
1435 ret = nntp_read_header (ctx, msgid, 0);
1436 /* since nntp_read_header() may set read flag, we must reset it */
1437 ctx->hdrs[ctx->msgcount]->read = 0;
1439 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1442 mx_update_context (ctx, 1);
1452 unsigned int *child;
1455 static int check_children (char *s, void *c)
1457 #define cc ((CHILD_CONTEXT *) c)
1460 if (!s || (n = atoi (s)) == 0)
1462 for (i = 0; i < cc->ctx->msgcount; i++)
1463 if (cc->ctx->hdrs[i]->article_num == n)
1465 if (cc->num >= cc->max)
1466 safe_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1467 cc->child[cc->num++] = n;
1473 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1475 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1477 int i, ret = 0, tmp = 0;
1480 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1481 !nntp_data->nserv->conn->account.host)
1483 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1485 if (!nntp_data->nserv->hasXPAT) {
1486 mutt_error (_("Server %s does not support this operation!"),
1487 nntp_data->nserv->conn->account.host);
1491 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1492 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1497 cc.child = safe_malloc (sizeof (unsigned int) * 25);
1498 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0)) {
1502 /* dont try to read the xover cache. check_children() already
1503 * made sure that we dont have the article, so we need to visit
1504 * the server. Reading the cache at this point is also bad
1505 * because it would duplicate messages */
1506 if (option (OPTNEWSCACHE)) {
1508 unset_option (OPTNEWSCACHE);
1510 for (i = 0; i < cc.num; i++) {
1511 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1513 if (ctx->msgcount &&
1514 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1515 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1518 set_option (OPTNEWSCACHE);