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"
26 #include "mutt_crypt.h"
31 #include "lib/debug.h"
38 static unsigned int _checked = 0;
40 static void nntp_error (const char *where, const char *msg) {
41 debug_print (1, ("unexpected response in %s: %s\n", where, msg));
44 static int nntp_auth (NNTP_SERVER * serv)
46 CONNECTION *conn = serv->conn;
48 unsigned char flags = conn->account.flags;
50 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
51 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
52 conn->account.flags = flags;
56 mutt_message _("Logging in...");
58 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
59 mutt_socket_write (conn, buf);
60 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
61 conn->account.flags = flags;
66 /* don't print the password unless we're at the ungodly debugging level */
67 if (DebugLevel < M_SOCK_LOG_FULL)
68 debug_print (M_SOCK_LOG_CMD, ("> AUTHINFO PASS *\n"));
70 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
71 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
72 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
73 conn->account.flags = flags;
77 if (str_ncmp ("281", buf, 3)) {
78 conn->account.flags = flags;
79 mutt_error _("Login failed.");
88 static int nntp_connect_error (NNTP_SERVER * serv)
90 serv->status = NNTP_NONE;
91 mutt_socket_close (serv->conn);
92 mutt_error _("Server closed connection!");
98 static int nntp_connect_and_auth (NNTP_SERVER * serv)
100 CONNECTION *conn = serv->conn;
104 serv->status = NNTP_NONE;
106 if (mutt_socket_open (conn) < 0)
109 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
110 return nntp_connect_error (serv);
112 if (!str_ncmp ("200", buf, 3))
113 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
114 else if (!str_ncmp ("201", buf, 3))
115 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
117 mutt_socket_close (conn);
118 str_skip_trailws (buf);
119 mutt_error ("%s", buf);
126 /* Tell INN to switch to mode reader if it isn't so. Ignore all
127 returned codes and messages. */
128 mutt_socket_write (conn, "MODE READER\r\n");
129 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
130 return nntp_connect_error (serv);
132 mutt_socket_write (conn, "STAT\r\n");
133 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
134 return nntp_connect_error (serv);
136 if (!(conn->account.flags & M_ACCT_USER) && str_ncmp ("480", buf, 3)) {
137 serv->status = NNTP_OK;
141 rc = nntp_auth (serv);
143 return nntp_connect_error (serv);
145 mutt_socket_close (conn);
146 serv->status = NNTP_BYE;
150 mutt_socket_close (conn);
151 mutt_error _("Login failed.");
156 serv->status = NNTP_OK;
160 static int nntp_attempt_features (NNTP_SERVER * serv)
162 char buf[LONG_STRING];
163 CONNECTION *conn = serv->conn;
165 mutt_socket_write (conn, "XOVER\r\n");
166 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
167 return nntp_connect_error (serv);
168 if (str_ncmp ("500", buf, 3))
171 mutt_socket_write (conn, "XPAT\r\n");
172 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
173 return nntp_connect_error (serv);
174 if (str_ncmp ("500", buf, 3))
177 mutt_socket_write (conn, "LISTGROUP\r\n");
178 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
179 return (nntp_connect_error (serv));
180 if (str_ncmp ("500", buf, 3))
181 serv->hasLISTGROUP = 1;
183 mutt_socket_write (conn, "XGTITLE +\r\n");
184 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
185 return nntp_connect_error (serv);
186 if (str_ncmp ("500", buf, 3))
187 serv->hasXGTITLE = 1;
189 if (!str_ncmp ("282", buf, 3)) {
191 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
192 return nntp_connect_error (serv);
193 } while (!(buf[0] == '.' && buf[1] == '\0'));
199 static int nntp_open_connection (NNTP_SERVER * serv)
201 if (serv->status == NNTP_OK)
203 if (serv->status == NNTP_BYE)
205 if (nntp_connect_and_auth (serv) < 0)
207 if (nntp_attempt_features (serv) < 0)
212 static int nntp_reconnect (NNTP_SERVER * serv)
214 char buf[SHORT_STRING];
216 mutt_socket_close (serv->conn);
219 if (nntp_connect_and_auth (serv) == 0)
222 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
223 serv->conn->account.host);
224 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
225 serv->status = NNTP_BYE;
231 /* Send data from line[LONG_STRING] and receive answer to same line */
232 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
234 char buf[LONG_STRING];
237 if (data->nserv->status == NNTP_BYE)
242 mutt_socket_write (data->nserv->conn, line);
244 else if (data->group) {
245 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
246 mutt_socket_write (data->nserv->conn, buf);
250 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
251 if (nntp_reconnect (data->nserv) < 0)
255 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
256 mutt_socket_write (data->nserv->conn, buf);
257 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
263 else if ((!str_ncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
267 strfcpy (line, buf, linelen);
272 * This function calls funct(*line, *data) for each received line,
273 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
276 * 1 - correct but not performed (may be, have to be continued),
277 * -1 - conection lost,
278 * -2 - invalid command or execution error,
279 * -3 - error in funct(*line, *data).
281 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, char *query, char *msg,
282 int (*funct) (char *, void *), void *data,
285 char buf[LONG_STRING];
293 strfcpy (buf, query, sizeof (buf));
294 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
303 inbuf = mem_malloc (sizeof (buf));
306 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
312 if (!lenbuf && buf[0] == '.') {
313 if (buf[1] == '\0') {
321 strfcpy (inbuf + lenbuf, p, sizeof (buf));
323 if (chunk >= sizeof (buf)) {
324 lenbuf += str_len (p);
328 if (msg && ReadInc && (line % ReadInc == 0)) {
330 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
332 mutt_message ("%s %d", msg, line);
335 if (ret == 0 && funct (inbuf, data) < 0)
340 mem_realloc (&inbuf, lenbuf + sizeof (buf));
349 static int nntp_read_tempfile (char *line, void *file)
351 FILE *f = (FILE *) file;
357 if (fputc ('\n', f) == EOF)
363 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
366 register char *p, *b;
367 register char *colon = NULL;
371 /* skip to next word */
373 while (*b && ((*b == ' ') || (*b == '\t')))
377 /* skip to end of word */
378 while (*p && (*p != ' ') && (*p != '\t')) {
390 nntp_get_status (ctx, h, p, atoi (colon));
391 if (h && h->article_num == 0 && str_cmp (group, b) == 0)
392 h->article_num = atoi (colon);
400 * 1 if article not found
401 * -1 if read or write error on tempfile or socket
403 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
406 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
408 char buf[LONG_STRING];
409 char tempfile[_POSIX_PATH_MAX];
411 HEADER *h = ctx->hdrs[ctx->msgcount];
413 mutt_mktemp (tempfile);
414 if (!(f = safe_fopen (tempfile, "w+")))
418 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
420 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
422 ret = mutt_nntp_fetch (nntp_data, buf, NULL, nntp_read_tempfile, f, 0);
425 debug_print (1, ("%s\n", buf));
428 return (ret == -1 ? -1 : 1);
431 h->article_num = article_num;
432 h->env = mutt_read_rfc822_header (f, h, 0, 0);
436 if (h->env->xref != NULL)
437 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
438 else if (h->article_num == 0 && msgid) {
439 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
440 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
441 h->article_num = atoi (buf + 4);
447 static int parse_description (char *line, void *n)
449 #define news ((NNTP_SERVER *) n)
450 register char *d = line;
455 while (*d && *d != '\t' && *d != ' ')
459 while (*d && (*d == '\t' || *d == ' '))
461 debug_print (2, ("group: %s, desc: %s\n", line, d));
462 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
463 str_cmp (d, data->desc)) {
464 mem_free (&data->desc);
465 data->desc = str_dup (d);
471 static void nntp_get_desc (NNTP_DATA * data, char *mask, char *msg)
475 if (!option (OPTLOADDESC) || !data || !data->nserv)
478 /* Get newsgroup description, if we can */
479 if (data->nserv->hasXGTITLE)
480 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
482 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
483 if (mutt_nntp_fetch (data, buf, msg, parse_description, data->nserv, 0) !=
486 nntp_error ("nntp_get_desc()", buf);
492 * XOVER returns a tab separated list of:
493 * id|subject|from|date|Msgid|references|bytes|lines|xref
495 * This has to duplicate some of the functionality of
496 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
497 * a limited number of headers which are "parsed" by placement in the list)
499 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
501 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
505 hdr->env = mutt_new_envelope ();
506 hdr->env->newsgroups = str_dup (nntp_data->group);
507 hdr->content = mutt_new_body ();
508 hdr->content->type = TYPETEXT;
509 hdr->content->subtype = str_dup ("plain");
510 hdr->content->encoding = ENC7BIT;
511 hdr->content->disposition = DISPINLINE;
512 hdr->content->length = -1;
515 for (x = 0; !done && x < 9; x++) {
516 /* if from file, need to skip newline character */
517 while (*p && *p != '\n' && *p != '\t')
526 hdr->article_num = atoi (b);
527 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
530 hdr->env->subject = str_dup (b);
531 /* Now we need to do the things which would normally be done in
532 * mutt_read_rfc822_header() */
533 if (hdr->env->subject) {
534 regmatch_t pmatch[1];
536 rfc2047_decode (&hdr->env->subject);
538 if (regexec (ReplyRegexp.rx, hdr->env->subject, 1, pmatch, 0) == 0)
539 hdr->env->real_subj = hdr->env->subject + pmatch[0].rm_eo;
541 hdr->env->real_subj = hdr->env->subject;
545 rfc822_free_address (&hdr->env->from);
546 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
547 rfc2047_decode_adrlist (hdr->env->from);
550 hdr->date_sent = mutt_parse_date (b, hdr);
551 hdr->received = hdr->date_sent;
554 mem_free (&hdr->env->message_id);
555 hdr->env->message_id = str_dup (b);
558 mutt_free_list (&hdr->env->references);
559 hdr->env->references = mutt_parse_references (b, 0);
562 hdr->content->length = atoi (b);
565 hdr->lines = atoi (b);
569 mem_free (&hdr->env->xref);
570 b = b + 6; /* skips the "Xref: " */
571 hdr->env->xref = str_dup (b);
572 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
586 unsigned short *messages;
590 #define fc ((FETCH_CONTEXT *) c)
591 static int _nntp_fetch_numbers (unsigned int num, void *c)
593 if (num < fc->base || num > fc->last)
595 fc->messages[num - fc->base] = 1;
598 static int nntp_fetch_numbers (char *line, void *c)
602 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
605 static int add_xover_line (char *line, void *c)
607 unsigned int num, total;
608 CONTEXT *ctx = fc->ctx;
609 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
614 if (ctx->msgcount >= ctx->hdrmax)
615 mx_alloc_memory (ctx);
616 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
617 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
619 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
620 num = ctx->hdrs[ctx->msgcount]->article_num;
622 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
624 if (num > data->lastLoaded)
625 data->lastLoaded = num;
626 num = num - fc->first + 1;
627 total = fc->last - fc->first + 1;
628 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
629 mutt_message ("%s %d/%d", fc->msg, num, total);
632 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
639 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
642 char buf[HUGE_STRING];
643 char *msg = _("Fetching message headers...");
644 char *msg2 = _("Fetching headers from cache...");
645 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
649 unsigned int current;
653 /* if empty group or nothing to do */
654 if (!last || first > last)
657 /* fetch list of articles */
658 mutt_message _("Fetching list of articles...");
663 fc.messages = mem_calloc (last - first + 1, sizeof (unsigned short));
664 if (nntp_data->nserv->hasLISTGROUP) {
665 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
666 if (mutt_nntp_fetch (nntp_data, buf, NULL, nntp_fetch_numbers, &fc, 0) !=
668 mutt_error (_("LISTGROUP command failed: %s"), buf);
671 nntp_error ("nntp_fetch_headers()", buf);
673 mem_free (&fc.messages);
678 for (num = 0; num < last - first + 1; num++)
679 fc.messages[num] = 1;
682 /* CACHE: must be loaded xover cache here */
683 num = nntp_data->lastCached - first + 1;
684 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
685 nntp_cache_expand (buf, nntp_data->cache);
688 if ((f = safe_fopen (buf, "r"))) {
691 /* counting number of lines */
692 while (fgets (buf, sizeof (buf), f) != NULL)
695 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
697 oldmsgcount = ctx->msgcount;
699 fc.last = first + num - 1;
701 while (fgets (buf, sizeof (buf), f) != NULL) {
702 if (ReadInc && ((++c) % ReadInc == 0))
703 mutt_message ("%s %d/%d", msg2, c, r);
704 add_xover_line (buf, &fc);
707 nntp_data->lastLoaded = fc.last;
709 if (ctx->msgcount > oldmsgcount)
710 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
713 nntp_delete_cache (nntp_data);
715 num = last - first + 1;
717 mem_free (&fc.messages);
722 * Without XOVER, we have to fetch each article header and parse
723 * it. With XOVER, we ask for all of them
726 if (nntp_data->nserv->hasXOVER) {
727 oldmsgcount = ctx->msgcount;
731 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
732 ret = mutt_nntp_fetch (nntp_data, buf, NULL, add_xover_line, &fc, 0);
733 if (ctx->msgcount > oldmsgcount)
734 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
736 mutt_error (_("XOVER command failed: %s"), buf);
738 nntp_error ("nntp_fetch_headers()", buf);
740 mem_free (&fc.messages);
746 for (current = first; current <= last; current++) {
749 ret = current - first + 1;
750 mutt_message ("%s %d/%d", msg, ret, num);
752 if (!fc.messages[current - fc.base])
755 if (ctx->msgcount >= ctx->hdrmax)
756 mx_alloc_memory (ctx);
757 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
758 h->index = ctx->msgcount;
760 ret = nntp_read_header (ctx, NULL, current);
761 if (ret == 0) { /* Got article. Fetch next header */
762 nntp_get_status (ctx, h, NULL, h->article_num);
764 mx_update_context (ctx, 1);
767 mutt_free_header (&h); /* skip it */
769 mem_free (&fc.messages);
773 if (current > nntp_data->lastLoaded)
774 nntp_data->lastLoaded = current;
776 mem_free (&fc.messages);
777 nntp_data->lastLoaded = last;
783 * currently, nntp "mailbox" is "newsgroup"
785 int nntp_open_mailbox (CONTEXT * ctx)
787 NNTP_DATA *nntp_data;
789 char buf[HUGE_STRING];
790 char server[LONG_STRING];
795 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
796 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
802 nntp_expand_path (server, sizeof (server), &acct);
803 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
806 CurrentNewsSrv = serv;
808 /* create NNTP-specific state struct if nof found in list */
809 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
810 nntp_data = mem_calloc (1, sizeof (NNTP_DATA) + str_len (buf) + 1);
811 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
812 strcpy (nntp_data->group, buf);
813 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
814 nntp_add_to_list (serv, nntp_data);
816 ctx->data = nntp_data;
817 nntp_data->nserv = serv;
819 mutt_message (_("Selecting %s..."), nntp_data->group);
821 if (!nntp_data->desc) {
822 nntp_get_desc (nntp_data, nntp_data->group, NULL);
824 nntp_save_cache_index (serv);
828 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
830 nntp_error ("nntp_open_mailbox()", buf);
835 if (str_ncmp ("211", buf, 3)) {
836 LIST *l = serv->list;
838 /* GROUP command failed */
839 if (!str_ncmp ("411", buf, 3)) {
840 mutt_error (_("Newsgroup %s not found on server %s"),
841 nntp_data->group, serv->conn->account.host);
843 /* CACHE: delete cache and line from .index */
844 nntp_delete_cache (nntp_data);
845 hash_delete (serv->newsgroups, nntp_data->group, NULL,
847 while (l && l->data != (void *) nntp_data)
858 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
859 &nntp_data->lastMessage, buf);
861 nntp_data->deleted = 0;
863 time (&serv->check_time);
866 * Check for max adding context. If it is greater than $nntp_context,
867 * strip off extra articles
869 first = nntp_data->firstMessage;
870 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
871 first = nntp_data->lastMessage - NntpContext + 1;
873 nntp_data->lastLoaded = first - 1;
874 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
877 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
879 char buf[LONG_STRING];
880 char path[_POSIX_PATH_MAX];
882 char *m = _("Fetching message...");
885 /* see if we already have the message in our cache */
887 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
890 /* if everything is fine, assign msg->fp and return */
891 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
892 (msg->fp = fopen (cache->path, "r")))
895 /* clear the previous entry */
896 unlink (cache->path);
901 cache->index = ctx->hdrs[msgno]->index;
903 cache->path = str_dup (path);
904 if (!(msg->fp = safe_fopen (path, "w+"))) {
905 mem_free (&cache->path);
909 if (ctx->hdrs[msgno]->article_num == 0)
910 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
911 ctx->hdrs[msgno]->env->message_id);
913 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
914 ctx->hdrs[msgno]->article_num);
916 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, m, nntp_read_tempfile,
917 msg->fp, ctx->tagged);
919 mutt_error (_("Article %d not found on server"),
920 ctx->hdrs[msgno]->article_num);
921 debug_print (1, ("%s\n", buf));
927 mem_free (&cache->path);
931 mutt_free_envelope (&ctx->hdrs[msgno]->env);
932 ctx->hdrs[msgno]->env =
933 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
934 /* fix content length */
935 fseek (msg->fp, 0, SEEK_END);
936 ctx->hdrs[msgno]->content->length = ftell (msg->fp) -
937 ctx->hdrs[msgno]->content->offset;
939 /* this is called in mutt before the open which fetches the message,
940 * which is probably wrong, but we just call it again here to handle
941 * the problem instead of fixing it.
943 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
945 /* These would normally be updated in mx_update_context(), but the
946 * full headers aren't parsed with XOVER, so the information wasn't
949 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
958 int nntp_post (const char *msg)
960 char buf[LONG_STRING];
963 NNTP_DATA *nntp_data;
965 if (Context && Context->magic == M_NNTP)
966 nntp_data = (NNTP_DATA *) Context->data;
968 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
969 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
970 mutt_error (_("Can't post article. No connection to news server."));
973 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
976 if (!(f = safe_fopen (msg, "r"))) {
977 mutt_error (_("Can't post article. Unable to open %s"), msg);
981 strfcpy (buf, "POST\r\n", sizeof (buf));
982 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
983 mutt_error (_("Can't post article. Connection to %s lost."),
984 nntp_data->nserv->conn->account.host);
988 mutt_error (_("Can't post article: %s"), buf);
994 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
996 if (buf[len - 1] == '\n') {
1003 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1005 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1009 if (buf[str_len (buf) - 1] != '\n')
1010 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1011 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1012 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1013 mutt_error (_("Can't post article. Connection to %s lost."),
1014 nntp_data->nserv->conn->account.host);
1017 if (buf[0] != '2') {
1018 mutt_error (_("Can't post article: %s"), buf);
1025 /* nntp_logout_all: close all open connections. */
1026 void nntp_logout_all (void)
1028 char buf[LONG_STRING];
1031 conn = mutt_socket_head ();
1034 CONNECTION* next = conn->next;
1035 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1036 mutt_message (_("Closing connection to %s..."), conn->account.host);
1037 mutt_socket_write (conn, "QUIT\r\n");
1038 mutt_socket_readln (buf, sizeof (buf), conn);
1039 mutt_clear_error ();
1040 mutt_socket_close (conn);
1041 mutt_socket_free (conn);
1047 static void nntp_free_acache (NNTP_DATA * data)
1051 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1052 if (data->acache[i].path) {
1053 unlink (data->acache[i].path);
1054 mem_free (&data->acache[i].path);
1059 void nntp_delete_data (void *p)
1061 NNTP_DATA *data = (NNTP_DATA *) p;
1065 mem_free (&data->entries);
1066 mem_free (&data->desc);
1067 mem_free (&data->cache);
1068 nntp_free_acache (data);
1072 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1074 NNTP_DATA *data = ctx->data;
1076 /* CACHE: update cache and .index files */
1077 if ((option (OPTSAVEUNSUB) || data->subscribed))
1078 nntp_save_cache_group (ctx);
1079 nntp_free_acache (data);
1081 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1085 void nntp_fastclose_mailbox (CONTEXT * ctx)
1087 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1091 nntp_free_acache (data);
1092 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1094 nntp_save_cache_index (data->nserv);
1095 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1097 nntp_delete_data (data);
1100 /* commit changes and terminate connection */
1101 int nntp_close_mailbox (CONTEXT * ctx)
1105 mutt_message _("Quitting newsgroup...");
1108 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1111 if (data->nserv && data->nserv->conn && ctx->unread) {
1112 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1114 mutt_newsgroup_catchup (data->nserv, data->group);
1119 nntp_sync_mailbox (ctx, 0, NULL);
1120 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1123 news = ((NNTP_DATA *) ctx->data)->nserv;
1124 newsrc_gen_entries (ctx);
1125 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1126 mutt_newsrc_update (news);
1128 mutt_clear_error ();
1132 /* use the GROUP command to poll for new mail */
1133 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1135 char buf[LONG_STRING];
1138 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1142 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1144 nntp_error ("nntp_check_mailbox()", buf);
1148 if (str_ncmp ("211", buf, 3)) {
1150 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1152 nntp_error ("nntp_check_mailbox()", buf);
1157 if (!str_ncmp ("211", buf, 3)) {
1161 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1162 nntp_data->firstMessage = first;
1163 nntp_data->lastMessage = last;
1164 if (ctx && last > nntp_data->lastLoaded) {
1165 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1166 time (&nntp_data->nserv->check_time);
1169 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1170 nntp_data->unread = count;
1172 mutt_newsgroup_stat (nntp_data);
1173 /* active was renumbered? */
1174 if (last < nntp_data->lastLoaded) {
1175 if (!nntp_data->max) {
1176 nntp_data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY));
1179 nntp_data->lastCached = 0;
1181 nntp_data->entries[0].first = 1;
1182 nntp_data->entries[0].last = 0;
1186 time (&nntp_data->nserv->check_time);
1190 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1192 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1195 static int add_group (char *buf, void *serv)
1197 #define s ((NNTP_SERVER *) serv)
1198 char group[LONG_STRING], mod, desc[HUGE_STRING];
1200 NNTP_DATA *nntp_data;
1203 _checked = n; /* _checked have N, where N = number of groups */
1204 if (!buf) /* at EOF must be zerouth */
1211 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1214 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1216 nntp_data = mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1);
1217 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1218 strcpy (nntp_data->group, group);
1219 nntp_data->nserv = s;
1220 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1221 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1222 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1223 nntp_add_to_list (s, nntp_data);
1225 nntp_data->deleted = 0;
1226 nntp_data->firstMessage = first;
1227 nntp_data->lastMessage = last;
1229 nntp_data->allowed = 1;
1231 nntp_data->allowed = 0;
1232 if (nntp_data->desc)
1233 mem_free (&nntp_data->desc);
1235 nntp_data->desc = str_dup (desc);
1236 if (nntp_data->rc || nntp_data->lastCached)
1237 mutt_newsgroup_stat (nntp_data);
1238 else if (nntp_data->lastMessage &&
1239 nntp_data->firstMessage <= nntp_data->lastMessage)
1240 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1242 nntp_data->unread = 0;
1248 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1250 char buf[LONG_STRING];
1251 NNTP_DATA nntp_data;
1257 if (!serv || !serv->newgroups_time)
1260 if (nntp_open_connection (serv) < 0)
1263 /* check subscribed groups for new news */
1264 if (option (OPTSHOWNEWNEWS)) {
1265 mutt_message _("Checking for new messages...");
1267 for (l = serv->list; l; l = l->next) {
1268 serv->check_time = 0; /* really check! */
1269 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1270 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1276 mutt_message _("Checking for new newsgroups...");
1278 now = serv->newgroups_time;
1279 time (&serv->newgroups_time);
1281 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1282 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1283 t->tm_min, t->tm_sec);
1284 nntp_data.nserv = serv;
1285 if (Context && Context->magic == M_NNTP)
1286 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1288 nntp_data.group = NULL;
1290 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."),
1291 add_group, serv, 0) != 0) {
1293 nntp_error ("nntp_check_newgroups()", buf);
1298 mutt_message _("Loading descriptions...");
1303 emp.next = serv->list;
1307 ((NNTP_DATA *) l->data)->new = 1;
1308 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1312 nntp_save_cache_index (serv);
1313 mutt_clear_error ();
1317 /* Load list of all newsgroups from cache ALL */
1318 int nntp_get_cache_all (NNTP_SERVER * serv)
1320 char buf[HUGE_STRING];
1323 nntp_cache_expand (buf, serv->cache);
1324 if ((f = safe_fopen (buf, "r"))) {
1327 while (fgets (buf, sizeof (buf), f) != NULL) {
1328 if (ReadInc && (i % ReadInc == 0))
1329 mutt_message (_("Loading list from cache... %d"), i);
1330 add_group (buf, serv);
1333 add_group (NULL, NULL);
1335 mutt_clear_error ();
1339 mem_free (&serv->cache);
1344 /* Load list of all newsgroups from active */
1345 int nntp_get_active (NNTP_SERVER * serv)
1347 char msg[SHORT_STRING];
1348 NNTP_DATA nntp_data;
1351 if (nntp_open_connection (serv) < 0)
1354 snprintf (msg, sizeof (msg),
1355 _("Loading list of all newsgroups on server %s..."),
1356 serv->conn->account.host);
1358 time (&serv->newgroups_time);
1359 nntp_data.nserv = serv;
1360 nntp_data.group = NULL;
1362 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, add_group, serv, 0) < 0) {
1364 nntp_error ("nntp_get_active()", "LIST\r\n");
1369 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1371 nntp_get_desc (&nntp_data, "*", msg);
1373 for (tmp = serv->list; tmp; tmp = tmp->next) {
1374 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1376 if (data && data->deleted && !data->rc) {
1377 nntp_delete_cache (data);
1378 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1382 nntp_save_cache_index (serv);
1384 mutt_clear_error ();
1389 * returns -1 if error ocurred while retrieving header,
1390 * number of articles which ones exist in context on success.
1392 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1396 /* if msgid is already in context, don't reload them */
1397 if (hash_find (ctx->id_hash, msgid))
1399 if (ctx->msgcount == ctx->hdrmax)
1400 mx_alloc_memory (ctx);
1401 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1402 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1404 mutt_message (_("Fetching %s from server..."), msgid);
1405 ret = nntp_read_header (ctx, msgid, 0);
1406 /* since nntp_read_header() may set read flag, we must reset it */
1407 ctx->hdrs[ctx->msgcount]->read = 0;
1409 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1412 mx_update_context (ctx, 1);
1422 unsigned int *child;
1425 static int check_children (char *s, void *c)
1427 #define cc ((CHILD_CONTEXT *) c)
1430 if (!s || (n = atoi (s)) == 0)
1432 for (i = 0; i < cc->ctx->msgcount; i++)
1433 if (cc->ctx->hdrs[i]->article_num == n)
1435 if (cc->num >= cc->max)
1436 mem_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1437 cc->child[cc->num++] = n;
1443 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1445 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1447 int i, ret = 0, tmp = 0;
1450 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1451 !nntp_data->nserv->conn->account.host)
1453 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1455 if (!nntp_data->nserv->hasXPAT) {
1456 mutt_error (_("Server %s does not support this operation!"),
1457 nntp_data->nserv->conn->account.host);
1461 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1462 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1467 cc.child = mem_malloc (sizeof (unsigned int) * 25);
1468 if (mutt_nntp_fetch (nntp_data, buf, NULL, check_children, &cc, 0)) {
1469 mem_free (&cc.child);
1472 /* dont try to read the xover cache. check_children() already
1473 * made sure that we dont have the article, so we need to visit
1474 * the server. Reading the cache at this point is also bad
1475 * because it would duplicate messages */
1476 if (option (OPTNEWSCACHE)) {
1478 unset_option (OPTNEWSCACHE);
1480 for (i = 0; i < cc.num; i++) {
1481 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1483 if (ctx->msgcount &&
1484 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1485 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1488 set_option (OPTNEWSCACHE);
1489 mem_free (&cc.child);