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(conn, buf);
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(buf, sizeof (buf), nntp_data->nserv->conn);
349 if (!lenbuf && buf[0] == '.') {
350 if (buf[1] == '\0') {
358 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
361 if (chunk >= ssizeof (buf)) {
362 lenbuf += m_strlen(p);
366 mutt_progress_bar (bar, pos);
369 if (ReadInc && (line % ReadInc == 0)) {
371 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
373 mutt_message ("%s %d", msg, line);
377 if (ret == 0 && funct (inbuf, data) < 0)
382 p_realloc(&inbuf, lenbuf + sizeof (buf));
391 static int nntp_read_tempfile (char *line, void *file)
393 FILE *f = (FILE *) file;
399 if (fputc ('\n', f) == EOF)
405 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
408 register char *p, *b;
409 register char *colon = NULL;
413 /* skip to next word */
415 while (*b && ((*b == ' ') || (*b == '\t')))
419 /* skip to end of word */
420 while (*p && (*p != ' ') && (*p != '\t')) {
432 nntp_get_status (ctx, h, b, atoi (colon));
433 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
434 h->article_num = atoi (colon);
442 * 1 if article not found
443 * -1 if read or write error on tempfile or socket
445 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
448 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
450 char buf[LONG_STRING];
451 char tempfile[_POSIX_PATH_MAX];
453 HEADER *h = ctx->hdrs[ctx->msgcount];
455 mutt_mktemp (tempfile);
456 if (!(f = safe_fopen (tempfile, "w+")))
460 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
462 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
464 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
468 return (ret == -1 ? -1 : 1);
471 h->article_num = article_num;
472 h->env = mutt_read_rfc822_header (f, h, 0, 0);
476 if (h->env->xref != NULL)
477 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
478 else if (h->article_num == 0 && msgid) {
479 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
480 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
481 h->article_num = atoi (buf + 4);
487 static int parse_description (char *line, void *n)
489 #define news ((NNTP_SERVER *) n)
490 register char *d = line;
495 while (*d && *d != '\t' && *d != ' ')
499 while (*d && (*d == '\t' || *d == ' '))
501 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
502 m_strcmp(d, data->desc)) {
503 p_delete(&data->desc);
504 data->desc = m_strdup(d);
510 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
514 if (!option (OPTLOADDESC) || !data || !data->nserv)
517 /* Get newsgroup description, if we can */
518 if (data->nserv->hasXGTITLE)
519 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
521 snprintf (buf, sizeof (buf), "string_list_t NEWSGROUPS %s\r\n", mask);
522 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
528 * XOVER returns a tab separated list of:
529 * id|subject|from|date|Msgid|references|bytes|lines|xref
531 * This has to duplicate some of the functionality of
532 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
533 * a limited number of headers which are "parsed" by placement in the list)
535 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
537 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
541 hdr->env = envelope_new();
542 hdr->env->newsgroups = m_strdup(nntp_data->group);
543 hdr->content = mutt_new_body ();
544 hdr->content->type = TYPETEXT;
545 hdr->content->subtype = m_strdup("plain");
546 hdr->content->encoding = ENC7BIT;
547 hdr->content->disposition = DISPINLINE;
548 hdr->content->length = -1;
551 for (x = 0; !done && x < 9; x++) {
552 /* if from file, need to skip newline character */
553 while (*p && *p != '\n' && *p != '\t')
562 hdr->article_num = atoi (b);
563 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
566 hdr->env->subject = m_strdup(b);
569 address_list_wipe(&hdr->env->from);
570 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
571 /* same as for mutt_parse_rfc822_line():
572 * don't leave from info NULL if there's an invalid address (or
573 * whatever) in From: field; mutt would just display it as empty
574 * and mark mail/(esp.) news article as your own. aaargh! this
575 * bothered me for _years_ */
576 if (!hdr->env->from) {
577 hdr->env->from = address_new ();
578 hdr->env->from->personal = m_strdup(b);
582 hdr->date_sent = mutt_parse_date (b, hdr);
583 hdr->received = hdr->date_sent;
586 p_delete(&hdr->env->message_id);
587 hdr->env->message_id = m_strdup(b);
590 string_list_wipe(&hdr->env->references);
591 hdr->env->references = mutt_parse_references (b, 0);
594 hdr->content->length = atoi (b);
597 hdr->lines = atoi (b);
601 p_delete(&hdr->env->xref);
602 b = b + 6; /* skips the "Xref: " */
603 hdr->env->xref = m_strdup(b);
604 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
606 rfc2047_decode_envelope(hdr->env);
619 unsigned short *messages;
623 #define fc ((FETCH_CONTEXT *) c)
625 #if WANT_LISTGROUP_COMMAND
626 static int _nntp_fetch_numbers (unsigned int num, void *c)
628 if (num < fc->base || num > fc->last)
630 fc->messages[num - fc->base] = 1;
633 static int nntp_fetch_numbers (char *line, void *c)
637 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
641 static int add_xover_line (char *line, void *c)
643 unsigned int num, total;
644 CONTEXT *ctx = fc->ctx;
645 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
650 if (ctx->msgcount >= ctx->hdrmax)
651 mx_alloc_memory (ctx);
652 ctx->hdrs[ctx->msgcount] = header_new();
653 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
655 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
656 num = ctx->hdrs[ctx->msgcount]->article_num;
658 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
660 if (num > data->lastLoaded)
661 data->lastLoaded = num;
662 num = num - fc->first + 1;
663 total = fc->last - fc->first + 1;
664 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
665 mutt_message ("%s %d/%d", fc->msg, num, total);
668 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
675 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
678 char buf[HUGE_STRING];
679 char *msg = _("Fetching message headers...");
680 char *msg2 = _("Fetching headers from cache...");
681 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
685 unsigned int current;
689 /* if empty group or nothing to do */
690 if (!last || first > last)
693 /* fetch list of articles */
694 mutt_message _("Fetching list of articles...");
699 fc.messages = p_new(unsigned short, last - first + 1);
700 #if WANT_LISTGROUP_COMMAND
701 if (nntp_data->nserv->hasLISTGROUP) {
702 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
703 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
705 mutt_error (_("LISTGROUP command failed: %s"), buf);
707 p_delete(&fc.messages);
713 for (num = 0; num < last - first + 1; num++)
714 fc.messages[num] = 1;
715 #if WANT_LISTGROUP_COMMAND
719 /* CACHE: must be loaded xover cache here */
720 num = nntp_data->lastCached - first + 1;
721 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
722 nntp_cache_expand (buf, nntp_data->cache);
725 if ((f = safe_fopen (buf, "r"))) {
728 /* counting number of lines */
729 while (fgets (buf, sizeof (buf), f) != NULL)
732 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
734 oldmsgcount = ctx->msgcount;
736 fc.last = first + num - 1;
738 while (fgets (buf, sizeof (buf), f) != NULL) {
739 if (ReadInc && ((++c) % ReadInc == 0))
740 mutt_message ("%s %d/%d", msg2, c, r);
741 add_xover_line (buf, &fc);
744 nntp_data->lastLoaded = fc.last;
746 if (ctx->msgcount > oldmsgcount)
747 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
750 nntp_delete_cache (nntp_data);
752 num = last - first + 1;
754 p_delete(&fc.messages);
759 * Without XOVER, we have to fetch each article header and parse
760 * it. With XOVER, we ask for all of them
763 if (nntp_data->nserv->hasXOVER) {
764 oldmsgcount = ctx->msgcount;
768 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
769 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
770 if (ctx->msgcount > oldmsgcount)
771 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
773 mutt_error (_("XOVER command failed: %s"), buf);
774 p_delete(&fc.messages);
780 for (current = first; current <= last; current++) {
783 ret = current - first + 1;
784 mutt_message ("%s %d/%d", msg, ret, num);
786 if (!fc.messages[current - fc.base])
789 if (ctx->msgcount >= ctx->hdrmax)
790 mx_alloc_memory (ctx);
791 h = ctx->hdrs[ctx->msgcount] = header_new();
792 h->index = ctx->msgcount;
794 ret = nntp_read_header (ctx, NULL, current);
795 if (ret == 0) { /* Got article. Fetch next header */
796 nntp_get_status (ctx, h, NULL, h->article_num);
798 mx_update_context (ctx, 1);
801 header_delete(&h); /* skip it */
803 p_delete(&fc.messages);
807 if (current > nntp_data->lastLoaded)
808 nntp_data->lastLoaded = current;
810 p_delete(&fc.messages);
811 nntp_data->lastLoaded = last;
817 * currently, nntp "mailbox" is "newsgroup"
819 int nntp_open_mailbox (CONTEXT * ctx)
821 NNTP_DATA *nntp_data;
823 char buf[HUGE_STRING];
824 char server[LONG_STRING];
831 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
832 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
838 nntp_expand_path (server, sizeof (server), &act);
839 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
842 CurrentNewsSrv = serv;
844 /* create NNTP-specific state struct if nof found in list */
845 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
846 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
847 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
848 strcpy (nntp_data->group, buf);
849 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
850 nntp_add_to_list (serv, nntp_data);
852 ctx->data = nntp_data;
853 nntp_data->nserv = serv;
855 mutt_message (_("Selecting %s..."), nntp_data->group);
857 if (!nntp_data->desc) {
858 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
860 nntp_save_cache_index (serv);
864 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
868 if (m_strncmp("211", buf, 3)) {
869 string_list_t *l = serv->list;
871 /* GROUP command failed */
872 if (!m_strncmp("411", buf, 3)) {
873 mutt_error (_("Newsgroup %s not found on server %s"),
874 nntp_data->group, serv->conn->account.host);
876 /* CACHE: delete cache and line from .index */
877 nntp_delete_cache (nntp_data);
878 hash_delete (serv->newsgroups, nntp_data->group, NULL,
880 while (l && l->data != (void *) nntp_data)
891 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
892 &nntp_data->lastMessage, buf);
894 nntp_data->deleted = 0;
896 time (&serv->check_time);
899 * Check for max adding context. If it is greater than $nntp_context,
900 * strip off extra articles
902 first = nntp_data->firstMessage;
903 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
904 first = nntp_data->lastMessage - NntpContext + 1;
906 nntp_data->lastLoaded = first - 1;
907 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
910 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
912 char buf[LONG_STRING];
913 char path[_POSIX_PATH_MAX];
918 /* see if we already have the message in our cache */
920 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
923 /* if everything is fine, assign msg->fp and return */
924 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
925 (msg->fp = fopen (cache->path, "r")))
928 /* clear the previous entry */
929 unlink (cache->path);
930 p_delete(&cache->path);
932 cache->index = ctx->hdrs[msgno]->index;
934 cache->path = m_strdup(path);
935 if (!(msg->fp = safe_fopen (path, "w+"))) {
936 p_delete(&cache->path);
940 if (ctx->hdrs[msgno]->article_num == 0)
941 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
942 ctx->hdrs[msgno]->env->message_id);
944 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
945 ctx->hdrs[msgno]->article_num);
947 bar.msg = _("Fetching message...");
949 mutt_progress_bar (&bar, 0);
951 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
952 msg->fp, ctx->tagged);
954 mutt_error (_("Article %d not found on server"),
955 ctx->hdrs[msgno]->article_num);
961 p_delete(&cache->path);
965 envelope_delete(&ctx->hdrs[msgno]->env);
966 ctx->hdrs[msgno]->env =
967 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
968 /* fix content length */
969 fseeko (msg->fp, 0, SEEK_END);
970 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
971 ctx->hdrs[msgno]->content->offset;
973 /* this is called in mutt before the open which fetches the message,
974 * which is probably wrong, but we just call it again here to handle
975 * the problem instead of fixing it.
977 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
979 /* These would normally be updated in mx_update_context(), but the
980 * full headers aren't parsed with XOVER, so the information wasn't
983 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
992 int nntp_post (const char *msg)
994 char buf[LONG_STRING];
997 NNTP_DATA *nntp_data;
999 if (Context && Context->magic == M_NNTP)
1000 nntp_data = (NNTP_DATA *) Context->data;
1002 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1003 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1004 mutt_error (_("Can't post article. No connection to news server."));
1007 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1010 if (!(f = safe_fopen (msg, "r"))) {
1011 mutt_error (_("Can't post article. Unable to open %s"), msg);
1015 m_strcpy(buf, sizeof(buf), "POST\r\n");
1016 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1017 mutt_error (_("Can't post article. Connection to %s lost."),
1018 nntp_data->nserv->conn->account.host);
1021 if (buf[0] != '3') {
1022 mutt_error (_("Can't post article: %s"), buf);
1028 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1029 len = m_strlen(buf);
1030 if (buf[len - 1] == '\n') {
1031 buf[len - 1] = '\r';
1037 mutt_socket_write(nntp_data->nserv->conn, buf);
1039 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
1043 if (buf[m_strlen(buf) - 1] != '\n')
1044 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
1045 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
1046 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1047 mutt_error (_("Can't post article. Connection to %s lost."),
1048 nntp_data->nserv->conn->account.host);
1051 if (buf[0] != '2') {
1052 mutt_error (_("Can't post article: %s"), buf);
1059 /* nntp_logout_all: close all open connections. */
1060 void nntp_logout_all (void)
1062 char buf[LONG_STRING];
1065 conn = mutt_socket_head ();
1068 CONNECTION* next = conn->next;
1069 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1070 mutt_message (_("Closing connection to %s..."), conn->account.host);
1071 mutt_socket_write (conn, "QUIT\r\n");
1072 mutt_socket_readln (buf, sizeof (buf), conn);
1073 mutt_clear_error ();
1074 mutt_socket_close (conn);
1075 mutt_socket_free (conn);
1081 static void nntp_free_acache (NNTP_DATA * data)
1085 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1086 if (data->acache[i].path) {
1087 unlink (data->acache[i].path);
1088 p_delete(&data->acache[i].path);
1093 void nntp_delete_data (void *p)
1095 NNTP_DATA *data = (NNTP_DATA *)p;
1099 p_delete(&data->entries);
1100 p_delete(&data->desc);
1101 p_delete(&data->cache);
1102 nntp_free_acache (data);
1106 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1108 NNTP_DATA *data = ctx->data;
1110 /* CACHE: update cache and .index files */
1111 if ((option (OPTSAVEUNSUB) || data->subscribed))
1112 nntp_save_cache_group (ctx);
1113 nntp_free_acache (data);
1115 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1119 void nntp_fastclose_mailbox (CONTEXT * ctx)
1121 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1125 nntp_free_acache (data);
1126 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1128 nntp_save_cache_index (data->nserv);
1129 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1131 nntp_delete_data (data);
1133 nntp_sync_sidebar (data);
1136 /* commit changes and terminate connection */
1137 int nntp_close_mailbox (CONTEXT * ctx)
1141 mutt_message _("Quitting newsgroup...");
1144 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1147 if (data->nserv && data->nserv->conn && ctx->unread) {
1148 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1150 mutt_newsgroup_catchup (data->nserv, data->group);
1155 nntp_sync_mailbox (ctx, 0, NULL);
1156 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1159 news = ((NNTP_DATA *) ctx->data)->nserv;
1160 newsrc_gen_entries (ctx);
1161 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1162 mutt_newsrc_update (news);
1164 mutt_clear_error ();
1168 /* use the GROUP command to poll for new mail */
1169 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1171 char buf[LONG_STRING];
1174 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1178 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1181 if (m_strncmp("211", buf, 3)) {
1183 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1187 if (!m_strncmp("211", buf, 3)) {
1191 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1192 nntp_data->firstMessage = first;
1193 nntp_data->lastMessage = last;
1194 if (ctx && last > nntp_data->lastLoaded) {
1195 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1196 time (&nntp_data->nserv->check_time);
1199 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1200 nntp_data->unread = count;
1202 mutt_newsgroup_stat (nntp_data);
1203 /* active was renumbered? */
1204 if (last < nntp_data->lastLoaded) {
1205 if (!nntp_data->max) {
1206 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1209 nntp_data->lastCached = 0;
1211 nntp_data->entries[0].first = 1;
1212 nntp_data->entries[0].last = 0;
1214 nntp_sync_sidebar (nntp_data);
1217 time (&nntp_data->nserv->check_time);
1221 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1223 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1226 static int add_group (char *buf, void *serv)
1228 #define s ((NNTP_SERVER *) serv)
1229 char group[LONG_STRING], mod, desc[HUGE_STRING];
1231 NNTP_DATA *nntp_data;
1234 _checked = n; /* _checked have N, where N = number of groups */
1235 if (!buf) /* at EOF must be zerouth */
1242 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1245 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1247 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1248 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1249 strcpy (nntp_data->group, group);
1250 nntp_data->nserv = s;
1251 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1252 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1253 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1254 nntp_add_to_list (s, nntp_data);
1256 nntp_data->deleted = 0;
1257 nntp_data->firstMessage = first;
1258 nntp_data->lastMessage = last;
1260 nntp_data->allowed = 1;
1262 nntp_data->allowed = 0;
1263 if (nntp_data->desc)
1264 p_delete(&nntp_data->desc);
1266 nntp_data->desc = m_strdup(desc);
1267 if (nntp_data->rc || nntp_data->lastCached)
1268 mutt_newsgroup_stat (nntp_data);
1269 else if (nntp_data->lastMessage &&
1270 nntp_data->firstMessage <= nntp_data->lastMessage)
1271 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1273 nntp_data->unread = 0;
1279 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1281 char buf[LONG_STRING];
1282 NNTP_DATA nntp_data;
1288 if (!serv || !serv->newgroups_time)
1291 if (nntp_open_connection (serv) < 0)
1294 /* check subscribed groups for new news */
1295 if (option (OPTSHOWNEWNEWS)) {
1296 mutt_message _("Checking for new messages...");
1298 for (l = serv->list; l; l = l->next) {
1299 serv->check_time = 0; /* really check! */
1300 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1301 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1303 sidebar_draw (CurrentMenu);
1308 mutt_message _("Checking for new newsgroups...");
1310 now = serv->newgroups_time;
1311 time (&serv->newgroups_time);
1313 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1314 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1315 t->tm_min, t->tm_sec);
1316 nntp_data.nserv = serv;
1317 if (Context && Context->magic == M_NNTP)
1318 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1320 nntp_data.group = NULL;
1322 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1323 add_group, serv, 0) != 0) {
1327 mutt_message _("Loading descriptions...");
1332 emp.next = serv->list;
1336 ((NNTP_DATA *) l->data)->new = 1;
1337 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1341 nntp_save_cache_index (serv);
1342 mutt_clear_error ();
1346 /* Load list of all newsgroups from cache ALL */
1347 int nntp_get_cache_all (NNTP_SERVER * serv)
1349 char buf[HUGE_STRING];
1352 nntp_cache_expand (buf, serv->cache);
1353 if ((f = safe_fopen (buf, "r"))) {
1356 while (fgets (buf, sizeof (buf), f) != NULL) {
1357 if (ReadInc && (i % ReadInc == 0))
1358 mutt_message (_("Loading list from cache... %d"), i);
1359 add_group (buf, serv);
1362 add_group (NULL, NULL);
1364 mutt_clear_error ();
1368 p_delete(&serv->cache);
1373 /* Load list of all newsgroups from active */
1374 int nntp_get_active (NNTP_SERVER * serv)
1376 char msg[SHORT_STRING];
1377 NNTP_DATA nntp_data;
1380 if (nntp_open_connection (serv) < 0)
1383 snprintf (msg, sizeof (msg),
1384 _("Loading list of all newsgroups on server %s..."),
1385 serv->conn->account.host);
1387 time (&serv->newgroups_time);
1388 nntp_data.nserv = serv;
1389 nntp_data.group = NULL;
1391 if (mutt_nntp_fetch (&nntp_data, "string_list_t\r\n", msg, NULL, add_group, serv, 0) < 0) {
1395 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1397 nntp_get_desc (&nntp_data, "*", msg, NULL);
1399 for (tmp = serv->list; tmp; tmp = tmp->next) {
1400 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1402 if (data && data->deleted && !data->rc) {
1403 nntp_delete_cache (data);
1404 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1408 nntp_save_cache_index (serv);
1410 mutt_clear_error ();
1415 * returns -1 if error ocurred while retrieving header,
1416 * number of articles which ones exist in context on success.
1418 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1422 /* if msgid is already in context, don't reload them */
1423 if (hash_find (ctx->id_hash, msgid))
1425 if (ctx->msgcount == ctx->hdrmax)
1426 mx_alloc_memory (ctx);
1427 ctx->hdrs[ctx->msgcount] = header_new();
1428 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1430 mutt_message (_("Fetching %s from server..."), msgid);
1431 ret = nntp_read_header (ctx, msgid, 0);
1432 /* since nntp_read_header() may set read flag, we must reset it */
1433 ctx->hdrs[ctx->msgcount]->read = 0;
1435 header_delete(&ctx->hdrs[ctx->msgcount]);
1438 mx_update_context (ctx, 1);
1448 unsigned int *child;
1451 static int check_children (char *s, void *c)
1453 #define cc ((CHILD_CONTEXT *) c)
1456 if (!s || (n = atoi (s)) == 0)
1458 for (i = 0; i < cc->ctx->msgcount; i++)
1459 if (cc->ctx->hdrs[i]->article_num == n)
1461 if (cc->num >= cc->max)
1462 p_realloc(&cc->child, cc->max += 25);
1463 cc->child[cc->num++] = n;
1469 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1471 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1473 int i, ret = 0, tmp = 0;
1476 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1477 !nntp_data->nserv->conn->account.host)
1479 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1481 if (!nntp_data->nserv->hasXPAT) {
1482 mutt_error (_("Server %s does not support this operation!"),
1483 nntp_data->nserv->conn->account.host);
1487 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1488 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1493 cc.child = p_new(unsigned int, 25);
1494 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1495 p_delete(&cc.child);
1498 /* dont try to read the xover cache. check_children() already
1499 * made sure that we dont have the article, so we need to visit
1500 * the server. Reading the cache at this point is also bad
1501 * because it would duplicate messages */
1502 if (option (OPTNEWSCACHE)) {
1504 unset_option (OPTNEWSCACHE);
1506 for (i = 0; i < cc.num; i++) {
1507 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1509 if (ctx->msgcount &&
1510 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1511 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1514 set_option (OPTNEWSCACHE);
1515 p_delete(&cc.child);