Top Level Namespace

Includes:
FileUtils

Defined Under Namespace

Classes: All_user, Dic, Fixnum, Hash, IpLocationSeeker, Numeric, String, Time

Constant Summary collapse

Ver =
'v0.53'
Help =
"我是 kk-irc-bot Ver:#{Ver} ㉿ s 新手资料 g google d define `new 取论坛新贴 `deb 包查询 tt 翻译 `t 词典 > s 计算s的值 > gg 公告 > b 服务器状态 `address 查某人地址 `host 查域名 . 末尾加入|重定向,如 g ubuntu | nick"
UserAgent =
"(X11; U; Linux i686; en-US; rv:1.9.1.2) Gecko/20090810 Ubuntu/#{`lsb_release -r`.split(/\s/)[1] rescue ''} (ub)"
CN_re =
/(?:\xe4[\xb8-\xbf][\x80-\xbf]|[\xe5-\xe8][\x80-\xbf][\x80-\xbf]|\xe9[\x80-\xbd][\x80-\xbf]|\xe9\xbe[\x80-\xa5])+/n
ChFreePlay =
/\-ot|arch|fire/i
Oneday =
86400
Myname =
File.basename($0, ".rb")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.empty?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/dic.rb', line 16

def nil.empty?
  true
end

Instance Method Details

#addTimChObject


1018
1019
1020
# File 'lib/dic.rb', line 1018

def addTimCh
	Time.now.hm
end

#aptw(name) ⇒ Object Also known as: get_deb_info

get deb info


966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# File 'lib/dic.rb', line 966

