NeoMutt  2023-05-17-56-ga67199
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:653
void * mdata
Driver specific data.
Definition: mailbox.h:132
struct HashTable * groups_hash
Hash Table: "newsgroup" -> NntpMboxData.
Definition: adata.h:61
unsigned int groups_num
Definition: adata.h:58
unsigned int groups_max
Definition: adata.h:59
void ** groups_list
Definition: adata.h:60
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));
126}
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:150
int mutt_file_unlock(int fd)
Unlock a file previously locked by mutt_file_lock()
Definition: file.c:1266
#define mutt_debug(LEVEL,...)
Definition: logging2.h:87
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
FILE * fp_newsrc
Definition: adata.h:50
char * newsrc_file
Definition: adata.h:51
+ 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 */
174 }
175 else
176 {
177 /* if file doesn't exist, create it */
178 adata->fp_newsrc = mutt_file_fopen(adata->newsrc_file, "a");
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 {
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}
FILE * mutt_file_fopen(const char *path, const char *mode)
Call fopen() safely.
Definition: file.c:634
int mutt_file_lock(int fd, bool excl, bool timeout)
(Try to) Lock a file using fcntl()
Definition: file.c:1218
#define mutt_perror(...)
Definition: logging2.h:91
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
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
static struct NntpMboxData * mdata_find(struct NntpAccountData *adata, const char *group)
Find NntpMboxData for given newsgroup or add it.
Definition: newsrc.c:72
void nntp_group_unread_stat(struct NntpMboxData *mdata)
Count number of unread articles using .newsrc data.
Definition: newsrc.c:132
#define ANUM
Definition: lib.h:61
An entry in a .newsrc (subscribed newsgroups)
Definition: lib.h:76
bool newsrc_modified
Definition: adata.h:49
off_t size
Definition: adata.h:54
time_t mtime
Definition: adata.h:55
+ 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 enum SortType 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 else
353 {
354 /* search for first read */
355 if (e->deleted || e->read)
356 {
357 first = last + 1;
358 series = true;
359 }
360 last = nntp_edata_get(e)->article_num;
361 }
362 }
363
364 if (series && (first <= mdata->last_loaded))
365 {
366 if (mdata->newsrc_len >= entries)
367 {
368 entries++;
369 mutt_mem_realloc(&mdata->newsrc_ent, entries * sizeof(struct NewsrcEntry));
370 }
371 mdata->newsrc_ent[mdata->newsrc_len].first = first;
372 mdata->newsrc_ent[mdata->newsrc_len].last = mdata->last_loaded;
373 mdata->newsrc_len++;
374 }
375 mutt_mem_realloc(&mdata->newsrc_ent, mdata->newsrc_len * sizeof(struct NewsrcEntry));
376
377 if (c_sort != SORT_ORDER)
378 {
379 cs_subset_str_native_set(NeoMutt->sub, "sort", c_sort, NULL);
381 }
382}
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:223
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:176
struct NntpEmailData * nntp_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:57
SortType
Methods for sorting.
Definition: sort2.h:38
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:44
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:310
+ Here is the call graph for this function:
+ Here is the caller 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 391 of file newsrc.c.

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

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

