Coverage Report

Created: 2023-09-25 07:17

/src/neomutt/nntp/browse.c
Line
Count
Source (jump to first uncovered line)
1
/**
2
 * @file
3
 * Browse NNTP groups
4
 *
5
 * @authors
6
 * Copyright (C) 2018 Richard Russon <rich@flatcap.org>
7
 *
8
 * @copyright
9
 * This program is free software: you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License as published by the Free Software
11
 * Foundation, either version 2 of the License, or (at your option) any later
12
 * version.
13
 *
14
 * This program is distributed in the hope that it will be useful, but WITHOUT
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17
 * details.
18
 *
19
 * You should have received a copy of the GNU General Public License along with
20
 * this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
/**
24
 * @page nntp_browse Browse NNTP groups
25
 *
26
 * Browse NNTP groups
27
 */
28
29
#include "config.h"
30
#include <stdbool.h>
31
#include <stdint.h>
32
#include <stdio.h>
33
#include "mutt/lib.h"
34
#include "config/lib.h"
35
#include "core/lib.h"
36
#include "lib.h"
37
#include "browser/lib.h"
38
#include "format_flags.h"
39
#include "mdata.h"
40
#include "muttlib.h"
41
42
/**
43
 * group_index_format_str - Format a string for the newsgroup menu - Implements ::format_t - @ingroup expando_api
44
 *
45
 * | Expando | Description
46
 * | :------ | :-------------------------------------------------------
47
 * | \%a     | Alert: 1 if user is notified of new mail
48
 * | \%C     | Current newsgroup number
49
 * | \%d     | Description of newsgroup (becomes from server)
50
 * | \%f     | Newsgroup name
51
 * | \%M     | - if newsgroup not allowed for direct post (moderated for example)
52
 * | \%N     | N if newsgroup is new, u if unsubscribed, blank otherwise
53
 * | \%n     | Number of new articles in newsgroup
54
 * | \%p     | Poll: 1 if Mailbox is checked for new mail
55
 * | \%s     | Number of unread articles in newsgroup
56
 */
57
const char *group_index_format_str(char *buf, size_t buflen, size_t col, int cols,
58
                                   char op, const char *src, const char *prec,
59
                                   const char *if_str, const char *else_str,
60
                                   intptr_t data, MuttFormatFlags flags)
61
0
{
62
0
  char fn[128], fmt[128];
63
0
  struct Folder *folder = (struct Folder *) data;
64
0
  bool optional = (flags & MUTT_FORMAT_OPTIONAL);
65
66
0
  switch (op)
67
0
  {
68
0
    case 'a':
69
0
      if (!optional)
70
0
      {
71
0
        snprintf(fmt, sizeof(fmt), "%%%sd", prec);
72
0
        snprintf(buf, buflen, fmt, folder->ff->notify_user);
73
0
      }
74
0
      else
75
0
      {
76
0
        if (folder->ff->notify_user == 0)
77
0
          optional = false;
78
0
      }
79
0
      break;
80
81
0
    case 'C':
82
0
      snprintf(fmt, sizeof(fmt), "%%%sd", prec);
83
0
      snprintf(buf, buflen, fmt, folder->num + 1);
84
0
      break;
85
86
0
    case 'd':
87
0
      if (folder->ff->nd->desc)
88
0
      {
89
0
        char *desc = mutt_str_dup(folder->ff->nd->desc);
90
0
        const char *const c_newsgroups_charset = cs_subset_string(NeoMutt->sub, "newsgroups_charset");
91
0
        if (c_newsgroups_charset)
92
0
          mutt_ch_convert_string(&desc, c_newsgroups_charset, cc_charset(), MUTT_ICONV_HOOK_FROM);
93
0
        mutt_mb_filter_unprintable(&desc);
94
95
0
        snprintf(fmt, sizeof(fmt), "%%%ss", prec);
96
0
        snprintf(buf, buflen, fmt, desc);
97
0
        FREE(&desc);
98
0
      }
99
0
      else
100
0
      {
101
0
        snprintf(fmt, sizeof(fmt), "%%%ss", prec);
102
0
        snprintf(buf, buflen, fmt, "");
103
0
      }
104
0
      break;
105
106
0
    case 'f':
107
0
      mutt_str_copy(fn, folder->ff->name, sizeof(fn));
108
0
      snprintf(fmt, sizeof(fmt), "%%%ss", prec);
109
0
      snprintf(buf, buflen, fmt, fn);
110
0
      break;
111
112
0
    case 'M':
113
0
      snprintf(fmt, sizeof(fmt), "%%%sc", prec);
114
0
      if (folder->ff->nd->deleted)
115
0
        snprintf(buf, buflen, fmt, 'D');
116
0
      else
117
0
        snprintf(buf, buflen, fmt, folder->ff->nd->allowed ? ' ' : '-');
118
0
      break;
119
120
0
    case 'N':
121
0
      snprintf(fmt, sizeof(fmt), "%%%sc", prec);
122
0
      if (folder->ff->nd->subscribed)
123
0
        snprintf(buf, buflen, fmt, ' ');
124
0
      else
125
0
        snprintf(buf, buflen, fmt, folder->ff->has_new_mail ? 'N' : 'u');
126
0
      break;
127
128
0
    case 'n':
129
0
    {
130
0
      const bool c_mark_old = cs_subset_bool(NeoMutt->sub, "mark_old");
131
0
      if (c_mark_old && (folder->ff->nd->last_cached >= folder->ff->nd->first_message) &&
132
0
          (folder->ff->nd->last_cached <= folder->ff->nd->last_message))
133
0
      {
134
0
        snprintf(fmt, sizeof(fmt), "%%%sd", prec);
135
0
        snprintf(buf, buflen, fmt, folder->ff->nd->last_message - folder->ff->nd->last_cached);
136
0
      }
137
0
      else
138
0
      {
139
0
        snprintf(fmt, sizeof(fmt), "%%%sd", prec);
140
0
        snprintf(buf, buflen, fmt, folder->ff->nd->unread);
141
0
      }
142
0
      break;
143
0
    }
144
145
0
    case 'p':
146
0
      if (!optional)
147
0
      {
148
0
        snprintf(fmt, sizeof(fmt), "%%%sd", prec);
149
0
        snprintf(buf, buflen, fmt, folder->ff->poll_new_mail);
150
0
      }
151
0
      else
152
0
      {
153
0
        if (folder->ff->poll_new_mail == 0)
154
0
          optional = false;
155
0
      }
156
0
      break;
157
158
0
    case 's':
159
0
      if (optional)
160
0
      {
161
0
        if (folder->ff->nd->unread != 0)
162
0
        {
163
0
          mutt_expando_format(buf, buflen, col, cols, if_str,
164
0
                              group_index_format_str, data, flags);
165
0
        }
166
0
        else
167
0
        {
168
0
          mutt_expando_format(buf, buflen, col, cols, else_str,
169
0
                              group_index_format_str, data, flags);
170
0
        }
171
0
      }
172
0
      else
173
0
      {
174
0
        snprintf(fmt, sizeof(fmt), "%%%sd", prec);
175
0
        snprintf(buf, buflen, fmt, folder->ff->nd->unread);
176
0
      }
177
0
      break;
178
0
  }
179
0
  return src;
180
0
}