unexist.dev

Changeset 332:f9600f0746c8

Updated multichannel handling
author unexist
date Fri, 30 Nov 2012 21:20:34 +0100
parents fc767a61f98b
children 8ce7a8235034
files bot/cinch.rb
diffstat 1 files changed, 81 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/bot/cinch.rb	Wed Sep 05 16:02:40 2012 +0200
+++ b/bot/cinch.rb	Fri Nov 30 21:20:34 2012 +0100
@@ -126,19 +126,19 @@
     match "builds", method: :builds
     def builds(m)
       ret = xmlrpc_buildbot(21600) #< Last 6 hours
-      m.reply ret if ret
+      m.reply(ret) if ret
     end
 
     match "buildbots", method: :buildbots
     def buildbots(m)
       ret = xmlrpc_buildbot_status
-      m.reply ret if ret
+      m.reply(ret) if ret
     end
 
     match "sublets", method: :sublets
     def sublets(m)
       ret = xmlrpc_sur(604800) #< Last week
-      m.reply ret if ret
+      m.reply(ret) if ret
     end
 
     timer INTERVAL, method: :stats
@@ -220,14 +220,14 @@
       if 0 == score.score
         score.destroy!
 
-        m.reply "Zeroed out", true
+        m.reply("Zeroed out", true)
       else
         score.save
 
-        m.reply "Score of %s is now %d." % [ key, score.score ], true
+        m.reply("Score of %s is now %d." % [ key, score.score ], true)
       end
     rescue => error
-      m.reply "Oops something went wrong.."
+      m.reply("Oops something went wrong..")
       raise
     end
 
@@ -237,10 +237,10 @@
       score  = Score.first(:name.like => lookup)
 
       unless score.nil?
-        m.reply "Score of %s is %d." % [ lookup, score.score ], true
+        m.reply("Score of %s is %d." % [ lookup, score.score ], true)
       end
     rescue => error
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end
 
@@ -262,11 +262,11 @@
         end
 
         unless matches.empty?
-          m.reply matches.join(", "), true
+          m.reply(matches.join(", "), true)
         end
       end
     rescue => error
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end
   end # }}}
@@ -295,7 +295,7 @@
       title  = page.title.gsub(/[\x00-\x1f]*/, "").gsub(/[ ]{2,}/, " ").strip
       status = page.search("//td[@class='status']").inner_html
 
-      m.reply "#%s: [%s] %s (%s)" % [ id, status, title, page.uri.to_s ]
+      m.reply("#%s: [%s] %s (%s)" % [ id, status, title, page.uri.to_s ])
     end
   end # }}}
 
@@ -330,7 +330,7 @@
         out << "%s (http://subforge.org%s)" % [ title, uri ]
       end
 
-      m.reply out.join(", ")
+      m.reply(out.join(", "))
     end
   end # }}}
 
@@ -350,7 +350,7 @@
       page  = @agent.get("http://downforeveryoneorjustme.com/%s" % [ uri ])
       state = page.search("//div[@id='container']").text.strip.split("\n").first rescue nil
 
-      m.reply state, true unless state.nil?
+      m.reply(state, true) unless state.nil?
     end
   end # }}}
 
@@ -370,7 +370,7 @@
       # Fetch commit message
       page = @agent.get("http://www.classnamer.com/")
 
-      m.reply page.search("//p[@id='classname']").text
+      m.reply(page.search("//p[@id='classname']").text)
     end
   end # }}}
 
@@ -390,7 +390,7 @@
       # Fetch commit message
       page = @agent.get("http://whatthecommit.com")
 
-      m.reply CGI.unescapeHTML(page.parser.xpath("//div/p").first.inner_html.chop)
+      m.reply(CGI.unescapeHTML(page.parser.xpath("//div/p").first.inner_html.chop))
     end
   end # }}}
 
@@ -417,9 +417,9 @@
           page = @agent.get(link)
         rescue Mechanize::ResponseCodeError
           if "www.youtube.com" == uri.host
