]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/basics.rb
basics: UI command to send NOTICEs
[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     if @bot.config['irc.join_after_identify']
34       log "waiting for identififcation before JOINing default channels"
35     else
36       join_channels
37     end
38   end
39
40   def ctcp_listen(m)
41     who = m.private? ? "me" : m.target
42     case m.ctcp.intern
43     when :PING
44       m.ctcp_reply m.message
45     when :TIME
46       m.ctcp_reply Time.now.to_s
47     end
48   end
49
50   def bot_join(m, param)
51     if param[:pass]
52       @bot.join param[:chan], param[:pass]
53     else
54       @bot.join param[:chan]
55     end
56   end
57
58   def invite(m)
59     if @bot.auth.allow?(:"basics::move::join", m.source, m.source)
60       @bot.join m.channel
61     end
62   end
63
64   def bot_part(m, param)
65     if param[:chan]
66       @bot.part param[:chan]
67     else
68       @bot.part m.target if m.public?
69     end
70   end
71
72   def bot_channel_list(m, param)
73     ret = _('I am in: ')
74     # sort the channels by the base name and then map with prefixes for the
75     # mode and display.
76     ret << @bot.channels.compact.sort { |a,b|
77         a.name.downcase <=> b.name.downcase
78     }.map { |c|
79         c.modes_of(@bot.myself).map{ |mo|
80           m.server.prefix_for_mode(mo)
81         }.to_s + c.name
82     }.join(', ')
83     m.reply ret
84   end
85
86   def bot_quit(m, param)
87     @bot.quit param[:msg].to_s
88   end
89
90   def bot_restart(m, param)
91     @bot.restart param[:msg].to_s
92   end
93
94   def bot_reconnect(m, param)
95     @bot.reconnect param[:msg].to_s
96   end
97
98   def bot_hide(m, param)
99     @bot.join 0
100   end
101
102   def bot_say(m, param)
103     @bot.say param[:where], param[:what].to_s
104   end
105
106   def bot_notify(m, param)
107     @bot.notice param[:where], param[:what].to_s
108   end
109
110   def bot_action(m, param)
111     @bot.action param[:where], param[:what].to_s
112   end
113
114   def bot_mode(m, param)
115     @bot.mode param[:where], param[:what], param[:who].join(" ")
116   end
117
118   def bot_ping(m, param)
119     m.reply "pong"
120   end
121
122   def bot_quiet(m, param)
123     if param.has_key?(:where)
124       @bot.set_quiet param[:where].sub(/^here$/, m.target.downcase)
125     else
126       @bot.set_quiet
127     end
128     # Make sense when the commmand is given in private or in a non-quieted
129     # channel
130     m.okay
131   end
132
133   def bot_talk(m, param)
134     if param.has_key?(:where)
135       @bot.reset_quiet param[:where].sub(/^here$/, m.target.downcase)
136     else
137       @bot.reset_quiet
138     end
139     # Make sense when the commmand is given in private or in a non-quieted
140     # channel
141     m.okay
142   end
143
144   def bot_help(m, param)
145     m.reply @bot.help(param[:topic].join(" "))
146   end
147
148   #TODO move these to a "chatback" plugin
149   # when (/^(botsnack|ciggie)$/i)
150   #   @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
151   #   @bot.say m.replyto, @lang.get("thanks") if(m.private?)
152   # when (/^#{Regexp.escape(@bot.nick)}!*$/)
153   #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick
154
155   # handle help requests for "core" topics
156   def help(cmd, topic="")
157     case cmd
158     when "quit"
159       _("quit [<message>] => quit IRC with message <message>")
160     when "restart"
161       _("restart => completely stop and restart the bot (including reconnect)")
162     when "reconnect"
163       _("reconnect => ask the bot to disconnect and then connect again")
164     when "join"
165       _("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")
166     when "part"
167       _("part <channel> => part channel <channel>")
168     when "hide"
169       _("hide => part all channels")
170     when "say"
171       _("say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>")
172     when "action"
173       _("action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>")
174     when "quiet"
175       _("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>")
176     when "talk"
177       _("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>")
178     when "ping"
179       _("ping => replies with a pong")
180     when "mode"
181       _("mode <channel> <mode> <nicks> => set channel modes for <nicks> on <channel> to <mode>")
182     #     when "botsnack"
183     #       return "botsnack => reward #{@bot.myself} for being good"
184     #     when "hello"
185     #       return "hello|hi|hey|yo [#{@bot.myself}] => greet the bot"
186     else
187       _("%{name}: quit, restart, join, part, hide, save, say, action, topic, quiet, talk, ping, mode") % {:name=>name}
188       #, botsnack, hello
189     end
190   end
191 end
192
193 basics = BasicsModule.new
194
195 basics.map "quit *msg",
196   :action => 'bot_quit',
197   :defaults => { :msg => nil },
198   :auth_path => 'quit'
199 basics.map "restart *msg",
200   :action => 'bot_restart',
201   :defaults => { :msg => nil },
202   :auth_path => 'quit'
203 basics.map "reconnect *msg",
204   :action => 'bot_reconnect',
205   :defaults => { :msg => nil },
206   :auth_path => 'quit'
207
208 basics.map "quiet [in] [:where]",
209   :action => 'bot_quiet',
210   :auth_path => 'talk::set'
211 basics.map "talk [in] [:where]",
212   :action => 'bot_talk',
213   :auth_path => 'talk::set'
214
215 basics.map "say :where *what",
216   :action => 'bot_say',
217   :auth_path => 'talk::do'
218 basics.map "notify :where *what",
219   :action => 'bot_notify',
220   :auth_path => 'talk::do'
221 basics.map "action :where *what",
222   :action => 'bot_action',
223   :auth_path => 'talk::do'
224 basics.map "mode :where :what *who",
225   :action => 'bot_mode',
226   :auth_path => 'talk::do'
227
228 basics.map "join :chan :pass",
229   :action => 'bot_join',
230   :defaults => {:pass => nil},
231   :auth_path => 'move'
232 basics.map "part :chan",
233   :action => 'bot_part',
234   :defaults => {:chan => nil},
235   :auth_path => 'move'
236 basics.map "channels",
237   :action => 'bot_channel_list',
238   :auth_path => 'move'
239 basics.map "hide",
240   :action => 'bot_hide',
241   :auth_path => 'move'
242
243 basics.map "ping",
244   :action => 'bot_ping',
245   :auth_path => '!ping!'
246 basics.map "help *topic",
247   :action => 'bot_help',
248   :defaults => { :topic => [""] },
249   :auth_path => '!help!'
250
251 basics.default_auth('*', false)
252