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.
22 #include "mutt_curses.h"
38 void nntp_add_to_list (NNTP_SERVER *s, NNTP_DATA *d)
45 l = safe_calloc (1, sizeof (LIST));
54 static int nntp_parse_newsrc_line (NNTP_SERVER *news, char *line)
57 char group[LONG_STRING];
59 char *p = line, *b, *h;
69 while (*p && (*p != ':' && *p != '!')) p++;
73 if (len > sizeof (group))
75 strfcpy (group, line, len);
76 if ((data = (NNTP_DATA *)hash_find (news->newsgroups, group)) == NULL)
78 data = (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
79 data->group = (char *) data + sizeof (NNTP_DATA);
80 strcpy (data->group, group);
83 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
84 news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
85 hash_insert (news->newsgroups, data->group, data, 0);
86 nntp_add_to_list (news, data);
89 FREE ((void **) &data->entries);
92 data->entries = safe_calloc (x*2, sizeof (NEWSRC_ENTRY));
105 while (*p && *p != ',' && *p != '\n') p++;
111 if ((h = strchr(b, '-')))
115 data->entries[x].first = atoi(b);
116 data->entries[x].last = atoi(h);
120 data->entries[x].first = atoi(b);
121 data->entries[x].last = data->entries[x].first;
124 if (data->entries[x].last != 0)
127 if (x && !data->lastMessage)
128 data->lastMessage = data->entries[x-1].last;
130 mutt_newsgroup_stat (data);
131 dprint (2, (debugfile, "parse_line: Newsgroup %s\n", data->group));
136 static int slurp_newsrc (NNTP_SERVER *news)
142 news->stat = stat (news->newsrc, &sb);
143 news->size = sb.st_size;
144 news->mtime = sb.st_mtime;
146 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
148 /* hmm, should we use dotlock? */
149 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1))
155 buf = safe_malloc (sb.st_size + 1);
156 while (fgets (buf, sb.st_size + 1, fp))
157 nntp_parse_newsrc_line (news, buf);
160 mx_unlock_file (news->newsrc, fileno (fp), 0);
165 void nntp_cache_expand (char *dst, const char *src)
167 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
168 mutt_expand_path (dst, _POSIX_PATH_MAX);
171 /* Loads $news_cache_dir/.index into memory, loads newsserver data
172 * and newsgroup cache names */
173 static int nntp_parse_cacheindex (NNTP_SERVER *news)
176 char buf[HUGE_STRING], *cp;
177 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
182 /* check is server name defined or not */
183 if (!news || !news->conn || !news->conn->account.host)
185 unset_option (OPTNEWSCACHE);
186 if (!NewsCacheDir || !*NewsCacheDir)
189 strfcpy (dir, NewsCacheDir, sizeof (dir));
190 mutt_expand_path (dir, sizeof(dir));
192 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0)
194 snprintf (buf, sizeof(buf), _("Directory %s not exist. Create it?"), dir);
195 if (mutt_yesorno (buf, M_YES) != M_YES || mkdir (dir, (S_IRWXU+S_IRWXG+
198 mutt_error _("Cache directory not created!");
204 set_option (OPTNEWSCACHE);
207 snprintf (buf, sizeof(buf), "%s/.index", dir);
208 if (!(index = safe_fopen (buf, "a+")))
211 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))
221 while (*cp && *cp != ' ') cp++;
224 if (!mutt_strcmp (buf, "#:"))
226 sscanf (cp + 1, "%s %d %d", file, &l, &m);
227 if (!mutt_strcmp (buf, "ALL"))
229 news->cache = safe_strdup (file);
230 news->newgroups_time = m;
232 else if (news->newsgroups)
234 if ((data = (NNTP_DATA *)hash_find (news->newsgroups, buf)) == NULL)
236 data = (NNTP_DATA *) safe_calloc (1, 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)
242 news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
243 hash_insert (news->newsgroups, data->group, data, 0);
244 nntp_add_to_list (news, data);
246 data->cache = safe_strdup (file);
248 if (!data->firstMessage || data->lastMessage < m)
250 if (!data->firstMessage)
251 data->firstMessage = l;
252 if (data->lastMessage < m)
253 data->lastMessage = m;
254 data->lastCached = m;
255 if (t || !data->unread)
256 mutt_newsgroup_stat (data);
264 nntp_format_str (char *dest, size_t destlen, char op, const char *src,
265 const char *fmt, const char *ifstring, const char *elsestring,
266 unsigned long data, format_flag flags)
268 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)
301 if (url.scheme == U_NNTPS)
303 acct->flags |= M_ACCT_SSL;
304 acct->port = NNTP_SSL_PORT;
309 strfcpy (group, url.path, group_len);
311 ret = mutt_account_fromurl (acct, &url);
318 void nntp_expand_path (char *line, size_t len, ACCOUNT *acct)
322 url.path = safe_strdup (line);
323 mutt_account_tourl (acct, &url);
324 url_ciss_tostring (&url, line, len, 0);
329 * Automatically loads a newsrc into memory, if necessary.
330 * Checks the size/mtime of a newsrc file, if it doesn't match, load
331 * again. Hmm, if a system has broken mtimes, this might mean the file
332 * is reloaded every time, which we'd have to fix.
334 * a newsrc file is a line per newsgroup, with the newsgroup, then a
335 * ':' denoting subscribed or '!' denoting unsubscribed, then a
336 * comma separated list of article numbers and ranges.
338 NNTP_SERVER *mutt_select_newsserver (char *server)
340 char file[_POSIX_PATH_MAX];
347 if (!server || !*server)
349 mutt_error _("No newsserver defined!");
353 buf = p = safe_calloc (strlen (server) + 10, sizeof (char));
354 if (url_check_scheme (server) == U_UNKNOWN)
356 strcpy (buf, "nntp://");
357 p = strchr (buf, '\0');
361 if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file)
364 mutt_error (_("%s is an invalid newsserver specification!"), server);
369 conn = mutt_conn_find (NULL, &acct);
373 mutt_FormatString (file, sizeof (file), NONULL (NewsRc), nntp_format_str, 0, 0);
374 mutt_expand_path (file, sizeof (file));
376 serv = (NNTP_SERVER *)conn->data;
381 /* externally modified? */
382 if (serv->stat != stat (file, &sb) || (!serv->stat &&
383 (serv->size != sb.st_size || serv->mtime != sb.st_mtime)))
385 for (list = serv->list; list; list = list->next)
387 NNTP_DATA *data = (NNTP_DATA *) list->data;
391 data->subscribed = 0;
397 nntp_clear_cacheindex (serv);
400 if (serv->status == NNTP_BYE)
401 serv->status = NNTP_NONE;
402 nntp_check_newgroups (serv, 0);
407 serv = safe_calloc (1, sizeof (NNTP_SERVER));
409 serv->newsrc = safe_strdup (file);
410 serv->newsgroups = hash_create (1009);
411 slurp_newsrc (serv); /* load .newsrc */
412 nntp_parse_cacheindex (serv); /* load .index */
413 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
414 nntp_check_newgroups (serv, 1);
415 else if (nntp_get_active (serv) < 0)
417 hash_destroy (&serv->newsgroups, nntp_delete_data);
418 for (list = serv->list; list; list = list->next)
420 mutt_free_list (&serv->list);
421 FREE (&serv->newsrc);
426 nntp_clear_cacheindex (serv);
427 conn->data = (void *)serv;
433 * full status flags are not supported by nntp, but we can fake some
434 * of them. This is how:
435 * Read = a read message number is in the .newsrc
436 * New = a message is new since we last read this newsgroup
437 * Old = anything else
438 * So, Read is marked as such in the newsrc, old is anything that is
439 * "skipped" in the newsrc, and new is anything not in the newsrc nor
440 * in the cache. By skipped, I mean before the last unread message
442 void nntp_get_status (CONTEXT *ctx, HEADER *h, char *group, int article)
444 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
448 data = (NNTP_DATA *) hash_find (data->nserv->newsgroups, group);
454 dprint (3, (debugfile, "newsgroup %s not found\n", group));
459 for (x = 0; x < data->num; x++)
461 if ((article >= data->entries[x].first) &&
462 (article <= data->entries[x].last))
464 /* we cannot use mutt_set_flag() because mx_update_context()
470 /* If article was not cached yet, it is new! :) */
471 if (!data->cache || article > data->lastCached)
473 /* Old articles are articles which aren't read but an article after them
475 if (option (OPTMARKOLD))
479 void mutt_newsgroup_stat (NNTP_DATA *data)
482 unsigned int first, last;
485 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
488 data->unread = data->lastMessage - data->firstMessage + 1;
489 for (i = 0; i < data->num; i++)
491 first = data->entries[i].first;
492 if (first < data->firstMessage)
493 first = data->firstMessage;
494 last = data->entries[i].last;
495 if (last > data->lastMessage)
496 last = data->lastMessage;
498 data->unread -= last - first + 1;
502 static int puti (char *line, int num)
508 *p++ = '0' + num % 10;
512 *line++ = *--p, num++;
517 static void nntp_create_newsrc_line (NNTP_DATA *data, char **buf, char **pline, size_t *buflen)
520 size_t len = *buflen - (*pline - *buf);
523 if (len < LONG_STRING * 10)
528 safe_realloc (buf, *buflen);
529 line = *buf + (*pline - line);
531 strcpy (line, data->group);
532 len -= strlen (line) + 1;
533 line += strlen (line);
534 *line++ = data->subscribed ? ':' : '!';
538 for (x = 0; x < data->num; x++)
540 if (len < LONG_STRING)
546 safe_realloc (buf, *buflen);
547 line = *buf + (*pline - line);
556 if (data->entries[x].first == data->entries[x].last)
557 snprintf (line, len, "%d%n", data->entries[x].first, &i);
559 snprintf (line, len, "%d-%d%n",
560 data->entries[x].first, data->entries[x].last, &i);
564 i = puti (line, data->entries[x].first);
566 if (data->entries[x].first != data->entries[x].last)
570 i = puti (line, data->entries[x].last);
580 void newsrc_gen_entries (CONTEXT *ctx)
582 NNTP_DATA *data = (NNTP_DATA *)ctx->data;
584 unsigned int last = 0, first = 1;
585 int save_sort = SORT_ORDER;
587 if (Sort != SORT_ORDER)
591 mutt_sort_headers (ctx, 0);
596 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
601 * Set up to fake initial sequence from 1 to the article before the
602 * first article in our list
607 for (x = 0; x < ctx->msgcount; x++)
609 if (series) /* search for first unread */
612 * We don't actually check sequential order, since we mark
613 * "missing" entries as read/deleted
615 last = ctx->hdrs[x]->article_num;
616 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
619 if (data->num >= data->max)
621 data->max = data->max * 2;
622 safe_realloc (&data->entries,
623 data->max * sizeof (NEWSRC_ENTRY));
625 data->entries[data->num].first = first;
626 data->entries[data->num].last = last - 1;
631 else /* search for first read */
633 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read)
638 last = ctx->hdrs[x]->article_num;
641 if (series && first <= data->lastLoaded)
643 if (data->num >= data->max)
645 data->max = data->max * 2;
646 safe_realloc (&data->entries,
647 data->max * sizeof (NEWSRC_ENTRY));
649 data->entries[data->num].first = first;
650 data->entries[data->num].last = data->lastLoaded;
654 if (save_sort != Sort)
657 mutt_sort_headers (ctx, 0);
661 int mutt_newsrc_update (NNTP_SERVER *news)
671 llen = len = 10 * LONG_STRING;
672 line = buf = safe_calloc (1, len);
673 /* we will generate full newsrc here */
674 for (tmp = news->list; tmp; tmp = tmp->next)
676 data = (NNTP_DATA *) tmp->data;
677 if (!data || !data->rc)
679 nntp_create_newsrc_line (data, &buf, &line, &llen);
680 dprint (2, (debugfile, "Added to newsrc: %s", line));
681 line += strlen (line);
683 /* newrc being fully rewritten */
685 (r = mutt_update_list_file (news->newsrc, NULL, "", buf)) == 0)
689 stat (news->newsrc, &st);
690 news->size = st.st_size;
691 news->mtime = st.st_mtime;
697 static FILE *mutt_mkname (char *s)
699 char buf[_POSIX_PATH_MAX], *pc;
703 nntp_cache_expand (buf, s);
704 if ((fp = safe_fopen (buf, "w")))
707 nntp_cache_expand (buf, "cache-XXXXXX");
708 pc = buf + strlen (buf) - 12; /* positioning to "cache-XXXXXX" */
709 if ((fd = mkstemp (buf)) == -1)
711 strcpy (s, pc); /* generated name */
712 return fdopen (fd, "w");
715 /* Updates info into .index file: ALL or about selected newsgroup */
716 static int nntp_update_cacheindex (NNTP_SERVER *serv, NNTP_DATA *data)
718 char buf[LONG_STRING], *key = "ALL";
719 char file[_POSIX_PATH_MAX];
721 if (!serv || !serv->conn || !serv->conn->account.host)
724 if (data && data->group)
727 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
728 data->firstMessage, data->lastLoaded);
732 strfcpy (file, serv->cache, sizeof (file));
733 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file, (int)serv->newgroups_time);
735 nntp_cache_expand (file, ".index");
736 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
739 /* Remove cache files of unsubscribed newsgroups */
740 void nntp_clear_cacheindex (NNTP_SERVER *news)
745 if (option (OPTSAVEUNSUB) || !news)
748 for (tmp = news->list; tmp; tmp = tmp->next)
750 data = (NNTP_DATA *) tmp->data;
751 if (!data || data->subscribed || !data->cache)
753 nntp_delete_cache (data);
754 dprint (2, (debugfile, "Removed from .index: %s\n", data->group));
759 int nntp_save_cache_index (NNTP_SERVER *news)
761 char buf[HUGE_STRING];
762 char file[_POSIX_PATH_MAX];
767 if (!news || !news->newsgroups)
769 if (!option (OPTNEWSCACHE))
774 nntp_cache_expand (file, news->cache);
776 f = safe_fopen (file, "w");
780 strfcpy (buf, news->conn->account.host, sizeof(buf));
781 f = mutt_mkname (buf);
782 news->cache = safe_strdup (buf);
783 nntp_cache_expand (file, buf);
788 for (l = news->list; l; l = l->next)
790 if ((d = (NNTP_DATA *)l->data) && !d->deleted)
793 snprintf (buf, sizeof(buf), "%s %d %d %c %s\n", d->group,
794 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
797 snprintf (buf, sizeof(buf), "%s %d %d %c\n", d->group,
798 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
799 if (fputs (buf, f) == EOF)
809 if (nntp_update_cacheindex (news, NULL))
817 int nntp_save_cache_group (CONTEXT *ctx)
819 char buf[HUGE_STRING], addr[STRING];
820 char file[_POSIX_PATH_MAX];
824 int i = 0, save = SORT_ORDER;
827 if (!option (OPTNEWSCACHE))
829 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
832 if (((NNTP_DATA *)ctx->data)->cache)
834 nntp_cache_expand (file, ((NNTP_DATA *)ctx->data)->cache);
836 f = safe_fopen (file, "w");
840 snprintf (buf, sizeof(buf), "%s-%s",
841 ((NNTP_DATA *)ctx->data)->nserv->conn->account.host,
842 ((NNTP_DATA *)ctx->data)->group);
843 f = mutt_mkname (buf);
844 ((NNTP_DATA *)ctx->data)->cache = safe_strdup (buf);
845 nntp_cache_expand (file, buf);
850 if (Sort != SORT_ORDER)
854 mutt_sort_headers (ctx, 0);
857 /* Save only $nntp_context messages... */
858 ((NNTP_DATA *)ctx->data)->lastCached = 0;
859 if (NntpContext && ctx->msgcount > NntpContext)
860 i = ctx->msgcount - NntpContext;
861 for (; i < ctx->msgcount; i++)
863 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev)
867 rfc822_write_address (addr, sizeof(addr), h->env->from, 0);
868 tm = gmtime (&h->date_sent);
869 snprintf (buf, sizeof(buf),
870 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
871 h->article_num, h->env->subject, addr, tm->tm_mday,
872 Months[tm->tm_mon], tm->tm_year+1900, tm->tm_hour, tm->tm_min,
873 tm->tm_sec, h->env->message_id);
875 if (h->env->references)
876 mutt_write_references (h->env->references, f);
877 snprintf (buf, sizeof(buf), "\t%ld\t%d\tXref: %s\n", h->content->length,
878 h->lines, NONULL(h->env->xref));
879 if (fputs (buf, f) == EOF)
886 prev = ctx->hdrs[i]->article_num;
892 mutt_sort_headers (ctx, 0);
896 if (nntp_update_cacheindex (((NNTP_DATA *)ctx->data)->nserv,
897 (NNTP_DATA *)ctx->data))
902 ((NNTP_DATA *)ctx->data)->lastCached = ((NNTP_DATA *)ctx->data)->lastLoaded;
906 void nntp_delete_cache (NNTP_DATA *data)
908 char buf[_POSIX_PATH_MAX];
910 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
913 nntp_cache_expand (buf, data->cache);
916 data->lastCached = 0;
917 nntp_cache_expand (buf, ".index");
918 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group, NULL);
921 NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER *news, char *group)
925 if (!news || !news->newsgroups || !group || !*group)
927 if (!(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
929 data = (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + strlen (group) + 1);
930 data->group = (char *) data + sizeof (NNTP_DATA);
931 strcpy (data->group, group);
934 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
935 news->newsgroups = hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
936 hash_insert (news->newsgroups, data->group, data, 0);
937 nntp_add_to_list (news, data);
939 if (!data->subscribed)
941 data->subscribed = 1;
947 NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER *news, char *group)
951 if (!news || !news->newsgroups || !group || !*group ||
952 !(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
954 if (data->subscribed)
956 data->subscribed = 0;
957 if (!option (OPTSAVEUNSUB))
963 NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER *news, char *group)
967 if (!news || !news->newsgroups || !group || !*group ||
968 !(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
972 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
976 data->entries[0].first = 1;
978 data->entries[0].last = data->lastMessage;
979 if (Context && Context->data == data)
983 for (x = 0; x < Context->msgcount; x++)
984 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
989 NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER *news, char *group)
993 if (!news || !news->newsgroups || !group || !*group ||
994 !(data = (NNTP_DATA *)hash_find (news->newsgroups, group)))
998 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
1002 data->entries[0].first = 1;
1003 data->entries[0].last = data->firstMessage - 1;
1004 if (Context && Context->data == data)
1008 data->unread = Context->msgcount;
1009 for (x = 0; x < Context->msgcount; x++)
1010 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
1013 data->unread = data->lastMessage - data->entries[0].last;
1017 /* this routine gives the first newsgroup with new messages */
1018 void nntp_buffy (char *s)
1022 for (list = CurrentNewsSrv->list; list; list = list->next)
1024 NNTP_DATA *data = (NNTP_DATA *) list->data;
1026 if (data && data->subscribed && data->unread)
1028 if (Context && Context->magic == M_NNTP &&
1029 !mutt_strcmp (data->group, ((NNTP_DATA *) Context->data)->group))
1031 unsigned int i, unread = 0;
1033 for (i = 0; i < Context->msgcount; i++)
1034 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
1039 strcpy (s, data->group);