]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/basics.rb
basics: log the fact that we're not joining default channels
[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_quit(m, param)
73     @bot.quit param[:msg].to_s
74   end
75
76   def bot_restart(m, param)
77     @bot.restart param[:msg].to_s
78   end
79
80   def bot_reconnect(m, param)
81     @bot.reconnect param[:msg].to_s
82   end
83
84   def bot_hide(m, param)
85     @bot.join 0
86   end
87
88   def bot_say(m, param)
89     @bot.say param[:where], param[:what].to_s
90   end
91
92   def bot_action(m, param)
93     @bot.action param[:where], param[:what].to_s
94   end
95
96   def bot_mode(m, param)
97     @bot.mode param[:where], param[:what], param[:who].join(" ")
98   end
99
100   def bot_ping(m, param)
101     m.reply "pong"
102   end
103
104   def bot_quiet(m, param)
105     if param.has_key?(:where)
106       @bot.set_quiet param[:where].sub(/^here$/, m.target.downcase)
107     else
108       @bot.set_quiet
109     end
110     # Make sense when the commmand is given in private or in a non-quieted
111     # channel
112     m.okay
113   end
114
115   def bot_talk(m, param)
116     if param.has_key?(:where)
117       @bot.reset_quiet param[:where].sub(/^here$/, m.target.downcase)
118     else
119       @bot.reset_quiet
120     end
121     # Make sense when the commmand is given in private or in a non-quieted
122     # channel
123     m.okay
124   end
125
126   def bot_help(m, param)
127     m.reply @bot.help(param[:topic].join(" "))
128   end
129
130   #TODO move these to a "chatback" plugin
131   # when (/^(botsnack|ciggie)$/i)
132   #   @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
133   #   @bot.say m.replyto, @lang.get("thanks") if(m.private?)
134   # when (/^#{Regexp.escape(@bot.nick)}!*$/)
135   #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick
136
137   # handle help requests for "core" topics
138   def help(cmd, topic="")
139     case cmd
140     when "quit"
141       _("quit [<message>] => quit IRC with message <message>")
142     when "restart"
143       _("restart => completely stop and restart the bot (including reconnect)")
144     when "reconnect"
145       _("reconnect => ask the bot to disconnect and then connect again")
146     when "join"
147       _("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")
148     when "part"
149       _("part <channel> => part channel <channel>")
150     when "hide"
151       _("hide => part all channels")
152     when "say"
153       _("say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>")
154     when "action"
155       _("action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>")
156     when "quiet"
157       _("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>")
158     when "talk"
159       _("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>")
160     when "ping"
161       _("ping => replies with a pong")
162     when "mode"
163       _("mode <channel> <mode> <nicks> => set channel modes for <nicks> on <channel> to <mode>")
164     #     when "botsnack"
165     #       return "botsnack => reward #{@bot.myself} for being good"
166     #     when "hello"
167     #       return "hello|hi|hey|yo [#{@bot.myself}] => greet the bot"
168     else
169       _("%{name}: quit, restart, join, part, hide, save, say, action, topic, quiet, talk, ping, mode") % {:name=>name}
170       #, botsnack, hello
171     end
172   end
173 end
174
175 basics = BasicsModule.new
176
177 basics.map "quit *msg",
178   :action => 'bot_quit',
179   :defaults => { :msg => nil },
180   :auth_path => 'quit'
181 basics.map "restart *msg",
182   :action => 'bot_restart',
183   :defaults => { :msg => nil },
184   :auth_path => 'quit'
185 basics.map "reconnect *msg",
186   :action => 'bot_reconnect',
187   :defaults => { :msg => nil },
188   :auth_path => 'quit'
189
190 basics.map "quiet [in] [:where]",
191   :action => 'bot_quiet',
192   :auth_path => 'talk::set'
193 basics.map "talk [in] [:where]",
194   :action => 'bot_talk',
195   :auth_path => 'talk::set'
196
197 basics.map "say :where *what",
198   :action => 'bot_say',
199   :auth_path => 'talk::do'
200 basics.map "action :where *what",
201   :action => 'bot_action',
202   :auth_path => 'talk::do'
203 basics.map "mode :where :what *who",
204   :action => 'bot_mode',
205   :auth_path => 'talk::do'
206
207 basics.map "join :chan :pass",
208   :action => 'bot_join',
209   :defaults => {:pass => nil},
210   :auth_path => 'move'
211 basics.map "part :chan",
212   :action => 'bot_part',
213   :defaults => {:chan => nil},
214   :auth_path => 'move'
215 basics.map "hide",
216   :action => 'bot_hide',
217   :auth_path => 'move'
218
219 basics.map "ping",
220   :action => 'bot_ping',
221   :auth_path => '!ping!'
222 basics.map "help *topic",
223   :action => 'bot_help',
224   :defaults => { :topic => [""] },
225   :auth_path => '!help!'
226
227 basics.default_auth('*', false)
228