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