]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/core/basics.rb
irclog core module: dispatch from listen() to catch double-delegated messages early
[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 # Copyright:: (C) 2006,2007 Giuseppe Bilotta
8 # License:: GPL v2
9
10 class BasicsModule < CoreBotModule
11
12   def ctcp_listen(m)
13     who = m.private? ? "me" : m.target
14     case m.ctcp.intern
15     when :PING
16       m.ctcp_reply m.message
17     when :TIME
18       m.ctcp_reply Time.now.to_s
19     end
20   end
21
22   def bot_join(m, param)
23     if param[:pass]
24       @bot.join param[:chan], param[:pass]
25     else
26       @bot.join param[:chan]
27     end
28   end
29
30   def invite(m)
31     if @bot.auth.allow?(:"basics::move::join", m.source, m.source)
32       @bot.join m.channel
33     end
34   end
35
36   def bot_part(m, param)
37     if param[:chan]
38       @bot.part param[:chan]
39     else
40       @bot.part m.target if m.public?
41     end
42   end
43
44   def bot_quit(m, param)
45     @bot.quit param[:msg].to_s
46   end
47
48   def bot_restart(m, param)
49     @bot.restart param[:msg].to_s
50   end
51
52   def bot_hide(m, param)
53     @bot.join 0
54   end
55
56   def bot_say(m, param)
57     @bot.say param[:where], param[:what].to_s
58   end
59
60   def bot_action(m, param)
61     @bot.action param[:where], param[:what].to_s
62   end
63
64   def bot_mode(m, param)
65     @bot.mode param[:where], param[:what], param[:who].join(" ")
66   end
67
68   def bot_ping(m, param)
69     m.reply "pong"
70   end
71
72   def bot_quiet(m, param)
73     if param.has_key?(:where)
74       @bot.set_quiet param[:where].sub(/^here$/, m.target.downcase)
75     else
76       @bot.set_quiet
77     end
78     # Make sense when the commmand is given in private or in a non-quieted
79     # channel
80     m.okay
81   end
82
83   def bot_talk(m, param)
84     if param.has_key?(:where)
85       @bot.reset_quiet param[:where].sub(/^here$/, m.target.downcase)
86     else
87       @bot.reset_quiet
88     end
89     # Make sense when the commmand is given in private or in a non-quieted
90     # channel
91     m.okay
92   end
93
94   def bot_help(m, param)
95     m.reply @bot.help(param[:topic].join(" "))
96   end
97
98   #TODO move these to a "chatback" plugin
99   # when (/^(botsnack|ciggie)$/i)
100   #   @bot.say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
101   #   @bot.say m.replyto, @lang.get("thanks") if(m.private?)
102   # when (/^#{Regexp.escape(@bot.nick)}!*$/)
103   #   @bot.say m.replyto, @lang.get("hello_X") % m.sourcenick
104
105   # handle help requests for "core" topics
106   def help(cmd, topic="")
107     case cmd
108     when "quit"
109       _("quit [<message>] => quit IRC with message <message>")
110     when "restart"
111       _("restart => completely stop and restart the bot (including reconnect)")
112     when "join"
113       _("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")
114     when "part"
115       _("part <channel> => part channel <channel>")
116     when "hide"
117       _("hide => part all channels")
118     when "nick"
119       _("nick <nick> => attempt to change nick to <nick>")
120     when "say"
121       _("say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>")
122     when "action"
123       _("action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>")
124     when "quiet"
125       _("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>")
126     when "talk"
127       _("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>")
128     when "ping"
129       _("ping => replies with a pong")
130     when "mode"
131       _("mode <channel> <mode> <nicks> => set channel modes for <nicks> on <channel> to <mode>")
132     #     when "botsnack"
133     #       return "botsnack => reward #{@bot.myself} for being good"
134     #     when "hello"
135     #       return "hello|hi|hey|yo [#{@bot.myself}] => greet the bot"
136     else
137       _("%{name}: quit, restart, join, part, hide, save, nick, say, action, topic, quiet, talk, ping, mode") % {:name=>name}
138       #, botsnack, hello
139     end
140   end
141 end
142
143 basics = BasicsModule.new
144
145 basics.map "quit *msg",
146   :action => 'bot_quit',
147   :defaults => { :msg => nil },
148   :auth_path => 'quit'
149 basics.map "restart *msg",
150   :action => 'bot_restart',
151   :defaults => { :msg => nil },
152   :auth_path => 'quit'
153
154 basics.map "quiet [in] [:where]",
155   :action => 'bot_quiet',
156   :auth_path => 'talk::set'
157 basics.map "talk [in] [:where]",
158   :action => 'bot_talk',
159   :auth_path => 'talk::set'
160
161 basics.map "say :where *what",
162   :action => 'bot_say',
163   :auth_path => 'talk::do'
164 basics.map "action :where *what",
165   :action => 'bot_action',
166   :auth_path => 'talk::do'
167 basics.map "mode :where :what *who",
168   :action => 'bot_mode',
169   :auth_path => 'talk::do'
170
171 basics.map "join :chan :pass", 
172   :action => 'bot_join',
173   :defaults => {:pass => nil},
174   :auth_path => 'move'
175 basics.map "part :chan",
176   :action => 'bot_part',
177   :defaults => {:chan => nil},
178   :auth_path => 'move'
179 basics.map "hide",
180   :action => 'bot_hide',
181   :auth_path => 'move'
182
183 basics.map "ping",
184   :action => 'bot_ping',
185   :auth_path => '!ping!'
186 basics.map "help *topic",
187   :action => 'bot_help',
188   :defaults => { :topic => [""] },
189   :auth_path => '!help!'
190
191 basics.default_auth('*', false)
192