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, "XOVER\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))
189 mutt_socket_write (conn, "XPAT\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, "XGTITLE +\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))
199 serv->hasXGTITLE = 1;
201 if (!mutt_strncmp ("282", buf, 3))
205 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
206 return nntp_connect_error (serv);
207 } while (!(buf[0] == '.' && buf[1] == '\0'));
213 static int nntp_open_connection (NNTP_SERVER *serv)
215 if (serv->status == NNTP_OK)
217 if (serv->status == NNTP_BYE)
219 if (nntp_connect_and_auth (serv) < 0)
221 if (nntp_attempt_features (serv) < 0)
226 static int nntp_reconnect (NNTP_SERVER *serv)
228 char buf[SHORT_STRING];
230 mutt_socket_close (serv->conn);
234 if (nntp_connect_and_auth (serv) == 0)
237 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
238 serv->conn->account.host);
239 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES)
241 serv->status = NNTP_BYE;
247 /* Send data from line[LONG_STRING] and receive answer to same line */
248 static int mutt_nntp_query (NNTP_DATA *data, char *line, size_t linelen)
250 char buf[LONG_STRING];
253 if (data->nserv->status == NNTP_BYE)
260 mutt_socket_write (data->nserv->conn, line);
262 else if (data->group)
264 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
265 mutt_socket_write (data->nserv->conn, buf);
269 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
271 if (nntp_reconnect (data->nserv) < 0)
276 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
277 mutt_socket_write (data->nserv->conn, buf);
278 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
284 else if ((!mutt_strncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
288 strfcpy (line, buf, linelen);
293 * This function calls funct(*line, *data) for each received line,
294 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
297 * 1 - correct but not performed (may be, have to be continued),
298 * -1 - conection lost,
299 * -2 - invalid command or execution error,
300 * -3 - error in funct(*line, *data).
302 static int mutt_nntp_fetch (NNTP_DATA *nntp_data, char *query, char *msg,
303 int (*funct) (char *, void *), void *data, int tagged)
305 char buf[LONG_STRING];
314 strfcpy (buf, query, sizeof (buf));
315 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
324 inbuf = safe_malloc (sizeof (buf));
328 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
334 if (!lenbuf && buf[0] == '.')
345 strfcpy (inbuf + lenbuf, p, sizeof (buf));
347 if (chunk >= sizeof (buf))
349 lenbuf += strlen (p);
354 if (msg && ReadInc && (line % ReadInc == 0)) {
356 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
358 mutt_message ("%s %d", msg, line);
361 if (ret == 0 && funct (inbuf, data) < 0)
366 safe_realloc (&inbuf, lenbuf + sizeof (buf));
375 static int nntp_read_tempfile (char *line, void *file)
377 FILE *f = (FILE *)file;
384 if (fputc ('\n', f) == EOF)
390 static void nntp_parse_xref (CONTEXT *ctx, char *group, char *xref, HEADER *h)
392 register char *p, *b;
393 register char *colon = NULL;
398 /* skip to next word */
400 while (*b && ((*b == ' ') || (*b == '\t'))) b++;
403 /* skip to end of word */
404 while (*p && (*p != ' ') && (*p != '\t'))
419 nntp_get_status (ctx, h, p, atoi(colon));
420 if (h && h->article_num == 0 && mutt_strcmp (group, b) == 0)
421 h->article_num = atoi(colon);
429 * 1 if article not found
430 * -1 if read or write error on tempfile or socket
432 static int nntp_read_header (CONTEXT *ctx, const char *msgid, int article_num)
434 NNTP_DATA *nntp_data = ((NNTP_DATA *)ctx->data);
436 char buf[LONG_STRING];
437 char tempfile[_POSIX_PATH_MAX];
439 HEADER *h = ctx->hdrs[ctx->msgcount];
441 mutt_mktemp (tempfile);
442 if (!(f = safe_fopen (tempfile, "w+")))
446 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
448 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
450 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
455 dprint(1, (debugfile, "nntp_read_header: %s\n", buf));
459 return (ret == -1 ? -1 : 1);
462 h->article_num = article_num;
463 h->env = mutt_read_rfc822_header (f, h, 0, 0);
467 if (h->env->xref != NULL)
468 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
469 else if (h->article_num == 0 && msgid)
471 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
472 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
473 h->article_num = atoi (buf + 4);
479 static int parse_description (char *line, void *n)
481 #define news ((NNTP_SERVER *) n)
482 register char *d = line;
487 while (*d && *d != '\t' && *d != ' ') d++;
490 while (*d && (*d == '\t' || *d == ' ')) d++;
491 dprint (2, (debugfile, "group: %s, desc: %s\n", line, d));
492 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
493 mutt_strcmp (d, data->desc))
496 data->desc = safe_strdup (d);
502 static void nntp_get_desc (NNTP_DATA *data, char *mask, char *msg)
506 if (!option (OPTLOADDESC) || !data || !data->nserv)
509 /* Get newsgroup description, if we can */
510 if (data->nserv->hasXGTITLE)
511 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
513 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
514 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) != 0)
517 nntp_error ("nntp_get_desc()", buf);
523 * XOVER returns a tab separated list of:
524 * id|subject|from|date|Msgid|references|bytes|lines|xref
526 * This has to duplicate some of the functionality of
527 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
528 * a limited number of headers which are "parsed" by placement in the list)
530 static int nntp_parse_xover (CONTEXT *ctx, char *buf, HEADER *hdr)
532 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
536 hdr->env = mutt_new_envelope();
537 hdr->env->newsgroups = safe_strdup (nntp_data->group);
538 hdr->content = mutt_new_body();
539 hdr->content->type = TYPETEXT;
540 hdr->content->subtype = safe_strdup ("plain");
541 hdr->content->encoding = ENC7BIT;
542 hdr->content->disposition = DISPINLINE;
543 hdr->content->length = -1;
546 for (x = 0; !done && x < 9; x++)
548 /* if from file, need to skip newline character */
549 while (*p && *p != '\n' && *p != '\t') p++;
557 hdr->article_num = atoi (b);
558 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
561 hdr->env->subject = safe_strdup (b);
562 /* Now we need to do the things which would normally be done in
563 * mutt_read_rfc822_header() */
564 if (hdr->env->subject)
566 regmatch_t pmatch[1];
568 rfc2047_decode (&hdr->env->subject);
570 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
571 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
573 hdr->env->real_subj = hdr->env->subject;
577 rfc822_free_address (&hdr->env->from);
578 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
579 rfc2047_decode_adrlist (hdr->env->from);
582 hdr->date_sent = mutt_parse_date (b, hdr);
583 hdr->received = hdr->date_sent;
586 FREE (&hdr->env->message_id);
587 hdr->env->message_id = safe_strdup (b);
590 mutt_free_list (&hdr->env->references);
591 hdr->env->references = mutt_parse_references (b, 0);
594 hdr->content->length = atoi (b);
597 hdr->lines = atoi (b);
601 FREE (&hdr->env->xref);
602 b = b + 6; /* skips the "Xref: " */
603 hdr->env->xref = safe_strdup (b);
604 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
619 unsigned short *messages;
623 #define fc ((FETCH_CONTEXT *) c)
624 static int nntp_fetch_numbers (char *line, void *c)
631 if (num < fc->base || num > fc->last)
633 fc->messages[num - fc->base] = 1;
637 static int add_xover_line (char *line, void *c)
639 unsigned int num, total;
640 CONTEXT *ctx = fc->ctx;
641 NNTP_DATA *data = (NNTP_DATA *)ctx->data;
646 if (ctx->msgcount >= ctx->hdrmax)
647 mx_alloc_memory (ctx);
648 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
649 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
651 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
652 num = ctx->hdrs[ctx->msgcount]->article_num;
654 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base])
657 if (num > data->lastLoaded)
658 data->lastLoaded = num;
659 num = num - fc->first + 1;
660 total = fc->last - fc->first + 1;
661 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
662 mutt_message ("%s %d/%d", fc->msg, num, total);
665 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
671 static int nntp_fetch_headers (CONTEXT *ctx, unsigned int first,
674 char buf[HUGE_STRING];
675 char *msg = _("Fetching message headers...");
676 NNTP_DATA *nntp_data = ((NNTP_DATA *)ctx->data);
680 unsigned int current;
684 /* if empty group or nothing to do */
685 if (!last || first > last)
688 /* fetch list of articles */
689 mutt_message _("Fetching list of articles...");
693 fc.messages = safe_calloc (last - first + 1, sizeof (unsigned short));
694 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
695 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) != 0)
697 mutt_error (_("LISTGROUP command failed: %s"), buf);
699 nntp_error ("nntp_fetch_headers()", buf);
705 /* CACHE: must be loaded xover cache here */
706 num = nntp_data->lastCached - first + 1;
707 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0)
709 nntp_cache_expand (buf, nntp_data->cache);
710 mutt_message _("Fetching headers from cache...");
711 if ((f = safe_fopen (buf, "r")))
715 /* counting number of lines */
716 while (fgets (buf, sizeof (buf), f) != NULL)
719 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
721 oldmsgcount = ctx->msgcount;
723 fc.last = first + num - 1;
725 while (fgets (buf, sizeof (buf), f) != NULL)
726 add_xover_line (buf, &fc);
728 nntp_data->lastLoaded = fc.last;
730 if (ctx->msgcount > oldmsgcount)
731 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
734 nntp_delete_cache (nntp_data);
736 num = last - first + 1;
744 * Without XOVER, we have to fetch each article header and parse
745 * it. With XOVER, we ask for all of them
748 if (nntp_data->nserv->hasXOVER)
750 oldmsgcount = ctx->msgcount;
754 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
755 ret = mutt_nntp_fetch (nntp_data, buf, NULL, add_xover_line, &fc, 0);
756 if (ctx->msgcount > oldmsgcount)
757 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
760 mutt_error (_("XOVER command failed: %s"), buf);
762 nntp_error ("nntp_fetch_headers()", buf);
770 for (current = first; current <= last; current++)
774 ret = current - first + 1;
775 mutt_message ("%s %d/%d", msg, ret, num);
777 if (!fc.messages[current - fc.base])
780 if (ctx->msgcount >= ctx->hdrmax)
781 mx_alloc_memory (ctx);
782 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
783 h->index = ctx->msgcount;
785 ret = nntp_read_header (ctx, NULL, current);
786 if (ret == 0) /* Got article. Fetch next header */
788 nntp_get_status (ctx, h, NULL, h->article_num);
790 mx_update_context (ctx, 1);
793 mutt_free_header (&h); /* skip it */
800 if (current > nntp_data->lastLoaded)
801 nntp_data->lastLoaded = current;
804 nntp_data->lastLoaded = last;
810 * currently, nntp "mailbox" is "newsgroup"
812 int nntp_open_mailbox (CONTEXT *ctx)
814 NNTP_DATA *nntp_data;
816 char buf[HUGE_STRING];
817 char server[LONG_STRING];
822 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf)
824 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
830 nntp_expand_path (server, sizeof (server), &acct);
831 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
834 CurrentNewsSrv = serv;
836 /* create NNTP-specific state struct if nof found in list */
837 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL)
839 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (buf) + 1);
840 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
841 strcpy (nntp_data->group, buf);
842 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
843 nntp_add_to_list (serv, nntp_data);
845 ctx->data = nntp_data;
846 nntp_data->nserv = serv;
848 mutt_message (_("Selecting %s..."), nntp_data->group);
850 if (!nntp_data->desc)
852 nntp_get_desc (nntp_data, nntp_data->group, NULL);
854 nntp_save_cache_index (serv);
858 if (mutt_nntp_query (nntp_data, buf, sizeof(buf)) < 0)
861 nntp_error ("nntp_open_mailbox()", buf);
866 if (mutt_strncmp ("211", buf, 3))
868 LIST *l = serv->list;
870 /* GROUP command failed */
871 if (!mutt_strncmp ("411", buf, 3))
873 mutt_error (_("Newsgroup %s not found on server %s"),
874 nntp_data->group, serv->conn->account.host);
876 /* CACHE: delete cache and line from .index */
877 nntp_delete_cache (nntp_data);
878 hash_delete (serv->newsgroups, nntp_data->group, NULL, nntp_delete_data);
879 while (l && l->data != (void *) nntp_data) l = l->next;
889 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
890 &nntp_data->lastMessage, buf);
892 nntp_data->deleted = 0;
894 time (&serv->check_time);
897 * Check for max adding context. If it is greater than $nntp_context,
898 * strip off extra articles
900 first = nntp_data->firstMessage;
901 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
902 first = nntp_data->lastMessage - NntpContext + 1;
904 nntp_data->lastLoaded = first - 1;
905 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
908 int nntp_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
910 char buf[LONG_STRING];
911 char path[_POSIX_PATH_MAX];
913 char *m = _("Fetching message...");
916 /* see if we already have the message in our cache */
917 cache = &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index % NNTP_CACHE_LEN];
919 /* if everything is fine, assign msg->fp and return */
920 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
921 (msg->fp = fopen (cache->path, "r")))
924 /* clear the previous entry */
925 unlink (cache->path);
930 cache->index = ctx->hdrs[msgno]->index;
932 cache->path = safe_strdup (path);
933 if (!(msg->fp = safe_fopen (path, "w+")))
939 if (ctx->hdrs[msgno]->article_num == 0)
940 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
941 ctx->hdrs[msgno]->env->message_id);
943 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
944 ctx->hdrs[msgno]->article_num);
946 ret = mutt_nntp_fetch ((NNTP_DATA *)ctx->data, buf, m, nntp_read_tempfile,
947 msg->fp, ctx->tagged);
950 mutt_error (_("Article %d not found on server"),
951 ctx->hdrs[msgno]->article_num);
952 dprint (1, (debugfile, "nntp_fetch_message: %s\n", buf));
963 mutt_free_envelope (&ctx->hdrs[msgno]->env);
964 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
965 /* fix content length */
966 fseek(msg->fp, 0, SEEK_END);
967 ctx->hdrs[msgno]->content->length = ftell (msg->fp) -
968 ctx->hdrs[msgno]->content->offset;
970 /* this is called in mutt before the open which fetches the message,
971 * which is probably wrong, but we just call it again here to handle
972 * the problem instead of fixing it.
974 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
976 /* These would normally be updated in mx_update_context(), but the
977 * full headers aren't parsed with XOVER, so the information wasn't
980 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
981 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
982 #endif /* HAVE_PGP || HAVE_SMIME */
991 int nntp_post (const char *msg) {
992 char buf[LONG_STRING];
995 NNTP_DATA *nntp_data;
997 if (Context && Context->magic == M_NNTP)
998 nntp_data = (NNTP_DATA *)Context->data;
1001 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1002 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data)
1004 mutt_error (_("Can't post article. No connection to news server."));
1007 nntp_data = (NNTP_DATA *)CurrentNewsSrv->list->data;
1010 if (!(f = safe_fopen (msg, "r")))
1012 mutt_error (_("Can't post article. Unable to open %s"), msg);
1016 strfcpy (buf, "POST\r\n", sizeof (buf));
1017 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1019 mutt_error (_("Can't post article. Connection to %s lost."),
1020 nntp_data->nserv->conn->account.host);
1025 mutt_error (_("Can't post article: %s"), buf);
1031 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL)
1034 if (buf[len - 1] == '\n')
1036 buf[len - 1] = '\r';
1042 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1044 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1048 if (buf[strlen (buf) - 1] != '\n')
1049 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1050 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1051 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0)
1053 mutt_error (_("Can't post article. Connection to %s lost."),
1054 nntp_data->nserv->conn->account.host);
1059 mutt_error (_("Can't post article: %s"), buf);
1066 /* nntp_logout_all: close all open connections. */
1067 void nntp_logout_all (void)
1069 char buf[LONG_STRING];
1073 conn = mutt_socket_head ();
1079 if (conn->account.type == M_ACCT_TYPE_NNTP)
1081 mutt_message (_("Closing connection to %s..."), conn->account.host);
1082 mutt_socket_write (conn, "QUIT\r\n");
1083 mutt_socket_readln (buf, sizeof (buf), conn);
1084 mutt_clear_error ();
1085 mutt_socket_close (conn);
1087 mutt_socket_free (tmp);
1094 static void nntp_free_acache (NNTP_DATA *data)
1098 for (i = 0; i < NNTP_CACHE_LEN; i++)
1100 if (data->acache[i].path)
1102 unlink (data->acache[i].path);
1103 FREE (&data->acache[i].path);
1108 void nntp_delete_data (void *p)
1110 NNTP_DATA *data = (NNTP_DATA *)p;
1114 FREE (&data->entries);
1116 FREE (&data->cache);
1117 nntp_free_acache (data);
1121 int nntp_sync_mailbox (CONTEXT *ctx)
1123 NNTP_DATA *data = ctx->data;
1125 /* CACHE: update cache and .index files */
1126 if ((option (OPTSAVEUNSUB) || data->subscribed))
1127 nntp_save_cache_group (ctx);
1128 nntp_free_acache (data);
1130 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1134 void nntp_fastclose_mailbox (CONTEXT *ctx)
1136 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1140 nntp_free_acache (data);
1141 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1143 nntp_save_cache_index (data->nserv);
1144 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1146 nntp_delete_data (data);
1149 /* commit changes and terminate connection */
1150 int nntp_close_mailbox (CONTEXT *ctx)
1154 mutt_message _("Quitting newsgroup...");
1157 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1160 if (data->nserv && data->nserv->conn && ctx->unread)
1162 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1164 mutt_newsgroup_catchup (data->nserv, data->group);
1169 nntp_sync_mailbox (ctx);
1170 if (ctx->data && ((NNTP_DATA *)ctx->data)->nserv)
1174 news = ((NNTP_DATA *)ctx->data)->nserv;
1175 newsrc_gen_entries (ctx);
1176 ((NNTP_DATA *)ctx->data)->unread = ctx->unread;
1177 mutt_newsrc_update (news);
1183 /* use the GROUP command to poll for new mail */
1184 static int _nntp_check_mailbox (CONTEXT *ctx, NNTP_DATA *nntp_data)
1186 char buf[LONG_STRING];
1189 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1193 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1196 nntp_error ("nntp_check_mailbox()", buf);
1200 if (mutt_strncmp ("211", buf, 3))
1203 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
1206 nntp_error ("nntp_check_mailbox()", buf);
1211 if (!mutt_strncmp ("211", buf, 3))
1216 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1217 nntp_data->firstMessage = first;
1218 nntp_data->lastMessage = last;
1219 if (ctx && last > nntp_data->lastLoaded)
1221 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1222 time (&nntp_data->nserv->check_time);
1225 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1226 nntp_data->unread = count;
1228 mutt_newsgroup_stat (nntp_data);
1229 /* active was renumbered? */
1230 if (last < nntp_data->lastLoaded)
1232 if (!nntp_data->max)
1234 nntp_data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1237 nntp_data->lastCached = 0;
1239 nntp_data->entries[0].first = 1;
1240 nntp_data->entries[0].last = 0;
1244 time (&nntp_data->nserv->check_time);
1248 int nntp_check_mailbox (CONTEXT *ctx)
1250 return _nntp_check_mailbox (ctx, (NNTP_DATA *)ctx->data);
1253 static int add_group (char *buf, void *serv)
1255 #define s ((NNTP_SERVER *) serv)
1256 char group[LONG_STRING], mod, desc[HUGE_STRING];
1258 NNTP_DATA *nntp_data;
1261 _checked = n; /* _checked have N, where N = number of groups */
1262 if (!buf) /* at EOF must be zerouth */
1269 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1272 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL)
1275 nntp_data = safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
1276 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1277 strcpy (nntp_data->group, group);
1278 nntp_data->nserv = s;
1279 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1280 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1281 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1282 nntp_add_to_list (s, nntp_data);
1284 nntp_data->deleted = 0;
1285 nntp_data->firstMessage = first;
1286 nntp_data->lastMessage = last;
1288 nntp_data->allowed = 1;
1290 nntp_data->allowed = 0;
1291 if (nntp_data->desc)
1292 FREE (&nntp_data->desc);
1294 nntp_data->desc = safe_strdup (desc);
1295 if (nntp_data->rc || nntp_data->lastCached)
1296 mutt_newsgroup_stat (nntp_data);
1297 else if (nntp_data->lastMessage &&
1298 nntp_data->firstMessage <= nntp_data->lastMessage)
1299 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1301 nntp_data->unread = 0;
1307 int nntp_check_newgroups (NNTP_SERVER *serv, int force)
1309 char buf[LONG_STRING];
1310 NNTP_DATA nntp_data;
1316 if (!serv || !serv->newgroups_time)
1319 if (nntp_open_connection (serv) < 0)
1322 /* check subscribed groups for new news */
1323 if (option (OPTSHOWNEWNEWS))
1325 mutt_message _("Checking for new messages...");
1326 for (l = serv->list; l; l = l->next)
1328 serv->check_time = 0; /* really check! */
1329 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1330 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1336 mutt_message _("Checking for new newsgroups...");
1337 now = serv->newgroups_time;
1338 time (&serv->newgroups_time);
1340 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1341 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min,
1343 nntp_data.nserv = serv;
1344 if (Context && Context->magic == M_NNTP)
1345 nntp_data.group = ((NNTP_DATA *)Context->data)->group;
1347 nntp_data.group = NULL;
1349 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1350 add_group, serv, 0) != 0)
1353 nntp_error ("nntp_check_newgroups()", buf);
1358 mutt_message _("Loading descriptions...");
1362 emp.next = serv->list;
1367 ((NNTP_DATA *) l->data)->new = 1;
1368 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group, NULL);
1371 nntp_save_cache_index (serv);
1372 mutt_clear_error ();
1376 /* Load list of all newsgroups from cache ALL */
1377 int nntp_get_cache_all (NNTP_SERVER *serv)
1379 char buf[HUGE_STRING];
1382 nntp_cache_expand (buf, serv->cache);
1383 if ((f = safe_fopen (buf, "r")))
1387 while (fgets (buf, sizeof(buf), f) != NULL)
1389 if (ReadInc && (i % ReadInc == 0))
1390 mutt_message (_("Loading list from cache... %d"), i);
1391 add_group (buf, serv);
1394 add_group (NULL, NULL);
1396 mutt_clear_error ();
1401 FREE (&serv->cache);
1406 /* Load list of all newsgroups from active */
1407 int nntp_get_active (NNTP_SERVER *serv)
1409 char msg[SHORT_STRING];
1410 NNTP_DATA nntp_data;
1413 if (nntp_open_connection (serv) < 0)
1416 snprintf (msg, sizeof(msg), _("Loading list of all newsgroups on server %s..."),
1417 serv->conn->account.host);
1419 time (&serv->newgroups_time);
1420 nntp_data.nserv = serv;
1421 nntp_data.group = NULL;
1423 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0)
1426 nntp_error ("nntp_get_active()", "LIST\r\n");
1431 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1433 nntp_get_desc (&nntp_data, "*", msg);
1435 for (tmp = serv->list; tmp; tmp = tmp->next)
1437 NNTP_DATA *data = (NNTP_DATA *)tmp->data;
1439 if (data && data->deleted && !data->rc)
1441 nntp_delete_cache (data);
1442 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1446 nntp_save_cache_index (serv);
1448 mutt_clear_error ();
1453 * returns -1 if error ocurred while retrieving header,
1454 * number of articles which ones exist in context on success.
1456 int nntp_check_msgid (CONTEXT *ctx, const char *msgid)
1460 /* if msgid is already in context, don't reload them */
1461 if (hash_find (ctx->id_hash, msgid))
1463 if (ctx->msgcount == ctx->hdrmax)
1464 mx_alloc_memory (ctx);
1465 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1466 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1468 mutt_message (_("Fetching %s from server..."), msgid);
1469 ret = nntp_read_header (ctx, msgid, 0);
1470 /* since nntp_read_header() may set read flag, we must reset it */
1471 ctx->hdrs[ctx->msgcount]->read = 0;
1473 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1477 mx_update_context (ctx, 1);
1488 unsigned int *child;
1491 static int check_children (char *s, void *c)
1493 #define cc ((CHILD_CONTEXT *) c)
1496 if (!s || (n = atoi (s)) == 0)
1498 for (i = 0; i < cc->ctx->msgcount; i++)
1499 if (cc->ctx->hdrs[i]->article_num == n)
1501 if (cc->num >= cc->max)
1502 safe_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1503 cc->child[cc->num++] = n;
1509 int nntp_check_children (CONTEXT *ctx, const char *msgid)
1511 NNTP_DATA *nntp_data = (NNTP_DATA *)ctx->data;
1513 int i, ret = 0, tmp = 0;
1516 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1517 !nntp_data->nserv->conn->account.host)
1519 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1521 if (!nntp_data->nserv->hasXPAT)
1523 mutt_error (_("Server %s does not support this operation!"),
1524 nntp_data->nserv->conn->account.host);
1528 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1529 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1534 cc.child = safe_malloc (sizeof (unsigned int) * 25);
1535 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0))
1540 /* dont try to read the xover cache. check_children() already
1541 * made sure that we dont have the article, so we need to visit
1542 * the server. Reading the cache at this point is also bad
1543 * because it would duplicate messages */
1544 if (option (OPTNEWSCACHE))
1547 unset_option (OPTNEWSCACHE);
1549 for (i = 0; i < cc.num; i++)
1551 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1553 if (ctx->msgcount &&
1554 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1555 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1558 set_option (OPTNEWSCACHE);