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"
37 void nntp_add_to_list (NNTP_SERVER * s, NNTP_DATA * d)
44 l = safe_calloc (1, sizeof (LIST));
53 static int nntp_parse_newsrc_line (NNTP_SERVER * news, char *line)
56 char group[LONG_STRING];
58 char *p = line, *b, *h;
67 while (*p && (*p != ':' && *p != '!'))
72 if (len > sizeof (group))
74 strfcpy (group, line, len);
75 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, group)) == NULL) {
77 (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + mutt_strlen (group) + 1);
78 data->group = (char *) data + sizeof (NNTP_DATA);
79 strcpy (data->group, group);
82 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
84 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));
104 while (*p && *p != ',' && *p != '\n')
110 if ((h = strchr (b, '-'))) {
113 data->entries[x].first = atoi (b);
114 data->entries[x].last = atoi (h);
117 data->entries[x].first = atoi (b);
118 data->entries[x].last = data->entries[x].first;
121 if (data->entries[x].last != 0)
124 if (x && !data->lastMessage)
125 data->lastMessage = data->entries[x - 1].last;
127 mutt_newsgroup_stat (data);
128 dprint (2, (debugfile, "parse_line: Newsgroup %s\n", data->group));
133 static int slurp_newsrc (NNTP_SERVER * news)
139 news->stat = stat (news->newsrc, &sb);
140 news->size = sb.st_size;
141 news->mtime = sb.st_mtime;
143 if ((fp = safe_fopen (news->newsrc, "r")) == NULL)
145 /* hmm, should we use dotlock? */
146 if (mx_lock_file (news->newsrc, fileno (fp), 0, 0, 1)) {
151 buf = safe_malloc (sb.st_size + 1);
152 while (fgets (buf, sb.st_size + 1, fp))
153 nntp_parse_newsrc_line (news, buf);
156 mx_unlock_file (news->newsrc, fileno (fp), 0);
161 void nntp_cache_expand (char *dst, const char *src)
163 snprintf (dst, _POSIX_PATH_MAX, "%s/%s", NewsCacheDir, src);
164 mutt_expand_path (dst, _POSIX_PATH_MAX);
167 /* Loads $news_cache_dir/.index into memory, loads newsserver data
168 * and newsgroup cache names */
169 static int nntp_parse_cacheindex (NNTP_SERVER * news)
172 char buf[HUGE_STRING], *cp;
173 char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX];
178 /* check is server name defined or not */
179 if (!news || !news->conn || !news->conn->account.host)
181 unset_option (OPTNEWSCACHE);
182 if (!NewsCacheDir || !*NewsCacheDir)
185 strfcpy (dir, NewsCacheDir, sizeof (dir));
186 mutt_expand_path (dir, sizeof (dir));
188 if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) {
189 snprintf (buf, sizeof (buf), _("Directory %s not exist. Create it?"),
191 if (mutt_yesorno (buf, M_YES) != M_YES
192 || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) {
193 mutt_error _("Cache directory not created!");
200 set_option (OPTNEWSCACHE);
203 snprintf (buf, sizeof (buf), "%s/.index", dir);
204 if (!(index = safe_fopen (buf, "a+")))
207 while (fgets (buf, sizeof (buf), index)) {
208 buf[mutt_strlen (buf) - 1] = 0; /* strip ending '\n' */
209 if (!mutt_strncmp (buf, "#: ", 3) &&
210 !mutt_strcasecmp (buf + 3, news->conn->account.host))
213 while (fgets (buf, sizeof (buf), index)) {
215 while (*cp && *cp != ' ')
220 if (!mutt_strcmp (buf, "#:"))
222 sscanf (cp + 1, "%s %d %d", file, &l, &m);
223 if (!mutt_strcmp (buf, "ALL")) {
224 news->cache = safe_strdup (file);
225 news->newgroups_time = m;
227 else if (news->newsgroups) {
228 if ((data = (NNTP_DATA *) hash_find (news->newsgroups, buf)) == NULL) {
230 (NNTP_DATA *) safe_calloc (1,
231 sizeof (NNTP_DATA) + mutt_strlen (buf) + 1);
232 data->group = (char *) data + sizeof (NNTP_DATA);
233 strcpy (data->group, buf);
236 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
238 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
239 hash_insert (news->newsgroups, data->group, data, 0);
240 nntp_add_to_list (news, data);
242 data->cache = safe_strdup (file);
244 if (!data->firstMessage || data->lastMessage < m)
246 if (!data->firstMessage)
247 data->firstMessage = l;
248 if (data->lastMessage < m)
249 data->lastMessage = m;
250 data->lastCached = m;
251 if (t || !data->unread)
252 mutt_newsgroup_stat (data);
259 const char *nntp_format_str (char *dest, size_t destlen, char op,
260 const char *src, const char *fmt,
261 const char *ifstring, const char *elsestring,
262 unsigned long data, format_flag flags)
264 char fn[SHORT_STRING], tmp[SHORT_STRING];
268 strncpy (fn, NewsServer, sizeof (fn) - 1);
270 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
271 snprintf (dest, destlen, tmp, fn);
277 /* nntp_parse_url: given an NNPT URL, return host, port,
278 * username, password and newsgroup will recognise. */
279 int nntp_parse_url (const char *server, ACCOUNT * acct,
280 char *group, size_t group_len)
288 acct->port = NNTP_PORT;
289 acct->type = M_ACCT_TYPE_NNTP;
291 c = safe_strdup (server);
292 url_parse_ciss (&url, c);
294 if (url.scheme == U_NNTP || url.scheme == U_NNTPS) {
295 if (url.scheme == U_NNTPS) {
296 acct->flags |= M_ACCT_SSL;
297 acct->port = NNTP_SSL_PORT;
302 strfcpy (group, url.path, group_len);
304 ret = mutt_account_fromurl (acct, &url);
311 void nntp_expand_path (char *line, size_t len, ACCOUNT * acct)
315 url.path = safe_strdup (line);
316 mutt_account_tourl (acct, &url);
317 url_ciss_tostring (&url, line, len, 0);
322 * Automatically loads a newsrc into memory, if necessary.
323 * Checks the size/mtime of a newsrc file, if it doesn't match, load
324 * again. Hmm, if a system has broken mtimes, this might mean the file
325 * is reloaded every time, which we'd have to fix.
327 * a newsrc file is a line per newsgroup, with the newsgroup, then a
328 * ':' denoting subscribed or '!' denoting unsubscribed, then a
329 * comma separated list of article numbers and ranges.
331 NNTP_SERVER *mutt_select_newsserver (char *server)
333 char file[_POSIX_PATH_MAX];
340 if (!server || !*server) {
341 mutt_error _("No newsserver defined!");
346 buf = p = safe_calloc (mutt_strlen (server) + 10, sizeof (char));
347 if (url_check_scheme (server) == U_UNKNOWN) {
348 strcpy (buf, "nntp://");
349 p = strchr (buf, '\0');
353 if ((nntp_parse_url (buf, &acct, file, sizeof (file))) < 0 || *file) {
355 mutt_error (_("%s is an invalid newsserver specification!"), server);
360 conn = mutt_conn_find (NULL, &acct);
364 mutt_FormatString (file, sizeof (file), NONULL (NewsRc), nntp_format_str, 0,
366 mutt_expand_path (file, sizeof (file));
368 serv = (NNTP_SERVER *) conn->data;
372 /* externally modified? */
373 if (serv->stat != stat (file, &sb) || (!serv->stat &&
374 (serv->size != sb.st_size
375 || serv->mtime != sb.st_mtime))) {
376 for (list = serv->list; list; list = list->next) {
377 NNTP_DATA *data = (NNTP_DATA *) list->data;
380 data->subscribed = 0;
386 nntp_clear_cacheindex (serv);
389 if (serv->status == NNTP_BYE)
390 serv->status = NNTP_NONE;
391 nntp_check_newgroups (serv, 0);
396 serv = safe_calloc (1, sizeof (NNTP_SERVER));
398 serv->newsrc = safe_strdup (file);
399 serv->newsgroups = hash_create (1009);
400 slurp_newsrc (serv); /* load .newsrc */
401 nntp_parse_cacheindex (serv); /* load .index */
402 if (option (OPTNEWSCACHE) && serv->cache && nntp_get_cache_all (serv) >= 0)
403 nntp_check_newgroups (serv, 1);
404 else if (nntp_get_active (serv) < 0) {
405 hash_destroy (&serv->newsgroups, nntp_delete_data);
406 for (list = serv->list; list; list = list->next)
408 mutt_free_list (&serv->list);
409 FREE (&serv->newsrc);
414 nntp_clear_cacheindex (serv);
415 conn->data = (void *) serv;
421 * full status flags are not supported by nntp, but we can fake some
422 * of them. This is how:
423 * Read = a read message number is in the .newsrc
424 * New = a message is new since we last read this newsgroup
425 * Old = anything else
426 * So, Read is marked as such in the newsrc, old is anything that is
427 * "skipped" in the newsrc, and new is anything not in the newsrc nor
428 * in the cache. By skipped, I mean before the last unread message
430 void nntp_get_status (CONTEXT * ctx, HEADER * h, char *group, int article)
432 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
436 data = (NNTP_DATA *) hash_find (data->nserv->newsgroups, group);
441 dprint (3, (debugfile, "newsgroup %s not found\n", group));
446 for (x = 0; x < data->num; x++) {
447 if ((article >= data->entries[x].first) &&
448 (article <= data->entries[x].last)) {
449 /* we cannot use mutt_set_flag() because mx_update_context()
455 /* If article was not cached yet, it is new! :) */
456 if (!data->cache || article > data->lastCached)
458 /* Old articles are articles which aren't read but an article after them
460 if (option (OPTMARKOLD))
464 void mutt_newsgroup_stat (NNTP_DATA * data)
467 unsigned int first, last;
470 if (data->lastMessage == 0 || data->firstMessage > data->lastMessage)
473 data->unread = data->lastMessage - data->firstMessage + 1;
474 for (i = 0; i < data->num; i++) {
475 first = data->entries[i].first;
476 if (first < data->firstMessage)
477 first = data->firstMessage;
478 last = data->entries[i].last;
479 if (last > data->lastMessage)
480 last = data->lastMessage;
482 data->unread -= last - first + 1;
486 static int puti (char *line, int num)
491 *p++ = '0' + num % 10;
495 *line++ = *--p, num++;
500 static void nntp_create_newsrc_line (NNTP_DATA * data, char **buf,
501 char **pline, size_t * buflen)
504 size_t len = *buflen - (*pline - *buf);
507 if (len < LONG_STRING * 10) {
511 safe_realloc (buf, *buflen);
512 line = *buf + (*pline - line);
514 strcpy (line, data->group);
515 len -= mutt_strlen (line) + 1;
516 line += mutt_strlen (line);
517 *line++ = data->subscribed ? ':' : '!';
521 for (x = 0; x < data->num; x++) {
522 if (len < LONG_STRING) {
527 safe_realloc (buf, *buflen);
528 line = *buf + (*pline - line);
536 if (data->entries[x].first == data->entries[x].last)
537 snprintf (line, len, "%d%n", data->entries[x].first, &i);
539 snprintf (line, len, "%d-%d%n",
540 data->entries[x].first, data->entries[x].last, &i);
544 i = puti (line, data->entries[x].first);
547 if (data->entries[x].first != data->entries[x].last) {
550 i = puti (line, data->entries[x].last);
561 void newsrc_gen_entries (CONTEXT * ctx)
563 NNTP_DATA *data = (NNTP_DATA *) ctx->data;
565 unsigned int last = 0, first = 1;
566 int save_sort = SORT_ORDER;
568 if (Sort != SORT_ORDER) {
571 mutt_sort_headers (ctx, 0);
575 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
580 * Set up to fake initial sequence from 1 to the article before the
581 * first article in our list
586 for (x = 0; x < ctx->msgcount; x++) {
587 if (series) { /* search for first unread */
589 * We don't actually check sequential order, since we mark
590 * "missing" entries as read/deleted
592 last = ctx->hdrs[x]->article_num;
593 if (last >= data->firstMessage && !ctx->hdrs[x]->deleted &&
594 !ctx->hdrs[x]->read) {
595 if (data->num >= data->max) {
596 data->max = data->max * 2;
597 safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
599 data->entries[data->num].first = first;
600 data->entries[data->num].last = last - 1;
605 else { /* search for first read */
607 if (ctx->hdrs[x]->deleted || ctx->hdrs[x]->read) {
611 last = ctx->hdrs[x]->article_num;
614 if (series && first <= data->lastLoaded) {
615 if (data->num >= data->max) {
616 data->max = data->max * 2;
617 safe_realloc (&data->entries, data->max * sizeof (NEWSRC_ENTRY));
619 data->entries[data->num].first = first;
620 data->entries[data->num].last = data->lastLoaded;
624 if (save_sort != Sort) {
626 mutt_sort_headers (ctx, 0);
630 int mutt_newsrc_update (NNTP_SERVER * news)
640 llen = len = 10 * LONG_STRING;
641 line = buf = safe_calloc (1, len);
642 /* we will generate full newsrc here */
643 for (tmp = news->list; tmp; tmp = tmp->next) {
644 data = (NNTP_DATA *) tmp->data;
645 if (!data || !data->rc)
647 nntp_create_newsrc_line (data, &buf, &line, &llen);
648 dprint (2, (debugfile, "Added to newsrc: %s", line));
649 line += mutt_strlen (line);
651 /* newrc being fully rewritten */
653 (r = mutt_update_list_file (news->newsrc, NULL, "", buf)) == 0) {
656 stat (news->newsrc, &st);
657 news->size = st.st_size;
658 news->mtime = st.st_mtime;
664 static FILE *mutt_mkname (char *s)
666 char buf[_POSIX_PATH_MAX], *pc;
670 nntp_cache_expand (buf, s);
671 if ((fp = safe_fopen (buf, "w")))
674 nntp_cache_expand (buf, "cache-XXXXXX");
675 pc = buf + mutt_strlen (buf) - 12; /* positioning to "cache-XXXXXX" */
676 if ((fd = mkstemp (buf)) == -1)
678 strcpy (s, pc); /* generated name */
679 return fdopen (fd, "w");
682 /* Updates info into .index file: ALL or about selected newsgroup */
683 static int nntp_update_cacheindex (NNTP_SERVER * serv, NNTP_DATA * data)
685 char buf[LONG_STRING], *key = "ALL";
686 char file[_POSIX_PATH_MAX];
688 if (!serv || !serv->conn || !serv->conn->account.host)
691 if (data && data->group) {
693 snprintf (buf, sizeof (buf), "%s %s %d %d", key, data->cache,
694 data->firstMessage, data->lastLoaded);
697 strfcpy (file, serv->cache, sizeof (file));
698 snprintf (buf, sizeof (buf), "ALL %s 0 %d", file,
699 (int) serv->newgroups_time);
701 nntp_cache_expand (file, ".index");
702 return mutt_update_list_file (file, serv->conn->account.host, key, buf);
705 /* Remove cache files of unsubscribed newsgroups */
706 void nntp_clear_cacheindex (NNTP_SERVER * news)
711 if (option (OPTSAVEUNSUB) || !news)
714 for (tmp = news->list; tmp; tmp = tmp->next) {
715 data = (NNTP_DATA *) tmp->data;
716 if (!data || data->subscribed || !data->cache)
718 nntp_delete_cache (data);
719 dprint (2, (debugfile, "Removed from .index: %s\n", data->group));
724 int nntp_save_cache_index (NNTP_SERVER * news)
726 char buf[HUGE_STRING];
727 char file[_POSIX_PATH_MAX];
732 if (!news || !news->newsgroups)
734 if (!option (OPTNEWSCACHE))
738 nntp_cache_expand (file, news->cache);
740 f = safe_fopen (file, "w");
743 strfcpy (buf, news->conn->account.host, sizeof (buf));
744 f = mutt_mkname (buf);
745 news->cache = safe_strdup (buf);
746 nntp_cache_expand (file, buf);
751 for (l = news->list; l; l = l->next) {
752 if ((d = (NNTP_DATA *) l->data) && !d->deleted) {
754 snprintf (buf, sizeof (buf), "%s %d %d %c %s\n", d->group,
755 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n',
758 snprintf (buf, sizeof (buf), "%s %d %d %c\n", d->group,
759 d->lastMessage, d->firstMessage, d->allowed ? 'y' : 'n');
760 if (fputs (buf, f) == EOF) {
769 if (nntp_update_cacheindex (news, NULL)) {
776 int nntp_save_cache_group (CONTEXT * ctx)
778 char buf[HUGE_STRING], addr[STRING];
779 char file[_POSIX_PATH_MAX];
783 int i = 0, save = SORT_ORDER;
786 if (!option (OPTNEWSCACHE))
788 if (!ctx || !ctx->data || ctx->magic != M_NNTP)
791 if (((NNTP_DATA *) ctx->data)->cache) {
792 nntp_cache_expand (file, ((NNTP_DATA *) ctx->data)->cache);
794 f = safe_fopen (file, "w");
797 snprintf (buf, sizeof (buf), "%s-%s",
798 ((NNTP_DATA *) ctx->data)->nserv->conn->account.host,
799 ((NNTP_DATA *) ctx->data)->group);
800 f = mutt_mkname (buf);
801 ((NNTP_DATA *) ctx->data)->cache = safe_strdup (buf);
802 nntp_cache_expand (file, buf);
807 if (Sort != SORT_ORDER) {
810 mutt_sort_headers (ctx, 0);
813 /* Save only $nntp_context messages... */
814 ((NNTP_DATA *) ctx->data)->lastCached = 0;
815 if (NntpContext && ctx->msgcount > NntpContext)
816 i = ctx->msgcount - NntpContext;
817 for (; i < ctx->msgcount; i++) {
818 if (!ctx->hdrs[i]->deleted && ctx->hdrs[i]->article_num != prev) {
821 rfc822_write_address (addr, sizeof (addr), h->env->from, 0);
822 tm = gmtime (&h->date_sent);
823 snprintf (buf, sizeof (buf),
824 "%d\t%s\t%s\t%d %s %d %02d:%02d:%02d GMT\t%s\t",
825 h->article_num, h->env->subject, addr, tm->tm_mday,
826 Months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour,
827 tm->tm_min, tm->tm_sec, h->env->message_id);
829 if (h->env->references)
830 mutt_write_references (h->env->references, f);
831 snprintf (buf, sizeof (buf), "\t%ld\t%d\tXref: %s\n",
832 h->content->length, h->lines, NONULL (h->env->xref));
833 if (fputs (buf, f) == EOF) {
839 prev = ctx->hdrs[i]->article_num;
844 mutt_sort_headers (ctx, 0);
848 if (nntp_update_cacheindex (((NNTP_DATA *) ctx->data)->nserv,
849 (NNTP_DATA *) ctx->data)) {
853 ((NNTP_DATA *) ctx->data)->lastCached =
854 ((NNTP_DATA *) ctx->data)->lastLoaded;
858 void nntp_delete_cache (NNTP_DATA * data)
860 char buf[_POSIX_PATH_MAX];
862 if (!option (OPTNEWSCACHE) || !data || !data->cache || !data->nserv)
865 nntp_cache_expand (buf, data->cache);
868 data->lastCached = 0;
869 nntp_cache_expand (buf, ".index");
870 mutt_update_list_file (buf, data->nserv->conn->account.host, data->group,
874 NNTP_DATA *mutt_newsgroup_subscribe (NNTP_SERVER * news, char *group)
878 if (!news || !news->newsgroups || !group || !*group)
880 if (!(data = (NNTP_DATA *) hash_find (news->newsgroups, group))) {
882 (NNTP_DATA *) safe_calloc (1, sizeof (NNTP_DATA) + mutt_strlen (group) + 1);
883 data->group = (char *) data + sizeof (NNTP_DATA);
884 strcpy (data->group, group);
887 if (news->newsgroups->nelem < news->newsgroups->curnelem * 2)
889 hash_resize (news->newsgroups, news->newsgroups->nelem * 2);
890 hash_insert (news->newsgroups, data->group, data, 0);
891 nntp_add_to_list (news, data);
893 if (!data->subscribed) {
894 data->subscribed = 1;
900 NNTP_DATA *mutt_newsgroup_unsubscribe (NNTP_SERVER * news, char *group)
904 if (!news || !news->newsgroups || !group || !*group ||
905 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
907 if (data->subscribed) {
908 data->subscribed = 0;
909 if (!option (OPTSAVEUNSUB))
915 NNTP_DATA *mutt_newsgroup_catchup (NNTP_SERVER * news, char *group)
919 if (!news || !news->newsgroups || !group || !*group ||
920 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
923 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
927 data->entries[0].first = 1;
929 data->entries[0].last = data->lastMessage;
930 if (Context && Context->data == data) {
933 for (x = 0; x < Context->msgcount; x++)
934 mutt_set_flag (Context, Context->hdrs[x], M_READ, 1);
939 NNTP_DATA *mutt_newsgroup_uncatchup (NNTP_SERVER * news, char *group)
943 if (!news || !news->newsgroups || !group || !*group ||
944 !(data = (NNTP_DATA *) hash_find (news->newsgroups, group)))
947 data->entries = safe_calloc (5, sizeof (NEWSRC_ENTRY));
951 data->entries[0].first = 1;
952 data->entries[0].last = data->firstMessage - 1;
953 if (Context && Context->data == data) {
956 data->unread = Context->msgcount;
957 for (x = 0; x < Context->msgcount; x++)
958 mutt_set_flag (Context, Context->hdrs[x], M_READ, 0);
961 data->unread = data->lastMessage - data->entries[0].last;
965 /* this routine gives the first newsgroup with new messages */
966 void nntp_buffy (char *s)
970 for (list = CurrentNewsSrv->list; list; list = list->next) {
971 NNTP_DATA *data = (NNTP_DATA *) list->data;
973 if (data && data->subscribed && data->unread) {
974 if (Context && Context->magic == M_NNTP &&
975 !mutt_strcmp (data->group, ((NNTP_DATA *) Context->data)->group)) {
976 unsigned int i, unread = 0;
978 for (i = 0; i < Context->msgcount; i++)
979 if (!Context->hdrs[i]->read && !Context->hdrs[i]->deleted)
984 strcpy (s, data->group);