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