NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
newsrc.c File Reference

Read/parse/write an NNTP config file of subscribed newsgroups. More...

#include "config.h"
#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "private.h"
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "conn/lib.h"
#include "mutt.h"
#include "lib.h"
#include "bcache/lib.h"
#include "adata.h"
#include "edata.h"
#include "format_flags.h"
#include "mdata.h"
#include "mutt_account.h"
#include "mutt_logging.h"
#include "mutt_socket.h"
#include "muttlib.h"
#include "protos.h"
#include "hcache/lib.h"
+ Include dependency graph for newsrc.c:

Go to the source code of this file.

Functions

static struct NntpMboxDatamdata_find (struct NntpAccountData *adata, const char *group)
 Find NntpMboxData for given newsgroup or add it. More...
 
void nntp_acache_free (struct NntpMboxData *mdata)
 Remove all temporarily cache files. More...
 
void nntp_newsrc_close (struct NntpAccountData *adata)
 Unlock and close .newsrc file. More...
 
void nntp_group_unread_stat (struct NntpMboxData *mdata)
 Count number of unread articles using .newsrc data. More...
 
int nntp_newsrc_parse (struct NntpAccountData *adata)
 Parse .newsrc file. More...
 
void nntp_newsrc_gen_entries (struct Mailbox *m)
 Generate array of .newsrc entries. More...
 
static int update_file (char *filename, char *buf)
 Update file with new contents. More...
 
int nntp_newsrc_update (struct NntpAccountData *adata)
 Update .newsrc file. More...
 
static void cache_expand (char *dst, size_t dstlen, struct ConnAccount *cac, const char *src)
 Make fully qualified cache file name. More...
 
void nntp_expand_path (char *buf, size_t buflen, struct ConnAccount *cac)
 Make fully qualified url from newsgroup name. More...
 
int nntp_add_group (char *line, void *data)
 Parse newsgroup. More...
 
static int active_get_cache (struct NntpAccountData *adata)
 Load list of all newsgroups from cache. More...
 
int nntp_active_save_cache (struct NntpAccountData *adata)
 Save list of all newsgroups to cache. More...
 
static void nntp_hcache_namer (const char *path, struct Buffer *dest)
 Compose hcache file names - Implements hcache_namer_t -. More...
 
struct HeaderCachenntp_hcache_open (struct NntpMboxData *mdata)
 Open newsgroup hcache. More...
 
void nntp_hcache_update (struct NntpMboxData *mdata, struct HeaderCache *hc)
 Remove stale cached headers. More...
 
static int nntp_bcache_delete (const char *id, struct BodyCache *bcache, void *data)
 Remove bcache file - Implements bcache_list_t -. More...
 
void nntp_bcache_update (struct NntpMboxData *mdata)
 Remove stale cached messages. More...
 
void nntp_delete_group_cache (struct NntpMboxData *mdata)
 Remove hcache and bcache of newsgroup. More...
 
void nntp_clear_cache (struct NntpAccountData *adata)
 Clear the NNTP cache. More...
 
