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"
34 #include "mutt_crypt.h"
36 #include "lib/debug.h"
43 #define WANT_LISTGROUP_COMMAND 0
45 static unsigned int _checked = 0;
47 void nntp_sync_sidebar (NNTP_DATA* data) {
52 if (list_empty (Incoming))
55 /* unfortunately, NNTP_DATA::group only is the plain
56 * group name, so for every single update, we need to
57 * compose the full string which must be defined via
58 * mailboxes command ;-((( FIXME
61 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
62 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
63 NONULL (data->nserv->conn->account.user),
64 *data->nserv->conn->account.pass ? ":" : "",
65 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
66 data->nserv->conn->account.host,
69 /* bail out if group not found via mailboxes */
70 if ((i = buffy_lookup (buf)) < 0)
73 tmp = (BUFFY*) Incoming->data[i];
74 /* copied from browser.c */
75 if (option (OPTMARKOLD) &&
76 data->lastCached >= data->firstMessage &&
77 data->lastCached <= data->lastMessage)
78 tmp->msg_unread = data->lastMessage - data->lastCached;
80 tmp->msg_unread = data->unread;
81 tmp->new = data->unread > 0;
82 /* this is closest to a "total" count we can get */
83 tmp->msgcount = data->lastMessage - data->firstMessage;
86 static void nntp_error (const char *where, const char *msg) {
87 debug_print (1, ("unexpected response in %s: %s\n", where, msg));
90 static int nntp_auth (NNTP_SERVER * serv)
92 CONNECTION *conn = serv->conn;
94 unsigned char flags = conn->account.flags;
96 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
97 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
98 conn->account.flags = flags;
102 mutt_message _("Logging in...");
104 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
105 mutt_socket_write (conn, buf);
106 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
107 conn->account.flags = flags;
112 /* don't print the password unless we're at the ungodly debugging level */
113 if (DebugLevel < M_SOCK_LOG_FULL)
114 debug_print (M_SOCK_LOG_CMD, ("> AUTHINFO PASS *\n"));
116 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
117 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
118 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
119 conn->account.flags = flags;
123 if (m_strncmp("281", buf, 3)) {
124 conn->account.flags = flags;
125 mutt_error _("Login failed.");
134 static int nntp_connect_error (NNTP_SERVER * serv)
136 serv->status = NNTP_NONE;
137 mutt_socket_close (serv->conn);
138 mutt_error _("Server closed connection!");
144 static int nntp_connect_and_auth (NNTP_SERVER * serv)
146 CONNECTION *conn = serv->conn;
150 serv->status = NNTP_NONE;
152 if (mutt_socket_open (conn) < 0)
155 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
156 return nntp_connect_error (serv);
158 if (!m_strncmp("200", buf, 3))
159 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
160 else if (!m_strncmp("201", buf, 3))
161 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
163 mutt_socket_close (conn);
164 str_skip_trailws (buf);
165 mutt_error ("%s", buf);
172 /* Tell INN to switch to mode reader if it isn't so. Ignore all
173 returned codes and messages. */
174 mutt_socket_write (conn, "MODE READER\r\n");
175 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
176 return nntp_connect_error (serv);
178 mutt_socket_write (conn, "STAT\r\n");
179 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
180 return nntp_connect_error (serv);
182 if (!(conn->account.flags & M_ACCT_USER) && m_strncmp("480", buf, 3)) {
183 serv->status = NNTP_OK;
187 rc = nntp_auth (serv);
189 return nntp_connect_error (serv);
191 mutt_socket_close (conn);
192 serv->status = NNTP_BYE;
196 mutt_socket_close (conn);
197 mutt_error _("Login failed.");
202 serv->status = NNTP_OK;
206 static int nntp_attempt_features (NNTP_SERVER * serv)
208 char buf[LONG_STRING];
209 CONNECTION *conn = serv->conn;
211 mutt_socket_write (conn, "XOVER\r\n");
212 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
213 return nntp_connect_error (serv);
214 if (m_strncmp("500", buf, 3))
217 mutt_socket_write (conn, "XPAT\r\n");
218 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
219 return nntp_connect_error (serv);
220 if (m_strncmp("500", buf, 3))
223 #if WANT_LISTGROUP_COMMAND
224 mutt_socket_write (conn, "LISTGROUP\r\n");
225 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
226 return (nntp_connect_error (serv));
227 if (m_strncmp("500", buf, 3))
228 serv->hasLISTGROUP = 1;
231 mutt_socket_write (conn, "XGTITLE +\r\n");
232 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
233 return nntp_connect_error (serv);
234 if (m_strncmp("500", buf, 3))
235 serv->hasXGTITLE = 1;
237 if (!m_strncmp("282", buf, 3)) {
239 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
240 return nntp_connect_error (serv);
241 } while (!(buf[0] == '.' && buf[1] == '\0'));
247 static int nntp_open_connection (NNTP_SERVER * serv)
249 if (serv->status == NNTP_OK)
251 if (serv->status == NNTP_BYE)
253 if (nntp_connect_and_auth (serv) < 0)
255 if (nntp_attempt_features (serv) < 0)
260 static int nntp_reconnect (NNTP_SERVER * serv)
262 char buf[SHORT_STRING];
264 mutt_socket_close (serv->conn);
267 if (nntp_connect_and_auth (serv) == 0)
270 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
271 serv->conn->account.host);
272 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
273 serv->status = NNTP_BYE;
279 /* Send data from line[LONG_STRING] and receive answer to same line */
280 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
282 char buf[LONG_STRING];
285 if (data->nserv->status == NNTP_BYE)
290 mutt_socket_write (data->nserv->conn, line);
292 else if (data->group) {
293 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
294 mutt_socket_write (data->nserv->conn, buf);
298 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
299 if (nntp_reconnect (data->nserv) < 0)
303 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
304 mutt_socket_write (data->nserv->conn, buf);
305 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
311 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
315 m_strcpy(line, linelen, buf);
320 * This function calls funct(*line, *data) for each received line,
321 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
324 * 1 - correct but not performed (may be, have to be continued),
325 * -1 - conection lost,
326 * -2 - invalid command or execution error,
327 * -3 - error in funct(*line, *data).
329 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg,
330 progress_t* bar, int (*funct) (char *, void *),
331 void *data, int tagged)
333 char buf[LONG_STRING];
342 m_strcpy(buf, sizeof(buf), query);
343 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
352 inbuf = p_new(char, sizeof(buf));
355 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
361 if (!lenbuf && buf[0] == '.') {
362 if (buf[1] == '\0') {
370 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
373 if (chunk >= sizeof (buf)) {
374 lenbuf += m_strlen(p);
378 mutt_progress_bar (bar, pos);
381 if (ReadInc && (line % ReadInc == 0)) {
383 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
385 mutt_message ("%s %d", msg, line);
389 if (ret == 0 && funct (inbuf, data) < 0)
394 p_realloc(&inbuf, lenbuf + sizeof (buf));
403 static int nntp_read_tempfile (char *line, void *file)
405 FILE *f = (FILE *) file;
411 if (fputc ('\n', f) == EOF)
417 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
420 register char *p, *b;
421 register char *colon = NULL;
425 /* skip to next word */
427 while (*b && ((*b == ' ') || (*b == '\t')))
431 /* skip to end of word */
432 while (*p && (*p != ' ') && (*p != '\t')) {
444 nntp_get_status (ctx, h, b, atoi (colon));
445 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
446 h->article_num = atoi (colon);
454 * 1 if article not found
455 * -1 if read or write error on tempfile or socket
457 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
460 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
462 char buf[LONG_STRING];
463 char tempfile[_POSIX_PATH_MAX];
465 HEADER *h = ctx->hdrs[ctx->msgcount];
467 mutt_mktemp (tempfile);
468 if (!(f = safe_fopen (tempfile, "w+")))
472 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
474 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
476 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
479 debug_print (1, ("%s\n", buf));
482 return (ret == -1 ? -1 : 1);
485 h->article_num = article_num;
486 h->env = mutt_read_rfc822_header (f, h, 0, 0);
490 if (h->env->xref != NULL)
491 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
492 else if (h->article_num == 0 && msgid) {
493 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
494 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
495 h->article_num = atoi (buf + 4);
501 static int parse_description (char *line, void *n)
503 #define news ((NNTP_SERVER *) n)
504 register char *d = line;
509 while (*d && *d != '\t' && *d != ' ')
513 while (*d && (*d == '\t' || *d == ' '))
515 debug_print (2, ("group: %s, desc: %s\n", line, d));
516 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
517 m_strcmp(d, data->desc)) {
518 p_delete(&data->desc);
519 data->desc = m_strdup(d);
525 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
529 if (!option (OPTLOADDESC) || !data || !data->nserv)
532 /* Get newsgroup description, if we can */
533 if (data->nserv->hasXGTITLE)
534 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
536 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
537 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
540 nntp_error ("nntp_get_desc()", buf);
546 * XOVER returns a tab separated list of:
547 * id|subject|from|date|Msgid|references|bytes|lines|xref
549 * This has to duplicate some of the functionality of
550 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
551 * a limited number of headers which are "parsed" by placement in the list)
553 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
555 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
559 hdr->env = mutt_new_envelope ();
560 hdr->env->newsgroups = m_strdup(nntp_data->group);
561 hdr->content = mutt_new_body ();
562 hdr->content->type = TYPETEXT;
563 hdr->content->subtype = m_strdup("plain");
564 hdr->content->encoding = ENC7BIT;
565 hdr->content->disposition = DISPINLINE;
566 hdr->content->length = -1;
569 for (x = 0; !done && x < 9; x++) {
570 /* if from file, need to skip newline character */
571 while (*p && *p != '\n' && *p != '\t')
580 hdr->article_num = atoi (b);
581 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
584 hdr->env->subject = m_strdup(b);
587 rfc822_free_address (&hdr->env->from);
588 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
589 /* same as for mutt_parse_rfc822_line():
590 * don't leave from info NULL if there's an invalid address (or
591 * whatever) in From: field; mutt would just display it as empty
592 * and mark mail/(esp.) news article as your own. aaargh! this
593 * bothered me for _years_ */
594 if (!hdr->env->from) {
595 hdr->env->from = rfc822_new_address ();
596 hdr->env->from->personal = m_strdup(b);
600 hdr->date_sent = mutt_parse_date (b, hdr);
601 hdr->received = hdr->date_sent;
604 p_delete(&hdr->env->message_id);
605 hdr->env->message_id = m_strdup(b);
608 mutt_free_list (&hdr->env->references);
609 hdr->env->references = mutt_parse_references (b, 0);
612 hdr->content->length = atoi (b);
615 hdr->lines = atoi (b);
619 p_delete(&hdr->env->xref);
620 b = b + 6; /* skips the "Xref: " */
621 hdr->env->xref = m_strdup(b);
622 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
624 rfc2047_decode_envelope (hdr->env);
637 unsigned short *messages;
641 #define fc ((FETCH_CONTEXT *) c)
643 #if WANT_LISTGROUP_COMMAND
644 static int _nntp_fetch_numbers (unsigned int num, void *c)
646 if (num < fc->base || num > fc->last)
648 fc->messages[num - fc->base] = 1;
651 static int nntp_fetch_numbers (char *line, void *c)
655 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
659 static int add_xover_line (char *line, void *c)
661 unsigned int num, total;
662 CONTEXT *ctx = fc->ctx;
663 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
668 if (ctx->msgcount >= ctx->hdrmax)
669 mx_alloc_memory (ctx);
670 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
671 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
673 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
674 num = ctx->hdrs[ctx->msgcount]->article_num;
676 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
678 if (num > data->lastLoaded)
679 data->lastLoaded = num;
680 num = num - fc->first + 1;
681 total = fc->last - fc->first + 1;
682 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
683 mutt_message ("%s %d/%d", fc->msg, num, total);
686 mutt_free_header (&ctx->hdrs[ctx->msgcount]); /* skip it */
693 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
696 char buf[HUGE_STRING];
697 char *msg = _("Fetching message headers...");
698 char *msg2 = _("Fetching headers from cache...");
699 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
703 unsigned int current;
707 /* if empty group or nothing to do */
708 if (!last || first > last)
711 /* fetch list of articles */
712 mutt_message _("Fetching list of articles...");
717 fc.messages = p_new(unsigned short, last - first + 1);
718 #if WANT_LISTGROUP_COMMAND
719 if (nntp_data->nserv->hasLISTGROUP) {
720 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
721 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
723 mutt_error (_("LISTGROUP command failed: %s"), buf);
726 nntp_error ("nntp_fetch_headers()", buf);
728 p_delete(&fc.messages);
734 for (num = 0; num < last - first + 1; num++)
735 fc.messages[num] = 1;
736 #if WANT_LISTGROUP_COMMAND
740 /* CACHE: must be loaded xover cache here */
741 num = nntp_data->lastCached - first + 1;
742 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
743 nntp_cache_expand (buf, nntp_data->cache);
746 if ((f = safe_fopen (buf, "r"))) {
749 /* counting number of lines */
750 while (fgets (buf, sizeof (buf), f) != NULL)
753 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
755 oldmsgcount = ctx->msgcount;
757 fc.last = first + num - 1;
759 while (fgets (buf, sizeof (buf), f) != NULL) {
760 if (ReadInc && ((++c) % ReadInc == 0))
761 mutt_message ("%s %d/%d", msg2, c, r);
762 add_xover_line (buf, &fc);
765 nntp_data->lastLoaded = fc.last;
767 if (ctx->msgcount > oldmsgcount)
768 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
771 nntp_delete_cache (nntp_data);
773 num = last - first + 1;
775 p_delete(&fc.messages);
780 * Without XOVER, we have to fetch each article header and parse
781 * it. With XOVER, we ask for all of them
784 if (nntp_data->nserv->hasXOVER) {
785 oldmsgcount = ctx->msgcount;
789 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
790 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
791 if (ctx->msgcount > oldmsgcount)
792 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
794 mutt_error (_("XOVER command failed: %s"), buf);
796 nntp_error ("nntp_fetch_headers()", buf);
798 p_delete(&fc.messages);
804 for (current = first; current <= last; current++) {
807 ret = current - first + 1;
808 mutt_message ("%s %d/%d", msg, ret, num);
810 if (!fc.messages[current - fc.base])
813 if (ctx->msgcount >= ctx->hdrmax)
814 mx_alloc_memory (ctx);
815 h = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
816 h->index = ctx->msgcount;
818 ret = nntp_read_header (ctx, NULL, current);
819 if (ret == 0) { /* Got article. Fetch next header */
820 nntp_get_status (ctx, h, NULL, h->article_num);
822 mx_update_context (ctx, 1);
825 mutt_free_header (&h); /* skip it */
827 p_delete(&fc.messages);
831 if (current > nntp_data->lastLoaded)
832 nntp_data->lastLoaded = current;
834 p_delete(&fc.messages);
835 nntp_data->lastLoaded = last;
841 * currently, nntp "mailbox" is "newsgroup"
843 int nntp_open_mailbox (CONTEXT * ctx)
845 NNTP_DATA *nntp_data;
847 char buf[HUGE_STRING];
848 char server[LONG_STRING];
855 if (nntp_parse_url (ctx->path, &acct, buf, sizeof (buf)) < 0 || !*buf) {
856 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
862 nntp_expand_path (server, sizeof (server), &acct);
863 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
866 CurrentNewsSrv = serv;
868 /* create NNTP-specific state struct if nof found in list */
869 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
870 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
871 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
872 strcpy (nntp_data->group, buf);
873 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
874 nntp_add_to_list (serv, nntp_data);
876 ctx->data = nntp_data;
877 nntp_data->nserv = serv;
879 mutt_message (_("Selecting %s..."), nntp_data->group);
881 if (!nntp_data->desc) {
882 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
884 nntp_save_cache_index (serv);
888 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
890 nntp_error ("nntp_open_mailbox()", buf);
895 if (m_strncmp("211", buf, 3)) {
896 LIST *l = serv->list;
898 /* GROUP command failed */
899 if (!m_strncmp("411", buf, 3)) {
900 mutt_error (_("Newsgroup %s not found on server %s"),
901 nntp_data->group, serv->conn->account.host);
903 /* CACHE: delete cache and line from .index */
904 nntp_delete_cache (nntp_data);
905 hash_delete (serv->newsgroups, nntp_data->group, NULL,
907 while (l && l->data != (void *) nntp_data)
918 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
919 &nntp_data->lastMessage, buf);
921 nntp_data->deleted = 0;
923 time (&serv->check_time);
926 * Check for max adding context. If it is greater than $nntp_context,
927 * strip off extra articles
929 first = nntp_data->firstMessage;
930 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
931 first = nntp_data->lastMessage - NntpContext + 1;
933 nntp_data->lastLoaded = first - 1;
934 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
937 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
939 char buf[LONG_STRING];
940 char path[_POSIX_PATH_MAX];
945 /* see if we already have the message in our cache */
947 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
950 /* if everything is fine, assign msg->fp and return */
951 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
952 (msg->fp = fopen (cache->path, "r")))
955 /* clear the previous entry */
956 unlink (cache->path);
957 p_delete(&cache->path);
959 cache->index = ctx->hdrs[msgno]->index;
961 cache->path = m_strdup(path);
962 if (!(msg->fp = safe_fopen (path, "w+"))) {
963 p_delete(&cache->path);
967 if (ctx->hdrs[msgno]->article_num == 0)
968 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
969 ctx->hdrs[msgno]->env->message_id);
971 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
972 ctx->hdrs[msgno]->article_num);
974 bar.msg = _("Fetching message...");
976 mutt_progress_bar (&bar, 0);
978 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
979 msg->fp, ctx->tagged);
981 mutt_error (_("Article %d not found on server"),
982 ctx->hdrs[msgno]->article_num);
983 debug_print (1, ("%s\n", buf));
989 p_delete(&cache->path);
993 mutt_free_envelope (&ctx->hdrs[msgno]->env);
994 ctx->hdrs[msgno]->env =
995 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
996 /* fix content length */
997 fseeko (msg->fp, 0, SEEK_END);
998 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
999 ctx->hdrs[msgno]->content->offset;
1001 /* this is called in mutt before the open which fetches the message,
1002 * which is probably wrong, but we just call it again here to handle
1003 * the problem instead of fixing it.
1005 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
1007 /* These would normally be updated in mx_update_context(), but the
1008 * full headers aren't parsed with XOVER, so the information wasn't
1011 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
1013 mutt_clear_error ();
1020 int nntp_post (const char *msg)
1022 char buf[LONG_STRING];
1025 NNTP_DATA *nntp_data;
1027 if (Context && Context->magic == M_NNTP)
1028 nntp_data = (NNTP_DATA *) Context->data;
1030 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1031 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1032 mutt_error (_("Can't post article. No connection to news server."));
1035 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1038 if (!(f = safe_fopen (msg, "r"))) {
1039 mutt_error (_("Can't post article. Unable to open %s"), msg);
1043 m_strcpy(buf, sizeof(buf), "POST\r\n");
1044 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1045 mutt_error (_("Can't post article. Connection to %s lost."),
1046 nntp_data->nserv->conn->account.host);
1049 if (buf[0] != '3') {
1050 mutt_error (_("Can't post article: %s"), buf);
1056 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1057 len = m_strlen(buf);
1058 if (buf[len - 1] == '\n') {
1059 buf[len - 1] = '\r';
1065 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1067 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1071 if (buf[m_strlen(buf) - 1] != '\n')
1072 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1073 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1074 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1075 mutt_error (_("Can't post article. Connection to %s lost."),
1076 nntp_data->nserv->conn->account.host);
1079 if (buf[0] != '2') {
1080 mutt_error (_("Can't post article: %s"), buf);
1087 /* nntp_logout_all: close all open connections. */
1088 void nntp_logout_all (void)
1090 char buf[LONG_STRING];
1093 conn = mutt_socket_head ();
1096 CONNECTION* next = conn->next;
1097 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1098 mutt_message (_("Closing connection to %s..."), conn->account.host);
1099 mutt_socket_write (conn, "QUIT\r\n");
1100 mutt_socket_readln (buf, sizeof (buf), conn);
1101 mutt_clear_error ();
1102 mutt_socket_close (conn);
1103 mutt_socket_free (conn);
1109 static void nntp_free_acache (NNTP_DATA * data)
1113 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1114 if (data->acache[i].path) {
1115 unlink (data->acache[i].path);
1116 p_delete(&data->acache[i].path);
1121 void nntp_delete_data (void *p)
1123 NNTP_DATA *data = (NNTP_DATA *)p;
1127 p_delete(&data->entries);
1128 p_delete(&data->desc);
1129 p_delete(&data->cache);
1130 nntp_free_acache (data);
1134 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1136 NNTP_DATA *data = ctx->data;
1138 /* CACHE: update cache and .index files */
1139 if ((option (OPTSAVEUNSUB) || data->subscribed))
1140 nntp_save_cache_group (ctx);
1141 nntp_free_acache (data);
1143 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1147 void nntp_fastclose_mailbox (CONTEXT * ctx)
1149 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1153 nntp_free_acache (data);
1154 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1156 nntp_save_cache_index (data->nserv);
1157 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1159 nntp_delete_data (data);
1161 nntp_sync_sidebar (data);
1164 /* commit changes and terminate connection */
1165 int nntp_close_mailbox (CONTEXT * ctx)
1169 mutt_message _("Quitting newsgroup...");
1172 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1175 if (data->nserv && data->nserv->conn && ctx->unread) {
1176 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1178 mutt_newsgroup_catchup (data->nserv, data->group);
1183 nntp_sync_mailbox (ctx, 0, NULL);
1184 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1187 news = ((NNTP_DATA *) ctx->data)->nserv;
1188 newsrc_gen_entries (ctx);
1189 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1190 mutt_newsrc_update (news);
1192 mutt_clear_error ();
1196 /* use the GROUP command to poll for new mail */
1197 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1199 char buf[LONG_STRING];
1202 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1206 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1208 nntp_error ("nntp_check_mailbox()", buf);
1212 if (m_strncmp("211", buf, 3)) {
1214 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1216 nntp_error ("nntp_check_mailbox()", buf);
1221 if (!m_strncmp("211", buf, 3)) {
1225 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1226 nntp_data->firstMessage = first;
1227 nntp_data->lastMessage = last;
1228 if (ctx && last > nntp_data->lastLoaded) {
1229 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1230 time (&nntp_data->nserv->check_time);
1233 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1234 nntp_data->unread = count;
1236 mutt_newsgroup_stat (nntp_data);
1237 /* active was renumbered? */
1238 if (last < nntp_data->lastLoaded) {
1239 if (!nntp_data->max) {
1240 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1243 nntp_data->lastCached = 0;
1245 nntp_data->entries[0].first = 1;
1246 nntp_data->entries[0].last = 0;
1248 nntp_sync_sidebar (nntp_data);
1251 time (&nntp_data->nserv->check_time);
1255 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1257 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1260 static int add_group (char *buf, void *serv)
1262 #define s ((NNTP_SERVER *) serv)
1263 char group[LONG_STRING], mod, desc[HUGE_STRING];
1265 NNTP_DATA *nntp_data;
1268 _checked = n; /* _checked have N, where N = number of groups */
1269 if (!buf) /* at EOF must be zerouth */
1276 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1279 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1281 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1282 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1283 strcpy (nntp_data->group, group);
1284 nntp_data->nserv = s;
1285 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1286 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1287 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1288 nntp_add_to_list (s, nntp_data);
1290 nntp_data->deleted = 0;
1291 nntp_data->firstMessage = first;
1292 nntp_data->lastMessage = last;
1294 nntp_data->allowed = 1;
1296 nntp_data->allowed = 0;
1297 if (nntp_data->desc)
1298 p_delete(&nntp_data->desc);
1300 nntp_data->desc = m_strdup(desc);
1301 if (nntp_data->rc || nntp_data->lastCached)
1302 mutt_newsgroup_stat (nntp_data);
1303 else if (nntp_data->lastMessage &&
1304 nntp_data->firstMessage <= nntp_data->lastMessage)
1305 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1307 nntp_data->unread = 0;
1313 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1315 char buf[LONG_STRING];
1316 NNTP_DATA nntp_data;
1322 if (!serv || !serv->newgroups_time)
1325 if (nntp_open_connection (serv) < 0)
1328 /* check subscribed groups for new news */
1329 if (option (OPTSHOWNEWNEWS)) {
1330 mutt_message _("Checking for new messages...");
1332 for (l = serv->list; l; l = l->next) {
1333 serv->check_time = 0; /* really check! */
1334 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1335 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1337 sidebar_draw (CurrentMenu);
1342 mutt_message _("Checking for new newsgroups...");
1344 now = serv->newgroups_time;
1345 time (&serv->newgroups_time);
1347 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1348 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1349 t->tm_min, t->tm_sec);
1350 nntp_data.nserv = serv;
1351 if (Context && Context->magic == M_NNTP)
1352 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1354 nntp_data.group = NULL;
1356 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1357 add_group, serv, 0) != 0) {
1359 nntp_error ("nntp_check_newgroups()", buf);
1364 mutt_message _("Loading descriptions...");
1369 emp.next = serv->list;
1373 ((NNTP_DATA *) l->data)->new = 1;
1374 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1378 nntp_save_cache_index (serv);
1379 mutt_clear_error ();
1383 /* Load list of all newsgroups from cache ALL */
1384 int nntp_get_cache_all (NNTP_SERVER * serv)
1386 char buf[HUGE_STRING];
1389 nntp_cache_expand (buf, serv->cache);
1390 if ((f = safe_fopen (buf, "r"))) {
1393 while (fgets (buf, sizeof (buf), f) != NULL) {
1394 if (ReadInc && (i % ReadInc == 0))
1395 mutt_message (_("Loading list from cache... %d"), i);
1396 add_group (buf, serv);
1399 add_group (NULL, NULL);
1401 mutt_clear_error ();
1405 p_delete(&serv->cache);
1410 /* Load list of all newsgroups from active */
1411 int nntp_get_active (NNTP_SERVER * serv)
1413 char msg[SHORT_STRING];
1414 NNTP_DATA nntp_data;
1417 if (nntp_open_connection (serv) < 0)
1420 snprintf (msg, sizeof (msg),
1421 _("Loading list of all newsgroups on server %s..."),
1422 serv->conn->account.host);
1424 time (&serv->newgroups_time);
1425 nntp_data.nserv = serv;
1426 nntp_data.group = NULL;
1428 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
1430 nntp_error ("nntp_get_active()", "LIST\r\n");
1435 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1437 nntp_get_desc (&nntp_data, "*", msg, NULL);
1439 for (tmp = serv->list; tmp; tmp = tmp->next) {
1440 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1442 if (data && data->deleted && !data->rc) {
1443 nntp_delete_cache (data);
1444 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1448 nntp_save_cache_index (serv);
1450 mutt_clear_error ();
1455 * returns -1 if error ocurred while retrieving header,
1456 * number of articles which ones exist in context on success.
1458 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1462 /* if msgid is already in context, don't reload them */
1463 if (hash_find (ctx->id_hash, msgid))
1465 if (ctx->msgcount == ctx->hdrmax)
1466 mx_alloc_memory (ctx);
1467 ctx->hdrs[ctx->msgcount] = mutt_new_header ();
1468 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1470 mutt_message (_("Fetching %s from server..."), msgid);
1471 ret = nntp_read_header (ctx, msgid, 0);
1472 /* since nntp_read_header() may set read flag, we must reset it */
1473 ctx->hdrs[ctx->msgcount]->read = 0;
1475 mutt_free_header (&ctx->hdrs[ctx->msgcount]);
1478 mx_update_context (ctx, 1);
1488 unsigned int *child;
1491 static int check_children (char *s, void *c)
1493 #define cc ((CHILD_CONTEXT *) c)
1496 if (!s || (n = atoi (s)) == 0)
1498 for (i = 0; i < cc->ctx->msgcount; i++)
1499 if (cc->ctx->hdrs[i]->article_num == n)
1501 if (cc->num >= cc->max)
1502 p_realloc(&cc->child, cc->max += 25);
1503 cc->child[cc->num++] = n;
1509 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1511 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1513 int i, ret = 0, tmp = 0;
1516 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1517 !nntp_data->nserv->conn->account.host)
1519 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1521 if (!nntp_data->nserv->hasXPAT) {
1522 mutt_error (_("Server %s does not support this operation!"),
1523 nntp_data->nserv->conn->account.host);
1527 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1528 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1533 cc.child = p_new(unsigned int, 25);
1534 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1535 p_delete(&cc.child);
1538 /* dont try to read the xover cache. check_children() already
1539 * made sure that we dont have the article, so we need to visit
1540 * the server. Reading the cache at this point is also bad
1541 * because it would duplicate messages */
1542 if (option (OPTNEWSCACHE)) {
1544 unset_option (OPTNEWSCACHE);
1546 for (i = 0; i < cc.num; i++) {
1547 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1549 if (ctx->msgcount &&
1550 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1551 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1554 set_option (OPTNEWSCACHE);
1555 p_delete(&cc.child);