]> jfr.im git - solanum.git/blob - modules/m_testmask.c
m_cap: Remove CLEAR subcommand as per v3 specs
[solanum.git] / modules / m_testmask.c
1 /*
2 * m_testmask.c: Shows the number of matching local and global clients
3 * for a user@host mask
4 *
5 * Copyright (C) 2003 by W. Campbell
6 * Coypright (C) 2004 ircd-ratbox development team
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 /* List of ircd includes from ../include/ */
35 #include "stdinc.h"
36 #include "client.h"
37 #include "common.h"
38 #include "ircd.h"
39 #include "match.h"
40 #include "numeric.h"
41 #include "s_conf.h"
42 #include "logger.h"
43 #include "s_serv.h"
44 #include "send.h"
45 #include "msg.h"
46 #include "parse.h"
47 #include "modules.h"
48
49 static const char testmask_desc[] =
50 "Provides the TESTMASK command to show the number of clients matching a hostmask or GECOS";
51
52 static void mo_testmask(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
53 int parc, const char *parv[]);
54
55 struct Message testmask_msgtab = {
56 "TESTMASK", 0, 0, 0, 0,
57 {mg_unreg, mg_not_oper, mg_ignore, mg_ignore, mg_ignore, {mo_testmask, 2}}
58 };
59
60 mapi_clist_av1 testmask_clist[] = { &testmask_msgtab, NULL };
61 DECLARE_MODULE_AV2(testmask, NULL, NULL, testmask_clist, NULL, NULL, NULL, NULL, testmask_desc);
62
63 static const char *empty_sockhost = "255.255.255.255";
64 static const char *spoofed_sockhost = "0";
65
66 static void
67 mo_testmask(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
68 int parc, const char *parv[])
69 {
70 struct Client *target_p;
71 int lcount = 0;
72 int gcount = 0;
73 char *name, *username, *hostname;
74 const char *sockhost;
75 char *gecos = NULL;
76 rb_dlink_node *ptr;
77
78 name = LOCAL_COPY(parv[1]);
79 collapse(name);
80
81 /* username is required */
82 if((hostname = strchr(name, '@')) == NULL)
83 {
84 sendto_one_notice(source_p, ":Invalid parameters");
85 return;
86 }
87
88 *hostname++ = '\0';
89
90 /* nickname is optional */
91 if((username = strchr(name, '!')) == NULL)
92 {
93 username = name;
94 name = NULL;
95 }
96 else
97 *username++ = '\0';
98
99 if(EmptyString(username) || EmptyString(hostname))
100 {
101 sendto_one_notice(source_p, ":Invalid parameters");
102 return;
103 }
104
105 if(parc > 2 && !EmptyString(parv[2]))
106 {
107 gecos = LOCAL_COPY(parv[2]);
108 collapse_esc(gecos);
109 }
110
111 RB_DLINK_FOREACH(ptr, global_client_list.head)
112 {
113 target_p = ptr->data;
114
115 if(!IsPerson(target_p))
116 continue;
117
118 if(EmptyString(target_p->sockhost))
119 sockhost = empty_sockhost;
120 else if(!show_ip(source_p, target_p))
121 sockhost = spoofed_sockhost;
122 else
123 sockhost = target_p->sockhost;
124
125 if(match(username, target_p->username) &&
126 (match(hostname, target_p->host) ||
127 match(hostname, target_p->orighost) ||
128 match(hostname, sockhost) || match_ips(hostname, sockhost)))
129 {
130 if(name && !match(name, target_p->name))
131 continue;
132
133 if(gecos && !match_esc(gecos, target_p->info))
134 continue;
135
136 if(MyClient(target_p))
137 lcount++;
138 else
139 gcount++;
140 }
141 }
142
143 sendto_one(source_p, form_str(RPL_TESTMASKGECOS),
144 me.name, source_p->name,
145 lcount, gcount, name ? name : "*",
146 username, hostname, gecos ? gecos : "*");
147 }