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/macros.h>
21 #include "mutt_curses.h"
32 #include "mutt_crypt.h"
34 #include "lib/debug.h"
41 #define WANT_LISTGROUP_COMMAND 0
43 static unsigned int _checked = 0;
45 void nntp_sync_sidebar (NNTP_DATA* data) {
50 if (list_empty (Incoming))
53 /* unfortunately, NNTP_DATA::group only is the plain
54 * group name, so for every single update, we need to
55 * compose the full string which must be defined via
56 * mailboxes command ;-((( FIXME
59 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
60 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
61 NONULL (data->nserv->conn->account.user),
62 *data->nserv->conn->account.pass ? ":" : "",
63 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
64 data->nserv->conn->account.host,
67 /* bail out if group not found via mailboxes */
68 if ((i = buffy_lookup (buf)) < 0)
71 tmp = (BUFFY*) Incoming->data[i];
72 /* copied from browser.c */
73 if (option (OPTMARKOLD) &&
74 data->lastCached >= data->firstMessage &&
75 data->lastCached <= data->lastMessage)
76 tmp->msg_unread = data->lastMessage - data->lastCached;
78 tmp->msg_unread = data->unread;
79 tmp->new = data->unread > 0;
80 /* this is closest to a "total" count we can get */
81 tmp->msgcount = data->lastMessage - data->firstMessage;
84 static void nntp_error (const char *where, const char *msg) {
85 debug_print (1, ("unexpected response in %s: %s\n", where, msg));
88 static int nntp_auth (NNTP_SERVER * serv)
90 CONNECTION *conn = serv->conn;
92 unsigned char flags = conn->account.flags;
94 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
95 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
96 conn->account.flags = flags;
100 mutt_message _("Logging in...");
102 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
103 mutt_socket_write (conn, buf);
104 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
105 conn->account.flags = flags;
110 /* don't print the password unless we're at the ungodly debugging level */
111 if (DebugLevel < M_SOCK_LOG_FULL)
112 debug_print (M_SOCK_LOG_CMD, ("> AUTHINFO PASS *\n"));
114 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
115 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
116 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
117 conn->account.flags = flags;
121 if (str_ncmp ("281", buf, 3)) {
122 conn->account.flags = flags;
123 mutt_error _("Login failed.");
132 static int nntp_connect_error (NNTP_SERVER * serv)
134 serv->status = NNTP_NONE;
135 mutt_socket_close (serv->conn);
136 mutt_error _("Server closed connection!");
142 static int nntp_connect_and_auth (NNTP_SERVER * serv)
144 CONNECTION *conn = serv->conn;
148 serv->status = NNTP_NONE;
150 if (mutt_socket_open (conn) < 0)
153 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
154 return nntp_connect_error (serv);
156 if (!str_ncmp ("200", buf, 3))
157 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
158 else if (!str_ncmp ("201", buf, 3))
159 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
161 mutt_socket_close (conn);
162 str_skip_trailws (buf);
163 mutt_error ("%s", buf);
170 /* Tell INN to switch to mode reader if it isn't so. Ignore all
171 returned codes and messages. */
172 mutt_socket_write (conn, "MODE READER\r\n");
173 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
174 return nntp_connect_error (serv);
176 mutt_socket_write (conn, "STAT\r\n");
177 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
178 return nntp_connect_error (serv);
180 if (!(conn->account.flags & M_ACCT_USER) && str_ncmp ("480", buf, 3)) {
181 serv->status = NNTP_OK;
185 rc = nntp_auth (serv);
187 return nntp_connect_error (serv);
189 mutt_socket_close (conn);
190 serv->status = NNTP_BYE;
194 mutt_socket_close (conn);
195 mutt_error _("Login failed.");
200 serv->status = NNTP_OK;
204 static int nntp_attempt_features (NNTP_SERVER * serv)
206 char buf[LONG_STRING];
207 CONNECTION *conn = serv->conn;
209 mutt_socket_write (conn, "XOVER\r\n");
210 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
211 return nntp_connect_error (serv);
212 if (str_ncmp ("500", buf, 3))
215 mutt_socket_write (conn, "XPAT\r\n");
216 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
217 return nntp_connect_error (serv);
218 if (str_ncmp ("500", buf, 3))
221 #if WANT_LISTGROUP_COMMAND
222 mutt_socket_write (conn, "LISTGROUP\r\n");
223 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
224 return (nntp_connect_error (serv));
225 if (str_ncmp ("500", buf, 3))
226 serv->hasLISTGROUP = 1;
229 mutt_socket_write (conn, "XGTITLE +\r\n");
230 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
231 return nntp_connect_error (serv);
232 if (str_ncmp ("500", buf, 3))
233 serv->hasXGTITLE = 1;
235 if (!str_ncmp ("282", buf, 3)) {
237 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
238 return nntp_connect_error (serv);
239 } while (!(buf[0] == '.' && buf[1] == '\0'));
245 static int nntp_open_connection (NNTP_SERVER * serv)
247 if (serv->status == NNTP_OK)
249 if (serv->status == NNTP_BYE)
251 if (nntp_connect_and_auth (serv) < 0)
253 if (nntp_attempt_features (serv) < 0)
258 static int nntp_reconnect (NNTP_SERVER * serv)
260 char buf[SHORT_STRING];
262 mutt_socket_close (serv->conn);
265 if (nntp_connect_and_auth (serv) == 0)
268 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
269 serv->conn->account.host);
270 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
271 serv->status = NNTP_BYE;
277 /* Send data from line[LONG_STRING] and receive answer to same line */
278 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
280 char buf[LONG_STRING];
283 if (data->nserv->status == NNTP_BYE)
288 mutt_socket_write (data->nserv->conn, line);
290 else if (data->group) {
291 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
292 mutt_socket_write (data->nserv->conn, buf);
296 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
297 if (nntp_reconnect (data->nserv) < 0)
301 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
302 mutt_socket_write (data->nserv->conn, buf);
303 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
309 else if ((!str_ncmp ("480", buf, 3)) && nntp_auth (data->nserv) < 0)
313 strfcpy (line, buf, linelen);
318 * This function calls funct(*line, *data) for each received line,
319 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
322 * 1 - correct but not performed (may be, have to be continued),
323 * -1 - conection lost,
324 * -2 - invalid command or execution error,
325 * -3 - error in funct(*line, *data).
327 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg,
328 progress_t* bar, int (*funct) (char *, void *),
329 void *data, int tagged)
331 char buf[LONG_STRING];
340 strfcpy (buf, query, sizeof (buf));
341 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
350 inbuf = p_new(char, sizeof(buf));
353 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
359 if (!lenbuf && buf[0] == '.') {
360 if (buf[1] == '\0') {
368 strfcpy (inbuf + lenbuf, p, sizeof (buf));
371 if (chunk >= sizeof (buf)) {
372 lenbuf += m_strlen(p);
376 mutt_progress_bar (bar, pos);
379 if (ReadInc && (line % ReadInc == 0)) {
381 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
383 mutt_message ("%s %d", msg, line);
387 if (ret == 0 && funct (inbuf, data) < 0)
392 p_realloc(&inbuf, lenbuf + sizeof (buf));
401 static int nntp_read_tempfile (char *line, void *file)
403 FILE *f = (FILE *) file;
409 if (fputc ('\n', f) == EOF)
415 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
418 register char *p, *b;
419 register char *colon = NULL;
423 /* skip to next word */
425 while (*b && ((*b == ' ') || (*b == '\t')))
429 /* skip to end of word */
430 while (*p && (*p != ' ') && (*p != '\t')) {
442 nntp_get_status (ctx, h, b, atoi (colon));
443 if (h && h->article_num == 0 && str_cmp (group, b) == 0)
444 h->article_num = atoi (colon);
452 * 1 if article not found
453 * -1 if read or write error on tempfile or socket
455 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
458 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
460 char buf[LONG_STRING];
461 char tempfile[_POSIX_PATH_MAX];
463 HEADER *h = ctx->hdrs[ctx->msgcount];
465 mutt_mktemp (tempfile);
466 if (!(f = safe_fopen (tempfile, "w+")))
470 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
472 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
474 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
477 debug_print (1, ("%s\n", buf));
480 return (ret == -1 ? -1 : 1);
483 h->article_num = article_num;
484 h->env = mutt_read_rfc822_header (f, h, 0, 0);
488 if (h->env->xref != NULL)
489 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
490 else if (h->article_num == 0 && msgid) {
491 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
492 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
493 h->article_num = atoi (buf + 4);
499 static int parse_description (char *line, void *n)
501 #define news ((NNTP_SERVER *) n)
502 register char *d = line;
507 while (*d && *d != '\t' && *d != ' ')
511 while (*d && (*d == '\t' || *d == ' '))
513 debug_print (2, ("group: %s, desc: %s\n", line, d));
514 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
515 str_cmp (d, data->desc)) {
516 p_delete(&data->desc);
517 data->desc = m_strdup(d);
523 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
527 if (!option (OPTLOADDESC) || !data || !data->nserv)
530 /* Get newsgroup description, if we can */
531 if (data->nserv->hasXGTITLE)
532 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
534 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
535 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
538 nntp_error ("nntp_get_desc()", buf);
544 * XOVER returns a tab separated list of:
545 * id|subject|from|date|Msgid|references|bytes|lines|xref
547 * This has to duplicate some of the functionality of
548 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
549 * a limited number of headers which are "parsed" by placement in the list)
551 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
553 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
557 hdr->env = mutt_new_envelope ();
558 hdr->env->newsgroups = m_strdup(nntp_data->group);
559 hdr->content = mutt_new_body ();
560 hdr->content->type = TYPETEXT;
561 hdr->content->subtype = m_strdup("plain");
562 hdr->content->encoding = ENC7BIT;
563 hdr->content->disposition = DISPINLINE;
564 hdr->content->length = -1;
567 for (x = 0; !done && x < 9; x++) {
568 /* if from file, need to skip newline character */
569 while (*p && *p != '\n' && *p != '\t')
578 hdr->article_num = atoi (b);
579 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
582 hdr->env->subject = m_strdup(b);
585 rfc822_free_address (&hdr->env->from);
586 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
587 /* same as for mutt_parse_rfc822_line():
588 * don't leave from info NULL if there's an invalid address (or
589 * whatever) in From: field; mutt would just display it as empty
590 * and mark mail/(esp.) news article as your own. aaargh! this
591 * bothered me for _years_ */
592 if (!hdr->env->from) {
593 hdr->env->from = rfc822_new_address ();
594 hdr->env->from->personal = m_strdup(b);
598 hdr->date_sent = mutt_parse_date (b, hdr);
599 hdr->received = hdr->date_sent;
602 p_delete(&hdr->env->message_id);
603 hdr->env->message_id = m_strdup(b);
606 mutt_free_list (&hdr->env->references);
607 hdr->env->references = mutt_parse_references (b, 0);
610 hdr->content->length = atoi (b);
613 hdr->lines = atoi (b);
617 p_delete(&hdr->env->xref);
618 b = b + 6; /* skips the "Xref: " */
619 hdr->env->xref = m_strdup(b);
620 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
622 rfc2047_decode_envelope (hdr->env);
635 unsigned short *messages;
639 #define fc ((FETCH_CONTEXT *) c)
641 #if WANT_LISTGROUP_COMMAND
642 static int _nntp_fetch_numbers (unsigned int num, void *c)
644 if (num < fc->base || num > fc->last)
646 fc->messages[num - fc->base] = 1;
649 static int nntp_fetch_numbers (char *line, void *c)
653 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
657 static int add_xover_line (char *line, void *c)
659 unsigned int num, total;
660 CONTEXT *ctx = fc->ctx;
661 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
666 if (ctx->msgcount >= ctx->hdrmax)
667 mx_alloc_memory (ctx);
668 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
669 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
671 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
672 num = ctx->hdrs[ctx->msgcount]->article_num;
674 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
676 if (num > data->lastLoaded)
677 data->lastLoaded = num;
678 num = num - fc->first + 1;
679 total = fc->last - fc->first + 1;
680 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
681 mutt_message ("%s %d/%d", fc->msg, num, total);
684 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
691 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
694 char buf[HUGE_STRING];
695 char *msg = _("Fetching message headers...");
696 char *msg2 = _("Fetching headers from cache...");
697 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
701 unsigned int current;
705 /* if empty group or nothing to do */
706 if (!last || first > last)
709 /* fetch list of articles */
710 mutt_message _("Fetching list of articles...");
715 fc.messages = p_new(unsigned short, last - first + 1);
716 #if WANT_LISTGROUP_COMMAND
717 if (nntp_data->nserv->hasLISTGROUP) {
718 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
719 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
721 mutt_error (_("LISTGROUP command failed: %s"), buf);
724 nntp_error ("nntp_fetch_headers()", buf);
726 p_delete(&fc.messages);
732 for (num = 0; num < last - first + 1; num++)
733 fc.messages[num] = 1;
734 #if WANT_LISTGROUP_COMMAND
738 /* CACHE: must be loaded xover cache here */
739 num = nntp_data->lastCached - first + 1;
740 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
741 nntp_cache_expand (buf, nntp_data->cache);
744 if ((f = safe_fopen (buf, "r"))) {
747 /* counting number of lines */
748 while (fgets (buf, sizeof (buf), f) != NULL)
751 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
753 oldmsgcount = ctx->msgcount;
755 fc.last = first + num - 1;
757 while (fgets (buf, sizeof (buf), f) != NULL) {
758 if (ReadInc && ((++c) % ReadInc == 0))
759 mutt_message ("%s %d/%d", msg2, c, r);
760 add_xover_line (buf, &fc);
763 nntp_data->lastLoaded = fc.last;
765 if (ctx->msgcount > oldmsgcount)
766 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
769 nntp_delete_cache (nntp_data);
771 num = last - first + 1;
773 p_delete(&fc.messages);
778 * Without XOVER, we have to fetch each article header and parse
779 * it. With XOVER, we ask for all of them
782 if (nntp_data->nserv->hasXOVER) {
783 oldmsgcount = ctx->msgcount;
787 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
788 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
789 if (ctx->msgcount > oldmsgcount)
790 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
792 mutt_error (_("XOVER command failed: %s"), buf);
794 nntp_error ("nntp_fetch_headers()", buf);
796 p_delete(&fc.messages);
802 for (current = first; current <= last; current++) {
805 ret = current - first + 1;
806 mutt_message ("%s %d/%d", msg, ret, num);
808 if (!fc.messages[current - fc.base])
811 if (ctx->msgcount >= ctx->hdrmax)
812 mx_alloc_memory (ctx);
813 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
814 h->index = ctx->msgcount;
816 ret = nntp_read_header (ctx, NULL, current);
817 if (ret == 0) { /* Got article. Fetch next header */
818 nntp_get_status (ctx, h, NULL, h->article_num);
820 mx_update_context (ctx, 1);
823 mutt_free_header (&h); /* skip it */
825 p_delete(&fc.messages);
829 if (current > nntp_data->lastLoaded)
830 nntp_data->lastLoaded = current;
832 p_delete(&fc.messages);
833 nntp_data->lastLoaded = last;
839 * currently, nntp "mailbox" is "newsgroup"
841 int nntp_open_mailbox (CONTEXT * ctx)
843 NNTP_DATA *nntp_data;
845 char buf[HUGE_STRING];
846 char server[LONG_STRING];
851 memset (&acct, 0, sizeof (ACCOUNT));
853 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
854 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
860 nntp_expand_path (server, sizeof (server), &acct);
861 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
864 CurrentNewsSrv = serv;
866 /* create NNTP-specific state struct if nof found in list */
867 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
868 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
869 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
870 strcpy (nntp_data->group, buf);
871 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
872 nntp_add_to_list (serv, nntp_data);
874 ctx->data = nntp_data;
875 nntp_data->nserv = serv;
877 mutt_message (_("Selecting %s..."), nntp_data->group);
879 if (!nntp_data->desc) {
880 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
882 nntp_save_cache_index (serv);
886 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
888 nntp_error ("nntp_open_mailbox()", buf);
893 if (str_ncmp ("211", buf, 3)) {
894 LIST *l = serv->list;
896 /* GROUP command failed */
897 if (!str_ncmp ("411", buf, 3)) {
898 mutt_error (_("Newsgroup %s not found on server %s"),
899 nntp_data->group, serv->conn->account.host);
901 /* CACHE: delete cache and line from .index */
902 nntp_delete_cache (nntp_data);
903 hash_delete (serv->newsgroups, nntp_data->group, NULL,
905 while (l && l->data != (void *) nntp_data)
916 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
917 &nntp_data->lastMessage, buf);
919 nntp_data->deleted = 0;
921 time (&serv->check_time);
924 * Check for max adding context. If it is greater than $nntp_context,
925 * strip off extra articles
927 first = nntp_data->firstMessage;
928 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
929 first = nntp_data->lastMessage - NntpContext + 1;
931 nntp_data->lastLoaded = first - 1;
932 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
935 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
937 char buf[LONG_STRING];
938 char path[_POSIX_PATH_MAX];
943 /* see if we already have the message in our cache */
945 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
948 /* if everything is fine, assign msg->fp and return */
949 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
950 (msg->fp = fopen (cache->path, "r")))
953 /* clear the previous entry */
954 unlink (cache->path);
955 p_delete(&cache->path);
957 cache->index = ctx->hdrs[msgno]->index;
959 cache->path = m_strdup(path);
960 if (!(msg->fp = safe_fopen (path, "w+"))) {
961 p_delete(&cache->path);
965 if (ctx->hdrs[msgno]->article_num == 0)
966 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
967 ctx->hdrs[msgno]->env->message_id);
969 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
970 ctx->hdrs[msgno]->article_num);
972 bar.msg = _("Fetching message...");
974 mutt_progress_bar (&bar, 0);
976 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
977 msg->fp, ctx->tagged);
979 mutt_error (_("Article %d not found on server"),
980 ctx->hdrs[msgno]->article_num);
981 debug_print (1, ("%s\n", buf));
987 p_delete(&cache->path);
991 mutt_free_envelope (&ctx->hdrs[msgno]->env);
992 ctx->hdrs[msgno]->env =
993 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
994 /* fix content length */
995 fseeko (msg->fp, 0, SEEK_END);
996 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
997 ctx->hdrs[msgno]->content->offset;
999 /* this is called in mutt before the open which fetches the message,
1000 * which is probably wrong, but we just call it again here to handle
1001 * the problem instead of fixing it.
1003 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1005 /* These would normally be updated in mx_update_context(), but the
1006 * full headers aren't parsed with XOVER, so the information wasn't
1009 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1011 mutt_clear_error ();
1018 int nntp_post (const char *msg)
1020 char buf[LONG_STRING];
1023 NNTP_DATA *nntp_data;
1025 if (Context && Context->magic == M_NNTP)
1026 nntp_data = (NNTP_DATA *) Context->data;
1028 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1029 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1030 mutt_error (_("Can't post article. No connection to news server."));
1033 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1036 if (!(f = safe_fopen (msg, "r"))) {
1037 mutt_error (_("Can't post article. Unable to open %s"), msg);
1041 strfcpy (buf, "POST\r\n", sizeof (buf));
1042 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1043 mutt_error (_("Can't post article. Connection to %s lost."),
1044 nntp_data->nserv->conn->account.host);
1047 if (buf[0] != '3') {
1048 mutt_error (_("Can't post article: %s"), buf);
1054 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1055 len = m_strlen(buf);
1056 if (buf[len - 1] == '\n') {
1057 buf[len - 1] = '\r';
1063 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1065 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1069 if (buf[m_strlen(buf) - 1] != '\n')
1070 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1071 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1072 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1073 mutt_error (_("Can't post article. Connection to %s lost."),
1074 nntp_data->nserv->conn->account.host);
1077 if (buf[0] != '2') {
1078 mutt_error (_("Can't post article: %s"), buf);
1085 /* nntp_logout_all: close all open connections. */
1086 void nntp_logout_all (void)
1088 char buf[LONG_STRING];
1091 conn = mutt_socket_head ();
1094 CONNECTION* next = conn->next;
1095 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1096 mutt_message (_("Closing connection to %s..."), conn->account.host);
1097 mutt_socket_write (conn, "QUIT\r\n");
1098 mutt_socket_readln (buf, sizeof (buf), conn);
1099 mutt_clear_error ();
1100 mutt_socket_close (conn);
1101 mutt_socket_free (conn);
1107 static void nntp_free_acache (NNTP_DATA * data)
1111 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1112 if (data->acache[i].path) {
1113 unlink (data->acache[i].path);
1114 p_delete(&data->acache[i].path);
1119 void nntp_delete_data (void *p)
1121 NNTP_DATA *data = (NNTP_DATA *)p;
1125 p_delete(&data->entries);
1126 p_delete(&data->desc);
1127 p_delete(&data->cache);
1128 nntp_free_acache (data);
1132 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1134 NNTP_DATA *data = ctx->data;
1136 /* CACHE: update cache and .index files */
1137 if ((option (OPTSAVEUNSUB) || data->subscribed))
1138 nntp_save_cache_group (ctx);
1139 nntp_free_acache (data);
1141 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1145 void nntp_fastclose_mailbox (CONTEXT * ctx)
1147 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1151 nntp_free_acache (data);
1152 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1154 nntp_save_cache_index (data->nserv);
1155 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1157 nntp_delete_data (data);
1159 nntp_sync_sidebar (data);
1162 /* commit changes and terminate connection */
1163 int nntp_close_mailbox (CONTEXT * ctx)
1167 mutt_message _("Quitting newsgroup...");
1170 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1173 if (data->nserv && data->nserv->conn && ctx->unread) {
1174 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1176 mutt_newsgroup_catchup (data->nserv, data->group);
1181 nntp_sync_mailbox (ctx, 0, NULL);
1182 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1185 news = ((NNTP_DATA *) ctx->data)->nserv;
1186 newsrc_gen_entries (ctx);
1187 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1188 mutt_newsrc_update (news);
1190 mutt_clear_error ();
1194 /* use the GROUP command to poll for new mail */
1195 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1197 char buf[LONG_STRING];
1200 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1204 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1206 nntp_error ("nntp_check_mailbox()", buf);
1210 if (str_ncmp ("211", buf, 3)) {
1212 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1214 nntp_error ("nntp_check_mailbox()", buf);
1219 if (!str_ncmp ("211", buf, 3)) {
1223 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1224 nntp_data->firstMessage = first;
1225 nntp_data->lastMessage = last;
1226 if (ctx && last > nntp_data->lastLoaded) {
1227 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1228 time (&nntp_data->nserv->check_time);
1231 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1232 nntp_data->unread = count;
1234 mutt_newsgroup_stat (nntp_data);
1235 /* active was renumbered? */
1236 if (last < nntp_data->lastLoaded) {
1237 if (!nntp_data->max) {
1238 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1241 nntp_data->lastCached = 0;
1243 nntp_data->entries[0].first = 1;
1244 nntp_data->entries[0].last = 0;
1246 nntp_sync_sidebar (nntp_data);
1249 time (&nntp_data->nserv->check_time);
1253 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1255 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1258 static int add_group (char *buf, void *serv)
1260 #define s ((NNTP_SERVER *) serv)
1261 char group[LONG_STRING], mod, desc[HUGE_STRING];
1263 NNTP_DATA *nntp_data;
1266 _checked = n; /* _checked have N, where N = number of groups */
1267 if (!buf) /* at EOF must be zerouth */
1274 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1277 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1279 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1280 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1281 strcpy (nntp_data->group, group);
1282 nntp_data->nserv = s;
1283 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1284 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1285 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1286 nntp_add_to_list (s, nntp_data);
1288 nntp_data->deleted = 0;
1289 nntp_data->firstMessage = first;
1290 nntp_data->lastMessage = last;
1292 nntp_data->allowed = 1;
1294 nntp_data->allowed = 0;
1295 if (nntp_data->desc)
1296 p_delete(&nntp_data->desc);
1298 nntp_data->desc = m_strdup(desc);
1299 if (nntp_data->rc || nntp_data->lastCached)
1300 mutt_newsgroup_stat (nntp_data);
1301 else if (nntp_data->lastMessage &&
1302 nntp_data->firstMessage <= nntp_data->lastMessage)
1303 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1305 nntp_data->unread = 0;
1311 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1313 char buf[LONG_STRING];
1314 NNTP_DATA nntp_data;
1320 if (!serv || !serv->newgroups_time)
1323 if (nntp_open_connection (serv) < 0)
1326 /* check subscribed groups for new news */
1327 if (option (OPTSHOWNEWNEWS)) {
1328 mutt_message _("Checking for new messages...");
1330 for (l = serv->list; l; l = l->next) {
1331 serv->check_time = 0; /* really check! */
1332 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1333 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1335 sidebar_draw (CurrentMenu);
1340 mutt_message _("Checking for new newsgroups...");
1342 now = serv->newgroups_time;
1343 time (&serv->newgroups_time);
1345 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1346 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1347 t->tm_min, t->tm_sec);
1348 nntp_data.nserv = serv;
1349 if (Context && Context->magic == M_NNTP)
1350 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1352 nntp_data.group = NULL;
1354 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1355 add_group, serv, 0) != 0) {
1357 nntp_error ("nntp_check_newgroups()", buf);
1362 mutt_message _("Loading descriptions...");
1367 emp.next = serv->list;
1371 ((NNTP_DATA *) l->data)->new = 1;
1372 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1376 nntp_save_cache_index (serv);
1377 mutt_clear_error ();
1381 /* Load list of all newsgroups from cache ALL */
1382 int nntp_get_cache_all (NNTP_SERVER * serv)
1384 char buf[HUGE_STRING];
1387 nntp_cache_expand (buf, serv->cache);
1388 if ((f = safe_fopen (buf, "r"))) {
1391 while (fgets (buf, sizeof (buf), f) != NULL) {
1392 if (ReadInc && (i % ReadInc == 0))
1393 mutt_message (_("Loading list from cache... %d"), i);
1394 add_group (buf, serv);
1397 add_group (NULL, NULL);
1399 mutt_clear_error ();
1403 p_delete(&serv->cache);
1408 /* Load list of all newsgroups from active */
1409 int nntp_get_active (NNTP_SERVER * serv)
1411 char msg[SHORT_STRING];
1412 NNTP_DATA nntp_data;
1415 if (nntp_open_connection (serv) < 0)
1418 snprintf (msg, sizeof (msg),
1419 _("Loading list of all newsgroups on server %s..."),
1420 serv->conn->account.host);
1422 time (&serv->newgroups_time);
1423 nntp_data.nserv = serv;
1424 nntp_data.group = NULL;
1426 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
1428 nntp_error ("nntp_get_active()", "LIST\r\n");
1433 strfcpy (msg, _("Loading descriptions..."), sizeof (msg));
1435 nntp_get_desc (&nntp_data, "*", msg, NULL);
1437 for (tmp = serv->list; tmp; tmp = tmp->next) {
1438 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1440 if (data && data->deleted && !data->rc) {
1441 nntp_delete_cache (data);
1442 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1446 nntp_save_cache_index (serv);
1448 mutt_clear_error ();
1453 * returns -1 if error ocurred while retrieving header,
1454 * number of articles which ones exist in context on success.
1456 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1460 /* if msgid is already in context, don't reload them */
1461 if (hash_find (ctx->id_hash, msgid))
1463 if (ctx->msgcount == ctx->hdrmax)
1464 mx_alloc_memory (ctx);
1465 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1466 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1468 mutt_message (_("Fetching %s from server..."), msgid);
1469 ret = nntp_read_header (ctx, msgid, 0);
1470 /* since nntp_read_header() may set read flag, we must reset it */
1471 ctx->hdrs[ctx->msgcount]->read = 0;
1473 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1476 mx_update_context (ctx, 1);
1486 unsigned int *child;
1489 static int check_children (char *s, void *c)
1491 #define cc ((CHILD_CONTEXT *) c)
1494 if (!s || (n = atoi (s)) == 0)
1496 for (i = 0; i < cc->ctx->msgcount; i++)
1497 if (cc->ctx->hdrs[i]->article_num == n)
1499 if (cc->num >= cc->max)
1500 p_realloc(&cc->child, cc->max += 25);
1501 cc->child[cc->num++] = n;
1507 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1509 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1511 int i, ret = 0, tmp = 0;
1514 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1515 !nntp_data->nserv->conn->account.host)
1517 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1519 if (!nntp_data->nserv->hasXPAT) {
1520 mutt_error (_("Server %s does not support this operation!"),
1521 nntp_data->nserv->conn->account.host);
1525 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1526 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1531 cc.child = p_new(unsigned int, 25);
1532 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1533 p_delete(&cc.child);
1536 /* dont try to read the xover cache. check_children() already
1537 * made sure that we dont have the article, so we need to visit
1538 * the server. Reading the cache at this point is also bad
1539 * because it would duplicate messages */
1540 if (option (OPTNEWSCACHE)) {
1542 unset_option (OPTNEWSCACHE);
1544 for (i = 0; i < cc.num; i++) {
1545 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1547 if (ctx->msgcount &&
1548 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1549 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1552 set_option (OPTNEWSCACHE);
1553 p_delete(&cc.child);