Plugin header boilerplating.
[rbot] / data / rbot / plugins / time.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Time Zone Plugin for rbot
5 #
6 # Author:: Ian Monroe <ian@monroe.nu>
7 # Copyright:: (C) 2006 Ian Monroe
8 # License:: MIT license
9
10 require 'tzinfo'
11
12 class TimePlugin < Plugin
13
14   def help(plugin, topic="")
15   "time <time zone> to get the local time of a certain location. <time zone> can be <Continent/City> or <two character country code>. time <nick> to see the local time of that person if their time zone is set. time admin set <nick> <time zone> to set the time zone for another user. time [admin] reset [nick] to let the bot forget about the tzinfo about someone"
16   end
17
18   def initialize
19     super
20     # this plugin only wants to store strings
21     class << @registry
22       def store(val)
23         val
24       end
25       def restore(val)
26         val
27       end
28     end
29   end
30
31   def getTime(m, zone )
32     if zone.length == 2 then #country code
33       zone.upcase!
34       zone = 'GB' if zone == 'UK' #country doesn't know its own name
35       begin
36         nationZones = TZInfo::Country.get(zone).zone_identifiers
37         if nationZones.size == 1 then
38           zone = nationZones[0]
39         else
40           m.reply "#{zone} has the cities of #{nationZones.join( ', ' )}."
41         end
42       rescue TZInfo::InvalidCountryCode
43         m.reply "#{zone} is not a valid country code."
44       end
45     end
46     ['/', '_'].each { |sp|
47         arr = Array.new
48         zone.split(sp).each{ |s| 
49             s[0] = s[0,1].upcase
50             s[1, s.length] = s[1, s.length].downcase if sp == '/'
51             arr.push(s) }
52             zone = arr.join( sp )
53         }
54     
55     TZInfo::Timezone.get( zone ).now
56   end
57
58   def showTime(m, params)
59     zone = params[:where].join('_')
60     if params[:where].size > 0 then
61       begin
62         m.reply "#{zone} - #{getTime( m,  zone )}"
63       rescue TZInfo::InvalidTimezoneIdentifier
64         if @registry.has_key?( zone ) then
65           zone =  @registry[ zone ]
66           m.reply "#{zone} - #{getTime( m,  zone )}"
67         else
68           m.reply "#{zone} is an unknown time."
69         end
70       end
71     else
72       if @registry.has_key?( m.sourcenick) then
73         zone = @registry[ m.sourcenick ]
74         m.reply "#{m.sourcenick}: #{zone} - #{getTime( m,  zone )}"
75       else
76         m.reply "#{m.sourcenick}: use time set <Continent/City> to set your timezone."
77       end
78     end
79   end
80
81   def setUserZone( m, params )
82     if params[:where].size > 0 then
83       s = setZone( m, m.sourcenick, params[:where].join('_') )
84     else
85       m.reply "Requires Continent/City or country code"
86     end
87   end
88
89   def resetUserZone( m, params )
90     s = resetZone( m, m.sourcenick)
91   end
92
93   def setAdminZone( m, params )
94     if params[:who] and params[:where].size > 0 then
95       s = setZone( m, params[:who], params[:where].join('_') )
96     else
97       m.reply "Requires a nick and the Continent/City or country code"
98     end
99   end
100
101   def resetAdminZone( m, params )
102     if params[:who]
103       s = resetZone( m, params[:who])
104     else
105       m.reply "Requires a nick"
106     end
107   end
108
109   def setZone( m, user, zone )
110     begin
111       getTime( m,  zone )
112     rescue TZInfo::InvalidTimezoneIdentifier
113       m.reply "#{zone} is an invalid timezone. Format is Continent/City or a two character country code."
114       return
115     end
116     @registry[ user ] = zone
117     m.reply "Ok, I'll remember that #{user} is on the #{zone} timezone"
118   end
119
120   def resetZone( m, user )
121     @registry.delete(user)
122     m.reply "Ok, I've forgotten #{user}'s timezone"
123   end
124 end
125
126 plugin = TimePlugin.new
127
128 plugin.default_auth('admin', false)
129
130 plugin.map 'time set [time][zone] [to] *where', :action=> 'setUserZone', :defaults => {:where => false}
131 plugin.map 'time reset [time][zone]', :action=> 'resetUserZone'
132 plugin.map 'time admin set [time][zone] [for] :who [to] *where', :action=> 'setAdminZone', :defaults => {:who => false, :where => false}
133 plugin.map 'time admin reset [time][zone] [for] :who', :action=> 'resetAdminZone', :defaults => {:who => false}
134 plugin.map 'time *where', :action => 'showTime', :defaults => {:where => false}