]> jfr.im git - irc/quakenet/newserv.git/blob - newsearch/ns-gline.c
4c2ba5e67ad3455899968c0a57c77f7818d37a66
[irc/quakenet/newserv.git] / newsearch / ns-gline.c
1 /*
2 * GLINE functionality
3 */
4
5 #include "newsearch.h"
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include "../control/control.h" /* controlreply() */
11 #include "../irc/irc.h" /* irc_send() */
12 #include "../lib/irc_string.h" /* IPtostr(), longtoduration(), durationtolong() */
13
14 /* used for *_free functions that need to warn users of certain things
15 i.e. hitting too many users in a (kill) or (gline) - declared in newsearch.c */
16 extern const struct nick *senderNSExtern;
17
18 void *gline_exe(struct searchNode *thenode, int type, void *theinput);
19 void gline_free(struct searchNode *thenode);
20
21 struct gline_localdata {
22 unsigned int marker;
23 unsigned int duration;
24 int count;
25 };
26
27 struct searchNode *gline_parse(int type, int argc, char **argv) {
28 struct gline_localdata *localdata;
29 struct searchNode *thenode;
30
31 if (!(localdata = (struct gline_localdata *) malloc(sizeof(struct gline_localdata)))) {
32 parseError = "malloc: could not allocate memory for this search.";
33 return NULL;
34 }
35 localdata->count = 0;
36 localdata->marker = nextnickmarker();
37
38 /* gline durations */
39 if (argc<1)
40 localdata->duration = NSGLINE_DURATION;
41 else {
42 localdata->duration = durationtolong(argv[0]);
43 /* error checking on gline duration */
44 if (localdata->duration == 0)
45 localdata->duration = NSGLINE_DURATION;
46 }
47
48 if (!(thenode=(struct searchNode *)malloc(sizeof (struct searchNode)))) {
49 /* couldn't malloc() memory for thenode, so free localdata to avoid leakage */
50 parseError = "malloc: could not allocate memory for this search.";
51 free(localdata);
52 return NULL;
53 }
54
55 thenode->returntype = RETURNTYPE_BOOL;
56 thenode->localdata = localdata;
57 thenode->exe = gline_exe;
58 thenode->free = gline_free;
59
60 return thenode;
61 }
62
63 void *gline_exe(struct searchNode *thenode, int type, void *theinput) {
64 struct gline_localdata *localdata;
65 nick *np = (nick *)theinput;
66
67 localdata = thenode->localdata;
68
69 np->marker = localdata->marker;
70 localdata->count++;
71
72 switch (type) {
73 case RETURNTYPE_INT:
74 case RETURNTYPE_BOOL:
75 return (void *)1;
76 case RETURNTYPE_STRING:
77 return "1";
78 }
79 return NULL;
80 }
81
82 void gline_free(struct searchNode *thenode) {
83 struct gline_localdata *localdata;
84 nick *np, *nnp;
85 int i, safe=0;
86
87 localdata = thenode->localdata;
88
89 if (localdata->count > NSMAX_GLINE_LIMIT) {
90 /* need to warn the user that they have just tried to twat half the network ... */
91 controlreply(senderNSExtern, "Warning: your pattern matches too many users (%d) - nothing done.", localdata->count);
92 free(localdata);
93 free(thenode);
94 return;
95 }
96
97 for (i=0;i<NICKHASHSIZE;i++) {
98 for (np=nicktable[i];np;np=nnp) {
99 nnp = np->next;
100 if (np->marker == localdata->marker) {
101 if (!IsOper(np) && !IsService(np) && !IsXOper(np)) {
102 if (np->ident[0] == '~')
103 irc_send("%s GL * +*@%s %u :You (%s!%s@%s) have been glined for violating our terms of service.",
104 mynumeric->content, IPtostr(np->ipaddress), localdata->duration, np->nick, np->ident, IPtostr(np->ipaddress));
105 else
106 irc_send("%s GL * +%s@%s %u :You (%s!%s@%s) have been glined for violating our terms of service.",
107 mynumeric->content, np->ident, IPtostr(np->ipaddress), localdata->duration, np->nick, np->ident, IPtostr(np->ipaddress));
108 }
109 else
110 safe++;
111 }
112 }
113 }
114 if (safe)
115 controlreply(senderNSExtern, "Warning: your pattern matched privileged users (%d in total) - these have not been touched.", safe);
116 /* notify opers of the action */
117 controlwall(NO_OPER, NL_GLINES, "%s/%s glined %d %s via nicksearch for %s [%d untouched].", senderNSExtern->nick, senderNSExtern->authname, (localdata->count - safe),
118 (localdata->count - safe) != 1 ? "users" : "user", longtoduration(localdata->duration, 1), safe);
119 free(localdata);
120 free(thenode);
121 }