Permalink
Browse files

小优化.

  • Loading branch information...
1 parent 6eb6749 commit c10da570eb531517979dee1eceff18ea6ef23cfc kk on ub1 committed Jan 29, 2011
Showing with 125 additions and 75 deletions.
  1. +36 −44 irc.rb
  2. +70 −19 lib/{Dic.rb → dic.rb}
  3. +6 −7 lib/irc_user.rb
  4. +13 −5 lib/log.rb
View
80 irc.rb
@@ -12,7 +12,7 @@
$: << 'lib'
$: << '.'
require 'platform.rb'
-load 'Dic.rb'
+load 'dic.rb'
include Math
require "monitor"
require "readline"
@@ -224,7 +224,7 @@ def check_code(s)
return if ! tmp
if tmp != @charset && tmp !~ /IBM855|windows-1252/i
if tmp =~ /^gb./i
- tmp = 'GBK'
+ #tmp = 'GBK'
s=Iconv.conv("#{@charset}//IGNORE","GB18030//IGNORE",s).strip
else
p tmp
@@ -271,6 +271,7 @@ def check_msg(s)
$otherbot_said=false
do_after_sec(to,"#{from}, #{$me.rand(sSay)}",10,15) if defined?$me
end
+
when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+?)\s:(.+)$/i #PRIVMSG channel
nick=from=a1=$1;name=a2=$2;ip=a3=$3;to=a4=$4;sSay=a5=$5
return if a1==@nick
@@ -287,7 +288,11 @@ def check_msg(s)
end
#p 'check flood'
- $u.said(nick,name,ip) if sSay.size > 320
+ if sSay.size > 290
+ p sSay.size
+ $u.said(nick,name,ip,1.1)
+ $u.said(nick,name,ip,1.1) if sSay.size > 380
+ end
if to !~ NoFloodAndPlay and $u.saidAndCheckFlood(nick,name,ip,sSay)
$u.floodreset(nick)
tmp = Time.now - $u.get_ban_time(nick)
@@ -299,7 +304,7 @@ def check_msg(s)
kick a1
else
autoban to,nick
- msg(a4,"#{a1}:...,谁说话这么快,#$kick_info",0)
+ msg(a4,"#{a1}:..., 有刷屏嫌疑 ,#$kick_info",0)
end
notice(nick,"#{a1}: ... #$kick_info",14)
return
@@ -310,8 +315,7 @@ def check_msg(s)
#ban ctcp but not /me
if sSay[0].ord == 1 then
if sSay[1,6] != /ACTION/i then
- $u.saidAndCheckFlood(nick,name,ip,sSay)
- #$u.saidAndCheckFlood(nick,name,ip,sSay)
+ $u.said(nick,name,ip,sSay,0.9)
end
return
end
@@ -442,7 +446,7 @@ def check_msg(s)
def check_dic(s,from,to)
case s.strip.force_encoding('utf-8')
when /^`?> (.+)$/i
- @e=Thread.start($1){|s|
+ @e=Thread.new($1){|s|
tmp = evaluate(s.to_s)
msg to,"#{from}, #{tmp}", 40 if not tmp.empty?
}
@@ -499,11 +503,11 @@ def check_dic(s,from,to)
sayDic(2,from,to,$1)
when /^`address\s(.*?)$/i #查某人ip
sayDic(22,from,to,$1)
- when /^`?f\s(.*?)$/i #查某人的老乡
+ when /^`f\s(.*?)$/i #查某人的老乡
sayDic(23,from,to,$1)
when /^`?(大家好(...)?|hi( all)?.?|hello)$/i
$otherbot_said=false
- do_after_sec(to,from + ', 好',10,18)
+ do_after_sec(to,from + ', 好',10,23)
when /^`?((有人(...)?(吗|不|么|否)((...)?|\??))|test.{0,5}|测试(下|中)?.{0,5})$/ui #有人吗?
$otherbot_said=false
do_after_sec(to,from + ', ....',10,12)
@@ -527,7 +531,7 @@ def check_dic(s,from,to)
$saytitle -= 1 if tmp[2].ord == 48
$saytitle += 1 if tmp[2].ord == 49 and $saytitle < 1
- load 'Dic.rb'
+ load 'dic.rb'
load 'irc_user.rb'
load "ipwry.rb"
#load 'irc.rb'
@@ -712,24 +716,7 @@ def do_after_sec(to,sSay,flg,second=18)
sleep 1
send "JOIN #{@channel}" if @channel != '#sevk'
when 10#打招呼回复
- tmp = Time.parse('2010-02-14 00:00:00+08:00')-Time.now
- if tmp < 0 #不用显示倒计时
- return if sSay =~ /\s$/
- send "PRIVMSG #{to} :#{sSay} \0039 #{chr_hour} \017"
- return
- end
-
- case tmp
- when 0..60
- tmp="#{tmp}"
- when 61..3600
- tmp="#{tmp/60}分钟"
- when 3601..86400
- tmp="#{tmp/60/60}小时"
- else
- tmp="#{tmp/60/60/24}"
- end
- send "privmsg #{to} :#{sSay} #{chr_hour} #{Time.now.strftime('[%H:%M]')} \0039新年快乐,离除夕0点还有 #{tmp}\017"
+ send hello_replay(to,sSay)
when 20#notice
send "NOTICE #{to} :#{sSay}"
end
@@ -794,7 +781,7 @@ def iSend()
$stdout.flush
#windows 好像不支持Readline
if os_family == 'windows'
- s = IO.select([$stdin],nil,nil,5)
+ s = IO.select([$stdin],nil,nil,1)
next if !s
#next if s[0][0] != IO
s = $stdin.gets
@@ -822,13 +809,16 @@ def iSend()
send s.gsub(/^[\/\:]/,'')
end
when /^`/ #直接执行
- p s
if s[1..-1] =~ />\s(.*)/
- Thread.new do
- tmp=eval($1.to_s) rescue $!
- p tmp
- say tmp if tmp.class == String
- end
+ p s
+ begin
+ tmp=eval($1.to_s)
+ say tmp if tmp.class == String
+ rescue Exception
+ p $!.message
+ #rescue
+ #p $!.message
+ end
else
check_dic(s,@nick,@channel)
end
@@ -886,19 +876,21 @@ def timer_start
def main_loop()
loop do
begin
- sleep 0.08
+ sleep 0.1
return if @exit
break if $need_reconn
- ready = select([@irc], nil, nil,0.01)
+ ready = select([@irc], nil, nil, 0.2)
next if not ready
for s in ready[0]
next if s != @irc
- s = @irc.recvfrom(2048)[0].strip
- if s.empty?
- p ' s.empty, must be lose conn '
- return
- end
- handle_server_input(s)
+ x = @irc.recvfrom(2048)[0]
+ if x.empty?
+ p ' x.empty, must be lose conn '
+ return
+ end
+ x.split(/\r?\n/).each{|s|
+ handle_server_input(s)
+ }
end
rescue Exception
log
@@ -943,7 +935,7 @@ def main_loop()
p Time.now
sleep 240
end
- Thread.list.each{|x|(x.kill; x.exit) rescue nil}
+ Thread.list.each{|x|(x.kill; x.exit) rescue nil}
end
# vim:set shiftwidth=2 tabstop=2 expandtab textwidth=79:
View
@@ -40,6 +40,7 @@ def addTimCh
def unescapeHTML
HTMLEntities.new.decode(self) rescue self
end
+ alias dir public_methods
end
require 'ipwry.rb'
@@ -96,7 +97,7 @@ def unescapeHTML
$min_next_say = Time.now
$Lsay=Time.now; $Lping=Time.now
$last_save = Time.now - 110
-$proxy_status_ok = false
+$proxy_status_ok = false if not defined? $proxy_status_ok
puts "$SAFE= #$SAFE"
NoFloodAndPlay=/\-ot|arch|fire/i
@@ -105,8 +106,8 @@ def unescapeHTML
$botlist_ub_feed=/crazyghost|\^?[Ou]_[ou]/i
$botlist_title=/raybot|\^?[Ou]_[ou]/i
#$tiList=/ub|deb|ux|ix|win|beta|py|ja|qq|dn|pr|qt|tk|ed|re|rt/i
-$urlList = $tiList = /ubunt|linux|debia|java|python|ruby|perl|vim|emacs|gnome|kde|x11|xorg|wine/i
-$urlProxy=/\.ubuntu\.(org|com)\.cn|linux\.org|ubuntuforums\.org|\.wikipedia\.org|\.twitter\.com|\.youtube\.com/i
+$urlList = $tiList = /ubunt|linux|unix|debia|java|python|ruby|perl|vim|emacs|gnome|kde|x11|xorg|wine/i
+$urlProxy=/\.ubuntu\.(org|com)\.cn|\.archive\.org|linux\.org|ubuntuforums\.org|\.wikipedia\.org|\.twitter\.com|\.youtube\.com/i
$urlNoMechanize=/.|google|\.cnbeta\.com|combatsim\.bbs\.net\/bbs|wikipedia\.org|wiki\.ubuntu/i
$my_s= '我的源代码: http://github.com/sevk/kk-irc-bot/ '
@@ -164,7 +165,7 @@ def loadDic()
def saveu
return if Time.now - $last_save < 120 rescue nil
$last_save = Time.now
- File.open("person_#{ARGV[0]}.yaml","w") do|io|
+ File.open("_#{ARGV[0]}.yaml","w") do|io|
YAML.dump($u,io)
end
puts ' save u ok'.red
@@ -419,10 +420,12 @@ def gettitle(url,proxy=nil,mechanize=1)
rescue Timeout::Error
return 'time out . IN gettitle '
rescue
- p $!.message + $@[0]
- #if $!.message == 'Connection reset by peer' && $proxy_status_ok
- #return Timeout.timeout(12){gettitle(url,true)}
- #end
+ p $!.message
+ if $!.message == 'Connection reset by peer' && $proxy_status_ok
+ p ' need pass wall '
+ return if proxy
+ return gettitle(url,true,true)
+ end
return $!.message[0,60] + ' . IN gettitle'
end
@@ -709,7 +712,7 @@ def getBaidu_tran(word,en=true)
#为Time类加入hm方法,返回格式化后的时和分
class Time
def hm
- Time.now.strftime(' %H:%M')
+ Time.now.strftime(' %H:%M')
end
#ch,小时字符. '㍘' = 0x3358
def ch
@@ -774,19 +777,19 @@ def evaluate(s)
l=2 if s =~ /^(b|gg|`pwd`|`uname -a`|`uptime`)$/
l=2 if s =~ /^`(free|lsb_release -a|ifconfig|ls|who[a-z]+)`$/
l=2 if s =~ /^`(ps) [a-z\/]+`$/i
- l=2 if s =~ /^`(date)`$/i
#l=2 if s =~ /^`[\w\s\-]+`$/i
- #return '' if s =~ /touch|kill|:|reboot|halt/i
+ return '' if s =~ /touch|kill|:\(\)|reboot|halt/i
+ #return '' if s =~ /kill|mkfs|mkswap|dd|\:\(\)|chmod|chown|fork|gcc|rm|reboot|halt/i
Timeout.timeout(5){
return safe(l){eval(s).to_s[0,280]}
#return safely(s,l)[0,400]
}
rescue Timeout::Error
return 'Timeout Error'
rescue Exception
- return ''#$!.message[0,38] # + $@[1..2].join(' ')
+ return ''#$!.message[0,28] # + $@[1..2].join(' ')
rescue
- return ''#$!.message[0,38] #+ $@[1..2].join(' ')
+ return $!.message[0,28] #+ $@[1..2].join(' ')
end
end
@@ -895,11 +898,12 @@ def check_proxy_status
rescue Timeout::Error
print $proxy_addr,':',$proxy_port,' ',false
$proxy_status_ok = false
- return
+ false
end
print $proxy_addr,':',$proxy_port,' ',true
- $proxy_status_ok = true
a.close
+ $proxy_status_ok = true
+ true
end
end
@@ -912,18 +916,65 @@ def addTimCh
end
def chr_hour
- Time.now.hm
+ Time.now.ch
end
#随机事件
def rand_do
case rand(1000)
- when 0..40
+ when 0..20
$my_s
- when 100..110
+ when 100..120
get_feed
- when 200..260
+ when 200..250
"...休息一下..."
end
end
+def hello_replay(to,sSay)
+ tmp = Time.parse('2011-02-02 00:00:00+08:00')-Time.now
+ if tmp < 0 #不用显示倒计时
+ return if sSay =~ /\s$/
+ return "PRIVMSG #{to} :#{sSay} \0039 #{chr_hour} \017"
+ end
+
+ case tmp
+ when 61..3600
+ tmp="#{tmp/60}分钟"
+ when 3601..86400
+ tmp="#{tmp/60/60}小时"
+ when 0..60
+ tmp="#{tmp}"
+ else
+ tmp="#{tmp/60/60/24}"
+ end
+ tmp = sprintf("%.2f", tmp)
+ return "privmsg #{to} :#{sSay} #{chr_hour} #{addTimCh} \0039新年快乐,除夕还有 #{tmp}\017"
+end
+
+def gettitle_https(url)
+ require 'net/http'
+ require 'net/https'
+
+ url = URI.parse(url)
+
+ http = Net::HTTP.new(url.host, url.port)
+ http.use_ssl = true if url.scheme == 'https'
+
+ request = Net::HTTP::Get.new(url.path)
+ puts http.request(request).body
+end
+
+def gettitle_proxy(url)
+#Net::HTTP 的类方法 Net::HTTP.Proxy通常会生成一个新的类,该类通过代理进行连接操作。由于该类继承了Net::HTTP,所以可以像使用Net::HTTP那样来操作它。
+
+require 'net/http'
+Net::HTTP.version_1_2 # 设定对象的运作方式
+#Net::HTTP::Proxy($proxy_addr, $proxy_port).start( 'some.www.server' ) {|http|
+ ## always connect to your.proxy.addr:8080
+ #:
+#}
+#若Net::HTTP.Proxy的第一参数为nil的话,它就会返回Net::HTTP本身。所以即使没有代理,上面的代码也可应对自如。
+
+end
+
Oops, something went wrong.

0 comments on commit c10da57

Please sign in to comment.