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