def aptw name
  agent = Mechanize.new
  agent.user_agent_alias = 'Linux Mozilla'
  agent.max_history = 0
  agent.open_timeout = 12
  agent.cookies
  begin
    url = 'http://packages.ubuntu.com/search?&searchon=names&suite=all&section=all&keywords=' + name.strip
    #url = 'http://packages.debian.org/search?suite=all&arch=any&searchon=names&keywords=' + name.strip
    #p url
    #page = agent.get(url)
    page = agent.get_file(url)
    #return nil if page.class != Mechanize::Page
  rescue
    #p e.message
    return $!.message[0,60] + ' . IN getdeb'
  end
  s = page.split(/<\/h2>/im)[1]
  s = s.match(/.*resultlink".+?:(.+?)<br>(.+?): .*<h2>/mi)[1..2].join ','
  s = s.gsub!(/\s+/,' ')
  s.gsub!(/<.*?>/,'')
  s.unescapeHTML
end

#auto_set_ch_baud(ch) ⇒ Object

记录频道说话的频率


1147
1148
1149
1150
1151
1152
# File 'lib/dic.rb', line 1147

def auto_set_ch_baud(ch)
	@ch_baud ||= Hash.new
	@ch_baud.default = Hash.new
  #最后1次发言时间
	@ch_baud[ch]['last']=Time.now
end

#bObject

返回uptime


935
936
937
# File 'lib/dic.rb', line 935

def b
  `uptime`
end

#botsay(s) ⇒ Object

调用 alice


1077
1078
1079
1080
1081
1082
# File 'lib/dic.rb', line 1077

def botsay(s)
  return if s.empty?
  s.zh2en.alice_say.en2zh
rescue
  '.. 休息一下 ..' if rand < 0.3
end

#check_proxy_statusObject

简单检测代理是否可用


1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/dic.rb', line 1000

def check_proxy_status
  Thread.new do
    a=true
    Thread.current[:name]= 'check proxy stat'
    begin
      Timeout.timeout(4){
        a=TCPSocket.open($proxy_addr2,$proxy_port2)
        a.close
      }
    rescue Exception
      print $proxy_addr2,':',$proxy_port2,' ',false,"\n"
      a=false
    end
    print $proxy_addr2,':',$proxy_port2,' ',a ,"\n"
    $proxy_status_ok = a
  end
end

#chr_hourObject


1022
1023
1024
1025
# File 'lib/dic.rb', line 1022

def chr_hour
	Time.now.hm
	#Time.now.ch
end

#dictcn(word) ⇒ Object

dict.cn


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/dic.rb', line 309

def dictcn(word)
  word = word.utf8_to_gb
  url = 'http://dict.cn/mini.php?q=' + word
  url = URI.escape(url)
  uri = URI.parse(url)
  uri.open(
  'Accept'=>'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*',
  'Accept'=>'text/html',
  'Referer'=> URI.escape(url),
  'Accept-Language'=>'zh-cn',
  #'Cookie' => cookie,
  'Range' => 'bytes=0-8000',
  'User-Agent'=> UserAgent
  ){ |f|
    re = f.read[0,5059].force_encoding('utf-8').gsub(/\s+/,' ').gb_to_utf8
    re.gsub!(/<script.*?<\/script>/i,'')
    re.gsub!(/<.*?>/i,'')
    re.gsub!(/.*?Define /i,'')
    re.gsub!(/加入生词本.*/,'')
    return re.unescapeHTML + ' << Dict.cn'
  }
rescue
  return $!.message
end

#encodeurl(url) ⇒ Object


605
606
607
# File 'lib/dic.rb', line 605

def encodeurl(url)
  URI.encode(url)
end

#evaluate(s) ⇒ Object

eval


896
897
898
899
900
901
902
903
904
905
906
# File 'lib/dic.rb', line 896

def evaluate(s)
	begin
		return Timeout.timeout(23){
      safe_eval(s)
		}
	rescue Timeout::Error
		return ' Timeout, 超时。。'
  rescue
    return $!.message[0,88]# + [email protected](' ')
	end
end

#g_tr(word, flg) ⇒ Object

google 全文翻译,参数可以是中文,也可以是英文.


262
263
264
265
266
267
268
269
270
271
272
# File 'lib/dic.rb', line 262

def g_tr(word,flg)
  word = URI.escape(word)
  url = "http://translate.google.com/translate_a/t?client=firefox-a&text=#{word}&langpair=#{flg}&ie=UTF-8&oe=UTF-8"
  uri = URI.parse(url)
  uri.open(
	 'Accept'=>'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*',
	 'Referer'=> URI.escape(url)
	 ){ |f|
			return f.read.match(/"trans":"(.*?)","/)[1]
  }
end

#get_banedObject

+q


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/plugin.rb', line 20

def get_baned
  re = []
  a = Dir.glob '*_baned.ban'
  a.each do |f|
    puts ' baned file : ' + f.to_s
    s = File.read(f)
    p s
    re << s
    File.delete f
  end
  re
end

#getaddr_fromip(ip) ⇒ Object


854
855
856
# File 'lib/dic.rb', line 854

def getaddr_fromip(ip)
  hostA(ip,true)
end

#getBaidu(word) ⇒ Object


773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
# File 'lib/dic.rb', line 773

def getBaidu(word)
  url=  'http://www.baidu.com/s?cl=3&ie=UTF-8&wd='+word
  if url =~ /[\u4E00-\u9FA5]/
    url = URI.encode(url)
  end
  p url
  open(url,
  'Accept'=> '*/*',
  'Referer'=> url,
  'Accept-Language'=>'zh-cn',
  'Accept-Encoding'=>'deflate',
  'User-Agent'=> UserAgent,
  'Host'=>'www.baidu.com',
  'Connection'=>'close'
  ) {|f|
      html=f.read().gsub!(/\s/,' ')
      re = html.match(/ScriptDiv(.*?)(http:\/\/\S+[^\s*])(.*?)size=-1>(.*?)<br><font color=#008000>(.*?)<a\ href(.*?)(http:\/\/\S+[^\s*])/i).to_s
      re = $4 ; a2=$2[0,120]
      re= re.unescapeHTML.gsub(/<.*?>/i,'')[0,330]
      $re = a2 + ' ' +  re
      $re = $re.code_a2b('gbk','UTF-8')[0,980]
  }
  $re
end

#getBaidu_tran(word, en = true) ⇒ Object


798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/dic.rb', line 798

def getBaidu_tran(word,en=true)
    url= 'http://www.baidu.com/s?cl=3&ie=UTF-8&wd='+word+'&ct=1048576'
    if url =~ /[\u4E00-\u9FA5]/
      url = URI.encode(url)
    end
    open(url,
    'Accept'=> '*/*',
    'Referer'=> url,
    'Accept-Language'=>'zh-cn',
    'Accept-Encoding'=>'deflate',
    'User-Agent'=> UserAgent,
    'Host'=>'www.baidu.com',
    'Connection'=>'close',
    'Cookie'=>'BAIDUID=EBBDCF1D3F9B11071169B4971122829A:FG=1; BDSTAT=172f338baaeb951db319ebc4b74543a98226cffc1f178a82b9014a90f703d697'
    ) {|f|
        html = f.read()
        html = html.gb_to_utf8.gsub(/\s+/,' ')
        re = ' <' + html.match(/class="wd"(.+?)<script>pronu/i)[1].to_s + ' '
        re += html.match(/class="explain">(.+?)<script/i)[1]
        re.gsub!(/<script\s?.+?>.+?<\/script>/i,'')
        re = re[0,600]
        re.gsub!(/&nbsp/,' ')
        re = re.unescapeHTML
        re.gsub!(/<.*?>/,'')
        $re = re.gsub(/>pronu.+?中文翻译/i,' ')
        $re.gsub!(/以下结果由.*?提供词典解释/,' ')
        $re.gsub!(/部首笔画部首.+?基本字义/,' 基本字义: ')
        if en
          $re.gsub!(/基本字义.*?英文翻译/,': ')
        end
    }
    $re
end

#getbody(url) ⇒ Object


247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/dic.rb', line 247

def getbody(url)
  p url
	agent = Mechanize.new
  agent.user_agent_alias = 'Linux Mozilla'
	#agent.user_agent_alias = 'Mac Safari'
  agent.max_history = 0
  agent.open_timeout = 12
  agent.cookies
	page = agent.get(url)
	#form = page.form_with(:name => 'f')
	#page = agent.post(url,{"input"=> self } )
  p ' get body ok '
	page.body
end

#getGoogle(word, flg = 0) ⇒ Object


661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'lib/dic.rb', line 661

def getGoogle(word,flg=0)
  print "word:"
  p word
  #url = 'http://www.google.com.hk/search?hl=zh-CN&oe=UTF-8&q=' + word.strip
  url = 'http://www.google.com.hk/search?q=' + word.strip
  #s=getbody(url)
  #puts s.size
  #File.new('/tmp/a.x','wb').puts s
  #p s.class
  #s = s.match(/<div id=resultStats>.+/i)[0]
  #File.open('tmp.html','wb').puts s
	#puts s.match(/.+?<div id=foot>/i)[0]
	#return
	#url = encodeurl(url)
	url = URI.encode(url)
  p url
	#url_mini = encodeurl('http://g.cn/search?q=' + word.strip)
	url_mini = 'http g.cn'

    re=''
    open(url
      #'Accept'=>'*/*',
      #'Referer'=> url,
      #'Accept-Language'=>'zh-CN',
      #'Accept-Encoding'=>'deflate',
      #'User-Agent'=> UserAgent
    ){ |f|
        html=f.read.gsub(/\s+/,' ')
        html=html.code_a2b(guess_charset(html) ,'utf-8')
        #File.new('/tmp/a.html','wb').puts html.match(/<div id="resultStats">.*/im)[0].gsub(/></,">\n<")
        File.new('/tmp/a.html','wb').puts html.match(/.*?<div id="resultStats">/im)[0].gsub(/></,">\n<")
        matched = true
        case html
        when /<div class=f .*?><h3 class="r"><nobr>.*?<\/nobr>(.*?)<!--n--><!--m-->.*?<li class="g"><div class="vsc" sig="U2O">/
            re = "#$1 #$2"
        when /相关词句:(.*?)网络上查询(.*?)(https?:\/\/\S+[^\s*])">/i#define
          tmp = $2.to_s + " > " + $3.to_s.gsub(/&amp;.*/i,'')
          tmp += ' ⋙ SEE ALSO ' + $1.to_s if rand(10)>5 and $1.to_s.size > 2
        when /专业气象台|比价仅作信息参考/
          tmp = html.match(/resultStats.*?\/nobr>(.*?)(class=hd>搜索结果|Google\s+主页)/i)[1]
        when /calc_img\.gif(.*?)Google 计算器详情/i #是计算器
          tmp = "<#{$1} Google 计算器" #(.*?)<li>
        else
          matched = false
        end
        #puts html.match(/搜索用时(.*?)搜索结果<\/h2>(.*?)网页快照/i)[0]
        if matched or html =~ /搜索用时(.*?)搜索结果<\/h2>(.*?)网页快照/i
          if !matched
            tmp =$2.gsub(/<cite>.+<\/cite>/,' ' + url_mini)
            #tmp1=$1
          end
          tmp.gsub!(/(.+?)您的广告/,'')
          if tmp=~/赞助商链接/
            tmp.gsub!(/赞助商链接.+?<ol.+?<\/ol>/,' ')
          end
          tmp.gsub!(/更多有关货币兑换的信息。/,"")
          tmp.gsub!(/<br>/i," ")
          tmp.gsub!(/(.*秒))|\s+/i,' ')
          if tmp.bytesize > 30 || word =~ /^.?13.{9}$/ || tmp =~ /小提示/ then
            re=tmp
          else
            do1=true
          end
        else
          do1=true
        end
        if do1
          puts '+普通搜索+'
          if html.match(/<div class=f .*?><h3 class="r"><nobr>.*?<\/nobr>(.*?)<!--n--><!--m-->.*?<li class="g"><div class="vsc" sig="U2O">/)
            re = "#$1 #$2"
          else
            html.match(/<div id="search"><div id="ires"(.*?)(<a href="\/url\?q=https?:\/\/[^\s]*?)">?(.*?)<span class="st">(.*?)<\/span>/i)
            #~ puts "$1=#{$1}\n$2=#{$2}\n$3=#{$3}\n$4=#{$4}\n$5=#{$5}"
            #url= $2.to_s
            re = $4.to_s + $5.to_s #+ $3.to_s.sub(/.*?>/i,'')
          end

          #if url =~ /https?:\/\/(.*?)(https?:\/\/.+?)/i
            #puts '清理二次http'
            #url=$2.to_s
          #end
          return if re.bytesize < 3
          re = url_mini + ' ' + re
        end
      re.gsub!(/<.*?>/i,'')
      re.gsub!(/\[\s翻译此页\s\]/,'')
      re= re.unescapeHTML
    }

    return unless re
    return if re.bytesize < url_mini.bytesize + 3
    return re
end

#getGoogle_tran(word) ⇒ Object


273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/dic.rb', line 273

def getGoogle_tran(word)
  if word.force_encoding("ASCII-8BIT") =~ CN_re #有中文
    flg = 'zh-CN%7cen'
    #flg = '#auto|en|' + word ; puts '中文>英文'
  else
    flg = 'auto%7czh-CN'
    #flg = '#auto|zh-CN|' + word
  end
  word = URI.escape(word)
  #url = "http://66.249.89.100/translate_t?hl=zh-CN#{flg}"
  url = "http://translate.google.com/translate_a/t?client=firefox-a&text=#{word}&langpair=#{flg}&ie=UTF-8&oe=UTF-8"
  uri = URI.parse(url)
  uri.open(
           'Accept'=>'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*',
           'Accept'=>'text/html',
           'Referer'=> URI.escape(url)
           #'Accept-Language'=>'zh-cn',
           #'Cookie' => cookie,
           #'Range' => 'bytes=0-8000',
           #'User-Agent'=> UserAgent
           ){
    |f|
    return f.read.match(/"trans":"(.*?)","/)[1]
    #re = f.read[0,5059].force_encoding('utf-8').gsub(/\s+/,' ').gb_to_utf8
    #re.gsub!(/<.*?>/i,'')
    #return re.unescapeHTML
  }

  #Net::HTTP.start('translate.google.com') {|http|
  #resp = http.get("/translate_a/t?client=firefox-a&text=#{word}&langpair=#{flg}&ie=UTF-8&oe=UTF-8", nil)
  #p resp.body
  #return resp.body
  #}
end

#getgoogleDefine(word) ⇒ Object


650
651
652
653
654
655
656
657
658
659
# File 'lib/dic.rb', line 650

def getgoogleDefine(word)
  s = Google::Search::Web.new do |a|
    a.query = word
  end
  #s.find.each{|x| return x.content.gsub!(/<.*?>/,'|') }
  return s.find.each{|x| break x.content }.gsub(/<.*?>/,'|')
    .unescapeHTML
rescue
  return ' not defined.'
end

#getjd_price(url) ⇒ Object


350
351
352
353
354
355
356
357
358
359
# File 'lib/dic.rb', line 350

def getjd_price url
  uri = URI.parse(url)
  uri.open(
    #'Accept'=>'text/html , application/*',
    'Range' => 'bytes=0-8999',
    #'Cookie' => cookie,
  ){ |f|
    return JSON.parse(f.read[0,8800])[0]['p'] rescue log('')
  }
end

#getProvince(domain) ⇒ Object

取省


865
866
867
# File 'lib/dic.rb', line 865

def getProvince(domain)#取省
  hostA(domain).gsub(/^.*(\s|省)/,'').match(/\s?(.*?)市/)[1]
end

#getPY(c) ⇒ Object


583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/dic.rb', line 583

def getPY(c)
  p 'getPY'
  c=' '+ c
  c.gsub!(/\sfirefox(.*?)\s/i,' huohuliulanqi ')
  c.gsub!(/\subuntu/i,' wu ban tu ')
  c.gsub!(/\sopen(.*?)\s/i,' ')
  c.gsub!(/\s(xubuntu|fedora)/i,' ')
  c.gsub!(/\s[A-Z](.*?)\s/,' ')
  if c =~ /\skubuntu/i
    needAddKub=true
    c.gsub!(/\skubuntu/i,' ')
  end
  #re = google_py(c)
  re = youdao_py(c)
  re = re + ' Kubuntu' if needAddKub==true
  re.gsub!(/还原/i,'换源')

  if re=~ CN_re#是中文才返回
    return re
  end
end

#gettaobao(url) ⇒ Object


334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/dic.rb', line 334

def gettaobao url
  doc = Nokogiri::HTML(open(url))
  doc.encoding = 'utf-8'

  case url
  when /taobao/i
    title = doc.css('.tb-item-title').text.strip
    price = doc.css('em.tb-rmb-num').first.text
  when /tmall/i
    title = doc.at('.tb-detail-hd').text.strip rescue doc.title
    price = doc.css('.J_originalPrice').first.text.strip rescue ''
  end

  "#{title } 价格:#{price}"
end

#gettitle(url, proxy = true, mechanize = 1) ⇒ Object

取标题,参数是url.


362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/dic.rb', line 362

def gettitle(url,proxy=true,mechanize=1)
  if not proxy and url =~  %r'^http://detail\.tmall\.com/item\.htm'i 
    return gettaobao url 
  end
  jg = nil
  if not proxy and url =~ %r'^http://item\.jd\.com/(\d+)\.html'i
    sjd = "http://p.3.cn/prices/get?skuid=J_#{$1}&type=1"
    jg = getjd_price sjd
  end
  timeout=7
  title = ''
  charset = ''
  if url.b =~ CN_re
    url = URI.encode(url)
  end

  if mechanize == 1
    mechanize = false if url =~ $urlNoMechanize
  else
    mechanize = true
  end
  mechanize = true if url =~ /www\.google\.com/i
  mechanize = true if url =~ $urlProxy
  mechanize = true if proxy
  print ' mechanize:' , mechanize , ' ' , url ,10.chr unless mechanize

  return gettitle_openURI url if not mechanize

  #用代理加快速度
  agent = Mechanize.new

  print ' proxy:' , proxy
  if proxy and url !~ /\.jetbrains\./i
    if $proxy_status_ok
      agent.set_proxy($proxy_addr2,$proxy_port2)
    else
      agent.set_proxy($proxy_addr,$proxy_port)
    end
  end
  agent.user_agent_alias = 'Linux Mozilla'
  agent.max_history = 0
  agent.open_timeout = timeout
  agent.read_timeout = timeout
  #agent.cookies
  #agent.auth('^k^', 'password')

  begin
    page = agent.head(url)
    #File.new('/tmp/h.x','wb').puts page.header
    type = page.header['content-type']
    #print 'get head ok: '
    if type =~ /image\/./i
      showpic(url)
      return type
    end
    if type and type !~ /^$|text\/html/i
      re = page.response.select{|x| x=~/^conten/i }
        .map{|x,y| "#{x}=#{y}" }.join(" ; ")
        .gsub(/content-/i,'')
      p re
      return if re =~ /length=\d\D/i
      return re.gsub(/(length=)(\d+)/i){ "长度="+Filesize.from($2+'b').pretty }
    end
  rescue
    print 'err in get head: '
    p $!
    case $!
    when Mechanize::ResponseCodeError
      if $!.message !~ /^403/ and proxy and $proxy_status_ok
        #sleep timeout
        #return $!.message + 'in get head'
      end
    end
  end

  begin
    page = agent.get(url)
    #File.new('/tmp/a.x','wb').puts page.title
    #File.new('/tmp/b.x','wb').puts Mechanize.new.get_file url
    if page.class != Mechanize::Page
      p 'no page'
      return
    end
    title = page.title
    title = nil if title.empty?
    charset= guess_charset(title)
    charset='GB18030' if charset =~ /^IBM855|windows-1252/i

    if charset and charset !~ /#@charset/i
      title = title.code_a2b(charset,@charset) rescue title
    end
    title = title.unescapeHTML
    auth = page.at('.postauthor').text.strip rescue nil
    title << " zz: #{auth} " if auth
    [ '.tb-rmb-num' , '.priceLarge' ,'.tm-price', '.price' ] .each {|x|
      break if jg
      jg = page.at(x).text.strip rescue nil
    }
    jg = nil if url =~ /\.douban\.com/i
    if jg and jg != ''
      title << " pp: #{jg[0,24]} "
    end
    return title[0,300] if title
  rescue
    print " err in get url:"
    log ''
    case $!
    when Mechanize::ResponseCodeError
      sleep timeout
      return $!.message if $!.message !~ /^403/
    end
  end

  gettitle_openURI url
rescue Exception
  log ''
  $!.message
end

#gettitle_openURI(url) ⇒ Object


481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/dic.rb', line 481

def gettitle_openURI url
  print 'U'
  #puts URI.split url
  #  p ' use URI.open '

  timeout = 7
  istxthtml = false
  charset = nil
  tmp =
    begin #加入错误处理
      Timeout.timeout(timeout) {
        $uri = URI.parse(url)
        $uri.open(
          #'Accept'=>'text/html , application/*',
          'Range' => 'bytes=0-8999',
          #'Cookie' => cookie,
        ){ |f|
          case f.content_type
          when /application\/octet-stream/i
            istxthtml = false
          when /image\/./i
            showpic(url)
            istxthtml = false
          when /text\/html|application\//i
            #p f.content_type
            istxthtml= true
          end

          return f.content_type unless istxthtml
          charset= f.charset          # "iso-8859-1"
          f.read[0,9800].gsub(/\s+/,' ')
        }
      }
    rescue Timeout::Error
      sleep timeout
      return "取标题超时 #{$!.message}"
    rescue
      p ' err in URI.open '
      p $!
      if $!.message =~ /Connection reset by peer/ && $proxy_status_ok
				p ' need pass wall '
				return
      end
      sleep timeout
      return "取标题 #{$!.message[0,210] }"
    end

  return unless istxthtml

  title = tmp.match(/<title.*?>(.*?)<\/title>/i)[1] rescue ''

  if title.empty?
    p tmp
    if tmp.match(/meta\shttp-equiv="refresh(?:.*?)url=(.*?)">/i)
      p 'refresh..'
      return Timeout.timeout(timeout){
        url = $1
        url = "http://#{$uri.host}/#{url}" if url !~ /^http/i
        gettitle(url)
      }
    end
  end

  #return if title =~ /index of/i
  charset= guess_charset(title)
  charset='GB18030' if charset =~ /^IBM855|windows-1252/i

  if charset !~ /#@charset/i
    title = title.code_a2b(charset,@charset) rescue title
  end

  return '取标题: no title' if title.empty?
  title = title.unescapeHTML rescue title
  title
end

#gettitleA(url, from = "_", proxy = true) ⇒ Object


558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'lib/dic.rb', line 558

def gettitleA(url,from="_",proxy=true)
  $last_url = url

  ti=nil
  begin
    ti=Timeout.timeout(19){gettitle(url,proxy)}
  rescue Timeout::Error
    Thread.pass
    p 'get title Time out'
    return
  end

  return if ti.empty?

  #检测是否有其它取标题机器人
  #
  return "#{from}: ⇪ #{ti} "  if ti !~ $tiList and url !~ $urlList
  #登录 • Ubuntu中文论坛
  if ti
    ti.gsub!(/登录 •/, '水区水贴? ')
    return " \x033⇪ fw: #{ti}\x030" if proxy
    return " \x033⇪ ti: #{ti}\x030"
  end
end

#geturl(url, type = 1) ⇒ Object


631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# File 'lib/dic.rb', line 631

def geturl(url,type=1)
  agent = Mechanize.new
  agent.user_agent_alias = 'Linux Mozilla'
  agent.max_history = 1
  agent.open_timeout = 12
  #agent.cookies
  begin
    page = agent.get_file(url)
  rescue
    return $!.message[0,60] + ' . IN geturl.'
  end
  puts page
  s = page.force_encoding('utf-8').match(/您是不是要找.*?<strong>(.*?)<\/strong>/im)[1]
  s.gsub!(/\s+/,' ')
  #puts s
  s.gsub!(/<.*?>/,'')#.unescapeHTML.gb_to_utf8
  s
end

#ggObject Also known as: 公告

公告


992
993
994
995
996
# File 'lib/dic.rb', line 992

def gg
  t=Time.now
#http://logs.ubuntu-eu.org/free/#{t.strftime('%Y/%m/%d')}/%23ubuntu-cn.html
" 当前log地址是 : http://irclogs.ubuntu.com/#{t.strftime('%Y/%m/%d')}/%23ubuntu-cn.html 有需要请浏览 #{t.strftime('%H:%M:%S')} "
end

#google_py(word) ⇒ Object


609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/dic.rb', line 609

def google_py(word)
  p 'google_py'
    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 $1.to_s.unescapeHTML
    }
end

#guess(s) ⇒ Object


78
79
80
# File 'lib/utf.rb', line 78

def guess(s)
  CharDet.detect(s)['encoding'].upcase
end

#guess_charset(str) ⇒ Object

字符串编码集猜测


140
141
142
143
144
145
146
147
148
# File 'lib/dic.rb', line 140

def guess_charset(str)
  return if str.empty?
   s=str.gsub(/[\x0-\x7f]/,'') rescue str.clone
  return if s.bytesize < 6
  while s.bytesize < 25
    s << s
  end
  return guess(s) rescue nil
end

#hello_replay(sSay) ⇒ Object


1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
# File 'lib/dic.rb', line 1041

def hello_replay(sSay)
	tmp = Time.parse('2015-02-18')-Time.now #除夕

  #不用显示倒计时
  if tmp < 0 or tmp > Oneday*60 or rand < 0.16
		return sSay if sSay =~ /\s$/
		return "#{sSay} \0039 #{chr_hour} "
	end

  a=''
	case tmp
	when 0..60
		a.prepend "#{tmp}"
	when 61..3600
		a.prepend "#{tmp/60}分钟 "
	when 3601..86400
		a.prepend "#{tmp/60/60}小时 "
	else
		a.prepend "#{tmp/60/60/24}"
	end
	a.gsub!(/([\.?\d]+)/){ "%.2f" % $1}
	"#{sSay} #{chr_hour} \0039新年快乐 : #{a}\017"
end

#helpObject


109
110
111
# File 'lib/dic.rb', line 109

def help
  Help
end

#host(domain) ⇒ Object

域名转化为IP


859
860
861
862
863
864
# File 'lib/dic.rb', line 859

def host(domain)
  return 'IPV6' if domain =~ /^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i
  domain.gsub!(/\/.*/i,'')
  return domain unless domain.include?('.')
  return Resolv.getaddress(domain) rescue domain
end

#hostA(domain, hideip = true) ⇒ Object

取IP或域名的地理位置 hostA('www.g.cn’,true)


871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# File 'lib/dic.rb', line 871

def hostA(domain,hideip=true)#处理IP 或域名
  return nil if !domain
  if domain=~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
    tmp = $1
  else
    #print " ip: ",domain ,10.chr
    tmp = domain.host
  end
  rtn=" "
  rtn.prepend tmp unless hideip
  tmp = IpLocationSeeker.new.seek tmp rescue tmp
  rtn << tmp

  rtn.gsub!(/CZ88\.NET/i,'')
  rtn.gsub!(/IANA/i,'不在宇宙')
  rtn.gsub(/\s+/,'').to_s + ' '
end

#init_dicObject


117
118
119
120
121
122
123
124
# File 'lib/dic.rb', line 117

def init_dic
  a=Time.at 0
  $Lsay=a
  $Lping=a
  $min_next_say = a
  $last_save=a
  $proxy_status_ok = false
end

#isaid(second = 0) ⇒ Object

记录自己说话的时间


1142
1143
1144
# File 'lib/dic.rb', line 1142

def isaid(second=0)
	$min_next_say=Time.now + $minsaytime + second
end

#loadDicObject


158
159
160
161
# File 'lib/dic.rb', line 158

def loadDic
  $str1 = open('U.txt').read rescue ''
  puts 'Dic load [ok]'
end

#log(s = nil) ⇒ Object

log => 写入 $!.message log “aa” => 写入 “aa” 到log文件 log '' => 不写入log文件


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/log.rb', line 61

def log(s=nil)
  if not s
      if $!
         s = "#{$!.message} && #{[email protected].join("\n")}"
      else
         return
      end
  elsif s.class != String
    s=s.inspect
  end

   if s.empty?
     if $!
       p $!.message
       puts "#{[email protected].select{|x| x !~/\/lib\/ruby\//i }.join("\n")}"
     end
     return
   else
     s=s.inspect if s.class != String
   end

   if $!
     p $!.message
      f= Myname + '-err.log'
      #p f
      f = Logger::LogDevice.new(File.join($logDir,f))
      le = Logger.new(f ,shift_age=30,shift_size = 1200000)
      le.datetime_format = "%m-%d %H:%M:%S"
      le.debug s
      le.close
   else
      f= Myname + '.log'
      #p f
      f = Logger::LogDevice.new(File.join($logDir,f))
      if $zip_log
         l = Logger.new(f , 'daily' )
      else
         l = Logger.new(f ,shift_age =33, shift_size = 1200000)
      end
      l.datetime_format = "%m-%d %H:%M:%S"
      l.info s
      l.close
   end
   nil
end

#log_initObject


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/log.rb', line 45

def log_init
  $log_init.exit if defined? $log_init
  $log_init = Thread.new{
     Thread.current[:name] = 'Log file to cab '
     loop do
        sleep 60
        zip_log rescue(p $!.message,[email protected])
        sleep 1740
     end
  }
end

#onehourObject


911
912
913
# File 'lib/dic.rb', line 911

def onehour
  3600
end

#oneminObject


908
909
910
# File 'lib/dic.rb', line 908

def onemin
  60
end

#osodObject

每日一句英语学习


940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
# File 'lib/dic.rb', line 940

def osod
  return '' if true
  agent = Mechanize.new
  agent.user_agent_alias = 'Linux Mozilla'
  agent.max_history = 0
  agent.open_timeout = 12
  #agent.cookies
  #url = 'http://ppcbook.hongen.com/eng/daily/sentence/0425sent.htm'
  t=Time.now
  m="%02d" % (t.sec%10+3)
  d="%02d" % t.day
  url = "http://ppcbook.hongen.com/eng/daily/sentence/#{m}#{d}sent.htm"
  begin
    page = agent.get_file(url)
  rescue
    return $!.message[0,60] + ' . IN osod'
  end
  s = page.match(/span class="e2">(.*?)<select name='selectmonth'>/mi)[1]
  s = s.gsub!(/\s+/,' ')
  s.gsub!(/<.*?>/,'').unescapeHTML.gb_to_utf8
end

#pr_highlighted(s) ⇒ Object

高亮打印消息


1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'lib/dic.rb', line 1085

def pr_highlighted(s)
  #s=s.force_encoding("utf-8")
  s=s.gb_to_utf8 if @charset !~ /UTF-8/i #如果频道编码不是utf-8,则转换成utf-8

  need_savelog = false
  case s
  when /^:(.+?)!(.+?)@(.+?)\s(.+?)\s((.+?)\s:)?(.+)$/i
    from=$1 || ''
    name=$2;ip=$3;mt=$4;to=$6;sy=$7
    return if $ignore_action =~ /#{Regexp::escape mt}/i
    case mt
    when /privmsg/i
      need_savelog = true
      mt.clear
      to.clear if to == @channel
      sy= sy.yellow if to =~ /#{Regexp::escape @nick}/i
    when /join|part|quit|nick|notice|kick/i
      mt = ' ' << mt[0,4].blue_on_white << ' '
      from << ' ' << ip.getaddr_fromip.underline
      if to =~ /#{Regexp::escape @channel}/i
        to.clear
      end
      need_savelog = true
    else
      #pp s.match(/^:(.+?)!(.+?)@(.+?)\s(.+?)\s((.+)\s:)?(.+)$/i)
      re= s.pink
      mt= ' ' + mt[0,4].blue + ' '
      sy=sy.green
      need_savelog = true
    end

    t = Time.now.strftime('%H%M%S')
    sy.force_encoding('utf-8') rescue sy
    re= "#{t}#{ (( from+':').rjust(13)).c_rand(name.sum)} #{mt}#{to}#{sy}"
  else
    re= s.red
  end
  if $local_charset !~ /UTF-8/i
     puts re.code_a2b('utf-8',$local_charset)
  else
     puts re
  end
  Readline.refresh_line
  savelog re if need_savelog
end

#rand_doObject

随机事件


1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/dic.rb', line 1028

def rand_do
	case rand(1000)
	when 0..130
		$my_s
	when 131..180
		''
	when 300..400
		"..休息一下.. #$my_s"
	else
		''
	end
end

#read_proxy_ruleObject


1072
1073
1074
# File 'lib/dic.rb', line 1072

def read_proxy_rule
  $proxy_rule = File.read('gfwlist.txt').unbase64.split(/\n/)
end

#reload_allObject


150
151
152
153
154
155
156
# File 'lib/dic.rb', line 150

def reload_all
  load 'dic.rb'
	loadDic
	Thread.list.each {|x| puts "#{x.inspect}: #{x[:name]}" }
rescue
  log
end

#roll(n = 100) ⇒ Object

返回roll


930
931
932
# File 'lib/dic.rb', line 930

def roll n=100
  "掷出了: #{rand(n.to_i + 1) } "
end

#safe(level) ⇒ Object


185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dic.rb', line 185

def safe(level)
  result = nil
  Thread.start {
    Thread.current[:name]= 'safe eval thread'
    $SAFE = level
    begin
      result = yield
    rescue Exception
      result = $!
    end
  }.join
  return result
end

#safe_eval(str) ⇒ Object


174
175
176
177
178
179
180
181
182
183
# File 'lib/dic.rb', line 174

def safe_eval(str)
  str.strip!
  log 'eval: ' + str
  if str =~ $eval_black_list
    return eval str
  else
    #return get_sandbox str rescue $!.message
    return get_eval_in str
  end
end

#savelog(s) ⇒ Object

写入聊天记录


1132
1133
1134
1135
1136
1137
1138
1139
# File 'lib/dic.rb', line 1132

def savelog(s)
  return if $not_savelog

	fn= "irclogs/#{@channel[1..-1]}/" + Time.now.strftime("%y%m%d.txt")
	File.open( fn,'a'){ |x|
    x.puts s.clear_color rescue s
	}
end

#saveuObject

保存缓存的users


164
165
166
167
168
169
170
171
172
# File 'lib/dic.rb', line 164

def saveu
  return if Time.now - $last_save < 120
  $last_save = Time.now
  a=File.open("_#{ARGV[0]}.yaml","w")
  a.write $u.to_yaml
  a=File.open("_#{ARGV[0]}.data", 'w')
  a.write $data.to_yaml
  puts ' save u ok'.red
end

#sh30Object

run sh 30 sec


3
4
5
6
7
# File 'lib/showpic.rb', line 3

def sh30
  Timeout.timeout(30){
    Thread.new { system s }
  }
end

#showpic(url) ⇒ Object


9
10
11
12
# File 'lib/showpic.rb', line 9

def showpic(url)
  return if not ENV.has_key? 'DISPLAY'
  sh30 "feh #{url}"
end

#update_proxy_ruleObject


1065
1066
1067
1068
1069
1070
# File 'lib/dic.rb', line 1065

def update_proxy_rule
  File.open('gfwlist.txt','w'){ |x|
    url = "nUE0pQbiY2S1qT9jpz94rF1aMaqfnKA0Yzqio2qfMJAiMTHhL29gY3A2ov90\npaIhnl9aMaqfnKA0YaE4qN==\n".rot13.ub64
    x.write Mechanize.new.get(url).body
  }
end

#win_platform?Boolean

Returns:

  • (Boolean)

5
6
7
# File 'lib/platform.rb', line 5

def win_platform?
	Gem.win_platform?
end

#youdao_py(words) ⇒ Object

拼音转中文


627
628
629
630
# File 'lib/dic.rb', line 627

def youdao_py(words)
  url = "http://www.youdao.com/search?q=#{words}&ue=utf8&keyfrom=web.index"
  geturl(url)
end

#zip_logObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/log.rb', line 22

def zip_log
   return unless $zip_log
   s_log = "#$logDir/#{Myname}.log"
   if File.exist? s_log
      d_now = "#$logDir/#{Myname}.log.7z"
      s_now = "#$logDir/#{Myname}1.log"
      FileUtils.cp(s_log,s_now)
      system("start /min cmd /c 7z a #{d_now} #{s_now}")
   end

   yd = (Time.now-86400).strftime("%Y%m%d")
   s = "#$logDir/#{Myname}.log.#{yd}"
   if File.exist? s
      d1 = "#$logDir\\#{yd}_#{Myname}.log.7z"
      unless File.exist? d1
         system("start /min cmd /c 7z a #{d1} #{s}")
      else
         FileUtils.rm s
      end
      p ' 7z cab log ok '
   end
end