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.
17 #include "mutt_curses.h"
43 static unsigned int _checked = 0;
46 static void nntp_error (const char *where, const char *msg)
49 (debugfile, "nntp_error(): unexpected response in %s: %s\n", where,
54 static int nntp_auth (NNTP_SERVER * serv)
56 CONNECTION *conn = serv->conn;
58 unsigned char flags = conn->account.flags;
60 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
61 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
62 conn->account.flags = flags;
66 mutt_message _("Logging in...");
68 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
69 mutt_socket_write (conn, buf);
70 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
71 conn->account.flags = flags;
76 /* don't print the password unless we're at the ungodly debugging level */
77 if (debuglevel < M_SOCK_LOG_FULL)
78 dprint (M_SOCK_LOG_CMD, (debugfile, "> AUTHINFO PASS *\n"));
80 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
81 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
82 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
83 conn->account.flags = flags;
87 if (safe_strncmp ("281", buf, 3)) {
88 conn->account.flags = flags;
89 mutt_error _("Login failed.");
98 static int nntp_connect_error (NNTP_SERVER * serv)
100 serv->status = NNTP_NONE;
101 mutt_socket_close (serv->conn);
102 mutt_error _("Server closed connection!");
108 static int nntp_connect_and_auth (NNTP_SERVER * serv)
110 CONNECTION *conn = serv->conn;
114 serv->status = NNTP_NONE;
116 if (mutt_socket_open (conn) < 0)
119 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
120 return nntp_connect_error (serv);
122 if (!safe_strncmp ("200", buf, 3))
123 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
124 else if (!safe_strncmp ("201", buf, 3))
125 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
127 mutt_socket_close (conn);
128 mutt_remove_trailing_ws (buf);
129 mutt_error ("%s", buf);
136 /* Tell INN to switch to mode reader if it isn't so. Ignore all
137 returned codes and messages. */
138 mutt_socket_write (conn, "MODE READER\r\n");
139 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
140 return nntp_connect_error (serv);
142 mutt_socket_write (conn, "STAT\r\n");
143 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
144 return nntp_connect_error (serv);
146 if (!(conn->account.flags & M_ACCT_USER) && safe_strncmp ("480", buf, 3)) {
147 serv->status = NNTP_OK;
151 rc = nntp_auth (serv);
153 return nntp_connect_error (serv);
155 mutt_socket_close (conn);
156 serv->status = NNTP_BYE;
160 mutt_socket_close (conn);
161 mutt_error _("Login failed.");
166 serv->status = NNTP_OK;
170 static int nntp_attempt_features (NNTP_SERVER * serv)
172 char buf[LONG_STRING];
173 CONNECTION *conn = serv->conn;
175 mutt_socket_write (conn, "LISTGROUP\r\n");
176 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
177 return (nntp_connect_error (serv));
178 if (safe_strncmp ("500", buf, 3))
179 serv->hasLISTGROUP = 1;
181 mutt_socket_write (conn, "XOVER\r\n");
182 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
183 return nntp_connect_error (serv);
184 if (safe_strncmp ("500", buf, 3))
187 mutt_socket_write (conn, "XPAT\r\n");
188 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
189 return nntp_connect_error (serv);
190 if (safe_strncmp ("500", buf, 3))
193 mutt_socket_write (conn, "XGTITLE +\r\n");
194 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
195 return nntp_connect_error (serv);
196 if (safe_strncmp ("500", buf, 3))
197 serv->hasXGTITLE = 1;
199 if (!safe_strncmp ("282", buf, 3)) {
201 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
202 return nntp_connect_error (serv);
203 } while (!(buf[0] == '.' && buf[1] == '\0'));
209 static int nntp_open_connection (NNTP_SERVER * serv)
211 if (serv->status == NNTP_OK)
213 if (serv->status == NNTP_BYE)
215 if (nntp_connect_and_auth (serv) < 0)
217 if (nntp_attempt_features (serv) < 0)
222 static int nntp_reconnect (NNTP_SERVER * serv)
224 char buf[SHORT_STRING];
226 mutt_socket_close (serv->conn);
229 if (nntp_connect_and_auth (serv) == 0)
232 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
233 serv->conn->account.host);
234 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
235 serv->status = NNTP_BYE;
241 /* Send data from line[LONG_STRING] and receive answer to same line */
242 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
244 char buf[LONG_STRING];
247 if (data->nserv->status == NNTP_BYE)
252 mutt_socket_write (data->nserv->conn, line);
254 else if (data->group) {
255 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
256 mutt_socket_write (data->nserv->conn, buf);
260 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
261 if (nntp_reconnect (data->nserv) < 0)
265 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
266 mutt_socket_write (data->nserv->conn, buf);
267 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
273 else if ((!safe_strncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
277 strfcpy (line, buf, linelen);
282 * This function calls funct(*line, *data) for each received line,
283 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
286 * 1 - correct but not performed (may be, have to be continued),
287 * -1 - conection lost,
288 * -2 - invalid command or execution error,
289 * -3 - error in funct(*line, *data).
291 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, char *query, char *msg,
292 int (*funct) (char *, void *), void *data,
295 char buf[LONG_STRING];
303 strfcpy (buf, query, sizeof (buf));
304 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
313 inbuf = safe_malloc (sizeof (buf));
316 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
322 if (!lenbuf && buf[0] == '.') {
323 if (buf[1] == '\0') {
331 strfcpy (inbuf + lenbuf, p, sizeof (buf));
333 if (chunk >= sizeof (buf)) {
334 lenbuf += safe_strlen (p);
338 if (msg && ReadInc && (line % ReadInc == 0)) {
340 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
342 mutt_message ("%s %d", msg, line);
345 if (ret == 0 && funct (inbuf, data) < 0)
350 safe_realloc (&inbuf, lenbuf + sizeof (buf));
359 static int nntp_read_tempfile (char *line, void *file)
361 FILE *f = (FILE *) file;
367 if (fputc ('\n', f) == EOF)
373 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
376 register char *p, *b;
377 register char *colon = NULL;
381 /* skip to next word */
383 while (*b && ((*b == ' ') || (*b == '\t')))
387 /* skip to end of word */
388 while (*p && (*p != ' ') && (*p != '\t')) {
400 nntp_get_status (ctx, h, p, atoi (colon));
401 if (h && h->article_num == 0 && safe_strcmp (group, b) == 0)
402 h->article_num = atoi (colon);
410 * 1 if article not found
411 * -1 if read or write error on tempfile or socket
413 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
416 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
418 char buf[LONG_STRING];
419 char tempfile[_POSIX_PATH_MAX];
421 HEADER *h = ctx->hdrs[ctx->msgcount];
423 mutt_mktemp (tempfile);
424 if (!(f = safe_fopen (tempfile, "w+")))
428 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
430 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
432 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
436 dprint (1, (debugfile, "nntp_read_header: %s\n", buf));
440 return (ret == -1 ? -1 : 1);
443 h->article_num = article_num;
444 h->env = mutt_read_rfc822_header (f, h, 0, 0);
448 if (h->env->xref != NULL)
449 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
450 else if (h->article_num == 0 && msgid) {
451 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
452 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
453 h->article_num = atoi (buf + 4);
459 static int parse_description (char *line, void *n)
461 #define news ((NNTP_SERVER *) n)
462 register char *d = line;
467 while (*d && *d != '\t' && *d != ' ')
471 while (*d && (*d == '\t' || *d == ' '))
473 dprint (2, (debugfile, "group: %s, desc: %s\n", line, d));
474 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
475 safe_strcmp (d, data->desc)) {
477 data->desc = safe_strdup (d);
483 static void nntp_get_desc (NNTP_DATA * data, char *mask, char *msg)
487 if (!option (OPTLOADDESC) || !data || !data->nserv)
490 /* Get newsgroup description, if we can */
491 if (data->nserv->hasXGTITLE)
492 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
494 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
495 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) !=
498 nntp_error ("nntp_get_desc()", buf);
504 * XOVER returns a tab separated list of:
505 * id|subject|from|date|Msgid|references|bytes|lines|xref
507 * This has to duplicate some of the functionality of
508 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
509 * a limited number of headers which are "parsed" by placement in the list)
511 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
513 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
517 hdr->env = mutt_new_envelope ();
518 hdr->env->newsgroups = safe_strdup (nntp_data->group);
519 hdr->content = mutt_new_body ();
520 hdr->content->type = TYPETEXT;
521 hdr->content->subtype = safe_strdup ("plain");
522 hdr->content->encoding = ENC7BIT;
523 hdr->content->disposition = DISPINLINE;
524 hdr->content->length = -1;
527 for (x = 0; !done && x < 9; x++) {
528 /* if from file, need to skip newline character */
529 while (*p && *p != '\n' && *p != '\t')
538 hdr->article_num = atoi (b);
539 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
542 hdr->env->subject = safe_strdup (b);
543 /* Now we need to do the things which would normally be done in
544 * mutt_read_rfc822_header() */
545 if (hdr->env->subject) {
546 regmatch_t pmatch[1];
548 rfc2047_decode (&hdr->env->subject);
550 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
551 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
553 hdr->env->real_subj = hdr->env->subject;
557 rfc822_free_address (&hdr->env->from);
558 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
559 rfc2047_decode_adrlist (hdr->env->from);
562 hdr->date_sent = mutt_parse_date (b, hdr);
563 hdr->received = hdr->date_sent;
566 FREE (&hdr->env->message_id);
567 hdr->env->message_id = safe_strdup (b);
570 mutt_free_list (&hdr->env->references);
571 hdr->env->references = mutt_parse_references (b, 0);
574 hdr->content->length = atoi (b);
577 hdr->lines = atoi (b);
581 FREE (&hdr->env->xref);
582 b = b + 6; /* skips the "Xref: " */
583 hdr->env->xref = safe_strdup (b);
584 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
598 unsigned short *messages;
602 #define fc ((FETCH_CONTEXT *) c)
603 static int _nntp_fetch_numbers (unsigned int num, void *c)
605 if (num < fc->base || num > fc->last)
607 fc->messages[num - fc->base] = 1;
610 static int nntp_fetch_numbers (char *line, void *c)
614 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
617 static int add_xover_line (char *line, void *c)
619 unsigned int num, total;
620 CONTEXT *ctx = fc->ctx;
621 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
626 if (ctx->msgcount >= ctx->hdrmax)
627 mx_alloc_memory (ctx);
628 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
629 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
631 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
632 num = ctx->hdrs[ctx->msgcount]->article_num;
634 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
636 if (num > data->lastLoaded)
637 data->lastLoaded = num;
638 num = num - fc->first + 1;
639 total = fc->last - fc->first + 1;
640 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
641 mutt_message ("%s %d/%d", fc->msg, num, total);
644 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
651 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
654 char buf[HUGE_STRING];
655 char *msg = _("Fetching message headers...");
656 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
660 unsigned int current;
664 /* if empty group or nothing to do */
665 if (!last || first > last)
668 /* fetch list of articles */
669 mutt_message _("Fetching list of articles...");
674 fc.messages = safe_calloc (last - first + 1, sizeof (unsigned short));
675 if (nntp_data->nserv->hasLISTGROUP) {
676 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
677 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) !=
679 mutt_error (_("LISTGROUP command failed: %s"), buf);
682 nntp_error ("nntp_fetch_headers()", buf);
689 /* mutt_nntp_query() issues a 'GROUP nntp_data->group'
690 * command on its own if !*buf */
692 mutt_nntp_query (nntp_data, buf, sizeof (buf));
693 if (sscanf (buf + 4, "%d %u %u %s", &num, &fc.first, &fc.last, buf) != 4) {
694 mutt_error (_("GROUP command failed: %s"), buf);
699 for (num = fc.first; num < fc.last; num++)
700 _nntp_fetch_numbers (num, &fc);
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);
708 mutt_message _("Fetching headers from 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 add_xover_line (buf, &fc);
726 nntp_data->lastLoaded = fc.last;
728 if (ctx->msgcount > oldmsgcount)
729 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
732 nntp_delete_cache (nntp_data);
734 num = last - first + 1;
741 * Without XOVER, we have to fetch each article header and parse
742 * it. With XOVER, we ask for all of them
745 if (nntp_data->nserv->hasXOVER) {
746 oldmsgcount = ctx->msgcount;
750 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
751 ret = mutt_nntp_fetch (nntp_data, buf, NULL, add_xover_line, &fc, 0);
752 if (ctx->msgcount > oldmsgcount)
753 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
755 mutt_error (_("XOVER command failed: %s"), buf);
757 nntp_error ("nntp_fetch_headers()", buf);
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] = mutt_new_header ();
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 mutt_free_header (&h); /* skip it */
792 if (current > nntp_data->lastLoaded)
793 nntp_data->lastLoaded = current;
796 nntp_data->lastLoaded = last;
802 * currently, nntp "mailbox" is "newsgroup"
804 int nntp_open_mailbox (CONTEXT * ctx)
806 NNTP_DATA *nntp_data;
808 char buf[HUGE_STRING];
809 char server[LONG_STRING];
814 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
815 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
821 nntp_expand_path (server, sizeof (server), &acct);
822 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
825 CurrentNewsSrv = serv;
827 /* create NNTP-specific state struct if nof found in list */
828 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
829 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + safe_strlen (buf) + 1);
830 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
831 strcpy (nntp_data->group, buf);
832 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
833 nntp_add_to_list (serv, nntp_data);
835 ctx->data = nntp_data;
836 nntp_data->nserv = serv;
838 mutt_message (_("Selecting %s..."), nntp_data->group);
840 if (!nntp_data->desc) {
841 nntp_get_desc (nntp_data, nntp_data->group, NULL);
843 nntp_save_cache_index (serv);
847 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
849 nntp_error ("nntp_open_mailbox()", buf);
854 if (safe_strncmp ("211", buf, 3)) {
855 LIST *l = serv->list;
857 /* GROUP command failed */
858 if (!safe_strncmp ("411", buf, 3)) {
859 mutt_error (_("Newsgroup %s not found on server %s"),
860 nntp_data->group, serv->conn->account.host);
862 /* CACHE: delete cache and line from .index */
863 nntp_delete_cache (nntp_data);
864 hash_delete (serv->newsgroups, nntp_data->group, NULL,
866 while (l && l->data != (void *) nntp_data)
877 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
878 &nntp_data->lastMessage, buf);
880 nntp_data->deleted = 0;
882 time (&serv->check_time);
885 * Check for max adding context. If it is greater than $nntp_context,
886 * strip off extra articles
888 first = nntp_data->firstMessage;
889 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
890 first = nntp_data->lastMessage - NntpContext + 1;
892 nntp_data->lastLoaded = first - 1;
893 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
896 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
898 char buf[LONG_STRING];
899 char path[_POSIX_PATH_MAX];
901 char *m = _("Fetching message...");
904 /* see if we already have the message in our cache */
906 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
909 /* if everything is fine, assign msg->fp and return */
910 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
911 (msg->fp = fopen (cache->path, "r")))
914 /* clear the previous entry */
915 unlink (cache->path);
920 cache->index = ctx->hdrs[msgno]->index;
922 cache->path = safe_strdup (path);
923 if (!(msg->fp = safe_fopen (path, "w+"))) {
928 if (ctx->hdrs[msgno]->article_num == 0)
929 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
930 ctx->hdrs[msgno]->env->message_id);
932 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
933 ctx->hdrs[msgno]->article_num);
935 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, m, nntp_read_tempfile,
936 msg->fp, ctx->tagged);
938 mutt_error (_("Article %d not found on server"),
939 ctx->hdrs[msgno]->article_num);
940 dprint (1, (debugfile, "nntp_fetch_message: %s\n", buf));
950 mutt_free_envelope (&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 fseek (msg->fp, 0, SEEK_END);
955 ctx->hdrs[msgno]->content->length = ftell (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 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
969 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
970 #endif /* HAVE_PGP || HAVE_SMIME */
979 int nntp_post (const char *msg)
981 char buf[LONG_STRING];
984 NNTP_DATA *nntp_data;
986 if (Context && Context->magic == M_NNTP)
987 nntp_data = (NNTP_DATA *) Context->data;
989 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
990 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
991 mutt_error (_("Can't post article. No connection to news server."));
994 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
997 if (!(f = safe_fopen (msg, "r"))) {
998 mutt_error (_("Can't post article. Unable to open %s"), msg);
1002 strfcpy (buf, "POST\r\n", sizeof (buf));
1003 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1004 mutt_error (_("Can't post article. Connection to %s lost."),
1005 nntp_data->nserv->conn->account.host);
1008 if (buf[0] != '3') {
1009 mutt_error (_("Can't post article: %s"), buf);
1015 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1016 len = safe_strlen (buf);
1017 if (buf[len - 1] == '\n') {
1018 buf[len - 1] = '\r';
1024 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1026 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1030 if (buf[safe_strlen (buf) - 1] != '\n')
1031 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1032 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1033 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1034 mutt_error (_("Can't post article. Connection to %s lost."),
1035 nntp_data->nserv->conn->account.host);
1038 if (buf[0] != '2') {
1039 mutt_error (_("Can't post article: %s"), buf);
1046 /* nntp_logout_all: close all open connections. */
1047 void nntp_logout_all (void)
1049 char buf[LONG_STRING];
1053 conn = mutt_socket_head ();
1058 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1059 mutt_message (_("Closing connection to %s..."), conn->account.host);
1060 mutt_socket_write (conn, "QUIT\r\n");
1061 mutt_socket_readln (buf, sizeof (buf), conn);
1062 mutt_clear_error ();
1063 mutt_socket_close (conn);
1065 mutt_socket_free (tmp);
1072 static void nntp_free_acache (NNTP_DATA * data)
1076 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1077 if (data->acache[i].path) {
1078 unlink (data->acache[i].path);
1079 FREE (&data->acache[i].path);
1084 void nntp_delete_data (void *p)
1086 NNTP_DATA *data = (NNTP_DATA *) p;
1090 FREE (&data->entries);
1092 FREE (&data->cache);
1093 nntp_free_acache (data);
1097 int nntp_sync_mailbox (CONTEXT * ctx)
1099 NNTP_DATA *data = ctx->data;
1101 /* CACHE: update cache and .index files */
1102 if ((option (OPTSAVEUNSUB) || data->subscribed))
1103 nntp_save_cache_group (ctx);
1104 nntp_free_acache (data);
1106 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1110 void nntp_fastclose_mailbox (CONTEXT * ctx)
1112 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1116 nntp_free_acache (data);
1117 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1119 nntp_save_cache_index (data->nserv);
1120 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1122 nntp_delete_data (data);
1125 /* commit changes and terminate connection */
1126 int nntp_close_mailbox (CONTEXT * ctx)
1130 mutt_message _("Quitting newsgroup...");
1133 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1136 if (data->nserv && data->nserv->conn && ctx->unread) {
1137 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1139 mutt_newsgroup_catchup (data->nserv, data->group);
1144 nntp_sync_mailbox (ctx);
1145 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1148 news = ((NNTP_DATA *) ctx->data)->nserv;
1149 newsrc_gen_entries (ctx);
1150 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1151 mutt_newsrc_update (news);
1153 mutt_clear_error ();
1157 /* use the GROUP command to poll for new mail */
1158 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1160 char buf[LONG_STRING];
1163 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1167 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1169 nntp_error ("nntp_check_mailbox()", buf);
1173 if (safe_strncmp ("211", buf, 3)) {
1175 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1177 nntp_error ("nntp_check_mailbox()", buf);
1182 if (!safe_strncmp ("211", buf, 3)) {
1186 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1187 nntp_data->firstMessage = first;
1188 nntp_data->lastMessage = last;
1189 if (ctx && last > nntp_data->lastLoaded) {
1190 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1191 time (&nntp_data->nserv->check_time);
1194 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1195 nntp_data->unread = count;
1197 mutt_newsgroup_stat (nntp_data);
1198 /* active was renumbered? */
1199 if (last < nntp_data->lastLoaded) {
1200 if (!nntp_data->max) {
1201 nntp_data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1204 nntp_data->lastCached = 0;
1206 nntp_data->entries[0].first = 1;
1207 nntp_data->entries[0].last = 0;
1211 time (&nntp_data->nserv->check_time);
1215 int nntp_check_mailbox (CONTEXT * ctx)
1217 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1220 static int add_group (char *buf, void *serv)
1222 #define s ((NNTP_SERVER *) serv)
1223 char group[LONG_STRING], mod, desc[HUGE_STRING];
1225 NNTP_DATA *nntp_data;
1228 _checked = n; /* _checked have N, where N = number of groups */
1229 if (!buf) /* at EOF must be zerouth */
1236 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1239 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1241 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + safe_strlen (group) + 1);
1242 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1243 strcpy (nntp_data->group, group);
1244 nntp_data->nserv = s;
1245 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1246 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1247 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1248 nntp_add_to_list (s, nntp_data);
1250 nntp_data->deleted = 0;
1251 nntp_data->firstMessage = first;
1252 nntp_data->lastMessage = last;
1254 nntp_data->allowed = 1;
1256 nntp_data->allowed = 0;
1257 if (nntp_data->desc)
1258 FREE (&nntp_data->desc);
1260 nntp_data->desc = safe_strdup (desc);
1261 if (nntp_data->rc || nntp_data->lastCached)
1262 mutt_newsgroup_stat (nntp_data);
1263 else if (nntp_data->lastMessage &&
1264 nntp_data->firstMessage <= nntp_data->lastMessage)
1265 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1267 nntp_data->unread = 0;
1273 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1275 char buf[LONG_STRING];
1276 NNTP_DATA nntp_data;
1282 if (!serv || !serv->newgroups_time)
1285 if (nntp_open_connection (serv) < 0)
1288 /* check subscribed groups for new news */
1289 if (option (OPTSHOWNEWNEWS)) {
1290 mutt_message _("Checking for new messages...");
1292 for (l = serv->list; l; l = l->next) {
1293 serv->check_time = 0; /* really check! */
1294 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1295 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1301 mutt_message _("Checking for new newsgroups...");
1303 now = serv->newgroups_time;
1304 time (&serv->newgroups_time);
1306 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1307 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1308 t->tm_min, t->tm_sec);
1309 nntp_data.nserv = serv;
1310 if (Context && Context->magic == M_NNTP)
1311 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1313 nntp_data.group = NULL;
1315 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1316 add_group, serv, 0) != 0) {
1318 nntp_error ("nntp_check_newgroups()", buf);
1323 mutt_message _("Loading descriptions...");
1328 emp.next = serv->list;
1332 ((NNTP_DATA *) l->data)->new = 1;
1333 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1337 nntp_save_cache_index (serv);
1338 mutt_clear_error ();
1342 /* Load list of all newsgroups from cache ALL */
1343 int nntp_get_cache_all (NNTP_SERVER * serv)
1345 char buf[HUGE_STRING];
1348 nntp_cache_expand (buf, serv->cache);
1349 if ((f = safe_fopen (buf, "r"))) {
1352 while (fgets (buf, sizeof (buf), f) != NULL) {
1353 if (ReadInc && (i % ReadInc == 0))
1354 mutt_message (_("Loading list from cache... %d"), i);
1355 add_group (buf, serv);
1358 add_group (NULL, NULL);
1360 mutt_clear_error ();
1364 FREE (&serv->cache);
1369 /* Load list of all newsgroups from active */
1370 int nntp_get_active (NNTP_SERVER * serv)
1372 char msg[SHORT_STRING];
1373 NNTP_DATA nntp_data;
1376 if (nntp_open_connection (serv) < 0)
1379 snprintf (msg, sizeof (msg),
1380 _("Loading list of all newsgroups on server %s..."),
1381 serv->conn->account.host);
1383 time (&serv->newgroups_time);
1384 nntp_data.nserv = serv;
1385 nntp_data.group = NULL;
1387 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0) {
1389 nntp_error ("nntp_get_active()", "LIST\r\n");
1394 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1396 nntp_get_desc (&nntp_data, "*", msg);
1398 for (tmp = serv->list; tmp; tmp = tmp->next) {
1399 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1401 if (data && data->deleted && !data->rc) {
1402 nntp_delete_cache (data);
1403 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1407 nntp_save_cache_index (serv);
1409 mutt_clear_error ();
1414 * returns -1 if error ocurred while retrieving header,
1415 * number of articles which ones exist in context on success.
1417 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1421 /* if msgid is already in context, don't reload them */
1422 if (hash_find (ctx->id_hash, msgid))
1424 if (ctx->msgcount == ctx->hdrmax)
1425 mx_alloc_memory (ctx);
1426 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1427 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1429 mutt_message (_("Fetching %s from server..."), msgid);
1430 ret = nntp_read_header (ctx, msgid, 0);
1431 /* since nntp_read_header() may set read flag, we must reset it */
1432 ctx->hdrs[ctx->msgcount]->read = 0;
1434 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1437 mx_update_context (ctx, 1);
1447 unsigned int *child;
1450 static int check_children (char *s, void *c)
1452 #define cc ((CHILD_CONTEXT *) c)
1455 if (!s || (n = atoi (s)) == 0)
1457 for (i = 0; i < cc->ctx->msgcount; i++)
1458 if (cc->ctx->hdrs[i]->article_num == n)
1460 if (cc->num >= cc->max)
1461 safe_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1462 cc->child[cc->num++] = n;
1468 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1470 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1472 int i, ret = 0, tmp = 0;
1475 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1476 !nntp_data->nserv->conn->account.host)
1478 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1480 if (!nntp_data->nserv->hasXPAT) {
1481 mutt_error (_("Server %s does not support this operation!"),
1482 nntp_data->nserv->conn->account.host);
1486 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1487 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1492 cc.child = safe_malloc (sizeof (unsigned int) * 25);
1493 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0)) {
1497 /* dont try to read the xover cache. check_children() already
1498 * made sure that we dont have the article, so we need to visit
1499 * the server. Reading the cache at this point is also bad
1500 * because it would duplicate messages */
1501 if (option (OPTNEWSCACHE)) {
1503 unset_option (OPTNEWSCACHE);
1505 for (i = 0; i < cc.num; i++) {
1506 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1508 if (ctx->msgcount &&
1509 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1510 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1513 set_option (OPTNEWSCACHE);