Permalink
Browse files

Added karma system and correct the msg issue

  • Loading branch information...
1 parent 0695825 commit 623da50d93ba145ba2c4c7f1dec896abb6e58e7f @hemanth committed Nov 4, 2010
Showing with 45 additions and 15 deletions.
  1. +45 −15 rgbot.rb
View
60 rgbot.rb
@@ -1,32 +1,40 @@
require 'rubygems'
require 'xmpp4r-simple'
require 'open-uri'
+ require 'net/http'
+ require 'json'
+ require 'httpclient'
+ require 'dbm'
+
class L33ty
+
def initialize(bot_name,bot_password)
@bot = bot_name
@pass = bot_password
@jabber = Jabber::Simple.new(@bot+'@gmail.com',@pass)
end
-
- def invoke(msg)
+
+ def invoke(msg)
restricted_methods=['main','invoke','deliver']
- meth=msg.body.split()[0]
- self.send(meth,msg) if self.respond_to?(meth) and !restricted_methods.include?(meth)
+ meth=@msg.body.split()[0]
+ @msg.body=@msg.body.sub(/\w+\s*/, '')
+ self.send(meth,@msg) if self.respond_to?(meth) and !restricted_methods.include?(meth)
end
-
+
def deliver(msg,res)
- @jabber.deliver(msg.from.node+"@gmail.com",res)
+ @jabber.deliver(@msg.from.node+"@gmail.com",res)
end
def l33t(msg)
- self.deliver(msg,open('http://nyhacker.org/~hemanth.hm/hacks/t.php?'+ URI.escape(msg.body, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))).read())
+ puts URI.escape(@msg.body, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
+ self.deliver(@msg,open('http://nyhacker.org/~hemanth.hm/hacks/t.php?'+ URI.escape(@msg.body, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))).read())
end
-
+
def goog(msg)
- gurl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+URI.escape(msg.body, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
- self.deliver(msg,JSON.parse(Net::HTTP.get_response(URI.parse(gurl)).body)['responseData']['results'][0]['url'])
+ gurl = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="+URI.escape(@msg.body, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
+ self.deliver(@msg,JSON.parse(Net::HTTP.get_response(URI.parse(gurl)).body)['responseData']['results'][0]['url'])
end
def xkcd(msg)
@@ -36,20 +44,42 @@ def xkcd(msg)
res = http.start { |server|
server.request(req)
}
- self.deliver(msg,res["location"]+ " Enjoy it!")
+ self.deliver(@msg,res["location"]+ " Enjoy it!")
end
+
+ def karma(msg)
+ kdb = DBM.open("karma.db")
+ whom,what = @msg.body.split(/(?=\+\+)|(?=\-\-)/)
+ puts whom
+ if(kdb[whom] == nil)
+ kdb[whom]=0
+ self.deliver(@msg,"New avatra, your karma is 0")
+ elsif(whom <=> @msg.from.node)
+ kdb[whom]=kdb[whom].to_i-1
+ self.deliver(@msg,"Very smart! your karma is : "+kdb[whom])
+ elsif(what <=> "++")
+ kdb[whom]=kdb[whom].to_i+1
+ self.deliver(@msg,"Karma is : "+kdb[whom])
+ elsif(what <=> "--")
+ kdb[whom]=kdb[whom].to_i-1
+ self.deliver(@msg,"Karma is : "+kdb[whom])
+ else
+ self.deliver(@msg,"Wrong useage!")
+ end
+ end
+
def main
while (true) do
- @jabber.received_messages do |msg|
- File.open('log', 'w') {|log| log.write(msg.from.node+" : "+msg.body)}
- self.invoke(msg)
+ @jabber.received_messages do |@msg|
+ File.open('log', 'w') {|log| log.write(@msg.from.node+" : "+@msg.body)}
+ self.invoke(@msg)
end
end
sleep(1)
end
end
-l33t = L33ty.new('gmail_user_name','password')
+l33t = L33ty.new('gmail_user_id','password')
l33t.main()

0 comments on commit 623da50

Please sign in to comment.