root/trunk/jabberbot.rb

Revision 29, 3.1 kB (checked in by astro, 5 months ago)

state

Line 
1 require 'RMagick'
2 require 'cgi'
3 require 'time'
4 require 'digest/sha1'
5 require 'xmpp4r'
6 require 'xmpp4r/vcard'
7 require 'xmpp4r/roster'
8 require 'rubygems'
9
10 $: << 'lib'
11 require 'blog'
12 Jabber::debug=true
13
14 def check_vcard(cl, jid, force_load=false)
15   puts "check_vcard(..., #{jid.inspect}, #{force_load.inspect})"
16   js = JabberUser.select(:jid=>jid.to_s)
17   js.first.delete if js.first and force_load
18
19   if js.first.nil? or force_load
20     begin
21       vcard = Jabber::Vcard::Helper::get(cl, jid)
22       j = JabberUser.new
23       j.jid = jid.to_s
24       j.jid_sha = Digest::SHA1.new(jid.to_s).hexdigest
25       j.nickname = vcard['NICKNAME'] || vcard['FN'] || jid.node.to_s
26       j.url = vcard['URL']
27       j.photo_mime = vcard['PHOTO/TYPE']
28
29       begin
30         binval = Base64::decode64(vcard['PHOTO/BINVAL'].to_s)
31         puts "Size of photo of #{jid}: #{binval.size}"
32         photo = Magick::Image.from_blob(binval)[0]
33         w = photo.columns
34         w = 64 if w > 64
35         h = photo.rows
36         h = 64 if h > 64
37         photo.crop_resized!(w, h)
38         j.photo = Base64::encode64(photo.to_blob)
39         j.photo_w = photo.columns
40         j.photo_h = photo.rows
41       rescue Exception => e
42         puts "Error in PHOTO: #{e.class}: #{e.to_s}\n#{e.backtrace.join("\n")}"
43         j.photo = nil
44         j.photo_w = 0
45         j.photo_h = 0
46       end
47
48       j.write
49
50       "Hello #{j.nickname}"
51     rescue Exception => e
52       e.to_s + " at " + e.backtrace.first
53     end
54   end
55 end
56
57 blog = Blog.new('config.yaml')
58 #Jabber::debug = true
59 cl = Jabber::Client.new(blog.jabberconf['jid'])
60 cl.connect
61 cl.auth(blog.jabberconf['password'])
62 cl.on_exception do
63   exit
64 end
65 roster = Jabber::Roster::Helper.new(cl)
66 roster.add_subscription_request_callback { |item,presence|
67         roster.accept_subscription(presence.from)
68 }
69
70 cl.add_message_callback do |msg|
71   if msg.type != :error
72     Thread.new do
73       cmd, arg = msg.body.to_s.split(/ /, 2)
74
75       reply = msg.answer(false)
76       reply.type = msg.type
77       reply.body = case cmd
78         when 'refresh-vcard'
79           check_vcard(cl, msg.from.strip, true)
80         when 'comment'
81           url, text = arg.split(/[ \n]/, 2)
82           param = {}
83           CGI::parse(url.split('?', 2).last.to_s).each { |k,v| param[k] = v.first }
84           entry = Entry.by_id(param['entry'].to_s)
85           if entry
86             comment = Comment.new
87             comment.entry_id = entry.entry_id
88             comment.date = Time.now.xmlschema
89             comment.address = "xmpp:#{msg.from.strip}"
90             comment.text = text.to_s.strip
91             comment.write
92
93             'Thanks very much!'
94           else
95             puts "No entry_id #{param['entry'].inspect}"
96             "Error: no such entry."
97           end
98         when 'help'
99           "Commands available: help, refresh-vcard"
100         else
101           "Try \"help\""
102       end
103
104       check_vcard(cl, msg.from.strip)
105       cl.send(reply)
106     end
107   end
108 end
109
110 cl.send(Jabber::Presence.new(nil, 'Astroblog2 in public beta now.'))
111
112 comment_addresses = []
113 Comment.select.each { |c|
114   comment_addresses << c.address.sub(/^xmpp:/, '').to_s
115 }
116 comment_addresses.uniq!
117 comment_addresses.each { |a|
118   check_vcard(cl, a, true)
119 }
120
121 loop do
122   cl.send " \t\n"
123   sleep 60
124 end
Note: See TracBrowser for help on using the browser.