2 * Copyright (C) 1998 Brandon Long <blong@fiction.net>
3 * Copyright (C) 1999 Andrej Gritsenko <andrej@lucky.net>
4 * Copyright (C) 2000-2001 Vsevolod Volkov <vvv@mutt.org.ua>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "mutt_curses.h"
42 void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d)
49 l = safe_calloc (1, sizeof (LIST));
58 static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
61 char group[LONG_STRING];
63 char *p = line, *b, *h;
72 while (*p && (*p != ':' && *p != '!'))
77 if (len > sizeof (group))
79 strfcpy (group, line, len);
80 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) {
82 (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
83 data->group = (char *) data + sizeof (NNTP_DATA);
84 strcpy (data->group, group);
87 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
89 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
90 hash_insert (news->newsgroups, data->group, data, 0);
91 nntp_add_to_list (news, data);
94 FREE ((void **) &data->entries);
97 data->entries = safe_calloc (x * 2, sizeof (NEWSRC_ENTRY));
101 data->subscribed = 1;
103 data->subscribed = 0;
109 while (*p && *p != ',' && *p != '\n')
115 if ((h = strchr (b, '-'))) {
118 data->entries[x].first = atoi (b);
119 data->entries[x].last = atoi (h);
122 data->entries[x].first = atoi (b);
123 data->entries[x].last = data->entries[x].first;
126 if (data->entries[x].last != 0)
129 if (x && !data->lastMessage)
130 data->lastMessage = data->entries[x - 1].last;
132 mutt_newsgroup_stat (data);
133 dprint (2, (debugfile, "parse_line: Newsgroup %s\n", data->group));
138 static int slurp_newsrc (NNTP_SERVER * news)
144 news->stat = stat (news->newsrc, &sb);
145 news->size = sb.st_size;
146 news->mtime = sb.st_mtime;
148 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
150 /* hmm, should we use dotlock? */
151 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1)) {
156 buf = safe_malloc (sb.st_size + 1);
157 while (fgets (buf, sb.st_size + 1, fp))
158 nntp_parse_newsrc_line (news, buf);
161 mx_unlock_file (news->newsrc, fileno (fp), 0);
166 void nntp_cache_expand (char *dst, const char *src)
168 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
169 mutt_expand_path (dst, _POSIX_PATH_MAX);
172 /* Loads $news_cache_dir/.index into memory, loads newsserver data
173 * and newsgroup cache names */
174 static int nntp_parse_cacheindex (NNTP_SERVER * news)
177 char buf[HUGE_STRING], *cp;
178 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
183 /* check is server name defined or not */
184 if (!news || !news->conn || !news->conn->account.host)
186 unset_option (OPTNEWSCACHE);
187 if (!NewsCacheDir || !*NewsCacheDir)
190 strfcpy (dir, NewsCacheDir, sizeof (dir));
191 mutt_expand_path (dir, sizeof (dir));
193 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
194 snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"),
196 if (mutt_yesorno (buf, M_YES) != M_YES
197 || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) {
198 mutt_error _("Cache directory not created!");
205 set_option (OPTNEWSCACHE);
208 snprintf (buf, sizeof (buf), "%s/.index", dir);
209 if (!(index = safe_fopen (buf, "a+")))
212 while (fgets (buf, sizeof (buf), index)) {
213 buf[strlen (buf) - 1] = 0; /* strip ending '\n' */
214 if (!mutt_strncmp (buf, "#: ", 3) &&
215 !mutt_strcasecmp (buf + 3, news->conn->account.host))
218 while (fgets (buf, sizeof (buf), index)) {
220 while (*cp && *cp != ' ')
225 if (!mutt_strcmp (buf, "#:"))
227 sscanf (cp + 1, "%s %d %d", file, &l, &m);
228 if (!mutt_strcmp (buf, "ALL")) {
229 news->cache = safe_strdup (file);
230 news->newgroups_time = m;
232 else if (news->newsgroups) {
233 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, buf)) == NULL) {
235 (NNTP_DATA *) safe_calloc (1,
236 sizeof (NNTP_DATA) + strlen (buf) + 1);
237 data->group = (char *) data + sizeof (NNTP_DATA);
238 strcpy (data->group, buf);
241 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
243 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
244 hash_insert (news->newsgroups, data->group, data, 0);
245 nntp_add_to_list (news, data);
247 data->cache = safe_strdup (file);
249 if (!data->firstMessage || data->lastMessage < m)
251 if (!data->firstMessage)
252 data->firstMessage = l;
253 if (data->lastMessage < m)
254 data->lastMessage = m;
255 data->lastCached = m;
256 if (t || !data->unread)
257 mutt_newsgroup_stat (data);
264 const char *nntp_format_str (char *dest, size_t destlen, char op,
265 const char *src, const char *fmt,
266 const char *ifstring, const char *elsestring,
267 unsigned long data, format_flag flags)
269 char fn[SHORT_STRING], tmp[SHORT_STRING];
273 strncpy (fn, NewsServer, sizeof (fn) - 1);
275 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
276 snprintf (dest, destlen, tmp, fn);
282 /* nntp_parse_url: given an NNPT URL, return host, port,
283 * username, password and newsgroup will recognise. */
284 int nntp_parse_url (const char *server, ACCOUNT * acct,
285 char *group, size_t group_len)
293 acct->port = NNTP_PORT;
294 acct->type = M_ACCT_TYPE_NNTP;
296 c = safe_strdup (server);
297 url_parse_ciss (&url, c);
299 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
300 if (url.scheme == U_NNTPS) {
301 acct->flags |= M_ACCT_SSL;
302 acct->port = NNTP_SSL_PORT;
307 strfcpy (group, url.path, group_len);
309 ret = mutt_account_fromurl (acct, &url);
316 void nntp_expand_path (char *line, size_t len, ACCOUNT * acct)
320 url.path = safe_strdup (line);
321 mutt_account_tourl (acct, &url);
322 url_ciss_tostring (&url, line, len, 0);
327 * Automatically loads a newsrc into memory, if necessary.
328 * Checks the size/mtime of a newsrc file, if it doesn't match, load
329 * again. Hmm, if a system has broken mtimes, this might mean the file
330 * is reloaded every time, which we'd have to fix.
332 * a newsrc file is a line per newsgroup, with the newsgroup, then a
333 * ':' denoting subscribed or '!' denoting unsubscribed, then a
334 * comma separated list of article numbers and ranges.
336 NNTP_SERVER *mutt_select_newsserver (char *server)
338 char file[_POSIX_PATH_MAX];
345 if (!server || !*server) {
346 mutt_error _("No newsserver defined!");
351 buf = p = safe_calloc (strlen (server) + 10, sizeof (char));
352 if (url_check_scheme (server) == U_UNKNOWN) {
353 strcpy (buf, "nntp://");
354 p = strchr (buf, '\0');
358 if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file) {
360 mutt_error (_("%s is an invalid newsserver specification!"), server);
365 conn = mutt_conn_find (NULL, &acct);
369 mutt_FormatString (file, sizeof (file), NONULL (NewsRc), nntp_format_str, 0,
371 mutt_expand_path (file, sizeof (file));
373 serv = (NNTP_SERVER *) conn->data;
377 /* externally modified? */
378 if (serv->stat != stat (file, &sb) || (!serv->stat &&
379 (serv->size != sb.st_size
380 || serv->mtime != sb.st_mtime))) {
381 for (list = serv->list; list; list = list->next) {
382 NNTP_DATA *data = (NNTP_DATA *) list->data;
385 data->subscribed = 0;
391 nntp_clear_cacheindex (serv);
394 if (serv->status == NNTP_BYE)
395 serv->status = NNTP_NONE;
396 nntp_check_newgroups (serv, 0);
401 serv = safe_calloc (1, sizeof (NNTP_SERVER));
403 serv->newsrc = safe_strdup (file);
404 serv->newsgroups = hash_create (1009);
405 slurp_newsrc (serv); /* load .newsrc */
406 nntp_parse_cacheindex (serv); /* load .index */
407 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
408 nntp_check_newgroups (serv, 1);
409 else if (nntp_get_active (serv) < 0) {
410 hash_destroy (&serv->newsgroups, nntp_delete_data);
411 for (list = serv->list; list; list = list->next)
413 mutt_free_list (&serv->list);
414 FREE (&serv->newsrc);
419 nntp_clear_cacheindex (serv);
420 conn->data = (void *) serv;
426 * full status flags are not supported by nntp, but we can fake some
427 * of them. This is how:
428 * Read = a read message number is in the .newsrc
429 * New = a message is new since we last read this newsgroup
430 * Old = anything else
431 * So, Read is marked as such in the newsrc, old is anything that is
432 * "skipped" in the newsrc, and new is anything not in the newsrc nor
433 * in the cache. By skipped, I mean before the last unread message
435 void nntp_get_status (CONTEXT * ctx, HEADER * h, char *group, int article)
437 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
441 data = (NNTP_DATA *) hash_find (data->nserv->newsgroups, group);
446 dprint (3, (debugfile, "newsgroup %s not found\n", group));
451 for (x = 0; x < data->num; x++) {
452 if ((article >= data->entries[x].first) &&
453 (article <= data->entries[x].last)) {
454 /* we cannot use mutt_set_flag() because mx_update_context()
460 /* If article was not cached yet, it is new! :) */
461 if (!data->cache || article > data->lastCached)
463 /* Old articles are articles which aren't read but an article after them
465 if (option (OPTMARKOLD))
469 void mutt_newsgroup_stat (NNTP_DATA * data)
472 unsigned int first, last;
475 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
478 data->unread = data->lastMessage - data->firstMessage + 1;
479 for (i = 0; i < data->num; i++) {
480 first = data->entries[i].first;
481 if (first < data->firstMessage)
482 first = data->firstMessage;
483 last = data->entries[i].last;
484 if (last > data->lastMessage)
485 last = data->lastMessage;
487 data->unread -= last - first + 1;
491 static int puti (char *line, int num)
496 *p++ = '0' + num % 10;
500 *line++ = *--p, num++;
505 static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf,
506 char **pline, size_t * buflen)
509 size_t len = *buflen - (*pline - *buf);
512 if (len < LONG_STRING * 10) {
516 safe_realloc (buf, *buflen);
517 line = *buf + (*pline - line);
519 strcpy (line, data->group);
520 len -= strlen (line) + 1;
521 line += strlen (line);
522 *line++ = data->subscribed ? ':' : '!';
526 for (x = 0; x < data->num; x++) {
527 if (len < LONG_STRING) {
532 safe_realloc (buf, *buflen);
533 line = *buf + (*pline - line);
541 if (data->entries[x].first == data->entries[x].last)
542 snprintf (line, len, "%d%n", data->entries[x].first, &i);
544 snprintf (line, len, "%d-%d%n",
545 data->entries[x].first, data->entries[x].last, &i);
549 i = puti (line, data->entries[x].first);
552 if (data->entries[x].first != data->entries[x].last) {
555 i = puti (line, data->entries[x].last);
566 void newsrc_gen_entries (CONTEXT * ctx)
568 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
570 unsigned int last = 0, first = 1;
571 int save_sort = SORT_ORDER;
573 if (Sort != SORT_ORDER) {
576 mutt_sort_headers (ctx, 0);
580 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
585 * Set up to fake initial sequence from 1 to the article before the
586 * first article in our list
591 for (x = 0; x < ctx->msgcount; x++) {
592 if (series) { /* search for first unread */
594 * We don't actually check sequential order, since we mark
595 * "missing" entries as read/deleted
597 last = ctx->hdrs[x]->article_num;
598 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
599 !ctx->hdrs[x]->read) {
600 if (data->num >= data->max) {
601 data->max = data->max * 2;
602 safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
604 data->entries[data->num].first = first;
605 data->entries[data->num].last = last - 1;
610 else { /* search for first read */
612 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
616 last = ctx->hdrs[x]->article_num;
619 if (series && first <= data->lastLoaded) {
620 if (data->num >= data->max) {
621 data->max = data->max * 2;
622 safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
624 data->entries[data->num].first = first;
625 data->entries[data->num].last = data->lastLoaded;
629 if (save_sort != Sort) {
631 mutt_sort_headers (ctx, 0);
635 int mutt_newsrc_update (NNTP_SERVER * news)
645 llen = len = 10 * LONG_STRING;
646 line = buf = safe_calloc (1, len);
647 /* we will generate full newsrc here */
648 for (tmp = news->list; tmp; tmp = tmp->next) {
649 data = (NNTP_DATA *) tmp->data;
650 if (!data || !data->rc)
652 nntp_create_newsrc_line (data, &buf, &line, &llen);
653 dprint (2, (debugfile, "Added to newsrc: %s", line));
654 line += strlen (line);
656 /* newrc being fully rewritten */
658 (r = mutt_update_list_file (news->newsrc, NULL, "", buf)) == 0) {
661 stat (news->newsrc, &st);
662 news->size = st.st_size;
663 news->mtime = st.st_mtime;
669 static FILE *mutt_mkname (char *s)
671 char buf[_POSIX_PATH_MAX], *pc;
675 nntp_cache_expand (buf, s);
676 if ((fp = safe_fopen (buf, "w")))
679 nntp_cache_expand (buf, "cache-XXXXXX");
680 pc = buf + strlen (buf) - 12; /* positioning to "cache-XXXXXX" */
681 if ((fd = mkstemp (buf)) == -1)
683 strcpy (s, pc); /* generated name */
684 return fdopen (fd, "w");
687 /* Updates info into .index file: ALL or about selected newsgroup */
688 static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data)
690 char buf[LONG_STRING], *key = "ALL";
691 char file[_POSIX_PATH_MAX];
693 if (!serv || !serv->conn || !serv->conn->account.host)
696 if (data && data->group) {
698 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
699 data->firstMessage, data->lastLoaded);
702 strfcpy (file, serv->cache, sizeof (file));
703 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
704 (int) serv->newgroups_time);
706 nntp_cache_expand (file, ".index");
707 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
710 /* Remove cache files of unsubscribed newsgroups */
711 void nntp_clear_cacheindex (NNTP_SERVER * news)
716 if (option (OPTSAVEUNSUB) || !news)
719 for (tmp = news->list; tmp; tmp = tmp->next) {
720 data = (NNTP_DATA *) tmp->data;
721 if (!data || data->subscribed || !data->cache)
723 nntp_delete_cache (data);
724 dprint (2, (debugfile, "Removed from .index: %s\n", data->group));
729 int nntp_save_cache_index (NNTP_SERVER * news)
731 char buf[HUGE_STRING];
732 char file[_POSIX_PATH_MAX];
737 if (!news || !news->newsgroups)
739 if (!option (OPTNEWSCACHE))
743 nntp_cache_expand (file, news->cache);
745 f = safe_fopen (file, "w");
748 strfcpy (buf, news->conn->account.host, sizeof (buf));
749 f = mutt_mkname (buf);
750 news->cache = safe_strdup (buf);
751 nntp_cache_expand (file, buf);
756 for (l = news->list; l; l = l->next) {
757 if ((d = (NNTP_DATA *) l->data) && !d->deleted) {
759 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
760 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
763 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
764 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
765 if (fputs (buf, f) == EOF) {
774 if (nntp_update_cacheindex (news, NULL)) {
781 int nntp_save_cache_group (CONTEXT * ctx)
783 char buf[HUGE_STRING], addr[STRING];
784 char file[_POSIX_PATH_MAX];
788 int i = 0, save = SORT_ORDER;
791 if (!option (OPTNEWSCACHE))
793 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
796 if (((NNTP_DATA *) ctx->data)->cache) {
797 nntp_cache_expand (file, ((NNTP_DATA *) ctx->data)->cache);
799 f = safe_fopen (file, "w");
802 snprintf (buf, sizeof (buf), "%s-%s",
803 ((NNTP_DATA *) ctx->data)->nserv->conn->account.host,
804 ((NNTP_DATA *) ctx->data)->group);
805 f = mutt_mkname (buf);
806 ((NNTP_DATA *) ctx->data)->cache = safe_strdup (buf);
807 nntp_cache_expand (file, buf);
812 if (Sort != SORT_ORDER) {
815 mutt_sort_headers (ctx, 0);
818 /* Save only $nntp_context messages... */
819 ((NNTP_DATA *) ctx->data)->lastCached = 0;
820 if (NntpContext && ctx->msgcount > NntpContext)
821 i = ctx->msgcount - NntpContext;
822 for (; i < ctx->msgcount; i++) {
823 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
826 rfc822_write_address (addr, sizeof (addr), h->env->from, 0);
827 tm = gmtime (&h->date_sent);
828 snprintf (buf, sizeof (buf),
829 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
830 h->article_num, h->env->subject, addr, tm->tm_mday,
831 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
832 tm->tm_min, tm->tm_sec, h->env->message_id);
834 if (h->env->references)
835 mutt_write_references (h->env->references, f);
836 snprintf (buf, sizeof (buf), "\t%ld\t%d\tXref: %s\n",
837 h->content->length, h->lines, NONULL (h->env->xref));
838 if (fputs (buf, f) == EOF) {
844 prev = ctx->hdrs[i]->article_num;
849 mutt_sort_headers (ctx, 0);
853 if (nntp_update_cacheindex (((NNTP_DATA *) ctx->data)->nserv,
854 (NNTP_DATA *) ctx->data)) {
858 ((NNTP_DATA *) ctx->data)->lastCached =
859 ((NNTP_DATA *) ctx->data)->lastLoaded;
863 void nntp_delete_cache (NNTP_DATA * data)
865 char buf[_POSIX_PATH_MAX];
867 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
870 nntp_cache_expand (buf, data->cache);
873 data->lastCached = 0;
874 nntp_cache_expand (buf, ".index");
875 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
879 NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
883 if (!news || !news->newsgroups || !group || !*group)
885 if (!(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) {
887 (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
888 data->group = (char *) data + sizeof (NNTP_DATA);
889 strcpy (data->group, group);
892 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
894 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
895 hash_insert (news->newsgroups, data->group, data, 0);
896 nntp_add_to_list (news, data);
898 if (!data->subscribed) {
899 data->subscribed = 1;
905 NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group)
909 if (!news || !news->newsgroups || !group || !*group ||
910 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
912 if (data->subscribed) {
913 data->subscribed = 0;
914 if (!option (OPTSAVEUNSUB))
920 NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
924 if (!news || !news->newsgroups || !group || !*group ||
925 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
928 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
932 data->entries[0].first = 1;
934 data->entries[0].last = data->lastMessage;
935 if (Context && Context->data == data) {
938 for (x = 0; x < Context->msgcount; x++)
939 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
944 NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
948 if (!news || !news->newsgroups || !group || !*group ||
949 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
952 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
956 data->entries[0].first = 1;
957 data->entries[0].last = data->firstMessage - 1;
958 if (Context && Context->data == data) {
961 data->unread = Context->msgcount;
962 for (x = 0; x < Context->msgcount; x++)
963 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
966 data->unread = data->lastMessage - data->entries[0].last;
970 /* this routine gives the first newsgroup with new messages */
971 void nntp_buffy (char *s)
975 for (list = CurrentNewsSrv->list; list; list = list->next) {
976 NNTP_DATA *data = (NNTP_DATA *) list->data;
978 if (data && data->subscribed && data->unread) {
979 if (Context && Context->magic == M_NNTP &&
980 !mutt_strcmp (data->group, ((NNTP_DATA *) Context->data)->group)) {
981 unsigned int i, unread = 0;
983 for (i = 0; i < Context->msgcount; i++)
984 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
989 strcpy (s, data->group);