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.
21 #include <lib-lib/mem.h>
22 #include <lib-lib/str.h>
23 #include <lib-lib/file.h>
24 #include <lib-lib/macros.h>
26 #include <lib-mime/mime.h>
28 #include <lib-ui/curses.h>
29 #include <lib-ui/sidebar.h>
38 #include <lib-crypt/crypt.h>
40 #define WANT_LISTGROUP_COMMAND 0
42 static unsigned int _checked = 0;
44 void nntp_sync_sidebar (NNTP_DATA* data) {
49 if (list_empty (Incoming))
52 /* unfortunately, NNTP_DATA::group only is the plain
53 * group name, so for every single update, we need to
54 * compose the full string which must be defined via
55 * mailboxes command ;-((( FIXME
58 snprintf (buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
59 (data->nserv->conn->account.flags & M_ACCT_SSL) ? "s" : "",
60 NONULL (data->nserv->conn->account.user),
61 *data->nserv->conn->account.pass ? ":" : "",
62 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
63 data->nserv->conn->account.host,
66 /* bail out if group not found via mailboxes */
67 if ((i = buffy_lookup (buf)) < 0)
70 tmp = (BUFFY*) Incoming->data[i];
71 /* copied from browser.c */
72 if (option (OPTMARKOLD) &&
73 data->lastCached >= data->firstMessage &&
74 data->lastCached <= data->lastMessage)
75 tmp->msg_unread = data->lastMessage - data->lastCached;
77 tmp->msg_unread = data->unread;
78 tmp->new = data->unread > 0;
79 /* this is closest to a "total" count we can get */
80 tmp->msgcount = data->lastMessage - data->firstMessage;
83 static int nntp_auth (NNTP_SERVER * serv)
85 CONNECTION *conn = serv->conn;
87 unsigned char flags = conn->account.flags;
89 if (mutt_account_getuser (&conn->account) || !conn->account.user[0] ||
90 mutt_account_getpass (&conn->account) || !conn->account.pass[0]) {
91 conn->account.flags = flags;
95 mutt_message _("Logging in...");
97 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
98 mutt_socket_write (conn, buf);
99 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
100 conn->account.flags = flags;
104 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
105 mutt_socket_write(conn, buf);
106 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
107 conn->account.flags = flags;
111 if (m_strncmp("281", buf, 3)) {
112 conn->account.flags = flags;
113 mutt_error _("Login failed.");
122 static int nntp_connect_error (NNTP_SERVER * serv)
124 serv->status = NNTP_NONE;
125 mutt_socket_close (serv->conn);
126 mutt_error _("Server closed connection!");
132 static int nntp_connect_and_auth (NNTP_SERVER * serv)
134 CONNECTION *conn = serv->conn;
138 serv->status = NNTP_NONE;
140 if (mutt_socket_open (conn) < 0)
143 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
144 return nntp_connect_error (serv);
146 if (!m_strncmp("200", buf, 3))
147 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
148 else if (!m_strncmp("201", buf, 3))
149 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
151 mutt_socket_close(conn);
153 mutt_error("%s", buf);
160 /* Tell INN to switch to mode reader if it isn't so. Ignore all
161 returned codes and messages. */
162 mutt_socket_write (conn, "MODE READER\r\n");
163 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
164 return nntp_connect_error (serv);
166 mutt_socket_write (conn, "STAT\r\n");
167 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
168 return nntp_connect_error (serv);
170 if (!(conn->account.flags & M_ACCT_USER) && m_strncmp("480", buf, 3)) {
171 serv->status = NNTP_OK;
175 rc = nntp_auth (serv);
177 return nntp_connect_error (serv);
179 mutt_socket_close (conn);
180 serv->status = NNTP_BYE;
184 mutt_socket_close (conn);
185 mutt_error _("Login failed.");
190 serv->status = NNTP_OK;
194 static int nntp_attempt_features (NNTP_SERVER * serv)
196 char buf[LONG_STRING];
197 CONNECTION *conn = serv->conn;
199 mutt_socket_write (conn, "XOVER\r\n");
200 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
201 return nntp_connect_error (serv);
202 if (m_strncmp("500", buf, 3))
205 mutt_socket_write (conn, "XPAT\r\n");
206 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
207 return nntp_connect_error (serv);
208 if (m_strncmp("500", buf, 3))
211 #if WANT_LISTGROUP_COMMAND
212 mutt_socket_write (conn, "LISTGROUP\r\n");
213 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
214 return (nntp_connect_error (serv));
215 if (m_strncmp("500", buf, 3))
216 serv->hasLISTGROUP = 1;
219 mutt_socket_write (conn, "XGTITLE +\r\n");
220 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
221 return nntp_connect_error (serv);
222 if (m_strncmp("500", buf, 3))
223 serv->hasXGTITLE = 1;
225 if (!m_strncmp("282", buf, 3)) {
227 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
228 return nntp_connect_error (serv);
229 } while (!(buf[0] == '.' && buf[1] == '\0'));
235 static int nntp_open_connection (NNTP_SERVER * serv)
237 if (serv->status == NNTP_OK)
239 if (serv->status == NNTP_BYE)
241 if (nntp_connect_and_auth (serv) < 0)
243 if (nntp_attempt_features (serv) < 0)
248 static int nntp_reconnect (NNTP_SERVER * serv)
250 char buf[SHORT_STRING];
252 mutt_socket_close (serv->conn);
255 if (nntp_connect_and_auth (serv) == 0)
258 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
259 serv->conn->account.host);
260 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
261 serv->status = NNTP_BYE;
267 /* Send data from line[LONG_STRING] and receive answer to same line */
268 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
270 char buf[LONG_STRING];
273 if (data->nserv->status == NNTP_BYE)
278 mutt_socket_write (data->nserv->conn, line);
280 else if (data->group) {
281 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
282 mutt_socket_write (data->nserv->conn, buf);
286 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
287 if (nntp_reconnect (data->nserv) < 0)
291 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
292 mutt_socket_write (data->nserv->conn, buf);
293 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
299 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
303 m_strcpy(line, linelen, buf);
308 * This function calls funct(*line, *data) for each received line,
309 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
312 * 1 - correct but not performed (may be, have to be continued),
313 * -1 - conection lost,
314 * -2 - invalid command or execution error,
315 * -3 - error in funct(*line, *data).
317 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query, char *msg,
318 progress_t* bar, int (*funct) (char *, void *),
319 void *data, int tagged)
321 char buf[LONG_STRING];
330 m_strcpy(buf, sizeof(buf), query);
331 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
340 inbuf = p_new(char, sizeof(buf));
343 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
348 if (!lenbuf && buf[0] == '.') {
349 if (buf[1] == '\0') {
357 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
360 if (chunk >= ssizeof (buf)) {
361 lenbuf += m_strlen(p);
365 mutt_progress_bar (bar, pos);
368 if (ReadInc && (line % ReadInc == 0)) {
370 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
372 mutt_message ("%s %d", msg, line);
376 if (ret == 0 && funct (inbuf, data) < 0)
381 p_realloc(&inbuf, lenbuf + sizeof (buf));
390 static int nntp_read_tempfile (char *line, void *file)
392 FILE *f = (FILE *) file;
398 if (fputc ('\n', f) == EOF)
404 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
407 register char *p, *b;
408 register char *colon = NULL;
412 /* skip to next word */
414 while (*b && ((*b == ' ') || (*b == '\t')))
418 /* skip to end of word */
419 while (*p && (*p != ' ') && (*p != '\t')) {
431 nntp_get_status (ctx, h, b, atoi (colon));
432 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
433 h->article_num = atoi (colon);
441 * 1 if article not found
442 * -1 if read or write error on tempfile or socket
444 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
447 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
449 char buf[LONG_STRING];
450 char tempfile[_POSIX_PATH_MAX];
452 HEADER *h = ctx->hdrs[ctx->msgcount];
454 mutt_mktemp (tempfile);
455 if (!(f = safe_fopen (tempfile, "w+")))
459 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
461 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
463 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
467 return (ret == -1 ? -1 : 1);
470 h->article_num = article_num;
471 h->env = mutt_read_rfc822_header (f, h, 0, 0);
475 if (h->env->xref != NULL)
476 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
477 else if (h->article_num == 0 && msgid) {
478 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
479 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
480 h->article_num = atoi (buf + 4);
486 static int parse_description (char *line, void *n)
488 #define news ((NNTP_SERVER *) n)
489 register char *d = line;
494 while (*d && *d != '\t' && *d != ' ')
498 while (*d && (*d == '\t' || *d == ' '))
500 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
501 m_strcmp(d, data->desc)) {
502 p_delete(&data->desc);
503 data->desc = m_strdup(d);
509 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
513 if (!option (OPTLOADDESC) || !data || !data->nserv)
516 /* Get newsgroup description, if we can */
517 if (data->nserv->hasXGTITLE)
518 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
520 snprintf (buf, sizeof (buf), "string_list_t NEWSGROUPS %s\r\n", mask);
521 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
527 * XOVER returns a tab separated list of:
528 * id|subject|from|date|Msgid|references|bytes|lines|xref
530 * This has to duplicate some of the functionality of
531 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
532 * a limited number of headers which are "parsed" by placement in the list)
534 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
536 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
540 hdr->env = envelope_new();
541 hdr->env->newsgroups = m_strdup(nntp_data->group);
542 hdr->content = body_new();
543 hdr->content->type = TYPETEXT;
544 hdr->content->subtype = m_strdup("plain");
545 hdr->content->encoding = ENC7BIT;
546 hdr->content->disposition = DISPINLINE;
547 hdr->content->length = -1;
550 for (x = 0; !done && x < 9; x++) {
551 /* if from file, need to skip newline character */
552 while (*p && *p != '\n' && *p != '\t')
561 hdr->article_num = atoi (b);
562 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
565 hdr->env->subject = m_strdup(b);
568 address_list_wipe(&hdr->env->from);
569 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
570 /* same as for mutt_parse_rfc822_line():
571 * don't leave from info NULL if there's an invalid address (or
572 * whatever) in From: field; mutt would just display it as empty
573 * and mark mail/(esp.) news article as your own. aaargh! this
574 * bothered me for _years_ */
575 if (!hdr->env->from) {
576 hdr->env->from = address_new ();
577 hdr->env->from->personal = m_strdup(b);
581 hdr->date_sent = mutt_parse_date (b, hdr);
582 hdr->received = hdr->date_sent;
585 p_delete(&hdr->env->message_id);
586 hdr->env->message_id = m_strdup(b);
589 string_list_wipe(&hdr->env->references);
590 hdr->env->references = mutt_parse_references (b, 0);
593 hdr->content->length = atoi (b);
596 hdr->lines = atoi (b);
600 p_delete(&hdr->env->xref);
601 b = b + 6; /* skips the "Xref: " */
602 hdr->env->xref = m_strdup(b);
603 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
605 rfc2047_decode_envelope(hdr->env);
618 unsigned short *messages;
622 #define fc ((FETCH_CONTEXT *) c)
624 #if WANT_LISTGROUP_COMMAND
625 static int _nntp_fetch_numbers (unsigned int num, void *c)
627 if (num < fc->base || num > fc->last)
629 fc->messages[num - fc->base] = 1;
632 static int nntp_fetch_numbers (char *line, void *c)
636 return (_nntp_fetch_numbers ((unsigned int) atoi (line), c));
640 static int add_xover_line (char *line, void *c)
642 unsigned int num, total;
643 CONTEXT *ctx = fc->ctx;
644 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
649 if (ctx->msgcount >= ctx->hdrmax)
650 mx_alloc_memory (ctx);
651 ctx->hdrs[ctx->msgcount] = header_new();
652 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
654 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
655 num = ctx->hdrs[ctx->msgcount]->article_num;
657 if (num >= fc->first && num <= fc->last && fc->messages[num - fc->base]) {
659 if (num > data->lastLoaded)
660 data->lastLoaded = num;
661 num = num - fc->first + 1;
662 total = fc->last - fc->first + 1;
663 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
664 mutt_message ("%s %d/%d", fc->msg, num, total);
667 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
674 static int nntp_fetch_headers (CONTEXT * ctx, unsigned int first,
677 char buf[HUGE_STRING];
678 char *msg = _("Fetching message headers...");
679 char *msg2 = _("Fetching headers from cache...");
680 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
684 unsigned int current;
688 /* if empty group or nothing to do */
689 if (!last || first > last)
692 /* fetch list of articles */
693 mutt_message _("Fetching list of articles...");
698 fc.messages = p_new(unsigned short, last - first + 1);
699 #if WANT_LISTGROUP_COMMAND
700 if (nntp_data->nserv->hasLISTGROUP) {
701 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
702 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_fetch_numbers, &fc, 0) !=
704 mutt_error (_("LISTGROUP command failed: %s"), buf);
706 p_delete(&fc.messages);
712 for (num = 0; num < last - first + 1; num++)
713 fc.messages[num] = 1;
714 #if WANT_LISTGROUP_COMMAND
718 /* CACHE: must be loaded xover cache here */
719 num = nntp_data->lastCached - first + 1;
720 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
721 nntp_cache_expand (buf, nntp_data->cache);
724 if ((f = safe_fopen (buf, "r"))) {
727 /* counting number of lines */
728 while (fgets (buf, sizeof (buf), f) != NULL)
731 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
733 oldmsgcount = ctx->msgcount;
735 fc.last = first + num - 1;
737 while (fgets (buf, sizeof (buf), f) != NULL) {
738 if (ReadInc && ((++c) % ReadInc == 0))
739 mutt_message ("%s %d/%d", msg2, c, r);
740 add_xover_line (buf, &fc);
743 nntp_data->lastLoaded = fc.last;
745 if (ctx->msgcount > oldmsgcount)
746 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
749 nntp_delete_cache (nntp_data);
751 num = last - first + 1;
753 p_delete(&fc.messages);
758 * Without XOVER, we have to fetch each article header and parse
759 * it. With XOVER, we ask for all of them
762 if (nntp_data->nserv->hasXOVER) {
763 oldmsgcount = ctx->msgcount;
767 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
768 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
769 if (ctx->msgcount > oldmsgcount)
770 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
772 mutt_error (_("XOVER command failed: %s"), buf);
773 p_delete(&fc.messages);
779 for (current = first; current <= last; current++) {
782 ret = current - first + 1;
783 mutt_message ("%s %d/%d", msg, ret, num);
785 if (!fc.messages[current - fc.base])
788 if (ctx->msgcount >= ctx->hdrmax)
789 mx_alloc_memory (ctx);
790 h = ctx->hdrs[ctx->msgcount] = header_new();
791 h->index = ctx->msgcount;
793 ret = nntp_read_header (ctx, NULL, current);
794 if (ret == 0) { /* Got article. Fetch next header */
795 nntp_get_status (ctx, h, NULL, h->article_num);
797 mx_update_context (ctx, 1);
800 header_delete(&h); /* skip it */
802 p_delete(&fc.messages);
806 if (current > nntp_data->lastLoaded)
807 nntp_data->lastLoaded = current;
809 p_delete(&fc.messages);
810 nntp_data->lastLoaded = last;
816 * currently, nntp "mailbox" is "newsgroup"
818 int nntp_open_mailbox (CONTEXT * ctx)
820 NNTP_DATA *nntp_data;
822 char buf[HUGE_STRING];
823 char server[LONG_STRING];
830 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
831 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
837 nntp_expand_path (server, sizeof (server), &act);
838 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
841 CurrentNewsSrv = serv;
843 /* create NNTP-specific state struct if nof found in list */
844 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
845 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
846 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
847 strcpy (nntp_data->group, buf);
848 hash_insert (serv->newsgroups, nntp_data->group, nntp_data, 0);
849 nntp_add_to_list (serv, nntp_data);
851 ctx->data = nntp_data;
852 nntp_data->nserv = serv;
854 mutt_message (_("Selecting %s..."), nntp_data->group);
856 if (!nntp_data->desc) {
857 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
859 nntp_save_cache_index (serv);
863 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
867 if (m_strncmp("211", buf, 3)) {
868 string_list_t *l = serv->list;
870 /* GROUP command failed */
871 if (!m_strncmp("411", buf, 3)) {
872 mutt_error (_("Newsgroup %s not found on server %s"),
873 nntp_data->group, serv->conn->account.host);
875 /* CACHE: delete cache and line from .index */
876 nntp_delete_cache (nntp_data);
877 hash_delete (serv->newsgroups, nntp_data->group, NULL,
879 while (l && l->data != (void *) nntp_data)
890 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
891 &nntp_data->lastMessage, buf);
893 nntp_data->deleted = 0;
895 time (&serv->check_time);
898 * Check for max adding context. If it is greater than $nntp_context,
899 * strip off extra articles
901 first = nntp_data->firstMessage;
902 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
903 first = nntp_data->lastMessage - NntpContext + 1;
905 nntp_data->lastLoaded = first - 1;
906 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
909 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
911 char buf[LONG_STRING];
912 char path[_POSIX_PATH_MAX];
917 /* see if we already have the message in our cache */
919 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
922 /* if everything is fine, assign msg->fp and return */
923 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
924 (msg->fp = fopen (cache->path, "r")))
927 /* clear the previous entry */
928 unlink (cache->path);
929 p_delete(&cache->path);
931 cache->index = ctx->hdrs[msgno]->index;
933 cache->path = m_strdup(path);
934 if (!(msg->fp = safe_fopen (path, "w+"))) {
935 p_delete(&cache->path);
939 if (ctx->hdrs[msgno]->article_num == 0)
940 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
941 ctx->hdrs[msgno]->env->message_id);
943 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
944 ctx->hdrs[msgno]->article_num);
946 bar.msg = _("Fetching message...");
948 mutt_progress_bar (&bar, 0);
950 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
951 msg->fp, ctx->tagged);
953 mutt_error (_("Article %d not found on server"),
954 ctx->hdrs[msgno]->article_num);
960 p_delete(&cache->path);
964 envelope_delete(&ctx->hdrs[msgno]->env);
965 ctx->hdrs[msgno]->env =
966 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
967 /* fix content length */
968 fseeko (msg->fp, 0, SEEK_END);
969 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
970 ctx->hdrs[msgno]->content->offset;
972 /* this is called in mutt before the open which fetches the message,
973 * which is probably wrong, but we just call it again here to handle
974 * the problem instead of fixing it.
976 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
978 /* These would normally be updated in mx_update_context(), but the
979 * full headers aren't parsed with XOVER, so the information wasn't
982 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
991 int nntp_post (const char *msg)
993 char buf[LONG_STRING];
996 NNTP_DATA *nntp_data;
998 if (Context && Context->magic == M_NNTP)
999 nntp_data = (NNTP_DATA *) Context->data;
1001 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
1002 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
1003 mutt_error (_("Can't post article. No connection to news server."));
1006 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
1009 if (!(f = safe_fopen (msg, "r"))) {
1010 mutt_error (_("Can't post article. Unable to open %s"), msg);
1014 m_strcpy(buf, sizeof(buf), "POST\r\n");
1015 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1016 mutt_error (_("Can't post article. Connection to %s lost."),
1017 nntp_data->nserv->conn->account.host);
1020 if (buf[0] != '3') {
1021 mutt_error (_("Can't post article: %s"), buf);
1027 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
1028 len = m_strlen(buf);
1029 if (buf[len - 1] == '\n') {
1030 buf[len - 1] = '\r';
1036 mutt_socket_write(nntp_data->nserv->conn, buf);
1038 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
1042 if (buf[m_strlen(buf) - 1] != '\n')
1043 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
1044 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
1045 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1046 mutt_error (_("Can't post article. Connection to %s lost."),
1047 nntp_data->nserv->conn->account.host);
1050 if (buf[0] != '2') {
1051 mutt_error (_("Can't post article: %s"), buf);
1058 /* nntp_logout_all: close all open connections. */
1059 void nntp_logout_all (void)
1061 char buf[LONG_STRING];
1064 conn = mutt_socket_head ();
1067 CONNECTION* next = conn->next;
1068 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1069 mutt_message (_("Closing connection to %s..."), conn->account.host);
1070 mutt_socket_write (conn, "QUIT\r\n");
1071 mutt_socket_readln (buf, sizeof (buf), conn);
1072 mutt_clear_error ();
1073 mutt_socket_close (conn);
1074 mutt_socket_free (conn);
1080 static void nntp_free_acache (NNTP_DATA * data)
1084 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1085 if (data->acache[i].path) {
1086 unlink (data->acache[i].path);
1087 p_delete(&data->acache[i].path);
1092 void nntp_delete_data (void *p)
1094 NNTP_DATA *data = (NNTP_DATA *)p;
1098 p_delete(&data->entries);
1099 p_delete(&data->desc);
1100 p_delete(&data->cache);
1101 nntp_free_acache (data);
1105 int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1107 NNTP_DATA *data = ctx->data;
1109 /* CACHE: update cache and .index files */
1110 if ((option (OPTSAVEUNSUB) || data->subscribed))
1111 nntp_save_cache_group (ctx);
1112 nntp_free_acache (data);
1114 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1118 void nntp_fastclose_mailbox (CONTEXT * ctx)
1120 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1124 nntp_free_acache (data);
1125 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1127 nntp_save_cache_index (data->nserv);
1128 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1130 nntp_delete_data (data);
1132 nntp_sync_sidebar (data);
1135 /* commit changes and terminate connection */
1136 int nntp_close_mailbox (CONTEXT * ctx)
1140 mutt_message _("Quitting newsgroup...");
1143 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1146 if (data->nserv && data->nserv->conn && ctx->unread) {
1147 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1149 mutt_newsgroup_catchup (data->nserv, data->group);
1154 nntp_sync_mailbox (ctx, 0, NULL);
1155 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1158 news = ((NNTP_DATA *) ctx->data)->nserv;
1159 newsrc_gen_entries (ctx);
1160 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1161 mutt_newsrc_update (news);
1163 mutt_clear_error ();
1167 /* use the GROUP command to poll for new mail */
1168 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1170 char buf[LONG_STRING];
1173 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1177 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1180 if (m_strncmp("211", buf, 3)) {
1182 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1186 if (!m_strncmp("211", buf, 3)) {
1190 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1191 nntp_data->firstMessage = first;
1192 nntp_data->lastMessage = last;
1193 if (ctx && last > nntp_data->lastLoaded) {
1194 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1195 time (&nntp_data->nserv->check_time);
1198 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1199 nntp_data->unread = count;
1201 mutt_newsgroup_stat (nntp_data);
1202 /* active was renumbered? */
1203 if (last < nntp_data->lastLoaded) {
1204 if (!nntp_data->max) {
1205 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1208 nntp_data->lastCached = 0;
1210 nntp_data->entries[0].first = 1;
1211 nntp_data->entries[0].last = 0;
1213 nntp_sync_sidebar (nntp_data);
1216 time (&nntp_data->nserv->check_time);
1220 int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1222 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1225 static int add_group (char *buf, void *serv)
1227 #define s ((NNTP_SERVER *) serv)
1228 char group[LONG_STRING], mod, desc[HUGE_STRING];
1230 NNTP_DATA *nntp_data;
1233 _checked = n; /* _checked have N, where N = number of groups */
1234 if (!buf) /* at EOF must be zerouth */
1241 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1244 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1246 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1247 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1248 strcpy (nntp_data->group, group);
1249 nntp_data->nserv = s;
1250 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1251 s->newsgroups = hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1252 hash_insert (s->newsgroups, nntp_data->group, nntp_data, 0);
1253 nntp_add_to_list (s, nntp_data);
1255 nntp_data->deleted = 0;
1256 nntp_data->firstMessage = first;
1257 nntp_data->lastMessage = last;
1259 nntp_data->allowed = 1;
1261 nntp_data->allowed = 0;
1262 if (nntp_data->desc)
1263 p_delete(&nntp_data->desc);
1265 nntp_data->desc = m_strdup(desc);
1266 if (nntp_data->rc || nntp_data->lastCached)
1267 mutt_newsgroup_stat (nntp_data);
1268 else if (nntp_data->lastMessage &&
1269 nntp_data->firstMessage <= nntp_data->lastMessage)
1270 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1272 nntp_data->unread = 0;
1278 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1280 char buf[LONG_STRING];
1281 NNTP_DATA nntp_data;
1287 if (!serv || !serv->newgroups_time)
1290 if (nntp_open_connection (serv) < 0)
1293 /* check subscribed groups for new news */
1294 if (option (OPTSHOWNEWNEWS)) {
1295 mutt_message _("Checking for new messages...");
1297 for (l = serv->list; l; l = l->next) {
1298 serv->check_time = 0; /* really check! */
1299 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1300 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1302 sidebar_draw (CurrentMenu);
1307 mutt_message _("Checking for new newsgroups...");
1309 now = serv->newgroups_time;
1310 time (&serv->newgroups_time);
1312 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1313 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1314 t->tm_min, t->tm_sec);
1315 nntp_data.nserv = serv;
1316 if (Context && Context->magic == M_NNTP)
1317 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1319 nntp_data.group = NULL;
1321 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1322 add_group, serv, 0) != 0) {
1326 mutt_message _("Loading descriptions...");
1331 emp.next = serv->list;
1335 ((NNTP_DATA *) l->data)->new = 1;
1336 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1340 nntp_save_cache_index (serv);
1341 mutt_clear_error ();
1345 /* Load list of all newsgroups from cache ALL */
1346 int nntp_get_cache_all (NNTP_SERVER * serv)
1348 char buf[HUGE_STRING];
1351 nntp_cache_expand (buf, serv->cache);
1352 if ((f = safe_fopen (buf, "r"))) {
1355 while (fgets (buf, sizeof (buf), f) != NULL) {
1356 if (ReadInc && (i % ReadInc == 0))
1357 mutt_message (_("Loading list from cache... %d"), i);
1358 add_group (buf, serv);
1361 add_group (NULL, NULL);
1363 mutt_clear_error ();
1367 p_delete(&serv->cache);
1372 /* Load list of all newsgroups from active */
1373 int nntp_get_active (NNTP_SERVER * serv)
1375 char msg[SHORT_STRING];
1376 NNTP_DATA nntp_data;
1379 if (nntp_open_connection (serv) < 0)
1382 snprintf (msg, sizeof (msg),
1383 _("Loading list of all newsgroups on server %s..."),
1384 serv->conn->account.host);
1386 time (&serv->newgroups_time);
1387 nntp_data.nserv = serv;
1388 nntp_data.group = NULL;
1390 if (mutt_nntp_fetch (&nntp_data, "string_list_t\r\n", msg, NULL, add_group, serv, 0) < 0) {
1394 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1396 nntp_get_desc (&nntp_data, "*", msg, NULL);
1398 for (tmp = serv->list; tmp; tmp = tmp->next) {
1399 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1401 if (data && data->deleted && !data->rc) {
1402 nntp_delete_cache (data);
1403 hash_delete (serv->newsgroups, data->group, NULL, nntp_delete_data);
1407 nntp_save_cache_index (serv);
1409 mutt_clear_error ();
1414 * returns -1 if error ocurred while retrieving header,
1415 * number of articles which ones exist in context on success.
1417 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1421 /* if msgid is already in context, don't reload them */
1422 if (hash_find (ctx->id_hash, msgid))
1424 if (ctx->msgcount == ctx->hdrmax)
1425 mx_alloc_memory (ctx);
1426 ctx->hdrs[ctx->msgcount] = header_new();
1427 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1429 mutt_message (_("Fetching %s from server..."), msgid);
1430 ret = nntp_read_header (ctx, msgid, 0);
1431 /* since nntp_read_header() may set read flag, we must reset it */
1432 ctx->hdrs[ctx->msgcount]->read = 0;
1434 header_delete(&ctx->hdrs[ctx->msgcount]);
1437 mx_update_context (ctx, 1);
1447 unsigned int *child;
1450 static int check_children (char *s, void *c)
1452 #define cc ((CHILD_CONTEXT *) c)
1455 if (!s || (n = atoi (s)) == 0)
1457 for (i = 0; i < cc->ctx->msgcount; i++)
1458 if (cc->ctx->hdrs[i]->article_num == n)
1460 if (cc->num >= cc->max)
1461 p_realloc(&cc->child, cc->max += 25);
1462 cc->child[cc->num++] = n;
1468 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1470 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1472 int i, ret = 0, tmp = 0;
1475 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1476 !nntp_data->nserv->conn->account.host)
1478 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1480 if (!nntp_data->nserv->hasXPAT) {
1481 mutt_error (_("Server %s does not support this operation!"),
1482 nntp_data->nserv->conn->account.host);
1486 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1487 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1492 cc.child = p_new(unsigned int, 25);
1493 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1494 p_delete(&cc.child);
1497 /* dont try to read the xover cache. check_children() already
1498 * made sure that we dont have the article, so we need to visit
1499 * the server. Reading the cache at this point is also bad
1500 * because it would duplicate messages */
1501 if (option (OPTNEWSCACHE)) {
1503 unset_option (OPTNEWSCACHE);
1505 for (i = 0; i < cc.num; i++) {
1506 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1508 if (ctx->msgcount &&
1509 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1510 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1513 set_option (OPTNEWSCACHE);
1514 p_delete(&cc.child);