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.
12 #include <lib-lib/lib-lib.h>
14 #include <lib-mime/mime.h>
15 #include <lib-ui/curses.h>
16 #include <lib-ui/sidebar.h>
17 #include <lib-mx/mx.h>
25 static unsigned int _checked = 0;
27 void nntp_sync_sidebar (NNTP_DATA* data) {
35 snprintf(buf, sizeof (buf), "nntp%s://%s%s%s%s/%s",
36 data->nserv->conn->account.has_ssl ? "s" : "",
37 NONULL(data->nserv->conn->account.user),
38 *data->nserv->conn->account.pass ? ":" : "",
39 *data->nserv->conn->account.pass ? data->nserv->conn->account.pass : "",
40 data->nserv->conn->account.host,
43 /* bail out if group not found via mailboxes */
44 if ((i = buffy_lookup (buf)) < 0)
47 tmp = Incoming.arr[i];
48 /* copied from browser.c */
49 if (option (OPTMARKOLD) &&
50 data->lastCached >= data->firstMessage &&
51 data->lastCached <= data->lastMessage)
52 tmp->msg_unread = data->lastMessage - data->lastCached;
54 tmp->msg_unread = data->unread;
55 tmp->new = data->unread > 0;
56 /* this is closest to a "total" count we can get */
57 tmp->msgcount = data->lastMessage - data->firstMessage;
60 static int nntp_auth (NNTP_SERVER * serv)
62 CONNECTION *conn = serv->conn;
64 unsigned char flags = conn->account.flags;
66 if (mutt_account_getuser(&conn->account) || !conn->account.user[0] ||
67 mutt_account_getpass(&conn->account) || !conn->account.pass[0]) {
68 conn->account.flags = flags;
72 mutt_message _("Logging in...");
74 snprintf (buf, sizeof (buf), "AUTHINFO USER %s\r\n", conn->account.user);
75 mutt_socket_write (conn, buf);
76 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
77 conn->account.flags = flags;
81 snprintf (buf, sizeof (buf), "AUTHINFO PASS %s\r\n", conn->account.pass);
82 mutt_socket_write(conn, buf);
83 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0) {
84 conn->account.flags = flags;
88 if (m_strncmp("281", buf, 3)) {
89 conn->account.flags = flags;
90 mutt_error _("Login failed.");
99 static int nntp_connect_error (NNTP_SERVER * serv)
101 serv->status = NNTP_NONE;
102 mutt_socket_close (serv->conn);
103 mutt_error _("Server closed connection!");
109 static int nntp_connect_and_auth (NNTP_SERVER * serv)
111 CONNECTION *conn = serv->conn;
115 serv->status = NNTP_NONE;
117 if (mutt_socket_open (conn) < 0)
120 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
121 return nntp_connect_error (serv);
123 if (!m_strncmp("200", buf, 3))
124 mutt_message (_("Connected to %s. Posting ok."), conn->account.host);
125 else if (!m_strncmp("201", buf, 3))
126 mutt_message (_("Connected to %s. Posting NOT ok."), conn->account.host);
128 mutt_socket_close(conn);
130 mutt_error("%s", buf);
135 /* Tell INN to switch to mode reader if it isn't so. Ignore all
136 returned codes and messages. */
137 mutt_socket_write (conn, "MODE READER\r\n");
138 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
139 return nntp_connect_error (serv);
141 mutt_socket_write (conn, "STAT\r\n");
142 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
143 return nntp_connect_error (serv);
145 if (!conn->account.has_user && m_strncmp("480", buf, 3)) {
146 serv->status = NNTP_OK;
150 rc = nntp_auth (serv);
152 return nntp_connect_error (serv);
154 mutt_socket_close (conn);
155 serv->status = NNTP_BYE;
159 mutt_socket_close (conn);
160 mutt_error _("Login failed.");
165 serv->status = NNTP_OK;
169 static int nntp_attempt_features (NNTP_SERVER * serv)
171 char buf[LONG_STRING];
172 CONNECTION *conn = serv->conn;
174 if (serv->feat_known)
177 mutt_socket_write (conn, "XOVER\r\n");
178 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
179 return nntp_connect_error (serv);
180 if (m_strncmp("500", buf, 3))
183 mutt_socket_write (conn, "XPAT\r\n");
184 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
185 return nntp_connect_error (serv);
186 if (m_strncmp("500", buf, 3))
189 mutt_socket_write (conn, "LISTGROUP\r\n");
190 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
191 return (nntp_connect_error (serv));
192 if (m_strncmp("500", buf, 3))
193 serv->hasLISTGROUP = 1;
195 mutt_socket_write (conn, "XGTITLE +\r\n");
196 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
197 return nntp_connect_error (serv);
198 if (m_strncmp("500", buf, 3))
199 serv->hasXGTITLE = 1;
201 if (!m_strncmp("282", buf, 3)) {
203 if (mutt_socket_readln (buf, sizeof (buf), conn) < 0)
204 return nntp_connect_error (serv);
205 } while (!(buf[0] == '.' && buf[1] == '\0'));
208 serv->feat_known = 1;
212 static int nntp_open_connection (NNTP_SERVER * serv)
214 if (serv->status == NNTP_OK)
216 if (serv->status == NNTP_BYE)
218 if (nntp_connect_and_auth (serv) < 0)
220 if (nntp_attempt_features (serv) < 0)
225 static int nntp_reconnect (NNTP_SERVER * serv)
229 mutt_socket_close (serv->conn);
232 if (nntp_connect_and_auth (serv) == 0)
235 snprintf (buf, sizeof (buf), _("Connection to %s lost. Reconnect?"),
236 serv->conn->account.host);
237 if (query_quadoption (OPT_NNTPRECONNECT, buf) != M_YES) {
238 serv->status = NNTP_BYE;
244 /* Send data from line[LONG_STRING] and receive answer to same line */
245 static int mutt_nntp_query (NNTP_DATA * data, char *line, size_t linelen)
247 char buf[LONG_STRING];
250 if (data->nserv->status == NNTP_BYE)
255 mutt_socket_write (data->nserv->conn, line);
257 else if (data->group) {
258 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
259 mutt_socket_write (data->nserv->conn, buf);
263 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0) {
264 if (nntp_reconnect (data->nserv) < 0)
268 snprintf (buf, sizeof (buf), "GROUP %s\r\n", data->group);
269 mutt_socket_write (data->nserv->conn, buf);
270 if (mutt_socket_readln (buf, sizeof (buf), data->nserv->conn) < 0)
276 else if ((!m_strncmp("480", buf, 3)) && nntp_auth (data->nserv) < 0)
280 m_strcpy(line, linelen, buf);
285 * This function calls funct(*line, *data) for each received line,
286 * funct(NULL, *data) if rewind(*data) needs, exits when fail or done.
289 * 1 - correct but not performed (may be, have to be continued),
290 * -1 - conection lost,
291 * -2 - invalid command or execution error,
292 * -3 - error in funct(*line, *data).
294 static int mutt_nntp_fetch (NNTP_DATA * nntp_data, const char *query,
295 const char *msg, progress_t* bar,
296 int (*funct) (char *, void *),
297 void *data, int tagged)
299 char buf[LONG_STRING];
308 m_strcpy(buf, sizeof(buf), query);
309 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0)
318 inbuf = p_new(char, sizeof(buf));
321 chunk = mutt_socket_readln(buf, sizeof (buf), nntp_data->nserv->conn);
326 if (!lenbuf && buf[0] == '.') {
327 if (buf[1] == '\0') {
335 m_strcpy(inbuf + lenbuf, sizeof(buf), p);
338 if (chunk >= ssizeof (buf)) {
339 lenbuf += m_strlen(p);
343 mutt_progress_bar (bar, pos);
346 if (ReadInc && (line % ReadInc == 0)) {
348 mutt_message (_("%s (tagged: %d) %d"), msg, tagged, line);
350 mutt_message ("%s %d", msg, line);
354 if (ret == 0 && funct (inbuf, data) < 0)
359 p_realloc(&inbuf, lenbuf + sizeof (buf));
368 static int nntp_read_tempfile (char *line, void *file)
370 FILE *f = (FILE *) file;
376 if (fputc ('\n', f) == EOF)
382 static void nntp_parse_xref (CONTEXT * ctx, char *group, char *xref,
385 register char *p, *b;
386 register char *colon = NULL;
390 /* skip to next word */
392 while (*b && ((*b == ' ') || (*b == '\t')))
396 /* skip to end of word */
397 while (*p && (*p != ' ') && (*p != '\t')) {
409 nntp_get_status (ctx, h, b, atoi (colon));
410 if (h && h->article_num == 0 && m_strcmp(group, b) == 0)
411 h->article_num = atoi (colon);
419 * 1 if article not found
420 * -1 if read or write error on tempfile or socket
422 static int nntp_read_header (CONTEXT * ctx, const char *msgid,
425 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
427 char buf[LONG_STRING];
428 char tempfile[_POSIX_PATH_MAX];
430 HEADER *h = ctx->hdrs[ctx->msgcount];
432 f = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
437 snprintf (buf, sizeof (buf), "HEAD %d\r\n", article_num);
439 snprintf (buf, sizeof (buf), "HEAD %s\r\n", msgid);
441 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, nntp_read_tempfile, f, 0);
445 return (ret == -1 ? -1 : 1);
448 h->article_num = article_num;
449 h->env = mutt_read_rfc822_header (f, h, 0, 0);
453 if (h->env->xref != NULL)
454 nntp_parse_xref (ctx, nntp_data->group, h->env->xref, h);
455 else if (h->article_num == 0 && msgid) {
456 snprintf (buf, sizeof (buf), "STAT %s\r\n", msgid);
457 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) == 0)
458 h->article_num = atoi (buf + 4);
464 static int parse_description (char *line, void *n)
466 register char *d = line;
468 NNTP_SERVER *news = n;
472 while (*d && *d != '\t' && *d != ' ')
476 while (*d && (*d == '\t' || *d == ' '))
478 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, line)) != NULL &&
479 m_strcmp(d, data->desc)) {
480 p_delete(&data->desc);
481 data->desc = m_strdup(d);
486 static void nntp_get_desc (NNTP_DATA * data, const char *mask, char *msg, progress_t* bar)
490 if (!option (OPTLOADDESC) || !data || !data->nserv)
493 /* Get newsgroup description, if we can */
494 if (data->nserv->hasXGTITLE)
495 snprintf (buf, sizeof (buf), "XGTITLE %s\r\n", mask);
497 snprintf (buf, sizeof (buf), "LIST NEWSGROUPS %s\r\n", mask);
498 if (mutt_nntp_fetch (data, buf, msg, bar, parse_description, data->nserv, 0) !=
504 * XOVER returns a tab separated list of:
505 * id|subject|from|date|Msgid|references|bytes|lines|xref
507 * This has to duplicate some of the functionality of
508 * mutt_read_rfc822_header(), since it replaces the call to that (albeit with
509 * a limited number of headers which are "parsed" by placement in the list)
511 static int nntp_parse_xover (CONTEXT * ctx, char *buf, HEADER * hdr)
513 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
517 hdr->env = envelope_new();
518 hdr->env->newsgroups = m_strdup(nntp_data->group);
519 hdr->content = body_new();
520 hdr->content->type = TYPETEXT;
521 hdr->content->subtype = m_strdup("plain");
522 hdr->content->encoding = ENC7BIT;
523 hdr->content->disposition = DISPINLINE;
524 hdr->content->length = -1;
527 for (x = 0; !done && x < 9; x++) {
528 /* if from file, need to skip newline character */
529 while (*p && *p != '\n' && *p != '\t')
538 hdr->article_num = atoi (b);
539 nntp_get_status (ctx, hdr, NULL, hdr->article_num);
542 hdr->env->subject = m_strdup(b);
545 address_list_wipe(&hdr->env->from);
546 hdr->env->from = rfc822_parse_adrlist (hdr->env->from, b);
547 /* same as for mutt_parse_rfc822_line():
548 * don't leave from info NULL if there's an invalid address (or
549 * whatever) in From: field; mutt would just display it as empty
550 * and mark mail/(esp.) news article as your own. aaargh! this
551 * bothered me for _years_ */
552 if (!hdr->env->from) {
553 hdr->env->from = address_new ();
554 hdr->env->from->personal = m_strdup(b);
558 hdr->date_sent = mutt_parse_date (b, hdr);
559 hdr->received = hdr->date_sent;
562 p_delete(&hdr->env->message_id);
563 hdr->env->message_id = m_strdup(b);
566 string_list_wipe(&hdr->env->references);
567 hdr->env->references = mutt_parse_references (b, 0);
570 hdr->content->length = atoi (b);
573 hdr->lines = atoi (b);
577 p_delete(&hdr->env->xref);
578 b = b + 6; /* skips the "Xref: " */
579 hdr->env->xref = m_strdup(b);
580 nntp_parse_xref (ctx, nntp_data->group, b, hdr);
582 rfc2047_decode_envelope(hdr->env);
598 static int nntp_fetch_numbers (char *line, void *c)
600 FETCH_CONTEXT *fc = c;
603 int num = atoi(line);
604 if (num < fc->first || num > fc->last)
607 bit_set(&fc->messages, num);
612 static int add_xover_line (char *line, void *c)
615 FETCH_CONTEXT *fc = c;
616 CONTEXT *ctx = fc->ctx;
617 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
622 if (ctx->msgcount >= ctx->hdrmax)
623 mx_alloc_memory (ctx);
624 ctx->hdrs[ctx->msgcount] = header_new();
625 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
627 nntp_parse_xover (ctx, line, ctx->hdrs[ctx->msgcount]);
628 num = ctx->hdrs[ctx->msgcount]->article_num;
630 if (num >= fc->first && num <= fc->last) {
632 if (num > data->lastLoaded)
633 data->lastLoaded = num;
634 num = num - fc->first + 1;
635 total = fc->last - fc->first + 1;
636 if (!ctx->quiet && fc->msg && ReadInc && (num % ReadInc == 0))
637 mutt_message ("%s %d/%d", fc->msg, num, total);
639 header_delete(&ctx->hdrs[ctx->msgcount]); /* skip it */
646 static int nntp_fetch_headers(CONTEXT * ctx, int first, int last)
648 char buf[HUGE_STRING];
649 const char *msg = _("Fetching message headers...");
650 const char *msg2 = _("Fetching headers from cache...");
651 NNTP_DATA *nntp_data = ((NNTP_DATA *) ctx->data);
652 int ret, num, oldmsgcount, current;
656 /* if empty group or nothing to do */
657 if (!last || first > last)
660 /* fetch list of articles */
661 mutt_message _("Fetching list of articles...");
667 /* CACHE: must be loaded xover cache here */
668 num = nntp_data->lastCached - first + 1;
669 if (option (OPTNEWSCACHE) && nntp_data->cache && num > 0) {
670 nntp_cache_expand (buf, nntp_data->cache);
673 if ((f = safe_fopen (buf, "r"))) {
676 /* counting number of lines */
677 while (fgets (buf, sizeof (buf), f) != NULL)
680 while (r > num && fgets (buf, sizeof (buf), f) != NULL)
682 oldmsgcount = ctx->msgcount;
684 fc.last = first + num - 1;
686 while (fgets (buf, sizeof (buf), f) != NULL) {
687 if (ReadInc && ((++c) % ReadInc == 0))
688 mutt_message ("%s %d/%d", msg2, c, r);
689 add_xover_line (buf, &fc);
692 nntp_data->lastLoaded = fc.last;
694 if (ctx->msgcount > oldmsgcount)
695 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
698 nntp_delete_cache (nntp_data);
700 num = last - first + 1;
706 * Without XOVER, we have to fetch each article header and parse
707 * it. With XOVER, we ask for all of them
710 if (nntp_data->nserv->hasXOVER) {
711 oldmsgcount = ctx->msgcount;
715 snprintf (buf, sizeof (buf), "XOVER %d-%d\r\n", first, last);
716 ret = mutt_nntp_fetch (nntp_data, buf, NULL, NULL, add_xover_line, &fc, 0);
717 if (ctx->msgcount > oldmsgcount)
718 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
720 mutt_error (_("XOVER command failed: %s"), buf);
724 bits_init(&fc.messages);
726 if (nntp_data->nserv->hasLISTGROUP) {
727 snprintf (buf, sizeof (buf), "LISTGROUP %s\r\n", nntp_data->group);
728 if (mutt_nntp_fetch(nntp_data, buf, NULL, NULL,
729 nntp_fetch_numbers, &fc, 0))
731 mutt_error (_("LISTGROUP command failed: %s"), buf);
733 bits_wipe(&fc.messages);
737 for (num = first; num <= last; num++)
738 bit_set(&fc.messages, num);
741 for (current = first; current <= last; current++) {
744 ret = current - first + 1;
745 mutt_message ("%s %d/%d", msg, ret, num);
747 if (!bit_isset(&fc.messages, current))
750 if (ctx->msgcount >= ctx->hdrmax)
751 mx_alloc_memory (ctx);
752 h = ctx->hdrs[ctx->msgcount] = header_new();
753 h->index = ctx->msgcount;
755 ret = nntp_read_header (ctx, NULL, current);
756 if (ret == 0) { /* Got article. Fetch next header */
757 nntp_get_status (ctx, h, NULL, h->article_num);
759 mx_update_context (ctx, 1);
762 header_delete(&h); /* skip it */
764 bits_wipe(&fc.messages);
768 if (current > nntp_data->lastLoaded)
769 nntp_data->lastLoaded = current;
771 bits_wipe(&fc.messages);
774 nntp_data->lastLoaded = last;
780 * currently, nntp "mailbox" is "newsgroup"
782 static int nntp_open_mailbox (CONTEXT * ctx)
784 NNTP_DATA *nntp_data;
786 char buf[HUGE_STRING];
787 char server[LONG_STRING];
788 int count = 0, first;
793 if (nntp_parse_url (ctx->path, &act, buf, sizeof (buf)) < 0 || !*buf) {
794 mutt_error (_("%s is an invalid newsgroup specification!"), ctx->path);
800 nntp_expand_path (server, sizeof (server), &act);
801 if (!(serv = mutt_select_newsserver (server)) || serv->status != NNTP_OK)
804 CurrentNewsSrv = serv;
806 /* create NNTP-specific state struct if nof found in list */
807 if ((nntp_data = (NNTP_DATA *) hash_find (serv->newsgroups, buf)) == NULL) {
808 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(buf) + 1);
809 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
810 strcpy (nntp_data->group, buf);
811 hash_insert (serv->newsgroups, nntp_data->group, nntp_data);
812 nntp_add_to_list (serv, nntp_data);
814 ctx->data = nntp_data;
815 nntp_data->nserv = serv;
817 mutt_message (_("Selecting %s..."), nntp_data->group);
819 if (!nntp_data->desc) {
820 nntp_get_desc (nntp_data, nntp_data->group, NULL, NULL);
822 nntp_save_cache_index (serv);
826 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
830 if (m_strncmp("211", buf, 3)) {
831 string_list_t *l = serv->list;
833 /* GROUP command failed */
834 if (!m_strncmp("411", buf, 3)) {
835 mutt_error (_("Newsgroup %s not found on server %s"),
836 nntp_data->group, serv->conn->account.host);
838 /* CACHE: delete cache and line from .index */
839 nntp_delete_cache (nntp_data);
840 hash_remove (serv->newsgroups, nntp_data->group, NULL,
842 while (l && l->data != (void *) nntp_data)
853 sscanf (buf + 4, "%d %u %u %s", &count, &nntp_data->firstMessage,
854 &nntp_data->lastMessage, buf);
856 nntp_data->deleted = 0;
858 time (&serv->check_time);
861 * Check for max adding context. If it is greater than $nntp_context,
862 * strip off extra articles
864 first = nntp_data->firstMessage;
865 if (NntpContext && nntp_data->lastMessage - first + 1 > NntpContext)
866 first = nntp_data->lastMessage - NntpContext + 1;
868 nntp_data->lastLoaded = first - 1;
869 return nntp_fetch_headers (ctx, first, nntp_data->lastMessage);
872 int nntp_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
874 char buf[LONG_STRING];
875 char path[_POSIX_PATH_MAX];
880 /* see if we already have the message in our cache */
882 &((NNTP_DATA *) ctx->data)->acache[ctx->hdrs[msgno]->index %
885 /* if everything is fine, assign msg->fp and return */
886 if (cache->path && cache->index == ctx->hdrs[msgno]->index &&
887 (msg->fp = fopen (cache->path, "r")))
890 /* clear the previous entry */
891 unlink (cache->path);
892 p_delete(&cache->path);
894 cache->index = ctx->hdrs[msgno]->index;
895 msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
899 cache->path = m_strdup(path);
901 if (ctx->hdrs[msgno]->article_num == 0)
902 snprintf (buf, sizeof (buf), "ARTICLE %s\r\n",
903 ctx->hdrs[msgno]->env->message_id);
905 snprintf (buf, sizeof (buf), "ARTICLE %d\r\n",
906 ctx->hdrs[msgno]->article_num);
908 bar.msg = _("Fetching message...");
910 mutt_progress_bar (&bar, 0);
912 ret = mutt_nntp_fetch ((NNTP_DATA *) ctx->data, buf, NULL, &bar, nntp_read_tempfile,
913 msg->fp, ctx->tagged);
915 mutt_error (_("Article %d not found on server"),
916 ctx->hdrs[msgno]->article_num);
922 p_delete(&cache->path);
926 envelope_delete(&ctx->hdrs[msgno]->env);
927 ctx->hdrs[msgno]->env =
928 mutt_read_rfc822_header (msg->fp, ctx->hdrs[msgno], 0, 0);
929 /* fix content length */
930 fseeko (msg->fp, 0, SEEK_END);
931 ctx->hdrs[msgno]->content->length = ftello (msg->fp) -
932 ctx->hdrs[msgno]->content->offset;
934 /* this is called in mutt before the open which fetches the message,
935 * which is probably wrong, but we just call it again here to handle
936 * the problem instead of fixing it.
938 mutt_parse_mime_message (ctx, ctx->hdrs[msgno]);
940 /* These would normally be updated in mx_update_context(), but the
941 * full headers aren't parsed with XOVER, so the information wasn't
944 ctx->hdrs[msgno]->security = crypt_query (ctx->hdrs[msgno]->content);
953 int nntp_post (const char *msg)
955 char buf[LONG_STRING];
958 NNTP_DATA *nntp_data;
960 if (Context && Context->magic == M_NNTP)
961 nntp_data = (NNTP_DATA *) Context->data;
963 if (!(CurrentNewsSrv = mutt_select_newsserver (NewsServer)) ||
964 !CurrentNewsSrv->list || !CurrentNewsSrv->list->data) {
965 mutt_error (_("Can't post article. No connection to news server."));
968 nntp_data = (NNTP_DATA *) CurrentNewsSrv->list->data;
971 if (!(f = safe_fopen (msg, "r"))) {
972 mutt_error (_("Can't post article. Unable to open %s"), msg);
976 m_strcpy(buf, sizeof(buf), "POST\r\n");
977 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
978 mutt_error (_("Can't post article. Connection to %s lost."),
979 nntp_data->nserv->conn->account.host);
983 mutt_error (_("Can't post article: %s"), buf);
989 while (fgets (buf + 1, sizeof (buf) - 2, f) != NULL) {
991 if (buf[len - 1] == '\n') {
998 mutt_socket_write(nntp_data->nserv->conn, buf);
1000 mutt_socket_write(nntp_data->nserv->conn, buf + 1);
1004 if (buf[m_strlen(buf) - 1] != '\n')
1005 mutt_socket_write(nntp_data->nserv->conn, "\r\n");
1006 mutt_socket_write(nntp_data->nserv->conn, ".\r\n");
1007 if (mutt_socket_readln (buf, sizeof (buf), nntp_data->nserv->conn) < 0) {
1008 mutt_error (_("Can't post article. Connection to %s lost."),
1009 nntp_data->nserv->conn->account.host);
1012 if (buf[0] != '2') {
1013 mutt_error (_("Can't post article: %s"), buf);
1020 /* nntp_logout_all: close all open connections. */
1021 void nntp_logout_all (void)
1023 char buf[LONG_STRING];
1026 conn = mutt_socket_head ();
1029 CONNECTION* next = conn->next;
1030 if (conn->account.type == M_ACCT_TYPE_NNTP) {
1031 mutt_message (_("Closing connection to %s..."), conn->account.host);
1032 mutt_socket_write (conn, "QUIT\r\n");
1033 mutt_socket_readln (buf, sizeof (buf), conn);
1034 mutt_clear_error ();
1035 mutt_socket_close (conn);
1036 mutt_socket_free (conn);
1042 static void nntp_free_acache (NNTP_DATA * data)
1046 for (i = 0; i < NNTP_CACHE_LEN; i++) {
1047 if (data->acache[i].path) {
1048 unlink (data->acache[i].path);
1049 p_delete(&data->acache[i].path);
1054 void nntp_delete_data (void *p)
1056 NNTP_DATA *data = (NNTP_DATA *)p;
1060 p_delete(&data->entries);
1061 p_delete(&data->desc);
1062 p_delete(&data->cache);
1063 nntp_free_acache (data);
1067 static int nntp_sync_mailbox (CONTEXT * ctx, int unused1, int* unused2)
1069 NNTP_DATA *data = ctx->data;
1071 /* CACHE: update cache and .index files */
1072 if ((option (OPTSAVEUNSUB) || data->subscribed))
1073 nntp_save_cache_group (ctx);
1074 nntp_free_acache (data);
1076 data->nserv->check_time = 0; /* next nntp_check_mailbox() will really check */
1080 static void nntp_fastclose_mailbox (CONTEXT * ctx)
1082 NNTP_DATA *data = (NNTP_DATA *) ctx->data, *tmp;
1086 nntp_free_acache (data);
1087 if (!data->nserv || !data->nserv->newsgroups || !data->group)
1089 nntp_save_cache_index (data->nserv);
1090 if ((tmp = hash_find (data->nserv->newsgroups, data->group)) == NULL
1092 nntp_delete_data (data);
1094 nntp_sync_sidebar (data);
1097 /* commit changes and terminate connection */
1098 int nntp_close_mailbox (CONTEXT * ctx)
1102 mutt_message _("Quitting newsgroup...");
1105 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
1108 if (data->nserv && data->nserv->conn && ctx->unread) {
1109 ret = query_quadoption (OPT_CATCHUP, _("Mark all articles read?"));
1111 mutt_newsgroup_catchup (data->nserv, data->group);
1116 nntp_sync_mailbox (ctx, 0, NULL);
1117 if (ctx->data && ((NNTP_DATA *) ctx->data)->nserv) {
1120 news = ((NNTP_DATA *) ctx->data)->nserv;
1121 newsrc_gen_entries (ctx);
1122 ((NNTP_DATA *) ctx->data)->unread = ctx->unread;
1123 mutt_newsrc_update (news);
1125 mutt_clear_error ();
1129 /* use the GROUP command to poll for new mail */
1130 static int _nntp_check_mailbox (CONTEXT * ctx, NNTP_DATA * nntp_data)
1132 char buf[LONG_STRING];
1135 if (nntp_data->nserv->check_time + NewsPollTimeout > time (NULL))
1139 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1142 if (m_strncmp("211", buf, 3)) {
1144 if (mutt_nntp_query (nntp_data, buf, sizeof (buf)) < 0) {
1148 if (!m_strncmp("211", buf, 3)) {
1152 sscanf (buf + 4, "%d %d %d", &count, &first, &last);
1153 nntp_data->firstMessage = first;
1154 nntp_data->lastMessage = last;
1155 if (ctx && last > nntp_data->lastLoaded) {
1156 nntp_fetch_headers (ctx, nntp_data->lastLoaded + 1, last);
1157 time (&nntp_data->nserv->check_time);
1160 if (!last || (!nntp_data->rc && !nntp_data->lastCached))
1161 nntp_data->unread = count;
1163 mutt_newsgroup_stat (nntp_data);
1164 /* active was renumbered? */
1165 if (last < nntp_data->lastLoaded) {
1166 if (!nntp_data->max) {
1167 nntp_data->entries = p_new(NEWSRC_ENTRY, 5);
1170 nntp_data->lastCached = 0;
1172 nntp_data->entries[0].first = 1;
1173 nntp_data->entries[0].last = 0;
1175 nntp_sync_sidebar (nntp_data);
1178 time (&nntp_data->nserv->check_time);
1182 static int nntp_check_mailbox (CONTEXT * ctx, int* unused1, int unused2)
1184 return _nntp_check_mailbox (ctx, (NNTP_DATA *) ctx->data);
1187 static int add_group (char *buf, void *serv)
1189 NNTP_SERVER *s = serv;
1190 char group[LONG_STRING], mod, desc[HUGE_STRING];
1192 NNTP_DATA *nntp_data;
1195 _checked = n; /* _checked have N, where N = number of groups */
1196 if (!buf) /* at EOF must be zerouth */
1203 sscanf (buf, "%s %d %d %c %[^\n]", group, &last, &first, &mod, desc);
1206 if ((nntp_data = (NNTP_DATA *) hash_find (s->newsgroups, group)) == NULL) {
1208 nntp_data = xmalloc(sizeof(NNTP_DATA) + m_strlen(group) + 1);
1209 nntp_data->group = (char *) nntp_data + sizeof (NNTP_DATA);
1210 strcpy (nntp_data->group, group);
1211 nntp_data->nserv = s;
1212 if (s->newsgroups->nelem < s->newsgroups->curnelem * 2)
1213 hash_resize (s->newsgroups, s->newsgroups->nelem * 2);
1214 hash_insert (s->newsgroups, nntp_data->group, nntp_data);
1215 nntp_add_to_list (s, nntp_data);
1217 nntp_data->deleted = 0;
1218 nntp_data->firstMessage = first;
1219 nntp_data->lastMessage = last;
1221 nntp_data->allowed = 1;
1223 nntp_data->allowed = 0;
1224 if (nntp_data->desc)
1225 p_delete(&nntp_data->desc);
1227 nntp_data->desc = m_strdup(desc);
1228 if (nntp_data->rc || nntp_data->lastCached)
1229 mutt_newsgroup_stat (nntp_data);
1230 else if (nntp_data->lastMessage &&
1231 nntp_data->firstMessage <= nntp_data->lastMessage)
1232 nntp_data->unread = nntp_data->lastMessage - nntp_data->firstMessage + 1;
1234 nntp_data->unread = 0;
1239 int nntp_check_newgroups (NNTP_SERVER * serv, int force)
1241 char buf[LONG_STRING];
1242 NNTP_DATA nntp_data;
1248 if (!serv || !serv->newgroups_time)
1251 if (nntp_open_connection (serv) < 0)
1254 /* check subscribed groups for new news */
1255 if (option (OPTSHOWNEWNEWS)) {
1256 mutt_message _("Checking for new messages...");
1258 for (l = serv->list; l; l = l->next) {
1259 serv->check_time = 0; /* really check! */
1260 if (l->data && ((NNTP_DATA *) l->data)->subscribed)
1261 _nntp_check_mailbox (NULL, (NNTP_DATA *) l->data);
1268 mutt_message _("Checking for new newsgroups...");
1270 now = serv->newgroups_time;
1271 time (&serv->newgroups_time);
1273 snprintf (buf, sizeof (buf), "NEWGROUPS %02d%02d%02d %02d%02d%02d GMT\r\n",
1274 (t->tm_year % 100), t->tm_mon + 1, t->tm_mday, t->tm_hour,
1275 t->tm_min, t->tm_sec);
1276 nntp_data.nserv = serv;
1277 if (Context && Context->magic == M_NNTP)
1278 nntp_data.group = ((NNTP_DATA *) Context->data)->group;
1280 nntp_data.group = NULL;
1282 if (mutt_nntp_fetch (&nntp_data, buf, _("Adding new newsgroups..."), NULL,
1283 add_group, serv, 0) != 0) {
1287 mutt_message _("Loading descriptions...");
1292 emp.next = serv->list;
1296 ((NNTP_DATA *) l->data)->new = 1;
1297 nntp_get_desc ((NNTP_DATA *) l->data, ((NNTP_DATA *) l->data)->group,
1301 nntp_save_cache_index (serv);
1302 mutt_clear_error ();
1306 /* Load list of all newsgroups from cache ALL */
1307 int nntp_get_cache_all (NNTP_SERVER * serv)
1309 char buf[HUGE_STRING];
1312 nntp_cache_expand (buf, serv->cache);
1313 if ((f = safe_fopen (buf, "r"))) {
1316 while (fgets (buf, sizeof (buf), f) != NULL) {
1317 if (ReadInc && (i % ReadInc == 0))
1318 mutt_message (_("Loading list from cache... %d"), i);
1319 add_group (buf, serv);
1322 add_group (NULL, NULL);
1324 mutt_clear_error ();
1328 p_delete(&serv->cache);
1333 /* Load list of all newsgroups from active */
1334 int nntp_get_active (NNTP_SERVER * serv)
1337 NNTP_DATA nntp_data;
1340 if (nntp_open_connection (serv) < 0)
1343 snprintf (msg, sizeof (msg),
1344 _("Loading list of all newsgroups on server %s..."),
1345 serv->conn->account.host);
1347 time (&serv->newgroups_time);
1348 nntp_data.nserv = serv;
1349 nntp_data.group = NULL;
1351 if (mutt_nntp_fetch (&nntp_data, "LIST\r\n", msg, NULL, add_group, serv, 0) < 0) {
1355 m_strcpy(msg, sizeof(msg), _("Loading descriptions..."));
1357 nntp_get_desc (&nntp_data, "*", msg, NULL);
1359 for (tmp = serv->list; tmp; tmp = tmp->next) {
1360 NNTP_DATA *data = (NNTP_DATA *) tmp->data;
1362 if (data && data->deleted && !data->rc) {
1363 nntp_delete_cache (data);
1364 hash_remove (serv->newsgroups, data->group, NULL, nntp_delete_data);
1368 nntp_save_cache_index (serv);
1370 mutt_clear_error ();
1375 * returns -1 if error ocurred while retrieving header,
1376 * number of articles which ones exist in context on success.
1378 int nntp_check_msgid (CONTEXT * ctx, const char *msgid)
1382 /* if msgid is already in context, don't reload them */
1383 if (hash_find (ctx->id_hash, msgid))
1385 if (ctx->msgcount == ctx->hdrmax)
1386 mx_alloc_memory (ctx);
1387 ctx->hdrs[ctx->msgcount] = header_new();
1388 ctx->hdrs[ctx->msgcount]->index = ctx->msgcount;
1390 mutt_message (_("Fetching %s from server..."), msgid);
1391 ret = nntp_read_header (ctx, msgid, 0);
1392 /* since nntp_read_header() may set read flag, we must reset it */
1393 ctx->hdrs[ctx->msgcount]->read = 0;
1395 header_delete(&ctx->hdrs[ctx->msgcount]);
1398 mx_update_context (ctx, 1);
1411 static int check_children (char *s, void *c)
1413 CHILD_CONTEXT *cc = c;
1416 if (!s || (n = atoi (s)) == 0)
1418 for (i = 0; i < cc->ctx->msgcount; i++)
1419 if (cc->ctx->hdrs[i]->article_num == n)
1421 if (cc->num >= cc->max)
1422 p_realloc(&cc->child, cc->max += 25);
1423 cc->child[cc->num++] = n;
1428 int nntp_check_children (CONTEXT * ctx, const char *msgid)
1430 NNTP_DATA *nntp_data = (NNTP_DATA *) ctx->data;
1432 int i, ret = 0, tmp = 0;
1435 if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->conn ||
1436 !nntp_data->nserv->conn->account.host)
1438 if (nntp_data->firstMessage > nntp_data->lastLoaded)
1440 if (!nntp_data->nserv->hasXPAT) {
1441 mutt_error (_("Server %s does not support this operation!"),
1442 nntp_data->nserv->conn->account.host);
1446 snprintf (buf, sizeof (buf), "XPAT References %d-%d *%s*\r\n",
1447 nntp_data->firstMessage, nntp_data->lastLoaded, msgid);
1452 cc.child = p_new(int, 25);
1453 if (mutt_nntp_fetch (nntp_data, buf, NULL, NULL, check_children, &cc, 0)) {
1454 p_delete(&cc.child);
1457 /* dont try to read the xover cache. check_children() already
1458 * made sure that we dont have the article, so we need to visit
1459 * the server. Reading the cache at this point is also bad
1460 * because it would duplicate messages */
1461 if (option (OPTNEWSCACHE)) {
1463 unset_option (OPTNEWSCACHE);
1465 for (i = 0; i < cc.num; i++) {
1466 if ((ret = nntp_fetch_headers (ctx, cc.child[i], cc.child[i])))
1468 if (ctx->msgcount &&
1469 ctx->hdrs[ctx->msgcount - 1]->article_num == cc.child[i])
1470 ctx->hdrs[ctx->msgcount - 1]->read = 0;
1473 set_option (OPTNEWSCACHE);
1474 p_delete(&cc.child);
1478 static int nntp_is_magic (const char* path, struct stat* st) {
1479 url_scheme_t s = url_check_scheme (NONULL (path));
1480 return ((s == U_NNTP || s == U_NNTPS) ? M_NNTP : -1);
1483 static int acl_check_nntp (CONTEXT* ctx, int bit) {
1485 case ACL_INSERT: /* editing messages */
1486 case ACL_WRITE: /* change importance */
1488 case ACL_DELETE: /* (un)deletion */
1489 case ACL_SEEN: /* mark as read */
1496 mx_t const nntp_mx = {
1506 nntp_fastclose_mailbox,