++/* Remove hcache and bcache of newsgroup */
++void nntp_delete_group_cache (NNTP_DATA *nntp_data)
++{
++ char file[_POSIX_PATH_MAX];
++
++ if (!nntp_data || !nntp_data->nserv || !nntp_data->nserv->cacheable)
++ return;
++
++#ifdef USE_HCACHE
++ nntp_hcache_namer (nntp_data->group, file, sizeof (file));
++ cache_expand (file, sizeof (file), &nntp_data->nserv->conn->account, file);
++ unlink (file);
++ nntp_data->lastCached = 0;
++ dprint (2, (debugfile, "nntp_delete_group_cache: %s\n", file));
++#endif
++
++ if (!nntp_data->bcache)
++ nntp_data->bcache = mutt_bcache_open (&nntp_data->nserv->conn->account,
++ nntp_data->group);
++ if (nntp_data->bcache)
++ {
++ dprint (2, (debugfile, "nntp_delete_group_cache: %s/*\n", nntp_data->group));
++ mutt_bcache_list (nntp_data->bcache, nntp_bcache_delete, NULL);
++ mutt_bcache_close (&nntp_data->bcache);
++ }
++}
++
++/* Remove hcache and bcache of all unexistent and unsubscribed newsgroups */
++void nntp_clear_cache (NNTP_SERVER *nserv)
++{
++ char file[_POSIX_PATH_MAX];
++ char *fp;
++ struct dirent *entry;
++ DIR *dp;
++
++ if (!nserv || !nserv->cacheable)
++ return;
++
++ cache_expand (file, sizeof (file), &nserv->conn->account, NULL);
++ dp = opendir (file);
++ if (dp)
++ {
++ safe_strncat (file, sizeof (file), "/", 1);
++ fp = file + strlen (file);
++ while ((entry = readdir (dp)))
++ {
++ char *group = entry->d_name;
++ struct stat sb;
++ NNTP_DATA *nntp_data;
++ NNTP_DATA nntp_tmp;
++
++ if (mutt_strcmp (group, ".") == 0 ||
++ mutt_strcmp (group, "..") == 0)
++ continue;
++ *fp = '\0';
++ safe_strncat (file, sizeof (file), group, strlen (group));
++ if (stat (file, &sb))
++ continue;
++
++#ifdef USE_HCACHE
++ if (S_ISREG (sb.st_mode))
++ {
++ char *ext = group + strlen (group) - 7;
++ if (strlen (group) < 8 || mutt_strcmp (ext, ".hcache"))
++ continue;
++ *ext = '\0';
++ }
++ else
++#endif
++ if (!S_ISDIR (sb.st_mode))
++ continue;
++
++ nntp_data = hash_find (nserv->groups_hash, group);
++ if (!nntp_data)
++ {
++ nntp_data = &nntp_tmp;
++ nntp_data->nserv = nserv;
++ nntp_data->group = group;
++ nntp_data->bcache = NULL;
++ }
++ else if (nntp_data->newsrc_ent || nntp_data->subscribed ||
++ option (OPTSAVEUNSUB))
++ continue;
++
++ nntp_delete_group_cache (nntp_data);
++ if (S_ISDIR (sb.st_mode))
++ {
++ rmdir (file);
++ dprint (2, (debugfile, "nntp_clear_cache: %s\n", file));
++ }
++ }
++ closedir (dp);
++ }
++ return;
++}
++
++/* %a = account url
++ * %p = port
++ * %P = port if specified
++ * %s = news server name
++ * %S = url schema
++ * %u = username */
++const char *
++nntp_format_str (char *dest, size_t destlen, size_t col, int cols, char op,
++ const char *src, const char *fmt, const char *ifstring,
++ const char *elsestring, unsigned long data, format_flag flags)
++{
++ NNTP_SERVER *nserv = (NNTP_SERVER *)data;
++ ACCOUNT *acct = &nserv->conn->account;
++ ciss_url_t url;
++ char fn[SHORT_STRING], tmp[SHORT_STRING], *p;
++
++ switch (op)
++ {
++ case 'a':
++ mutt_account_tourl (acct, &url);
++ url_ciss_tostring (&url, fn, sizeof (fn), U_PATH);
++ p = strchr (fn, '/');
++ if (p)
++ *p = '\0';
++ snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
++ snprintf (dest, destlen, tmp, fn);
++ break;
++ case 'p':
++ snprintf (tmp, sizeof (tmp), "%%%su", fmt);
++ snprintf (dest, destlen, tmp, acct->port);
++ break;
++ case 'P':
++ *dest = '\0';
++ if (acct->flags & MUTT_ACCT_PORT)
++ {
++ snprintf (tmp, sizeof (tmp), "%%%su", fmt);
++ snprintf (dest, destlen, tmp, acct->port);
++ }
++ break;
++ case 's':
++ strncpy (fn, acct->host, sizeof (fn) - 1);
++ mutt_strlower (fn);
++ snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
++ snprintf (dest, destlen, tmp, fn);
++ break;
++ case 'S':
++ mutt_account_tourl (acct, &url);
++ url_ciss_tostring (&url, fn, sizeof (fn), U_PATH);
++ p = strchr (fn, ':');
++ if (p)
++ *p = '\0';
++ snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
++ snprintf (dest, destlen, tmp, fn);
++ break;
++ case 'u':
++ snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
++ snprintf (dest, destlen, tmp, acct->user);
++ break;
++ }
++ return (src);
++}
++
++/* Automatically loads a newsrc into memory, if necessary.
++ * Checks the size/mtime of a newsrc file, if it doesn't match, load
++ * again. Hmm, if a system has broken mtimes, this might mean the file
++ * is reloaded every time, which we'd have to fix. */
++NNTP_SERVER *nntp_select_server (char *server, int leave_lock)
++{
++ char file[_POSIX_PATH_MAX];
++ char *p;
++ int rc;
++ struct stat sb;
++ ACCOUNT acct;
++ NNTP_SERVER *nserv;
++ NNTP_DATA *nntp_data;
++ CONNECTION *conn;
++ ciss_url_t url;
++
++ if (!server || !*server)
++ {
++ mutt_error _("No news server defined!");
++ mutt_sleep (2);
++ return NULL;
++ }
++
++ /* create account from news server url */
++ acct.flags = 0;
++ acct.port = NNTP_PORT;
++ acct.type = MUTT_ACCT_TYPE_NNTP;
++ snprintf (file, sizeof (file), "%s%s",
++ strstr (server, "://") ? "" : "news://", server);
++ if (url_parse_ciss (&url, file) < 0 ||
++ (url.path && *url.path) ||
++ !(url.scheme == U_NNTP || url.scheme == U_NNTPS) ||
++ mutt_account_fromurl (&acct, &url) < 0)
++ {
++ mutt_error (_("%s is an invalid news server specification!"), server);
++ mutt_sleep (2);
++ return NULL;
++ }
++ if (url.scheme == U_NNTPS)
++ {
++ acct.flags |= MUTT_ACCT_SSL;
++ acct.port = NNTP_SSL_PORT;
++ }
++
++ /* find connection by account */
++ conn = mutt_conn_find (NULL, &acct);
++ if (!conn)
++ return NULL;
++ if (!(conn->account.flags & MUTT_ACCT_USER) && acct.flags & MUTT_ACCT_USER)
++ {
++ conn->account.flags |= MUTT_ACCT_USER;
++ conn->account.user[0] = '\0';
++ }
++
++ /* news server already exists */
++ nserv = conn->data;
++ if (nserv)
++ {
++ if (nserv->status == NNTP_BYE)
++ nserv->status = NNTP_NONE;
++ if (nntp_open_connection (nserv) < 0)
++ return NULL;
++
++ rc = nntp_newsrc_parse (nserv);
++ if (rc < 0)
++ return NULL;
++
++ /* check for new newsgroups */
++ if (!leave_lock && nntp_check_new_groups (nserv) < 0)
++ rc = -1;
++
++ /* .newsrc has been externally modified */
++ if (rc > 0)
++ nntp_clear_cache (nserv);
++ if (rc < 0 || !leave_lock)
++ nntp_newsrc_close (nserv);
++ return rc < 0 ? NULL : nserv;
++ }
++
++ /* new news server */
++ nserv = safe_calloc (1, sizeof (NNTP_SERVER));
++ nserv->conn = conn;
++ nserv->groups_hash = hash_create (1009, 0);
++ nserv->groups_max = 16;
++ nserv->groups_list = safe_malloc (nserv->groups_max * sizeof (nntp_data));
++
++ rc = nntp_open_connection (nserv);
++
++ /* try to create cache directory and enable caching */
++ nserv->cacheable = 0;
++ if (rc >= 0 && NewsCacheDir && *NewsCacheDir)
++ {
++ cache_expand (file, sizeof (file), &conn->account, NULL);
++ p = *file == '/' ? file + 1 : file;
++ while (1)
++ {
++ p = strchr (p, '/');
++ if (p)
++ *p = '\0';
++ if ((stat (file, &sb) || (sb.st_mode & S_IFDIR) == 0) &&
++ mkdir (file, 0700))
++ {
++ mutt_error (_("Can't create %s: %s."), file, strerror (errno));
++ mutt_sleep (2);
++ break;
++ }
++ if (!p)
++ {
++ nserv->cacheable = 1;
++ break;
++ }
++ *p++ = '/';
++ }
++ }
++
++ /* load .newsrc */
++ if (rc >= 0)
++ {
++ mutt_FormatString (file, sizeof (file), 0, sizeof (file), NONULL (NewsRc),
++ nntp_format_str, (unsigned long)nserv, 0);
++ mutt_expand_path (file, sizeof (file));
++ nserv->newsrc_file = safe_strdup (file);
++ rc = nntp_newsrc_parse (nserv);
++ }
++ if (rc >= 0)
++ {
++ /* try to load list of newsgroups from cache */
++ if (nserv->cacheable && active_get_cache (nserv) == 0)
++ rc = nntp_check_new_groups (nserv);
++
++ /* load list of newsgroups from server */
++ else
++ rc = nntp_active_fetch (nserv, 0);
++ }
++
++ if (rc >= 0)
++ nntp_clear_cache (nserv);
++
++#ifdef USE_HCACHE
++ /* check cache files */
++ if (rc >= 0 && nserv->cacheable)
++ {
++ struct dirent *entry;
++ DIR *dp = opendir (file);
++
++ if (dp)
++ {
++ while ((entry = readdir (dp)))
++ {
++ header_cache_t *hc;
++ void *hdata;
++ char *group = entry->d_name;
++
++ p = group + strlen (group) - 7;
++ if (strlen (group) < 8 || strcmp (p, ".hcache"))
++ continue;
++ *p = '\0';
++ nntp_data = hash_find (nserv->groups_hash, group);
++ if (!nntp_data)
++ continue;
++
++ hc = nntp_hcache_open (nntp_data);
++ if (!hc)
++ continue;
++
++ /* fetch previous values of first and last */
++ hdata = mutt_hcache_fetch_raw (hc, "index", strlen);
++ if (hdata)
++ {
++ anum_t first, last;
++
++ if (sscanf (hdata, ANUM " " ANUM, &first, &last) == 2)
++ {
++ if (nntp_data->deleted)
++ {
++ nntp_data->firstMessage = first;
++ nntp_data->lastMessage = last;
++ }
++ if (last >= nntp_data->firstMessage &&
++ last <= nntp_data->lastMessage)
++ {
++ nntp_data->lastCached = last;
++ dprint (2, (debugfile, "nntp_select_server: %s lastCached=%u\n",
++ nntp_data->group, last));
++ }
++ }
++ FREE (&hdata);
++ }
++ mutt_hcache_close (hc);
++ }
++ closedir (dp);
++ }
++ }
++#endif
++
++ if (rc < 0 || !leave_lock)
++ nntp_newsrc_close (nserv);
++
++ if (rc < 0)
++ {
++ hash_destroy (&nserv->groups_hash, nntp_data_free);
++ FREE (&nserv->groups_list);
++ FREE (&nserv->newsrc_file);
++ FREE (&nserv->authenticators);
++ FREE (&nserv);
++ mutt_socket_close (conn);
++ mutt_socket_free (conn);