Advertisement
Guest User

Untitled

a guest
Aug 15th, 2011
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.81 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'xmpp4r'
  3. require 'xmpp4r/muc/helper/simplemucclient'
  4.  
  5. # Print a line formatted depending on time.nil?
  6. def print_line(time, line)
  7.   if time.nil?
  8.     puts line
  9.   else
  10.     puts "#{time.strftime('%I:%M')} #{line}"
  11.   end
  12. end
  13.  
  14.  
  15. #Jabber::debug = true
  16. cl = Jabber::Client.new(Jabber::JID.new(ENV['JID']))
  17. cl.connect
  18. cl.auth(ENV['JPASSWORD'])
  19.  
  20. # For waking up...
  21. mainthread = Thread.current
  22.  
  23. # This is the SimpleMUCClient helper!
  24. m = Jabber::MUC::SimpleMUCClient.new(cl)
  25.  
  26. # SimpleMUCClient callback-blocks
  27.  
  28. m.on_join { |time,nick|
  29.   print_line time, "#{nick} has joined!"
  30.   puts "Users: " + m.roster.keys.join(', ')
  31. }
  32. m.on_leave { |time,nick|
  33.   print_line time, "#{nick} has left!"
  34. }
  35.  
  36. m.on_message { |time,nick,text|
  37.   print_line time, "<#{nick}> #{text}"
  38.  
  39.   # Avoid reacting on messaged delivered as room history
  40.   unless time
  41.     # Bot: invite astro@spaceboyz.net
  42.     if text.strip =~ /^(.+?): invite (.+)$/
  43.       jid = $2
  44.       if $1.downcase == m.jid.resource.downcase
  45.         m.invite(jid => "Inviting you on behalf of #{nick}")
  46.         m.say("Inviting #{jid}...")
  47.       end
  48.     # Bot: subject This is room is powered by XMPP4R
  49.     elsif text.strip =~ /^(.+?): subject (.+)$/
  50.       if $1.downcase == m.jid.resource.downcase
  51.         m.subject = $2
  52.       end
  53.     # Bot: exit please
  54.     elsif text.strip =~ /^(.+?): exit please$/
  55.       if $1.downcase == m.jid.resource.downcase
  56.         puts "exiting"
  57.         m.exit "Exiting on behalf of #{nick}"
  58.         mainthread.wakeup
  59.       end
  60.     end
  61.   end
  62. }
  63. m.on_room_message { |time,text|
  64.   print_line time, "- #{text}"
  65. }
  66. m.on_subject { |time,nick,subject|
  67.   print_line time, "*** (#{nick}) #{subject}"
  68. }
  69.  
  70. m.join("conf@conference.jabber.org/rbot")
  71.  
  72. # Wait for being waken up by m.on_message
  73. Thread.stop
  74.  
  75. cl.close
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement