net-yail 1.6.1 → 1.6.2
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/simple/kick.rb +39 -0
- data/lib/net/yail.rb +20 -4
- data/lib/net/yail/yail-version.rb +1 -1
- data/tests/mock_irc.rb +12 -0
- data/tests/tc_yail.rb +29 -0
- metadata +53 -39
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
# Want a specific version of net/yail? Try uncommenting this:
|
4
|
+
# gem 'net-yail', '1.x.y'
|
5
|
+
|
6
|
+
require 'net/yail'
|
7
|
+
require 'getopt/long'
|
8
|
+
|
9
|
+
# User specifies channel and nick
|
10
|
+
opt = Getopt::Long.getopts(
|
11
|
+
['--network', Getopt::REQUIRED],
|
12
|
+
['--nick', Getopt::REQUIRED],
|
13
|
+
['--port', Getopt::REQUIRED],
|
14
|
+
['--loud', Getopt::BOOLEAN]
|
15
|
+
)
|
16
|
+
|
17
|
+
opts = {
|
18
|
+
:address => opt['network'],
|
19
|
+
:username => 'FrakkingBot',
|
20
|
+
:realname => 'John Botfrakker',
|
21
|
+
:nicknames => [opt['nick']],
|
22
|
+
}
|
23
|
+
opts[:port] = opt['port'] if opt['port']
|
24
|
+
|
25
|
+
irc = Net::YAIL.new(opts)
|
26
|
+
|
27
|
+
irc.log.level = Logger::DEBUG if opt['loud']
|
28
|
+
|
29
|
+
# Register handlers
|
30
|
+
irc.heard_welcome { |e| irc.join('#bots') } # Filter - runs after the server's welcome message is read
|
31
|
+
|
32
|
+
# KICK example - kicks everybody from the channel other than self
|
33
|
+
data = {}
|
34
|
+
irc.heard_join do |e|
|
35
|
+
irc.kick(e.nick, e.channel, "I'm USING THE BATHROOM! Give me a minute!") unless e.nick == irc.me
|
36
|
+
end
|
37
|
+
|
38
|
+
# Start the bot and enjoy the endless loop
|
39
|
+
irc.start_listening!
|
data/lib/net/yail.rb
CHANGED
@@ -2,6 +2,7 @@ require 'socket'
|
|
2
2
|
require 'thread'
|
3
3
|
require 'yaml'
|
4
4
|
require 'logger'
|
5
|
+
require 'openssl'
|
5
6
|
|
6
7
|
# To make this library seem smaller, a lot of code has been split up and put
|
7
8
|
# into semi-logical files. I don't really like this hacky solution, but I
|
@@ -244,7 +245,7 @@ module Net
|
|
244
245
|
# contain a comma-separated list of channels, which will restrict the list to the given channels.
|
245
246
|
# If <tt>server</tt> is present, the request is forwarded to the given server.
|
246
247
|
# * <tt>invite(nick, channel)</tt>: Invites a user to the given channel.
|
247
|
-
# * <tt>kick(nick, channel, [message])</tt>:
|
248
|
+
# * <tt>kick(nick, channel, [message])</tt>: Kicks the given user from the given channel with an optional message
|
248
249
|
# * <tt>whois(nick, [server]): Issues a WHOIS command for the given nickname with an optional server.
|
249
250
|
#
|
250
251
|
# =Simple Example
|
@@ -538,7 +539,6 @@ class YAIL
|
|
538
539
|
|
539
540
|
# If user asked for SSL, this is where we set it all up
|
540
541
|
def setup_ssl
|
541
|
-
require 'openssl'
|
542
542
|
ssl_context = OpenSSL::SSL::SSLContext.new()
|
543
543
|
ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
544
544
|
@socket = OpenSSL::SSL::SSLSocket.new(@socket, ssl_context)
|
@@ -553,8 +553,10 @@ class YAIL
|
|
553
553
|
# Simple non-ssl socket == return a single line
|
554
554
|
return [@socket.gets] unless @ssl
|
555
555
|
|
556
|
-
# SSL socket == return all lines available
|
557
|
-
|
556
|
+
# SSL socket == return all lines available - but preserve newlines for servers that split
|
557
|
+
# up words! Newlines tell us where commands end!
|
558
|
+
messages = @socket.readpartial(OpenSSL::Buffering::BLOCK_SIZE)
|
559
|
+
return messages.split(/(#{$/})/).each_slice(2).map(&:join)
|
558
560
|
end
|
559
561
|
|
560
562
|
# Reads incoming data - should only be called by io_loop, and only when
|
@@ -576,6 +578,20 @@ class YAIL
|
|
576
578
|
|
577
579
|
# Chomp and push each message
|
578
580
|
for message in messages
|
581
|
+
# Message must have one of \r or \n at the end of it, otherwise it's a partial command and
|
582
|
+
# we need to hang onto it to join it with the next message
|
583
|
+
if message !~ /[\r\n]+$/
|
584
|
+
@prepend_message ||= ""
|
585
|
+
@prepend_message += message.dup
|
586
|
+
next
|
587
|
+
end
|
588
|
+
|
589
|
+
# If we had a partial message recently, attach it to the new message and clear it out
|
590
|
+
if @prepend_message
|
591
|
+
message = @prepend_message + message
|
592
|
+
@prepend_message = nil
|
593
|
+
end
|
594
|
+
|
579
595
|
message.chomp!
|
580
596
|
@log.debug "+++INCOMING: #{message.inspect}"
|
581
597
|
|
data/tests/mock_irc.rb
CHANGED
@@ -46,6 +46,13 @@ class MockIRC
|
|
46
46
|
return output
|
47
47
|
end
|
48
48
|
|
49
|
+
# Hack for SSL mocking - it's expected tests will set the broken lines however we want them, so
|
50
|
+
# this just spits out what gets would have
|
51
|
+
def readpartial(size)
|
52
|
+
data = gets
|
53
|
+
return data || ""
|
54
|
+
end
|
55
|
+
|
49
56
|
# Hack to let YAIL know if we have data to read
|
50
57
|
def ready?
|
51
58
|
return @output.empty? ? nil : true
|
@@ -160,4 +167,9 @@ class MockIRC
|
|
160
167
|
def add_output(*args)
|
161
168
|
args.each {|arg| @output.push(arg + "\n")}
|
162
169
|
end
|
170
|
+
|
171
|
+
# Sets up our internal string to add a broken line (no newline character)
|
172
|
+
def add_partial_output(string)
|
173
|
+
@output.push(string)
|
174
|
+
end
|
163
175
|
end
|
data/tests/tc_yail.rb
CHANGED
@@ -406,4 +406,33 @@ class YailSessionTest < Test::Unit::TestCase
|
|
406
406
|
assert_equal({:not_bad => 1}, @msg)
|
407
407
|
end
|
408
408
|
end
|
409
|
+
|
410
|
+
def test_split_messages
|
411
|
+
# We have to set up a whole new YAIL for this since split messages only work on SSL
|
412
|
+
@yail = Net::YAIL.new(
|
413
|
+
:io => @mockirc, :address => "fake-irc.nerdbucket.com", :log => @log,
|
414
|
+
:nicknames => ["Bot"], :realname => "Net::YAIL", :username => "Username",
|
415
|
+
:use_ssl => true
|
416
|
+
)
|
417
|
+
|
418
|
+
@privmsg = []
|
419
|
+
@yail.on_msg { |event| @privmsg.push({:channel => event.channel, :nick => event.nick, :message => event.message}) }
|
420
|
+
@mockirc.add_partial_output ":Nerdmaster!nerd@nerdbucket.com PRIVMSG #foosball :First line\n"
|
421
|
+
@mockirc.add_partial_output ":Nerdmaster!nerd@n"
|
422
|
+
@mockirc.add_partial_output "erdbucket.com PRIVMSG"
|
423
|
+
@mockirc.add_partial_output " #foosball :Second line\n:Nerdmaster!nerd@nerdbucket.com PRIVMSG #foosball :Third line\n"
|
424
|
+
@yail.start_listening
|
425
|
+
wait_for_irc
|
426
|
+
|
427
|
+
assert_equal(@privmsg.length, 3)
|
428
|
+
|
429
|
+
0.upto(2) do |idx|
|
430
|
+
assert_equal("Nerdmaster", @privmsg[idx][:nick])
|
431
|
+
assert_equal("#foosball", @privmsg[idx][:channel])
|
432
|
+
end
|
433
|
+
|
434
|
+
assert_equal("First line", @privmsg[0][:message])
|
435
|
+
assert_equal("Second line", @privmsg[1][:message])
|
436
|
+
assert_equal("Third line", @privmsg[2][:message])
|
437
|
+
end
|
409
438
|
end
|
metadata
CHANGED
@@ -1,88 +1,102 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: net-yail
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 6
|
9
|
+
- 2
|
10
|
+
version: 1.6.2
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Jeremy Echols
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
17
|
+
|
18
|
+
date: 2013-04-04 00:00:00 Z
|
13
19
|
dependencies: []
|
14
|
-
description: ! 'Net::YAIL is an IRC library written in pure Ruby. Using simple functions,
|
15
|
-
it
|
16
20
|
|
21
|
+
description: |-
|
22
|
+
Net::YAIL is an IRC library written in pure Ruby. Using simple functions, it
|
17
23
|
is trivial to build a complex, event-driven IRC application, such as a bot or
|
18
|
-
|
19
24
|
even a full command-line client. All events can have a single callback and
|
20
|
-
|
21
25
|
any number of before-callback and after-callback filters. Even outgoing events,
|
22
|
-
|
23
26
|
such as when you join a channel or send a message, can have filters for stats
|
24
|
-
|
25
|
-
gathering, text filtering, etc.'
|
27
|
+
gathering, text filtering, etc.
|
26
28
|
email: yail<at>nerdbucket dot com
|
27
29
|
executables: []
|
30
|
+
|
28
31
|
extensions: []
|
32
|
+
|
29
33
|
extra_rdoc_files: []
|
30
|
-
|
34
|
+
|
35
|
+
files:
|
31
36
|
- examples/simple/dumbbot.rb
|
32
37
|
- examples/simple/whois.rb
|
33
|
-
- examples/
|
38
|
+
- examples/simple/kick.rb
|
34
39
|
- examples/logger/logger_bot.rb
|
40
|
+
- examples/logger/default.yml
|
35
41
|
- examples/logger/run.rb
|
36
42
|
- lib/net/yail.rb
|
43
|
+
- lib/net/yail/magic_events.rb
|
44
|
+
- lib/net/yail/eventmap.yml
|
37
45
|
- lib/net/yail/IRCBot.rb
|
46
|
+
- lib/net/yail/message_parser.rb
|
38
47
|
- lib/net/yail/default_events.rb
|
39
|
-
- lib/net/yail/
|
48
|
+
- lib/net/yail/output_api.rb
|
40
49
|
- lib/net/yail/event.rb
|
41
|
-
- lib/net/yail/
|
42
|
-
- lib/net/yail/handler.rb
|
50
|
+
- lib/net/yail/yail-version.rb
|
43
51
|
- lib/net/yail/irc_bot.rb
|
44
52
|
- lib/net/yail/legacy_events.rb
|
45
|
-
- lib/net/yail/magic_events.rb
|
46
|
-
- lib/net/yail/message_parser.rb
|
47
|
-
- lib/net/yail/output_api.rb
|
48
53
|
- lib/net/yail/report_events.rb
|
49
|
-
- lib/net/yail/
|
54
|
+
- lib/net/yail/dispatch.rb
|
55
|
+
- lib/net/yail/handler.rb
|
56
|
+
- tests/net_yail.rb
|
57
|
+
- tests/tc_message_parser.rb
|
50
58
|
- tests/tc_event.rb
|
51
59
|
- tests/tc_yail.rb
|
52
|
-
- tests/tc_message_parser.rb
|
53
60
|
- tests/mock_irc.rb
|
54
|
-
- tests/net_yail.rb
|
55
61
|
homepage: http://ruby-irc-yail.nerdbucket.com/
|
56
62
|
licenses: []
|
63
|
+
|
57
64
|
post_install_message:
|
58
|
-
rdoc_options:
|
65
|
+
rdoc_options:
|
59
66
|
- -m
|
60
67
|
- Net::YAIL
|
61
68
|
- -f
|
62
69
|
- sdoc
|
63
|
-
require_paths:
|
70
|
+
require_paths:
|
64
71
|
- lib
|
65
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
73
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
82
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
hash: 3
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
77
90
|
requirements: []
|
91
|
+
|
78
92
|
rubyforge_project: net-yail
|
79
93
|
rubygems_version: 1.8.24
|
80
94
|
signing_key:
|
81
95
|
specification_version: 3
|
82
|
-
summary:
|
83
|
-
test_files:
|
96
|
+
summary: "Yet Another IRC Library: wrapper for IRC communications in Ruby."
|
97
|
+
test_files:
|
98
|
+
- tests/net_yail.rb
|
99
|
+
- tests/tc_message_parser.rb
|
84
100
|
- tests/tc_event.rb
|
85
101
|
- tests/tc_yail.rb
|
86
|
-
- tests/tc_message_parser.rb
|
87
102
|
- tests/mock_irc.rb
|
88
|
-
- tests/net_yail.rb
|