@@ -69,6 +69,8 @@ def initialize()
69
69
[ "STATS" , 0 , 0 , SFLG_NOMAXPARAM , ADMIN_FLAG , lm ( 'GS_HLP_STS_SHORT' ) , lm ( 'GS_HLP_STS_LONG' ) ] ,
70
70
[ "ENFORCE" , 0 , 0 , SFLG_NOMAXPARAM , ADMIN_FLAG , lm ( 'GS_HLP_ENF_SHORT' ) , lm ( 'GS_HLP_ENF_LONG' ) ] ,
71
71
[ "BADSERV" , 0 , 1 , SFLG_NOMAXPARAM , ADMIN_FLAG , lm ( 'GS_HLP_SRV_SHORT' ) , lm ( 'GS_HLP_SRV_LONG' ) ] ,
72
+ [ "PROTECT" , 1 , 2 , SFLG_NOMAXPARAM , ADMIN_FLAG , lm ( 'GS_HLP_PRT_SHORT' ) , lm ( 'GS_HLP_PRT_LONG' ) ] ,
73
+ [ "UNPROTECT" , 1 , 2 , SFLG_NOMAXPARAM , ADMIN_FLAG , lm ( 'GS_HLP_UPR_SHORT' ) , lm ( 'GS_HLP_UPR_LONG' ) ] ,
72
74
] ) # register
73
75
74
76
# Which hooks do we want?
@@ -100,6 +102,13 @@ def initialize()
100
102
irc_lower($1)' )
101
103
@dbq [ 'INCREASE_KILLS' ] = DB . prepare ( 'UPDATE ganneffserv SET kills = kills+1
102
104
WHERE irc_lower(channel) = irc_lower($1)' )
105
+ @dbq [ 'INSERT_PROTECT' ] = DB . prepare ( 'INSERT INTO ganneffprotect(setter, time,
106
+ pattern, reason) VALUES($1, $2, $3, $4)' )
107
+ @dbq [ 'DELETE_PROTECT' ] = DB . prepare ( 'DELETE FROM ganneffprotect WHERE
108
+ irc_lower(pattern) = irc_lower($1)' )
109
+ @dbq [ 'GET_PROTECTED_PATTERNS' ] = DB . prepare ( 'SELECT pattern, reason FROM ganneffprotect' )
110
+ @dbq [ 'GET_PROTECTED_PATTERNS_DETAILED' ] = DB . prepare ( 'SELECT pattern, setter, time,
111
+ reason FROM ganneffprotect' )
103
112
end # def initialize
104
113
105
114
########################################################################
@@ -221,6 +230,48 @@ def DEL(client, parv = [])
221
230
true
222
231
end # def DEL
223
232
233
+ # ------------------------------------------------------------------------
234
+
235
+ # Protect users from collatoral damage
236
+ def PROTECT ( client , parv = [ ] )
237
+ parv [ 1 ] . downcase!
238
+ debug ( LOG_DEBUG , "#{ client . name } called PROTECT and the params are #{ parv . join ( "," ) } " )
239
+
240
+ requested_pattern = parv [ 1 ]
241
+ pattern = irc_pattern_to_regex ( requested_pattern )
242
+ reason = parv [ 2 ]
243
+
244
+ ret = DB . execute_nonquery ( @dbq [ 'INSERT_PROTECT' ] , 'iiss' , client . nick . account_id ,
245
+ Time . now . to_i , pattern , reason )
246
+ if ret then
247
+ debug ( LOG_NOTICE , "#{ client . name } added protection #{ pattern } , reason #{ reason } " )
248
+ @protection [ pattern ] = reason
249
+ load_protected_patterns
250
+ reply ( client , "Protection #{ requested_pattern } successfully added" )
251
+ else
252
+ reply ( client , "Failed to add #{ requested_pattern } " )
253
+ end
254
+
255
+ # ------------------------------------------------------------------------
256
+
257
+ # Unprotect users from collatoral damage
258
+ def UNPROTECT ( client , parv = [ ] )
259
+ parv [ 1 ] . downcase!
260
+ debug ( LOG_DEBUG , "#{ client . name } called UNPROTECT and the params are #{ parv . join ( "," ) } " )
261
+
262
+ pattern = irc_pattern_to_regex ( parv [ 1 ] )
263
+ return unless @protection . has_key? ( pattern )
264
+
265
+ ret = DB . execute_nonquery ( @dbq [ 'DELETE_PROTECT' ] , 's' , pattern )
266
+ if ret then
267
+ debug ( LOG_NOTICE , "#{ client . name } removed protection #{ pattern } " )
268
+ @protection . delete ( pattern )
269
+ load_protected_patterns
270
+ reply ( client , "Protection #{ pattern } successfully deleted." )
271
+ else
272
+ reply ( client , "Failed to delete protection #{ pattern } ." )
273
+ end
274
+
224
275
# ------------------------------------------------------------------------
225
276
226
277
# List all channels we monitor
@@ -245,6 +296,18 @@ def LIST(client, parv = [])
245
296
}
246
297
result . free
247
298
299
+ reply ( client , "Protected host patterns\n \n " )
300
+ reply ( client , "%-50s %-10s %-19s %s" % [ "Pattern" , "By" , "When" , "Reason" ] )
301
+ result = DB . execute ( @dbq [ 'GET_PROTECTED_PATTERNS_DETAILED' ] )
302
+ result . row_each { |row |
303
+ pattern = row [ 0 ]
304
+ by = row [ 1 ]
305
+ time = Time . at ( row [ 2 ] . to_i ) . strftime ( '%Y-%m-%d %H:%M:%S' )
306
+ reason = row [ 3 ]
307
+ reply ( client , "%-50s %-10s %-19s %s" % [ pattern , by , time , reason ] )
308
+ }
309
+ result . free
310
+
248
311
reply ( client , "\n CRFJ - checks Connect, Register nick, Join channel within 15 seconds (i.e. Fast)" )
249
312
reply ( client , "J - triggers on every Join" )
250
313
@@ -574,8 +637,13 @@ def akill(client, reason, operreason, channel="")
574
637
ret = kill_user ( client , reason )
575
638
else # if host
576
639
reason = "#{ reason } |#{ operreason } "
577
- debug ( LOG_DEBUG , "Issuing AKILL: *@#{ host } , #{ reason } lasting for #{ @akill_duration } seconds" )
578
- ret = akill_add ( "*@#{ host } " , reason , @akill_duration )
640
+ if client . host =~ /#{ @protected_patterns } /i # if protected hosts
641
+ debug ( LOG_DEBUG , "Using /kill instead of AKILL for protected user #{ client . name } " )
642
+ ret = kill_user ( client , reason )
643
+ else
644
+ debug ( LOG_DEBUG , "Issuing AKILL: *@#{ host } , #{ reason } lasting for #{ @akill_duration } seconds" )
645
+ ret = akill_add ( "*@#{ host } " , reason , @akill_duration )
646
+ end # if protected hosts
579
647
end # if host
580
648
581
649
channel . downcase!
@@ -596,6 +664,31 @@ def akill(client, reason, operreason, channel="")
596
664
end # if kill_user
597
665
end # def akill
598
666
667
+ # ------------------------------------------------------------------------
668
+
669
+ # convert irc pattern to regular expression
670
+ def irc_pattern_to_regex ( pattern
671
+ # "." -> "\.", "*" -> ".*", "?" -> "."
672
+ # wrap with "^...$"
673
+ return pattern if pattern . start_with? '^'
674
+ pattern = pattern . gsub ( /\. / , '\\.' )
675
+ . gsub ( /\* / , '.*' )
676
+ . gsub ( /\? / , '.' )
677
+ return "^#{ pattern } $"
678
+ end
679
+
680
+ # ------------------------------------------------------------------------
681
+
682
+ # get protected patterns as pattern
683
+ def load_protected_patterns ( )
684
+ patterns = @protection . keys
685
+ if patterns . empty?
686
+ @protected_patterns = '^$'
687
+ else
688
+ @protected_patterns = patterns . join ( '|' )
689
+ end
690
+ end # def get_protected_patterns
691
+
599
692
# ------------------------------------------------------------------------
600
693
601
694
# enforce a channel - kill all of its users
@@ -648,6 +741,20 @@ def load_data()
648
741
count += 1
649
742
}
650
743
result . free
744
+
745
+ @protection = Hash . new
746
+ result = DB . execute ( @dbq [ 'GET_PROTECTED_PATTERNS' ] , '' )
747
+ count = 0
748
+ result . row_each { |row |
749
+ pattern = row [ 0 ]
750
+ reason = row [ 1 ]
751
+ pattern = irc_pattern_to_regex ( pattern )
752
+ @protection [ pattern ] = reason
753
+ count += 1
754
+ }
755
+ result . free
756
+ load_protected_patterns
757
+
651
758
debug ( LOG_DEBUG , "All channel data successfully loaded" )
652
759
end # def load_data
653
760
0 commit comments