-            m.reply "Thank you, GEMA!"
+            m.reply("Thank you, GEMA!")
           else
-            m.reply "Y U POST BROKEN LINKS?", true
+            m.reply("Y U POST BROKEN LINKS?", true)
           end
 
           next
@@ -435,9 +435,9 @@
             rating = page.search("//strong/span[@itemprop='ratingValue']").text
             votes  = page.search("//a/span[@itemprop='ratingCount']").text
 
-            m.reply "Title: %s (at %s, %s/10 from %s users)" % [
+            m.reply("Title: %s (at %s, %s/10 from %s users)" % [
               title, uri.host, rating, votes
-            ]
+            ])
           when "www.youtube.com"
             # Reload with nofeather
             page = @agent.get(link + "&nofeather=True")
@@ -450,9 +450,9 @@
             likes = page.search("//span[@class='watch-likes-dislikes']")
             likes = likes.text.gsub(/[.,]/, "")
 
-            m.reply "Title: %s (at %s, %s hits, %s)" % [
+            m.reply("Title: %s (at %s, %s hits, %s)" % [
               title, uri.host, hits, likes.strip
-            ]
+            ])
           when "gist.github.com"
             # Get owner
             owner = page.search("//div[@class='name']/a").inner_html
@@ -461,26 +461,26 @@
             age = Time.parse(page.search("//span[@class='date']/abbr").text)
             age = age.strftime("%Y-%m-%d %H:%M")
 
-            m.reply "Title: %s (at %s, %s on %s)" % [
+            m.reply("Title: %s (at %s, %s on %s)" % [
               title, uri.host, owner, age
-            ]
+            ])
           when "pastie.org"
             # Get time
             age = Time.parse(page.search("//span[@class='typo_date']").text)
             age = age.strftime("%Y-%m-%d %H:%M")
 
-            m.reply "Title: %s (at %s, on %s)" % [
+            m.reply("Title: %s (at %s, on %s)" % [
               title, uri.host, age
-            ]
+            ])
           when "subforge.org", "subtle.de"
-            m.reply "Title: %s (at %s)" % [ title, uri.host ]
+            m.reply("Title: %s (at %s)" % [ title, uri.host ])
           when "twitter.com"
             if link =~ /!\/.+?\/status\/(\d+)$/
               json      = @agent.get("https://twitter.com/statuses/show/#{$1}.json").body
               tweet     = JSON.parse(json)
               unescaped = CGI.unescapeHTML(tweet["text"])
 
-              m.reply "@%s: %s" % [ tweet["user"]["screen_name"], unescaped ]
+              m.reply("@%s: %s" % [ tweet["user"]["screen_name"], unescaped ])
             end
           else
             # Check if we know it
@@ -495,11 +495,11 @@
               )
               u.save
 
-              m.reply "Title: %s (at %s)" % [ title, uri.host ] if title
+              m.reply("Title: %s (at %s)" % [ title, uri.host ]) if title
             else
-              m.reply "Oooold! (First seen by %s on %s)" % [
+              m.reply("Oooold! (First seen by %s on %s)" % [
                 u.nick, u.created_at.strftime("%Y-%m-%d %H:%M")
-              ], true
+              ], true)
             end
           end
       end
@@ -518,7 +518,7 @@
     match /^!rem (.+) ([+-]?)= (.+)/, method: :store_phrase, use_prefix: false # {{{
     def store_phrase(m, key, op, value)
       lookup = key.downcase.strip
