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>
23 #include <lib-ui/curses.h>
33 #include <lib-crypt/crypt.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 int nntp_auth (NNTP_SERVER * serv)
86 CONNECTION *conn = serv->conn;
88 unsigned char flags = conn->account.flags;
90 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
91 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
92 conn->account.flags = flags;
96 mutt_message _("Logging in...");
98 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
99 mutt_socket_write (conn, buf);
100 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
101 conn->account.flags = flags;
105 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
106 mutt_socket_write_d (conn, buf, M_SOCK_LOG_FULL);
107 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
108 conn->account.flags = flags;
112 if (m_strncmp("281", buf, 3)) {
113 conn->account.flags = flags;
114 mutt_error _("Login failed.");
123 static int nntp_connect_error (NNTP_SERVER * serv)
125 serv->status = NNTP_NONE;
126 mutt_socket_close (serv->conn);
127 mutt_error _("Server closed connection!");
133 static int nntp_connect_and_auth (NNTP_SERVER * serv)
135 CONNECTION *conn = serv->conn;
139 serv->status = NNTP_NONE;
141 if (mutt_socket_open (conn) < 0)
144 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
145 return nntp_connect_error (serv);
147 if (!m_strncmp("200", buf, 3))
148 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
149 else if (!m_strncmp("201", buf, 3))
150 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
152 mutt_socket_close(conn);
154 mutt_error("%s", buf);
161 /* Tell INN to switch to mode reader if it isn't so. Ignore all
162 returned codes and messages. */
163 mutt_socket_write (conn, "MODE READER\r\n");
164 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
165 return nntp_connect_error (serv);
167 mutt_socket_write (conn, "STAT\r\n");
168 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
169 return nntp_connect_error (serv);
171 if (!(conn->account.flags & M_ACCT_USER) && m_strncmp("480", buf, 3)) {
172 serv->status = NNTP_OK;
176 rc = nntp_auth (serv);
178 return nntp_connect_error (serv);
180 mutt_socket_close (conn);
181 serv->status = NNTP_BYE;
185 mutt_socket_close (conn);
186 mutt_error _("Login failed.");
191 serv->status = NNTP_OK;
195 static int nntp_attempt_features (NNTP_SERVER * serv)
197 char buf[LONG_STRING];
198 CONNECTION *conn = serv->conn;
200 mutt_socket_write (conn, "XOVER\r\n");
201 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
202 return nntp_connect_error (serv);
203 if (m_strncmp("500", buf, 3))
206 mutt_socket_write (conn, "XPAT\r\n");
207 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
208 return nntp_connect_error (serv);
209 if (m_strncmp("500", buf, 3))
212 #if WANT_LISTGROUP_COMMAND
213 mutt_socket_write (conn, "LISTGROUP\r\n");
214 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
215 return (nntp_connect_error (serv));
216 if (m_strncmp("500", buf, 3))
217 serv->hasLISTGROUP = 1;
220 mutt_socket_write (conn, "XGTITLE +\r\n");
221 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
222 return nntp_connect_error (serv);
223 if (m_strncmp("500", buf, 3))
224 serv->hasXGTITLE = 1;
226 if (!m_strncmp("282", buf, 3)) {
228 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
229 return nntp_connect_error (serv);
230 } while (!(buf[0] == '.' && buf[1] == '\0'));
236 static int nntp_open_connection (NNTP_SERVER * serv)
238 if (serv->status == NNTP_OK)
240 if (serv->status == NNTP_BYE)
242 if (nntp_connect_and_auth (serv) < 0)
244 if (nntp_attempt_features (serv) < 0)
249 static int nntp_reconnect (NNTP_SERVER * serv)
251 char buf[SHORT_STRING];
253 mutt_socket_close (serv->conn);
256 if (nntp_connect_and_auth (serv) == 0)
259 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
260 serv->conn->account.host);
261 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
262 serv->status = NNTP_BYE;
268 /* Send data from line[LONG_STRING] and receive answer to same line */
269 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
271 char buf[LONG_STRING];
274 if (data->nserv->status == NNTP_BYE)
279 mutt_socket_write (data->nserv->conn, line);
281 else if (data->group) {
282 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
283 mutt_socket_write (data->nserv->conn, buf);
287 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
288 if (nntp_reconnect (data->nserv) < 0)
292 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
293 mutt_socket_write (data->nserv->conn, buf);
294 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
300 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
304 m_strcpy(line, linelen, buf);
309 * This function calls funct(*line, *data) for each received line,
310 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
313 * 1 - correct but not performed (may be, have to be continued),
314 * -1 - conection lost,
315 * -2 - invalid command or execution error,
316 * -3 - error in funct(*line, *data).
318 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg,
319 progress_t* bar, int (*funct) (char *, void *),
320 void *data, int tagged)
322 char buf[LONG_STRING];
331 m_strcpy(buf, sizeof(buf), query);
332 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
341 inbuf = p_new(char, sizeof(buf));
344 chunk = mutt_socket_readln_d (buf, sizeof (buf), nntp_data->nserv->conn,
350 if (!lenbuf && buf[0] == '.') {
351 if (buf[1] == '\0') {
359 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
362 if (chunk >= ssizeof (buf)) {
363 lenbuf += m_strlen(p);
367 mutt_progress_bar (bar, pos);
370 if (ReadInc && (line % ReadInc == 0)) {
372 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
374 mutt_message ("%s %d", msg, line);
378 if (ret == 0 && funct (inbuf, data) < 0)
383 p_realloc(&inbuf, lenbuf + sizeof (buf));
392 static int nntp_read_tempfile (char *line, void *file)
394 FILE *f = (FILE *) file;
400 if (fputc ('\n', f) == EOF)
406 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
409 register char *p, *b;
410 register char *colon = NULL;
414 /* skip to next word */
416 while (*b && ((*b == ' ') || (*b == '\t')))
420 /* skip to end of word */
421 while (*p && (*p != ' ') && (*p != '\t')) {
433 nntp_get_status (ctx, h, b, atoi (colon));
434 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
435 h->article_num = atoi (colon);
443 * 1 if article not found
444 * -1 if read or write error on tempfile or socket
446 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
449 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
451 char buf[LONG_STRING];
452 char tempfile[_POSIX_PATH_MAX];
454 HEADER *h = ctx->hdrs[ctx->msgcount];
456 mutt_mktemp (tempfile);
457 if (!(f = safe_fopen (tempfile, "w+")))
461 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
463 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
465 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
469 return (ret == -1 ? -1 : 1);
472 h->article_num = article_num;
473 h->env = mutt_read_rfc822_header (f, h, 0, 0);
477 if (h->env->xref != NULL)
478 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
479 else if (h->article_num == 0 && msgid) {
480 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
481 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
482 h->article_num = atoi (buf + 4);
488 static int parse_description (char *line, void *n)
490 #define news ((NNTP_SERVER *) n)
491 register char *d = line;
496 while (*d && *d != '\t' && *d != ' ')
500 while (*d && (*d == '\t' || *d == ' '))
502 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
503 m_strcmp(d, data->desc)) {
504 p_delete(&data->desc);
505 data->desc = m_strdup(d);
511 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
515 if (!option (OPTLOADDESC) || !data || !data->nserv)
518 /* Get newsgroup description, if we can */
519 if (data->nserv->hasXGTITLE)
520 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
522 snprintf (buf, sizeof (buf), "string_list_t NEWSGROUPS %s\r\n", mask);
523 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
529 * XOVER returns a tab separated list of:
530 * id|subject|from|date|Msgid|references|bytes|lines|xref
532 * This has to duplicate some of the functionality of
533 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
534 * a limited number of headers which are "parsed" by placement in the list)
536 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
538 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
542 hdr->env = envelope_new();
543 hdr->env->newsgroups = m_strdup(nntp_data->group);
544 hdr->content = mutt_new_body ();
545 hdr->content->type = TYPETEXT;
546 hdr->content->subtype = m_strdup("plain");
547 hdr->content->encoding = ENC7BIT;
548 hdr->content->disposition = DISPINLINE;
549 hdr->content->length = -1;
552 for (x = 0; !done && x < 9; x++) {
553 /* if from file, need to skip newline character */
554 while (*p && *p != '\n' && *p != '\t')
563 hdr->article_num = atoi (b);
564 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
567 hdr->env->subject = m_strdup(b);
570 address_list_wipe(&hdr->env->from);
571 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
572 /* same as for mutt_parse_rfc822_line():
573 * don't leave from info NULL if there's an invalid address (or
574 * whatever) in From: field; mutt would just display it as empty
575 * and mark mail/(esp.) news article as your own. aaargh! this
576 * bothered me for _years_ */
577 if (!hdr->env->from) {
578 hdr->env->from = address_new ();
579 hdr->env->from->personal = m_strdup(b);
583 hdr->date_sent = mutt_parse_date (b, hdr);
584 hdr->received = hdr->date_sent;
587 p_delete(&hdr->env->message_id);
588 hdr->env->message_id = m_strdup(b);
591 string_list_wipe(&hdr->env->references);
592 hdr->env->references = mutt_parse_references (b, 0);
595 hdr->content->length = atoi (b);
598 hdr->lines = atoi (b);
602 p_delete(&hdr->env->xref);
603 b = b + 6; /* skips the "Xref: " */
604 hdr->env->xref = m_strdup(b);
605 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
607 rfc2047_decode_envelope(hdr->env);
620 unsigned short *messages;
624 #define fc ((FETCH_CONTEXT *) c)
626 #if WANT_LISTGROUP_COMMAND
627 static int _nntp_fetch_numbers (unsigned int num, void *c)
629 if (num < fc->base || num > fc->last)
631 fc->messages[num - fc->base] = 1;
634 static int nntp_fetch_numbers (char *line, void *c)
638 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
642 static int add_xover_line (char *line, void *c)
644 unsigned int num, total;
645 CONTEXT *ctx = fc->ctx;
646 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
651 if (ctx->msgcount >= ctx->hdrmax)
652 mx_alloc_memory (ctx);
653 ctx->hdrs[ctx->msgcount] = header_new();
654 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
656 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
657 num = ctx->hdrs[ctx->msgcount]->article_num;
659 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
661 if (num > data->lastLoaded)
662 data->lastLoaded = num;
663 num = num - fc->first + 1;
664 total = fc->last - fc->first + 1;
665 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
666 mutt_message ("%s %d/%d", fc->msg, num, total);
669 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
676 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
679 char buf[HUGE_STRING];
680 char *msg = _("Fetching message headers...");
681 char *msg2 = _("Fetching headers from cache...");
682 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
686 unsigned int current;
690 /* if empty group or nothing to do */
691 if (!last || first > last)
694 /* fetch list of articles */
695 mutt_message _("Fetching list of articles...");
700 fc.messages = p_new(unsigned short, last - first + 1);
701 #if WANT_LISTGROUP_COMMAND
702 if (nntp_data->nserv->hasLISTGROUP) {
703 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
704 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
706 mutt_error (_("LISTGROUP command failed: %s"), buf);
708 p_delete(&fc.messages);
714 for (num = 0; num < last - first + 1; num++)
715 fc.messages[num] = 1;
716 #if WANT_LISTGROUP_COMMAND
720 /* CACHE: must be loaded xover cache here */
721 num = nntp_data->lastCached - first + 1;
722 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
723 nntp_cache_expand (buf, nntp_data->cache);
726 if ((f = safe_fopen (buf, "r"))) {
729 /* counting number of lines */
730 while (fgets (buf, sizeof (buf), f) != NULL)
733 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
735 oldmsgcount = ctx->msgcount;
737 fc.last = first + num - 1;
739 while (fgets (buf, sizeof (buf), f) != NULL) {
740 if (ReadInc && ((++c) % ReadInc == 0))
741 mutt_message ("%s %d/%d", msg2, c, r);
742 add_xover_line (buf, &fc);
745 nntp_data->lastLoaded = fc.last;
747 if (ctx->msgcount > oldmsgcount)
748 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
751 nntp_delete_cache (nntp_data);
753 num = last - first + 1;
755 p_delete(&fc.messages);
760 * Without XOVER, we have to fetch each article header and parse
761 * it. With XOVER, we ask for all of them
764 if (nntp_data->nserv->hasXOVER) {
765 oldmsgcount = ctx->msgcount;
769 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
770 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
771 if (ctx->msgcount > oldmsgcount)
772 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
774 mutt_error (_("XOVER command failed: %s"), buf);
775 p_delete(&fc.messages);
781 for (current = first; current <= last; current++) {
784 ret = current - first + 1;
785 mutt_message ("%s %d/%d", msg, ret, num);
787 if (!fc.messages[current - fc.base])
790 if (ctx->msgcount >= ctx->hdrmax)
791 mx_alloc_memory (ctx);
792 h = ctx->hdrs[ctx->msgcount] = header_new();
793 h->index = ctx->msgcount;
795 ret = nntp_read_header (ctx, NULL, current);
796 if (ret == 0) { /* Got article. Fetch next header */
797 nntp_get_status (ctx, h, NULL, h->article_num);
799 mx_update_context (ctx, 1);
802 header_delete(&h); /* skip it */
804 p_delete(&fc.messages);
808 if (current > nntp_data->lastLoaded)
809 nntp_data->lastLoaded = current;
811 p_delete(&fc.messages);
812 nntp_data->lastLoaded = last;
818 * currently, nntp "mailbox" is "newsgroup"
820 int nntp_open_mailbox (CONTEXT * ctx)
822 NNTP_DATA *nntp_data;
824 char buf[HUGE_STRING];
825 char server[LONG_STRING];
832 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
833 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
839 nntp_expand_path (server, sizeof (server), &act);
840 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
843 CurrentNewsSrv = serv;
845 /* create NNTP-specific state struct if nof found in list */
846 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
847 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
848 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
849 strcpy (nntp_data->group, buf);
850 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
851 nntp_add_to_list (serv, nntp_data);
853 ctx->data = nntp_data;
854 nntp_data->nserv = serv;
856 mutt_message (_("Selecting %s..."), nntp_data->group);
858 if (!nntp_data->desc) {
859 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
861 nntp_save_cache_index (serv);
865 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
869 if (m_strncmp("211", buf, 3)) {
870 string_list_t *l = serv->list;
872 /* GROUP command failed */
873 if (!m_strncmp("411", buf, 3)) {
874 mutt_error (_("Newsgroup %s not found on server %s"),
875 nntp_data->group, serv->conn->account.host);
877 /* CACHE: delete cache and line from .index */
878 nntp_delete_cache (nntp_data);
879 hash_delete (serv->newsgroups, nntp_data->group, NULL,
881 while (l && l->data != (void *) nntp_data)
892 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
893 &nntp_data->lastMessage, buf);
895 nntp_data->deleted = 0;
897 time (&serv->check_time);
900 * Check for max adding context. If it is greater than $nntp_context,
901 * strip off extra articles
903 first = nntp_data->firstMessage;
904 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
905 first = nntp_data->lastMessage - NntpContext + 1;
907 nntp_data->lastLoaded = first - 1;
908 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
911 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
913 char buf[LONG_STRING];
914 char path[_POSIX_PATH_MAX];
919 /* see if we already have the message in our cache */
921 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
924 /* if everything is fine, assign msg->fp and return */
925 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
926 (msg->fp = fopen (cache->path, "r")))
929 /* clear the previous entry */
930 unlink (cache->path);
931 p_delete(&cache->path);
933 cache->index = ctx->hdrs[msgno]->index;
935 cache->path = m_strdup(path);
936 if (!(msg->fp = safe_fopen (path, "w+"))) {
937 p_delete(&cache->path);
941 if (ctx->hdrs[msgno]->article_num == 0)
942 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
943 ctx->hdrs[msgno]->env->message_id);
945 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
946 ctx->hdrs[msgno]->article_num);
948 bar.msg = _("Fetching message...");
950 mutt_progress_bar (&bar, 0);
952 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
953 msg->fp, ctx->tagged);
955 mutt_error (_("Article %d not found on server"),
956 ctx->hdrs[msgno]->article_num);
962 p_delete(&cache->path);
966 envelope_delete(&ctx->hdrs[msgno]->env);
967 ctx->hdrs[msgno]->env =
968 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
969 /* fix content length */
970 fseeko (msg->fp, 0, SEEK_END);
971 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
972 ctx->hdrs[msgno]->content->offset;
974 /* this is called in mutt before the open which fetches the message,
975 * which is probably wrong, but we just call it again here to handle
976 * the problem instead of fixing it.
978 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
980 /* These would normally be updated in mx_update_context(), but the
981 * full headers aren't parsed with XOVER, so the information wasn't
984 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
993 int nntp_post (const char *msg)
995 char buf[LONG_STRING];
998 NNTP_DATA *nntp_data;
1000 if (Context && Context->magic == M_NNTP)
1001 nntp_data = (NNTP_DATA *) Context->data;
1003 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1004 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1005 mutt_error (_("Can't post article. No connection to news server."));
1008 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1011 if (!(f = safe_fopen (msg, "r"))) {
1012 mutt_error (_("Can't post article. Unable to open %s"), msg);
1016 m_strcpy(buf, sizeof(buf), "POST\r\n");
1017 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1018 mutt_error (_("Can't post article. Connection to %s lost."),
1019 nntp_data->nserv->conn->account.host);
1022 if (buf[0] != '3') {
1023 mutt_error (_("Can't post article: %s"), buf);
1029 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1030 len = m_strlen(buf);
1031 if (buf[len - 1] == '\n') {
1032 buf[len - 1] = '\r';
1038 mutt_socket_write_d (nntp_data->nserv->conn, buf, M_SOCK_LOG_HDR);
1040 mutt_socket_write_d (nntp_data->nserv->conn, buf + 1, M_SOCK_LOG_HDR);
1044 if (buf[m_strlen(buf) - 1] != '\n')
1045 mutt_socket_write_d (nntp_data->nserv->conn, "\r\n", M_SOCK_LOG_HDR);
1046 mutt_socket_write_d (nntp_data->nserv->conn, ".\r\n", M_SOCK_LOG_HDR);
1047 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1048 mutt_error (_("Can't post article. Connection to %s lost."),
1049 nntp_data->nserv->conn->account.host);
1052 if (buf[0] != '2') {
1053 mutt_error (_("Can't post article: %s"), buf);
1060 /* nntp_logout_all: close all open connections. */
1061 void nntp_logout_all (void)
1063 char buf[LONG_STRING];
1066 conn = mutt_socket_head ();
1069 CONNECTION* next = conn->next;
1070 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1071 mutt_message (_("Closing connection to %s..."), conn->account.host);
1072 mutt_socket_write (conn, "QUIT\r\n");
1073 mutt_socket_readln (buf, sizeof (buf), conn);
1074 mutt_clear_error ();
1075 mutt_socket_close (conn);
1076 mutt_socket_free (conn);
1082 static void nntp_free_acache (NNTP_DATA * data)
1086 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1087 if (data->acache[i].path) {
1088 unlink (data->acache[i].path);
1089 p_delete(&data->acache[i].path);
1094 void nntp_delete_data (void *p)
1096 NNTP_DATA *data = (NNTP_DATA *)p;
1100 p_delete(&data->entries);
1101 p_delete(&data->desc);
1102 p_delete(&data->cache);
1103 nntp_free_acache (data);
1107 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1109 NNTP_DATA *data = ctx->data;
1111 /* CACHE: update cache and .index files */
1112 if ((option (OPTSAVEUNSUB) || data->subscribed))
1113 nntp_save_cache_group (ctx);
1114 nntp_free_acache (data);
1116 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1120 void nntp_fastclose_mailbox (CONTEXT * ctx)
1122 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1126 nntp_free_acache (data);
1127 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1129 nntp_save_cache_index (data->nserv);
1130 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1132 nntp_delete_data (data);
1134 nntp_sync_sidebar (data);
1137 /* commit changes and terminate connection */
1138 int nntp_close_mailbox (CONTEXT * ctx)
1142 mutt_message _("Quitting newsgroup...");
1145 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1148 if (data->nserv && data->nserv->conn && ctx->unread) {
1149 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1151 mutt_newsgroup_catchup (data->nserv, data->group);
1156 nntp_sync_mailbox (ctx, 0, NULL);
1157 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1160 news = ((NNTP_DATA *) ctx->data)->nserv;
1161 newsrc_gen_entries (ctx);
1162 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1163 mutt_newsrc_update (news);
1165 mutt_clear_error ();
1169 /* use the GROUP command to poll for new mail */
1170 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1172 char buf[LONG_STRING];
1175 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1179 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1182 if (m_strncmp("211", buf, 3)) {
1184 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1188 if (!m_strncmp("211", buf, 3)) {
1192 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1193 nntp_data->firstMessage = first;
1194 nntp_data->lastMessage = last;
1195 if (ctx && last > nntp_data->lastLoaded) {
1196 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1197 time (&nntp_data->nserv->check_time);
1200 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1201 nntp_data->unread = count;
1203 mutt_newsgroup_stat (nntp_data);
1204 /* active was renumbered? */
1205 if (last < nntp_data->lastLoaded) {
1206 if (!nntp_data->max) {
1207 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1210 nntp_data->lastCached = 0;
1212 nntp_data->entries[0].first = 1;
1213 nntp_data->entries[0].last = 0;
1215 nntp_sync_sidebar (nntp_data);
1218 time (&nntp_data->nserv->check_time);
1222 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1224 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1227 static int add_group (char *buf, void *serv)
1229 #define s ((NNTP_SERVER *) serv)
1230 char group[LONG_STRING], mod, desc[HUGE_STRING];
1232 NNTP_DATA *nntp_data;
1235 _checked = n; /* _checked have N, where N = number of groups */
1236 if (!buf) /* at EOF must be zerouth */
1243 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1246 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1248 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1249 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1250 strcpy (nntp_data->group, group);
1251 nntp_data->nserv = s;
1252 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1253 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1254 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1255 nntp_add_to_list (s, nntp_data);
1257 nntp_data->deleted = 0;
1258 nntp_data->firstMessage = first;
1259 nntp_data->lastMessage = last;
1261 nntp_data->allowed = 1;
1263 nntp_data->allowed = 0;
1264 if (nntp_data->desc)
1265 p_delete(&nntp_data->desc);
1267 nntp_data->desc = m_strdup(desc);
1268 if (nntp_data->rc || nntp_data->lastCached)
1269 mutt_newsgroup_stat (nntp_data);
1270 else if (nntp_data->lastMessage &&
1271 nntp_data->firstMessage <= nntp_data->lastMessage)
1272 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1274 nntp_data->unread = 0;
1280 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1282 char buf[LONG_STRING];
1283 NNTP_DATA nntp_data;
1289 if (!serv || !serv->newgroups_time)
1292 if (nntp_open_connection (serv) < 0)
1295 /* check subscribed groups for new news */
1296 if (option (OPTSHOWNEWNEWS)) {
1297 mutt_message _("Checking for new messages...");
1299 for (l = serv->list; l; l = l->next) {
1300 serv->check_time = 0; /* really check! */
1301 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1302 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1304 sidebar_draw (CurrentMenu);
1309 mutt_message _("Checking for new newsgroups...");
1311 now = serv->newgroups_time;
1312 time (&serv->newgroups_time);
1314 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1315 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1316 t->tm_min, t->tm_sec);
1317 nntp_data.nserv = serv;
1318 if (Context && Context->magic == M_NNTP)
1319 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1321 nntp_data.group = NULL;
1323 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1324 add_group, serv, 0) != 0) {
1328 mutt_message _("Loading descriptions...");
1333 emp.next = serv->list;
1337 ((NNTP_DATA *) l->data)->new = 1;
1338 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1342 nntp_save_cache_index (serv);
1343 mutt_clear_error ();
1347 /* Load list of all newsgroups from cache ALL */
1348 int nntp_get_cache_all (NNTP_SERVER * serv)
1350 char buf[HUGE_STRING];
1353 nntp_cache_expand (buf, serv->cache);
1354 if ((f = safe_fopen (buf, "r"))) {
1357 while (fgets (buf, sizeof (buf), f) != NULL) {
1358 if (ReadInc && (i % ReadInc == 0))
1359 mutt_message (_("Loading list from cache... %d"), i);
1360 add_group (buf, serv);
1363 add_group (NULL, NULL);
1365 mutt_clear_error ();
1369 p_delete(&serv->cache);
1374 /* Load list of all newsgroups from active */
1375 int nntp_get_active (NNTP_SERVER * serv)
1377 char msg[SHORT_STRING];
1378 NNTP_DATA nntp_data;
1381 if (nntp_open_connection (serv) < 0)
1384 snprintf (msg, sizeof (msg),
1385 _("Loading list of all newsgroups on server %s..."),
1386 serv->conn->account.host);
1388 time (&serv->newgroups_time);
1389 nntp_data.nserv = serv;
1390 nntp_data.group = NULL;
1392 if (mutt_nntp_fetch (&nntp_data, "string_list_t\r\n", msg, NULL, add_group, serv, 0) < 0) {
1396 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1398 nntp_get_desc (&nntp_data, "*", msg, NULL);
1400 for (tmp = serv->list; tmp; tmp = tmp->next) {
1401 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1403 if (data && data->deleted && !data->rc) {
1404 nntp_delete_cache (data);
1405 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1409 nntp_save_cache_index (serv);
1411 mutt_clear_error ();
1416 * returns -1 if error ocurred while retrieving header,
1417 * number of articles which ones exist in context on success.
1419 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1423 /* if msgid is already in context, don't reload them */
1424 if (hash_find (ctx->id_hash, msgid))
1426 if (ctx->msgcount == ctx->hdrmax)
1427 mx_alloc_memory (ctx);
1428 ctx->hdrs[ctx->msgcount] = header_new();
1429 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1431 mutt_message (_("Fetching %s from server..."), msgid);
1432 ret = nntp_read_header (ctx, msgid, 0);
1433 /* since nntp_read_header() may set read flag, we must reset it */
1434 ctx->hdrs[ctx->msgcount]->read = 0;
1436 header_delete(&ctx->hdrs[ctx->msgcount]);
1439 mx_update_context (ctx, 1);
1449 unsigned int *child;
1452 static int check_children (char *s, void *c)
1454 #define cc ((CHILD_CONTEXT *) c)
1457 if (!s || (n = atoi (s)) == 0)
1459 for (i = 0; i < cc->ctx->msgcount; i++)
1460 if (cc->ctx->hdrs[i]->article_num == n)
1462 if (cc->num >= cc->max)
1463 p_realloc(&cc->child, cc->max += 25);
1464 cc->child[cc->num++] = n;
1470 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1472 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1474 int i, ret = 0, tmp = 0;
1477 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1478 !nntp_data->nserv->conn->account.host)
1480 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1482 if (!nntp_data->nserv->hasXPAT) {
1483 mutt_error (_("Server %s does not support this operation!"),
1484 nntp_data->nserv->conn->account.host);
1488 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1489 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1494 cc.child = p_new(unsigned int, 25);
1495 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1496 p_delete(&cc.child);
1499 /* dont try to read the xover cache. check_children() already
1500 * made sure that we dont have the article, so we need to visit
1501 * the server. Reading the cache at this point is also bad
1502 * because it would duplicate messages */
1503 if (option (OPTNEWSCACHE)) {
1505 unset_option (OPTNEWSCACHE);
1507 for (i = 0; i < cc.num; i++) {
1508 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1510 if (ctx->msgcount &&
1511 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1512 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1515 set_option (OPTNEWSCACHE);
1516 p_delete(&cc.child);