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.
16 #include <lib-lib/mem.h>
17 #include <lib-lib/str.h>
18 #include <lib-lib/file.h>
19 #include <lib-lib/macros.h>
21 #include <lib-mime/mime.h>
24 #include "mutt_curses.h"
33 #include <lib-crypt/crypt.h>
35 #include "lib/debug.h"
42 #define WANT_LISTGROUP_COMMAND 0
44 static unsigned int _checked = 0;
46 void nntp_sync_sidebar (NNTP_DATA* data) {
51 if (list_empty (Incoming))
54 /* unfortunately, NNTP_DATA::group only is the plain
55 * group name, so for every single update, we need to
56 * compose the full string which must be defined via
57 * mailboxes command ;-((( FIXME
60 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
61 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
62 NONULL (data->nserv->conn->account.user),
63 *data->nserv->conn->account.pass ? ":" : "",
64 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
65 data->nserv->conn->account.host,
68 /* bail out if group not found via mailboxes */
69 if ((i = buffy_lookup (buf)) < 0)
72 tmp = (BUFFY*) Incoming->data[i];
73 /* copied from browser.c */
74 if (option (OPTMARKOLD) &&
75 data->lastCached >= data->firstMessage &&
76 data->lastCached <= data->lastMessage)
77 tmp->msg_unread = data->lastMessage - data->lastCached;
79 tmp->msg_unread = data->unread;
80 tmp->new = data->unread > 0;
81 /* this is closest to a "total" count we can get */
82 tmp->msgcount = data->lastMessage - data->firstMessage;
85 static void nntp_error (const char *where, const char *msg) {
86 debug_print (1, ("unexpected response in %s: %s\n", where, msg));
89 static int nntp_auth (NNTP_SERVER * serv)
91 CONNECTION *conn = serv->conn;
93 unsigned char flags = conn->account.flags;
95 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
96 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
97 conn->account.flags = flags;
101 mutt_message _("Logging in...");
103 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
104 mutt_socket_write (conn, buf);
105 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
106 conn->account.flags = flags;
111 /* don't print the password unless we're at the ungodly debugging level */
112 if (DebugLevel < M_SOCK_LOG_FULL)
113 debug_print (M_SOCK_LOG_CMD, ("> AUTHINFO PASS *\n"));
115 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
116 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
117 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
118 conn->account.flags = flags;
122 if (m_strncmp("281", buf, 3)) {
123 conn->account.flags = flags;
124 mutt_error _("Login failed.");
133 static int nntp_connect_error (NNTP_SERVER * serv)
135 serv->status = NNTP_NONE;
136 mutt_socket_close (serv->conn);
137 mutt_error _("Server closed connection!");
143 static int nntp_connect_and_auth (NNTP_SERVER * serv)
145 CONNECTION *conn = serv->conn;
149 serv->status = NNTP_NONE;
151 if (mutt_socket_open (conn) < 0)
154 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
155 return nntp_connect_error (serv);
157 if (!m_strncmp("200", buf, 3))
158 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
159 else if (!m_strncmp("201", buf, 3))
160 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
162 mutt_socket_close(conn);
164 mutt_error("%s", buf);
171 /* Tell INN to switch to mode reader if it isn't so. Ignore all
172 returned codes and messages. */
173 mutt_socket_write (conn, "MODE READER\r\n");
174 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
175 return nntp_connect_error (serv);
177 mutt_socket_write (conn, "STAT\r\n");
178 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
179 return nntp_connect_error (serv);
181 if (!(conn->account.flags & M_ACCT_USER) && m_strncmp("480", buf, 3)) {
182 serv->status = NNTP_OK;
186 rc = nntp_auth (serv);
188 return nntp_connect_error (serv);
190 mutt_socket_close (conn);
191 serv->status = NNTP_BYE;
195 mutt_socket_close (conn);
196 mutt_error _("Login failed.");
201 serv->status = NNTP_OK;
205 static int nntp_attempt_features (NNTP_SERVER * serv)
207 char buf[LONG_STRING];
208 CONNECTION *conn = serv->conn;
210 mutt_socket_write (conn, "XOVER\r\n");
211 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
212 return nntp_connect_error (serv);
213 if (m_strncmp("500", buf, 3))
216 mutt_socket_write (conn, "XPAT\r\n");
217 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
218 return nntp_connect_error (serv);
219 if (m_strncmp("500", buf, 3))
222 #if WANT_LISTGROUP_COMMAND
223 mutt_socket_write (conn, "LISTGROUP\r\n");
224 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
225 return (nntp_connect_error (serv));
226 if (m_strncmp("500", buf, 3))
227 serv->hasLISTGROUP = 1;
230 mutt_socket_write (conn, "XGTITLE +\r\n");
231 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
232 return nntp_connect_error (serv);
233 if (m_strncmp("500", buf, 3))
234 serv->hasXGTITLE = 1;
236 if (!m_strncmp("282", buf, 3)) {
238 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
239 return nntp_connect_error (serv);
240 } while (!(buf[0] == '.' && buf[1] == '\0'));
246 static int nntp_open_connection (NNTP_SERVER * serv)
248 if (serv->status == NNTP_OK)
250 if (serv->status == NNTP_BYE)
252 if (nntp_connect_and_auth (serv) < 0)
254 if (nntp_attempt_features (serv) < 0)
259 static int nntp_reconnect (NNTP_SERVER * serv)
261 char buf[SHORT_STRING];
263 mutt_socket_close (serv->conn);
266 if (nntp_connect_and_auth (serv) == 0)
269 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
270 serv->conn->account.host);
271 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
272 serv->status = NNTP_BYE;
278 /* Send data from line[LONG_STRING] and receive answer to same line */
279 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
281 char buf[LONG_STRING];
284 if (data->nserv->status == NNTP_BYE)
289 mutt_socket_write (data->nserv->conn, line);
291 else if (data->group) {
292 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
293 mutt_socket_write (data->nserv->conn, buf);
297 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
298 if (nntp_reconnect (data->nserv) < 0)
302 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
303 mutt_socket_write (data->nserv->conn, buf);
304 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
310 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
314 m_strcpy(line, linelen, buf);
319 * This function calls funct(*line, *data) for each received line,
320 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
323 * 1 - correct but not performed (may be, have to be continued),
324 * -1 - conection lost,
325 * -2 - invalid command or execution error,
326 * -3 - error in funct(*line, *data).
328 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg,
329 progress_t* bar, int (*funct) (char *, void *),
330 void *data, int tagged)
332 char buf[LONG_STRING];
341 m_strcpy(buf, sizeof(buf), query);
342 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
351 inbuf = p_new(char, sizeof(buf));
354 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
360 if (!lenbuf && buf[0] == '.') {
361 if (buf[1] == '\0') {
369 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
372 if (chunk >= sizeof (buf)) {
373 lenbuf += m_strlen(p);
377 mutt_progress_bar (bar, pos);
380 if (ReadInc && (line % ReadInc == 0)) {
382 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
384 mutt_message ("%s %d", msg, line);
388 if (ret == 0 && funct (inbuf, data) < 0)
393 p_realloc(&inbuf, lenbuf + sizeof (buf));
402 static int nntp_read_tempfile (char *line, void *file)
404 FILE *f = (FILE *) file;
410 if (fputc ('\n', f) == EOF)
416 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
419 register char *p, *b;
420 register char *colon = NULL;
424 /* skip to next word */
426 while (*b && ((*b == ' ') || (*b == '\t')))
430 /* skip to end of word */
431 while (*p && (*p != ' ') && (*p != '\t')) {
443 nntp_get_status (ctx, h, b, atoi (colon));
444 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
445 h->article_num = atoi (colon);
453 * 1 if article not found
454 * -1 if read or write error on tempfile or socket
456 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
459 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
461 char buf[LONG_STRING];
462 char tempfile[_POSIX_PATH_MAX];
464 HEADER *h = ctx->hdrs[ctx->msgcount];
466 mutt_mktemp (tempfile);
467 if (!(f = safe_fopen (tempfile, "w+")))
471 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
473 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
475 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
478 debug_print (1, ("%s\n", buf));
481 return (ret == -1 ? -1 : 1);
484 h->article_num = article_num;
485 h->env = mutt_read_rfc822_header (f, h, 0, 0);
489 if (h->env->xref != NULL)
490 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
491 else if (h->article_num == 0 && msgid) {
492 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
493 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
494 h->article_num = atoi (buf + 4);
500 static int parse_description (char *line, void *n)
502 #define news ((NNTP_SERVER *) n)
503 register char *d = line;
508 while (*d && *d != '\t' && *d != ' ')
512 while (*d && (*d == '\t' || *d == ' '))
514 debug_print (2, ("group: %s, desc: %s\n", line, d));
515 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
516 m_strcmp(d, data->desc)) {
517 p_delete(&data->desc);
518 data->desc = m_strdup(d);
524 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
528 if (!option (OPTLOADDESC) || !data || !data->nserv)
531 /* Get newsgroup description, if we can */
532 if (data->nserv->hasXGTITLE)
533 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
535 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
536 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
539 nntp_error ("nntp_get_desc()", buf);
545 * XOVER returns a tab separated list of:
546 * id|subject|from|date|Msgid|references|bytes|lines|xref
548 * This has to duplicate some of the functionality of
549 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
550 * a limited number of headers which are "parsed" by placement in the list)
552 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
554 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
558 hdr->env = envelope_new();
559 hdr->env->newsgroups = m_strdup(nntp_data->group);
560 hdr->content = mutt_new_body ();
561 hdr->content->type = TYPETEXT;
562 hdr->content->subtype = m_strdup("plain");
563 hdr->content->encoding = ENC7BIT;
564 hdr->content->disposition = DISPINLINE;
565 hdr->content->length = -1;
568 for (x = 0; !done && x < 9; x++) {
569 /* if from file, need to skip newline character */
570 while (*p && *p != '\n' && *p != '\t')
579 hdr->article_num = atoi (b);
580 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
583 hdr->env->subject = m_strdup(b);
586 address_delete (&hdr->env->from);
587 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
588 /* same as for mutt_parse_rfc822_line():
589 * don't leave from info NULL if there's an invalid address (or
590 * whatever) in From: field; mutt would just display it as empty
591 * and mark mail/(esp.) news article as your own. aaargh! this
592 * bothered me for _years_ */
593 if (!hdr->env->from) {
594 hdr->env->from = address_new ();
595 hdr->env->from->personal = m_strdup(b);
599 hdr->date_sent = mutt_parse_date (b, hdr);
600 hdr->received = hdr->date_sent;
603 p_delete(&hdr->env->message_id);
604 hdr->env->message_id = m_strdup(b);
607 mutt_free_list (&hdr->env->references);
608 hdr->env->references = mutt_parse_references (b, 0);
611 hdr->content->length = atoi (b);
614 hdr->lines = atoi (b);
618 p_delete(&hdr->env->xref);
619 b = b + 6; /* skips the "Xref: " */
620 hdr->env->xref = m_strdup(b);
621 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
623 rfc2047_decode_envelope(hdr->env);
636 unsigned short *messages;
640 #define fc ((FETCH_CONTEXT *) c)
642 #if WANT_LISTGROUP_COMMAND
643 static int _nntp_fetch_numbers (unsigned int num, void *c)
645 if (num < fc->base || num > fc->last)
647 fc->messages[num - fc->base] = 1;
650 static int nntp_fetch_numbers (char *line, void *c)
654 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
658 static int add_xover_line (char *line, void *c)
660 unsigned int num, total;
661 CONTEXT *ctx = fc->ctx;
662 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
667 if (ctx->msgcount >= ctx->hdrmax)
668 mx_alloc_memory (ctx);
669 ctx->hdrs[ctx->msgcount] = header_new();
670 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
672 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
673 num = ctx->hdrs[ctx->msgcount]->article_num;
675 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
677 if (num > data->lastLoaded)
678 data->lastLoaded = num;
679 num = num - fc->first + 1;
680 total = fc->last - fc->first + 1;
681 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
682 mutt_message ("%s %d/%d", fc->msg, num, total);
685 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
692 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
695 char buf[HUGE_STRING];
696 char *msg = _("Fetching message headers...");
697 char *msg2 = _("Fetching headers from cache...");
698 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
702 unsigned int current;
706 /* if empty group or nothing to do */
707 if (!last || first > last)
710 /* fetch list of articles */
711 mutt_message _("Fetching list of articles...");
716 fc.messages = p_new(unsigned short, last - first + 1);
717 #if WANT_LISTGROUP_COMMAND
718 if (nntp_data->nserv->hasLISTGROUP) {
719 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
720 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
722 mutt_error (_("LISTGROUP command failed: %s"), buf);
725 nntp_error ("nntp_fetch_headers()", buf);
727 p_delete(&fc.messages);
733 for (num = 0; num < last - first + 1; num++)
734 fc.messages[num] = 1;
735 #if WANT_LISTGROUP_COMMAND
739 /* CACHE: must be loaded xover cache here */
740 num = nntp_data->lastCached - first + 1;
741 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
742 nntp_cache_expand (buf, nntp_data->cache);
745 if ((f = safe_fopen (buf, "r"))) {
748 /* counting number of lines */
749 while (fgets (buf, sizeof (buf), f) != NULL)
752 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
754 oldmsgcount = ctx->msgcount;
756 fc.last = first + num - 1;
758 while (fgets (buf, sizeof (buf), f) != NULL) {
759 if (ReadInc && ((++c) % ReadInc == 0))
760 mutt_message ("%s %d/%d", msg2, c, r);
761 add_xover_line (buf, &fc);
764 nntp_data->lastLoaded = fc.last;
766 if (ctx->msgcount > oldmsgcount)
767 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
770 nntp_delete_cache (nntp_data);
772 num = last - first + 1;
774 p_delete(&fc.messages);
779 * Without XOVER, we have to fetch each article header and parse
780 * it. With XOVER, we ask for all of them
783 if (nntp_data->nserv->hasXOVER) {
784 oldmsgcount = ctx->msgcount;
788 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
789 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
790 if (ctx->msgcount > oldmsgcount)
791 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
793 mutt_error (_("XOVER command failed: %s"), buf);
795 nntp_error ("nntp_fetch_headers()", buf);
797 p_delete(&fc.messages);
803 for (current = first; current <= last; current++) {
806 ret = current - first + 1;
807 mutt_message ("%s %d/%d", msg, ret, num);
809 if (!fc.messages[current - fc.base])
812 if (ctx->msgcount >= ctx->hdrmax)
813 mx_alloc_memory (ctx);
814 h = ctx->hdrs[ctx->msgcount] = header_new();
815 h->index = ctx->msgcount;
817 ret = nntp_read_header (ctx, NULL, current);
818 if (ret == 0) { /* Got article. Fetch next header */
819 nntp_get_status (ctx, h, NULL, h->article_num);
821 mx_update_context (ctx, 1);
824 header_delete(&h); /* skip it */
826 p_delete(&fc.messages);
830 if (current > nntp_data->lastLoaded)
831 nntp_data->lastLoaded = current;
833 p_delete(&fc.messages);
834 nntp_data->lastLoaded = last;
840 * currently, nntp "mailbox" is "newsgroup"
842 int nntp_open_mailbox (CONTEXT * ctx)
844 NNTP_DATA *nntp_data;
846 char buf[HUGE_STRING];
847 char server[LONG_STRING];
854 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
855 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
861 nntp_expand_path (server, sizeof (server), &acct);
862 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
865 CurrentNewsSrv = serv;
867 /* create NNTP-specific state struct if nof found in list */
868 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
869 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
870 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
871 strcpy (nntp_data->group, buf);
872 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
873 nntp_add_to_list (serv, nntp_data);
875 ctx->data = nntp_data;
876 nntp_data->nserv = serv;
878 mutt_message (_("Selecting %s..."), nntp_data->group);
880 if (!nntp_data->desc) {
881 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
883 nntp_save_cache_index (serv);
887 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
889 nntp_error ("nntp_open_mailbox()", buf);
894 if (m_strncmp("211", buf, 3)) {
895 LIST *l = serv->list;
897 /* GROUP command failed */
898 if (!m_strncmp("411", buf, 3)) {
899 mutt_error (_("Newsgroup %s not found on server %s"),
900 nntp_data->group, serv->conn->account.host);
902 /* CACHE: delete cache and line from .index */
903 nntp_delete_cache (nntp_data);
904 hash_delete (serv->newsgroups, nntp_data->group, NULL,
906 while (l && l->data != (void *) nntp_data)
917 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
918 &nntp_data->lastMessage, buf);
920 nntp_data->deleted = 0;
922 time (&serv->check_time);
925 * Check for max adding context. If it is greater than $nntp_context,
926 * strip off extra articles
928 first = nntp_data->firstMessage;
929 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
930 first = nntp_data->lastMessage - NntpContext + 1;
932 nntp_data->lastLoaded = first - 1;
933 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
936 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
938 char buf[LONG_STRING];
939 char path[_POSIX_PATH_MAX];
944 /* see if we already have the message in our cache */
946 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
949 /* if everything is fine, assign msg->fp and return */
950 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
951 (msg->fp = fopen (cache->path, "r")))
954 /* clear the previous entry */
955 unlink (cache->path);
956 p_delete(&cache->path);
958 cache->index = ctx->hdrs[msgno]->index;
960 cache->path = m_strdup(path);
961 if (!(msg->fp = safe_fopen (path, "w+"))) {
962 p_delete(&cache->path);
966 if (ctx->hdrs[msgno]->article_num == 0)
967 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
968 ctx->hdrs[msgno]->env->message_id);
970 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
971 ctx->hdrs[msgno]->article_num);
973 bar.msg = _("Fetching message...");
975 mutt_progress_bar (&bar, 0);
977 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
978 msg->fp, ctx->tagged);
980 mutt_error (_("Article %d not found on server"),
981 ctx->hdrs[msgno]->article_num);
982 debug_print (1, ("%s\n", buf));
988 p_delete(&cache->path);
992 envelope_delete(&ctx->hdrs[msgno]->env);
993 ctx->hdrs[msgno]->env =
994 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
995 /* fix content length */
996 fseeko (msg->fp, 0, SEEK_END);
997 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
998 ctx->hdrs[msgno]->content->offset;
1000 /* this is called in mutt before the open which fetches the message,
1001 * which is probably wrong, but we just call it again here to handle
1002 * the problem instead of fixing it.
1004 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1006 /* These would normally be updated in mx_update_context(), but the
1007 * full headers aren't parsed with XOVER, so the information wasn't
1010 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1012 mutt_clear_error ();
1019 int nntp_post (const char *msg)
1021 char buf[LONG_STRING];
1024 NNTP_DATA *nntp_data;
1026 if (Context && Context->magic == M_NNTP)
1027 nntp_data = (NNTP_DATA *) Context->data;
1029 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1030 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1031 mutt_error (_("Can't post article. No connection to news server."));
1034 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1037 if (!(f = safe_fopen (msg, "r"))) {
1038 mutt_error (_("Can't post article. Unable to open %s"), msg);
1042 m_strcpy(buf, sizeof(buf), "POST\r\n");
1043 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1044 mutt_error (_("Can't post article. Connection to %s lost."),
1045 nntp_data->nserv->conn->account.host);
1048 if (buf[0] != '3') {
1049 mutt_error (_("Can't post article: %s"), buf);
1055 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1056 len = m_strlen(buf);
1057 if (buf[len - 1] == '\n') {
1058 buf[len - 1] = '\r';
1064 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1066 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1070 if (buf[m_strlen(buf) - 1] != '\n')
1071 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1072 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1073 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1074 mutt_error (_("Can't post article. Connection to %s lost."),
1075 nntp_data->nserv->conn->account.host);
1078 if (buf[0] != '2') {
1079 mutt_error (_("Can't post article: %s"), buf);
1086 /* nntp_logout_all: close all open connections. */
1087 void nntp_logout_all (void)
1089 char buf[LONG_STRING];
1092 conn = mutt_socket_head ();
1095 CONNECTION* next = conn->next;
1096 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1097 mutt_message (_("Closing connection to %s..."), conn->account.host);
1098 mutt_socket_write (conn, "QUIT\r\n");
1099 mutt_socket_readln (buf, sizeof (buf), conn);
1100 mutt_clear_error ();
1101 mutt_socket_close (conn);
1102 mutt_socket_free (conn);
1108 static void nntp_free_acache (NNTP_DATA * data)
1112 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1113 if (data->acache[i].path) {
1114 unlink (data->acache[i].path);
1115 p_delete(&data->acache[i].path);
1120 void nntp_delete_data (void *p)
1122 NNTP_DATA *data = (NNTP_DATA *)p;
1126 p_delete(&data->entries);
1127 p_delete(&data->desc);
1128 p_delete(&data->cache);
1129 nntp_free_acache (data);
1133 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1135 NNTP_DATA *data = ctx->data;
1137 /* CACHE: update cache and .index files */
1138 if ((option (OPTSAVEUNSUB) || data->subscribed))
1139 nntp_save_cache_group (ctx);
1140 nntp_free_acache (data);
1142 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1146 void nntp_fastclose_mailbox (CONTEXT * ctx)
1148 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1152 nntp_free_acache (data);
1153 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1155 nntp_save_cache_index (data->nserv);
1156 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1158 nntp_delete_data (data);
1160 nntp_sync_sidebar (data);
1163 /* commit changes and terminate connection */
1164 int nntp_close_mailbox (CONTEXT * ctx)
1168 mutt_message _("Quitting newsgroup...");
1171 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1174 if (data->nserv && data->nserv->conn && ctx->unread) {
1175 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1177 mutt_newsgroup_catchup (data->nserv, data->group);
1182 nntp_sync_mailbox (ctx, 0, NULL);
1183 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1186 news = ((NNTP_DATA *) ctx->data)->nserv;
1187 newsrc_gen_entries (ctx);
1188 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1189 mutt_newsrc_update (news);
1191 mutt_clear_error ();
1195 /* use the GROUP command to poll for new mail */
1196 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1198 char buf[LONG_STRING];
1201 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1205 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1207 nntp_error ("nntp_check_mailbox()", buf);
1211 if (m_strncmp("211", buf, 3)) {
1213 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1215 nntp_error ("nntp_check_mailbox()", buf);
1220 if (!m_strncmp("211", buf, 3)) {
1224 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1225 nntp_data->firstMessage = first;
1226 nntp_data->lastMessage = last;
1227 if (ctx && last > nntp_data->lastLoaded) {
1228 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1229 time (&nntp_data->nserv->check_time);
1232 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1233 nntp_data->unread = count;
1235 mutt_newsgroup_stat (nntp_data);
1236 /* active was renumbered? */
1237 if (last < nntp_data->lastLoaded) {
1238 if (!nntp_data->max) {
1239 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1242 nntp_data->lastCached = 0;
1244 nntp_data->entries[0].first = 1;
1245 nntp_data->entries[0].last = 0;
1247 nntp_sync_sidebar (nntp_data);
1250 time (&nntp_data->nserv->check_time);
1254 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1256 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1259 static int add_group (char *buf, void *serv)
1261 #define s ((NNTP_SERVER *) serv)
1262 char group[LONG_STRING], mod, desc[HUGE_STRING];
1264 NNTP_DATA *nntp_data;
1267 _checked = n; /* _checked have N, where N = number of groups */
1268 if (!buf) /* at EOF must be zerouth */
1275 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1278 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1280 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1281 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1282 strcpy (nntp_data->group, group);
1283 nntp_data->nserv = s;
1284 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1285 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1286 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1287 nntp_add_to_list (s, nntp_data);
1289 nntp_data->deleted = 0;
1290 nntp_data->firstMessage = first;
1291 nntp_data->lastMessage = last;
1293 nntp_data->allowed = 1;
1295 nntp_data->allowed = 0;
1296 if (nntp_data->desc)
1297 p_delete(&nntp_data->desc);
1299 nntp_data->desc = m_strdup(desc);
1300 if (nntp_data->rc || nntp_data->lastCached)
1301 mutt_newsgroup_stat (nntp_data);
1302 else if (nntp_data->lastMessage &&
1303 nntp_data->firstMessage <= nntp_data->lastMessage)
1304 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1306 nntp_data->unread = 0;
1312 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1314 char buf[LONG_STRING];
1315 NNTP_DATA nntp_data;
1321 if (!serv || !serv->newgroups_time)
1324 if (nntp_open_connection (serv) < 0)
1327 /* check subscribed groups for new news */
1328 if (option (OPTSHOWNEWNEWS)) {
1329 mutt_message _("Checking for new messages...");
1331 for (l = serv->list; l; l = l->next) {
1332 serv->check_time = 0; /* really check! */
1333 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1334 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1336 sidebar_draw (CurrentMenu);
1341 mutt_message _("Checking for new newsgroups...");
1343 now = serv->newgroups_time;
1344 time (&serv->newgroups_time);
1346 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1347 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1348 t->tm_min, t->tm_sec);
1349 nntp_data.nserv = serv;
1350 if (Context && Context->magic == M_NNTP)
1351 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1353 nntp_data.group = NULL;
1355 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1356 add_group, serv, 0) != 0) {
1358 nntp_error ("nntp_check_newgroups()", buf);
1363 mutt_message _("Loading descriptions...");
1368 emp.next = serv->list;
1372 ((NNTP_DATA *) l->data)->new = 1;
1373 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1377 nntp_save_cache_index (serv);
1378 mutt_clear_error ();
1382 /* Load list of all newsgroups from cache ALL */
1383 int nntp_get_cache_all (NNTP_SERVER * serv)
1385 char buf[HUGE_STRING];
1388 nntp_cache_expand (buf, serv->cache);
1389 if ((f = safe_fopen (buf, "r"))) {
1392 while (fgets (buf, sizeof (buf), f) != NULL) {
1393 if (ReadInc && (i % ReadInc == 0))
1394 mutt_message (_("Loading list from cache... %d"), i);
1395 add_group (buf, serv);
1398 add_group (NULL, NULL);
1400 mutt_clear_error ();
1404 p_delete(&serv->cache);
1409 /* Load list of all newsgroups from active */
1410 int nntp_get_active (NNTP_SERVER * serv)
1412 char msg[SHORT_STRING];
1413 NNTP_DATA nntp_data;
1416 if (nntp_open_connection (serv) < 0)
1419 snprintf (msg, sizeof (msg),
1420 _("Loading list of all newsgroups on server %s..."),
1421 serv->conn->account.host);
1423 time (&serv->newgroups_time);
1424 nntp_data.nserv = serv;
1425 nntp_data.group = NULL;
1427 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
1429 nntp_error ("nntp_get_active()", "LIST\r\n");
1434 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1436 nntp_get_desc (&nntp_data, "*", msg, NULL);
1438 for (tmp = serv->list; tmp; tmp = tmp->next) {
1439 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1441 if (data && data->deleted && !data->rc) {
1442 nntp_delete_cache (data);
1443 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1447 nntp_save_cache_index (serv);
1449 mutt_clear_error ();
1454 * returns -1 if error ocurred while retrieving header,
1455 * number of articles which ones exist in context on success.
1457 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1461 /* if msgid is already in context, don't reload them */
1462 if (hash_find (ctx->id_hash, msgid))
1464 if (ctx->msgcount == ctx->hdrmax)
1465 mx_alloc_memory (ctx);
1466 ctx->hdrs[ctx->msgcount] = header_new();
1467 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1469 mutt_message (_("Fetching %s from server..."), msgid);
1470 ret = nntp_read_header (ctx, msgid, 0);
1471 /* since nntp_read_header() may set read flag, we must reset it */
1472 ctx->hdrs[ctx->msgcount]->read = 0;
1474 header_delete(&ctx->hdrs[ctx->msgcount]);
1477 mx_update_context (ctx, 1);
1487 unsigned int *child;
1490 static int check_children (char *s, void *c)
1492 #define cc ((CHILD_CONTEXT *) c)
1495 if (!s || (n = atoi (s)) == 0)
1497 for (i = 0; i < cc->ctx->msgcount; i++)
1498 if (cc->ctx->hdrs[i]->article_num == n)
1500 if (cc->num >= cc->max)
1501 p_realloc(&cc->child, cc->max += 25);
1502 cc->child[cc->num++] = n;
1508 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1510 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1512 int i, ret = 0, tmp = 0;
1515 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1516 !nntp_data->nserv->conn->account.host)
1518 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1520 if (!nntp_data->nserv->hasXPAT) {
1521 mutt_error (_("Server %s does not support this operation!"),
1522 nntp_data->nserv->conn->account.host);
1526 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1527 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1532 cc.child = p_new(unsigned int, 25);
1533 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1534 p_delete(&cc.child);
1537 /* dont try to read the xover cache. check_children() already
1538 * made sure that we dont have the article, so we need to visit
1539 * the server. Reading the cache at this point is also bad
1540 * because it would duplicate messages */
1541 if (option (OPTNEWSCACHE)) {
1543 unset_option (OPTNEWSCACHE);
1545 for (i = 0; i < cc.num; i++) {
1546 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1548 if (ctx->msgcount &&
1549 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1550 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1553 set_option (OPTNEWSCACHE);
1554 p_delete(&cc.child);