X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fremind.rb;h=f66c4fc8a40b79b7f9da4974df1c9f13cf6afc01;hb=676dd61e6b0bea5f506d064039a685944aefd6fb;hp=5ad980aeac8d33012da673e0c0594796d8100f32;hpb=438d56ceb82755961229d222d82a1c22ce04ab1d;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git 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