const char * nntp_format_str (char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
 Expand the newsrc filename - Implements format_t -. More...
 
static const char * nntp_get_field (enum ConnAccountField field, void *gf_data)
 Get connection login credentials - Implements ConnAccount::get_field() More...
 
struct NntpAccountDatanntp_select_server (struct Mailbox *m, const char *server, bool leave_lock)
 Open a connection to an NNTP server. More...
 
void nntp_article_status (struct Mailbox *m, struct Email *e, char *group, anum_t anum)
 Get status of articles from .newsrc. More...
 
struct NntpMboxDatamutt_newsgroup_subscribe (struct NntpAccountData *adata, char *group)
 Subscribe newsgroup. More...
 
struct NntpMboxDatamutt_newsgroup_unsubscribe (struct NntpAccountData *adata, char *group)
 Unsubscribe newsgroup. More...
 
struct NntpMboxDatamutt_newsgroup_catchup (struct Mailbox *m, struct NntpAccountData *adata, char *group)
 Catchup newsgroup. More...
 
struct NntpMboxDatamutt_newsgroup_uncatchup (struct Mailbox *m, struct NntpAccountData *adata, char *group)
 Uncatchup newsgroup. More...
 
void nntp_mailbox (struct Mailbox *m, char *buf, size_t buflen)
 Get first newsgroup with new messages. More...
 

Detailed Description

Read/parse/write an NNTP config file of subscribed newsgroups.

Authors
  • Brandon Long
  • Andrej Gritsenko
  • Vsevolod Volkov

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file newsrc.c.

Function Documentation

◆ mdata_find()

static struct NntpMboxData* mdata_find ( struct NntpAccountData adata,
const char *  group 
)
static

Find NntpMboxData for given newsgroup or add it.

Parameters
adataNNTP server
groupNewsgroup
Return values
ptrNNTP data
NULLError

Definition at line 72 of file newsrc.c.

73 {
75  if (mdata)
76  return mdata;
77 
78  size_t len = strlen(group) + 1;
79  /* create NntpMboxData structure and add it to hash */
80  mdata = mutt_mem_calloc(1, sizeof(struct NntpMboxData) + len);
81  mdata->group = (char *) mdata + sizeof(struct NntpMboxData);
82  mutt_str_copy(mdata->group, group, len);
83  mdata->adata = adata;
84  mdata->deleted = true;
86 
87  /* add NntpMboxData to list */
89  {
90  adata->groups_max *= 2;
92  }
94 
95  return mdata;
96 }
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:335
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:362
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:629
void * mdata
Driver specific data.
Definition: mailbox.h:133
struct HashTable * groups_hash
Definition: adata.h:62
unsigned int groups_num
Definition: adata.h:59
unsigned int groups_max
Definition: adata.h:60
void ** groups_list
Definition: adata.h:61
NNTP-specific Mailbox data -.
Definition: mdata.h:33
char * group
Name of newsgroup.
Definition: mdata.h:34
struct NntpAccountData * adata
Definition: mdata.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_acache_free()

void nntp_acache_free ( struct NntpMboxData mdata)

Remove all temporarily cache files.

Parameters
mdataNNTP Mailbox data

Definition at line 102 of file newsrc.c.

103 {
104  for (int i = 0; i < NNTP_ACACHE_LEN; i++)
105  {
106  if (mdata->acache[i].path)
107  {
108  unlink(mdata->acache[i].path);
109  FREE(&mdata->acache[i].path);
110  }
111  }
112 }
#define FREE(x)
Definition: memory.h:43
#define NNTP_ACACHE_LEN
Definition: lib.h:82
char * path
Cache path.
Definition: lib.h:69
struct NntpAcache acache[NNTP_ACACHE_LEN]
Definition: mdata.h:48
+ Here is the caller graph for this function:

◆ nntp_newsrc_close()

void nntp_newsrc_close ( struct NntpAccountData adata)

Unlock and close .newsrc file.

Parameters
adataNNTP server

Definition at line 118 of file newsrc.c.

119 {
120  if (!adata->fp_newsrc)
121  return;
122 
123  mutt_debug(LL_DEBUG1, "Unlocking %s\n", adata->newsrc_file);
124  mutt_file_unlock(fileno(adata->fp_newsrc));
125  mutt_file_fclose(&adata->fp_newsrc);
126 }
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1279
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
FILE * fp_newsrc
Definition: adata.h:51
char * newsrc_file
Definition: adata.h:52
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_group_unread_stat()

void nntp_group_unread_stat ( struct NntpMboxData mdata)

Count number of unread articles using .newsrc data.

Parameters
mdataNNTP Mailbox data

Definition at line 132 of file newsrc.c.

133 {
134  mdata->unread = 0;
135  if ((mdata->last_message == 0) ||
136  (mdata->first_message > mdata->last_message) || !mdata->newsrc_ent)
137  {
138  return;
139  }
140 
141  mdata->unread = mdata->last_message - mdata->first_message + 1;
142  for (unsigned int i = 0; i < mdata->newsrc_len; i++)
143  {
144  anum_t first = mdata->newsrc_ent[i].first;
145  if (first < mdata->first_message)
146  first = mdata->first_message;
147  anum_t last = mdata->newsrc_ent[i].last;
148  if (last > mdata->last_message)
149  last = mdata->last_message;
150  if (first <= last)
151  mdata->unread -= last - first + 1;
152  }
153 }
#define anum_t
Definition: lib.h:60
anum_t last
Last article number in run.
Definition: lib.h:78
anum_t first
First article number in run.
Definition: lib.h:77
anum_t last_message
Definition: mdata.h:37
struct NewsrcEntry * newsrc_ent
Definition: mdata.h:46
anum_t unread
Definition: mdata.h:40
unsigned int newsrc_len
Definition: mdata.h:45
anum_t first_message
Definition: mdata.h:36
+ Here is the caller graph for this function:

◆ nntp_newsrc_parse()

int nntp_newsrc_parse ( struct NntpAccountData adata)

Parse .newsrc file.

Parameters
adataNNTP server
Return values
0Not changed
1Parsed
-1Error

Definition at line 162 of file newsrc.c.

163 {
164  if (!adata)
165  return -1;
166 
167  char *line = NULL;
168  struct stat st = { 0 };
169 
170  if (adata->fp_newsrc)
171  {
172  /* if we already have a handle, close it and reopen */
173  mutt_file_fclose(&adata->fp_newsrc);
174  }
175  else
176  {
177  /* if file doesn't exist, create it */
178  adata->fp_newsrc = mutt_file_fopen(adata->newsrc_file, "a");
179  mutt_file_fclose(&adata->fp_newsrc);
180  }
181 
182  /* open .newsrc */
183  adata->fp_newsrc = mutt_file_fopen(adata->newsrc_file, "r");
184  if (!adata->fp_newsrc)
185  {
186  mutt_perror(adata->newsrc_file);
187  return -1;
188  }
189 
190  /* lock it */
191  mutt_debug(LL_DEBUG1, "Locking %s\n", adata->newsrc_file);
192  if (mutt_file_lock(fileno(adata->fp_newsrc), false, true))
193  {
194  mutt_file_fclose(&adata->fp_newsrc);
195  return -1;
196  }
197 
198  if (stat(adata->newsrc_file, &st) != 0)
199  {
200  mutt_perror(adata->newsrc_file);
201  nntp_newsrc_close(adata);
202  return -1;
203  }
204 
205  if ((adata->size == st.st_size) && (adata->mtime == st.st_mtime))
206  return 0;
207 
208  adata->size = st.st_size;
209  adata->mtime = st.st_mtime;
210  adata->newsrc_modified = true;
211  mutt_debug(LL_DEBUG1, "Parsing %s\n", adata->newsrc_file);
212 
213  /* .newsrc has been externally modified or hasn't been loaded yet */
214  for (unsigned int i = 0; i < adata->groups_num; i++)
215  {
216  struct NntpMboxData *mdata = adata->groups_list[i];
217  if (!mdata)
218  continue;
219 
220  mdata->subscribed = false;
221  mdata->newsrc_len = 0;
222  FREE(&mdata->newsrc_ent);
223  }
224 
225  line = mutt_mem_malloc(st.st_size + 1);
226  while (st.st_size && fgets(line, st.st_size + 1, adata->fp_newsrc))
227  {
228  char *b = NULL, *h = NULL;
229  unsigned int j = 1;
230  bool subs = false;
231 
232  /* find end of newsgroup name */
233  char *p = strpbrk(line, ":!");
234  if (!p)
235  continue;
236 
237  /* ":" - subscribed, "!" - unsubscribed */
238  if (*p == ':')
239  subs = true;
240  *p++ = '\0';
241 
242  /* get newsgroup data */
243  struct NntpMboxData *mdata = mdata_find(adata, line);
244  FREE(&mdata->newsrc_ent);
245 
246  /* count number of entries */
247  b = p;
248  while (*b)
249  if (*b++ == ',')
250  j++;
251  mdata->newsrc_ent = mutt_mem_calloc(j, sizeof(struct NewsrcEntry));
252  mdata->subscribed = subs;
253 
254  /* parse entries */
255  j = 0;
256  while (p)
257  {
258  b = p;
259 
260  /* find end of entry */
261  p = strchr(p, ',');
262  if (p)
263  *p++ = '\0';
264 
265  /* first-last or single number */
266  h = strchr(b, '-');
267  if (h)
268  *h++ = '\0';
269  else
270  h = b;
271 
272  if ((sscanf(b, ANUM, &mdata->newsrc_ent[j].first) == 1) &&
273  (sscanf(h, ANUM, &mdata->newsrc_ent[j].last) == 1))
274  {
275  j++;
276  }
277  }
278  if (j == 0)
279  {
280  mdata->newsrc_ent[j].first = 1;
281  mdata->newsrc_ent[j].last = 0;
282  j++;
283  }
284  if (mdata->last_message == 0)
285  mdata->last_message = mdata->newsrc_ent[j - 1].last;
286  mdata->newsrc_len = j;
287  mutt_mem_realloc(&mdata->newsrc_ent, j * sizeof(struct NewsrcEntry));
289  mutt_debug(LL_DEBUG2, "%s\n", mdata->group);
290  }
291  FREE(&line);
292  return 1;
293 }
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1231
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:618
#define mutt_perror(...)
Definition: logging.h:88
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
void nntp_newsrc_close(struct NntpAccountData *adata)
Unlock and close .newsrc file.
Definition: newsrc.c:118
void nntp_group_unread_stat(struct NntpMboxData *mdata)
Count number of unread articles using .newsrc data.
Definition: newsrc.c:132
static struct NntpMboxData * mdata_find(struct NntpAccountData *adata, const char *group)
Find NntpMboxData for given newsgroup or add it.
Definition: newsrc.c:72
#define ANUM
Definition: lib.h:61
An entry in a .newsrc (subscribed newsgroups)
Definition: lib.h:76
bool newsrc_modified
Definition: adata.h:50
off_t size
Definition: adata.h:55
time_t mtime
Definition: adata.h:56
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_newsrc_gen_entries()

void nntp_newsrc_gen_entries ( struct Mailbox m)

Generate array of .newsrc entries.

Parameters
mMailbox

Definition at line 299 of file newsrc.c.

300 {
301  if (!m)
302  return;
303 
304  struct NntpMboxData *mdata = m->mdata;
305  anum_t last = 0, first = 1;
306  bool series;
307  unsigned int entries;
308 
309  const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
310  if (c_sort != SORT_ORDER)
311  {
314  }
315 
316  entries = mdata->newsrc_len;
317  if (!entries)
318  {
319  entries = 5;
320  mdata->newsrc_ent = mutt_mem_calloc(entries, sizeof(struct NewsrcEntry));
321  }
322 
323  /* Set up to fake initial sequence from 1 to the article before the
324  * first article in our list */
325  mdata->newsrc_len = 0;
326  series = true;
327  for (int i = 0; i < m->msg_count; i++)
328  {
329  struct Email *e = m->emails[i];
330  if (!e)
331  break;
332 
333  /* search for first unread */
334  if (series)
335  {
336  /* We don't actually check sequential order, since we mark
337  * "missing" entries as read/deleted */
338  last = nntp_edata_get(e)->article_num;
339  if ((last >= mdata->first_message) && !e->deleted && !e->read)
340  {
341  if (mdata->newsrc_len >= entries)
342  {
343  entries *= 2;
344  mutt_mem_realloc(&mdata->newsrc_ent, entries * sizeof(struct NewsrcEntry));
345  }
346  mdata->newsrc_ent[mdata->newsrc_len].first = first;
347  mdata->newsrc_ent[mdata->newsrc_len].last = last - 1;
348  mdata->newsrc_len++;
349  series = false;
350  }
351  }
352 
353  /* search for first read */
354  else
355  {
356  if (e->deleted || e->read)
357  {
358  first = last + 1;
359  series = true;
360  }
361  last = nntp_edata_get(e)->article_num;
362  }
363  }
364 
365  if (series && (first <= mdata->last_loaded))
366  {
367  if (mdata->newsrc_len >= entries)
368  {
369  entries++;
370  mutt_mem_realloc(&mdata->newsrc_ent, entries * sizeof(struct NewsrcEntry));
371  }
372  mdata->newsrc_ent[mdata->newsrc_len].first = first;
373  mdata->newsrc_ent[mdata->newsrc_len].last = mdata->last_loaded;
374  mdata->newsrc_len++;
375  }
376  mutt_mem_realloc(&mdata->newsrc_ent, mdata->newsrc_len * sizeof(struct NewsrcEntry));
377 
378  if (c_sort != SORT_ORDER)
379  {
380  cs_subset_str_native_set(NeoMutt->sub, "sort", c_sort, NULL);
382  }
383 }
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:222
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:178
struct NntpEmailData * nntp_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:58
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:48
The envelope/body of an email.
Definition: email.h:37
bool read
Email is read.
Definition: email.h:48
bool deleted
Email is deleted.
Definition: email.h:76
int msg_count
Total number of messages.
Definition: mailbox.h:88
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
anum_t article_num
NNTP article number.
Definition: edata.h:36
anum_t last_loaded
Definition: mdata.h:38
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:305
+ Here is the call graph for this function:

◆ update_file()

static int update_file ( char *  filename,
char *  buf 
)
static

Update file with new contents.

Parameters
filenameFile to update
bufNew context
Return values
0Success
-1Failure

Definition at line 392 of file newsrc.c.

393 {
394  FILE *fp = NULL;
395  char tmpfile[PATH_MAX];
396  int rc = -1;
397 
398  while (true)
399  {
400  snprintf(tmpfile, sizeof(tmpfile), "%s.tmp", filename);
401  fp = mutt_file_fopen(tmpfile, "w");
402  if (!fp)
403  {
404  mutt_perror(tmpfile);
405  *tmpfile = '\0';
406  break;
407  }
408  if (fputs(buf, fp) == EOF)
409  {
410  mutt_perror(tmpfile);
411  break;
412  }
413  if (mutt_file_fclose(&fp) == EOF)
414  {
415  mutt_perror(tmpfile);
416  fp = NULL;
417  break;
418  }
419  fp = NULL;
420  if (rename(tmpfile, filename) < 0)
421  {
422  mutt_perror(filename);
423  break;
424  }
425  *tmpfile = '\0';
426  rc = 0;
427  break;
428  }
429  mutt_file_fclose(&fp);
430 
431  if (*tmpfile)
432  unlink(tmpfile);
433  return rc;
434 }
#define PATH_MAX
Definition: mutt.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_newsrc_update()

int nntp_newsrc_update ( struct NntpAccountData adata)

Update .newsrc file.

Parameters
adataNNTP server
Return values
0Success
-1Failure

Definition at line 442 of file newsrc.c.

443 {
444  if (!adata)
445  return -1;
446 
447  int rc = -1;
448 
449  size_t buflen = 10240;
450  char *buf = mutt_mem_calloc(1, buflen);
451  size_t off = 0;
452 
453  /* we will generate full newsrc here */
454  for (unsigned int i = 0; i < adata->groups_num; i++)
455  {
456  struct NntpMboxData *mdata = adata->groups_list[i];
457 
458  if (!mdata || !mdata->newsrc_ent)
459  continue;
460 
461  /* write newsgroup name */
462  if (off + strlen(mdata->group) + 3 > buflen)
463  {
464  buflen *= 2;
465  mutt_mem_realloc(&buf, buflen);
466  }
467  snprintf(buf + off, buflen - off, "%s%c ", mdata->group, mdata->subscribed ? ':' : '!');
468  off += strlen(buf + off);
469 
470  /* write entries */
471  for (unsigned int j = 0; j < mdata->newsrc_len; j++)
472  {
473  if (off + 1024 > buflen)
474  {
475  buflen *= 2;
476  mutt_mem_realloc(&buf, buflen);
477  }
478  if (j)
479  buf[off++] = ',';
480  if (mdata->newsrc_ent[j].first == mdata->newsrc_ent[j].last)
481  snprintf(buf + off, buflen - off, "%u", mdata->newsrc_ent[j].first);
482  else if (mdata->newsrc_ent[j].first < mdata->newsrc_ent[j].last)
483  {
484  snprintf(buf + off, buflen - off, "%u-%u", mdata->newsrc_ent[j].first,
485  mdata->newsrc_ent[j].last);
486  }
487  off += strlen(buf + off);
488  }
489  buf[off++] = '\n';
490  }
491  buf[off] = '\0';
492 
493  /* newrc being fully rewritten */
494  mutt_debug(LL_DEBUG1, "Updating %s\n", adata->newsrc_file);
495  if (adata->newsrc_file && (update_file(adata->newsrc_file, buf) == 0))
496  {
497  struct stat st = { 0 };
498 
499  rc = stat(adata->newsrc_file, &st);
500  if (rc == 0)
501  {
502  adata->size = st.st_size;
503  adata->mtime = st.st_mtime;
504  }
505  else
506  {
507  mutt_perror(adata->newsrc_file);
508  }
509  }
510  FREE(&buf);
511  return rc;
512 }
static int update_file(char *filename, char *buf)
Update file with new contents.
Definition: newsrc.c:392
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ cache_expand()

static void cache_expand ( char *  dst,
size_t  dstlen,
struct ConnAccount cac,
const char *  src 
)
static

Make fully qualified cache file name.

Parameters
dstBuffer for filename
dstlenLength of buffer
cacAccount
srcPath to add to the URL

Definition at line 521 of file newsrc.c.

522 {
523  char *c = NULL;
524  char file[PATH_MAX];
525 
526  /* server subdirectory */
527  if (cac)
528  {
529  struct Url url = { 0 };
530 
531  mutt_account_tourl(cac, &url);
532  url.path = mutt_str_dup(src);
533  url_tostring(&url, file, sizeof(file), U_PATH);
534  FREE(&url.path);
535  }
536  else
537  mutt_str_copy(file, src ? src : "", sizeof(file));
538 
539  const char *const c_news_cache_dir = cs_subset_path(NeoMutt->sub, "news_cache_dir");
540  snprintf(dst, dstlen, "%s/%s", c_news_cache_dir, file);
541 
542  /* remove trailing slash */
543  c = dst + strlen(dst) - 1;
544  if (*c == '/')
545  *c = '\0';
546 
547  struct Buffer *tmp = mutt_buffer_pool_get();
548  mutt_buffer_addstr(tmp, dst);
550  mutt_encode_path(tmp, dst);
551  mutt_str_copy(dst, mutt_buffer_string(tmp), dstlen);
553 }
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:223
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
void mutt_account_tourl(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
Definition: mutt_account.c:79
void mutt_buffer_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:322
void mutt_encode_path(struct Buffer *buf, const char *src)
Convert a path to 'us-ascii'.
Definition: muttlib.c:1484
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
String manipulation buffer.
Definition: buffer.h:34
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:69
char * src
Raw URL string.
Definition: url.h:77
char * path
Path.
Definition: url.h:75
int url_tostring(struct Url *url, char *dest, size_t len, uint8_t flags)
Output the URL string for a given Url object.
Definition: url.c:418
#define U_PATH
Definition: url.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_expand_path()

void nntp_expand_path ( char *  buf,
size_t  buflen,
struct ConnAccount cac 
)

Make fully qualified url from newsgroup name.

Parameters
bufBuffer for the result
buflenLength of buffer
cacAccount to serialise

Definition at line 561 of file newsrc.c.

562 {
563  struct Url url = { 0 };
564 
565  mutt_account_tourl(cac, &url);
566  url.path = mutt_str_dup(buf);
567  url_tostring(&url, buf, buflen, U_NO_FLAGS);
568  FREE(&url.path);
569 }
#define U_NO_FLAGS
Definition: url.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_add_group()

int nntp_add_group ( char *  line,
void *  data 
)

Parse newsgroup.

Parameters
lineString to parse
dataNNTP data
Return values
0Always

Definition at line 577 of file newsrc.c.

578 {
579  struct NntpAccountData *adata = data;
580  struct NntpMboxData *mdata = NULL;
581  char group[1024] = { 0 };
582  char desc[8192] = { 0 };
583  char mod;
584  anum_t first, last;
585 
586  if (!adata || !line)
587  return 0;
588 
589  /* These sscanf limits must match the sizes of the group and desc arrays */
590  if (sscanf(line, "%1023s " ANUM " " ANUM " %c %8191[^\n]", group, &last,
591  &first, &mod, desc) < 4)
592  {
593  mutt_debug(LL_DEBUG2, "Can't parse server line: %s\n", line);
594  return 0;
595  }
596 
598  mdata->deleted = false;
599  mdata->first_message = first;
600  mdata->last_message = last;
601  mdata->allowed = (mod == 'y') || (mod == 'm');
602  mutt_str_replace(&mdata->desc, desc);
603  if (mdata->newsrc_ent || (mdata->last_cached != 0))
605  else if (mdata->last_message && (mdata->first_message <= mdata->last_message))
606  mdata->unread = mdata->last_message - mdata->first_message + 1;
607  else
608  mdata->unread = 0;
609  return 0;
610 }
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
NNTP-specific Account data -.
Definition: adata.h:37
char * desc
Description of newsgroup.
Definition: mdata.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ active_get_cache()

static int active_get_cache ( struct NntpAccountData adata)
static

Load list of all newsgroups from cache.

Parameters
adataNNTP server
Return values
0Success
-1Failure

Definition at line 618 of file newsrc.c.

619 {
620  char buf[8192];
621  char file[4096];
622  time_t t = 0;
623 
624  cache_expand(file, sizeof(file), &adata->conn->account, ".active");
625  mutt_debug(LL_DEBUG1, "Parsing %s\n", file);
626  FILE *fp = mutt_file_fopen(file, "r");
627  if (!fp)
628  return -1;
629 
630  if (!fgets(buf, sizeof(buf), fp) || (sscanf(buf, "%jd%4095s", &t, file) != 1) || (t == 0))
631  {
632  mutt_file_fclose(&fp);
633  return -1;
634  }
635  adata->newgroups_time = t;
636 
637  mutt_message(_("Loading list of groups from cache..."));
638  while (fgets(buf, sizeof(buf), fp))
639  nntp_add_group(buf, adata);
640  nntp_add_group(NULL, NULL);
641  mutt_file_fclose(&fp);
643  return 0;
644 }
#define mutt_message(...)
Definition: logging.h:86
#define _(a)
Definition: message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:74
int nntp_add_group(char *line, void *data)
Parse newsgroup.
Definition: newsrc.c:577
static void cache_expand(char *dst, size_t dstlen, struct ConnAccount *cac, const char *src)
Make fully qualified cache file name.
Definition: newsrc.c:521
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:50
time_t newgroups_time
Definition: adata.h:57
struct Connection * conn
Connection to NNTP Server.
Definition: adata.h:63
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_active_save_cache()

int nntp_active_save_cache ( struct NntpAccountData adata)

Save list of all newsgroups to cache.

Parameters
adataNNTP server
Return values
0Success
-1Failure

Definition at line 652 of file newsrc.c.

653 {
654  if (!adata->cacheable)
655  return 0;
656 
657  size_t buflen = 10240;
658  char *buf = mutt_mem_calloc(1, buflen);
659  snprintf(buf, buflen, "%lu\n", (unsigned long) adata->newgroups_time);
660  size_t off = strlen(buf);
661 
662  for (unsigned int i = 0; i < adata->groups_num; i++)
663  {
664  struct NntpMboxData *mdata = adata->groups_list[i];
665 
666  if (!mdata || mdata->deleted)
667  continue;
668 
669  if ((off + strlen(mdata->group) + (mdata->desc ? strlen(mdata->desc) : 0) + 50) > buflen)
670  {
671  buflen *= 2;
672  mutt_mem_realloc(&buf, buflen);
673  }
674  snprintf(buf + off, buflen - off, "%s %u %u %c%s%s\n", mdata->group,
675  mdata->last_message, mdata->first_message, mdata->allowed ? 'y' : 'n',
676  mdata->desc ? " " : "", mdata->desc ? mdata->desc : "");
677  off += strlen(buf + off);
678  }
679 
680  char file[PATH_MAX];
681  cache_expand(file, sizeof(file), &adata->conn->account, ".active");
682  mutt_debug(LL_DEBUG1, "Updating %s\n", file);
683  int rc = update_file(file, buf);
684  FREE(&buf);
685  return rc;
686 }
bool cacheable
Definition: adata.h:49
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_hcache_open()

struct HeaderCache* nntp_hcache_open ( struct NntpMboxData mdata)

Open newsgroup hcache.

Parameters
mdataNNTP Mailbox data
Return values
ptrHeader cache
NULLError

Definition at line 711 of file newsrc.c.

712 {
713  struct Url url = { 0 };
714  char file[PATH_MAX];
715 
716  const bool c_save_unsubscribed = cs_subset_bool(NeoMutt->sub, "save_unsubscribed");
717  if (!mdata->adata || !mdata->adata->cacheable || !mdata->adata->conn || !mdata->group ||
718  !(mdata->newsrc_ent || mdata->subscribed || c_save_unsubscribed))
719  {
720  return NULL;
721  }
722 
723  mutt_account_tourl(&mdata->adata->conn->account, &url);
724  url.path = mdata->group;
725  url_tostring(&url, file, sizeof(file), U_PATH);
726  const char *const c_news_cache_dir = cs_subset_path(NeoMutt->sub, "news_cache_dir");
727  return mutt_hcache_open(c_news_cache_dir, file, nntp_hcache_namer);
728 }
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
static void nntp_hcache_namer(const char *path, struct Buffer *dest)
Compose hcache file names - Implements hcache_namer_t -.
Definition: newsrc.c:692
struct HeaderCache * mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:332
bool subscribed
Definition: mdata.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_hcache_update()

void nntp_hcache_update ( struct NntpMboxData mdata,
struct HeaderCache hc 
)

Remove stale cached headers.

Parameters
mdataNNTP Mailbox data
hcHeader cache

Definition at line 735 of file newsrc.c.

736 {
737  if (!hc)
738  return;
739 
740  char buf[32];
741  bool old = false;
742  anum_t first = 0, last = 0;
743 
744  /* fetch previous values of first and last */
745  size_t dlen = 0;
746  void *hdata = mutt_hcache_fetch_raw(hc, "index", 5, &dlen);
747  if (hdata)
748  {
749  mutt_debug(LL_DEBUG2, "mutt_hcache_fetch index: %s\n", (char *) hdata);
750  if (sscanf(hdata, ANUM " " ANUM, &first, &last) == 2)
751  {
752  old = true;
753  mdata->last_cached = last;
754 
755  /* clean removed headers from cache */
756  for (anum_t current = first; current <= last; current++)
757  {
758  if ((current >= mdata->first_message) && (current <= mdata->last_message))
759  continue;
760 
761  snprintf(buf, sizeof(buf), "%u", current);
762  mutt_debug(LL_DEBUG2, "mutt_hcache_delete_record %s\n", buf);
763  mutt_hcache_delete_record(hc, buf, strlen(buf));
764  }
765  }
766  mutt_hcache_free_raw(hc, &hdata);
767  }
768 
769  /* store current values of first and last */
770  if (!old || (mdata->first_message != first) || (mdata->last_message != last))
771  {
772  snprintf(buf, sizeof(buf), "%u %u", mdata->first_message, mdata->last_message);
773  mutt_debug(LL_DEBUG2, "mutt_hcache_store index: %s\n", buf);
774  mutt_hcache_store_raw(hc, "index", 5, buf, strlen(buf) + 1);
775  }
776 }
int mutt_hcache_delete_record(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:627
int mutt_hcache_store_raw(struct HeaderCache *hc, const char *key, size_t keylen, void *data, size_t dlen)
Store a key / data pair.
Definition: hcache.c:606
void mutt_hcache_free_raw(struct HeaderCache *hc, void **data)
Multiplexor for StoreOps::free.
Definition: hcache.c:534
void * mutt_hcache_fetch_raw(struct HeaderCache *hc, const char *key, size_t keylen, size_t *dlen)
Fetch a message's header from the cache.
Definition: hcache.c:515
anum_t last_cached
Definition: mdata.h:39
+ Here is the call graph for this function:

◆ nntp_bcache_update()

void nntp_bcache_update ( struct NntpMboxData mdata)

Remove stale cached messages.

Parameters
mdataNNTP Mailbox data

Definition at line 803 of file newsrc.c.

804 {
806 }
int mutt_bcache_list(struct BodyCache *bcache, bcache_list_t want_id, void *data)
Find matching entries in the Body Cache.
Definition: bcache.c:330
static int nntp_bcache_delete(const char *id, struct BodyCache *bcache, void *data)
Remove bcache file - Implements bcache_list_t -.
Definition: newsrc.c:783
struct BodyCache * bcache
Definition: mdata.h:49
+ Here is the call graph for this function:

◆ nntp_delete_group_cache()

void nntp_delete_group_cache ( struct NntpMboxData mdata)

Remove hcache and bcache of newsgroup.

Parameters
mdataNNTP Mailbox data

Definition at line 812 of file newsrc.c.

813 {
814  if (!mdata || !mdata->adata || !mdata->adata->cacheable)
815  return;
816 
817 #ifdef USE_HCACHE
818  struct Buffer file = mutt_buffer_make(PATH_MAX);
819  nntp_hcache_namer(mdata->group, &file);
820  cache_expand(file.data, file.dsize, &mdata->adata->conn->account,
821  mutt_buffer_string(&file));
822  unlink(mutt_buffer_string(&file));
823  mdata->last_cached = 0;
824  mutt_debug(LL_DEBUG2, "%s\n", mutt_buffer_string(&file));
825  mutt_buffer_dealloc(&file);
826 #endif
827 
828  if (!mdata->bcache)
829  {
830  mdata->bcache = mutt_bcache_open(&mdata->adata->conn->account, mdata->group);
831  }
832  if (mdata->bcache)
833  {
834  mutt_debug(LL_DEBUG2, "%s/*\n", mdata->group);
836  mutt_bcache_close(&mdata->bcache);
837  }
838 }
struct BodyCache * mutt_bcache_open(struct ConnAccount *account, const char *mailbox)
Open an Email-Body Cache.
Definition: bcache.c:144
void mutt_bcache_close(struct BodyCache **bcache)
Close an Email-Body Cache.
Definition: bcache.c:165
struct Buffer mutt_buffer_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:63
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:292
size_t dsize
Length of data.
Definition: buffer.h:37
char * data
Pointer to data.
Definition: buffer.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_clear_cache()

void nntp_clear_cache ( struct NntpAccountData adata)

Clear the NNTP cache.

Parameters
adataNNTP server

Remove hcache and bcache of all unexistent and unsubscribed newsgroups

Definition at line 846 of file newsrc.c.

847 {
848  char file[PATH_MAX];
849  char *fp = NULL;
850  struct dirent *entry = NULL;
851  DIR *dp = NULL;
852 
853  if (!adata || !adata->cacheable)
854  return;
855 
856  cache_expand(file, sizeof(file), &adata->conn->account, NULL);
857  dp = opendir(file);
858  if (dp)
859  {
860  mutt_strn_cat(file, sizeof(file), "/", 1);
861  fp = file + strlen(file);
862  while ((entry = readdir(dp)))
863  {
864  char *group = entry->d_name;
865  struct stat st = { 0 };
866  struct NntpMboxData *mdata = NULL;
867  struct NntpMboxData tmp_mdata;
868 
869  if (mutt_str_equal(group, ".") || mutt_str_equal(group, ".."))
870  continue;
871  *fp = '\0';
872  mutt_strn_cat(file, sizeof(file), group, strlen(group));
873  if (stat(file, &st) != 0)
874  continue;
875 
876 #ifdef USE_HCACHE
877  if (S_ISREG(st.st_mode))
878  {
879  char *ext = group + strlen(group) - 7;
880  if ((strlen(group) < 8) || !mutt_str_equal(ext, ".hcache"))
881  continue;
882  *ext = '\0';
883  }
884  else
885 #endif
886  if (!S_ISDIR(st.st_mode))
887  continue;
888 
889  const bool c_save_unsubscribed = cs_subset_bool(NeoMutt->sub, "save_unsubscribed");
891  if (!mdata)
892  {
893  mdata = &tmp_mdata;
894  mdata->adata = adata;
895  mdata->group = group;
896  mdata->bcache = NULL;
897  }
898  else if (mdata->newsrc_ent || mdata->subscribed || c_save_unsubscribed)
899  continue;
900 
902  if (S_ISDIR(st.st_mode))
903  {
904  rmdir(file);
905  mutt_debug(LL_DEBUG2, "%s\n", file);
906  }
907  }
908  closedir(dp);
909  }
910 }
char * mutt_strn_cat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:294
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:784
void nntp_delete_group_cache(struct NntpMboxData *mdata)
Remove hcache and bcache of newsgroup.
Definition: newsrc.c:812
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_get_field()

static const char* nntp_get_field ( enum ConnAccountField  field,
void *  gf_data 
)
static

Get connection login credentials - Implements ConnAccount::get_field()

Definition at line 987 of file newsrc.c.

988 {
989  switch (field)
990  {
991  case MUTT_CA_LOGIN:
992  case MUTT_CA_USER:
993  return cs_subset_string(NeoMutt->sub, "nntp_user");
994  case MUTT_CA_PASS:
995  return cs_subset_string(NeoMutt->sub, "nntp_pass");
996  case MUTT_CA_OAUTH_CMD:
997  case MUTT_CA_HOST:
998  default:
999  return NULL;
1000  }
1001 }
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
@ MUTT_CA_OAUTH_CMD
OAuth refresh command.
Definition: connaccount.h:38
@ MUTT_CA_USER
User name.
Definition: connaccount.h:36
@ MUTT_CA_LOGIN
Login name.
Definition: connaccount.h:35
@ MUTT_CA_HOST
Server name.
Definition: connaccount.h:34
@ MUTT_CA_PASS
Password.
Definition: connaccount.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_select_server()

struct NntpAccountData* nntp_select_server ( struct Mailbox m,
const char *  server,
bool  leave_lock 
)

Open a connection to an NNTP server.

Parameters
mMailbox
serverServer URL
leave_lockLeave the server locked?
Return values
ptrNNTP server
NULLError

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.

See also
$newsrc, nntp_format_str()

Definition at line 1018 of file newsrc.c.

1019 {
1020  char file[PATH_MAX];
1021  int rc;
1022  struct ConnAccount cac = { { 0 } };
1023  struct NntpAccountData *adata = NULL;
1024  struct Connection *conn = NULL;
1025 
1026  if (!server || (*server == '\0'))
1027  {
1028  mutt_error(_("No news server defined"));
1029  return NULL;
1030  }
1031 
1032  /* create account from news server url */
1033  cac.flags = 0;
1034  cac.port = NNTP_PORT;
1035  cac.type = MUTT_ACCT_TYPE_NNTP;
1036  cac.service = "nntp";
1037  cac.get_field = nntp_get_field;
1038 
1039  snprintf(file, sizeof(file), "%s%s", strstr(server, "://") ? "" : "news://", server);
1040  struct Url *url = url_parse(file);
1041  if (!url || (url->path && *url->path) ||
1042  !((url->scheme == U_NNTP) || (url->scheme == U_NNTPS)) || !url->host ||
1043  (mutt_account_fromurl(&cac, url) < 0))
1044  {
1045  url_free(&url);
1046  mutt_error(_("%s is an invalid news server specification"), server);
1047  return NULL;
1048  }
1049  if (url->scheme == U_NNTPS)
1050  {
1051  cac.flags |= MUTT_ACCT_SSL;
1052  cac.port = NNTP_SSL_PORT;
1053  }
1054  url_free(&url);
1055 
1056  // If nntp_user and nntp_pass are specified in the config, use them to find the connection
1057  const char *user = NULL;
1058  const char *pass = NULL;
1059  if ((user = cac.get_field(MUTT_CA_USER, NULL)))
1060  {
1061  mutt_str_copy(cac.user, user, sizeof(cac.user));
1062  cac.flags |= MUTT_ACCT_USER;
1063  }
1064  if ((pass = cac.get_field(MUTT_CA_PASS, NULL)))
1065  {
1066  mutt_str_copy(cac.pass, pass, sizeof(cac.pass));
1067  cac.flags |= MUTT_ACCT_PASS;
1068  }
1069 
1070  /* find connection by account */
1071  conn = mutt_conn_find(&cac);
1072  if (!conn)
1073  return NULL;
1074  if (!(conn->account.flags & MUTT_ACCT_USER) && cac.flags & MUTT_ACCT_USER)
1075  {
1076  conn->account.flags |= MUTT_ACCT_USER;
1077  conn->account.user[0] = '\0';
1078  }
1079 
1080  /* news server already exists */
1081  // adata = conn->data;
1082  if (adata)
1083  {
1084  if (adata->status == NNTP_BYE)
1085  adata->status = NNTP_NONE;
1086  if (nntp_open_connection(adata) < 0)
1087  return NULL;
1088 
1089  rc = nntp_newsrc_parse(adata);
1090  if (rc < 0)
1091  return NULL;
1092 
1093  /* check for new newsgroups */
1094  if (!leave_lock && (nntp_check_new_groups(m, adata) < 0))
1095  rc = -1;
1096 
1097  /* .newsrc has been externally modified */
1098  if (rc > 0)
1099  nntp_clear_cache(adata);
1100  if ((rc < 0) || !leave_lock)
1101  nntp_newsrc_close(adata);
1102  return (rc < 0) ? NULL : adata;
1103  }
1104 
1105  /* new news server */
1106  adata = nntp_adata_new(conn);
1107 
1108  rc = nntp_open_connection(adata);
1109 
1110  /* try to create cache directory and enable caching */
1111  adata->cacheable = false;
1112  const char *const c_news_cache_dir = cs_subset_path(NeoMutt->sub, "news_cache_dir");
1113  if ((rc >= 0) && c_news_cache_dir)
1114  {
1115  cache_expand(file, sizeof(file), &conn->account, NULL);
1116  if (mutt_file_mkdir(file, S_IRWXU) < 0)
1117  {
1118  mutt_error(_("Can't create %s: %s"), file, strerror(errno));
1119  }
1120  adata->cacheable = true;
1121  }
1122 
1123  /* load .newsrc */
1124  if (rc >= 0)
1125  {
1126  const char *const c_newsrc = cs_subset_path(NeoMutt->sub, "newsrc");
1127  mutt_expando_format(file, sizeof(file), 0, sizeof(file), NONULL(c_newsrc),
1128  nntp_format_str, (intptr_t) adata, MUTT_FORMAT_NO_FLAGS);
1129  mutt_expand_path(file, sizeof(file));
1130  adata->newsrc_file = mutt_str_dup(file);
1131  rc = nntp_newsrc_parse(adata);
1132  }
1133  if (rc >= 0)
1134  {
1135  /* try to load list of newsgroups from cache */
1136  if (adata->cacheable && (active_get_cache(adata) == 0))
1137  rc = nntp_check_new_groups(m, adata);
1138 
1139  /* load list of newsgroups from server */
1140  else
1141  rc = nntp_active_fetch(adata, false);
1142  }
1143 
1144  if (rc >= 0)
1145  nntp_clear_cache(adata);
1146 
1147 #ifdef USE_HCACHE
1148  /* check cache files */
1149  if ((rc >= 0) && adata->cacheable)
1150  {
1151  struct dirent *entry = NULL;
1152  DIR *dp = opendir(file);
1153 
1154  if (dp)
1155  {
1156  while ((entry = readdir(dp)))
1157  {
1158  struct HeaderCache *hc = NULL;
1159  void *hdata = NULL;
1160  char *group = entry->d_name;
1161 
1162  char *p = group + strlen(group) - 7;
1163  if ((strlen(group) < 8) || (strcmp(p, ".hcache") != 0))
1164  continue;
1165  *p = '\0';
1167  if (!mdata)
1168  continue;
1169 
1170  hc = nntp_hcache_open(mdata);
1171  if (!hc)
1172  continue;
1173 
1174  /* fetch previous values of first and last */
1175  size_t dlen = 0;
1176  hdata = mutt_hcache_fetch_raw(hc, "index", 5, &dlen);
1177  if (hdata)
1178  {
1179  anum_t first, last;
1180 
1181  if (sscanf(hdata, ANUM " " ANUM, &first, &last) == 2)
1182  {
1183  if (mdata->deleted)
1184  {
1185  mdata->first_message = first;
1186  mdata->last_message = last;
1187  }
1188  if ((last >= mdata->first_message) && (last <= mdata->last_message))
1189  {
1190  mdata->last_cached = last;
1191  mutt_debug(LL_DEBUG2, "%s last_cached=%u\n", mdata->group, last);
1192  }
1193  }
1194  mutt_hcache_free_raw(hc, &hdata);
1195  }
1196  mutt_hcache_close(hc);
1197  }
1198  closedir(dp);
1199  }
1200  }
1201 #endif
1202 
1203  if ((rc < 0) || !leave_lock)
1205 
1206  if (rc < 0)
1207  {
1209  FREE(&adata->groups_list);
1210  FREE(&adata->newsrc_file);
1212  FREE(&adata);
1213  mutt_socket_close(conn);
1214  FREE(&conn);
1215  return NULL;
1216  }
1217 
1218  return adata;
1219 }
#define MUTT_ACCT_SSL
Account uses SSL/TLS.
Definition: connaccount.h:47
#define MUTT_ACCT_PASS
Password field has been set.
Definition: connaccount.h:46
#define MUTT_ACCT_USER
User field has been set.
Definition: connaccount.h:44
int mutt_file_mkdir(const char *path, mode_t mode)
Recursively create directories.
Definition: file.c:930
#define MUTT_FORMAT_NO_FLAGS
No flags are set.
Definition: format_flags.h:30
const char * nntp_format_str(char *buf, size_t buflen, size_t col, int cols, char op, const char *src, const char *prec, const char *if_str, const char *else_str, intptr_t data, MuttFormatFlags flags)
Expand the newsrc filename - Implements format_t -.
Definition: newsrc.c:924
void mutt_expando_format(char *buf, size_t buflen, size_t col, int cols, const char *src, format_t callback, intptr_t data, MuttFormatFlags flags)
Expand expandos (x) in a string -.
Definition: muttlib.c:777
#define mutt_error(...)
Definition: logging.h:87
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
void mutt_hcache_close(struct HeaderCache *hc)
Multiplexor for StoreOps::close.
Definition: hcache.c:432
int mutt_account_fromurl(struct ConnAccount *cac, const struct Url *url)
Fill ConnAccount with information from url.
Definition: mutt_account.c:43
@ MUTT_ACCT_TYPE_NNTP
Nntp (Usenet) Account.
Definition: mutt_account.h:40
struct Connection * mutt_conn_find(const struct ConnAccount *cac)
Find a connection from a list.
Definition: mutt_socket.c:89
char * mutt_expand_path(char *buf, size_t buflen)
Create the canonical path.
Definition: muttlib.c:123
void nntp_clear_cache(struct NntpAccountData *adata)
Clear the NNTP cache.
Definition: newsrc.c:846
static const char * nntp_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field()
Definition: newsrc.c:987
int nntp_newsrc_parse(struct NntpAccountData *adata)
Parse .newsrc file.
Definition: newsrc.c:162
static int active_get_cache(struct NntpAccountData *adata)
Load list of all newsgroups from cache.
Definition: newsrc.c:618
struct HeaderCache * nntp_hcache_open(struct NntpMboxData *mdata)
Open newsgroup hcache.
Definition: newsrc.c:711
struct NntpAccountData * nntp_adata_new(struct Connection *conn)
Allocate and initialise a new NntpAccountData structure.
Definition: adata.c:62
int nntp_active_fetch(struct NntpAccountData *adata, bool mark_new)
Fetch list of all newsgroups from server.
Definition: nntp.c:1954
#define NNTP_SSL_PORT
Definition: private.h:37
@ NNTP_NONE
No connection to server.
Definition: private.h:44
@ NNTP_BYE
Disconnected from server.
Definition: private.h:46
#define NNTP_PORT
Definition: private.h:36
int nntp_check_new_groups(struct Mailbox *m, struct NntpAccountData *adata)
Check for new groups/articles in subscribed groups.
Definition: nntp.c:2022
int nntp_open_connection(struct NntpAccountData *adata)
Connect to server, authenticate and get capabilities.
Definition: nntp.c:1706
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:97
#define NONULL(x)
Definition: string2.h:37
Login details for a remote server.
Definition: connaccount.h:53
char user[128]
Username.
Definition: connaccount.h:56
char pass[256]
Password.
Definition: connaccount.h:57
const char * service
Name of the service, e.g. "imap".
Definition: connaccount.h:61
const char *(* get_field)(enum ConnAccountField field, void *gf_data)
Function to get some login credentials.
Definition: connaccount.h:68
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
Definition: connaccount.h:59
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:60
unsigned short port
Port to connect to.
Definition: connaccount.h:58
Header cache structure.
Definition: lib.h:87
unsigned int status
Definition: adata.h:48
char * authenticators
Definition: adata.h:53
char * user
Username.
Definition: url.h:71
char * host
Host.
Definition: url.h:73
char * pass
Password.
Definition: url.h:72
enum UrlScheme scheme
Scheme, e.g. U_SMTPS.
Definition: url.h:70
struct Url * url_parse(const char *src)
Fill in Url.
Definition: url.c:234
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition: url.c:123
@ U_NNTPS
Url is nntps://.
Definition: url.h:42
@ U_NNTP
Url is nntp://.
Definition: url.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_article_status()

void nntp_article_status ( struct Mailbox m,
struct Email e,
char *  group,
anum_t  anum 
)

Get status of articles from .newsrc.

Parameters
mMailbox
eEmail
groupNewsgroup
anumArticle number

Full status flags are not supported by nntp, but we can fake some of them: Read = a read message number is in the .newsrc New = not read and not cached Old = not read but cached

Definition at line 1233 of file newsrc.c.

1234 {
1235  struct NntpMboxData *mdata = m->mdata;
1236 
1237  if (group)
1238  mdata = mutt_hash_find(mdata->adata->groups_hash, group);
1239 
1240  if (!mdata)
1241  return;
1242 
1243  for (unsigned int i = 0; i < mdata->newsrc_len; i++)
1244  {
1245  if ((anum >= mdata->newsrc_ent[i].first) && (anum <= mdata->newsrc_ent[i].last))
1246  {
1247  /* can't use mutt_set_flag() because mview_update() didn't get called yet */
1248  e->read = true;
1249  return;
1250  }
1251  }
1252 
1253  /* article was not cached yet, it's new */
1254  if (anum > mdata->last_cached)
1255  return;
1256 
1257  /* article isn't read but cached, it's old */
1258  const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old");
1259  if (c_mark_old)
1260  e->old = true;
1261 }
bool old
Email is seen, but unread.
Definition: email.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_newsgroup_subscribe()

struct NntpMboxData* mutt_newsgroup_subscribe ( struct NntpAccountData adata,
char *  group 
)

Subscribe newsgroup.

Parameters
adataNNTP server
groupNewsgroup
Return values
ptrNNTP data
NULLError

Definition at line 1270 of file newsrc.c.

1271 {
1272  if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1273  return NULL;
1274 
1275  struct NntpMboxData *mdata = mdata_find(adata, group);
1276  mdata->subscribed = true;
1277  if (!mdata->newsrc_ent)
1278  {
1279  mdata->newsrc_ent = mutt_mem_calloc(1, sizeof(struct NewsrcEntry));
1280  mdata->newsrc_len = 1;
1281  mdata->newsrc_ent[0].first = 1;
1282  mdata->newsrc_ent[0].last = 0;
1283  }
1284  return mdata;
1285 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_newsgroup_unsubscribe()

struct NntpMboxData* mutt_newsgroup_unsubscribe ( struct NntpAccountData adata,
char *  group 
)

Unsubscribe newsgroup.

Parameters
adataNNTP server
groupNewsgroup
Return values
ptrNNTP data
NULLError

Definition at line 1294 of file newsrc.c.

1295 {
1296  if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1297  return NULL;
1298 
1300  if (!mdata)
1301  return NULL;
1302 
1303  mdata->subscribed = false;
1304  const bool c_save_unsubscribed = cs_subset_bool(NeoMutt->sub, "save_unsubscribed");
1305  if (!c_save_unsubscribed)
1306  {
1307  mdata->newsrc_len = 0;
1308  FREE(&mdata->newsrc_ent);
1309  }
1310  return mdata;
1311 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_newsgroup_catchup()

struct NntpMboxData* mutt_newsgroup_catchup ( struct Mailbox m,
struct NntpAccountData adata,
char *  group 
)

Catchup newsgroup.

Parameters
mMailbox
adataNNTP server
groupNewsgroup
Return values
ptrNNTP data
NULLError

Definition at line 1321 of file newsrc.c.

1323 {
1324  if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1325  return NULL;
1326 
1328  if (!mdata)
1329  return NULL;
1330 
1331  if (mdata->newsrc_ent)
1332  {
1333  mutt_mem_realloc(&mdata->newsrc_ent, sizeof(struct NewsrcEntry));
1334  mdata->newsrc_len = 1;
1335  mdata->newsrc_ent[0].first = 1;
1336  mdata->newsrc_ent[0].last = mdata->last_message;
1337  }
1338  mdata->unread = 0;
1339  if (m && (m->mdata == mdata))
1340  {
1341  for (unsigned int i = 0; i < m->msg_count; i++)
1342  {
1343  struct Email *e = m->emails[i];
1344  if (!e)
1345  break;
1346  mutt_set_flag(m, e, MUTT_READ, true);
1347  }
1348  }
1349  return mdata;
1350 }
@ MUTT_READ
Messages that have been read.
Definition: mutt.h:93
#define mutt_set_flag(m, e, flag, bf)
Definition: protos.h:64
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_newsgroup_uncatchup()

struct NntpMboxData* mutt_newsgroup_uncatchup ( struct Mailbox m,
struct NntpAccountData adata,
char *  group 
)

Uncatchup newsgroup.

Parameters
mMailbox
adataNNTP server
groupNewsgroup
Return values
ptrNNTP data
NULLError

Definition at line 1360 of file newsrc.c.

1362 {
1363  if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1364  return NULL;
1365 
1367  if (!mdata)
1368  return NULL;
1369 
1370  if (mdata->newsrc_ent)
1371  {
1372  mutt_mem_realloc(&mdata->newsrc_ent, sizeof(struct NewsrcEntry));
1373  mdata->newsrc_len = 1;
1374  mdata->newsrc_ent[0].first = 1;
1375  mdata->newsrc_ent[0].last = mdata->first_message - 1;
1376  }
1377  if (m && (m->mdata == mdata))
1378  {
1379  mdata->unread = m->msg_count;
1380  for (unsigned int i = 0; i < m->msg_count; i++)
1381  {
1382  struct Email *e = m->emails[i];
1383  if (!e)
1384  break;
1385  mutt_set_flag(m, e, MUTT_READ, false);
1386  }
1387  }
1388  else
1389  {
1390  mdata->unread = mdata->last_message;
1391  if (mdata->newsrc_ent)
1392  mdata->unread -= mdata->newsrc_ent[0].last;
1393  }
1394  return mdata;
1395 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nntp_mailbox()

void nntp_mailbox ( struct Mailbox m,
char *  buf,
size_t  buflen 
)

Get first newsgroup with new messages.

Parameters
mMailbox
bufBuffer for result
buflenLength of buffer

Definition at line 1403 of file newsrc.c.

1404 {
1405  if (!m)
1406  return;
1407 
1408  for (unsigned int i = 0; i < CurrentNewsSrv->groups_num; i++)
1409  {
1411 
1412  if (!mdata || !mdata->subscribed || !mdata->unread)
1413  continue;
1414 
1415  if ((m->type == MUTT_NNTP) &&
1416  mutt_str_equal(mdata->group, ((struct NntpMboxData *) m->mdata)->group))
1417  {
1418  unsigned int unread = 0;
1419 
1420  for (unsigned int j = 0; j < m->msg_count; j++)
1421  {
1422  struct Email *e = m->emails[j];
1423  if (!e)
1424  break;
1425  if (!e->read && !e->deleted)
1426  unread++;
1427  }
1428  if (unread == 0)
1429  continue;
1430  }
1431  mutt_str_copy(buf, mdata->group, buflen);
1432  break;
1433  }
1434 }
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:49
struct NntpAccountData * CurrentNewsSrv
Current NNTP news server.
Definition: nntp.c:77
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
+ Here is the call graph for this function:
+ Here is the caller graph for this function: