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