]>
jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-gline.c
12 #include "../control/control.h"
13 #include "../irc/irc.h" /* irc_send() */
14 #include "../lib/irc_string.h" /* IPtostr(), longtoduration(), durationtolong() */
15 #include "../lib/strlfunc.h"
17 /* used for *_free functions that need to warn users of certain things
18 i.e. hitting too many users in a (kill) or (gline) - declared in newsearch.c */
19 extern nick
*senderNSExtern
;
20 static const char *defaultreason
= "You (%u) have been g-lined for violating our terms of service";
22 void *gline_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
);
23 void gline_free(searchCtx
*ctx
, struct searchNode
*thenode
);
25 struct gline_localdata
{
27 unsigned int duration
;
29 char reason
[NSMAX_REASON_LEN
];
32 struct searchNode
*gline_parse(searchCtx
*ctx
, int argc
, char **argv
) {
33 struct gline_localdata
*localdata
;
34 struct searchNode
*thenode
;
36 if (!(localdata
= (struct gline_localdata
*) malloc(sizeof(struct gline_localdata
)))) {
37 parseError
= "malloc: could not allocate memory for this search.";
41 if (ctx
->searchcmd
== reg_chansearch
)
42 localdata
->marker
= nextchanmarker();
43 else if (ctx
->searchcmd
== reg_nicksearch
)
44 localdata
->marker
= nextnickmarker();
47 parseError
= "gline: invalid search type";
51 /* default duration, default reason */
53 localdata
->duration
= NSGLINE_DURATION
;
54 strlcpy(localdata
->reason
, defaultreason
, sizeof(localdata
->reason
));
57 parseError
= "gline: invalid number of arguments";
60 char *argzerop
, *reasonp
, *durationp
;
61 struct searchNode
*durationsn
, *reasonsn
, *argzerosn
;
63 if (!(argzerosn
=argtoconststr("gline", ctx
, argv
[0], &argzerop
))) {
69 durationp
= reasonp
= NULL
;
70 durationsn
= reasonsn
= NULL
;
72 /* if we have a space it's a reason */
73 if(strchr(argzerop
, ' ')) {
77 durationsn
= argzerosn
;
81 durationsn
= argzerosn
;
84 if (!(reasonsn
=argtoconststr("gline", ctx
, argv
[1], &reasonp
))) {
85 durationsn
->free(ctx
, durationsn
);
92 strlcpy(localdata
->reason
, defaultreason
, sizeof(localdata
->reason
));
94 strlcpy(localdata
->reason
, reasonp
, sizeof(localdata
->reason
));
95 reasonsn
->free(ctx
, reasonsn
);
99 localdata
->duration
= NSGLINE_DURATION
;
101 localdata
->duration
= durationtolong(durationp
);
102 durationsn
->free(ctx
, durationsn
);
104 if (localdata
->duration
== 0) {
105 parseError
= "gline duration invalid.";
112 if (!(thenode
=(struct searchNode
*)malloc(sizeof (struct searchNode
)))) {
113 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
114 parseError
= "malloc: could not allocate memory for this search.";
119 thenode
->returntype
= RETURNTYPE_BOOL
;
120 thenode
->localdata
= localdata
;
121 thenode
->exe
= gline_exe
;
122 thenode
->free
= gline_free
;
127 void *gline_exe(searchCtx
*ctx
, struct searchNode
*thenode
, void *theinput
) {
128 struct gline_localdata
*localdata
;
132 localdata
= thenode
->localdata
;
134 if (ctx
->searchcmd
== reg_chansearch
) {
135 cip
= (chanindex
*)theinput
;
136 cip
->marker
= localdata
->marker
;
137 localdata
->count
+= cip
->channel
->users
->totalusers
;
140 np
= (nick
*)theinput
;
141 np
->marker
= localdata
->marker
;
148 static int glineuser(nick
*np
, struct gline_localdata
*localdata
, time_t ti
) {
150 if (!IsOper(np
) && !IsService(np
) && !IsXOper(np
)) {
151 nssnprintf(msgbuf
, sizeof(msgbuf
), localdata
->reason
, np
);
152 if (np
->host
->clonecount
<= NSMAX_GLINE_CLONES
)
153 irc_send("%s GL * +*@%s %u %jd :%s", mynumeric
->content
, IPtostr(np
->p_ipaddr
), localdata
->duration
, (intmax_t)ti
, msgbuf
);
155 irc_send("%s GL * +%s@%s %u %jd :%s", mynumeric
->content
, np
->ident
, IPtostr(np
->p_ipaddr
), localdata
->duration
, (intmax_t)ti
, msgbuf
);
162 void gline_free(searchCtx
*ctx
, struct searchNode
*thenode
) {
163 struct gline_localdata
*localdata
;
165 chanindex
*cip
, *ncip
;
167 time_t ti
= time(NULL
);
169 localdata
= thenode
->localdata
;
171 if (localdata
->count
> NSMAX_GLINE_LIMIT
) {
172 /* need to warn the user that they have just tried to twat half the network ... */
173 ctx
->reply(senderNSExtern
, "Warning: your pattern matches too many users (%d) - nothing done.", localdata
->count
);
179 if (ctx
->searchcmd
== reg_chansearch
) {
180 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
181 for (cip
=chantable
[i
];cip
;cip
=ncip
) {
183 if (cip
!= NULL
&& cip
->channel
!= NULL
&& cip
->marker
== localdata
->marker
) {
184 for (j
=0;j
<cip
->channel
->users
->hashsize
;j
++) {
185 if (cip
->channel
->users
->content
[j
]==nouser
)
188 if ((np
=getnickbynumeric(cip
->channel
->users
->content
[j
]))) {
189 if(!glineuser(np
, localdata
, ti
))
198 for (i
=0;i
<NICKHASHSIZE
;i
++) {
199 for (np
=nicktable
[i
];np
;np
=nnp
) {
201 if (np
->marker
== localdata
->marker
) {
202 if(!glineuser(np
, localdata
, ti
))
209 ctx
->reply(senderNSExtern
, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe
);
210 /* notify opers of the action */
211 ctx
->wall(NL_GLINES
, "%s/%s glined %d %s via %s for %s [%d untouched].", senderNSExtern
->nick
, senderNSExtern
->authname
, (localdata
->count
- safe
),
212 (localdata
->count
- safe
) != 1 ? "users" : "user", (ctx
->searchcmd
== reg_chansearch
) ? "chansearch" : "nicksearch", longtoduration(localdata
->duration
, 1), safe
);