em-irc-bot 0.1.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e78c1ab5d757c56c181033211f06be8150b75592
4
- data.tar.gz: 4efaea1d51c1c9c891cc4dac2a42a32e14261c1c
3
+ metadata.gz: a268a89c7bd0bab9f524ff94d7079a0e84b5e884
4
+ data.tar.gz: 15f98e9245cc3710db11bd557fd91d2ced75dc88
5
5
  SHA512:
6
- metadata.gz: 25f6c3a59befbd5183d8005289a91ffdab33e7c2b218271b3f1396099dc4db86f4b7e086cbb8df4abeef6b9b76b7a1958b92695f4050394094179fa2aaaf6017
7
- data.tar.gz: f049ba6506e2def21de565a2bbe9db2d8e6db5e0c602a7b8a90e1b5bf6f10cff249272804dff08bae4777ba1dc8803afbdc34d3446a4f40f9d12d6b973e1062e
6
+ metadata.gz: f14695cda664f52416611c1522bce4010d26d564401a4ae831dad2e51fe77ac7379363a2efbd01864a1684f0287273e0ef1638ee62bcbcb4c462ce94d99cd4e3
7
+ data.tar.gz: 1151587b6f8460427339f7561a14a5fd36542823ed6780c193bb1db37f51f8d781cb1d11336f9bb25d0181e91084a470b6c971e879f9401f04e4d8e1836ba368
@@ -18,6 +18,12 @@ require 'logger'
18
18
  # regular expression. That's... pretty much it. The rest is up to you.
19
19
  #
20
20
  class EM::IrcBot
21
+ # This bot's nick.
22
+ #
23
+ # @return [String]
24
+ #
25
+ attr_reader :nick
26
+
21
27
  # Create a new IRC bot.
22
28
  #
23
29
  # If you want to have the bot connect to the server immediately upon
@@ -62,12 +68,16 @@ class EM::IrcBot
62
68
  # specified, a default logger will be setup to send only fatal errors
63
69
  # to `$stderr`.
64
70
  #
71
+ # @option opts [String] :command_prefix The string which must precede
72
+ # any commands sent to the bot in-channel. The default is `"!"`.
73
+ #
65
74
  def initialize(nick, opts = {})
66
75
  @nick = nick
67
76
  @ready = false
68
77
  @backlog = ""
69
78
  @line_handlers = {}
70
79
  @privmsg_handlers = {}
80
+ @commands = {}
71
81
 
72
82
  @serverpass = opts[:serverpass]
73
83
  @username = opts[:username] || "em-irc-bot"
@@ -77,6 +87,7 @@ class EM::IrcBot
77
87
  l.level = Logger::FATAL
78
88
  l.formatter = proc { |s, dt, p, m| "#{m}\n" }
79
89
  end
90
+ @cmd_prefix = opts[:command_prefix] || "!"
80
91
 
81
92
  if opts[:server] and opts[:port]
82
93
  connect(opts[:server], opts[:port], opts[:tls])
@@ -87,6 +98,8 @@ class EM::IrcBot
87
98
  end
88
99
 
89
100
  on(/^:[^\s]+ PRIVMSG /, &method(:do_privmsg))
101
+
102
+ listen_for(/./, &method(:command_handler))
90
103
  end
91
104
 
92
105
  # Connect to an IRC server.
@@ -122,6 +135,38 @@ class EM::IrcBot
122
135
  send_line("JOIN #{ch}")
123
136
  end
124
137
 
138
+ # Register a callback to be executed on a given command.
139
+ #
140
+ # A command is anything said in-channel which is prefixed by the
141
+ # `:command_prefix` option passed to the bot's constructor (`"!"` by
142
+ # default), or anything at all said to the bot privately.
143
+ #
144
+ # Only one callback can be registered for a given command. If you
145
+ # register for the same command twice, only the last callback will be
146
+ # activated.
147
+ #
148
+ # @param cmd [String] The command to call back for. It must have
149
+ # no whitespace.
150
+ #
151
+ # @param blk [Proc] The callback.
152
+ #
153
+ # @yieldparam [EM::IrcBot::Message] The full message that was received.
154
+ # This is given so you can reply through it
155
+ #
156
+ # @yieldparam [String] The command itself. Just in case you have multiple
157
+ # commands pointing to the same block, so you can differentiate between
158
+ # them.
159
+ #
160
+ # @yieldparam [Array<String>] The remaining arguments that were passed to
161
+ # the command. All command arguments are strictly whitespace-separated.
162
+ # There is no way to pass an argument containing whitespace.
163
+ #
164
+ # @return void
165
+ #
166
+ def command(cmd, &blk)
167
+ @commands[cmd] = blk
168
+ end
169
+
125
170
  # Register a new callback for a PRIVMSG seen by the bot.
