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.
22 #include "mutt_curses.h"
44 static unsigned int _checked = 0;
47 static void nntp_error (const char *where, const char *msg)
49 dprint (1, (debugfile, "nntp_error(): unexpected response in %s: %s\n", where, msg));
53 static int nntp_auth (NNTP_SERVER *serv)
55 CONNECTION *conn = serv->conn;
57 unsigned char flags = conn->account.flags;
59 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
60 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)
72 conn->account.flags = flags;
77 /* don't print the password unless we're at the ungodly debugging level */
78 if (debuglevel < M_SOCK_LOG_FULL)
79 dprint (M_SOCK_LOG_CMD, (debugfile, "> AUTHINFO PASS *\n"));
81 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
82 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
83 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
85 conn->account.flags = flags;
89 if (mutt_strncmp ("281", buf, 3))
91 conn->account.flags = flags;
92 mutt_error _("Login failed.");
100 static int nntp_connect_error (NNTP_SERVER *serv)
102 serv->status = NNTP_NONE;
103 mutt_socket_close (serv->conn);
104 mutt_error _("Server closed connection!");
109 static int nntp_connect_and_auth (NNTP_SERVER *serv)
111 CONNECTION *conn = serv->conn;
115 serv->status = NNTP_NONE;
117 if (mutt_socket_open (conn) < 0)
120 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
121 return nntp_connect_error (serv);
123 if (!mutt_strncmp ("200", buf, 3))
124 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
125 else if (!mutt_strncmp ("201", buf, 3))
126 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
129 mutt_socket_close (conn);
130 mutt_remove_trailing_ws (buf);
131 mutt_error ("%s", buf);
138 /* Tell INN to switch to mode reader if it isn't so. Ignore all
139 returned codes and messages. */
140 mutt_socket_write (conn, "MODE READER\r\n");
141 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
142 return nntp_connect_error (serv);
144 mutt_socket_write (conn, "STAT\r\n");
145 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
146 return nntp_connect_error (serv);
148 if (!(conn->account.flags & M_ACCT_USER) && mutt_strncmp ("480", buf, 3))
150 serv->status = NNTP_OK;
154 rc = nntp_auth (serv);
156 return nntp_connect_error (serv);
159 mutt_socket_close (conn);
160 serv->status = NNTP_BYE;
165 mutt_socket_close (conn);
166 mutt_error _("Login failed.");
170 serv->status = NNTP_OK;
174 static int nntp_attempt_features (NNTP_SERVER *serv)
176 char buf[LONG_STRING];
177 CONNECTION *conn = serv->conn;
179 mutt_socket_write (conn, "XOVER\r\n");
180 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
181 return nntp_connect_error (serv);
182 if (mutt_strncmp ("500", buf, 3))
185 mutt_socket_write (conn, "XPAT\r\n");
186 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
187 return nntp_connect_error (serv);
188 if (mutt_strncmp ("500", buf, 3))
191 mutt_socket_write (conn, "XGTITLE +\r\n");
192 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
193 return nntp_connect_error (serv);
194 if (mutt_strncmp ("500", buf, 3))
195 serv->hasXGTITLE = 1;
197 if (!mutt_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);
230 if (nntp_connect_and_auth (serv) == 0)
233 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
234 serv->conn->account.host);
235 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES)
237 serv->status = NNTP_BYE;
243 /* Send data from line[LONG_STRING] and receive answer to same line */
244 static int mutt_nntp_query (NNTP_DATA *data, char *line, size_t linelen)
246 char buf[LONG_STRING];
249 if (data->nserv->status == NNTP_BYE)
256 mutt_socket_write (data->nserv->conn, line);
258 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)
267 if (nntp_reconnect (data->nserv) < 0)
272 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
273 mutt_socket_write (data->nserv->conn, buf);
274 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
280 else if ((!mutt_strncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
284 strfcpy (line, buf, linelen);
289 * This function calls funct(*line, *data) for each received line,
290 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
293 * 1 - correct but not performed (may be, have to be continued),
294 * -1 - conection lost,
295 * -2 - invalid command or execution error,
296 * -3 - error in funct(*line, *data).
298 static int mutt_nntp_fetch (NNTP_DATA *nntp_data, char *query, char *msg,
299 int (*funct) (char *, void *), void *data, int tagged)
301 char buf[LONG_STRING];
310 strfcpy (buf, query, sizeof (buf));
311 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
320 inbuf = safe_malloc (sizeof (buf));
324 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
330 if (!lenbuf && buf[0] == '.')
341 strfcpy (inbuf + lenbuf, p, sizeof (buf));
343 if (chunk >= sizeof (buf))
345 lenbuf += strlen (p);
350 if (msg && ReadInc && (line % ReadInc == 0)) {
352 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
354 mutt_message ("%s %d", msg, line);
357 if (ret == 0 && funct (inbuf, data) < 0)
362 safe_realloc (&inbuf, lenbuf + sizeof (buf));
371 static int nntp_read_tempfile (char *line, void *file)
373 FILE *f = (FILE *)file;
380 if (fputc ('\n', f) == EOF)
386 static void nntp_parse_xref (CONTEXT *ctx, char *group, char *xref, HEADER *h)
388 register char *p, *b;
389 register char *colon = NULL;
394 /* skip to next word */
396 while (*b && ((*b == ' ') || (*b == '\t'))) b++;
399 /* skip to end of word */
400 while (*p && (*p != ' ') && (*p != '\t'))
415 nntp_get_status (ctx, h, p, atoi(colon));
416 if (h && h->article_num == 0 && mutt_strcmp (group, b) == 0)
417 h->article_num = atoi(colon);
425 * 1 if article not found
426 * -1 if read or write error on tempfile or socket
428 static int nntp_read_header (CONTEXT *ctx, const char *msgid, int article_num)
430 NNTP_DATA *nntp_data = ((NNTP_DATA *)ctx->data);
432 char buf[LONG_STRING];
433 char tempfile[_POSIX_PATH_MAX];
435 HEADER *h = ctx->hdrs[ctx->msgcount];
437 mutt_mktemp (tempfile);
438 if (!(f = safe_fopen (tempfile, "w+")))
442 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
444 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
446 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
451 dprint(1, (debugfile, "nntp_read_header: %s\n", buf));
455 return (ret == -1 ? -1 : 1);
458 h->article_num = article_num;
459 h->env = mutt_read_rfc822_header (f, h, 0, 0);
463 if (h->env->xref != NULL)
464 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
465 else if (h->article_num == 0 && msgid)
467 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
468 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
469 h->article_num = atoi (buf + 4);
475 static int parse_description (char *line, void *n)
477 #define news ((NNTP_SERVER *) n)
478 register char *d = line;
483 while (*d && *d != '\t' && *d != ' ') d++;
486 while (*d && (*d == '\t' || *d == ' ')) d++;
487 dprint (2, (debugfile, "group: %s, desc: %s\n", line, d));
488 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
489 mutt_strcmp (d, data->desc))
492 data->desc = safe_strdup (d);
498 static void nntp_get_desc (NNTP_DATA *data, char *mask, char *msg)
502 if (!option (OPTLOADDESC) || !data || !data->nserv)
505 /* Get newsgroup description, if we can */
506 if (data->nserv->hasXGTITLE)
507 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
509 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
510 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) != 0)
513 nntp_error ("nntp_get_desc()", buf);
519 * XOVER returns a tab separated list of:
520 * id|subject|from|date|Msgid|references|bytes|lines|xref
522 * This has to duplicate some of the functionality of
523 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
524 * a limited number of headers which are "parsed" by placement in the list)
526 static int nntp_parse_xover (CONTEXT *ctx, char *buf, HEADER *hdr)
528 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
532 hdr->env = mutt_new_envelope();
533 hdr->env->newsgroups = safe_strdup (nntp_data->group);
534 hdr->content = mutt_new_body();
535 hdr->content->type = TYPETEXT;
536 hdr->content->subtype = safe_strdup ("plain");
537 hdr->content->encoding = ENC7BIT;
538 hdr->content->disposition = DISPINLINE;
539 hdr->content->length = -1;
542 for (x = 0; !done && x < 9; x++)
544 /* if from file, need to skip newline character */
545 while (*p && *p != '\n' && *p != '\t') p++;
553 hdr->article_num = atoi (b);
554 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
557 hdr->env->subject = safe_strdup (b);
558 /* Now we need to do the things which would normally be done in
559 * mutt_read_rfc822_header() */
560 if (hdr->env->subject)
562 regmatch_t pmatch[1];
564 rfc2047_decode (&hdr->env->subject);
566 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
567 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
569 hdr->env->real_subj = hdr->env->subject;
573 rfc822_free_address (&hdr->env->from);
574 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
575 rfc2047_decode_adrlist (hdr->env->from);
578 hdr->date_sent = mutt_parse_date (b, hdr);
579 hdr->received = hdr->date_sent;
582 FREE (&hdr->env->message_id);
583 hdr->env->message_id = safe_strdup (b);
586 mutt_free_list (&hdr->env->references);
587 hdr->env->references = mutt_parse_references (b, 0);
590 hdr->content->length = atoi (b);
593 hdr->lines = atoi (b);
597 FREE (&hdr->env->xref);
598 b = b + 6; /* skips the "Xref: " */
599 hdr->env->xref = safe_strdup (b);
600 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
615 unsigned short *messages;
619 #define fc ((FETCH_CONTEXT *) c)
620 static int nntp_fetch_numbers (char *line, void *c)
627 if (num < fc->base || num > fc->last)
629 fc->messages[num - fc->base] = 1;
633 static int add_xover_line (char *line, void *c)
635 unsigned int num, total;
636 CONTEXT *ctx = fc->ctx;
637 NNTP_DATA *data = (NNTP_DATA *)ctx->data;
642 if (ctx->msgcount >= ctx->hdrmax)
643 mx_alloc_memory (ctx);
644 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
645 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
647 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
648 num = ctx->hdrs[ctx->msgcount]->article_num;
650 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base])
653 if (num > data->lastLoaded)
654 data->lastLoaded = num;
655 num = num - fc->first + 1;
656 total = fc->last - fc->first + 1;
657 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
658 mutt_message ("%s %d/%d", fc->msg, num, total);
661 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
667 static int nntp_fetch_headers (CONTEXT *ctx, unsigned int first,
670 char buf[HUGE_STRING];
671 char *msg = _("Fetching message headers...");
672 NNTP_DATA *nntp_data = ((NNTP_DATA *)ctx->data);
676 unsigned int current;
680 /* if empty group or nothing to do */
681 if (!last || first > last)
684 /* fetch list of articles */
685 mutt_message _("Fetching list of articles...");
689 fc.messages = safe_calloc (last - first + 1, sizeof (unsigned short));
690 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
691 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) != 0)
693 mutt_error (_("LISTGROUP command failed: %s"), buf);
695 nntp_error ("nntp_fetch_headers()", buf);
701 /* CACHE: must be loaded xover cache here */
702 num = nntp_data->lastCached - first + 1;
703 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0)
705 nntp_cache_expand (buf, nntp_data->cache);
706 mutt_message _("Fetching headers from cache...");
707 if ((f = safe_fopen (buf, "r")))
711 /* counting number of lines */
712 while (fgets (buf, sizeof (buf), f) != NULL)
715 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
717 oldmsgcount = ctx->msgcount;
719 fc.last = first + num - 1;
721 while (fgets (buf, sizeof (buf), f) != NULL)
722 add_xover_line (buf, &fc);
724 nntp_data->lastLoaded = fc.last;
726 if (ctx->msgcount > oldmsgcount)
727 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
730 nntp_delete_cache (nntp_data);
732 num = last - first + 1;
740 * Without XOVER, we have to fetch each article header and parse
741 * it. With XOVER, we ask for all of them
744 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);
756 mutt_error (_("XOVER command failed: %s"), buf);
758 nntp_error ("nntp_fetch_headers()", buf);
766 for (current = first; current <= last; current++)
770 ret = current - first + 1;
771 mutt_message ("%s %d/%d", msg, ret, num);
773 if (!fc.messages[current - fc.base])
776 if (ctx->msgcount >= ctx->hdrmax)
777 mx_alloc_memory (ctx);
778 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
779 h->index = ctx->msgcount;
781 ret = nntp_read_header (ctx, NULL, current);
782 if (ret == 0) /* Got article. Fetch next header */
784 nntp_get_status (ctx, h, NULL, h->article_num);
786 mx_update_context (ctx, 1);
789 mutt_free_header (&h); /* skip it */
796 if (current > nntp_data->lastLoaded)
797 nntp_data->lastLoaded = current;
800 nntp_data->lastLoaded = last;
806 * currently, nntp "mailbox" is "newsgroup"
808 int nntp_open_mailbox (CONTEXT *ctx)
810 NNTP_DATA *nntp_data;
812 char buf[HUGE_STRING];
813 char server[LONG_STRING];
818 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)
835 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (buf) + 1);
836 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
837 strcpy (nntp_data->group, buf);
838 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
839 nntp_add_to_list (serv, nntp_data);
841 ctx->data = nntp_data;
842 nntp_data->nserv = serv;
844 mutt_message (_("Selecting %s..."), nntp_data->group);
846 if (!nntp_data->desc)
848 nntp_get_desc (nntp_data, nntp_data->group, NULL);
850 nntp_save_cache_index (serv);
854 if (mutt_nntp_query (nntp_data, buf, sizeof(buf)) < 0)
857 nntp_error ("nntp_open_mailbox()", buf);
862 if (mutt_strncmp ("211", buf, 3))
864 LIST *l = serv->list;
866 /* GROUP command failed */
867 if (!mutt_strncmp ("411", buf, 3))
869 mutt_error (_("Newsgroup %s not found on server %s"),
870 nntp_data->group, serv->conn->account.host);
872 /* CACHE: delete cache and line from .index */
873 nntp_delete_cache (nntp_data);
874 hash_delete (serv->newsgroups, nntp_data->group, NULL, nntp_delete_data);
875 while (l && l->data != (void *) nntp_data) l = l->next;
885 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
886 &nntp_data->lastMessage, buf);
888 nntp_data->deleted = 0;
890 time (&serv->check_time);
893 * Check for max adding context. If it is greater than $nntp_context,
894 * strip off extra articles
896 first = nntp_data->firstMessage;
897 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
898 first = nntp_data->lastMessage - NntpContext + 1;
900 nntp_data->lastLoaded = first - 1;
901 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
904 int nntp_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
906 char buf[LONG_STRING];
907 char path[_POSIX_PATH_MAX];
909 char *m = _("Fetching message...");
912 /* see if we already have the message in our cache */
913 cache = &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index % NNTP_CACHE_LEN];
915 /* if everything is fine, assign msg->fp and return */
916 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
917 (msg->fp = fopen (cache->path, "r")))
920 /* clear the previous entry */
921 unlink (cache->path);
926 cache->index = ctx->hdrs[msgno]->index;
928 cache->path = safe_strdup (path);
929 if (!(msg->fp = safe_fopen (path, "w+")))
935 if (ctx->hdrs[msgno]->article_num == 0)
936 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
937 ctx->hdrs[msgno]->env->message_id);
939 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
940 ctx->hdrs[msgno]->article_num);
942 ret = mutt_nntp_fetch ((NNTP_DATA *)ctx->data, buf, m, nntp_read_tempfile,
943 msg->fp, ctx->tagged);
946 mutt_error (_("Article %d not found on server"),
947 ctx->hdrs[msgno]->article_num);
948 dprint (1, (debugfile, "nntp_fetch_message: %s\n", buf));
959 mutt_free_envelope (&ctx->hdrs[msgno]->env);
960 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
961 /* fix content length */
962 fseek(msg->fp, 0, SEEK_END);
963 ctx->hdrs[msgno]->content->length = ftell (msg->fp) -
964 ctx->hdrs[msgno]->content->offset;
966 /* this is called in mutt before the open which fetches the message,
967 * which is probably wrong, but we just call it again here to handle
968 * the problem instead of fixing it.
970 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
972 /* These would normally be updated in mx_update_context(), but the
973 * full headers aren't parsed with XOVER, so the information wasn't
976 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
977 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
978 #endif /* HAVE_PGP || HAVE_SMIME */
987 int nntp_post (const char *msg) {
988 char buf[LONG_STRING];
991 NNTP_DATA *nntp_data;
993 if (Context && Context->magic == M_NNTP)
994 nntp_data = (NNTP_DATA *)Context->data;
997 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
998 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data)
1000 mutt_error (_("Can't post article. No connection to news server."));
1003 nntp_data = (NNTP_DATA *)CurrentNewsSrv->list->data;
1006 if (!(f = safe_fopen (msg, "r")))
1008 mutt_error (_("Can't post article. Unable to open %s"), msg);
1012 strfcpy (buf, "POST\r\n", sizeof (buf));
1013 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1015 mutt_error (_("Can't post article. Connection to %s lost."),
1016 nntp_data->nserv->conn->account.host);
1021 mutt_error (_("Can't post article: %s"), buf);
1027 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL)
1030 if (buf[len - 1] == '\n')
1032 buf[len - 1] = '\r';
1038 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1040 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1044 if (buf[strlen (buf) - 1] != '\n')
1045 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1046 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1047 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0)
1049 mutt_error (_("Can't post article. Connection to %s lost."),
1050 nntp_data->nserv->conn->account.host);
1055 mutt_error (_("Can't post article: %s"), buf);
1062 /* nntp_logout_all: close all open connections. */
1063 void nntp_logout_all (void)
1065 char buf[LONG_STRING];
1069 conn = mutt_socket_head ();
1075 if (conn->account.type == M_ACCT_TYPE_NNTP)
1077 mutt_message (_("Closing connection to %s..."), conn->account.host);
1078 mutt_socket_write (conn, "QUIT\r\n");
1079 mutt_socket_readln (buf, sizeof (buf), conn);
1080 mutt_clear_error ();
1081 mutt_socket_close (conn);
1083 mutt_socket_free (tmp);
1090 static void nntp_free_acache (NNTP_DATA *data)
1094 for (i = 0; i < NNTP_CACHE_LEN; i++)
1096 if (data->acache[i].path)
1098 unlink (data->acache[i].path);
1099 FREE (&data->acache[i].path);
1104 void nntp_delete_data (void *p)
1106 NNTP_DATA *data = (NNTP_DATA *)p;
1110 FREE (&data->entries);
1112 FREE (&data->cache);
1113 nntp_free_acache (data);
1117 int nntp_sync_mailbox (CONTEXT *ctx)
1119 NNTP_DATA *data = ctx->data;
1121 /* CACHE: update cache and .index files */
1122 if ((option (OPTSAVEUNSUB) || data->subscribed))
1123 nntp_save_cache_group (ctx);
1124 nntp_free_acache (data);
1126 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1130 void nntp_fastclose_mailbox (CONTEXT *ctx)
1132 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1136 nntp_free_acache (data);
1137 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1139 nntp_save_cache_index (data->nserv);
1140 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1142 nntp_delete_data (data);
1145 /* commit changes and terminate connection */
1146 int nntp_close_mailbox (CONTEXT *ctx)
1150 mutt_message _("Quitting newsgroup...");
1153 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1156 if (data->nserv && data->nserv->conn && ctx->unread)
1158 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1160 mutt_newsgroup_catchup (data->nserv, data->group);
1165 nntp_sync_mailbox (ctx);
1166 if (ctx->data && ((NNTP_DATA *)ctx->data)->nserv)
1170 news = ((NNTP_DATA *)ctx->data)->nserv;
1171 newsrc_gen_entries (ctx);
1172 ((NNTP_DATA *)ctx->data)->unread = ctx->unread;
1173 mutt_newsrc_update (news);
1179 /* use the GROUP command to poll for new mail */
1180 static int _nntp_check_mailbox (CONTEXT *ctx, NNTP_DATA *nntp_data)
1182 char buf[LONG_STRING];
1185 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1189 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1192 nntp_error ("nntp_check_mailbox()", buf);
1196 if (mutt_strncmp ("211", buf, 3))
1199 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1202 nntp_error ("nntp_check_mailbox()", buf);
1207 if (!mutt_strncmp ("211", buf, 3))
1212 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1213 nntp_data->firstMessage = first;
1214 nntp_data->lastMessage = last;
1215 if (ctx && last > nntp_data->lastLoaded)
1217 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1218 time (&nntp_data->nserv->check_time);
1221 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1222 nntp_data->unread = count;
1224 mutt_newsgroup_stat (nntp_data);
1225 /* active was renumbered? */
1226 if (last < nntp_data->lastLoaded)
1228 if (!nntp_data->max)
1230 nntp_data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1233 nntp_data->lastCached = 0;
1235 nntp_data->entries[0].first = 1;
1236 nntp_data->entries[0].last = 0;
1240 time (&nntp_data->nserv->check_time);
1244 int nntp_check_mailbox (CONTEXT *ctx)
1246 return _nntp_check_mailbox (ctx, (NNTP_DATA *)ctx->data);
1249 static int add_group (char *buf, void *serv)
1251 #define s ((NNTP_SERVER *) serv)
1252 char group[LONG_STRING], mod, desc[HUGE_STRING];
1254 NNTP_DATA *nntp_data;
1257 _checked = n; /* _checked have N, where N = number of groups */
1258 if (!buf) /* at EOF must be zerouth */
1265 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1268 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL)
1271 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
1272 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1273 strcpy (nntp_data->group, group);
1274 nntp_data->nserv = s;
1275 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1276 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1277 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1278 nntp_add_to_list (s, nntp_data);
1280 nntp_data->deleted = 0;
1281 nntp_data->firstMessage = first;
1282 nntp_data->lastMessage = last;
1284 nntp_data->allowed = 1;
1286 nntp_data->allowed = 0;
1287 if (nntp_data->desc)
1288 FREE (&nntp_data->desc);
1290 nntp_data->desc = safe_strdup (desc);
1291 if (nntp_data->rc || nntp_data->lastCached)
1292 mutt_newsgroup_stat (nntp_data);
1293 else if (nntp_data->lastMessage &&
1294 nntp_data->firstMessage <= nntp_data->lastMessage)
1295 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1297 nntp_data->unread = 0;
1303 int nntp_check_newgroups (NNTP_SERVER *serv, int force)
1305 char buf[LONG_STRING];
1306 NNTP_DATA nntp_data;
1312 if (!serv || !serv->newgroups_time)
1315 if (nntp_open_connection (serv) < 0)
1318 /* check subscribed groups for new news */
1319 if (option (OPTSHOWNEWNEWS))
1321 mutt_message _("Checking for new messages...");
1322 for (l = serv->list; l; l = l->next)
1324 serv->check_time = 0; /* really check! */
1325 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1326 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1332 mutt_message _("Checking for new newsgroups...");
1333 now = serv->newgroups_time;
1334 time (&serv->newgroups_time);
1336 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1337 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min,
1339 nntp_data.nserv = serv;
1340 if (Context && Context->magic == M_NNTP)
1341 nntp_data.group = ((NNTP_DATA *)Context->data)->group;
1343 nntp_data.group = NULL;
1345 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1346 add_group, serv, 0) != 0)
1349 nntp_error ("nntp_check_newgroups()", buf);
1354 mutt_message _("Loading descriptions...");
1358 emp.next = serv->list;
1363 ((NNTP_DATA *) l->data)->new = 1;
1364 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group, NULL);
1367 nntp_save_cache_index (serv);
1368 mutt_clear_error ();
1372 /* Load list of all newsgroups from cache ALL */
1373 int nntp_get_cache_all (NNTP_SERVER *serv)
1375 char buf[HUGE_STRING];
1378 nntp_cache_expand (buf, serv->cache);
1379 if ((f = safe_fopen (buf, "r")))
1383 while (fgets (buf, sizeof(buf), f) != NULL)
1385 if (ReadInc && (i % ReadInc == 0))
1386 mutt_message (_("Loading list from cache... %d"), i);
1387 add_group (buf, serv);
1390 add_group (NULL, NULL);
1392 mutt_clear_error ();
1397 FREE (&serv->cache);
1402 /* Load list of all newsgroups from active */
1403 int nntp_get_active (NNTP_SERVER *serv)
1405 char msg[SHORT_STRING];
1406 NNTP_DATA nntp_data;
1409 if (nntp_open_connection (serv) < 0)
1412 snprintf (msg, sizeof(msg), _("Loading list of all newsgroups on server %s..."),
1413 serv->conn->account.host);
1415 time (&serv->newgroups_time);
1416 nntp_data.nserv = serv;
1417 nntp_data.group = NULL;
1419 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0)
1422 nntp_error ("nntp_get_active()", "LIST\r\n");
1427 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1429 nntp_get_desc (&nntp_data, "*", msg);
1431 for (tmp = serv->list; tmp; tmp = tmp->next)
1433 NNTP_DATA *data = (NNTP_DATA *)tmp->data;
1435 if (data && data->deleted && !data->rc)
1437 nntp_delete_cache (data);
1438 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1442 nntp_save_cache_index (serv);
1444 mutt_clear_error ();
1449 * returns -1 if error ocurred while retrieving header,
1450 * number of articles which ones exist in context on success.
1452 int nntp_check_msgid (CONTEXT *ctx, const char *msgid)
1456 /* if msgid is already in context, don't reload them */
1457 if (hash_find (ctx->id_hash, msgid))
1459 if (ctx->msgcount == ctx->hdrmax)
1460 mx_alloc_memory (ctx);
1461 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1462 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1464 mutt_message (_("Fetching %s from server..."), msgid);
1465 ret = nntp_read_header (ctx, msgid, 0);
1466 /* since nntp_read_header() may set read flag, we must reset it */
1467 ctx->hdrs[ctx->msgcount]->read = 0;
1469 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1473 mx_update_context (ctx, 1);
1484 unsigned int *child;
1487 static int check_children (char *s, void *c)
1489 #define cc ((CHILD_CONTEXT *) c)
1492 if (!s || (n = atoi (s)) == 0)
1494 for (i = 0; i < cc->ctx->msgcount; i++)
1495 if (cc->ctx->hdrs[i]->article_num == n)
1497 if (cc->num >= cc->max)
1498 safe_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1499 cc->child[cc->num++] = n;
1505 int nntp_check_children (CONTEXT *ctx, const char *msgid)
1507 NNTP_DATA *nntp_data = (NNTP_DATA *)ctx->data;
1509 int i, ret = 0, tmp = 0;
1512 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1513 !nntp_data->nserv->conn->account.host)
1515 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1517 if (!nntp_data->nserv->hasXPAT)
1519 mutt_error (_("Server %s does not support this operation!"),
1520 nntp_data->nserv->conn->account.host);
1524 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1525 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1530 cc.child = safe_malloc (sizeof (unsigned int) * 25);
1531 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0))
1536 /* dont try to read the xover cache. check_children() already
1537 * made sure that we dont have the article, so we need to visit
1538 * the server. Reading the cache at this point is also bad
1539 * because it would duplicate messages */
1540 if (option (OPTNEWSCACHE))
1543 unset_option (OPTNEWSCACHE);
1545 for (i = 0; i < cc.num; i++)
1547 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1549 if (ctx->msgcount &&
1550 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1551 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1554 set_option (OPTNEWSCACHE);