]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/basics.rb
3fc794bc6656d701617970a5db04fea4647f32f9
[user/henk/code/ruby/rbot.git] / lib / rbot / core / basics.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: rbot basic management from IRC
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7
8 class BasicsModule < CoreBotModule
9
10   Config.register Config::BooleanValue.new('irc.join_after_identify',
11     :default => false, :wizard => true, :requires_restart => true,
12     :desc => "Should the bot wait until its identification is confirmed before joining any channels?")
13
14   def join_channels
15     @bot.config['irc.join_channels'].each { |c|
16       debug "autojoining channel #{c}"
17       if(c =~ /^(\S+)\s+(\S+)$/i)
18         @bot.join $1, $2
19       else
20         @bot.join c if(c)
21       end
22     }
23   end
24
25   def identified
26     join_channels
27   end
28
29   # on connect, we join the default channels unless we have to wait for
30   # identification. Observe that this means the bot may not connect any channels
31   # until the 'identified' method gets delegated
32   def connect
33     join_channels unless @bot.config['irc.join_after_identify']
34   end
35
36   def ctcp_listen(m)
37     who = m.private? ? "me" : m.target
38     case m.ctcp.intern
39     when :PING
40       m.ctcp_reply m.message
41     when :TIME
42       m.ctcp_reply Time.now.to_s
43     end
44   end
45
46   def bot_join(m, param)
47     if param[:pass]
48       @bot.join param[:chan], param[:pass]
49     else
50       @bot.join param[:chan]
51     end
52   end
53
54   def invite(m)
55     if @bot.auth.allow?(:"basics::move::join", m.source, m.source)
56       @bot.join m.channel
57     end
58   end
59
60   def bot_part(m, param)
61     if param[:chan]
62       @bot.part param[:chan]
63     else
64       @bot.part m.target if m.public?
65     end
66   end
67
68   def bot_quit(m, param)
69     @bot.quit param[:msg].to_s
70   end
71
72   def bot_restart(m, param)
73     @bot.restart param[:msg].to_s
74   end
75
76   def bot_hide(m, param)
77     @bot.join 0
78   end
79
80   def bot_say(m, param)
81     @bot.say param[:where], param[:what].to_s
82   end
83
84   def bot_action(m, param)
85     @bot.action param[:where], param[:what].to_s
86   end
87
88   def bot_mode(m, param)
89     @bot.mode param[:where], param[:what], param[:who].join(" ")
90   end
91
92   def bot_ping(m, param)
93     m.reply "pong"
94   end
95
96   def bot_quiet(m, param)
97     if param.has_key?(:where)
98       @bot.set_quiet param[:where].sub(/^here$/, m.target.downcase)
99     else
100       @bot.set_quiet
101     end
102     # Make sense when the commmand is given in private or in a non-quieted
103     # channel
104     m.okay
105   end
106
107   def bot_talk(m, param)
108     if param.has_key?(:where)
109       @bot.reset_quiet param[:where].sub(/^here$/, m.target.downcase)
110     else
111       @bot.reset_quiet
112     end
113     # Make sense when the commmand is given in private or in a non-quieted
114     # channel
115     m.okay
116   end
117
118   def bot_help(m, param)
119     m.reply @bot.help(param[:topic].join(" "))
120   end
121
122   #TODO move these to a "chatback" plugin
123   # when (/^(botsnack|ciggie)$/i)
124   #   @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
125   #   @bot.say m.replyto, @lang.get("thanks") if(m.private?)
126   # when (/^#{Regexp.escape(@bot.nick)}!*$/)
127   #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick
128
129   # handle help requests for "core" topics
130   def help(cmd, topic="")
131     case cmd
132     when "quit"
133       _("quit [<message>] => quit IRC with message <message>")
134     when "restart"
135       _("restart => completely stop and restart the bot (including reconnect)")
136     when "join"
137       _("join <channel> [<key>] => join channel <channel> with secret key <key> if specified. #{@bot.myself} also responds to invites if you have the required access level")
138     when "part"
139       _("part <channel> => part channel <channel>")
140     when "hide"
141       _("hide => part all channels")
142     when "say"
143       _("say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>")
144     when "action"
145       _("action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>")
146     when "quiet"
147       _("quiet [in here|<channel>] => with no arguments, stop speaking in all channels, if \"in here\", stop speaking in this channel, or stop speaking in <channel>")
148     when "talk"
149       _("talk [in here|<channel>] => with no arguments, resume speaking in all channels, if \"in here\", resume speaking in this channel, or resume speaking in <channel>")
150     when "ping"
151       _("ping => replies with a pong")
152     when "mode"
153       _("mode <channel> <mode> <nicks> => set channel modes for <nicks> on <channel> to <mode>")
154     #     when "botsnack"
155     #       return "botsnack => reward #{@bot.myself} for being good"
156     #     when "hello"
157     #       return "hello|hi|hey|yo [#{@bot.myself}] => greet the bot"
158     else
159       _("%{name}: quit, restart, join, part, hide, save, say, action, topic, quiet, talk, ping, mode") % {:name=>name}
160       #, botsnack, hello
161     end
162   end
163 end
164
165 basics = BasicsModule.new
166
167 basics.map "quit *msg",
168   :action => 'bot_quit',
169   :defaults => { :msg => nil },
170   :auth_path => 'quit'
171 basics.map "restart *msg",
172   :action => 'bot_restart',
173   :defaults => { :msg => nil },
174   :auth_path => 'quit'
175
176 basics.map "quiet [in] [:where]",
177   :action => 'bot_quiet',
178   :auth_path => 'talk::set'
179 basics.map "talk [in] [:where]",
180   :action => 'bot_talk',
181   :auth_path => 'talk::set'
182
183 basics.map "say :where *what",
184   :action => 'bot_say',
185   :auth_path => 'talk::do'
186 basics.map "action :where *what",
187   :action => 'bot_action',
188   :auth_path => 'talk::do'
189 basics.map "mode :where :what *who",
190   :action => 'bot_mode',
191   :auth_path => 'talk::do'
192
193 basics.map "join :chan :pass", 
194   :action => 'bot_join',
195   :defaults => {:pass => nil},
196   :auth_path => 'move'
197 basics.map "part :chan",
198   :action => 'bot_part',
199   :defaults => {:chan => nil},
200   :auth_path => 'move'
201 basics.map "hide",
202   :action => 'bot_hide',
203   :auth_path => 'move'
204
205 basics.map "ping",
206   :action => 'bot_ping',
207   :auth_path => '!ping!'
208 basics.map "help *topic",
209   :action => 'bot_help',
210   :defaults => { :topic => [""] },
211   :auth_path => '!help!'
212
213 basics.default_auth('*', false)
214