-
Notifications
You must be signed in to change notification settings - Fork 0
/
jibiki.rb
130 lines (78 loc) · 2.72 KB
/
jibiki.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/local/bin/ruby -wKu
=begin
= jibiki.rb version 0.3.0 2001/11/26 - 2002/11/28
Copyright (c) 2001 2002 msnm@zerobase.jp
This program is free software. You can re-distribute and/or
modify this program under the same terms as Ruby itself,
Ruby Distribute License or GNU General Public License.
== What is This?
search quoted words
=end
require 'msnm'
require 'net/http'
class JibikiSessionHandler < Net::InstantMessaging::MSNMessenger::SessionHandler
def handle_message( peer_id, peer_nick, msg_header, msg_body )
if msg_header =~ /\r\nContent-Type: text\/plain/um
search_fmt = '/search?hl=ja&lr=&q=%s'
reply_lines = []
msg_body.split("\r\n").each do |msg_line|
search_words = []
msg_line.scan(/\343\200\214(.+?)\343\200\215/n) do |search_word,|
search_words << search_word
end
search_words.each do |search_word|
search_path = format( search_fmt, urlencode(search_word) )
search_url = 'http://www.google.com' + search_path
hit_count = 0
begin
Net::HTTP::start('www.google.com') do |http|
http.get(search_path) do |html|
if m = /swrnum=(\d+)/.match(html)
hit_count = ( m[-1] || 0 ).to_i
end
end
end
rescue
hit_count = 0
end
reply_lines << format( '%s %s ( %d hit)', search_word, search_url, hit_count )
end
end
unless reply_lines.empty?
queue_message(reply_lines.join("\r\n"))
end
end
end
def urlencode( str )
str.split(//n).collect{|c|format('%%%02X',c[0])}.join('')
end
def on_join( peer_id, peer_nick )
queue_message( 'hello ' + peer_id )
end
end
class JibikiSessionHandlerFactory
def create( msnm, session )
JibikiSessionHandler.new( msnm, session )
end
end
if __FILE__ == $0
USERID = '***@hotmail.com' # account for an IM agent
PASSWD = '***' # password
# start logging
logfile = $stdout
Net::InstantMessaging::MSNMessenger::ProtocolHandler.logfile = logfile
now = Time.now
process_tag = now.strftime('%Y%m%d%H%M%S') + now.usec.to_s
logfile << "process start #{process_tag}\n"
# initialize service
msnm = Net::InstantMessaging::MSNMessenger.new( USERID, PASSWD,
JibikiSessionHandlerFactory.new )
msnm.ns.synchronize
msnm.ns.privacy_mode :BLP_ALLOW
# start service
msnm.ns.online
msnm.listen # -> listening...
msnm.logout
# finish logging
logfile << "process finish #{process_tag}\n"
end