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 #define WANT_LISTGROUP_COMMAND 0
42 static unsigned int _checked = 0;
44 void nntp_sync_sidebar (NNTP_DATA* data) {
49 if (list_empty (Incoming))
52 /* unfortunately, NNTP_DATA::group only is the plain
53 * group name, so for every single update, we need to
54 * compose the full string which must be defined via
55 * mailboxes command ;-((( FIXME
58 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
59 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
60 NONULL (data->nserv->conn->account.user),
61 *data->nserv->conn->account.pass ? ":" : "",
62 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
63 data->nserv->conn->account.host,
66 /* bail out if group not found via mailboxes */
67 if ((i = buffy_lookup (buf)) < 0)
70 tmp = (BUFFY*) Incoming->data[i];
71 /* copied from browser.c */
72 if (option (OPTMARKOLD) &&
73 data->lastCached >= data->firstMessage &&
74 data->lastCached <= data->lastMessage)
75 tmp->msg_unread = data->lastMessage - data->lastCached;
77 tmp->msg_unread = data->unread;
78 tmp->new = data->unread > 0;
79 /* this is closest to a "total" count we can get */
80 tmp->msgcount = data->lastMessage - data->firstMessage;
83 static void nntp_error (const char *where, const char *msg) {
84 debug_print (1, ("unexpected response in %s: %s\n", where, msg));
87 static int nntp_auth (NNTP_SERVER * serv)
89 CONNECTION *conn = serv->conn;
91 unsigned char flags = conn->account.flags;
93 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
94 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
95 conn->account.flags = flags;
99 mutt_message _("Logging in...");
101 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
102 mutt_socket_write (conn, buf);
103 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
104 conn->account.flags = flags;
109 /* don't print the password unless we're at the ungodly debugging level */
110 if (DebugLevel < M_SOCK_LOG_FULL)
111 debug_print (M_SOCK_LOG_CMD, ("> AUTHINFO PASS *\n"));
113 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
114 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
115 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
116 conn->account.flags = flags;
120 if (str_ncmp ("281", buf, 3)) {
121 conn->account.flags = flags;
122 mutt_error _("Login failed.");
131 static int nntp_connect_error (NNTP_SERVER * serv)
133 serv->status = NNTP_NONE;
134 mutt_socket_close (serv->conn);
135 mutt_error _("Server closed connection!");
141 static int nntp_connect_and_auth (NNTP_SERVER * serv)
143 CONNECTION *conn = serv->conn;
147 serv->status = NNTP_NONE;
149 if (mutt_socket_open (conn) < 0)
152 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
153 return nntp_connect_error (serv);
155 if (!str_ncmp ("200", buf, 3))
156 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
157 else if (!str_ncmp ("201", buf, 3))
158 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
160 mutt_socket_close (conn);
161 str_skip_trailws (buf);
162 mutt_error ("%s", buf);
169 /* Tell INN to switch to mode reader if it isn't so. Ignore all
170 returned codes and messages. */
171 mutt_socket_write (conn, "MODE READER\r\n");
172 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
173 return nntp_connect_error (serv);
175 mutt_socket_write (conn, "STAT\r\n");
176 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
177 return nntp_connect_error (serv);
179 if (!(conn->account.flags & M_ACCT_USER) && str_ncmp ("480", buf, 3)) {
180 serv->status = NNTP_OK;
184 rc = nntp_auth (serv);
186 return nntp_connect_error (serv);
188 mutt_socket_close (conn);
189 serv->status = NNTP_BYE;
193 mutt_socket_close (conn);
194 mutt_error _("Login failed.");
199 serv->status = NNTP_OK;
203 static int nntp_attempt_features (NNTP_SERVER * serv)
205 char buf[LONG_STRING];
206 CONNECTION *conn = serv->conn;
208 mutt_socket_write (conn, "XOVER\r\n");
209 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
210 return nntp_connect_error (serv);
211 if (str_ncmp ("500", buf, 3))
214 mutt_socket_write (conn, "XPAT\r\n");
215 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
216 return nntp_connect_error (serv);
217 if (str_ncmp ("500", buf, 3))
220 #if WANT_LISTGROUP_COMMAND
221 mutt_socket_write (conn, "LISTGROUP\r\n");
222 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
223 return (nntp_connect_error (serv));
224 if (str_ncmp ("500", buf, 3))
225 serv->hasLISTGROUP = 1;
228 mutt_socket_write (conn, "XGTITLE +\r\n");
229 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
230 return nntp_connect_error (serv);
231 if (str_ncmp ("500", buf, 3))
232 serv->hasXGTITLE = 1;
234 if (!str_ncmp ("282", buf, 3)) {
236 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
237 return nntp_connect_error (serv);
238 } while (!(buf[0] == '.' && buf[1] == '\0'));
244 static int nntp_open_connection (NNTP_SERVER * serv)
246 if (serv->status == NNTP_OK)
248 if (serv->status == NNTP_BYE)
250 if (nntp_connect_and_auth (serv) < 0)
252 if (nntp_attempt_features (serv) < 0)
257 static int nntp_reconnect (NNTP_SERVER * serv)
259 char buf[SHORT_STRING];
261 mutt_socket_close (serv->conn);
264 if (nntp_connect_and_auth (serv) == 0)
267 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
268 serv->conn->account.host);
269 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
270 serv->status = NNTP_BYE;
276 /* Send data from line[LONG_STRING] and receive answer to same line */
277 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
279 char buf[LONG_STRING];
282 if (data->nserv->status == NNTP_BYE)
287 mutt_socket_write (data->nserv->conn, line);
289 else if (data->group) {
290 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
291 mutt_socket_write (data->nserv->conn, buf);
295 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
296 if (nntp_reconnect (data->nserv) < 0)
300 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
301 mutt_socket_write (data->nserv->conn, buf);
302 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
308 else if ((!str_ncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
312 strfcpy (line, buf, linelen);
317 * This function calls funct(*line, *data) for each received line,
318 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
321 * 1 - correct but not performed (may be, have to be continued),
322 * -1 - conection lost,
323 * -2 - invalid command or execution error,
324 * -3 - error in funct(*line, *data).
326 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, char *query, char *msg,
327 progress_t* bar, int (*funct) (char *, void *),
328 void *data, int tagged)
330 char buf[LONG_STRING];
339 strfcpy (buf, query, sizeof (buf));
340 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
349 inbuf = mem_malloc (sizeof (buf));
352 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
358 if (!lenbuf && buf[0] == '.') {
359 if (buf[1] == '\0') {
367 strfcpy (inbuf + lenbuf, p, sizeof (buf));
370 if (chunk >= sizeof (buf)) {
371 lenbuf += str_len (p);
375 mutt_progress_bar (bar, pos);
378 if (ReadInc && (line % ReadInc == 0)) {
380 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
382 mutt_message ("%s %d", msg, line);
386 if (ret == 0 && funct (inbuf, data) < 0)
391 mem_realloc (&inbuf, lenbuf + sizeof (buf));
400 static int nntp_read_tempfile (char *line, void *file)
402 FILE *f = (FILE *) file;
408 if (fputc ('\n', f) == EOF)
414 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
417 register char *p, *b;
418 register char *colon = NULL;
422 /* skip to next word */
424 while (*b && ((*b == ' ') || (*b == '\t')))
428 /* skip to end of word */
429 while (*p && (*p != ' ') && (*p != '\t')) {
441 nntp_get_status (ctx, h, b, atoi (colon));
442 if (h && h->article_num == 0 && str_cmp (group, b) == 0)
443 h->article_num = atoi (colon);
451 * 1 if article not found
452 * -1 if read or write error on tempfile or socket
454 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
457 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
459 char buf[LONG_STRING];
460 char tempfile[_POSIX_PATH_MAX];
462 HEADER *h = ctx->hdrs[ctx->msgcount];
464 mutt_mktemp (tempfile);
465 if (!(f = safe_fopen (tempfile, "w+")))
469 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
471 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
473 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
476 debug_print (1, ("%s\n", buf));
479 return (ret == -1 ? -1 : 1);
482 h->article_num = article_num;
483 h->env = mutt_read_rfc822_header (f, h, 0, 0);
487 if (h->env->xref != NULL)
488 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
489 else if (h->article_num == 0 && msgid) {
490 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
491 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
492 h->article_num = atoi (buf + 4);
498 static int parse_description (char *line, void *n)
500 #define news ((NNTP_SERVER *) n)
501 register char *d = line;
506 while (*d && *d != '\t' && *d != ' ')
510 while (*d && (*d == '\t' || *d == ' '))
512 debug_print (2, ("group: %s, desc: %s\n", line, d));
513 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
514 str_cmp (d, data->desc)) {
515 mem_free (&data->desc);
516 data->desc = str_dup (d);
522 static void nntp_get_desc (NNTP_DATA * data, char *mask, char *msg, progress_t* bar)
526 if (!option (OPTLOADDESC) || !data || !data->nserv)
529 /* Get newsgroup description, if we can */
530 if (data->nserv->hasXGTITLE)
531 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
533 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
534 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
537 nntp_error ("nntp_get_desc()", buf);
543 * XOVER returns a tab separated list of:
544 * id|subject|from|date|Msgid|references|bytes|lines|xref
546 * This has to duplicate some of the functionality of
547 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
548 * a limited number of headers which are "parsed" by placement in the list)
550 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
552 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
556 hdr->env = mutt_new_envelope ();
557 hdr->env->newsgroups = str_dup (nntp_data->group);
558 hdr->content = mutt_new_body ();
559 hdr->content->type = TYPETEXT;
560 hdr->content->subtype = str_dup ("plain");
561 hdr->content->encoding = ENC7BIT;
562 hdr->content->disposition = DISPINLINE;
563 hdr->content->length = -1;
566 for (x = 0; !done && x < 9; x++) {
567 /* if from file, need to skip newline character */
568 while (*p && *p != '\n' && *p != '\t')
577 hdr->article_num = atoi (b);
578 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
581 hdr->env->subject = str_dup (b);
584 rfc822_free_address (&hdr->env->from);
585 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
586 /* same as for mutt_parse_rfc822_line():
587 * don't leave from info NULL if there's an invalid address (or
588 * whatever) in From: field; mutt would just display it as empty
589 * and mark mail/(esp.) news article as your own. aaargh! this
590 * bothered me for _years_ */
591 if (!hdr->env->from) {
592 hdr->env->from = rfc822_new_address ();
593 hdr->env->from->personal = str_dup (b);
597 hdr->date_sent = mutt_parse_date (b, hdr);
598 hdr->received = hdr->date_sent;
601 mem_free (&hdr->env->message_id);
602 hdr->env->message_id = str_dup (b);
605 mutt_free_list (&hdr->env->references);
606 hdr->env->references = mutt_parse_references (b, 0);
609 hdr->content->length = atoi (b);
612 hdr->lines = atoi (b);
616 mem_free (&hdr->env->xref);
617 b = b + 6; /* skips the "Xref: " */
618 hdr->env->xref = str_dup (b);
619 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
621 rfc2047_decode_envelope (hdr->env);
634 unsigned short *messages;
638 #define fc ((FETCH_CONTEXT *) c)
640 #if WANT_LISTGROUP_COMMAND
641 static int _nntp_fetch_numbers (unsigned int num, void *c)
643 if (num < fc->base || num > fc->last)
645 fc->messages[num - fc->base] = 1;
648 static int nntp_fetch_numbers (char *line, void *c)
652 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
656 static int add_xover_line (char *line, void *c)
658 unsigned int num, total;
659 CONTEXT *ctx = fc->ctx;
660 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
665 if (ctx->msgcount >= ctx->hdrmax)
666 mx_alloc_memory (ctx);
667 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
668 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
670 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
671 num = ctx->hdrs[ctx->msgcount]->article_num;
673 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
675 if (num > data->lastLoaded)
676 data->lastLoaded = num;
677 num = num - fc->first + 1;
678 total = fc->last - fc->first + 1;
679 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
680 mutt_message ("%s %d/%d", fc->msg, num, total);
683 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
690 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
693 char buf[HUGE_STRING];
694 char *msg = _("Fetching message headers...");
695 char *msg2 = _("Fetching headers from cache...");
696 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
700 unsigned int current;
704 /* if empty group or nothing to do */
705 if (!last || first > last)
708 /* fetch list of articles */
709 mutt_message _("Fetching list of articles...");
714 fc.messages = mem_calloc (last - first + 1, sizeof (unsigned short));
715 #if WANT_LISTGROUP_COMMAND
716 if (nntp_data->nserv->hasLISTGROUP) {
717 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
718 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
720 mutt_error (_("LISTGROUP command failed: %s"), buf);
723 nntp_error ("nntp_fetch_headers()", buf);
725 mem_free (&fc.messages);
731 for (num = 0; num < last - first + 1; num++)
732 fc.messages[num] = 1;
733 #if WANT_LISTGROUP_COMMAND
737 /* CACHE: must be loaded xover cache here */
738 num = nntp_data->lastCached - first + 1;
739 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
740 nntp_cache_expand (buf, nntp_data->cache);
743 if ((f = safe_fopen (buf, "r"))) {
746 /* counting number of lines */
747 while (fgets (buf, sizeof (buf), f) != NULL)
750 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
752 oldmsgcount = ctx->msgcount;
754 fc.last = first + num - 1;
756 while (fgets (buf, sizeof (buf), f) != NULL) {
757 if (ReadInc && ((++c) % ReadInc == 0))
758 mutt_message ("%s %d/%d", msg2, c, r);
759 add_xover_line (buf, &fc);
762 nntp_data->lastLoaded = fc.last;
764 if (ctx->msgcount > oldmsgcount)
765 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
768 nntp_delete_cache (nntp_data);
770 num = last - first + 1;
772 mem_free (&fc.messages);
777 * Without XOVER, we have to fetch each article header and parse
778 * it. With XOVER, we ask for all of them
781 if (nntp_data->nserv->hasXOVER) {
782 oldmsgcount = ctx->msgcount;
786 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
787 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
788 if (ctx->msgcount > oldmsgcount)
789 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
791 mutt_error (_("XOVER command failed: %s"), buf);
793 nntp_error ("nntp_fetch_headers()", buf);
795 mem_free (&fc.messages);
801 for (current = first; current <= last; current++) {
804 ret = current - first + 1;
805 mutt_message ("%s %d/%d", msg, ret, num);
807 if (!fc.messages[current - fc.base])
810 if (ctx->msgcount >= ctx->hdrmax)
811 mx_alloc_memory (ctx);
812 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
813 h->index = ctx->msgcount;
815 ret = nntp_read_header (ctx, NULL, current);
816 if (ret == 0) { /* Got article. Fetch next header */
817 nntp_get_status (ctx, h, NULL, h->article_num);
819 mx_update_context (ctx, 1);
822 mutt_free_header (&h); /* skip it */
824 mem_free (&fc.messages);
828 if (current > nntp_data->lastLoaded)
829 nntp_data->lastLoaded = current;
831 mem_free (&fc.messages);
832 nntp_data->lastLoaded = last;
838 * currently, nntp "mailbox" is "newsgroup"
840 int nntp_open_mailbox (CONTEXT * ctx)
842 NNTP_DATA *nntp_data;
844 char buf[HUGE_STRING];
845 char server[LONG_STRING];
850 memset (&acct, 0, sizeof (ACCOUNT));
852 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
853 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
859 nntp_expand_path (server, sizeof (server), &acct);
860 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
863 CurrentNewsSrv = serv;
865 /* create NNTP-specific state struct if nof found in list */
866 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
867 nntp_data = mem_calloc (1, sizeof (NNTP_DATA) + str_len (buf) + 1);
868 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
869 strcpy (nntp_data->group, buf);
870 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
871 nntp_add_to_list (serv, nntp_data);
873 ctx->data = nntp_data;
874 nntp_data->nserv = serv;
876 mutt_message (_("Selecting %s..."), nntp_data->group);
878 if (!nntp_data->desc) {
879 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
881 nntp_save_cache_index (serv);
885 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
887 nntp_error ("nntp_open_mailbox()", buf);
892 if (str_ncmp ("211", buf, 3)) {
893 LIST *l = serv->list;
895 /* GROUP command failed */
896 if (!str_ncmp ("411", buf, 3)) {
897 mutt_error (_("Newsgroup %s not found on server %s"),
898 nntp_data->group, serv->conn->account.host);
900 /* CACHE: delete cache and line from .index */
901 nntp_delete_cache (nntp_data);
902 hash_delete (serv->newsgroups, nntp_data->group, NULL,
904 while (l && l->data != (void *) nntp_data)
915 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
916 &nntp_data->lastMessage, buf);
918 nntp_data->deleted = 0;
920 time (&serv->check_time);
923 * Check for max adding context. If it is greater than $nntp_context,
924 * strip off extra articles
926 first = nntp_data->firstMessage;
927 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
928 first = nntp_data->lastMessage - NntpContext + 1;
930 nntp_data->lastLoaded = first - 1;
931 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
934 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
936 char buf[LONG_STRING];
937 char path[_POSIX_PATH_MAX];
942 /* see if we already have the message in our cache */
944 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
947 /* if everything is fine, assign msg->fp and return */
948 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
949 (msg->fp = fopen (cache->path, "r")))
952 /* clear the previous entry */
953 unlink (cache->path);
954 mem_free (&cache->path);
956 cache->index = ctx->hdrs[msgno]->index;
958 cache->path = str_dup (path);
959 if (!(msg->fp = safe_fopen (path, "w+"))) {
960 mem_free (&cache->path);
964 if (ctx->hdrs[msgno]->article_num == 0)
965 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
966 ctx->hdrs[msgno]->env->message_id);
968 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
969 ctx->hdrs[msgno]->article_num);
971 bar.msg = _("Fetching message...");
973 mutt_progress_bar (&bar, 0);
975 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
976 msg->fp, ctx->tagged);
978 mutt_error (_("Article %d not found on server"),
979 ctx->hdrs[msgno]->article_num);
980 debug_print (1, ("%s\n", buf));
986 mem_free (&cache->path);
990 mutt_free_envelope (&ctx->hdrs[msgno]->env);
991 ctx->hdrs[msgno]->env =
992 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
993 /* fix content length */
994 fseeko (msg->fp, 0, SEEK_END);
995 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
996 ctx->hdrs[msgno]->content->offset;
998 /* this is called in mutt before the open which fetches the message,
999 * which is probably wrong, but we just call it again here to handle
1000 * the problem instead of fixing it.
1002 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1004 /* These would normally be updated in mx_update_context(), but the
1005 * full headers aren't parsed with XOVER, so the information wasn't
1008 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1010 mutt_clear_error ();
1017 int nntp_post (const char *msg)
1019 char buf[LONG_STRING];
1022 NNTP_DATA *nntp_data;
1024 if (Context && Context->magic == M_NNTP)
1025 nntp_data = (NNTP_DATA *) Context->data;
1027 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1028 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1029 mutt_error (_("Can't post article. No connection to news server."));
1032 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1035 if (!(f = safe_fopen (msg, "r"))) {
1036 mutt_error (_("Can't post article. Unable to open %s"), msg);
1040 strfcpy (buf, "POST\r\n", sizeof (buf));
1041 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1042 mutt_error (_("Can't post article. Connection to %s lost."),
1043 nntp_data->nserv->conn->account.host);
1046 if (buf[0] != '3') {
1047 mutt_error (_("Can't post article: %s"), buf);
1053 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1054 len = str_len (buf);
1055 if (buf[len - 1] == '\n') {
1056 buf[len - 1] = '\r';
1062 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1064 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1068 if (buf[str_len (buf) - 1] != '\n')
1069 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1070 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1071 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1072 mutt_error (_("Can't post article. Connection to %s lost."),
1073 nntp_data->nserv->conn->account.host);
1076 if (buf[0] != '2') {
1077 mutt_error (_("Can't post article: %s"), buf);
1084 /* nntp_logout_all: close all open connections. */
1085 void nntp_logout_all (void)
1087 char buf[LONG_STRING];
1090 conn = mutt_socket_head ();
1093 CONNECTION* next = conn->next;
1094 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1095 mutt_message (_("Closing connection to %s..."), conn->account.host);
1096 mutt_socket_write (conn, "QUIT\r\n");
1097 mutt_socket_readln (buf, sizeof (buf), conn);
1098 mutt_clear_error ();
1099 mutt_socket_close (conn);
1100 mutt_socket_free (conn);
1106 static void nntp_free_acache (NNTP_DATA * data)
1110 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1111 if (data->acache[i].path) {
1112 unlink (data->acache[i].path);
1113 mem_free (&data->acache[i].path);
1118 void nntp_delete_data (void *p)
1120 NNTP_DATA *data = (NNTP_DATA *) p;
1124 mem_free (&data->entries);
1125 mem_free (&data->desc);
1126 mem_free (&data->cache);
1127 nntp_free_acache (data);
1131 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1133 NNTP_DATA *data = ctx->data;
1135 /* CACHE: update cache and .index files */
1136 if ((option (OPTSAVEUNSUB) || data->subscribed))
1137 nntp_save_cache_group (ctx);
1138 nntp_free_acache (data);
1140 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1144 void nntp_fastclose_mailbox (CONTEXT * ctx)
1146 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1150 nntp_free_acache (data);
1151 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1153 nntp_save_cache_index (data->nserv);
1154 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1156 nntp_delete_data (data);
1158 nntp_sync_sidebar (data);
1161 /* commit changes and terminate connection */
1162 int nntp_close_mailbox (CONTEXT * ctx)
1166 mutt_message _("Quitting newsgroup...");
1169 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1172 if (data->nserv && data->nserv->conn && ctx->unread) {
1173 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1175 mutt_newsgroup_catchup (data->nserv, data->group);
1180 nntp_sync_mailbox (ctx, 0, NULL);
1181 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1184 news = ((NNTP_DATA *) ctx->data)->nserv;
1185 newsrc_gen_entries (ctx);
1186 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1187 mutt_newsrc_update (news);
1189 mutt_clear_error ();
1193 /* use the GROUP command to poll for new mail */
1194 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1196 char buf[LONG_STRING];
1199 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1203 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1205 nntp_error ("nntp_check_mailbox()", buf);
1209 if (str_ncmp ("211", buf, 3)) {
1211 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1213 nntp_error ("nntp_check_mailbox()", buf);
1218 if (!str_ncmp ("211", buf, 3)) {
1222 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1223 nntp_data->firstMessage = first;
1224 nntp_data->lastMessage = last;
1225 if (ctx && last > nntp_data->lastLoaded) {
1226 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1227 time (&nntp_data->nserv->check_time);
1230 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1231 nntp_data->unread = count;
1233 mutt_newsgroup_stat (nntp_data);
1234 /* active was renumbered? */
1235 if (last < nntp_data->lastLoaded) {
1236 if (!nntp_data->max) {
1237 nntp_data->entries = mem_calloc (5, sizeof (NEWSRC_ENTRY));
1240 nntp_data->lastCached = 0;
1242 nntp_data->entries[0].first = 1;
1243 nntp_data->entries[0].last = 0;
1245 nntp_sync_sidebar (nntp_data);
1248 time (&nntp_data->nserv->check_time);
1252 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1254 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1257 static int add_group (char *buf, void *serv)
1259 #define s ((NNTP_SERVER *) serv)
1260 char group[LONG_STRING], mod, desc[HUGE_STRING];
1262 NNTP_DATA *nntp_data;
1265 _checked = n; /* _checked have N, where N = number of groups */
1266 if (!buf) /* at EOF must be zerouth */
1273 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1276 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1278 nntp_data = mem_calloc (1, sizeof (NNTP_DATA) + str_len (group) + 1);
1279 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1280 strcpy (nntp_data->group, group);
1281 nntp_data->nserv = s;
1282 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1283 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1284 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1285 nntp_add_to_list (s, nntp_data);
1287 nntp_data->deleted = 0;
1288 nntp_data->firstMessage = first;
1289 nntp_data->lastMessage = last;
1291 nntp_data->allowed = 1;
1293 nntp_data->allowed = 0;
1294 if (nntp_data->desc)
1295 mem_free (&nntp_data->desc);
1297 nntp_data->desc = str_dup (desc);
1298 if (nntp_data->rc || nntp_data->lastCached)
1299 mutt_newsgroup_stat (nntp_data);
1300 else if (nntp_data->lastMessage &&
1301 nntp_data->firstMessage <= nntp_data->lastMessage)
1302 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1304 nntp_data->unread = 0;
1310 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1312 char buf[LONG_STRING];
1313 NNTP_DATA nntp_data;
1319 if (!serv || !serv->newgroups_time)
1322 if (nntp_open_connection (serv) < 0)
1325 /* check subscribed groups for new news */
1326 if (option (OPTSHOWNEWNEWS)) {
1327 mutt_message _("Checking for new messages...");
1329 for (l = serv->list; l; l = l->next) {
1330 serv->check_time = 0; /* really check! */
1331 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1332 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1334 sidebar_draw (CurrentMenu);
1339 mutt_message _("Checking for new newsgroups...");
1341 now = serv->newgroups_time;
1342 time (&serv->newgroups_time);
1344 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1345 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1346 t->tm_min, t->tm_sec);
1347 nntp_data.nserv = serv;
1348 if (Context && Context->magic == M_NNTP)
1349 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1351 nntp_data.group = NULL;
1353 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1354 add_group, serv, 0) != 0) {
1356 nntp_error ("nntp_check_newgroups()", buf);
1361 mutt_message _("Loading descriptions...");
1366 emp.next = serv->list;
1370 ((NNTP_DATA *) l->data)->new = 1;
1371 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1375 nntp_save_cache_index (serv);
1376 mutt_clear_error ();
1380 /* Load list of all newsgroups from cache ALL */
1381 int nntp_get_cache_all (NNTP_SERVER * serv)
1383 char buf[HUGE_STRING];
1386 nntp_cache_expand (buf, serv->cache);
1387 if ((f = safe_fopen (buf, "r"))) {
1390 while (fgets (buf, sizeof (buf), f) != NULL) {
1391 if (ReadInc && (i % ReadInc == 0))
1392 mutt_message (_("Loading list from cache... %d"), i);
1393 add_group (buf, serv);
1396 add_group (NULL, NULL);
1398 mutt_clear_error ();
1402 mem_free (&serv->cache);
1407 /* Load list of all newsgroups from active */
1408 int nntp_get_active (NNTP_SERVER * serv)
1410 char msg[SHORT_STRING];
1411 NNTP_DATA nntp_data;
1414 if (nntp_open_connection (serv) < 0)
1417 snprintf (msg, sizeof (msg),
1418 _("Loading list of all newsgroups on server %s..."),
1419 serv->conn->account.host);
1421 time (&serv->newgroups_time);
1422 nntp_data.nserv = serv;
1423 nntp_data.group = NULL;
1425 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
1427 nntp_error ("nntp_get_active()", "LIST\r\n");
1432 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1434 nntp_get_desc (&nntp_data, "*", msg, NULL);
1436 for (tmp = serv->list; tmp; tmp = tmp->next) {
1437 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1439 if (data && data->deleted && !data->rc) {
1440 nntp_delete_cache (data);
1441 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1445 nntp_save_cache_index (serv);
1447 mutt_clear_error ();
1452 * returns -1 if error ocurred while retrieving header,
1453 * number of articles which ones exist in context on success.
1455 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1459 /* if msgid is already in context, don't reload them */
1460 if (hash_find (ctx->id_hash, msgid))
1462 if (ctx->msgcount == ctx->hdrmax)
1463 mx_alloc_memory (ctx);
1464 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1465 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1467 mutt_message (_("Fetching %s from server..."), msgid);
1468 ret = nntp_read_header (ctx, msgid, 0);
1469 /* since nntp_read_header() may set read flag, we must reset it */
1470 ctx->hdrs[ctx->msgcount]->read = 0;
1472 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1475 mx_update_context (ctx, 1);
1485 unsigned int *child;
1488 static int check_children (char *s, void *c)
1490 #define cc ((CHILD_CONTEXT *) c)
1493 if (!s || (n = atoi (s)) == 0)
1495 for (i = 0; i < cc->ctx->msgcount; i++)
1496 if (cc->ctx->hdrs[i]->article_num == n)
1498 if (cc->num >= cc->max)
1499 mem_realloc (&cc->child, sizeof (unsigned int) * (cc->max += 25));
1500 cc->child[cc->num++] = n;
1506 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1508 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1510 int i, ret = 0, tmp = 0;
1513 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1514 !nntp_data->nserv->conn->account.host)
1516 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1518 if (!nntp_data->nserv->hasXPAT) {
1519 mutt_error (_("Server %s does not support this operation!"),
1520 nntp_data->nserv->conn->account.host);
1524 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1525 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1530 cc.child = mem_malloc (sizeof (unsigned int) * 25);
1531 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1532 mem_free (&cc.child);
1535 /* dont try to read the xover cache. check_children() already
1536 * made sure that we dont have the article, so we need to visit
1537 * the server. Reading the cache at this point is also bad
1538 * because it would duplicate messages */
1539 if (option (OPTNEWSCACHE)) {
1541 unset_option (OPTNEWSCACHE);
1543 for (i = 0; i < cc.num; i++) {
1544 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1546 if (ctx->msgcount &&
1547 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1548 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1551 set_option (OPTNEWSCACHE);
1552 mem_free (&cc.child);