Skip to content
Browse files

添加自动ban功能.

添加消息ignore功能.
  • Loading branch information...
1 parent d80be0a commit 4609a37357563ce10c40a4d5b61737eb473ec9b6 kk committed Dec 26, 2009
Showing with 94 additions and 56 deletions.
  1. +38 −13 Dic.rb
  2. +3 −2 default.conf
  3. +53 −41 irc.rb
View
51 Dic.rb
@@ -69,7 +69,7 @@ def URLDecode(str)
#str.gsub(/%[a-fA-F0-9]{2}/) { |x| x = x[1..2].hex.chr }
URI.unescape(str)
end
-
+
def URLEncode(str)
#str.gsub(/[^\w$&\-+.,\/:;=?@]/) { |x| x = format("%%%x", x.ord) }
URI.escape(str)
@@ -117,7 +117,7 @@ def loadDic()
puts 'Dic load [ok]'
end
def saveu
- return if Time.now - $last_save < 60 rescue nil
+ return if Time.now - $last_save < 120 rescue nil
$last_save = Time.now
File.open("person_#{ARGV[0]}.yaml","w") do|io|
YAML.dump($u,io)
@@ -288,13 +288,13 @@ def gettitle(url)
return nil if title =~ /index of/i
- puts "1=" + charset.to_s
+ #puts "1=" + charset.to_s
tmp.match(/<meta.*?charset=(.+?)["']/i)
charset=$1 if $1
if charset =~ /^gb/i
charset='gb18030'
end
- puts '2=' + charset.to_s
+ #puts '2=' + charset.to_s
#tmp = guess_charset(title * 2).to_s
#charset = 'gb18030' if tmp == 'TIS-620'
@@ -319,7 +319,7 @@ def getPY(c)
needAddKub=true
c.gsub!(/\skubuntu/i,' ')
end
- re = getGoogle(c ,1).to_s
+ re = google_py(c)
re = re + ' Kubuntu' if needAddKub==true
re.gsub!(/还原/i,'换源')
@@ -347,6 +347,23 @@ def encodeurl(url)
url
end
+def google_py(word)
+ re=''
+ url = 'http://www.google.com/search?hl=zh-CN&oe=UTF-8&q=' + word.strip
+ url = encodeurl(url)
+ url_mini = encodeurl('http://www.google.com/search?q=' + word.strip)
+
+ open(url,
+ 'Referer'=> url,
+ 'Accept-Encoding'=>'deflate',
+ 'User-Agent'=> UserAgent
+ ){ |f|
+ html=f.read.gsub(/\s+/,' ')
+ html.match(/是不是要找.*<em>(.*?)<\/em>/i)
+ return unescapeHTML($1.to_s)
+ }
+end
+
def getGoogle(word,flg)
re=''
url = 'http://www.google.com/search?hl=zh-CN&oe=UTF-8&q=' + word.strip
@@ -361,12 +378,6 @@ def getGoogle(word,flg)
'User-Agent'=> UserAgent
){ |f|
html=f.read.gsub(/\s+/,' ')
- case flg
- when 1#拼音查询
- html.match(/是不是要找:.*?<em>(.*?)<\/em>/i)#<!--a-->
- re = $1.to_s.gsub(/\s/i,' ')
- re = unescapeHTML(re)
- when 0
matched = true
case html
when /相关词句:(.*?)网络上查询(.*?)(https?:\/\/\S+[^\s*])">/i#define
@@ -432,7 +443,6 @@ def getGoogle(word,flg)
#end
re = url_mini + ' ' + re
end
- end
return nil if re.bytesize < 3
re.gsub!(/<.*?>/i,'')
re.gsub!(/\[\s翻译此页\s\]/,'')
@@ -442,7 +452,6 @@ def getGoogle(word,flg)
return unless re
return if re.bytesize < url_mini.bytesize + 3
return re
-
end
def geted2kinfo(url)
@@ -586,6 +595,22 @@ def hostA(domain,hideip=false)#处理IP 或域名
tmp.gsub(/\s+/,'').to_s + ' '
end
+#eval
+def evaluate(s)
+ #return '操作不安全' if s=~/pass|serv/i
+ return if s !~ /\d/ and s.size < 6
+ result = nil
+ begin
+ #p s
+ Timeout.timeout(4) {
+ result = safe(4) {eval(s).to_s[0,400]}
+ }
+ rescue Exception => detail
+ result = detail.message()
+ end
+ return result
+end
+
#为字符串添加2个方法,用于gb18030和utf8互转.
class String
def utf8
View
5 default.conf
@@ -1,7 +1,7 @@
#!/usr/bin/ruby -w
# coding: utf-8
-#irc 设置
+#irc 服务器设置
$server = 'irc.freenode.net'
$port = 6667
$nick = "kk-bot"
@@ -10,7 +10,7 @@ $channel = '#sevk'
$charset = 'UTF-8' # irc频道的字符设置
$user = "bot svn ver bot :svn Ver bot"
-#功能设置
+#bot功能设置
$need_Check_code = 1
$need_say_feed = 0
$saytitle = 0
@@ -20,4 +20,5 @@ $debug = false # 调试开关
#本地显示设置
$local_charset = 'UTF-8'# windows 平台可以设置为 'gb2312 或 gb18030'
$local_charset = 'GB18030' if RUBY_PLATFORM =~ /win/i # 也可以自动判断是不是windows系统
+$ignore = "quit join" #过滤词
View
94 irc.rb
@@ -47,8 +47,24 @@ def initialize(server, port, nick, channel, charset, pass, user)
#kick踢出
def kick(s)
- send "kick #@channel #{s} 大段内容请贴到http://paste.ubuntu.org.cn"
+ send "kick #@channel #{s} 大段内容请贴到http://pastebin.ca 或 http://paste.ubuntu.org.cn"
end
+
+ #/mode #ubuntu-cn +b *!*@1.1.1.0
+ def autoban(chan,s)
+ send "mode #{chan} +b #{s}"
+ Thread.new do
+ tmp = s
+ sleep 35
+ puts 'unban: ' + tmp
+ send "mode #{chan} -b #{tmp}"
+ end
+ end
+
+ def unban(chan,s)
+ send "mode #{chan} -b #{s}"
+ end
+
def ping(s)
$Lping = Time.now
$Lsay = Time.now
@@ -103,22 +119,6 @@ def connect()
send "USER #@str_user"
end
- #eval
- def evaluate(s)
- #return '操作不安全' if s=~/pass|serv/i
- return if s !~ /\d/ and s.size < 4
- result = nil
- begin
- p s
- Timeout.timeout(4) {
- result = safe(4) {eval(s).to_s[0,400]}
- }
- rescue Exception => detail
- result = detail.message()
- end
- return result
- end
-
#发送字典结果 ,取字典,可以用>之类的重定向,向某人提供字典数据
def sayDic(dic,from,to,s='')
direction = ''
@@ -218,7 +218,7 @@ def check_code(s)
if s =~ /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+?)\s:(.*)$/i#需要提示
from=b1=$1;name=b2=$2;ip=b3=$3;to=b4=$4;say=$5.to_s.untaint
#return if s =~ /action/i
- send "Notice #{from} :use #{@charset} charset, not #{tmp}"
+ send "Notice #{from} :使用 #{@charset} 字符编码,别用#{tmp}".utf8_to_gb
return 'matched err charset, but not need check code' if $need_Check_code < 1
send "PRIVMSG #{((b4==@nick)? from: to)} :#{from}:said #{say} in #{tmp} ? But we use #{@charset} !"
return 'matched err charset'
@@ -246,6 +246,10 @@ def check_msg(s)
sSay = '`help |'
end
+ if $u.isBlocked?(from)
+ return nil
+ end
+
tmp = check_dic(a5,a1,a1)
if tmp.class == Fixnum
if sSay.bytesize < 4 and rand(10) > 6
@@ -282,14 +286,14 @@ def check_msg(s)
#$u.floodmereset(a1)
return if to =~ NoFloodAndPlay # 不检测flood和玩bot
$otherbot_said=true
- msg to ,"#{from}, 玩机器人? ? ... 去 #Sevk or #{to}-ot ",0 if rand(10) > 4
+ msg to ,"#{from}, 玩机器人? ? ... 去 #Sevk or #{to}-ot ",0 if rand(10) > 5
return nil
end
end
return 'msg with my name:.+'
else
- #不处理gateway用户
- return if a3=~ /^gateway\//i
+ ##不处理gateway用户
+ return if a3=~ /^gateway\//i && $black_gateway
end
#禁掉一段时间
@@ -308,15 +312,17 @@ def check_msg(s)
#puts '消息以我名字开头'
#$otherbot_said=false
#do_after_sec(to,"#{from}, #{$me.rand(sSay)}",10,15) if $me
- #`sh sound.sh`
+ #`sh sound.sh` if File.exist? 'sound.sh'
else
#$u.said(from,name,ip)
#$u.setLastSay(from,sSay)
- if $u.saidAndCheckFlood(a1,a2,a3,sSay)
- $u.floodreset(a1)
+ if $u.saidAndCheckFlood(nick,name,ip,sSay)
+ $u.floodreset(nick)
return if to =~ NoFloodAndPlay # 不检测flood和玩bot
- msg(a4,"#{a1}: ...flood ? 超过4行或图片请贴到 http://paste.ubuntu.org.cn",4)
- kick a1
+ #kick a1
+ autoban to,"#{nick}!*@*"
+ #msg(a4,"#{a1}: ... 大段内容请贴到http://pastebin.ca 或 http://paste.ubuntu.org.cn",0)
+ notice(nick,"#{a1}: ... 大段内容请贴到http://pastebin.ca 或 http://paste.ubuntu.org.cn",4)
return nil
end
end
@@ -386,7 +392,7 @@ def check_dic(s,from,to)
case s.strip
when /^\`?>\s?(.+)$/i #eval
puts "[4 EVAL #{$1} from #{from}]"
- tmp=evaluate($1.to_s)
+ tmp = evaluate($1.to_s)
msg to,"#{from}, #{tmp}",0 if tmp
when /^`host\s(.*?)$/i # host
sayDic(10,from,to,$1.gsub(/http:\/\//i,''))
@@ -486,7 +492,7 @@ def check_dic(s,from,to)
do_after_sec(to,from + ', hi .',10,21) if rand(10) > 6
when /^`?((有人(...)?(吗|不|么|否)((...)?|\??))|test.{0,6}|测试(中)?(.{1,7})?)$/i #有人吗?
$otherbot_said=false
- do_after_sec(to,from + ', hello .',10,17)
+ do_after_sec(to,from + ', hello .',10,20)
when /^`?(bu|wo|ni|ta|shi|ru|zen|hai|neng|shen|shang|wei|guo|qing|mei|xia|zhuang|geng|zai)\s(.+)$/i #拼音
return nil if s =~ /[^,.?\s\w]/ #只能是拼音或标点
return nil if s.bytesize < 12
@@ -500,18 +506,18 @@ def check_dic(s,from,to)
tmp = "%03s" % tmp
$need_Check_code -= 1 if tmp[0].ord == 48
- $need_Check_code += 1 if tmp[0].ord == 49
+ $need_Check_code += 1 if tmp[0].ord == 49 and $need_Check_code < 1
$need_say_feed -= 1 if tmp[1].ord == 48
- $need_say_feed += 1 if tmp[1].ord == 49
+ $need_say_feed += 1 if tmp[1].ord == 49 and $need_say_feed < 1
$saytitle -= 1 if tmp[2].ord == 48
- $saytitle += 1 if tmp[2].ord == 49
+ $saytitle += 1 if tmp[2].ord == 49 and $saytitle < 1
load 'Dic.rb'
load 'irc_user.rb'
load "ipwry.rb"
- #load 'plugin.rb'
+ #load 'plugin.rb'
loadDic
- msg(to,"restarted, check_charset=#$need_Check_code, get_ub_feed=#$need_say_feed, get_title=#{$saytitle}",0)
+ msg(to,"restarted, check_charset=#$need_Check_code, get_ub_feed=#$need_say_feed, get_title=#{$saytitle}",0)
else
return 1#not match dic_event
end
@@ -607,15 +613,15 @@ def save_log(s)
def handle_server_input(s)
return if check_irc_event(s) #服务器消息
return if check_code(s) #乱码
- puts highlighted(s) rescue nil #高亮显示消息
+ pr_highlighted(s) #高亮显示消息
save_log(s)#写入日志
return if not $bot_on #bot 功能
#s=s.force_encoding("UTF-8")
return if check_msg(s).class != Fixnum #字典消息
end
#显示高亮
- def highlighted(s)
+ def pr_highlighted(s)
s=s.force_encoding("utf-8")
s=s.gb_to_utf8 if @charset !~ /UTF-8/i
case s
@@ -624,15 +630,17 @@ def highlighted(s)
if mt =~ /^priv/i
mt= ''
else
+ #return if mt =~ Regexp.new(Regexp.escape($ignore),Regexp::IGNORECASE)
+ return if mt =~ Regexp.new($ignore,Regexp::IGNORECASE)
mt= mt.green
end
- sy=sy.yellow if mt =~ /\s#{Regexp::escape @nick}/i
+ sy= sy.yellow if mt =~ /\s#{Regexp::escape @nick}/i
re= "<#{from.cyan}> #{mt} #{sy}"
else
re= s.red
end
re = re.utf8_to_gb if $local_charset !~ /UTF-8/i
- return re
+ puts re
end
#记录自己说话的时间
@@ -712,8 +720,8 @@ def iSend()
tmp = $1.to_s
send 'quit optimize ' + tmp
p 'quit...'
- sleep 3
myexit()
+ sleep 2
exit
when /^\/msg\s(.+?)\s(.+)$/i
who = $1;s=$2
@@ -727,21 +735,25 @@ def iSend()
send s.gsub(/^[\/:]/,"")
when /^`/
check_dic(s,@nick,@channel)
+ when /^\>(.*)/
+ say eval($1).to_s
else
say s
end
#end
end
end
def mystart
- $u = YAML.load_file("person_#{ARGV[0]}.yaml") rescue nil
- $u = ALL_USER.new if ! $u
+ $u = YAML.load_file("person_#{ARGV[0]}.yaml") rescue (p $!.message)
+ p $u.class
+ $u = ALL_USER.new if $u.class != ALL_USER
$u.init_pp
puts $u.all_nick.count.to_s + ' nicks loaded from yaml file.'.red
end
def myexit
saveu
+ sleep 1
puts 'exiting...'.yellow
@exit = true
end
@@ -764,7 +776,7 @@ def timer_start
n = 0
loop do
sleep(700 + rand(850))
- p Time.now.to_s.yellow
+ puts Time.now.to_s.yellow
n+=1
next if n%2 ==0
next unless (8..24) === Time.now.hour

0 comments on commit 4609a37

Please sign in to comment.
Something went wrong with that request. Please try again.