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;
73 while (*p && (*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 data = (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)
88 news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
89 hash_insert (news->newsgroups, data->group, data, 0);
90 nntp_add_to_list (news, data);
93 FREE ((void **) &data->entries);
96 data->entries = safe_calloc (x*2, sizeof (NEWSRC_ENTRY));
100 data->subscribed = 1;
102 data->subscribed = 0;
109 while (*p && *p != ',' && *p != '\n') p++;
115 if ((h = strchr(b, '-')))
119 data->entries[x].first = atoi(b);
120 data->entries[x].last = atoi(h);
124 data->entries[x].first = atoi(b);
125 data->entries[x].last = data->entries[x].first;
128 if (data->entries[x].last != 0)
131 if (x && !data->lastMessage)
132 data->lastMessage = data->entries[x-1].last;
134 mutt_newsgroup_stat (data);
135 dprint (2, (debugfile, "parse_line: Newsgroup %s\n", data->group));
140 static int slurp_newsrc (NNTP_SERVER *news)
146 news->stat = stat (news->newsrc, &sb);
147 news->size = sb.st_size;
148 news->mtime = sb.st_mtime;
150 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
152 /* hmm, should we use dotlock? */
153 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1))
159 buf = safe_malloc (sb.st_size + 1);
160 while (fgets (buf, sb.st_size + 1, fp))
161 nntp_parse_newsrc_line (news, buf);
164 mx_unlock_file (news->newsrc, fileno (fp), 0);
169 void nntp_cache_expand (char *dst, const char *src)
171 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
172 mutt_expand_path (dst, _POSIX_PATH_MAX);
175 /* Loads $news_cache_dir/.index into memory, loads newsserver data
176 * and newsgroup cache names */
177 static int nntp_parse_cacheindex (NNTP_SERVER *news)
180 char buf[HUGE_STRING], *cp;
181 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
186 /* check is server name defined or not */
187 if (!news || !news->conn || !news->conn->account.host)
189 unset_option (OPTNEWSCACHE);
190 if (!NewsCacheDir || !*NewsCacheDir)
193 strfcpy (dir, NewsCacheDir, sizeof (dir));
194 mutt_expand_path (dir, sizeof(dir));
196 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0)
198 snprintf (buf, sizeof(buf), _("Directory %s not exist. Create it?"), dir);
199 if (mutt_yesorno (buf, M_YES) != M_YES || mkdir (dir, (S_IRWXU+S_IRWXG+
202 mutt_error _("Cache directory not created!");
208 set_option (OPTNEWSCACHE);
211 snprintf (buf, sizeof(buf), "%s/.index", dir);
212 if (!(index = safe_fopen (buf, "a+")))
215 while (fgets (buf, sizeof(buf), index))
217 buf[strlen(buf) - 1] = 0; /* strip ending '\n' */
218 if (!mutt_strncmp (buf, "#: ", 3) &&
219 !mutt_strcasecmp (buf+3, news->conn->account.host))
222 while (fgets (buf, sizeof(buf), index))
225 while (*cp && *cp != ' ') cp++;
228 if (!mutt_strcmp (buf, "#:"))
230 sscanf (cp + 1, "%s %d %d", file, &l, &m);
231 if (!mutt_strcmp (buf, "ALL"))
233 news->cache = safe_strdup (file);
234 news->newgroups_time = m;
236 else if (news->newsgroups)
238 if ((data = (NNTP_DATA *)hash_find (news->newsgroups, buf)) == NULL)
240 data = (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + strlen (buf) + 1);
241 data->group = (char *) data + sizeof (NNTP_DATA);
242 strcpy(data->group, buf);
245 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
246 news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
247 hash_insert (news->newsgroups, data->group, data, 0);
248 nntp_add_to_list (news, data);
250 data->cache = safe_strdup (file);
252 if (!data->firstMessage || data->lastMessage < m)
254 if (!data->firstMessage)
255 data->firstMessage = l;
256 if (data->lastMessage < m)
257 data->lastMessage = m;
258 data->lastCached = m;
259 if (t || !data->unread)
260 mutt_newsgroup_stat (data);
268 nntp_format_str (char *dest, size_t destlen, char op, const char *src,
269 const char *fmt, const char *ifstring, const char *elsestring,
270 unsigned long data, format_flag flags)
272 char fn[SHORT_STRING], tmp[SHORT_STRING];
277 strncpy (fn, NewsServer, sizeof(fn) - 1);
279 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
280 snprintf (dest, destlen, tmp, fn);
286 /* nntp_parse_url: given an NNPT URL, return host, port,
287 * username, password and newsgroup will recognise. */
288 int nntp_parse_url (const char *server, ACCOUNT *acct,
289 char *group, size_t group_len)
297 acct->port = NNTP_PORT;
298 acct->type = M_ACCT_TYPE_NNTP;
300 c = safe_strdup (server);
301 url_parse_ciss (&url, c);
303 if (url.scheme == U_NNTP || url.scheme == U_NNTPS)
305 if (url.scheme == U_NNTPS)
307 acct->flags |= M_ACCT_SSL;
308 acct->port = NNTP_SSL_PORT;
313 strfcpy (group, url.path, group_len);
315 ret = mutt_account_fromurl (acct, &url);
322 void nntp_expand_path (char *line, size_t len, ACCOUNT *acct)
326 url.path = safe_strdup (line);
327 mutt_account_tourl (acct, &url);
328 url_ciss_tostring (&url, line, len, 0);
333 * Automatically loads a newsrc into memory, if necessary.
334 * Checks the size/mtime of a newsrc file, if it doesn't match, load
335 * again. Hmm, if a system has broken mtimes, this might mean the file
336 * is reloaded every time, which we'd have to fix.
338 * a newsrc file is a line per newsgroup, with the newsgroup, then a
339 * ':' denoting subscribed or '!' denoting unsubscribed, then a
340 * comma separated list of article numbers and ranges.
342 NNTP_SERVER *mutt_select_newsserver (char *server)
344 char file[_POSIX_PATH_MAX];
351 if (!server || !*server)
353 mutt_error _("No newsserver defined!");
357 buf = p = safe_calloc (strlen (server) + 10, sizeof (char));
358 if (url_check_scheme (server) == U_UNKNOWN)
360 strcpy (buf, "nntp://");
361 p = strchr (buf, '\0');
365 if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file)
368 mutt_error (_("%s is an invalid newsserver specification!"), server);
373 conn = mutt_conn_find (NULL, &acct);
377 mutt_FormatString (file, sizeof (file), NONULL (NewsRc), nntp_format_str, 0, 0);
378 mutt_expand_path (file, sizeof (file));
380 serv = (NNTP_SERVER *)conn->data;
385 /* externally modified? */
386 if (serv->stat != stat (file, &sb) || (!serv->stat &&
387 (serv->size != sb.st_size || serv->mtime != sb.st_mtime)))
389 for (list = serv->list; list; list = list->next)
391 NNTP_DATA *data = (NNTP_DATA *) list->data;
395 data->subscribed = 0;
401 nntp_clear_cacheindex (serv);
404 if (serv->status == NNTP_BYE)
405 serv->status = NNTP_NONE;
406 nntp_check_newgroups (serv, 0);
411 serv = safe_calloc (1, sizeof (NNTP_SERVER));
413 serv->newsrc = safe_strdup (file);
414 serv->newsgroups = hash_create (1009);
415 slurp_newsrc (serv); /* load .newsrc */
416 nntp_parse_cacheindex (serv); /* load .index */
417 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
418 nntp_check_newgroups (serv, 1);
419 else if (nntp_get_active (serv) < 0)
421 hash_destroy (&serv->newsgroups, nntp_delete_data);
422 for (list = serv->list; list; list = list->next)
424 mutt_free_list (&serv->list);
425 FREE (&serv->newsrc);
430 nntp_clear_cacheindex (serv);
431 conn->data = (void *)serv;
437 * full status flags are not supported by nntp, but we can fake some
438 * of them. This is how:
439 * Read = a read message number is in the .newsrc
440 * New = a message is new since we last read this newsgroup
441 * Old = anything else
442 * So, Read is marked as such in the newsrc, old is anything that is
443 * "skipped" in the newsrc, and new is anything not in the newsrc nor
444 * in the cache. By skipped, I mean before the last unread message
446 void nntp_get_status (CONTEXT *ctx, HEADER *h, char *group, int article)
448 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
452 data = (NNTP_DATA *) hash_find (data->nserv->newsgroups, group);
458 dprint (3, (debugfile, "newsgroup %s not found\n", group));
463 for (x = 0; x < data->num; x++)
465 if ((article >= data->entries[x].first) &&
466 (article <= data->entries[x].last))
468 /* we cannot use mutt_set_flag() because mx_update_context()
474 /* If article was not cached yet, it is new! :) */
475 if (!data->cache || article > data->lastCached)
477 /* Old articles are articles which aren't read but an article after them
479 if (option (OPTMARKOLD))
483 void mutt_newsgroup_stat (NNTP_DATA *data)
486 unsigned int first, last;
489 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
492 data->unread = data->lastMessage - data->firstMessage + 1;
493 for (i = 0; i < data->num; i++)
495 first = data->entries[i].first;
496 if (first < data->firstMessage)
497 first = data->firstMessage;
498 last = data->entries[i].last;
499 if (last > data->lastMessage)
500 last = data->lastMessage;
502 data->unread -= last - first + 1;
506 static int puti (char *line, int num)
512 *p++ = '0' + num % 10;
516 *line++ = *--p, num++;
521 static void nntp_create_newsrc_line (NNTP_DATA *data, char **buf, char **pline, size_t *buflen)
524 size_t len = *buflen - (*pline - *buf);
527 if (len < LONG_STRING * 10)
532 safe_realloc (buf, *buflen);
533 line = *buf + (*pline - line);
535 strcpy (line, data->group);
536 len -= strlen (line) + 1;
537 line += strlen (line);
538 *line++ = data->subscribed ? ':' : '!';
542 for (x = 0; x < data->num; x++)
544 if (len < LONG_STRING)
550 safe_realloc (buf, *buflen);
551 line = *buf + (*pline - line);
560 if (data->entries[x].first == data->entries[x].last)
561 snprintf (line, len, "%d%n", data->entries[x].first, &i);
563 snprintf (line, len, "%d-%d%n",
564 data->entries[x].first, data->entries[x].last, &i);
568 i = puti (line, data->entries[x].first);
570 if (data->entries[x].first != data->entries[x].last)
574 i = puti (line, data->entries[x].last);
584 void newsrc_gen_entries (CONTEXT *ctx)
586 NNTP_DATA *data = (NNTP_DATA *)ctx->data;
588 unsigned int last = 0, first = 1;
589 int save_sort = SORT_ORDER;
591 if (Sort != SORT_ORDER)
595 mutt_sort_headers (ctx, 0);
600 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
605 * Set up to fake initial sequence from 1 to the article before the
606 * first article in our list
611 for (x = 0; x < ctx->msgcount; x++)
613 if (series) /* search for first unread */
616 * We don't actually check sequential order, since we mark
617 * "missing" entries as read/deleted
619 last = ctx->hdrs[x]->article_num;
620 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
623 if (data->num >= data->max)
625 data->max = data->max * 2;
626 safe_realloc (&data->entries,
627 data->max * sizeof (NEWSRC_ENTRY));
629 data->entries[data->num].first = first;
630 data->entries[data->num].last = last - 1;
635 else /* search for first read */
637 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read)
642 last = ctx->hdrs[x]->article_num;
645 if (series && first <= data->lastLoaded)
647 if (data->num >= data->max)
649 data->max = data->max * 2;
650 safe_realloc (&data->entries,
651 data->max * sizeof (NEWSRC_ENTRY));
653 data->entries[data->num].first = first;
654 data->entries[data->num].last = data->lastLoaded;
658 if (save_sort != Sort)
661 mutt_sort_headers (ctx, 0);
665 int mutt_newsrc_update (NNTP_SERVER *news)
675 llen = len = 10 * LONG_STRING;
676 line = buf = safe_calloc (1, len);
677 /* we will generate full newsrc here */
678 for (tmp = news->list; tmp; tmp = tmp->next)
680 data = (NNTP_DATA *) tmp->data;
681 if (!data || !data->rc)
683 nntp_create_newsrc_line (data, &buf, &line, &llen);
684 dprint (2, (debugfile, "Added to newsrc: %s", line));
685 line += strlen (line);
687 /* newrc being fully rewritten */
689 (r = mutt_update_list_file (news->newsrc, NULL, "", buf)) == 0)
693 stat (news->newsrc, &st);
694 news->size = st.st_size;
695 news->mtime = st.st_mtime;
701 static FILE *mutt_mkname (char *s)
703 char buf[_POSIX_PATH_MAX], *pc;
707 nntp_cache_expand (buf, s);
708 if ((fp = safe_fopen (buf, "w")))
711 nntp_cache_expand (buf, "cache-XXXXXX");
712 pc = buf + strlen (buf) - 12; /* positioning to "cache-XXXXXX" */
713 if ((fd = mkstemp (buf)) == -1)
715 strcpy (s, pc); /* generated name */
716 return fdopen (fd, "w");
719 /* Updates info into .index file: ALL or about selected newsgroup */
720 static int nntp_update_cacheindex (NNTP_SERVER *serv, NNTP_DATA *data)
722 char buf[LONG_STRING], *key = "ALL";
723 char file[_POSIX_PATH_MAX];
725 if (!serv || !serv->conn || !serv->conn->account.host)
728 if (data && data->group)
731 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
732 data->firstMessage, data->lastLoaded);
736 strfcpy (file, serv->cache, sizeof (file));
737 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file, (int)serv->newgroups_time);
739 nntp_cache_expand (file, ".index");
740 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
743 /* Remove cache files of unsubscribed newsgroups */
744 void nntp_clear_cacheindex (NNTP_SERVER *news)
749 if (option (OPTSAVEUNSUB) || !news)
752 for (tmp = news->list; tmp; tmp = tmp->next)
754 data = (NNTP_DATA *) tmp->data;
755 if (!data || data->subscribed || !data->cache)
757 nntp_delete_cache (data);
758 dprint (2, (debugfile, "Removed from .index: %s\n", data->group));
763 int nntp_save_cache_index (NNTP_SERVER *news)
765 char buf[HUGE_STRING];
766 char file[_POSIX_PATH_MAX];
771 if (!news || !news->newsgroups)
773 if (!option (OPTNEWSCACHE))
778 nntp_cache_expand (file, news->cache);
780 f = safe_fopen (file, "w");
784 strfcpy (buf, news->conn->account.host, sizeof(buf));
785 f = mutt_mkname (buf);
786 news->cache = safe_strdup (buf);
787 nntp_cache_expand (file, buf);
792 for (l = news->list; l; l = l->next)
794 if ((d = (NNTP_DATA *)l->data) && !d->deleted)
797 snprintf (buf, sizeof(buf), "%s %d %d %c %s\n", d->group,
798 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
801 snprintf (buf, sizeof(buf), "%s %d %d %c\n", d->group,
802 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
803 if (fputs (buf, f) == EOF)
813 if (nntp_update_cacheindex (news, NULL))
821 int nntp_save_cache_group (CONTEXT *ctx)
823 char buf[HUGE_STRING], addr[STRING];
824 char file[_POSIX_PATH_MAX];
828 int i = 0, save = SORT_ORDER;
831 if (!option (OPTNEWSCACHE))
833 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
836 if (((NNTP_DATA *)ctx->data)->cache)
838 nntp_cache_expand (file, ((NNTP_DATA *)ctx->data)->cache);
840 f = safe_fopen (file, "w");
844 snprintf (buf, sizeof(buf), "%s-%s",
845 ((NNTP_DATA *)ctx->data)->nserv->conn->account.host,
846 ((NNTP_DATA *)ctx->data)->group);
847 f = mutt_mkname (buf);
848 ((NNTP_DATA *)ctx->data)->cache = safe_strdup (buf);
849 nntp_cache_expand (file, buf);
854 if (Sort != SORT_ORDER)
858 mutt_sort_headers (ctx, 0);
861 /* Save only $nntp_context messages... */
862 ((NNTP_DATA *)ctx->data)->lastCached = 0;
863 if (NntpContext && ctx->msgcount > NntpContext)
864 i = ctx->msgcount - NntpContext;
865 for (; i < ctx->msgcount; i++)
867 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev)
871 rfc822_write_address (addr, sizeof(addr), h->env->from, 0);
872 tm = gmtime (&h->date_sent);
873 snprintf (buf, sizeof(buf),
874 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
875 h->article_num, h->env->subject, addr, tm->tm_mday,
876 Months[tm->tm_mon], tm->tm_year+1900, tm->tm_hour, tm->tm_min,
877 tm->tm_sec, h->env->message_id);
879 if (h->env->references)
880 mutt_write_references (h->env->references, f);
881 snprintf (buf, sizeof(buf), "\t%ld\t%d\tXref: %s\n", h->content->length,
882 h->lines, NONULL(h->env->xref));
883 if (fputs (buf, f) == EOF)
890 prev = ctx->hdrs[i]->article_num;
896 mutt_sort_headers (ctx, 0);
900 if (nntp_update_cacheindex (((NNTP_DATA *)ctx->data)->nserv,
901 (NNTP_DATA *)ctx->data))
906 ((NNTP_DATA *)ctx->data)->lastCached = ((NNTP_DATA *)ctx->data)->lastLoaded;
910 void nntp_delete_cache (NNTP_DATA *data)
912 char buf[_POSIX_PATH_MAX];
914 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
917 nntp_cache_expand (buf, data->cache);
920 data->lastCached = 0;
921 nntp_cache_expand (buf, ".index");
922 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group, NULL);
925 NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER *news, char *group)
929 if (!news || !news->newsgroups || !group || !*group)
931 if (!(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
933 data = (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
934 data->group = (char *) data + sizeof (NNTP_DATA);
935 strcpy (data->group, group);
938 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
939 news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
940 hash_insert (news->newsgroups, data->group, data, 0);
941 nntp_add_to_list (news, data);
943 if (!data->subscribed)
945 data->subscribed = 1;
951 NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER *news, char *group)
955 if (!news || !news->newsgroups || !group || !*group ||
956 !(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
958 if (data->subscribed)
960 data->subscribed = 0;
961 if (!option (OPTSAVEUNSUB))
967 NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER *news, char *group)
971 if (!news || !news->newsgroups || !group || !*group ||
972 !(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
976 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
980 data->entries[0].first = 1;
982 data->entries[0].last = data->lastMessage;
983 if (Context && Context->data == data)
987 for (x = 0; x < Context->msgcount; x++)
988 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
993 NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER *news, char *group)
997 if (!news || !news->newsgroups || !group || !*group ||
998 !(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
1002 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1006 data->entries[0].first = 1;
1007 data->entries[0].last = data->firstMessage - 1;
1008 if (Context && Context->data == data)
1012 data->unread = Context->msgcount;
1013 for (x = 0; x < Context->msgcount; x++)
1014 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
1017 data->unread = data->lastMessage - data->entries[0].last;
1021 /* this routine gives the first newsgroup with new messages */
1022 void nntp_buffy (char *s)
1026 for (list = CurrentNewsSrv->list; list; list = list->next)
1028 NNTP_DATA *data = (NNTP_DATA *) list->data;
1030 if (data && data->subscribed && data->unread)
1032 if (Context && Context->magic == M_NNTP &&
1033 !mutt_strcmp (data->group, ((NNTP_DATA *) Context->data)->group))
1035 unsigned int i, unread = 0;
1037 for (i = 0; i < Context->msgcount; i++)
1038 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1043 strcpy (s, data->group);