523{
524 char *c = NULL;
525 char file[PATH_MAX] = { 0 };
526
527 /* server subdirectory */
528 if (cac)
529 {
530 struct Url url = { 0 };
531
532 mutt_account_tourl(cac, &url);
533 url.path = mutt_str_dup(src);
534 url_tostring(&url, file, sizeof(file), U_PATH);
535 FREE(&url.path);
536 }
537 else
538 {
539 mutt_str_copy(file, src ? src : "", sizeof(file));
540 }
541
542 const char *const c_news_cache_dir = cs_subset_path(NeoMutt->sub, "news_cache_dir");
543 snprintf(dst, dstlen, "%s/%s", c_news_cache_dir, file);
544
545 /* remove trailing slash */
546 c = dst + strlen(dst) - 1;
547 if (*c == '/')
548 *c = '\0';
549
550 struct Buffer *tmp = buf_pool_get();
551 buf_addstr(tmp, dst);
552 buf_expand_path(tmp);
553 mutt_encode_path(tmp, dst);
554 mutt_str_copy(dst, buf_string(tmp), dstlen);
555 buf_pool_release(&tmp);
556}
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:238
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:90
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:251
void mutt_account_tourl(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
Definition: mutt_account.c:79
void buf_expand_path(struct Buffer *buf)
Create the canonical path.
Definition: muttlib.c:333
void mutt_encode_path(struct Buffer *buf, const char *src)
Convert a path to 'us-ascii'.
Definition: muttlib.c:1453
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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:422
#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 564 of file newsrc.c.

565{
566 struct Url url = { 0 };
567
568 mutt_account_tourl(cac, &url);
569 url.path = mutt_str_dup(buf);
570 url_tostring(&url, buf, buflen, U_NO_FLAGS);
571 FREE(&url.path);
572}
#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 580 of file newsrc.c.

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

622{
623 char buf[8192] = { 0 };
624 char file[4096] = { 0 };
625 time_t t = 0;
626
627 cache_expand(file, sizeof(file), &adata->conn->account, ".active");
628 mutt_debug(LL_DEBUG1, "Parsing %s\n", file);
629 FILE *fp = mutt_file_fopen(file, "r");
630 if (!fp)
631 return -1;
632
633 if (!fgets(buf, sizeof(buf), fp) || (sscanf(buf, "%jd%4095s", &t, file) != 1) || (t == 0))
634 {
635 mutt_file_fclose(&fp);
636 return -1;
637 }
638 adata->newgroups_time = t;
639
640 mutt_message(_("Loading list of groups from cache..."));
641 while (fgets(buf, sizeof(buf), fp))
642 nntp_add_group(buf, adata);
643 nntp_add_group(NULL, NULL);
644 mutt_file_fclose(&fp);
646 return 0;
647}
#define mutt_message(...)
Definition: logging2.h:89
#define _(a)
Definition: message.h:28
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:73
int nntp_add_group(char *line, void *data)
Parse newsgroup.
Definition: newsrc.c:580
static void cache_expand(char *dst, size_t dstlen, struct ConnAccount *cac, const char *src)
Make fully qualified cache file name.
Definition: newsrc.c:522
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:50
time_t newgroups_time
Definition: adata.h:56
struct Connection * conn
Connection to NNTP Server.
Definition: adata.h:62
+ 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 655 of file newsrc.c.

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

715{
716 struct Url url = { 0 };
717 char file[PATH_MAX] = { 0 };
718
719 const bool c_save_unsubscribed = cs_subset_bool(NeoMutt->sub, "save_unsubscribed");
720 if (!mdata->adata || !mdata->adata->cacheable || !mdata->adata->conn || !mdata->group ||
721 !(mdata->newsrc_ent || mdata->subscribed || c_save_unsubscribed))
722 {
723 return NULL;
724 }
725
726 mutt_account_tourl(&mdata->adata->conn->account, &url);
727 url.path = mdata->group;
728 url_tostring(&url, file, sizeof(file), U_PATH);
729 const char *const c_news_cache_dir = cs_subset_path(NeoMutt->sub, "news_cache_dir");
730 return mutt_hcache_open(c_news_cache_dir, file, nntp_hcache_namer);
731}
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:695
struct HeaderCache * mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:379
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 738 of file newsrc.c.

739{
740 if (!hc)
741 return;
742
743 char buf[32] = { 0 };
744 bool old = false;
745 anum_t first = 0, last = 0;
746
747 /* fetch previous values of first and last */
748 char *hdata = mutt_hcache_fetch_str(hc, "index", 5);
749 if (hdata)
750 {
751 mutt_debug(LL_DEBUG2, "mutt_hcache_fetch index: %s\n", hdata);
752 if (sscanf(hdata, ANUM " " ANUM, &first, &last) == 2)
753 {
754 old = true;
755 mdata->last_cached = last;
756
757 /* clean removed headers from cache */
758 for (anum_t current = first; current <= last; current++)
759 {
760 if ((current >= mdata->first_message) && (current <= mdata->last_message))
761 continue;
762
763 snprintf(buf, sizeof(buf), ANUM, current);
764 mutt_debug(LL_DEBUG2, "mutt_hcache_delete_record %s\n", buf);
765 mutt_hcache_delete_record(hc, buf, strlen(buf));
766 }
767 }
768 FREE(&hdata);
769 }
770
771 /* store current values of first and last */
772 if (!old || (mdata->first_message != first) || (mdata->last_message != last))
773 {
774 snprintf(buf, sizeof(buf), ANUM " " ANUM, mdata->first_message, mdata->last_message);
775 mutt_debug(LL_DEBUG2, "mutt_hcache_store index: %s\n", buf);
776 mutt_hcache_store_raw(hc, "index", 5, buf, strlen(buf) + 1);
777 }
778}
int mutt_hcache_delete_record(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:695
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:674
char * mutt_hcache_fetch_str(struct HeaderCache *hc, const char *key, size_t keylen)
Fetch a string from the cache.
Definition: hcache.c:600
anum_t last_cached
Definition: mdata.h:39
+ Here is the call graph for this function:
+ Here is the caller 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 805 of file newsrc.c.

806{
808}
int mutt_bcache_list(struct BodyCache *bcache, bcache_list_t want_id, void *data)
Find matching entries in the Body Cache.
Definition: bcache.c:328
static int nntp_bcache_delete(const char *id, struct BodyCache *bcache, void *data)
Remove bcache file - Implements bcache_list_t -.
Definition: newsrc.c:785
struct BodyCache * bcache
Definition: mdata.h:49
+ Here is the call graph for this function:
+ Here is the caller 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 814 of file newsrc.c.

815{
816 if (!mdata || !mdata->adata || !mdata->adata->cacheable)
817 return;
818
819#ifdef USE_HCACHE
820 struct Buffer file = buf_make(PATH_MAX);
821 nntp_hcache_namer(mdata->group, &file);
822 cache_expand(file.data, file.dsize, &mdata->adata->conn->account, buf_string(&file));
823 unlink(buf_string(&file));
824 mdata->last_cached = 0;
825 mutt_debug(LL_DEBUG2, "%s\n", buf_string(&file));
826 buf_dealloc(&file);
827#endif
828
829 if (!mdata->bcache)
830 {
831 mdata->bcache = mutt_bcache_open(&mdata->adata->conn->account, mdata->group);
832 }
833 if (mdata->bcache)
834 {
835 mutt_debug(LL_DEBUG2, "%s/*\n", mdata->group);
837 mutt_bcache_close(&mdata->bcache);
838 }
839}
struct BodyCache * mutt_bcache_open(struct ConnAccount *account, const char *mailbox)
Open an Email-Body Cache.
Definition: bcache.c:143
void mutt_bcache_close(struct BodyCache **bcache)
Close an Email-Body Cache.
Definition: bcache.c:164
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:383
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
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 847 of file newsrc.c.

848{
849 char file[PATH_MAX] = { 0 };
850 char *fp = NULL;
851 struct dirent *de = NULL;
852 DIR *dir = NULL;
853
854 if (!adata || !adata->cacheable)
855 return;
856
857 cache_expand(file, sizeof(file), &adata->conn->account, NULL);
859 if (dir)
860 {
861 mutt_strn_cat(file, sizeof(file), "/", 1);
862 fp = file + strlen(file);
863 while ((de = readdir(dir)))
864 {
865 char *group = de->d_name;
866 struct stat st = { 0 };
867 struct NntpMboxData *mdata = NULL;
868 struct NntpMboxData tmp_mdata;
869
870 if (mutt_str_equal(group, ".") || mutt_str_equal(group, ".."))
871 continue;
872 *fp = '\0';
873 mutt_strn_cat(file, sizeof(file), group, strlen(group));
874 if (stat(file, &st) != 0)
875 continue;
876
877#ifdef USE_HCACHE
878 if (S_ISREG(st.st_mode))
879 {
880 char *ext = group + strlen(group) - 7;
881 if ((strlen(group) < 8) || !mutt_str_equal(ext, ".hcache"))
882 continue;
883 *ext = '\0';
884 }
885 else
886#endif
887 if (!S_ISDIR(st.st_mode))
888 continue;
889
890 const bool c_save_unsubscribed = cs_subset_bool(NeoMutt->sub, "save_unsubscribed");
892 if (!mdata)
893 {
894 mdata = &tmp_mdata;
895 mdata->adata = adata;
896 mdata->group = group;
897 mdata->bcache = NULL;
898 }
899 else if (mdata->newsrc_ent || mdata->subscribed || c_save_unsubscribed)
900 {
901 continue;
902 }
903
905 if (S_ISDIR(st.st_mode))
906 {
907 rmdir(file);
908 mutt_debug(LL_DEBUG2, "%s\n", file);
909 }
910 }
911 closedir(dir);
912 }
913}
DIR * mutt_file_opendir(const char *path, enum MuttOpenDirMode mode)
Open a directory.
Definition: file.c:614
@ MUTT_OPENDIR_NONE
Plain opendir()
Definition: file.h:73
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
char * mutt_strn_cat(char *d, size_t l, const char *s, size_t sl)
Concatenate two strings.
Definition: string.c:295
void nntp_delete_group_cache(struct NntpMboxData *mdata)
Remove hcache and bcache of newsgroup.
Definition: newsrc.c:814
+ 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 990 of file newsrc.c.

991{
992 switch (field)
993 {
994 case MUTT_CA_LOGIN:
995 case MUTT_CA_USER:
996 return cs_subset_string(NeoMutt->sub, "nntp_user");
997 case MUTT_CA_PASS:
998 return cs_subset_string(NeoMutt->sub, "nntp_pass");
1000 case MUTT_CA_HOST:
1001 default:
1002 return NULL;
1003 }
1004}
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 1021 of file newsrc.c.

1022{
1023 char file[PATH_MAX] = { 0 };
1024 int rc;
1025 struct ConnAccount cac = { { 0 } };
1026 struct NntpAccountData *adata = NULL;
1027 struct Connection *conn = NULL;
1028
1029 if (!server || (*server == '\0'))
1030 {
1031 mutt_error(_("No news server defined"));
1032 return NULL;
1033 }
1034
1035 /* create account from news server url */
1036 cac.flags = 0;
1037 cac.port = NNTP_PORT;
1039 cac.service = "nntp";
1041
1042 snprintf(file, sizeof(file), "%s%s", strstr(server, "://") ? "" : "news://", server);
1043 struct Url *url = url_parse(file);
1044 if (!url || (url->path && *url->path) ||
1045 !((url->scheme == U_NNTP) || (url->scheme == U_NNTPS)) || !url->host ||
1046 (mutt_account_fromurl(&cac, url) < 0))
1047 {
1048 url_free(&url);
1049 mutt_error(_("%s is an invalid news server specification"), server);
1050 return NULL;
1051 }
1052 if (url->scheme == U_NNTPS)
1053 {
1054 cac.flags |= MUTT_ACCT_SSL;
1055 cac.port = NNTP_SSL_PORT;
1056 }
1057 url_free(&url);
1058
1059 // If nntp_user and nntp_pass are specified in the config, use them to find the connection
1060 const char *user = NULL;
1061 const char *pass = NULL;
1062 if ((user = cac.get_field(MUTT_CA_USER, NULL)))
1063 {
1064 mutt_str_copy(cac.user, user, sizeof(cac.user));
1065 cac.flags |= MUTT_ACCT_USER;
1066 }
1067 if ((pass = cac.get_field(MUTT_CA_PASS, NULL)))
1068 {
1069 mutt_str_copy(cac.pass, pass, sizeof(cac.pass));
1070 cac.flags |= MUTT_ACCT_PASS;
1071 }
1072
1073 /* find connection by account */
1074 conn = mutt_conn_find(&cac);
1075 if (!conn)
1076 return NULL;
1077 if (!(conn->account.flags & MUTT_ACCT_USER) && cac.flags & MUTT_ACCT_USER)
1078 {
1079 conn->account.flags |= MUTT_ACCT_USER;
1080 conn->account.user[0] = '\0';
1081 }
1082
1083 /* news server already exists */
1084 if (adata)
1085 {
1086 if (adata->status == NNTP_BYE)
1087 adata->status = NNTP_NONE;
1088 if (nntp_open_connection(adata) < 0)
1089 return NULL;
1090
1091 rc = nntp_newsrc_parse(adata);
1092 if (rc < 0)
1093 return NULL;
1094
1095 /* check for new newsgroups */
1096 if (!leave_lock && (nntp_check_new_groups(m, adata) < 0))
1097 rc = -1;
1098
1099 /* .newsrc has been externally modified */
1100 if (rc > 0)
1101 nntp_clear_cache(adata);
1102 if ((rc < 0) || !leave_lock)
1103 nntp_newsrc_close(adata);
1104 return (rc < 0) ? NULL : adata;
1105 }
1106
1107 /* new news server */
1108 adata = nntp_adata_new(conn);
1109
1110 rc = nntp_open_connection(adata);
1111
1112 /* try to create cache directory and enable caching */
1113 adata->cacheable = false;
1114 const char *const c_news_cache_dir = cs_subset_path(NeoMutt->sub, "news_cache_dir");
1115 if ((rc >= 0) && c_news_cache_dir)
1116 {
1117 cache_expand(file, sizeof(file), &conn->account, NULL);
1118 if (mutt_file_mkdir(file, S_IRWXU) < 0)
1119 {
1120 mutt_error(_("Can't create %s: %s"), file, strerror(errno));
1121 }
1122 adata->cacheable = true;
1123 }
1124
1125 /* load .newsrc */
1126 if (rc >= 0)
1127 {
1128 const char *const c_newsrc = cs_subset_path(NeoMutt->sub, "newsrc");
1129 mutt_expando_format(file, sizeof(file), 0, sizeof(file), NONULL(c_newsrc),
1130 nntp_format_str, (intptr_t) adata, MUTT_FORMAT_NO_FLAGS);
1131 mutt_expand_path(file, sizeof(file));
1132 adata->newsrc_file = mutt_str_dup(file);
1133 rc = nntp_newsrc_parse(adata);
1134 }
1135 if (rc >= 0)
1136 {
1137 /* try to load list of newsgroups from cache */
1138 if (adata->cacheable && (active_get_cache(adata) == 0))
1139 {
1140 rc = nntp_check_new_groups(m, adata);
1141 }
1142 else
1143 {
1144 /* load list of newsgroups from server */
1145 rc = nntp_active_fetch(adata, false);
1146 }
1147 }
1148
1149 if (rc >= 0)
1150 nntp_clear_cache(adata);
1151
1152#ifdef USE_HCACHE
1153 /* check cache files */
1154 if ((rc >= 0) && adata->cacheable)
1155 {
1156 struct dirent *de = NULL;
1157 DIR *dir = mutt_file_opendir(file, MUTT_OPENDIR_NONE);
1158
1159 if (dir)
1160 {
1161 while ((de = readdir(dir)))
1162 {
1163 struct HeaderCache *hc = NULL;
1164 char *group = de->d_name;
1165
1166 char *p = group + strlen(group) - 7;
1167 if ((strlen(group) < 8) || !mutt_str_equal(p, ".hcache"))
1168 continue;
1169 *p = '\0';
1171 if (!mdata)
1172 continue;
1173
1174 hc = nntp_hcache_open(mdata);
1175 if (!hc)
1176 continue;
1177
1178 /* fetch previous values of first and last */
1179 char *hdata = mutt_hcache_fetch_str(hc, "index", 5);
1180 if (hdata)
1181 {
1182 anum_t first, last;
1183
1184 if (sscanf(hdata, ANUM " " ANUM, &first, &last) == 2)
1185 {
1186 if (mdata->deleted)
1187 {
1188 mdata->first_message = first;
1189 mdata->last_message = last;
1190 }
1191 if ((last >= mdata->first_message) && (last <= mdata->last_message))
1192 {
1193 mdata->last_cached = last;
1194 mutt_debug(LL_DEBUG2, "%s last_cached=" ANUM "\n", mdata->group, last);
1195 }
1196 }
1197 FREE(&hdata);
1198 }
1200 }
1201 closedir(dir);
1202 }
1203 }
1204#endif
1205
1206 if ((rc < 0) || !leave_lock)
1208
1209 if (rc < 0)
1210 {
1215 FREE(&adata);
1216 mutt_socket_close(conn);
1217 FREE(&conn);
1218 return NULL;
1219 }
1220
1221 return adata;
1222}
#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:952
#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:927
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:742
#define mutt_error(...)
Definition: logging2.h:90
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:489
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:124
struct HeaderCache * nntp_hcache_open(struct NntpMboxData *mdata)
Open newsgroup hcache.
Definition: newsrc.c:714
void nntp_clear_cache(struct NntpAccountData *adata)
Clear the NNTP cache.
Definition: newsrc.c:847
int nntp_newsrc_parse(struct NntpAccountData *adata)
Parse .newsrc file.
Definition: newsrc.c:162
static const char * nntp_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field()
Definition: newsrc.c:990
static int active_get_cache(struct NntpAccountData *adata)
Load list of all newsgroups from cache.
Definition: newsrc.c:621
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:1994
#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:2062
int nntp_open_connection(struct NntpAccountData *adata)
Connect to server, authenticate and get capabilities.
Definition: nntp.c:1738
int mutt_socket_close(struct Connection *conn)
Close a socket.
Definition: socket.c:101
#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:88
unsigned int status
Definition: adata.h:47
char * authenticators
Definition: adata.h:52
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:238
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 1236 of file newsrc.c.

1237{
1238 struct NntpMboxData *mdata = m->mdata;
1239
1240 if (group)
1241 mdata = mutt_hash_find(mdata->adata->groups_hash, group);
1242
1243 if (!mdata)
1244 return;
1245
1246 for (unsigned int i = 0; i < mdata->newsrc_len; i++)
1247 {
1248 if ((anum >= mdata->newsrc_ent[i].first) && (anum <= mdata->newsrc_ent[i].last))
1249 {
1250 /* can't use mutt_set_flag() because mview_update() didn't get called yet */
1251 e->read = true;
1252 return;
1253 }
1254 }
1255
1256 /* article was not cached yet, it's new */
1257 if (anum > mdata->last_cached)
1258 return;
1259
1260 /* article isn't read but cached, it's old */
1261 const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old");
1262 if (c_mark_old)
1263 e->old = true;
1264}
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 1273 of file newsrc.c.

1274{
1275 if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1276 return NULL;
1277
1279 mdata->subscribed = true;
1280 if (!mdata->newsrc_ent)
1281 {
1282 mdata->newsrc_ent = mutt_mem_calloc(1, sizeof(struct NewsrcEntry));
1283 mdata->newsrc_len = 1;
1284 mdata->newsrc_ent[0].first = 1;
1285 mdata->newsrc_ent[0].last = 0;
1286 }
1287 return mdata;
1288}
+ 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 1297 of file newsrc.c.

1298{
1299 if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1300 return NULL;
1301
1303 if (!mdata)
1304 return NULL;
1305
1306 mdata->subscribed = false;
1307 const bool c_save_unsubscribed = cs_subset_bool(NeoMutt->sub, "save_unsubscribed");
1308 if (!c_save_unsubscribed)
1309 {
1310 mdata->newsrc_len = 0;
1311 FREE(&mdata->newsrc_ent);
1312 }
1313 return mdata;
1314}
+ 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 1324 of file newsrc.c.

1326{
1327 if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1328 return NULL;
1329
1331 if (!mdata)
1332 return NULL;
1333
1334 if (mdata->newsrc_ent)
1335 {
1336 mutt_mem_realloc(&mdata->newsrc_ent, sizeof(struct NewsrcEntry));
1337 mdata->newsrc_len = 1;
1338 mdata->newsrc_ent[0].first = 1;
1339 mdata->newsrc_ent[0].last = mdata->last_message;
1340 }
1341 mdata->unread = 0;
1342 if (m && (m->mdata == mdata))
1343 {
1344 for (unsigned int i = 0; i < m->msg_count; i++)
1345 {
1346 struct Email *e = m->emails[i];
1347 if (!e)
1348 break;
1349 mutt_set_flag(m, e, MUTT_READ, true, true);
1350 }
1351 }
1352 return mdata;
1353}
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition: flags.c:52
@ MUTT_READ
Messages that have been read.
Definition: mutt.h:81
+ 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 1363 of file newsrc.c.

1365{
1366 if (!adata || !adata->groups_hash || !group || (*group == '\0'))
1367 return NULL;
1368
1370 if (!mdata)
1371 return NULL;
1372
1373 if (mdata->newsrc_ent)
1374 {
1375 mutt_mem_realloc(&mdata->newsrc_ent, sizeof(struct NewsrcEntry));
1376 mdata->newsrc_len = 1;
1377 mdata->newsrc_ent[0].first = 1;
1378 mdata->newsrc_ent[0].last = mdata->first_message - 1;
1379 }
1380 if (m && (m->mdata == mdata))
1381 {
1382 mdata->unread = m->msg_count;
1383 for (unsigned int i = 0; i < m->msg_count; i++)
1384 {
1385 struct Email *e = m->emails[i];
1386 if (!e)
1387 break;
1388 mutt_set_flag(m, e, MUTT_READ, false, true);
1389 }
1390 }
1391 else
1392 {
1393 mdata->unread = mdata->last_message;
1394 if (mdata->newsrc_ent)
1395 mdata->unread -= mdata->newsrc_ent[0].last;
1396 }
1397 return mdata;
1398}
+ 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 1406 of file newsrc.c.

1407{
1408 if (!m)
1409 return;
1410
1411 for (unsigned int i = 0; i < CurrentNewsSrv->groups_num; i++)
1412 {
1414
1415 if (!mdata || !mdata->subscribed || !mdata->unread)
1416 continue;
1417
1418 if ((m->type == MUTT_NNTP) &&
1419 mutt_str_equal(mdata->group, ((struct NntpMboxData *) m->mdata)->group))
1420 {
1421 unsigned int unread = 0;
1422
1423 for (unsigned int j = 0; j < m->msg_count; j++)
1424 {
1425 struct Email *e = m->emails[j];
1426 if (!e)
1427 break;
1428 if (!e->read && !e->deleted)
1429 unread++;
1430 }
1431 if (unread == 0)
1432 continue;
1433 }
1434 mutt_str_copy(buf, mdata->group, buflen);
1435 break;
1436 }
1437}
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:49
struct NntpAccountData * CurrentNewsSrv
Current NNTP news server.
Definition: nntp.c:78
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: