NeoMutt  2018-07-16 +952-a2da0a
Teaching an old dog new tricks
DOXYGEN
group.c File Reference

Handling for email address groups. More...

#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "group.h"
#include "globals.h"
#include "send.h"
+ Include dependency graph for group.c:

Go to the source code of this file.

Functions

struct Groupmutt_pattern_group (const char *k)
 Match a pattern to a Group. More...
 
static void group_remove (struct Group *g)
 Remove a Group from the Hash Table. More...
 
int mutt_group_context_clear (struct GroupContext **ctx)
 Empty a Group List. More...
 
static bool empty_group (struct Group *g)
 Is a Group empty? More...
 
void mutt_group_context_add (struct GroupContext **ctx, struct Group *group)
 Add a Group to a List. More...
 
void mutt_group_context_destroy (struct GroupContext **ctx)
 Destroy a Group List. More...
 
static void group_add_addrlist (struct Group *g, struct Address *a)
 Add an Address List to a Group. More...
 
static int group_remove_addrlist (struct Group *g, struct Address *a)
 Remove an Address List from a Group. More...
 
static int group_add_regex (struct Group *g, const char *s, int flags, struct Buffer *err)
 Add a Regex to a Group. More...
 
static int group_remove_regex (struct Group *g, const char *s)
 Remove a Regex from a Group. More...
 
void mutt_group_context_add_addrlist (struct GroupContext *ctx, struct Address *a)
 Add an Address List to a Group List. More...
 
int mutt_group_context_remove_addrlist (struct GroupContext *ctx, struct Address *a)
 Remove an Address List from a Group List. More...
 
int mutt_group_context_add_regex (struct GroupContext *ctx, const char *s, int flags, struct Buffer *err)
 Add a Regex to a Group List. More...
 
int mutt_group_context_remove_regex (struct GroupContext *ctx, const char *s)
 Remove a Regex from a Group List. More...
 
bool mutt_group_match (struct Group *g, const char *s)
 Does a string match an entry in a Group? More...
 

Detailed Description

Handling for email address groups.

Authors
  • Thomas Roessler
  • Rocco Rutte

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 group.c.

Function Documentation

struct Group* mutt_pattern_group ( const char *  k)

Match a pattern to a Group.

Parameters
kPattern to match
Return values
ptrMatching Group
ptrNewly created Group (if no match)

Definition at line 45 of file group.c.

46 {
47  struct Group *p = NULL;
48 
49  if (!k)
50  return 0;
51 
52  p = mutt_hash_find(Groups, k);
53  if (!p)
54  {
55  mutt_debug(2, "Creating group %s.\n", k);
56  p = mutt_mem_calloc(1, sizeof(struct Group));
57  p->name = mutt_str_strdup(k);
58  STAILQ_INIT(&p->rs);
60  }
61 
62  return p;
63 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
WHERE struct Hash * Groups
Hash table of alias groups (name -> Group)
Definition: globals.h:57
void * mutt_hash_find(const struct Hash *table, const char *strkey)
Find the HashElem data in a Hash table element using a key.
Definition: hash.c:374
#define STAILQ_INIT(head)
Definition: queue.h:370
char * name
Definition: group.h:42
struct RegexList rs
Definition: group.h:41
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:384
A set of email addresses.
Definition: group.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
struct HashElem * mutt_hash_insert(struct Hash *table, const char *strkey, void *data)
Add a new element to the Hash table (with string keys)
Definition: hash.c:349

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void group_remove ( struct Group g)
static

Remove a Group from the Hash Table.

Parameters
gGroup to remove

Definition at line 69 of file group.c.

70 {
71  if (!g)
72  return;
74  mutt_addr_free(&g->as);
76  FREE(&g->name);
77  FREE(&g);
78 }
void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data)
Remove an element from a Hash table.
Definition: hash.c:434
WHERE struct Hash * Groups
Hash table of alias groups (name -> Group)
Definition: globals.h:57
char * name
Definition: group.h:42
struct Address * as
Definition: group.h:40
struct RegexList rs
Definition: group.h:41
#define FREE(x)
Definition: memory.h:46
void mutt_addr_free(struct Address **p)
Free a list of Addresses.
Definition: address.c:446
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:166

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_group_context_clear ( struct GroupContext **  ctx)

