Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move most support to plugins form

  • Loading branch information...
commit e2936c498b4d7bb151c5f7347512d7a927d41c05 1 parent f2fa5f0
Ky-Anh Huynh icy authored
Showing with 110 additions and 78 deletions.
  1. +7 −7 bot/TODO
  2. +103 −71 bot/bot.rb
14 bot/TODO
View
@@ -1,7 +1,7 @@
-#1 Support !command (ty4ever)
-#2 Fix Hello behavior
-#3 Provide help to any commands
-#4 Support !reload so channel admin can reload the bot
-#5 Move stuff to plugin form
-#6 Support !load <plugin> to load a special plugin
-
+ -1 Support !command (ty4ever)
+ -2 Fix Hello behavior
+ #3 Provide help to any commands
+ #4 Support !reload so channel admin can reload the bot
+ #5 Move stuff to plugin form
+ #6 Support !load <plugin> to load a special plugin
+
174 bot/bot.rb
View
@@ -11,104 +11,136 @@
require 'uri'
require 'open-uri'
-# FIXME: Don't say hello too much
-class Hello
+BOT_NAME = "archl0n0xvn"
+
+########################################################################
+# PLUGINS #
+########################################################################
+
+class IcyUserMonitor
include Cinch::Plugin
- match /hello/
+ listen_to :connect, method: :on_connect
+ listen_to :online, method: :on_online
+ #listen_to :offline, method: :on_offline
- def execute(m)
- m.reply "Hello, #{m.user.nick}"
- end
-end
+ # def on_offline(m, user)
+ # @bot.loggers.info "I miss my master :("
+ # end
-bot = Cinch::Bot.new do
- configure do |c|
- c.server = "irc.freenode.org"
- c.channels = ["#archlinuxvn"]
- c.nick = "archl0n0xvn"
- c.user = "archl0n0xvn"
- c.realname = "archl0n0xvn"
- c.plugins.plugins = [Hello]
+ def on_connect(m)
+ User(m.user.nick).monitor
end
# Say hello when someone has logged in
-
- # FIXME: This doesn't work
- on :message, /([^ ]+) \[(.+)@(.+)\] entered the room/ do |m, nick, user, host|
- m.reply "Hello, #{nick}. You come from #{host} using username = #{user}."
+ def on_online(m, user)
+ user.send "Hello #{m.user.nick}"
end
+end
- # FIXME: This doesn't work
- on :message, /^:(.+)!(.+)@(.+) JOIN #archlinuxvn/ do |m, nick, user, host|
- m.reply "Hello, #{nick}. You come from #{host}. Are you #{user}?"
- end
+# Say another hello to follow a previous Hello message
+# If A says Hi to B, the bot also says Hi to B (unless B is the bot itself)
+class IcyHello
+ include Cinch::Plugin
- # Say another hello to follow a previous Hello message
- # If A says Hi to B, the bot also says Hi to B (unless B is the bot itself)
- on :message, /^hello[\t ,]*([^\t ]+)/i do |m, text|
- if text.match("archl0n0xvn")
- m.reply "Hello, #{m.user.nick}"
- else
- m.reply "Hello, #{text}"
+ listen_to :message
+
+ def listen(m)
+ text = nil
+ if gs = m.message.match(/^hello[\t ,]*([^\t ]+)/i)
+ text = gs[1]
+ elsif gs = m.message.match(/^([^ ]: hello)/i)
+ text = gs[1]
end
- end
- # Same as above, with another form.
- # FIXME: Why not support two patterns in the same block?
- on :message, /^([^ ]: hello)/i do |m, nick|
- if nick.match("archl0n0xvn")
+ return unless text
+
+ if text.match(BOT_NAME)
m.reply "Hello, #{m.user.nick}"
else
- m.reply "Hello, #{nick}"
+ m.reply "Hello, #{text}"
end
end
+end
+
+# Provice command !tinyurl
+class IcyCmdTinyURL
+ include Cinch::Plugin
+
+ listen_to :message
- # Sensored words
- on :message, /\b(vcl|wtf|sh[1i]t|f.ck|d[e3]k|clgt)\b/i do |m, text|
- m.reply "#{m.user.nick}: take it easy. don't say #{text}"
+ # Provide a simple command , example
+ # botname: tinyrul <your_url>. The bot will reply to the author
+ # a tiny version of your URL. HTTP and HTTPS only.
+ def tinyurl(url)
+ url = open("http://tinyurl.com/api-create.php?url=#{URI.escape(url)}").read
+ url == "Error" ? nil : url
+ rescue OpenURI::HTTPError
+ nil
end
- # A simple helper
- helpers do
- def shorten(url)
- url = open("http://tinyurl.com/api-create.php?url=#{URI.escape(url)}").read
- url == "Error" ? nil : url
- rescue OpenURI::HTTPError
- nil
+ def listen(m)
+ if gs = m.message.match(/^!tinyurl (https?:\/\/[^ ]+)/)
+ url = gs[1]
+ if url_ = tinyurl(url)
+ m.reply "#{url_} <- #{url.slice(0, 20)}"
+ end
end
end
+end
- on :message, /^(!|archl0n0xvn: )info/ do |m|
- m.reply "ArchLinuxVn aka Vietnamese Groups of ArchLinux Users"
- m.reply " * irc channel #archlinuxvn on irc.freenode.net"
- m.reply " * homepage http://archlinuxvn.tuxfamily.org/"
- m.reply " * mailing list http://groups.google.com/group/archlinuxvn"
- m.reply " * source code http://github.com/archlinuxvn/"
- end
+# Provide basic commands
+class IcyCmdBasic
+ include Cinch::Plugin
- on :message, /^(!|archl0n0xvn: )help/ do |m|
- m.reply "Available commands: info, help, tinyurl"
- m.reply "To send command, use !command, or send a message to the bot"
- m.reply "The bot will say hello sometimes."
- m.reply "To fix the bot's behavior, visit http://github.com/archlinuxvn/irclog"
+ listen_to :message
+
+ def listen(m)
+ if m.message.match(/^!info/)
+ m.reply "ArchLinuxVn aka Vietnamese Groups of ArchLinux Users"
+ m.reply " * irc channel #archlinuxvn on irc.freenode.net"
+ m.reply " * homepage http://archlinuxvn.tuxfamily.org/"
+ m.reply " * mailing list http://groups.google.com/group/archlinuxvn"
+ m.reply " * source code http://github.com/archlinuxvn/"
+ elsif m.message.match(/^!help/)
+ m.reply "To send command, use !command."
+ m.reply "Available commands: info, help, tinyurl"
+ m.reply "The bot will say hello sometimes."
+ m.reply "To fix the bot's behavior, visit http://github.com/archlinuxvn/irclog"
+ end
end
+end
- # Provide a simple command , example
- # botname: tinyrul <your_url>. The bot will reply to the author
- # a tiny version of your URL. HTTP and HTTPS only.
- on :message, /^(!|archl0n0xvn: )tinyurl (https?:\/\/[^ ]+)/ do |m, url|
- # urls = URI.extract(m.message, "http")
- # puts ":::: #{urls}"
- urls = [] << url.strip
- unless urls.empty?
- short_urls = urls.map {|url| shorten(url) }.compact
-
- unless short_urls.empty?
- m.reply short_urls.join(", ")
- end
+# Provide basic sensor :)
+class IcySensor
+ include Cinch::Plugin
+
+ listen_to :message
+
+ def listen(m)
+ if gs = m.message.match(/\b(vcl|wtf|sh[1i]t|f.ck|d[e3]k|clgt)\b/i)
+ m.reply "#{m.user.nick}: take it easy. don't say #{gs[1]}"
end
end
end
+########################################################################
+# MAIN BOT #
+########################################################################
+
+bot = Cinch::Bot.new do
+ configure do |c|
+ c.server = "irc.freenode.org"
+ c.channels = ["#archlinuxvn"]
+ c.nick = c.user = c.realname = BOT_NAME
+ c.plugins.plugins = [
+ IcyHello,
+ IcySensor,
+ IcyUserMonitor,
+ IcyCmdTinyURL,
+ IcyCmdBasic,
+ ]
+ end
+end
+
bot.start
Please sign in to comment.
Something went wrong with that request. Please try again.