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-2001 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.
17 #include "mutt_curses.h"
33 void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d)
40 l = safe_calloc (1, sizeof (LIST));
49 static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
52 char group[LONG_STRING];
54 char *p = line, *b, *h;
63 while (*p && (*p != ':' && *p != '!'))
68 if (len > sizeof (group))
70 strfcpy (group, line, len);
71 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) {
73 (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + mutt_strlen (group) + 1);
74 data->group = (char *) data + sizeof (NNTP_DATA);
75 strcpy (data->group, group);
78 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
80 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
81 hash_insert (news->newsgroups, data->group, data, 0);
82 nntp_add_to_list (news, data);
85 FREE ((void **) &data->entries);
88 data->entries = safe_calloc (x * 2, sizeof (NEWSRC_ENTRY));
100 while (*p && *p != ',' && *p != '\n')
106 if ((h = strchr (b, '-'))) {
109 data->entries[x].first = atoi (b);
110 data->entries[x].last = atoi (h);
113 data->entries[x].first = atoi (b);
114 data->entries[x].last = data->entries[x].first;
117 if (data->entries[x].last != 0)
120 if (x && !data->lastMessage)
121 data->lastMessage = data->entries[x - 1].last;
123 mutt_newsgroup_stat (data);
124 dprint (2, (debugfile, "parse_line: Newsgroup %s\n", data->group));
129 static int slurp_newsrc (NNTP_SERVER * news)
135 news->stat = stat (news->newsrc, &sb);
136 news->size = sb.st_size;
137 news->mtime = sb.st_mtime;
139 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
141 /* hmm, should we use dotlock? */
142 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1)) {
147 buf = safe_malloc (sb.st_size + 1);
148 while (fgets (buf, sb.st_size + 1, fp))
149 nntp_parse_newsrc_line (news, buf);
152 mx_unlock_file (news->newsrc, fileno (fp), 0);
157 void nntp_cache_expand (char *dst, const char *src)
159 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
160 mutt_expand_path (dst, _POSIX_PATH_MAX);
163 /* Loads $news_cache_dir/.index into memory, loads newsserver data
164 * and newsgroup cache names */
165 static int nntp_parse_cacheindex (NNTP_SERVER * news)
168 char buf[HUGE_STRING], *cp;
169 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
174 /* check is server name defined or not */
175 if (!news || !news->conn || !news->conn->account.host)
177 unset_option (OPTNEWSCACHE);
178 if (!NewsCacheDir || !*NewsCacheDir)
181 strfcpy (dir, NewsCacheDir, sizeof (dir));
182 mutt_expand_path (dir, sizeof (dir));
184 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
185 snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"),
187 if (mutt_yesorno (buf, M_YES) != M_YES
188 || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) {
189 mutt_error _("Cache directory not created!");
196 set_option (OPTNEWSCACHE);
199 snprintf (buf, sizeof (buf), "%s/.index", dir);
200 if (!(index = safe_fopen (buf, "a+")))
203 while (fgets (buf, sizeof (buf), index)) {
204 buf[mutt_strlen (buf) - 1] = 0; /* strip ending '\n' */
205 if (!mutt_strncmp (buf, "#: ", 3) &&
206 !mutt_strcasecmp (buf + 3, news->conn->account.host))
209 while (fgets (buf, sizeof (buf), index)) {
211 while (*cp && *cp != ' ')
216 if (!mutt_strcmp (buf, "#:"))
218 sscanf (cp + 1, "%s %d %d", file, &l, &m);
219 if (!mutt_strcmp (buf, "ALL")) {
220 news->cache = safe_strdup (file);
221 news->newgroups_time = m;
223 else if (news->newsgroups) {
224 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, buf)) == NULL) {
226 (NNTP_DATA *) safe_calloc (1,
227 sizeof (NNTP_DATA) + mutt_strlen (buf) + 1);
228 data->group = (char *) data + sizeof (NNTP_DATA);
229 strcpy (data->group, buf);
232 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
234 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
235 hash_insert (news->newsgroups, data->group, data, 0);
236 nntp_add_to_list (news, data);
238 data->cache = safe_strdup (file);
240 if (!data->firstMessage || data->lastMessage < m)
242 if (!data->firstMessage)
243 data->firstMessage = l;
244 if (data->lastMessage < m)
245 data->lastMessage = m;
246 data->lastCached = m;
247 if (t || !data->unread)
248 mutt_newsgroup_stat (data);
255 const char *nntp_format_str (char *dest, size_t destlen, char op,
256 const char *src, const char *fmt,
257 const char *ifstring, const char *elsestring,
258 unsigned long data, format_flag flags)
260 char fn[SHORT_STRING], tmp[SHORT_STRING];
264 strncpy (fn, NewsServer, sizeof (fn) - 1);
266 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
267 snprintf (dest, destlen, tmp, fn);
273 /* nntp_parse_url: given an NNPT URL, return host, port,
274 * username, password and newsgroup will recognise. */
275 int nntp_parse_url (const char *server, ACCOUNT * acct,
276 char *group, size_t group_len)
284 acct->port = NNTP_PORT;
285 acct->type = M_ACCT_TYPE_NNTP;
287 c = safe_strdup (server);
288 url_parse_ciss (&url, c);
290 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
291 if (url.scheme == U_NNTPS) {
292 acct->flags |= M_ACCT_SSL;
293 acct->port = NNTP_SSL_PORT;
298 strfcpy (group, url.path, group_len);
300 ret = mutt_account_fromurl (acct, &url);
307 void nntp_expand_path (char *line, size_t len, ACCOUNT * acct)
311 url.path = safe_strdup (line);
312 mutt_account_tourl (acct, &url);
313 url_ciss_tostring (&url, line, len, 0);
318 * Automatically loads a newsrc into memory, if necessary.
319 * Checks the size/mtime of a newsrc file, if it doesn't match, load
320 * again. Hmm, if a system has broken mtimes, this might mean the file
321 * is reloaded every time, which we'd have to fix.
323 * a newsrc file is a line per newsgroup, with the newsgroup, then a
324 * ':' denoting subscribed or '!' denoting unsubscribed, then a
325 * comma separated list of article numbers and ranges.
327 NNTP_SERVER *mutt_select_newsserver (char *server)
329 char file[_POSIX_PATH_MAX];
336 if (!server || !*server) {
337 mutt_error _("No newsserver defined!");
342 buf = p = safe_calloc (mutt_strlen (server) + 10, sizeof (char));
343 if (url_check_scheme (server) == U_UNKNOWN) {
344 strcpy (buf, "nntp://");
345 p = strchr (buf, '\0');
349 if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file) {
351 mutt_error (_("%s is an invalid newsserver specification!"), server);
356 conn = mutt_conn_find (NULL, &acct);
360 mutt_FormatString (file, sizeof (file), NONULL (NewsRc), nntp_format_str, 0,
362 mutt_expand_path (file, sizeof (file));
364 serv = (NNTP_SERVER *) conn->data;
368 /* externally modified? */
369 if (serv->stat != stat (file, &sb) || (!serv->stat &&
370 (serv->size != sb.st_size
371 || serv->mtime != sb.st_mtime))) {
372 for (list = serv->list; list; list = list->next) {
373 NNTP_DATA *data = (NNTP_DATA *) list->data;
376 data->subscribed = 0;
382 nntp_clear_cacheindex (serv);
385 if (serv->status == NNTP_BYE)
386 serv->status = NNTP_NONE;
387 nntp_check_newgroups (serv, 0);
392 serv = safe_calloc (1, sizeof (NNTP_SERVER));
394 serv->newsrc = safe_strdup (file);
395 serv->newsgroups = hash_create (1009);
396 slurp_newsrc (serv); /* load .newsrc */
397 nntp_parse_cacheindex (serv); /* load .index */
398 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
399 nntp_check_newgroups (serv, 1);
400 else if (nntp_get_active (serv) < 0) {
401 hash_destroy (&serv->newsgroups, nntp_delete_data);
402 for (list = serv->list; list; list = list->next)
404 mutt_free_list (&serv->list);
405 FREE (&serv->newsrc);
410 nntp_clear_cacheindex (serv);
411 conn->data = (void *) serv;
417 * full status flags are not supported by nntp, but we can fake some
418 * of them. This is how:
419 * Read = a read message number is in the .newsrc
420 * New = a message is new since we last read this newsgroup
421 * Old = anything else
422 * So, Read is marked as such in the newsrc, old is anything that is
423 * "skipped" in the newsrc, and new is anything not in the newsrc nor
424 * in the cache. By skipped, I mean before the last unread message
426 void nntp_get_status (CONTEXT * ctx, HEADER * h, char *group, int article)
428 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
432 data = (NNTP_DATA *) hash_find (data->nserv->newsgroups, group);
437 dprint (3, (debugfile, "newsgroup %s not found\n", group));
442 for (x = 0; x < data->num; x++) {
443 if ((article >= data->entries[x].first) &&
444 (article <= data->entries[x].last)) {
445 /* we cannot use mutt_set_flag() because mx_update_context()
451 /* If article was not cached yet, it is new! :) */
452 if (!data->cache || article > data->lastCached)
454 /* Old articles are articles which aren't read but an article after them
456 if (option (OPTMARKOLD))
460 void mutt_newsgroup_stat (NNTP_DATA * data)
463 unsigned int first, last;
466 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
469 data->unread = data->lastMessage - data->firstMessage + 1;
470 for (i = 0; i < data->num; i++) {
471 first = data->entries[i].first;
472 if (first < data->firstMessage)
473 first = data->firstMessage;
474 last = data->entries[i].last;
475 if (last > data->lastMessage)
476 last = data->lastMessage;
478 data->unread -= last - first + 1;
482 static int puti (char *line, int num)
487 *p++ = '0' + num % 10;
491 *line++ = *--p, num++;
496 static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf,
497 char **pline, size_t * buflen)
500 size_t len = *buflen - (*pline - *buf);
503 if (len < LONG_STRING * 10) {
507 safe_realloc (buf, *buflen);
508 line = *buf + (*pline - line);
510 strcpy (line, data->group);
511 len -= mutt_strlen (line) + 1;
512 line += mutt_strlen (line);
513 *line++ = data->subscribed ? ':' : '!';
517 for (x = 0; x < data->num; x++) {
518 if (len < LONG_STRING) {
523 safe_realloc (buf, *buflen);
524 line = *buf + (*pline - line);
532 if (data->entries[x].first == data->entries[x].last)
533 snprintf (line, len, "%d%n", data->entries[x].first, &i);
535 snprintf (line, len, "%d-%d%n",
536 data->entries[x].first, data->entries[x].last, &i);
540 i = puti (line, data->entries[x].first);
543 if (data->entries[x].first != data->entries[x].last) {
546 i = puti (line, data->entries[x].last);
557 void newsrc_gen_entries (CONTEXT * ctx)
559 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
561 unsigned int last = 0, first = 1;
562 int save_sort = SORT_ORDER;
564 if (Sort != SORT_ORDER) {
567 mutt_sort_headers (ctx, 0);
571 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
576 * Set up to fake initial sequence from 1 to the article before the
577 * first article in our list
582 for (x = 0; x < ctx->msgcount; x++) {
583 if (series) { /* search for first unread */
585 * We don't actually check sequential order, since we mark
586 * "missing" entries as read/deleted
588 last = ctx->hdrs[x]->article_num;
589 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
590 !ctx->hdrs[x]->read) {
591 if (data->num >= data->max) {
592 data->max = data->max * 2;
593 safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
595 data->entries[data->num].first = first;
596 data->entries[data->num].last = last - 1;
601 else { /* search for first read */
603 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
607 last = ctx->hdrs[x]->article_num;
610 if (series && first <= data->lastLoaded) {
611 if (data->num >= data->max) {
612 data->max = data->max * 2;
613 safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
615 data->entries[data->num].first = first;
616 data->entries[data->num].last = data->lastLoaded;
620 if (save_sort != Sort) {
622 mutt_sort_headers (ctx, 0);
626 int mutt_newsrc_update (NNTP_SERVER * news)
636 llen = len = 10 * LONG_STRING;
637 line = buf = safe_calloc (1, len);
638 /* we will generate full newsrc here */
639 for (tmp = news->list; tmp; tmp = tmp->next) {
640 data = (NNTP_DATA *) tmp->data;
641 if (!data || !data->rc)
643 nntp_create_newsrc_line (data, &buf, &line, &llen);
644 dprint (2, (debugfile, "Added to newsrc: %s", line));
645 line += mutt_strlen (line);
647 /* newrc being fully rewritten */
649 (r = mutt_update_list_file (news->newsrc, NULL, "", buf)) == 0) {
652 stat (news->newsrc, &st);
653 news->size = st.st_size;
654 news->mtime = st.st_mtime;
660 static FILE *mutt_mkname (char *s)
662 char buf[_POSIX_PATH_MAX], *pc;
666 nntp_cache_expand (buf, s);
667 if ((fp = safe_fopen (buf, "w")))
670 nntp_cache_expand (buf, "cache-XXXXXX");
671 pc = buf + mutt_strlen (buf) - 12; /* positioning to "cache-XXXXXX" */
672 if ((fd = mkstemp (buf)) == -1)
674 strcpy (s, pc); /* generated name */
675 return fdopen (fd, "w");
678 /* Updates info into .index file: ALL or about selected newsgroup */
679 static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data)
681 char buf[LONG_STRING], *key = "ALL";
682 char file[_POSIX_PATH_MAX];
684 if (!serv || !serv->conn || !serv->conn->account.host)
687 if (data && data->group) {
689 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
690 data->firstMessage, data->lastLoaded);
693 strfcpy (file, serv->cache, sizeof (file));
694 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
695 (int) serv->newgroups_time);
697 nntp_cache_expand (file, ".index");
698 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
701 /* Remove cache files of unsubscribed newsgroups */
702 void nntp_clear_cacheindex (NNTP_SERVER * news)
707 if (option (OPTSAVEUNSUB) || !news)
710 for (tmp = news->list; tmp; tmp = tmp->next) {
711 data = (NNTP_DATA *) tmp->data;
712 if (!data || data->subscribed || !data->cache)
714 nntp_delete_cache (data);
715 dprint (2, (debugfile, "Removed from .index: %s\n", data->group));
720 int nntp_save_cache_index (NNTP_SERVER * news)
722 char buf[HUGE_STRING];
723 char file[_POSIX_PATH_MAX];
728 if (!news || !news->newsgroups)
730 if (!option (OPTNEWSCACHE))
734 nntp_cache_expand (file, news->cache);
736 f = safe_fopen (file, "w");
739 strfcpy (buf, news->conn->account.host, sizeof (buf));
740 f = mutt_mkname (buf);
741 news->cache = safe_strdup (buf);
742 nntp_cache_expand (file, buf);
747 for (l = news->list; l; l = l->next) {
748 if ((d = (NNTP_DATA *) l->data) && !d->deleted) {
750 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
751 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
754 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
755 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
756 if (fputs (buf, f) == EOF) {
765 if (nntp_update_cacheindex (news, NULL)) {
772 int nntp_save_cache_group (CONTEXT * ctx)
774 char buf[HUGE_STRING], addr[STRING];
775 char file[_POSIX_PATH_MAX];
779 int i = 0, save = SORT_ORDER;
782 if (!option (OPTNEWSCACHE))
784 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
787 if (((NNTP_DATA *) ctx->data)->cache) {
788 nntp_cache_expand (file, ((NNTP_DATA *) ctx->data)->cache);
790 f = safe_fopen (file, "w");
793 snprintf (buf, sizeof (buf), "%s-%s",
794 ((NNTP_DATA *) ctx->data)->nserv->conn->account.host,
795 ((NNTP_DATA *) ctx->data)->group);
796 f = mutt_mkname (buf);
797 ((NNTP_DATA *) ctx->data)->cache = safe_strdup (buf);
798 nntp_cache_expand (file, buf);
803 if (Sort != SORT_ORDER) {
806 mutt_sort_headers (ctx, 0);
809 /* Save only $nntp_context messages... */
810 ((NNTP_DATA *) ctx->data)->lastCached = 0;
811 if (NntpContext && ctx->msgcount > NntpContext)
812 i = ctx->msgcount - NntpContext;
813 for (; i < ctx->msgcount; i++) {
814 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
817 rfc822_write_address (addr, sizeof (addr), h->env->from, 0);
818 tm = gmtime (&h->date_sent);
819 snprintf (buf, sizeof (buf),
820 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
821 h->article_num, h->env->subject, addr, tm->tm_mday,
822 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
823 tm->tm_min, tm->tm_sec, h->env->message_id);
825 if (h->env->references)
826 mutt_write_references (h->env->references, f);
827 snprintf (buf, sizeof (buf), "\t%ld\t%d\tXref: %s\n",
828 h->content->length, h->lines, NONULL (h->env->xref));
829 if (fputs (buf, f) == EOF) {
835 prev = ctx->hdrs[i]->article_num;
840 mutt_sort_headers (ctx, 0);
844 if (nntp_update_cacheindex (((NNTP_DATA *) ctx->data)->nserv,
845 (NNTP_DATA *) ctx->data)) {
849 ((NNTP_DATA *) ctx->data)->lastCached =
850 ((NNTP_DATA *) ctx->data)->lastLoaded;
854 void nntp_delete_cache (NNTP_DATA * data)
856 char buf[_POSIX_PATH_MAX];
858 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
861 nntp_cache_expand (buf, data->cache);
864 data->lastCached = 0;
865 nntp_cache_expand (buf, ".index");
866 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
870 NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
874 if (!news || !news->newsgroups || !group || !*group)
876 if (!(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) {
878 (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + mutt_strlen (group) + 1);
879 data->group = (char *) data + sizeof (NNTP_DATA);
880 strcpy (data->group, group);
883 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
885 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
886 hash_insert (news->newsgroups, data->group, data, 0);
887 nntp_add_to_list (news, data);
889 if (!data->subscribed) {
890 data->subscribed = 1;
896 NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group)
900 if (!news || !news->newsgroups || !group || !*group ||
901 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
903 if (data->subscribed) {
904 data->subscribed = 0;
905 if (!option (OPTSAVEUNSUB))
911 NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
915 if (!news || !news->newsgroups || !group || !*group ||
916 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
919 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
923 data->entries[0].first = 1;
925 data->entries[0].last = data->lastMessage;
926 if (Context && Context->data == data) {
929 for (x = 0; x < Context->msgcount; x++)
930 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
935 NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
939 if (!news || !news->newsgroups || !group || !*group ||
940 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
943 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
947 data->entries[0].first = 1;
948 data->entries[0].last = data->firstMessage - 1;
949 if (Context && Context->data == data) {
952 data->unread = Context->msgcount;
953 for (x = 0; x < Context->msgcount; x++)
954 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
957 data->unread = data->lastMessage - data->entries[0].last;
961 /* this routine gives the first newsgroup with new messages */
962 void nntp_buffy (char *s)
966 for (list = CurrentNewsSrv->list; list; list = list->next) {
967 NNTP_DATA *data = (NNTP_DATA *) list->data;
969 if (data && data->subscribed && data->unread) {
970 if (Context && Context->magic == M_NNTP &&
971 !mutt_strcmp (data->group, ((NNTP_DATA *) Context->data)->group)) {
972 unsigned int i, unread = 0;
974 for (i = 0; i < Context->msgcount; i++)
975 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
980 strcpy (s, data->group);