5 FUTURE: natural (sort of) language parsing
8 PPA: if multiple users match the same user@host or *@host it'll send multiple glines?!
11 #include "regexgline.h"
12 #include "../lib/version.h"
16 typedef struct rg_glinenode
{
18 struct rg_struct
*reason
;
20 struct rg_glinenode
*next
;
23 typedef struct rg_glinelist
{
24 struct rg_glinenode
*start
;
25 struct rg_glinenode
*end
;
28 typedef struct rg_delay
{
31 struct rg_struct
*reason
;
33 struct rg_delay
*next
;
38 void rg_setdelay(nick
*np
, struct rg_struct
*reason
, short punish
);
39 void rg_deletedelay(rg_delay
*delay
);
40 void rg_dodelay(void *arg
);
42 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
);
45 sstring
*max_casualties
, *max_spew
, *expiry_time
, *max_per_gline
;
47 max_casualties
= getcopyconfigitem("regexgline", "maxcasualties", RGStringise(RG_MAX_CASUALTIES_DEFAULT
), 8);
48 if(!protectedatoi(max_casualties
->content
, &rg_max_casualties
))
49 rg_max_casualties
= RG_MAX_CASUALTIES_DEFAULT
;
51 freesstring(max_casualties
);
53 max_spew
= getcopyconfigitem("regexgline", "maxspew", RGStringise(RG_MAX_SPEW_DEFAULT
), 8);
54 if(!protectedatoi(max_spew
->content
, &rg_max_spew
))
55 rg_max_spew
= RG_MAX_SPEW_DEFAULT
;
57 freesstring(max_spew
);
59 expiry_time
= getcopyconfigitem("regexgline", "expirytime", RGStringise(RG_EXPIRY_TIME_DEFAULT
), 8);
60 if(!protectedatoi(expiry_time
->content
, &rg_expiry_time
))
61 rg_expiry_time
= RG_EXPIRY_TIME_DEFAULT
;
63 freesstring(expiry_time
);
65 max_per_gline
= getcopyconfigitem("regexgline", "maxpergline", RGStringise(RG_MAX_PER_GLINE_DEFAULT
), 8);
66 if(!protectedatoi(max_per_gline
->content
, &rg_max_per_gline
))
67 rg_max_per_gline
= RG_MAX_PER_GLINE_DEFAULT
;
69 freesstring(max_per_gline
);
76 registercontrolhelpcmd("regexgline", NO_OPER
, 4, &rg_gline
, "Usage: regexgline <text>\nAdds a new regular expression pattern.");
77 registercontrolhelpcmd("regexdelgline", NO_OPER
, 1, &rg_delgline
, "Usage: regexdelgline <pattern>\nDeletes a regular expression pattern.");
78 registercontrolhelpcmd("regexglist", NO_OPER
, 1, &rg_glist
, "Usage: regexglist <pattern>\nLists regular expression patterns.");
79 registercontrolhelpcmd("regexspew", NO_OPER
, 1, &rg_spew
, "Usage: regexspew <pattern>\nLists users currently on the network which match the given pattern.");
80 registercontrolhelpcmd("regexidlookup", NO_OPER
, 1, &rg_idlist
, "Usage: regexidlookup <id>\nFinds a regular expression pattern by it's ID number.");
82 registerhook(HOOK_NICK_NEWNICK
, &rg_nick
);
83 registerhook(HOOK_NICK_RENAME
, &rg_nick
);
84 registerhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
87 rg_schedule
= schedulerecurring(time(NULL
) + 1, 0, 1, rg_checkexpiry
, NULL
);
92 struct rg_struct
*gp
= rg_list
, *oldgp
;
93 rg_delay
*delay
, *delaynext
;
95 deregisterhook(HOOK_NICK_NEWNICK
, &rg_nick
);
96 deregisterhook(HOOK_NICK_RENAME
, &rg_nick
);
97 deregisterhook(HOOK_NICK_LOSTNICK
, &rg_lostnick
);
98 deregistercontrolcmd("regexspew", rg_spew
);
99 deregistercontrolcmd("regexglist", rg_glist
);
100 deregistercontrolcmd("regexdelgline", rg_delgline
);
101 deregistercontrolcmd("regexgline", rg_gline
);
102 deregistercontrolcmd("regexidlookup", rg_idlist
);
105 for(delay
=rg_delays
;delay
;delay
=delaynext
) {
106 delaynext
=delay
->next
;
107 deleteschedule(delay
->sch
, rg_dodelay
, delay
);
113 deleteschedule(rg_schedule
, &rg_checkexpiry
, NULL
);
117 for(gp
=rg_list
;gp
;) {
120 rg_freestruct(oldgp
);
127 void rg_checkexpiry(void *arg
) {
128 struct rg_struct
*rp
= rg_list
, *lp
= NULL
;
129 time_t current
= time(NULL
);
132 if (current
>= rp
->expires
) {
149 void rg_setdelay(nick
*np
, rg_struct
*reason
, short punish
) {
151 delay
= (rg_delay
*)malloc(sizeof(rg_delay
));
155 killuser(NULL
, np
, "%s (ID: %08lx)", reason
->reason
->content
, reason
->glineid
);
160 delay
->reason
= reason
;
161 delay
->punish
= punish
;
162 delay
->next
= rg_delays
;
165 delay
->sch
= scheduleoneshot(time(NULL
) + (RG_MINIMUM_DELAY_TIME
+ (rand() % RG_MAXIMUM_RAND_TIME
)), rg_dodelay
, delay
);
168 void rg_deletedelay(rg_delay
*delay
) {
169 rg_delay
*temp
, *prev
;
171 for (temp
=rg_delays
;temp
;temp
=temp
->next
) {
174 rg_delays
= temp
->next
;
176 prev
->next
= temp
->next
;
186 void rg_dodelay(void *arg
) {
187 rg_delay
*delay
= (rg_delay
*)arg
;
188 char hostname
[RG_MASKLEN
];
189 int hostlen
, usercount
= 0;
191 /* User or regex gline no longer exists */
192 if((!delay
->np
) || (!delay
->reason
)) {
193 rg_deletedelay(delay
);
197 hostlen
= RGBuildHostname(hostname
, delay
->np
);
199 /* User has wisely changed nicknames */
200 if(pcre_exec(delay
->reason
->regex
, delay
->reason
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) < 0) {
201 rg_deletedelay(delay
);
205 if (delay
->reason
->type
== 5) {
206 usercount
= delay
->np
->host
->clonecount
;
207 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(delay
->np
->p_ipaddr
));
210 if((delay
->reason
->type
== 4) || (usercount
> rg_max_per_gline
)) {
213 for(usercount
=0,tnp
=delay
->np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
214 if(!ircd_strcmp(delay
->np
->ident
, tnp
->ident
))
217 snprintf(hostname
, sizeof(hostname
), "%s@%s", delay
->np
->ident
, IPtostr(delay
->np
->p_ipaddr
));
220 if ((delay
->reason
->type
== 6) || (usercount
> rg_max_per_gline
)) {
221 if (IsAccount(delay
->np
)) {
222 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched delayed kill regex %08lx", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->np
->authname
, delay
->reason
->glineid
);
224 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched delayed kill regex %08lx", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->reason
->glineid
);
226 killuser(NULL
, delay
->np
, "%s (ID: %08lx)", delay
->reason
->reason
->content
, delay
->reason
->glineid
);
230 if ((delay
->reason
->type
<= 3) || (delay
->reason
->type
>= 6))
233 if (delay
->reason
->type
== 4) {
234 if (IsAccount(delay
->np
)) {
235 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched delayed user@host gline regex %08lx (hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->np
->authname
, delay
->reason
->glineid
, usercount
, (usercount
!=1)?"s":"");
237 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched delayed user@host gline regex %08lx (hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->reason
->glineid
, usercount
, (usercount
!=1)?"s":"");
240 if (IsAccount(delay
->np
)) {
241 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched delayed *@host gline regex %08lx (hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->np
->authname
, delay
->reason
->glineid
, usercount
, (usercount
!=1)?"s":"");
243 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched delayed *@host gline regex %08lx (hit %d user%s)", delay
->np
->nick
, delay
->np
->ident
, delay
->np
->host
->name
->content
, delay
->reason
->glineid
, usercount
, (usercount
!=1)?"s":"");
247 irc_send("%s GL * +%s %d :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, hostname
, rg_expiry_time
, delay
->reason
->reason
->content
, delay
->reason
->glineid
);
248 rg_deletedelay(delay
);
251 void rg_initglinelist(struct rg_glinelist
*gll
) {
256 void rg_flushglines(struct rg_glinelist
*gll
) {
257 struct rg_glinenode
*nn
, *pn
;
258 for(nn
=gll
->start
;nn
;nn
=pn
) {
260 if(nn
->punish
== 3) {
261 if ( IsAccount(nn
->np
) ) {
262 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched kill regex %08lx", nn
->np
->nick
, nn
->np
->ident
, nn
->np
->host
->name
->content
, nn
->np
->authname
, nn
->reason
->glineid
);
264 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched kill regex %08lx", nn
->np
->nick
, nn
->np
->ident
, nn
->np
->host
->name
->content
, nn
->reason
->glineid
);
267 killuser(NULL
, nn
->np
, "%s (ID: %08lx)", nn
->reason
->reason
->content
, nn
->reason
->glineid
);
268 } else if ((nn
->punish
== 4) || (nn
->punish
== 5) || (nn
->punish
== 6)) {
269 rg_setdelay(nn
->np
, nn
->reason
, nn
->punish
);
274 rg_initglinelist(gll
);
277 int rg_dbconnect(void) {
278 sstring
*dbhost
, *dbusername
, *dbpassword
, *dbdatabase
, *dbport
;
280 dbhost
= getcopyconfigitem("regexgline", "dbhost", "localhost", HOSTLEN
);
281 dbusername
= getcopyconfigitem("regexgline", "dbusername", "regexgline", 20);
282 dbpassword
= getcopyconfigitem("regexgline", "dbpassword", "moo", 20);
283 dbdatabase
= getcopyconfigitem("regexgline", "dbdatabase", "regexgline", 20);
284 dbport
= getcopyconfigitem("regexgline", "dbport", "3306", 8);
286 if(rg_sqlconnect(dbhost
->content
, dbusername
->content
, dbpassword
->content
, dbdatabase
->content
, strtol(dbport
->content
, NULL
, 10))) {
287 Error("regexgline", ERR_FATAL
, "Cannot connect to database host!");
288 return 1; /* PPA: splidge: do something here 8]! */
294 freesstring(dbusername
);
295 freesstring(dbpassword
);
296 freesstring(dbdatabase
);
302 int rg_dbload(void) {
303 rg_sqlquery("CREATE TABLE regexglines (id INT(10) PRIMARY KEY AUTO_INCREMENT, gline TEXT NOT NULL, setby VARCHAR(%d) NOT NULL, reason VARCHAR(%d) NOT NULL, expires BIGINT NOT NULL, type TINYINT(4) NOT NULL DEFAULT 1)", ACCOUNTLEN
, RG_REASON_MAX
);
304 rg_sqlquery("CREATE TABLE regexlogs (id INT(10) PRIMARY KEY AUTO_INCREMENT, host VARCHAR(%d) NOT NULL, account VARCHAR(%d) NOT NULL, event TEXT NOT NULL, arg TEXT NOT NULL, ts TIMESTAMP)", RG_MASKLEN
- 1, ACCOUNTLEN
);
305 rg_sqlquery("CREATE TABLE regexglinelog (id INT(10) PRIMARY KEY AUTO_INCREMENT, glineid INT(10) NOT NULL, ts TIMESTAMP, nickname VARCHAR(%d) NOT NULL, username VARCHAR(%d) NOT NULL, hostname VARCHAR(%d) NOT NULL, realname VARCHAR(%d))", NICKLEN
, USERLEN
, HOSTLEN
, REALLEN
);
307 if(!rg_sqlquery("SELECT id, gline, setby, reason, expires, type FROM regexglines")) {
309 if((res
= rg_sqlstoreresult())) {
311 while((row
= rg_sqlgetrow(res
))) {
312 if (!rg_newsstruct(row
[0], row
[1], row
[2], row
[3], row
[4], row
[5], 0, 0))
313 rg_sqlquery("DELETE FROM regexglines WHERE id = %s", row
[0]);
322 void rg_nick(int hooknum
, void *arg
) {
323 nick
*np
= (nick
*)arg
;
324 struct rg_struct
*rp
;
325 char hostname
[RG_MASKLEN
];
327 struct rg_glinelist gll
;
329 rg_initglinelist(&gll
);
331 hostlen
= RGBuildHostname(hostname
, np
);
333 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
334 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
335 rg_dogline(&gll
, np
, rp
, hostname
);
340 rg_flushglines(&gll
);
343 void rg_lostnick(int hooknum
, void *arg
) {
344 nick
*np
= (nick
*)arg
;
347 /* Cleanup the delays */
348 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
353 int rg_gline(void *source
, int cargc
, char **cargv
) {
354 nick
*np
= (nick
*)source
, *tnp
;
356 const char *expirybuf
;
357 int expiry
, count
, j
, hostlen
;
358 struct rg_struct
*rp
;
359 struct rg_glinelist gll
;
361 char eemask
[RG_QUERY_BUF_SIZE
], eesetby
[RG_QUERY_BUF_SIZE
], eereason
[RG_QUERY_BUF_SIZE
];
362 char hostname
[RG_MASKLEN
];
367 if ((strlen(cargv
[2]) != 1) || ((cargv
[2][0] != '1') && (cargv
[2][0] != '2') && (cargv
[2][0] != '3') && (cargv
[2][0] != '4') && (cargv
[2][0] != '5') && (cargv
[2][0] != '6'))) {
368 controlreply(np
, "Invalid type specified!");
372 if (!(expiry
= durationtolong(cargv
[1]))) {
373 controlreply(np
, "Invalid duration specified!");
377 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
378 if (RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
379 controlreply(np
, "That regexp gline already exists!");
384 if (rg_sanitycheck(cargv
[0], &count
)) {
385 controlreply(np
, "Error in expression.");
387 } else if (count
< 0) {
388 controlreply(np
, "That expression would hit too many users (%d)!", -count
);
392 realexpiry
= expiry
+ time(NULL
);
394 rg_sqlescape_string(eemask
, cargv
[0], strlen(cargv
[0]));
395 rg_sqlescape_string(eesetby
, np
->nick
, strlen(np
->nick
));
396 rg_sqlescape_string(eereason
, cargv
[3], strlen(cargv
[3]));
398 rg_sqlquery("INSERT INTO regexglines (gline, setby, reason, expires, type) VALUES ('%s', '%s', '%s', %d, %s)", eemask
, eesetby
, eereason
, realexpiry
, cargv
[2]);
399 if (!rg_sqlquery("SELECT id FROM regexglines WHERE gline = '%s' AND setby = '%s' AND reason = '%s' AND expires = %d AND type = %s ORDER BY ID DESC LIMIT 1", eemask
, eesetby
, eereason
, realexpiry
, cargv
[2])) {
401 if((res
= rg_sqlstoreresult())) {
403 row
= rg_sqlgetrow(res
);
405 rp
= rg_newsstruct(row
[0], cargv
[0], np
->nick
, cargv
[3], "", cargv
[2], realexpiry
, 0);
408 rg_sqlquery("DELETE FROM regexglines WHERE gline = '%s' AND setby = '%s' AND reason = '%s' AND expires = %d AND type = %s ORDER BY ID DESC LIMIT 1", eemask
, eesetby
, eereason
, realexpiry
, cargv
[2]);
409 controlreply(np
, "Error allocating - regexgline NOT ADDED.");
414 rg_sqlquery("DELETE FROM regexglines WHERE gline = '%s' AND setby = '%s' AND reason = '%s' AND expires = %d AND type = %s ORDER BY ID DESC LIMIT 1", eemask
, eesetby
, eereason
, realexpiry
, cargv
[2]);
415 controlreply(np
, "Error selecting ID from database - regexgline NOT ADDED.");
419 rg_sqlquery("DELETE FROM regexglines WHERE gline = '%s' AND setby = '%s' AND reason = '%s' AND expires = %d AND type = %s ORDER BY ID DESC LIMIT 1", eemask
, eesetby
, eereason
, realexpiry
, cargv
[2]);
420 controlreply(np
, "Error fetching ID from database - regexgline NOT ADDED.");
424 rg_sqlquery("DELETE FROM regexglines WHERE gline = '%s' AND setby = '%s' AND reason = '%s' AND expires = %d AND type = %s ORDER BY ID DESC LIMIT 1", eemask
, eesetby
, eereason
, realexpiry
, cargv
[2]);
425 controlreply(np
, "Error executing query - regexgline NOT ADDED.");
429 rg_initglinelist(&gll
);
431 for(j
=0;j
<NICKHASHSIZE
;j
++) {
432 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
433 hostlen
= RGBuildHostname(hostname
, tnp
);
434 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0)
435 rg_dogline(&gll
, tnp
, rp
, hostname
);
439 rg_flushglines(&gll
);
441 expirybuf
= longtoduration(expiry
, 0);
443 rg_logevent(np
, "regexgline", "%s %d %d %s", cargv
[0], expiry
, count
, cargv
[3]);
444 controlreply(np
, "Added regexgline: %s (expires in: %s, hit %d user%s): %s", cargv
[0], expirybuf
, count
, (count
!=1)?"s":"", cargv
[3]);
445 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
446 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s added regexgline: %s (expires in: %s, hit %d user%s): %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0], expirybuf
, count
, (count
!=1)?"s":"", cargv
[3]);
451 int rg_sanitycheck(char *mask
, int *count
) {
453 char hostname
[RG_MASKLEN
];
454 int erroroffset
, hostlen
, j
, masklen
= strlen(mask
);
459 if((masklen
< RG_MIN_MASK_LEN
) || (masklen
> RG_REGEXGLINE_MAX
))
462 if(!(regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
463 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
466 hint
= pcre_study(regex
, 0, &error
);
468 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
475 for(j
=0;j
<NICKHASHSIZE
;j
++) {
476 for(np
=nicktable
[j
];np
;np
=np
->next
) {
477 hostlen
= RGBuildHostname(hostname
, np
);
478 if(pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
488 if(*count
>= rg_max_casualties
)
494 int rg_delgline(void *source
, int cargc
, char **cargv
) {
495 nick
*np
= (nick
*)source
;
497 struct rg_struct
*rp
= rg_list
, *last
= NULL
;
503 rg_logevent(np
, "regexdelgline", "%s", cargv
[0]);
505 if(RGMasksEqual(rp
->mask
->content
, cargv
[0])) {
508 /* Cleanup the delays */
509 for(delay
=rg_delays
;delay
;delay
=delay
->next
)
510 if(delay
->reason
==rp
)
511 delay
->reason
= NULL
;
513 rg_sqlquery("DELETE FROM regexglines WHERE id = %d", rp
->id
);
515 last
->next
= rp
->next
;
529 controlreply(np
, "Deleted (matched: %d).", count
);
530 /* If we are using NO, can we safely assume the user is authed here and use ->authname? */
531 controlwall(NO_OPER
, NL_GLINES
, "%s!%s@%s/%s removed regexgline: %s", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, cargv
[0]);
533 controlreply(np
, "No glines matched: %s", cargv
[0]);
538 int rg_idlist(void *source
, int cargc
, char **cargv
) {
539 nick
*np
= (nick
*)source
;
543 } else if (strlen(cargv
[0]) != 8) {
544 controlreply(np
, "Invalid gline id!");
547 struct rg_struct
*rp
;
548 unsigned long id
= 0;
552 if(0xff == rc_hexlookup
[(int)cargv
[0][i
]]) {
553 controlreply(np
, "Invalid gline id!");
556 id
= (id
<< 4) | rc_hexlookup
[(int)cargv
[0][i
]];
560 controlreply(np
, "Mask Expires Set by Type Reason");
561 for(rp
=rg_list
;rp
;rp
=rp
->next
)
562 if(id
== rp
->glineid
)
563 rg_displaygline(np
, rp
);
564 controlreply(np
, "Done.");
570 int rg_glist(void *source
, int cargc
, char **cargv
) {
571 nick
*np
= (nick
*)source
;
572 struct rg_struct
*rp
;
580 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
581 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
584 hint
= pcre_study(regex
, 0, &error
);
586 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
592 rg_logevent(np
, "regexglist", "%s", cargv
[0]);
593 controlreply(np
, "Mask Expires Set by Type Reason");
594 for(rp
=rg_list
;rp
;rp
=rp
->next
)
595 if(pcre_exec(regex
, hint
, rp
->mask
->content
, rp
->mask
->length
, 0, 0, NULL
, 0) >= 0)
596 rg_displaygline(np
, rp
);
603 rg_logevent(np
, "regexglist", "");
604 controlreply(np
, "Mask Expires Set by Type Reason");
605 for(rp
=rg_list
;rp
;rp
=rp
->next
)
606 rg_displaygline(np
, rp
);
609 controlreply(np
, "Done.");
613 void rg_displaygline(nick
*np
, struct rg_struct
*rp
) { /* could be a macro? I'll assume the C compiler inlines it */
614 controlreply(np
, "%-25s %-20s %-15s %-4d %s", rp
->mask
->content
, longtoduration(rp
->expires
- time(NULL
), 0), rp
->setby
->content
, rp
->type
, rp
->reason
->content
);
617 int rg_spew(void *source
, int cargc
, char **cargv
) {
618 nick
*np
= (nick
*)source
, *tnp
;
619 int counter
= 0, erroroffset
, hostlen
, j
;
623 char hostname
[RG_MASKLEN
];
630 if(!(regex
= pcre_compile(cargv
[0], RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
631 controlreply(np
, "Error compiling expression %s at offset %d: %s", cargv
[0], erroroffset
, error
);
634 hint
= pcre_study(regex
, 0, &error
);
636 controlreply(np
, "Error studying expression %s: %s", cargv
[0], error
);
642 rg_logevent(np
, "regexspew", "%s", cargv
[0]);
644 for(j
=0;j
<NICKHASHSIZE
;j
++) {
645 for(tnp
=nicktable
[j
];tnp
;tnp
=tnp
->next
) {
646 hostlen
= RGBuildHostname(hostname
, tnp
);
647 pcreret
= pcre_exec(regex
, hint
, hostname
, hostlen
, 0, 0, ovector
, sizeof(ovector
) / sizeof(int));
649 if(counter
== rg_max_spew
) {
650 controlreply(np
, "Reached maximum spew count (%d) - aborting display.", rg_max_spew
);
651 } else if (counter
< rg_max_spew
) {
652 /* 15 should be number of bolds */
653 char boldbuf
[RG_MASKLEN
+ 15], *tp
, *fp
, *realname
= NULL
;
655 for(tp
=hostname
,fp
=boldbuf
;*tp
;) {
656 if(tp
- hostname
== ovector
[0]) {
660 if(tp
- hostname
== ovector
[1]) {
679 controlreply(np
, "%s (%s) (%dc)", boldbuf
, realname
, tnp
->channels
->cursi
);
685 controlreply(np
, "Done - %d matches.", counter
);
694 int rg_sqlconnect(char *dbhost
, char *dbuser
, char *dbpass
, char *db
, unsigned int port
) {
696 if(!mysql_real_connect(&rg_sql
, dbhost
, dbuser
, dbpass
, db
, port
, NULL
, 0))
701 void rg_sqldisconnect(void) {
702 mysql_close(&rg_sql
);
705 void rg_sqlescape_string(char *dest
, char *source
, size_t length
) {
706 if(length
>= RG_QUERY_BUF_SIZE
)
707 length
= RG_QUERY_BUF_SIZE
- 1;
709 mysql_escape_string(dest
, source
, length
);
712 int rg_sqlquery(char *format
, ...) {
713 char rg_sqlquery
[RG_QUERY_BUF_SIZE
];
716 va_start(va
, format
);
717 vsnprintf(rg_sqlquery
, sizeof(rg_sqlquery
), format
, va
);
720 return mysql_query(&rg_sql
, rg_sqlquery
);
723 rg_sqlresult
rg_sqlstoreresult(void) {
724 return mysql_store_result(&rg_sql
);
727 rg_sqlrow
rg_sqlgetrow(rg_sqlresult res
) {
728 return mysql_fetch_row(res
);
731 void rg_sqlfree(rg_sqlresult res
) {
732 mysql_free_result(res
);
735 void rg_startup(void) {
738 struct rg_struct
*rp
;
739 struct rg_glinelist gll
;
740 char hostname
[RG_MASKLEN
];
742 rg_initglinelist(&gll
);
744 for(j
=0;j
<NICKHASHSIZE
;j
++) {
745 for(np
=nicktable
[j
];np
;np
=np
->next
) {
746 hostlen
= RGBuildHostname(hostname
, np
);
747 for(rp
=rg_list
;rp
;rp
=rp
->next
) {
748 if(pcre_exec(rp
->regex
, rp
->hint
, hostname
, hostlen
, 0, 0, NULL
, 0) >= 0) {
749 rg_dogline(&gll
, np
, rp
, hostname
);
756 rg_flushglines(&gll
);
759 void rg_freestruct(struct rg_struct
*rp
) {
760 freesstring(rp
->mask
);
761 freesstring(rp
->setby
);
762 freesstring(rp
->reason
);
763 pcre_free(rp
->regex
);
769 struct rg_struct
*rg_newstruct(time_t expires
) {
770 struct rg_struct
*rp
;
772 if (time(NULL
) >= expires
)
775 rp
= (struct rg_struct
*)malloc(sizeof(struct rg_struct
));
777 struct rg_struct
*tp
, *lp
;
782 rp
->expires
= expires
;
787 for(lp
=NULL
,tp
=rg_list
;tp
;lp
=tp
,tp
=tp
->next
) {
788 if (expires
<= tp
->expires
) { /* <= possible, slight speed increase */
811 struct rg_struct
*rg_newsstruct(char *id
, char *mask
, char *setby
, char *reason
, char *expires
, char *type
, time_t iexpires
, int iid
) {
812 struct rg_struct
*newrow
, *lp
, *cp
;
814 char glineiddata
[1024];
817 if(!protectedatoi(expires
, &qexpires
))
819 rexpires
= (time_t)qexpires
;
824 newrow
= rg_newstruct(rexpires
);
830 if(!(newrow
->regex
= pcre_compile(mask
, RG_PCREFLAGS
, &error
, &erroroffset
, NULL
))) {
831 Error("regexgline", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", mask
, erroroffset
, error
);
834 newrow
->hint
= pcre_study(newrow
->regex
, 0, &error
);
836 Error("regexgline", ERR_WARNING
, "Error studying expression %s: %s", mask
, error
);
837 pcre_free(newrow
->regex
);
843 if(!protectedatoi(id
, &newrow
->id
))
849 newrow
->mask
= getsstring(mask
, RG_REGEXGLINE_MAX
);
851 Error("regexgline", ERR_WARNING
, "Error allocating memory for mask!");
855 newrow
->setby
= getsstring(setby
, ACCOUNTLEN
);
857 Error("regexgline", ERR_WARNING
, "Error allocating memory for setby!");
861 newrow
->reason
= getsstring(reason
, RG_REASON_MAX
);
862 if(!newrow
->reason
) {
863 Error("regexgline", ERR_WARNING
, "Error allocating memory for reason!");
867 if(!protectedatoi(type
, &newrow
->type
))
868 newrow
->type
= 0; /* just in case */
870 snprintf(glineiddata
, sizeof(glineiddata
), "%s regexgline %s %s %s %d %d", mynumeric
->content
, mask
, setby
, reason
, (int)iexpires
, newrow
->type
);
871 newrow
->glineid
= crc32(glineiddata
);
878 freesstring(newrow
->mask
);
880 freesstring(newrow
->setby
);
882 freesstring(newrow
->reason
);
883 pcre_free(newrow
->regex
);
885 pcre_free(newrow
->hint
);
888 for(lp
=NULL
,cp
=rg_list
;cp
;lp
=cp
,cp
=cp
->next
) {
902 void rg_dogline(struct rg_glinelist
*gll
, nick
*np
, struct rg_struct
*rp
, char *matched
) { /* PPA: if multiple users match the same user@host or *@host it'll send multiple glines?! */
903 char hostname
[RG_MASKLEN
];
909 usercount
= np
->host
->clonecount
;
910 snprintf(hostname
, sizeof(hostname
), "*@%s", IPtostr(np
->p_ipaddr
));
913 if ((rp
->type
== 1) || (usercount
> rg_max_per_gline
)) {
916 for(usercount
=0,tnp
=np
->host
->nicks
;tnp
;tnp
=tnp
->nextbyhost
)
917 if(!ircd_strcmp(np
->ident
, tnp
->ident
))
920 snprintf(hostname
, sizeof(hostname
), "%s@%s", np
->ident
, IPtostr(np
->p_ipaddr
));
923 if ((rp
->type
>= 3) || (usercount
> rg_max_per_gline
)) {
924 struct rg_glinenode
*nn
= (struct rg_glinenode
*)malloc(sizeof(struct rg_glinenode
));
940 nn
->punish
= rp
->type
;
946 if ((rp
->type
<= 0) || (rp
->type
>= 3))
951 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched user@host gline regex %08lx (hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, rp
->glineid
, usercount
, (usercount
!=1)?"s":"");
953 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched user@host gline regex %08lx (hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, rp
->glineid
, usercount
, (usercount
!=1)?"s":"");
957 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s/%s matched *@host gline regex %08lx (hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, np
->authname
, rp
->glineid
, usercount
, (usercount
!=1)?"s":"");
959 controlwall(NO_OPER
, NL_HITS
, "%s!%s@%s matched *@host gline regex %08lx (hit %d user%s)", np
->nick
, np
->ident
, np
->host
->name
->content
, rp
->glineid
, usercount
, (usercount
!=1)?"s":"");
963 irc_send("%s GL * +%s %d :AUTO: %s (ID: %08lx)\r\n", mynumeric
->content
, hostname
, rg_expiry_time
, rp
->reason
->content
, rp
->glineid
);
966 void rg_logevent(nick
*np
, char *event
, char *details
, ...) {
967 char eeevent
[RG_QUERY_BUF_SIZE
], eedetails
[RG_QUERY_BUF_SIZE
], eemask
[RG_QUERY_BUF_SIZE
], eeaccount
[RG_QUERY_BUF_SIZE
];
968 char buf
[513], account
[ACCOUNTLEN
+ 1], mask
[RG_MASKLEN
];
974 va_start(va
, details
);
975 vsnprintf(buf
, sizeof(buf
), details
, va
);
980 strncpy(account
, np
->authname
, sizeof(account
) - 1);
981 account
[sizeof(account
) - 1] = '\0';
985 masklen
= RGBuildHostname(mask
, np
);
991 rg_sqlescape_string(eeevent
, event
, strlen(event
));
992 rg_sqlescape_string(eedetails
, buf
, strlen(buf
));
993 rg_sqlescape_string(eeaccount
, event
, strlen(account
));
994 rg_sqlescape_string(eemask
, mask
, masklen
);
996 rg_sqlquery("INSERT INTO regexlogs (host, account, event, arg) VALUES ('%s', '%s', '%s', '%s')", eemask
, eeaccount
, eeevent
, eedetails
);
999 void rg_loggline(struct rg_struct
*rg
, nick
*np
) {
1000 char eenick
[RG_QUERY_BUF_SIZE
], eeuser
[RG_QUERY_BUF_SIZE
], eehost
[RG_QUERY_BUF_SIZE
], eereal
[RG_QUERY_BUF_SIZE
];
1003 rg_sqlescape_string(eenick
, np
->nick
, strlen(np
->nick
));
1004 rg_sqlescape_string(eeuser
, np
->ident
, strlen(np
->ident
));
1005 rg_sqlescape_string(eehost
, np
->host
->name
->content
, strlen(np
->host
->name
->content
));
1006 rg_sqlescape_string(eereal
, np
->realname
->name
->content
, strlen(np
->realname
->name
->content
));
1008 rg_sqlquery("INSERT INTO regexglinelog (glineid, nickname, username, hostname, realname) VALUES (%d, '%s', '%s', '%s', '%s')", rg
->id
, eenick
, eeuser
, eehost
, eereal
);