1 /*
2  *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3  *
4  *  Copyright (c) 1998-2021 ircd-hybrid development team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19  *  USA
20  */
21 
22 /*! \file conf_gecos.c
23  * \brief Implements gecos {} block configuration management.
24  * \version $Id: conf_gecos.c 9858 2021-01-01 04:43:42Z michael $
25  */
26 
27 #include "stdinc.h"
28 #include "list.h"
29 #include "send.h"
30 #include "client.h"
31 #include "ircd.h"
32 #include "memory.h"
33 #include "conf.h"
34 #include "conf_gecos.h"
35 
36 
37 static dlink_list gecos_list;
38 
39 
40 const dlink_list *
gecos_get_list(void)41 gecos_get_list(void)
42 {
43   return &gecos_list;
44 }
45 
46 void
gecos_clear(void)47 gecos_clear(void)
48 {
49   dlink_node *node, *node_next;
50 
51   DLINK_FOREACH_SAFE(node, node_next, gecos_list.head)
52   {
53     struct GecosItem *gecos = node->data;
54 
55     if (gecos->in_database == false)
56       gecos_delete(gecos, false);
57   }
58 }
59 
60 void
gecos_delete(struct GecosItem * gecos,bool expired)61 gecos_delete(struct GecosItem *gecos, bool expired)
62 {
63   if (expired == true)
64     sendto_realops_flags(UMODE_EXPIRATION, L_ALL, SEND_NOTICE, "Temporary X-line for [%s] expired",
65                          gecos->mask);
66 
67   dlinkDelete(&gecos->node, &gecos_list);
68   xfree(gecos->mask);
69   xfree(gecos->reason);
70   xfree(gecos);
71 }
72 
73 struct GecosItem *
gecos_make(void)74 gecos_make(void)
75 {
76   struct GecosItem *gecos = xcalloc(sizeof(*gecos));
77   dlinkAdd(gecos, &gecos->node, &gecos_list);
78 
79   return gecos;
80 }
81 
82 struct GecosItem *
gecos_find(const char * name,int (* compare)(const char *,const char *))83 gecos_find(const char *name, int (*compare)(const char *, const char *))
84 {
85   dlink_node *node, *node_next;
86 
87   DLINK_FOREACH_SAFE(node, node_next, gecos_list.head)
88   {
89     struct GecosItem *gecos = node->data;
90 
91     if (gecos->expire &&
92         (gecos->expire <= event_base->time.sec_real))
93       gecos_delete(gecos, true);
94     else if (compare(gecos->mask, name) == 0)
95       return gecos;
96   }
97 
98   return NULL;
99 }
100 
101 void
gecos_expire(void)102 gecos_expire(void)
103 {
104   dlink_node *node, *node_next;
105 
106   DLINK_FOREACH_SAFE(node, node_next, gecos_list.head)
107   {
108     struct GecosItem *gecos = node->data;
109 
110     if (gecos->expire &&
111         (gecos->expire <= event_base->time.sec_real))
112       gecos_delete(gecos, true);
113   }
114 }
115