From: Tom Gilbert Date: Fri, 29 Jul 2005 13:44:33 +0000 (+0000) Subject: Fri Jul 29 13:07:56 BST 2005 Tom Gilbert X-Git-Url: https://git.netwichtig.de/gitweb/?a=commitdiff_plain;h=676dd61e6b0bea5f506d064039a685944aefd6fb;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git Fri Jul 29 13:07:56 BST 2005 Tom Gilbert * Moved some stuff out of util.rb into the plugins that actually need them. Those methods didn't belong in util as they were plugin-specific. * moved a few more plugins to use map() where appropriate * made the url plugin only store unique urls --- diff --git a/ChangeLog b/ChangeLog index fcb65aac..aca85919 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri Jul 29 13:07:56 BST 2005 Tom Gilbert + + * Moved some stuff out of util.rb into the plugins that actually need + them. Those methods didn't belong in util as they were plugin-specific. + * moved a few more plugins to use map() where appropriate + * made the url plugin only store unique urls + Thu Jul 28 23:45:26 BST 2005 Tom Gilbert * Reworked the Timer module. The Timer now has a smart thread manager to diff --git a/data/rbot/plugins/nslookup.rb b/data/rbot/plugins/nslookup.rb index 92da1ba7..160fee85 100644 --- a/data/rbot/plugins/nslookup.rb +++ b/data/rbot/plugins/nslookup.rb @@ -1,56 +1,43 @@ class DnsPlugin < Plugin - begin - require 'resolv-replace' - def gethostname(address) - Resolv.getname(address) - end - def getaddresses(name) - Resolv.getaddresses(name) - end - rescue LoadError - def gethostname(address) - Socket.gethostbyname(address).first - end - def getaddresses(name) - a = Socket.gethostbyname(name) - list = Socket.getaddrinfo(a[0], 'http') - addresses = Array.new - list.each {|line| - addresses << line[3] - } - addresses - end + require 'resolv' + def gethostname(address) + Resolv.getname(address) + end + def getaddresses(name) + Resolv.getaddresses(name) end def help(plugin, topic="") - "nslookup|dns => show local resolution results for hostname or ip address" + "dns => show local resolution results for hostname or ip address" end - def privmsg(m) - unless(m.params) - m.reply "incorrect usage: " + help(m.plugin) - return + + def name_to_ip(m, params) + Thread.new do + begin + a = getaddresses(params[:host]) + if a.length > 0 + m.reply m.params + ": " + a.join(", ") + else + m.reply "#{params[:host]}: not found" + end + rescue StandardError => err + m.reply "#{params[:host]}: not found" + end end + end + + def ip_to_name(m, params) Thread.new do - if(m.params =~ /^\d+\.\d+\.\d+\.\d+$/) begin - a = gethostname(m.params) + a = gethostname(params[:ip]) m.reply m.params + ": " + a if a rescue StandardError => err - m.reply "#{m.params}: not found" - end - elsif(m.params =~ /^\S+$/) - begin - a = getaddresses(m.params) - m.reply m.params + ": " + a.join(", ") - rescue StandardError => err - m.reply "#{m.params}: not found" + m.reply "#{params[:ip]}: not found (does not reverse resolve)" end - else - m.reply "incorrect usage: " + help(m.plugin) - end - end + end end end plugin = DnsPlugin.new -plugin.register("nslookup") -plugin.register("dns") +plugin.map 'dns :ip', :action => 'ip_to_name', + :requirements => {:ip => /^\d+\.\d+\.\d+\.\d+$/} +plugin.map 'dns :host', :action => 'name_to_ip' diff --git a/data/rbot/plugins/opmeh.rb b/data/rbot/plugins/opmeh.rb deleted file mode 100644 index aad388a9..00000000 --- a/data/rbot/plugins/opmeh.rb +++ /dev/null @@ -1,19 +0,0 @@ -class OpMePlugin < Plugin - - def help(plugin, topic="") - return "opme => grant user ops in " - end - - def privmsg(m) - if(m.params) - channel = m.params - else - channel = m.channel - end - target = m.sourcenick - @bot.sendq("MODE #{channel} +o #{target}") - m.okay - end -end -plugin = OpMePlugin.new -plugin.register("opme") diff --git a/data/rbot/plugins/remind.rb b/data/rbot/plugins/remind.rb index 5ad980ae..f66c4fc8 100644 --- a/data/rbot/plugins/remind.rb +++ b/data/rbot/plugins/remind.rb @@ -1,6 +1,108 @@ require 'rbot/utils' class RemindPlugin < Plugin + # read a time in string format, turn it into "seconds from now". + # example formats handled are "5 minutes", "2 days", "five hours", + # "11:30", "15:45:11", "one day", etc. + # + # Throws:: RunTimeError "invalid time string" on parse failure + def timestr_offset(timestr) + case timestr + when (/^(\S+)\s+(\S+)$/) + mult = $1 + unit = $2 + if(mult =~ /^([\d.]+)$/) + num = $1.to_f + raise "invalid time string" unless num + else + case mult + when(/^(one|an|a)$/) + num = 1 + when(/^two$/) + num = 2 + when(/^three$/) + num = 3 + when(/^four$/) + num = 4 + when(/^five$/) + num = 5 + when(/^six$/) + num = 6 + when(/^seven$/) + num = 7 + when(/^eight$/) + num = 8 + when(/^nine$/) + num = 9 + when(/^ten$/) + num = 10 + when(/^fifteen$/) + num = 15 + when(/^twenty$/) + num = 20 + when(/^thirty$/) + num = 30 + when(/^sixty$/) + num = 60 + else + raise "invalid time string" + end + end + case unit + when (/^(s|sec(ond)?s?)$/) + return num + when (/^(m|min(ute)?s?)$/) + return num * 60 + when (/^(h|h(ou)?rs?)$/) + return num * 60 * 60 + when (/^(d|days?)$/) + return num * 60 * 60 * 24 + else + raise "invalid time string" + end + when (/^(\d+):(\d+):(\d+)$/) + hour = $1.to_i + min = $2.to_i + sec = $3.to_i + now = Time.now + later = Time.mktime(now.year, now.month, now.day, hour, min, sec) + return later - now + when (/^(\d+):(\d+)$/) + hour = $1.to_i + min = $2.to_i + now = Time.now + later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec) + return later - now + when (/^(\d+):(\d+)(am|pm)$/) + hour = $1.to_i + min = $2.to_i + ampm = $3 + if ampm == "pm" + hour += 12 + end + now = Time.now + later = Time.mktime(now.year, now.month, now.day, hour, min, now.sec) + return later - now + when (/^(\S+)$/) + num = 1 + unit = $1 + case unit + when (/^(s|sec(ond)?s?)$/) + return num + when (/^(m|min(ute)?s?)$/) + return num * 60 + when (/^(h|h(ou)?rs?)$/) + return num * 60 * 60 + when (/^(d|days?)$/) + return num * 60 * 60 * 24 + else + raise "invalid time string" + end + else + raise "invalid time string" + end + end + def initialize super @reminders = Hash.new @@ -14,15 +116,11 @@ class RemindPlugin < Plugin @reminders.clear end def help(plugin, topic="") - if(plugin =~ /^remind\+$/) - "see remind. remind+ can be used to remind someone else of something, using instead of 'me'. However this will generally require a higher auth level than remind." - else - "remind me [about] in