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