126
171
  #
127
172
  # The fundamental way of causing the bot to interact with its environment
@@ -308,11 +353,11 @@ class EM::IrcBot
308
353
  "do_privmsg got line that wasn't a PRIVMSG: #{l.inspect}"
309
354
  end
310
355
 
311
- source = $2
312
- sender = $1
356
+ channel = ($2 == nick) ? $1 : $2
357
+ sender = $1
313
358
  message = $3
314
359
 
315
- msg = EM::IrcBot::Message.new(self, source, sender, message)
360
+ msg = EM::IrcBot::Message.new(self, channel, sender, message)
316
361
 
317
362
  @privmsg_handlers.each_pair do |re, blk|
318
363
  if (matchdata = message.match(re))
@@ -320,6 +365,17 @@ class EM::IrcBot
320
365
  end
321
366
  end
322
367
  end
368
+
369
+ def command_handler(msg)
370
+ if msg.private? or msg.line.index(@cmd_prefix) == 0
371
+ cmdline = msg.line.gsub(/^#{@cmd_prefix}/, '').split(/\s+/)
372
+ if @commands[cmdline[0]]
373
+ @commands[cmdline[0]].call(msg, cmdline[0], cmdline[1..-1])
374
+ else
375
+ msg.reply "I don't understand that."
376
+ end
377
+ end
378
+ end
323
379
  end
324
380
 
325
381
  require_relative "irc_bot/conn_handler"
@@ -17,15 +17,16 @@ class EM::IrcBot::Message
17
17
  #
18
18
  attr_reader :bot
19
19
 
20
- # The nick or channel which originated the message.
20
+ # The public channel, or nick, which originated the message. This is the
21
+ # place which replies will be sent to by {#reply}.
21
22
  #
22
23
  # @return [String]
23
24
  #
24
- attr_reader :source
25
+ attr_reader :channel
25
26
 
26
- # The nick which sent the message. For private messages, this will
27
- # be the same as {#source}, but for messages in-channel, this will be
28
- # the nick which sent the message, while {#source} is the channel.
27
+ # The nick which sent the message. For private messages, this will be
28
+ # the same as {#channel}, but for messages in-channel, this will be the
29
+ # nick which sent the message, while {#channel} is, well, the channel.
29
30
  #
30
31
  # @return [String]
31
32
  #
@@ -39,23 +40,33 @@ class EM::IrcBot::Message
39
40
 
40
41
  #:nodoc:
41
42
  #
42
- def initialize(bot, source, sender, line)
43
- @bot = bot
44
- @source = source
45
- @sender = sender
46
- @line = line
43
+ def initialize(bot, channel, sender, line)
44
+ @bot = bot
45
+ @channel = channel
46
+ @sender = sender
47
+ @line = line
47
48
  end
48
49
 
49
- # Send a line of text back to the source of the message.
50
+ # Send a line of text back to the channel which originated the message.
50
51
  #
51
- # This is only a reply to the *source*; it doesn't modify your message at
52
- # all to, say, prepend the nick of the sender. That bit's up to you.
52
+ # This is only a reply to the *source channel*; it doesn't modify your
53
+ # message at all to, say, prepend the nick of the sender. That bit's up
54
+ # to you.
53
55
  #
54
56
  # @param s [String] The message to send.
55
57
  #
56
58
  # @return void
57
59
  #
58
60
  def reply(s)
59
- bot.say(source, s)
61
+ bot.say(channel, s)
62
+ end
63
+
64
+ # Whether or not this message was sent directly to the bot, or came to us
65
+ # via a channel.
66
+ #
67
+ # @return [Boolean]
68
+ #
69
+ def private?
70
+ channel == sender
60
71
  end
61
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-irc-bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Palmer