-      phrase = Phrase.first(:name => lookup)
+      phrase = Phrase.first(:name => lookup, :channel => m.channel.name)
 
       if phrase.nil? #< New phrase
         phrase = Phrase.new(
@@ -563,17 +563,17 @@
         end
       end
 
-      m.reply "Thank you, sir.", true
+      m.reply("Thank you, sir.", true)
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end # }}}
 
     match /^([0-9]*)?\!give ([^ ]+) ([^ ]+)((?:\s(?:[^ ]*))*)/, method: :give_phrase, use_prefix: false # {{{
     def give_phrase(m, version, target, key, args)
-      phrase, *args = phrase_args(key, args)
+      phrase, *args = phrase_args(key, m.channel.name, args)
 
-      unless(phrase.nil?)
+      unless phrase.nil?
         # Get phrase
         if not version.empty? and phrase.has_version?(version.to_i)
           v = phrase.specific_version(version.to_i)
@@ -583,7 +583,7 @@
 
         m.reply "%s: %s" % [ target, replace_args(v.value, args) ]
       else
-        m.reply "Did you mean: %s" % [ find_alike(key, 5) ], true
+        m.reply "Did you mean: %s" % [ find_alike(key, m.channel.name, 5) ], true
       end
     rescue
       m.reply "Oops something went wrong..", true
@@ -592,9 +592,9 @@
 
     match /^([0-9]*)?\!msg ([^ ]+) ([^ ]+)((?:\s(?:[^ ]*))*)/, method: :msg_phrase, use_prefix: false # {{{
     def msg_phrase(m, version, target, key, args)
-      phrase, *args = phrase_args(key, args)
+      phrase, *args = phrase_args(key, m.channel.name, args)
 
-      unless(phrase.nil?)
+      unless phrase.nil?
         # Get phrase
         if !version.empty? and phrase.has_version?(version.to_i)
           v = phrase.specific_version(version.to_i)
@@ -605,26 +605,26 @@
         user = User(target)
         user.msg "%s" % replace_args(v.value, args)
       else
-        m.reply "Did you mean: %s" % [ find_alike(key, 5) ], true
+        m.reply("Did you mean: %s" % [ find_alike(key, m.channel.name, 5) ], true)
       end
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end # }}}
 
     match /^!find (.+)/, method: :find_phrase, use_prefix: false # {{{
     def find_phrase(m, key)
-      likes = find_alike(key)
-      m.reply "Matches for %s: %s" % [ key, likes ], true unless(likes.empty?)
+      likes = find_alike(key, m.channel.name)
+      m.reply("Matches for %s: %s" % [ key, likes ], true) unless likes.empty?
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end # }}}
 
     match /^(\d+)?!forget (.+)/, method: :forget_phrase, use_prefix: false # {{{
     def forget_phrase(m, version, key)
       lookup = key.downcase.strip
-      phrase = Phrase.first(:name => lookup)
+      phrase = Phrase.first(:name => lookup, :channel => m.channel.name)
 
       unless phrase.nil?
         # Get phrase
@@ -639,23 +639,23 @@
           v.destroy!
 
           # Delete version or whole phrase
-          if(0 == phrase.version)
+          if 0 == phrase.version
             phrase.destroy!
           else
             phrase.version = phrase.version - 1
             phrase.save
           end
 
-          m.reply "Thank you, sir.", true
+          m.reply("Thank you, sir.", true)
         else
-          m.reply "Doesn't belong to you.", true
+          m.reply("Doesn't belong to you.", true)
         end
       else
-        likes = find_alike(key, 5)
-        m.reply "Did you mean: %s" % [ likes  ], true unless(likes.empty?)
+        likes = find_alike(key, m.channel.name, 5)
+        m.reply("Did you mean: %s" % [ likes  ], true) unless likes.empty?
       end
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end # }}}
 
@@ -664,7 +664,12 @@
       # FIXME: Exclude keywords until groups are implemented
       return if [ "news", "best", "worst", "alias", "rem", "give", "find", "forget", "proto", "weather", "imdb" ].include?(key)
 
-      phrase, *args = phrase_args(key, args)
+      phrase, *args = phrase_args(key, m.channel.name, args)
+
+      # Channel aware
+      if phrase.nil?
+        phrase, *args = phrase_args(key, nil, args)
+      end
 
       unless phrase.nil?
         # Get phrase version
@@ -677,20 +682,20 @@
         # Output based on op
         case op
           when "!"
-            m.reply replace_args(v.value, args)
+            m.reply(replace_args(v.value, args))
           when "?"
-            m.reply "'%s' is '%s' (Stored by %s on %s, r%d)" % [
+            m.reply("'%s' is '%s' (Stored by %s on %s, r%d)" % [
               key, v.value, v.nick,
               v.created_at.strftime("%Y-%m-%d at %H:%M:%S"),
               v.version
-            ], true
+            ], true)
         end
       else
-        likes = find_alike(key, 5)
-        m.reply "Did you mean: %s" % [ likes ], true unless likes.empty?
+        likes = find_alike(key, m.channel.name, 5)
+        m.reply("Did you mean: %s" % [ likes ], true) unless likes.empty?
       end
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
       raise
     end # }}}
 
@@ -754,7 +759,7 @@
       value
     end # }}}
 
-    def phrase_args(key, args = []) # {{{
+    def phrase_args(key, channel, args = []) # {{{
       lookup = key.downcase.strip
       arg    = ""
 
@@ -765,7 +770,11 @@
       begin
         lookup << " #{arg}" unless arg.nil? or arg.empty?
 
-        phrase = Phrase.first(:name => lookup)
+        unless channel.nil?
+          phrase = Phrase.first(:name => lookup, :channel => channel)
+        else
+          phrase = Phrase.first(:name => lookup)
+        end
 
         arg = args.shift if phrase.nil?
       end while phrase.nil? and arg.is_a?(String) and !arg.empty?
@@ -773,10 +782,11 @@
       [ phrase, *args ]
     end # }}}
 
-    def find_alike(key, limit = 10) # {{{
+    def find_alike(key, channel, limit = 10) # {{{
       result  = ""
       lookup  = key.downcase.strip
-      phrases = Phrase.all(:name.like => "%#{lookup}%", :limit => limit)
+      phrases = Phrase.all(:name.like => "%#{lookup}%",
+        :channel => channel, :limit => limit)
 
       unless phrases.nil?
         matches = []
@@ -801,7 +811,7 @@
     def action(m, text)
       case text.strip
         when "kicks %s" % [ NICK ]
-          m.reply "Oww..", true
+          m.reply("Oww..", true)
       end
     end
   end # }}}
@@ -816,7 +826,7 @@
         @protos = IO.readlines("protolol.txt")
       end
 
-      m.reply @protos[rand(@protos.size)]
+      m.reply(@protos[rand(@protos.size)])
     end
   end # }}}
 
@@ -838,9 +848,9 @@
       remark = page.search("//p[@class='remark']").text
       flavor = page.search("//p[@class='flavor']").text
 
-      m.reply "%s°?! %s! (%s)" % [ temp, remark, flavor ]
+      m.reply("%s°?! %s! (%s)" % [ temp, remark, flavor ])
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
     end
   end # }}}
 
@@ -867,11 +877,11 @@
         match = list["title_exact"].first
       else
         if list.has_key?("title_approx")
-          m.reply "Did you mean one of these? %s" %
-            list["title_approx"].map { |m| CGI.unescape(m["title"]) }.take(5).join(", ")
+          m.reply("Did you mean one of these? %s" %
+            list["title_approx"].map { |m| CGI.unescape(m["title"]) }.take(5).join(", "))
           return
         else
-          m.reply "No title found", true
+          m.reply("No title found", true)
         end
       end
 
@@ -880,11 +890,11 @@
       rating = page.search("//strong/span[@itemprop='ratingValue']").text
       votes  = page.search("//a/span[@itemprop='ratingCount']").text
 
-      m.reply "Title: %s (at http://imdb.com/title/%s, %s/10 from %s users)" % [
+      m.reply("Title: %s (at http://imdb.com/title/%s, %s/10 from %s users)" % [
         match["title"], match["id"], rating, votes
-      ]
+      ])
     rescue
-      m.reply "Oops something went wrong..", true
+      m.reply("Oops something went wrong..", true)
     end
   end # }}}
 end # }}}