Empty a Group List.

Parameters
ctxGroup List to modify
Return values
0Always

Definition at line 85 of file group.c.

86 {
87  struct GroupContext *t = NULL;
88  for (; ctx && *ctx; (*ctx) = t)
89  {
90  group_remove((*ctx)->g);
91  t = (*ctx)->next;
92  FREE(ctx);
93  }
94  return 0;
95 }
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:69
A set of Groups.
Definition: group.h:48
struct GroupContext * next
Definition: group.h:51
#define FREE(x)
Definition: memory.h:46

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool empty_group ( struct Group g)
static

Is a Group empty?

Parameters
gGroup to test
Return values
trueIf the Group is empty

Definition at line 102 of file group.c.

103 {
104  if (!g)
105  return true;
106  return !g->as && STAILQ_EMPTY(&g->rs);
107 }
struct Address * as
Definition: group.h:40
struct RegexList rs
Definition: group.h:41
#define STAILQ_EMPTY(head)
Definition: queue.h:346

+ Here is the caller graph for this function:

void mutt_group_context_add ( struct GroupContext **  ctx,
struct Group group 
)

Add a Group to a List.

Parameters
ctxGroup List
groupGroup to add

Definition at line 114 of file group.c.

115 {
116  for (; *ctx; ctx = &((*ctx)->next))
117  {
118  if ((*ctx)->g == group)
119  return;
120  }
121 
122  *ctx = mutt_mem_calloc(1, sizeof(struct GroupContext));
123  (*ctx)->g = group;
124 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
A set of Groups.
Definition: group.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_group_context_destroy ( struct GroupContext **  ctx)

Destroy a Group List.

Parameters
ctxGroup List to destroy

Definition at line 130 of file group.c.

131 {
132  struct GroupContext *p = NULL;
133  for (; *ctx; *ctx = p)
134  {
135  p = (*ctx)->next;
136  FREE(ctx);
137  }
138 }
A set of Groups.
Definition: group.h:48
struct GroupContext * next
Definition: group.h:51
#define FREE(x)
Definition: memory.h:46

+ Here is the caller graph for this function:

static void group_add_addrlist ( struct Group g,
struct Address a 
)
static

Add an Address List to a Group.

Parameters
gGroup to add to
aAddress List

Definition at line 145 of file group.c.

146 {
147  struct Address **p = NULL, *q = NULL;
148 
149  if (!g)
150  return;
151  if (!a)
152  return;
153 
154  for (p = &g->as; *p; p = &((*p)->next))
155  ;
156 
157  q = mutt_addr_copy_list(a, false);
158  q = mutt_remove_xrefs(g->as, q);
159  *p = q;
160 }
An email address.
Definition: address.h:32
struct Address * mutt_remove_xrefs(struct Address *a, struct Address *b)
Remove cross-references.
Definition: send.c:157
struct Address * as
Definition: group.h:40
struct Address * mutt_addr_copy_list(struct Address *addr, bool prune)
Copy a list of addresses.
Definition: address.c:730

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int group_remove_addrlist ( struct Group g,
struct Address a 
)
static

Remove an Address List from a Group.

Parameters
gGroup to modify
aAddress List to remove
Return values
0Success
-1Error

Definition at line 169 of file group.c.

170 {
171  struct Address *p = NULL;
172 
173  if (!g)
174  return -1;
175  if (!a)
176  return -1;
177 
178  for (p = a; p; p = p->next)
180 
181  return 0;
182 }
int mutt_addr_remove_from_list(struct Address **a, const char *mailbox)
Remove an Address from a list.
Definition: address.c:413
An email address.
Definition: address.h:32
char * mailbox
mailbox and host address
Definition: address.h:35
struct Address * as
Definition: group.h:40
struct Address * next
Definition: address.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int group_add_regex ( struct Group g,
const char *  s,
int  flags,
struct Buffer err 
)
static

Add a Regex to a Group.

Parameters
gGroup to add to
sRegex string to add
flagsFlags, e.g. REG_ICASE
errBuffer for error message
Return values
0Success
-1Error

Definition at line 193 of file group.c.

194 {
195  return mutt_regexlist_add(&g->rs, s, flags, err);
196 }
int mutt_regexlist_add(struct RegexList *rl, const char *str, int flags, struct Buffer *err)
Compile a regex string and add it to a list.
Definition: regex.c:128
struct RegexList rs
Definition: group.h:41

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int group_remove_regex ( struct Group g,
const char *  s 
)
static

Remove a Regex from a Group.

Parameters
gGroup to modify
sRegex string to match
Return values
0Success
-1Error

Definition at line 205 of file group.c.

206 {
207  return mutt_regexlist_remove(&g->rs, s);
208 }
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:224
struct RegexList rs
Definition: group.h:41

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_group_context_add_addrlist ( struct GroupContext ctx,
struct Address a 
)

Add an Address List to a Group List.

Parameters
ctxGroup List to add to
aAddress List to add

Definition at line 215 of file group.c.

216 {
217  for (; ctx; ctx = ctx->next)
218  group_add_addrlist(ctx->g, a);
219 }
struct Group * g
Definition: group.h:50
static void group_add_addrlist(struct Group *g, struct Address *a)
Add an Address List to a Group.
Definition: group.c:145
struct GroupContext * next
Definition: group.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_group_context_remove_addrlist ( struct GroupContext ctx,
struct Address a 
)

Remove an Address List from a Group List.

Parameters
ctxGroup List to modify
aAddress List to remove
Return values
0Success
-1Error

Definition at line 228 of file group.c.

229 {
230  int rc = 0;
231 
232  for (; (!rc) && ctx; ctx = ctx->next)
233  {
234  rc = group_remove_addrlist(ctx->g, a);
235  if (empty_group(ctx->g))
236  group_remove(ctx->g);
237  }
238 
239  return rc;
240 }
struct Group * g
Definition: group.h:50
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:102
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:69
struct GroupContext * next
Definition: group.h:51
static int group_remove_addrlist(struct Group *g, struct Address *a)
Remove an Address List from a Group.
Definition: group.c:169

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_group_context_add_regex ( struct GroupContext ctx,
const char *  s,
int  flags,
struct Buffer err 
)

Add a Regex to a Group List.

Parameters
ctxGroup List to add to
sRegex string to add
flagsFlags, e.g. REG_ICASE
errBuffer for error message
Return values
0Success
-1Error

Definition at line 251 of file group.c.

253 {
254  int rc = 0;
255 
256  for (; (!rc) && ctx; ctx = ctx->next)
257  rc = group_add_regex(ctx->g, s, flags, err);
258 
259  return rc;
260 }
struct Group * g
Definition: group.h:50
static int group_add_regex(struct Group *g, const char *s, int flags, struct Buffer *err)
Add a Regex to a Group.
Definition: group.c:193
struct GroupContext * next
Definition: group.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_group_context_remove_regex ( struct GroupContext ctx,
const char *  s 
)

Remove a Regex from a Group List.

Parameters
ctxGroup List to modify
sRegex string to remove
Return values
0Success
-1Error

Definition at line 269 of file group.c.

270 {
271  int rc = 0;
272 
273  for (; (!rc) && ctx; ctx = ctx->next)
274  {
275  rc = group_remove_regex(ctx->g, s);
276  if (empty_group(ctx->g))
277  group_remove(ctx->g);
278  }
279 
280  return rc;
281 }
static int group_remove_regex(struct Group *g, const char *s)
Remove a Regex from a Group.
Definition: group.c:205
struct Group * g
Definition: group.h:50
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:102
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:69
struct GroupContext * next
Definition: group.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool mutt_group_match ( struct Group g,
const char *  s 
)

Does a string match an entry in a Group?

Parameters
gGroup to match against
sString to match
Return values
trueIf there's a match

Definition at line 289 of file group.c.

290 {
291  if (!g || !s)
292  return false;
293 
294  if (mutt_regexlist_match(&g->rs, s))
295  return true;
296  for (struct Address *ap = g->as; ap; ap = ap->next)
297  if (ap->mailbox && (mutt_str_strcasecmp(s, ap->mailbox) == 0))
298  return true;
299 
300  return false;
301 }
An email address.
Definition: address.h:32
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:187
struct Address * as
Definition: group.h:40
struct RegexList rs
Definition: group.h:41
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:625
struct Address * next
Definition: address.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function: