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)
53 dprint (1, (debugfile, "nntp_error(): unexpected response in %s: %s\n", where, msg));
57 static int nntp_auth (NNTP_SERVER *serv)
59 CONNECTION *conn = serv->conn;
61 unsigned char flags = conn->account.flags;
63 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
64 mutt_account_getpass (&conn->account) || !conn->account.pass[0])
66 conn->account.flags = flags;
70 mutt_message _("Logging in...");
72 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
73 mutt_socket_write (conn, buf);
74 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)
89 conn->account.flags = flags;
93 if (mutt_strncmp ("281", buf, 3))
95 conn->account.flags = flags;
96 mutt_error _("Login failed.");
104 static int nntp_connect_error (NNTP_SERVER *serv)
106 serv->status = NNTP_NONE;
107 mutt_socket_close (serv->conn);
108 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);
133 mutt_socket_close (conn);
134 mutt_remove_trailing_ws (buf);
135 mutt_error ("%s", buf);
142 /* Tell INN to switch to mode reader if it isn't so. Ignore all
143 returned codes and messages. */
144 mutt_socket_write (conn, "MODE READER\r\n");
145 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
146 return nntp_connect_error (serv);
148 mutt_socket_write (conn, "STAT\r\n");
149 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
150 return nntp_connect_error (serv);
152 if (!(conn->account.flags & M_ACCT_USER) && mutt_strncmp ("480", buf, 3))
154 serv->status = NNTP_OK;
158 rc = nntp_auth (serv);
160 return nntp_connect_error (serv);
163 mutt_socket_close (conn);
164 serv->status = NNTP_BYE;
169 mutt_socket_close (conn);
170 mutt_error _("Login failed.");
174 serv->status = NNTP_OK;
178 static int nntp_attempt_features (NNTP_SERVER *serv)
180 char buf[LONG_STRING];
181 CONNECTION *conn = serv->conn;
183 mutt_socket_write (conn, "LISTGROUP\r\n");
184 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
185 return (nntp_connect_error (serv));
186 if (mutt_strncmp ("500", buf, 3))
187 serv->hasLISTGROUP = 1;
189 mutt_socket_write (conn, "XOVER\r\n");
190 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
191 return nntp_connect_error (serv);
192 if (mutt_strncmp ("500", buf, 3))
195 mutt_socket_write (conn, "XPAT\r\n");
196 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
197 return nntp_connect_error (serv);
198 if (mutt_strncmp ("500", buf, 3))
201 mutt_socket_write (conn, "XGTITLE +\r\n");
202 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
203 return nntp_connect_error (serv);
204 if (mutt_strncmp ("500", buf, 3))
205 serv->hasXGTITLE = 1;
207 if (!mutt_strncmp ("282", buf, 3))
211 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
212 return nntp_connect_error (serv);
213 } while (!(buf[0] == '.' && buf[1] == '\0'));
219 static int nntp_open_connection (NNTP_SERVER *serv)
221 if (serv->status == NNTP_OK)
223 if (serv->status == NNTP_BYE)
225 if (nntp_connect_and_auth (serv) < 0)
227 if (nntp_attempt_features (serv) < 0)
232 static int nntp_reconnect (NNTP_SERVER *serv)
234 char buf[SHORT_STRING];
236 mutt_socket_close (serv->conn);
240 if (nntp_connect_and_auth (serv) == 0)
243 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
244 serv->conn->account.host);
245 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)
266 mutt_socket_write (data->nserv->conn, line);
268 else if (data->group)
270 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
271 mutt_socket_write (data->nserv->conn, buf);
275 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
277 if (nntp_reconnect (data->nserv) < 0)
282 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
283 mutt_socket_write (data->nserv->conn, buf);
284 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
290 else if ((!mutt_strncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
294 strfcpy (line, buf, linelen);
299 * This function calls funct(*line, *data) for each received line,
300 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
303 * 1 - correct but not performed (may be, have to be continued),
304 * -1 - conection lost,
305 * -2 - invalid command or execution error,
306 * -3 - error in funct(*line, *data).
308 static int mutt_nntp_fetch (NNTP_DATA *nntp_data, char *query, char *msg,
309 int (*funct) (char *, void *), void *data, int tagged)
311 char buf[LONG_STRING];
320 strfcpy (buf, query, sizeof (buf));
321 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
330 inbuf = safe_malloc (sizeof (buf));
334 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
340 if (!lenbuf && buf[0] == '.')
351 strfcpy (inbuf + lenbuf, p, sizeof (buf));
353 if (chunk >= sizeof (buf))
355 lenbuf += strlen (p);
360 if (msg && ReadInc && (line % ReadInc == 0)) {
362 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
364 mutt_message ("%s %d", msg, line);
367 if (ret == 0 && funct (inbuf, data) < 0)
372 safe_realloc (&inbuf, lenbuf + sizeof (buf));
381 static int nntp_read_tempfile (char *line, void *file)
383 FILE *f = (FILE *)file;
390 if (fputc ('\n', f) == EOF)
396 static void nntp_parse_xref (CONTEXT *ctx, char *group, char *xref, HEADER *h)
398 register char *p, *b;
399 register char *colon = NULL;
404 /* skip to next word */
406 while (*b && ((*b == ' ') || (*b == '\t'))) b++;
409 /* skip to end of word */
410 while (*p && (*p != ' ') && (*p != '\t'))
425 nntp_get_status (ctx, h, p, atoi(colon));
426 if (h && h->article_num == 0 && mutt_strcmp (group, b) == 0)
427 h->article_num = atoi(colon);
435 * 1 if article not found
436 * -1 if read or write error on tempfile or socket
438 static int nntp_read_header (CONTEXT *ctx, const char *msgid, int article_num)
440 NNTP_DATA *nntp_data = ((NNTP_DATA *)ctx->data);
442 char buf[LONG_STRING];
443 char tempfile[_POSIX_PATH_MAX];
445 HEADER *h = ctx->hdrs[ctx->msgcount];
447 mutt_mktemp (tempfile);
448 if (!(f = safe_fopen (tempfile, "w+")))
452 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
454 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
456 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
461 dprint(1, (debugfile, "nntp_read_header: %s\n", buf));
465 return (ret == -1 ? -1 : 1);
468 h->article_num = article_num;
469 h->env = mutt_read_rfc822_header (f, h, 0, 0);
473 if (h->env->xref != NULL)
474 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
475 else if (h->article_num == 0 && msgid)
477 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
478 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
479 h->article_num = atoi (buf + 4);
485 static int parse_description (char *line, void *n)
487 #define news ((NNTP_SERVER *) n)
488 register char *d = line;
493 while (*d && *d != '\t' && *d != ' ') d++;
496 while (*d && (*d == '\t' || *d == ' ')) d++;
497 dprint (2, (debugfile, "group: %s, desc: %s\n", line, d));
498 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
499 mutt_strcmp (d, data->desc))
502 data->desc = safe_strdup (d);
508 static void nntp_get_desc (NNTP_DATA *data, char *mask, char *msg)
512 if (!option (OPTLOADDESC) || !data || !data->nserv)
515 /* Get newsgroup description, if we can */
516 if (data->nserv->hasXGTITLE)
517 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
519 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
520 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) != 0)
523 nntp_error ("nntp_get_desc()", buf);
529 * XOVER returns a tab separated list of:
530 * id|subject|from|date|Msgid|references|bytes|lines|xref
532 * This has to duplicate some of the functionality of
533 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
534 * a limited number of headers which are "parsed" by placement in the list)
536 static int nntp_parse_xover (CONTEXT *ctx, char *buf, HEADER *hdr)
538 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
542 hdr->env = mutt_new_envelope();
543 hdr->env->newsgroups = safe_strdup (nntp_data->group);
544 hdr->content = mutt_new_body();
545 hdr->content->type = TYPETEXT;
546 hdr->content->subtype = safe_strdup ("plain");
547 hdr->content->encoding = ENC7BIT;
548 hdr->content->disposition = DISPINLINE;
549 hdr->content->length = -1;
552 for (x = 0; !done && x < 9; x++)
554 /* if from file, need to skip newline character */
555 while (*p && *p != '\n' && *p != '\t') p++;
563 hdr->article_num = atoi (b);
564 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
567 hdr->env->subject = safe_strdup (b);
568 /* Now we need to do the things which would normally be done in
569 * mutt_read_rfc822_header() */
570 if (hdr->env->subject)
572 regmatch_t pmatch[1];
574 rfc2047_decode (&hdr->env->subject);
576 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
577 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
579 hdr->env->real_subj = hdr->env->subject;
583 rfc822_free_address (&hdr->env->from);
584 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
585 rfc2047_decode_adrlist (hdr->env->from);
588 hdr->date_sent = mutt_parse_date (b, hdr);
589 hdr->received = hdr->date_sent;
592 FREE (&hdr->env->message_id);
593 hdr->env->message_id = safe_strdup (b);
596 mutt_free_list (&hdr->env->references);
597 hdr->env->references = mutt_parse_references (b, 0);
600 hdr->content->length = atoi (b);
603 hdr->lines = atoi (b);
607 FREE (&hdr->env->xref);
608 b = b + 6; /* skips the "Xref: " */
609 hdr->env->xref = safe_strdup (b);
610 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
625 unsigned short *messages;
629 #define fc ((FETCH_CONTEXT *) c)
630 static int _nntp_fetch_numbers (unsigned int num ,void* c) {
631 if (num < fc->base || num > fc->last)
633 fc->messages[num - fc->base] = 1;
636 static int nntp_fetch_numbers (char *line, void *c)
640 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
643 static int add_xover_line (char *line, void *c)
645 unsigned int num, total;
646 CONTEXT *ctx = fc->ctx;
647 NNTP_DATA *data = (NNTP_DATA *)ctx->data;
652 if (ctx->msgcount >= ctx->hdrmax)
653 mx_alloc_memory (ctx);
654 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
655 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
657 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
658 num = ctx->hdrs[ctx->msgcount]->article_num;
660 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base])
663 if (num > data->lastLoaded)
664 data->lastLoaded = num;
665 num = num - fc->first + 1;
666 total = fc->last - fc->first + 1;
667 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
668 mutt_message ("%s %d/%d", fc->msg, num, total);
671 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
677 static int nntp_fetch_headers (CONTEXT *ctx, unsigned int first,
680 char buf[HUGE_STRING];
681 char *msg = _("Fetching message headers...");
682 NNTP_DATA *nntp_data = ((NNTP_DATA *)ctx->data);
686 unsigned int current;
690 /* if empty group or nothing to do */
691 if (!last || first > last)
694 /* fetch list of articles */
695 mutt_message _("Fetching list of articles...");
699 fc.messages = safe_calloc (last - first + 1, sizeof (unsigned short));
700 if (nntp_data->nserv->hasLISTGROUP) {
701 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
702 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) != 0)
704 mutt_error (_("LISTGROUP command failed: %s"), buf);
707 nntp_error ("nntp_fetch_headers()", buf);
713 /* mutt_nntp_query() issues a 'GROUP nntp_data->group'
714 * command on its own if !*buf */
716 mutt_nntp_query (nntp_data, buf, sizeof (buf));
717 if (sscanf (buf + 4, "%d %d %d %s", &num, &fc.first, &fc.last, buf) != 4) {
718 mutt_error (_("GROUP command failed: %s"), buf);
722 for (num = fc.first; num < fc.last; num++)
723 _nntp_fetch_numbers (num, &fc);
727 /* CACHE: must be loaded xover cache here */
728 num = nntp_data->lastCached - first + 1;
729 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0)
731 nntp_cache_expand (buf, nntp_data->cache);
732 mutt_message _("Fetching headers from cache...");
733 if ((f = safe_fopen (buf, "r")))
737 /* counting number of lines */
738 while (fgets (buf, sizeof (buf), f) != NULL)
741 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
743 oldmsgcount = ctx->msgcount;
745 fc.last = first + num - 1;
747 while (fgets (buf, sizeof (buf), f) != NULL)
748 add_xover_line (buf, &fc);
750 nntp_data->lastLoaded = fc.last;
752 if (ctx->msgcount > oldmsgcount)
753 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
756 nntp_delete_cache (nntp_data);
758 num = last - first + 1;
766 * Without XOVER, we have to fetch each article header and parse
767 * it. With XOVER, we ask for all of them
770 if (nntp_data->nserv->hasXOVER)
772 oldmsgcount = ctx->msgcount;
776 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
777 ret = mutt_nntp_fetch (nntp_data, buf, NULL, add_xover_line, &fc, 0);
778 if (ctx->msgcount > oldmsgcount)
779 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
782 mutt_error (_("XOVER command failed: %s"), buf);
784 nntp_error ("nntp_fetch_headers()", buf);
792 for (current = first; current <= last; current++)
796 ret = current - first + 1;
797 mutt_message ("%s %d/%d", msg, ret, num);
799 if (!fc.messages[current - fc.base])
802 if (ctx->msgcount >= ctx->hdrmax)
803 mx_alloc_memory (ctx);
804 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
805 h->index = ctx->msgcount;
807 ret = nntp_read_header (ctx, NULL, current);
808 if (ret == 0) /* Got article. Fetch next header */
810 nntp_get_status (ctx, h, NULL, h->article_num);
812 mx_update_context (ctx, 1);
815 mutt_free_header (&h); /* skip it */
822 if (current > nntp_data->lastLoaded)
823 nntp_data->lastLoaded = current;
826 nntp_data->lastLoaded = last;
832 * currently, nntp "mailbox" is "newsgroup"
834 int nntp_open_mailbox (CONTEXT *ctx)
836 NNTP_DATA *nntp_data;
838 char buf[HUGE_STRING];
839 char server[LONG_STRING];
844 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf)
846 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
852 nntp_expand_path (server, sizeof (server), &acct);
853 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
856 CurrentNewsSrv = serv;
858 /* create NNTP-specific state struct if nof found in list */
859 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL)
861 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (buf) + 1);
862 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
863 strcpy (nntp_data->group, buf);
864 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
865 nntp_add_to_list (serv, nntp_data);
867 ctx->data = nntp_data;
868 nntp_data->nserv = serv;
870 mutt_message (_("Selecting %s..."), nntp_data->group);
872 if (!nntp_data->desc)
874 nntp_get_desc (nntp_data, nntp_data->group, NULL);
876 nntp_save_cache_index (serv);
880 if (mutt_nntp_query (nntp_data, buf, sizeof(buf)) < 0)
883 nntp_error ("nntp_open_mailbox()", buf);
888 if (mutt_strncmp ("211", buf, 3))
890 LIST *l = serv->list;
892 /* GROUP command failed */
893 if (!mutt_strncmp ("411", buf, 3))
895 mutt_error (_("Newsgroup %s not found on server %s"),
896 nntp_data->group, serv->conn->account.host);
898 /* CACHE: delete cache and line from .index */
899 nntp_delete_cache (nntp_data);
900 hash_delete (serv->newsgroups, nntp_data->group, NULL, nntp_delete_data);
901 while (l && l->data != (void *) nntp_data) l = l->next;
911 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
912 &nntp_data->lastMessage, buf);
914 nntp_data->deleted = 0;
916 time (&serv->check_time);
919 * Check for max adding context. If it is greater than $nntp_context,
920 * strip off extra articles
922 first = nntp_data->firstMessage;
923 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
924 first = nntp_data->lastMessage - NntpContext + 1;
926 nntp_data->lastLoaded = first - 1;
927 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
930 int nntp_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
932 char buf[LONG_STRING];
933 char path[_POSIX_PATH_MAX];
935 char *m = _("Fetching message...");
938 /* see if we already have the message in our cache */
939 cache = &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index % NNTP_CACHE_LEN];
941 /* if everything is fine, assign msg->fp and return */
942 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
943 (msg->fp = fopen (cache->path, "r")))
946 /* clear the previous entry */
947 unlink (cache->path);
952 cache->index = ctx->hdrs[msgno]->index;
954 cache->path = safe_strdup (path);
955 if (!(msg->fp = safe_fopen (path, "w+")))
961 if (ctx->hdrs[msgno]->article_num == 0)
962 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
963 ctx->hdrs[msgno]->env->message_id);
965 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
966 ctx->hdrs[msgno]->article_num);
968 ret = mutt_nntp_fetch ((NNTP_DATA *)ctx->data, buf, m, nntp_read_tempfile,
969 msg->fp, ctx->tagged);
972 mutt_error (_("Article %d not found on server"),
973 ctx->hdrs[msgno]->article_num);
974 dprint (1, (debugfile, "nntp_fetch_message: %s\n", buf));
985 mutt_free_envelope (&ctx->hdrs[msgno]->env);
986 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
987 /* fix content length */
988 fseek(msg->fp, 0, SEEK_END);
989 ctx->hdrs[msgno]->content->length = ftell (msg->fp) -
990 ctx->hdrs[msgno]->content->offset;
992 /* this is called in mutt before the open which fetches the message,
993 * which is probably wrong, but we just call it again here to handle
994 * the problem instead of fixing it.
996 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
998 /* These would normally be updated in mx_update_context(), but the
999 * full headers aren't parsed with XOVER, so the information wasn't
1002 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
1003 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1004 #endif /* HAVE_PGP || HAVE_SMIME */
1013 int nntp_post (const char *msg) {
1014 char buf[LONG_STRING];
1017 NNTP_DATA *nntp_data;
1019 if (Context && Context->magic == M_NNTP)
1020 nntp_data = (NNTP_DATA *)Context->data;
1023 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1024 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data)
1026 mutt_error (_("Can't post article. No connection to news server."));
1029 nntp_data = (NNTP_DATA *)CurrentNewsSrv->list->data;
1032 if (!(f = safe_fopen (msg, "r")))
1034 mutt_error (_("Can't post article. Unable to open %s"), msg);
1038 strfcpy (buf, "POST\r\n", sizeof (buf));
1039 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1041 mutt_error (_("Can't post article. Connection to %s lost."),
1042 nntp_data->nserv->conn->account.host);
1047 mutt_error (_("Can't post article: %s"), buf);
1053 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL)
1056 if (buf[len - 1] == '\n')
1058 buf[len - 1] = '\r';
1064 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1066 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1070 if (buf[strlen (buf) - 1] != '\n')
1071 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1072 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1073 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0)
1075 mutt_error (_("Can't post article. Connection to %s lost."),
1076 nntp_data->nserv->conn->account.host);
1081 mutt_error (_("Can't post article: %s"), buf);
1088 /* nntp_logout_all: close all open connections. */
1089 void nntp_logout_all (void)
1091 char buf[LONG_STRING];
1095 conn = mutt_socket_head ();
1101 if (conn->account.type == M_ACCT_TYPE_NNTP)
1103 mutt_message (_("Closing connection to %s..."), conn->account.host);
1104 mutt_socket_write (conn, "QUIT\r\n");
1105 mutt_socket_readln (buf, sizeof (buf), conn);
1106 mutt_clear_error ();
1107 mutt_socket_close (conn);
1109 mutt_socket_free (tmp);
1116 static void nntp_free_acache (NNTP_DATA *data)
1120 for (i = 0; i < NNTP_CACHE_LEN; i++)
1122 if (data->acache[i].path)
1124 unlink (data->acache[i].path);
1125 FREE (&data->acache[i].path);
1130 void nntp_delete_data (void *p)
1132 NNTP_DATA *data = (NNTP_DATA *)p;
1136 FREE (&data->entries);
1138 FREE (&data->cache);
1139 nntp_free_acache (data);
1143 int nntp_sync_mailbox (CONTEXT *ctx)
1145 NNTP_DATA *data = ctx->data;
1147 /* CACHE: update cache and .index files */
1148 if ((option (OPTSAVEUNSUB) || data->subscribed))
1149 nntp_save_cache_group (ctx);
1150 nntp_free_acache (data);
1152 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1156 void nntp_fastclose_mailbox (CONTEXT *ctx)
1158 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1162 nntp_free_acache (data);
1163 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1165 nntp_save_cache_index (data->nserv);
1166 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1168 nntp_delete_data (data);
1171 /* commit changes and terminate connection */
1172 int nntp_close_mailbox (CONTEXT *ctx)
1176 mutt_message _("Quitting newsgroup...");
1179 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1182 if (data->nserv && data->nserv->conn && ctx->unread)
1184 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1186 mutt_newsgroup_catchup (data->nserv, data->group);
1191 nntp_sync_mailbox (ctx);
1192 if (ctx->data && ((NNTP_DATA *)ctx->data)->nserv)
1196 news = ((NNTP_DATA *)ctx->data)->nserv;
1197 newsrc_gen_entries (ctx);
1198 ((NNTP_DATA *)ctx->data)->unread = ctx->unread;
1199 mutt_newsrc_update (news);
1205 /* use the GROUP command to poll for new mail */
1206 static int _nntp_check_mailbox (CONTEXT *ctx, NNTP_DATA *nntp_data)
1208 char buf[LONG_STRING];
1211 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1215 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1218 nntp_error ("nntp_check_mailbox()", buf);
1222 if (mutt_strncmp ("211", buf, 3))
1225 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1228 nntp_error ("nntp_check_mailbox()", buf);
1233 if (!mutt_strncmp ("211", buf, 3))
1238 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1239 nntp_data->firstMessage = first;
1240 nntp_data->lastMessage = last;
1241 if (ctx && last > nntp_data->lastLoaded)
1243 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1244 time (&nntp_data->nserv->check_time);
1247 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1248 nntp_data->unread = count;
1250 mutt_newsgroup_stat (nntp_data);
1251 /* active was renumbered? */
1252 if (last < nntp_data->lastLoaded)
1254 if (!nntp_data->max)
1256 nntp_data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1259 nntp_data->lastCached = 0;
1261 nntp_data->entries[0].first = 1;
1262 nntp_data->entries[0].last = 0;
1266 time (&nntp_data->nserv->check_time);
1270 int nntp_check_mailbox (CONTEXT *ctx)
1272 return _nntp_check_mailbox (ctx, (NNTP_DATA *)ctx->data);
1275 static int add_group (char *buf, void *serv)
1277 #define s ((NNTP_SERVER *) serv)
1278 char group[LONG_STRING], mod, desc[HUGE_STRING];
1280 NNTP_DATA *nntp_data;
1283 _checked = n; /* _checked have N, where N = number of groups */
1284 if (!buf) /* at EOF must be zerouth */
1291 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1294 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL)
1297 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
1298 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1299 strcpy (nntp_data->group, group);
1300 nntp_data->nserv = s;
1301 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1302 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1303 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1304 nntp_add_to_list (s, nntp_data);
1306 nntp_data->deleted = 0;
1307 nntp_data->firstMessage = first;
1308 nntp_data->lastMessage = last;
1310 nntp_data->allowed = 1;
1312 nntp_data->allowed = 0;
1313 if (nntp_data->desc)
1314 FREE (&nntp_data->desc);
1316 nntp_data->desc = safe_strdup (desc);
1317 if (nntp_data->rc || nntp_data->lastCached)
1318 mutt_newsgroup_stat (nntp_data);
1319 else if (nntp_data->lastMessage &&
1320 nntp_data->firstMessage <= nntp_data->lastMessage)
1321 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1323 nntp_data->unread = 0;
1329 int nntp_check_newgroups (NNTP_SERVER *serv, int force)
1331 char buf[LONG_STRING];
1332 NNTP_DATA nntp_data;
1338 if (!serv || !serv->newgroups_time)
1341 if (nntp_open_connection (serv) < 0)
1344 /* check subscribed groups for new news */
1345 if (option (OPTSHOWNEWNEWS))
1347 mutt_message _("Checking for new messages...");
1348 for (l = serv->list; l; l = l->next)
1350 serv->check_time = 0; /* really check! */
1351 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1352 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1358 mutt_message _("Checking for new newsgroups...");
1359 now = serv->newgroups_time;
1360 time (&serv->newgroups_time);
1362 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1363 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min,
1365 nntp_data.nserv = serv;
1366 if (Context && Context->magic == M_NNTP)
1367 nntp_data.group = ((NNTP_DATA *)Context->data)->group;
1369 nntp_data.group = NULL;
1371 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1372 add_group, serv, 0) != 0)
1375 nntp_error ("nntp_check_newgroups()", buf);
1380 mutt_message _("Loading descriptions...");
1384 emp.next = serv->list;
1389 ((NNTP_DATA *) l->data)->new = 1;
1390 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group, NULL);
1393 nntp_save_cache_index (serv);
1394 mutt_clear_error ();
1398 /* Load list of all newsgroups from cache ALL */
1399 int nntp_get_cache_all (NNTP_SERVER *serv)
1401 char buf[HUGE_STRING];
1404 nntp_cache_expand (buf, serv->cache);
1405 if ((f = safe_fopen (buf, "r")))
1409 while (fgets (buf, sizeof(buf), f) != NULL)
1411 if (ReadInc && (i % ReadInc == 0))
1412 mutt_message (_("Loading list from cache... %d"), i);
1413 add_group (buf, serv);
1416 add_group (NULL, NULL);
1418 mutt_clear_error ();
1423 FREE (&serv->cache);
1428 /* Load list of all newsgroups from active */
1429 int nntp_get_active (NNTP_SERVER *serv)
1431 char msg[SHORT_STRING];
1432 NNTP_DATA nntp_data;
1435 if (nntp_open_connection (serv) < 0)
1438 snprintf (msg, sizeof(msg), _("Loading list of all newsgroups on server %s..."),
1439 serv->conn->account.host);
1441 time (&serv->newgroups_time);
1442 nntp_data.nserv = serv;
1443 nntp_data.group = NULL;
1445 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0)
1448 nntp_error ("nntp_get_active()", "LIST\r\n");
1453 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1455 nntp_get_desc (&nntp_data, "*", msg);
1457 for (tmp = serv->list; tmp; tmp = tmp->next)
1459 NNTP_DATA *data = (NNTP_DATA *)tmp->data;
1461 if (data && data->deleted && !data->rc)
1463 nntp_delete_cache (data);
1464 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1468 nntp_save_cache_index (serv);
1470 mutt_clear_error ();
1475 * returns -1 if error ocurred while retrieving header,
1476 * number of articles which ones exist in context on success.
1478 int nntp_check_msgid (CONTEXT *ctx, const char *msgid)
1482 /* if msgid is already in context, don't reload them */
1483 if (hash_find (ctx->id_hash, msgid))
1485 if (ctx->msgcount == ctx->hdrmax)
1486 mx_alloc_memory (ctx);
1487 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1488 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1490 mutt_message (_("Fetching %s from server..."), msgid);
1491 ret = nntp_read_header (ctx, msgid, 0);
1492 /* since nntp_read_header() may set read flag, we must reset it */
1493 ctx->hdrs[ctx->msgcount]->read = 0;
1495 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1499 mx_update_context (ctx, 1);
1510 unsigned int *child;
1513 static int check_children (char *s, void *c)
1515 #define cc ((CHILD_CONTEXT *) c)
1518 if (!s || (n = atoi (s)) == 0)
1520 for (i = 0; i < cc->ctx->msgcount; i++)
1521 if (cc->ctx->hdrs[i]->article_num == n)
1523 if (cc->num >= cc->max)
1524 safe_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1525 cc->child[cc->num++] = n;
1531 int nntp_check_children (CONTEXT *ctx, const char *msgid)
1533 NNTP_DATA *nntp_data = (NNTP_DATA *)ctx->data;
1535 int i, ret = 0, tmp = 0;
1538 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1539 !nntp_data->nserv->conn->account.host)
1541 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1543 if (!nntp_data->nserv->hasXPAT)
1545 mutt_error (_("Server %s does not support this operation!"),
1546 nntp_data->nserv->conn->account.host);
1550 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1551 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1556 cc.child = safe_malloc (sizeof (unsigned int) * 25);
1557 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0))
1562 /* dont try to read the xover cache. check_children() already
1563 * made sure that we dont have the article, so we need to visit
1564 * the server. Reading the cache at this point is also bad
1565 * because it would duplicate messages */
1566 if (option (OPTNEWSCACHE))
1569 unset_option (OPTNEWSCACHE);
1571 for (i = 0; i < cc.num; i++)
1573 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1575 if (ctx->msgcount &&
1576 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1577 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1580 set_option (OPTNEWSCACHE);