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"
28 #include "mutt_crypt.h"
33 #include "lib/debug.h"
40 static unsigned int _checked = 0;
42 void nntp_sync_sidebar (NNTP_DATA* data) {
47 if (list_empty (Incoming))
50 /* unfortunately, NNTP_DATA::group only is the plain
51 * group name, so for every single update, we need to
52 * compose the full string which must be defined via
53 * mailboxes command ;-((( FIXME
56 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
57 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
58 NONULL (data->nserv->conn->account.user),
59 *data->nserv->conn->account.pass ? ":" : "",
60 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
61 data->nserv->conn->account.host,
63 debug_print (4, ("group == '%s'\n", buf));
65 /* bail out if group not found via mailboxes */
66 if ((i = buffy_lookup (buf)) < 0)
69 tmp = (BUFFY*) Incoming->data[i];
70 /* copied from browser.c */
71 if (option (OPTMARKOLD) &&
72 data->lastCached >= data->firstMessage &&
73 data->lastCached <= data->lastMessage)
74 tmp->msg_unread = data->lastMessage - data->lastCached;
76 tmp->msg_unread = data->unread;
77 tmp->new = data->unread > 0;
78 /* this is closest to a "total" count we can get */
79 tmp->msgcount = data->lastMessage - data->firstMessage;
82 static void nntp_error (const char *where, const char *msg) {
83 debug_print (1, ("unexpected response in %s: %s\n", where, msg));
86 static int nntp_auth (NNTP_SERVER * serv)
88 CONNECTION *conn = serv->conn;
90 unsigned char flags = conn->account.flags;
92 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
93 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
94 conn->account.flags = flags;
98 mutt_message _("Logging in...");
100 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
101 mutt_socket_write (conn, buf);
102 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
103 conn->account.flags = flags;
108 /* don't print the password unless we're at the ungodly debugging level */
109 if (DebugLevel < M_SOCK_LOG_FULL)
110 debug_print (M_SOCK_LOG_CMD, ("> AUTHINFO PASS *\n"));
112 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
113 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
114 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
115 conn->account.flags = flags;
119 if (str_ncmp ("281", buf, 3)) {
120 conn->account.flags = flags;
121 mutt_error _("Login failed.");
130 static int nntp_connect_error (NNTP_SERVER * serv)
132 serv->status = NNTP_NONE;
133 mutt_socket_close (serv->conn);
134 mutt_error _("Server closed connection!");
140 static int nntp_connect_and_auth (NNTP_SERVER * serv)
142 CONNECTION *conn = serv->conn;
146 serv->status = NNTP_NONE;
148 if (mutt_socket_open (conn) < 0)
151 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
152 return nntp_connect_error (serv);
154 if (!str_ncmp ("200", buf, 3))
155 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
156 else if (!str_ncmp ("201", buf, 3))
157 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
159 mutt_socket_close (conn);
160 str_skip_trailws (buf);
161 mutt_error ("%s", buf);
168 /* Tell INN to switch to mode reader if it isn't so. Ignore all
169 returned codes and messages. */
170 mutt_socket_write (conn, "MODE READER\r\n");
171 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
172 return nntp_connect_error (serv);
174 mutt_socket_write (conn, "STAT\r\n");
175 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
176 return nntp_connect_error (serv);
178 if (!(conn->account.flags & M_ACCT_USER) && str_ncmp ("480", buf, 3)) {
179 serv->status = NNTP_OK;
183 rc = nntp_auth (serv);
185 return nntp_connect_error (serv);
187 mutt_socket_close (conn);
188 serv->status = NNTP_BYE;
192 mutt_socket_close (conn);
193 mutt_error _("Login failed.");
198 serv->status = NNTP_OK;
202 static int nntp_attempt_features (NNTP_SERVER * serv)
204 char buf[LONG_STRING];
205 CONNECTION *conn = serv->conn;
207 mutt_socket_write (conn, "XOVER\r\n");
208 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
209 return nntp_connect_error (serv);
210 if (str_ncmp ("500", buf, 3))
213 mutt_socket_write (conn, "XPAT\r\n");
214 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
215 return nntp_connect_error (serv);
216 if (str_ncmp ("500", buf, 3))
219 mutt_socket_write (conn, "LISTGROUP\r\n");
220 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
221 return (nntp_connect_error (serv));
222 if (str_ncmp ("500", buf, 3))
223 serv->hasLISTGROUP = 1;
225 mutt_socket_write (conn, "XGTITLE +\r\n");
226 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
227 return nntp_connect_error (serv);
228 if (str_ncmp ("500", buf, 3))
229 serv->hasXGTITLE = 1;
231 if (!str_ncmp ("282", buf, 3)) {
233 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
234 return nntp_connect_error (serv);
235 } while (!(buf[0] == '.' && buf[1] == '\0'));
241 static int nntp_open_connection (NNTP_SERVER * serv)
243 if (serv->status == NNTP_OK)
245 if (serv->status == NNTP_BYE)
247 if (nntp_connect_and_auth (serv) < 0)
249 if (nntp_attempt_features (serv) < 0)
254 static int nntp_reconnect (NNTP_SERVER * serv)
256 char buf[SHORT_STRING];
258 mutt_socket_close (serv->conn);
261 if (nntp_connect_and_auth (serv) == 0)
264 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
265 serv->conn->account.host);
266 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
267 serv->status = NNTP_BYE;
273 /* Send data from line[LONG_STRING] and receive answer to same line */
274 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
276 char buf[LONG_STRING];
279 if (data->nserv->status == NNTP_BYE)
284 mutt_socket_write (data->nserv->conn, line);
286 else if (data->group) {
287 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
288 mutt_socket_write (data->nserv->conn, buf);
292 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
293 if (nntp_reconnect (data->nserv) < 0)
297 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
298 mutt_socket_write (data->nserv->conn, buf);
299 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
305 else if ((!str_ncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
309 strfcpy (line, buf, linelen);
314 * This function calls funct(*line, *data) for each received line,
315 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
318 * 1 - correct but not performed (may be, have to be continued),
319 * -1 - conection lost,
320 * -2 - invalid command or execution error,
321 * -3 - error in funct(*line, *data).
323 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, char *query, char *msg,
324 int (*funct) (char *, void *), void *data,
327 char buf[LONG_STRING];
335 strfcpy (buf, query, sizeof (buf));
336 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
345 inbuf = mem_malloc (sizeof (buf));
348 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
354 if (!lenbuf && buf[0] == '.') {
355 if (buf[1] == '\0') {
363 strfcpy (inbuf + lenbuf, p, sizeof (buf));
365 if (chunk >= sizeof (buf)) {
366 lenbuf += str_len (p);
370 if (msg && ReadInc && (line % ReadInc == 0)) {
372 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
374 mutt_message ("%s %d", msg, line);
377 if (ret == 0 && funct (inbuf, data) < 0)
382 mem_realloc (&inbuf, lenbuf + sizeof (buf));
391 static int nntp_read_tempfile (char *line, void *file)
393 FILE *f = (FILE *) file;
399 if (fputc ('\n', f) == EOF)
405 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
408 register char *p, *b;
409 register char *colon = NULL;
413 /* skip to next word */
415 while (*b && ((*b == ' ') || (*b == '\t')))
419 /* skip to end of word */
420 while (*p && (*p != ' ') && (*p != '\t')) {
432 nntp_get_status (ctx, h, b, atoi (colon));
433 if (h && h->article_num == 0 && str_cmp (group, b) == 0)
434 h->article_num = atoi (colon);
442 * 1 if article not found
443 * -1 if read or write error on tempfile or socket
445 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
448 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
450 char buf[LONG_STRING];
451 char tempfile[_POSIX_PATH_MAX];
453 HEADER *h = ctx->hdrs[ctx->msgcount];
455 mutt_mktemp (tempfile);
456 if (!(f = safe_fopen (tempfile, "w+")))
460 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
462 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
464 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
467 debug_print (1, ("%s\n", buf));
470 return (ret == -1 ? -1 : 1);
473 h->article_num = article_num;
474 h->env = mutt_read_rfc822_header (f, h, 0, 0);
478 if (h->env->xref != NULL)
479 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
480 else if (h->article_num == 0 && msgid) {
481 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
482 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
483 h->article_num = atoi (buf + 4);
489 static int parse_description (char *line, void *n)
491 #define news ((NNTP_SERVER *) n)
492 register char *d = line;
497 while (*d && *d != '\t' && *d != ' ')
501 while (*d && (*d == '\t' || *d == ' '))
503 debug_print (2, ("group: %s, desc: %s\n", line, d));
504 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
505 str_cmp (d, data->desc)) {
506 mem_free (&data->desc);
507 data->desc = str_dup (d);
513 static void nntp_get_desc (NNTP_DATA * data, char *mask, char *msg)
517 if (!option (OPTLOADDESC) || !data || !data->nserv)
520 /* Get newsgroup description, if we can */
521 if (data->nserv->hasXGTITLE)
522 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
524 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
525 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) !=
528 nntp_error ("nntp_get_desc()", buf);
534 * XOVER returns a tab separated list of:
535 * id|subject|from|date|Msgid|references|bytes|lines|xref
537 * This has to duplicate some of the functionality of
538 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
539 * a limited number of headers which are "parsed" by placement in the list)
541 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
543 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
547 hdr->env = mutt_new_envelope ();
548 hdr->env->newsgroups = str_dup (nntp_data->group);
549 hdr->content = mutt_new_body ();
550 hdr->content->type = TYPETEXT;
551 hdr->content->subtype = str_dup ("plain");
552 hdr->content->encoding = ENC7BIT;
553 hdr->content->disposition = DISPINLINE;
554 hdr->content->length = -1;
557 for (x = 0; !done && x < 9; x++) {
558 /* if from file, need to skip newline character */
559 while (*p && *p != '\n' && *p != '\t')
568 hdr->article_num = atoi (b);
569 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
572 hdr->env->subject = str_dup (b);
573 /* Now we need to do the things which would normally be done in
574 * mutt_read_rfc822_header() */
575 if (hdr->env->subject) {
576 regmatch_t pmatch[1];
578 rfc2047_decode (&hdr->env->subject);
580 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
581 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
583 hdr->env->real_subj = hdr->env->subject;
587 rfc822_free_address (&hdr->env->from);
588 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
589 rfc2047_decode_adrlist (hdr->env->from);
592 hdr->date_sent = mutt_parse_date (b, hdr);
593 hdr->received = hdr->date_sent;
596 mem_free (&hdr->env->message_id);
597 hdr->env->message_id = str_dup (b);
600 mutt_free_list (&hdr->env->references);
601 hdr->env->references = mutt_parse_references (b, 0);
604 hdr->content->length = atoi (b);
607 hdr->lines = atoi (b);
611 mem_free (&hdr->env->xref);
612 b = b + 6; /* skips the "Xref: " */
613 hdr->env->xref = str_dup (b);
614 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
628 unsigned short *messages;
632 #define fc ((FETCH_CONTEXT *) c)
633 static int _nntp_fetch_numbers (unsigned int num, void *c)
635 if (num < fc->base || num > fc->last)
637 fc->messages[num - fc->base] = 1;
640 static int nntp_fetch_numbers (char *line, void *c)
644 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
647 static int add_xover_line (char *line, void *c)
649 unsigned int num, total;
650 CONTEXT *ctx = fc->ctx;
651 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
656 if (ctx->msgcount >= ctx->hdrmax)
657 mx_alloc_memory (ctx);
658 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
659 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
661 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
662 num = ctx->hdrs[ctx->msgcount]->article_num;
664 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
666 if (num > data->lastLoaded)
667 data->lastLoaded = num;
668 num = num - fc->first + 1;
669 total = fc->last - fc->first + 1;
670 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
671 mutt_message ("%s %d/%d", fc->msg, num, total);
674 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
681 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
684 char buf[HUGE_STRING];
685 char *msg = _("Fetching message headers...");
686 char *msg2 = _("Fetching headers from cache...");
687 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
691 unsigned int current;
695 /* if empty group or nothing to do */
696 if (!last || first > last)
699 /* fetch list of articles */
700 mutt_message _("Fetching list of articles...");
705 fc.messages = mem_calloc (last - first + 1, sizeof (unsigned short));
706 if (nntp_data->nserv->hasLISTGROUP) {
707 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
708 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) !=
710 mutt_error (_("LISTGROUP command failed: %s"), buf);
713 nntp_error ("nntp_fetch_headers()", buf);
715 mem_free (&fc.messages);
720 for (num = 0; num < last - first + 1; num++)
721 fc.messages[num] = 1;
724 /* CACHE: must be loaded xover cache here */
725 num = nntp_data->lastCached - first + 1;
726 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
727 nntp_cache_expand (buf, nntp_data->cache);
730 if ((f = safe_fopen (buf, "r"))) {
733 /* counting number of lines */
734 while (fgets (buf, sizeof (buf), f) != NULL)
737 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
739 oldmsgcount = ctx->msgcount;
741 fc.last = first + num - 1;
743 while (fgets (buf, sizeof (buf), f) != NULL) {
744 if (ReadInc && ((++c) % ReadInc == 0))
745 mutt_message ("%s %d/%d", msg2, c, r);
746 add_xover_line (buf, &fc);
749 nntp_data->lastLoaded = fc.last;
751 if (ctx->msgcount > oldmsgcount)
752 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
755 nntp_delete_cache (nntp_data);
757 num = last - first + 1;
759 mem_free (&fc.messages);
764 * Without XOVER, we have to fetch each article header and parse
765 * it. With XOVER, we ask for all of them
768 if (nntp_data->nserv->hasXOVER) {
769 oldmsgcount = ctx->msgcount;
773 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
774 ret = mutt_nntp_fetch (nntp_data, buf, NULL, add_xover_line, &fc, 0);
775 if (ctx->msgcount > oldmsgcount)
776 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
778 mutt_error (_("XOVER command failed: %s"), buf);
780 nntp_error ("nntp_fetch_headers()", buf);
782 mem_free (&fc.messages);
788 for (current = first; current <= last; current++) {
791 ret = current - first + 1;
792 mutt_message ("%s %d/%d", msg, ret, num);
794 if (!fc.messages[current - fc.base])
797 if (ctx->msgcount >= ctx->hdrmax)
798 mx_alloc_memory (ctx);
799 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
800 h->index = ctx->msgcount;
802 ret = nntp_read_header (ctx, NULL, current);
803 if (ret == 0) { /* Got article. Fetch next header */
804 nntp_get_status (ctx, h, NULL, h->article_num);
806 mx_update_context (ctx, 1);
809 mutt_free_header (&h); /* skip it */
811 mem_free (&fc.messages);
815 if (current > nntp_data->lastLoaded)
816 nntp_data->lastLoaded = current;
818 mem_free (&fc.messages);
819 nntp_data->lastLoaded = last;
825 * currently, nntp "mailbox" is "newsgroup"
827 int nntp_open_mailbox (CONTEXT * ctx)
829 NNTP_DATA *nntp_data;
831 char buf[HUGE_STRING];
832 char server[LONG_STRING];
837 memset (&acct, 0, sizeof (ACCOUNT));
839 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
840 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
846 nntp_expand_path (server, sizeof (server), &acct);
847 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
850 CurrentNewsSrv = serv;
852 /* create NNTP-specific state struct if nof found in list */
853 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
854 nntp_data = mem_calloc (1, sizeof (NNTP_DATA) + str_len (buf) + 1);
855 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
856 strcpy (nntp_data->group, buf);
857 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
858 nntp_add_to_list (serv, nntp_data);
860 ctx->data = nntp_data;
861 nntp_data->nserv = serv;
863 mutt_message (_("Selecting %s..."), nntp_data->group);
865 if (!nntp_data->desc) {
866 nntp_get_desc (nntp_data, nntp_data->group, NULL);
868 nntp_save_cache_index (serv);
872 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
874 nntp_error ("nntp_open_mailbox()", buf);
879 if (str_ncmp ("211", buf, 3)) {
880 LIST *l = serv->list;
882 /* GROUP command failed */
883 if (!str_ncmp ("411", buf, 3)) {
884 mutt_error (_("Newsgroup %s not found on server %s"),
885 nntp_data->group, serv->conn->account.host);
887 /* CACHE: delete cache and line from .index */
888 nntp_delete_cache (nntp_data);
889 hash_delete (serv->newsgroups, nntp_data->group, NULL,
891 while (l && l->data != (void *) nntp_data)
902 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
903 &nntp_data->lastMessage, buf);
905 nntp_data->deleted = 0;
907 time (&serv->check_time);
910 * Check for max adding context. If it is greater than $nntp_context,
911 * strip off extra articles
913 first = nntp_data->firstMessage;
914 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
915 first = nntp_data->lastMessage - NntpContext + 1;
917 nntp_data->lastLoaded = first - 1;
918 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
921 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
923 char buf[LONG_STRING];
924 char path[_POSIX_PATH_MAX];
926 char *m = _("Fetching message...");
929 /* see if we already have the message in our cache */
931 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
934 /* if everything is fine, assign msg->fp and return */
935 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
936 (msg->fp = fopen (cache->path, "r")))
939 /* clear the previous entry */
940 unlink (cache->path);
945 cache->index = ctx->hdrs[msgno]->index;
947 cache->path = str_dup (path);
948 if (!(msg->fp = safe_fopen (path, "w+"))) {
949 mem_free (&cache->path);
953 if (ctx->hdrs[msgno]->article_num == 0)
954 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
955 ctx->hdrs[msgno]->env->message_id);
957 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
958 ctx->hdrs[msgno]->article_num);
960 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, m, nntp_read_tempfile,
961 msg->fp, ctx->tagged);
963 mutt_error (_("Article %d not found on server"),
964 ctx->hdrs[msgno]->article_num);
965 debug_print (1, ("%s\n", buf));
971 mem_free (&cache->path);
975 mutt_free_envelope (&ctx->hdrs[msgno]->env);
976 ctx->hdrs[msgno]->env =
977 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
978 /* fix content length */
979 fseek (msg->fp, 0, SEEK_END);
980 ctx->hdrs[msgno]->content->length = ftell (msg->fp) -
981 ctx->hdrs[msgno]->content->offset;
983 /* this is called in mutt before the open which fetches the message,
984 * which is probably wrong, but we just call it again here to handle
985 * the problem instead of fixing it.
987 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
989 /* These would normally be updated in mx_update_context(), but the
990 * full headers aren't parsed with XOVER, so the information wasn't
993 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1002 int nntp_post (const char *msg)
1004 char buf[LONG_STRING];
1007 NNTP_DATA *nntp_data;
1009 if (Context && Context->magic == M_NNTP)
1010 nntp_data = (NNTP_DATA *) Context->data;
1012 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1013 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1014 mutt_error (_("Can't post article. No connection to news server."));
1017 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1020 if (!(f = safe_fopen (msg, "r"))) {
1021 mutt_error (_("Can't post article. Unable to open %s"), msg);
1025 strfcpy (buf, "POST\r\n", sizeof (buf));
1026 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1027 mutt_error (_("Can't post article. Connection to %s lost."),
1028 nntp_data->nserv->conn->account.host);
1031 if (buf[0] != '3') {
1032 mutt_error (_("Can't post article: %s"), buf);
1038 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1039 len = str_len (buf);
1040 if (buf[len - 1] == '\n') {
1041 buf[len - 1] = '\r';
1047 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1049 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1053 if (buf[str_len (buf) - 1] != '\n')
1054 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1055 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1056 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1057 mutt_error (_("Can't post article. Connection to %s lost."),
1058 nntp_data->nserv->conn->account.host);
1061 if (buf[0] != '2') {
1062 mutt_error (_("Can't post article: %s"), buf);
1069 /* nntp_logout_all: close all open connections. */
1070 void nntp_logout_all (void)
1072 char buf[LONG_STRING];
1075 conn = mutt_socket_head ();
1078 CONNECTION* next = conn->next;
1079 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1080 mutt_message (_("Closing connection to %s..."), conn->account.host);
1081 mutt_socket_write (conn, "QUIT\r\n");
1082 mutt_socket_readln (buf, sizeof (buf), conn);
1083 mutt_clear_error ();
1084 mutt_socket_close (conn);
1085 mutt_socket_free (conn);
1091 static void nntp_free_acache (NNTP_DATA * data)
1095 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1096 if (data->acache[i].path) {
1097 unlink (data->acache[i].path);
1098 mem_free (&data->acache[i].path);
1103 void nntp_delete_data (void *p)
1105 NNTP_DATA *data = (NNTP_DATA *) p;
1109 mem_free (&data->entries);
1110 mem_free (&data->desc);
1111 mem_free (&data->cache);
1112 nntp_free_acache (data);
1116 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1118 NNTP_DATA *data = ctx->data;
1120 /* CACHE: update cache and .index files */
1121 if ((option (OPTSAVEUNSUB) || data->subscribed))
1122 nntp_save_cache_group (ctx);
1123 nntp_free_acache (data);
1125 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1129 void nntp_fastclose_mailbox (CONTEXT * ctx)
1131 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1135 nntp_free_acache (data);
1136 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1138 nntp_save_cache_index (data->nserv);
1139 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1141 nntp_delete_data (data);
1143 nntp_sync_sidebar (data);
1146 /* commit changes and terminate connection */
1147 int nntp_close_mailbox (CONTEXT * ctx)
1151 mutt_message _("Quitting newsgroup...");
1154 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1157 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, 0, NULL);
1166 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1169 news = ((NNTP_DATA *) ctx->data)->nserv;
1170 newsrc_gen_entries (ctx);
1171 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1172 mutt_newsrc_update (news);
1174 mutt_clear_error ();
1178 /* use the GROUP command to poll for new mail */
1179 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1181 char buf[LONG_STRING];
1184 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1188 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1190 nntp_error ("nntp_check_mailbox()", buf);
1194 if (str_ncmp ("211", buf, 3)) {
1196 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1198 nntp_error ("nntp_check_mailbox()", buf);
1203 if (!str_ncmp ("211", buf, 3)) {
1207 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1208 nntp_data->firstMessage = first;
1209 nntp_data->lastMessage = last;
1210 if (ctx && last > nntp_data->lastLoaded) {
1211 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1212 time (&nntp_data->nserv->check_time);
1215 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1216 nntp_data->unread = count;
1218 mutt_newsgroup_stat (nntp_data);
1219 /* active was renumbered? */
1220 if (last < nntp_data->lastLoaded) {
1221 if (!nntp_data->max) {
1222 nntp_data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY));
1225 nntp_data->lastCached = 0;
1227 nntp_data->entries[0].first = 1;
1228 nntp_data->entries[0].last = 0;
1230 nntp_sync_sidebar (nntp_data);
1233 time (&nntp_data->nserv->check_time);
1237 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1239 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1242 static int add_group (char *buf, void *serv)
1244 #define s ((NNTP_SERVER *) serv)
1245 char group[LONG_STRING], mod, desc[HUGE_STRING];
1247 NNTP_DATA *nntp_data;
1250 _checked = n; /* _checked have N, where N = number of groups */
1251 if (!buf) /* at EOF must be zerouth */
1258 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1261 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1263 nntp_data = mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1);
1264 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1265 strcpy (nntp_data->group, group);
1266 nntp_data->nserv = s;
1267 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1268 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1269 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1270 nntp_add_to_list (s, nntp_data);
1272 nntp_data->deleted = 0;
1273 nntp_data->firstMessage = first;
1274 nntp_data->lastMessage = last;
1276 nntp_data->allowed = 1;
1278 nntp_data->allowed = 0;
1279 if (nntp_data->desc)
1280 mem_free (&nntp_data->desc);
1282 nntp_data->desc = str_dup (desc);
1283 if (nntp_data->rc || nntp_data->lastCached)
1284 mutt_newsgroup_stat (nntp_data);
1285 else if (nntp_data->lastMessage &&
1286 nntp_data->firstMessage <= nntp_data->lastMessage)
1287 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1289 nntp_data->unread = 0;
1295 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1297 char buf[LONG_STRING];
1298 NNTP_DATA nntp_data;
1304 if (!serv || !serv->newgroups_time)
1307 if (nntp_open_connection (serv) < 0)
1310 /* check subscribed groups for new news */
1311 if (option (OPTSHOWNEWNEWS)) {
1312 mutt_message _("Checking for new messages...");
1314 for (l = serv->list; l; l = l->next) {
1315 serv->check_time = 0; /* really check! */
1316 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1317 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1319 sidebar_draw (CurrentMenu);
1324 mutt_message _("Checking for new newsgroups...");
1326 now = serv->newgroups_time;
1327 time (&serv->newgroups_time);
1329 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1330 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1331 t->tm_min, t->tm_sec);
1332 nntp_data.nserv = serv;
1333 if (Context && Context->magic == M_NNTP)
1334 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1336 nntp_data.group = NULL;
1338 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1339 add_group, serv, 0) != 0) {
1341 nntp_error ("nntp_check_newgroups()", buf);
1346 mutt_message _("Loading descriptions...");
1351 emp.next = serv->list;
1355 ((NNTP_DATA *) l->data)->new = 1;
1356 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1360 nntp_save_cache_index (serv);
1361 mutt_clear_error ();
1365 /* Load list of all newsgroups from cache ALL */
1366 int nntp_get_cache_all (NNTP_SERVER * serv)
1368 char buf[HUGE_STRING];
1371 nntp_cache_expand (buf, serv->cache);
1372 if ((f = safe_fopen (buf, "r"))) {
1375 while (fgets (buf, sizeof (buf), f) != NULL) {
1376 if (ReadInc && (i % ReadInc == 0))
1377 mutt_message (_("Loading list from cache... %d"), i);
1378 add_group (buf, serv);
1381 add_group (NULL, NULL);
1383 mutt_clear_error ();
1387 mem_free (&serv->cache);
1392 /* Load list of all newsgroups from active */
1393 int nntp_get_active (NNTP_SERVER * serv)
1395 char msg[SHORT_STRING];
1396 NNTP_DATA nntp_data;
1399 if (nntp_open_connection (serv) < 0)
1402 snprintf (msg, sizeof (msg),
1403 _("Loading list of all newsgroups on server %s..."),
1404 serv->conn->account.host);
1406 time (&serv->newgroups_time);
1407 nntp_data.nserv = serv;
1408 nntp_data.group = NULL;
1410 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0) {
1412 nntp_error ("nntp_get_active()", "LIST\r\n");
1417 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1419 nntp_get_desc (&nntp_data, "*", msg);
1421 for (tmp = serv->list; tmp; tmp = tmp->next) {
1422 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1424 if (data && data->deleted && !data->rc) {
1425 nntp_delete_cache (data);
1426 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1430 nntp_save_cache_index (serv);
1432 mutt_clear_error ();
1437 * returns -1 if error ocurred while retrieving header,
1438 * number of articles which ones exist in context on success.
1440 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1444 /* if msgid is already in context, don't reload them */
1445 if (hash_find (ctx->id_hash, msgid))
1447 if (ctx->msgcount == ctx->hdrmax)
1448 mx_alloc_memory (ctx);
1449 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1450 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1452 mutt_message (_("Fetching %s from server..."), msgid);
1453 ret = nntp_read_header (ctx, msgid, 0);
1454 /* since nntp_read_header() may set read flag, we must reset it */
1455 ctx->hdrs[ctx->msgcount]->read = 0;
1457 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1460 mx_update_context (ctx, 1);
1470 unsigned int *child;
1473 static int check_children (char *s, void *c)
1475 #define cc ((CHILD_CONTEXT *) c)
1478 if (!s || (n = atoi (s)) == 0)
1480 for (i = 0; i < cc->ctx->msgcount; i++)
1481 if (cc->ctx->hdrs[i]->article_num == n)
1483 if (cc->num >= cc->max)
1484 mem_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1485 cc->child[cc->num++] = n;
1491 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1493 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1495 int i, ret = 0, tmp = 0;
1498 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1499 !nntp_data->nserv->conn->account.host)
1501 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1503 if (!nntp_data->nserv->hasXPAT) {
1504 mutt_error (_("Server %s does not support this operation!"),
1505 nntp_data->nserv->conn->account.host);
1509 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1510 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1515 cc.child = mem_malloc (sizeof (unsigned int) * 25);
1516 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0)) {
1517 mem_free (&cc.child);
1520 /* dont try to read the xover cache. check_children() already
1521 * made sure that we dont have the article, so we need to visit
1522 * the server. Reading the cache at this point is also bad
1523 * because it would duplicate messages */
1524 if (option (OPTNEWSCACHE)) {
1526 unset_option (OPTNEWSCACHE);
1528 for (i = 0; i < cc.num; i++) {
1529 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1531 if (ctx->msgcount &&
1532 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1533 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1536 set_option (OPTNEWSCACHE);
1537 mem_free (&cc.child);