summaryrefslogtreecommitdiff
path: root/data/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-02-14 20:01:24 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-02-14 20:01:24 +0000
commitad78fb47422664c9ce24a3b62194e42974274af7 (patch)
tree94fc3bebf7e1abe938a76102ce9739d4f99bcec6 /data/rbot
parent474700ba4d38f1ca8ff097c464c5aa4810570760 (diff)
shortenurls plugin, merging tinyurl and rubyurl and adding access to most of ShortURL's services
Diffstat (limited to 'data/rbot')
-rw-r--r--data/rbot/plugins/rubyurl.rb36
-rw-r--r--data/rbot/plugins/shortenurls.rb66
-rw-r--r--data/rbot/plugins/tinyurl.rb39
3 files changed, 66 insertions, 75 deletions
diff --git a/data/rbot/plugins/rubyurl.rb b/data/rbot/plugins/rubyurl.rb
deleted file mode 100644
index 96e78b92..00000000
--- a/data/rbot/plugins/rubyurl.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-require "shorturl"
-require "uri"
-
-class RubyURL < Plugin
-
- # return a help string when the bot is asked for help on this plugin
- def help(plugin, topic="")
- return "rubyurl <your long url>"
- end
-
- def shorten(m, params)
- if (params[:url] == "help")
- m.reply help(m.plugin)
- return
- end
-
- url = params[:url]
- begin
- to_uri = URI.parse(url)
- # We don't accept 'generic' URLs because almost everything gets in there
- raise URI::InvalidURIError if to_uri.class == URI::Generic
- rescue URI::InvalidURIError
- m.reply "#{url} doesn't look like an URL to me ..."
- return
- end
-
- short = WWW::ShortURL.shorten(url)
-
- m.reply "#{url} shortened to #{short} on RubyURL"
- end
-
-end
-
-# create an instance of the RubyURL class and register it as a plugin
-rubyurl = RubyURL.new
-rubyurl.map "rubyurl :url", :action => 'shorten'
diff --git a/data/rbot/plugins/shortenurls.rb b/data/rbot/plugins/shortenurls.rb
new file mode 100644
index 00000000..5d8df80a
--- /dev/null
+++ b/data/rbot/plugins/shortenurls.rb
@@ -0,0 +1,66 @@
+#-- vim:sw=2:et
+#++
+#
+# Plugin to handle ShortURL, merges the funcionality of the old rubyurl and tinyurl plugins
+# Note that it's called ShortenURLs and not ShortURL, to prevent conflicts with
+# the actual ruby package used
+#
+# Author:: Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
+# (C) 2007 Giuseppe Bilotta
+# Based on existing rbot plugins, as mentioned above :)
+
+require "shorturl"
+require "uri"
+
+class ShortenURLs < Plugin
+ include WWW
+
+ attr_accessor :services
+ def initialize
+ super
+ # Instead of catering for all the services, we only pick the ones with 'link' or 'url' in the name
+ @services = ShortURL.valid_services.select { |service| service.to_s =~ /(?:link|url)/ } << :shorturl
+ end
+
+ # return a help string when the bot is asked for help on this plugin
+ def help(plugin, topic="")
+ return "shorten urls. syntax: <service> <your long url> => creates a shortened url using the required service (choose between #{@services.join(', ')}). Example: #{@bot.nick}, tinyurl http://some.long.url/wow-this-is/really-long.html"
+ end
+
+ # do the dirty job. This method can be called by other plugins, in which case you
+ # should set the :called param to true
+ def shorten(m, params)
+ url = params[:url]
+ if url == "help"
+ m.reply help(m.plugin) unless params[:called]
+ return
+ end
+ begin
+ to_uri = URI.parse(url)
+ # We don't accept 'generic' URLs because almost everything gets in there
+ raise URI::InvalidURIError if to_uri.class == URI::Generic
+ rescue URI::InvalidURIError
+ m.reply "#{url} doesn't look like an URL to me ..." unless params[:called]
+ return
+ end
+
+ service = params[:service] || m.plugin.to_sym
+ service = :rubyurl if service == :shorturl
+
+ short = WWW::ShortURL.shorten(url, service)
+
+ if params[:called]
+ return short
+ else
+ m.reply "#{url} shortened to #{short}"
+ end
+ end
+
+end
+
+# create an instance of the RubyURL class and register it as a plugin
+plugin = ShortenURLs.new
+
+plugin.services.each { |service|
+ plugin.map "#{service} :url", :action => 'shorten'
+}
diff --git a/data/rbot/plugins/tinyurl.rb b/data/rbot/plugins/tinyurl.rb
deleted file mode 100644
index b74969d9..00000000
--- a/data/rbot/plugins/tinyurl.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-require "rubygems"
-require "shorturl"
-
-class TinyURL < Plugin
- include WWW
-
- # return a help string when the bot is asked for help on this plugin
- def help(plugin, topic="")
- return "tinyurl <your long url>"
- end
-
- # reply to a private message that we've registered for
- def privmsg(m)
-
- # m.params contains the rest of the message, m.plugin contains the first
- # word (useful because it's possible to register for multiple commands)
- unless(m.params)
- m.reply "incorrect usage. " + help(m.plugin)
- end
-
- # TODO: might want to add a check here to validate the url
- # if they call 'rubyurl help' backwards, don't return a lame link
-
- if (m.params == "help")
- m.reply "Try again. Correct usage is: " + help(m.plugin)
- return false
- end
-
- # call the ShortURL library with the value of the url
- url = ShortURL.shorten(m.params, :tinyurl)
-
- m.reply "tinyurl: #{url}"
-
- end
-end
-
-# create an instance of the RubyURL class and register it as a plugin
-tinyurl = TinyURL.new
-tinyurl.register("tinyurl")