libertybot 0.1
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.
- data/lib/libertybot.rb +95 -0
- data/lib/libertybot/bot.rb +29 -0
- data/lib/libertybot/client.rb +111 -0
- data/lib/libertybot/message.rb +79 -0
- data/lib/libertybot/prefix.rb +62 -0
- metadata +50 -0
data/lib/libertybot.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Some IRC Bot
|
2
|
+
# vim: sw=2 ts=2 ai et
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011 Fritz Grimpen
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
14
|
+
# copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
|
24
|
+
require "libertybot/client"
|
25
|
+
|
26
|
+
module LibertyBot
|
27
|
+
attr_accessor :client, :nick, :username, :realname, :prefix, :split_pattern
|
28
|
+
|
29
|
+
def initialize(client)
|
30
|
+
@client = client
|
31
|
+
@nick = nil
|
32
|
+
@username = nil
|
33
|
+
@realname = nil
|
34
|
+
@prefix = "!"
|
35
|
+
@split_pattern = /[\s]+/
|
36
|
+
@commands = {}
|
37
|
+
|
38
|
+
setup
|
39
|
+
end
|
40
|
+
|
41
|
+
def connect
|
42
|
+
@client.connect
|
43
|
+
@client.send("USER #{username} localhost localhost :#{realname}\r\n")
|
44
|
+
@client.send("NICK #{nick}\r\n")
|
45
|
+
end
|
46
|
+
|
47
|
+
def command(command, &block)
|
48
|
+
@commands = {} unless @commands
|
49
|
+
@commands[command] = block
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
def setup
|
54
|
+
@client.register "PING" do |msg|
|
55
|
+
@client.send("PONG #{client.server}\r\n")
|
56
|
+
end
|
57
|
+
|
58
|
+
@client.register "001" do |msg|
|
59
|
+
@nick = msg.params.first
|
60
|
+
self.connected
|
61
|
+
end
|
62
|
+
|
63
|
+
@client.register "433" do |msg|
|
64
|
+
@client.send("NICK #{msg.params[1]}_\r\n")
|
65
|
+
end
|
66
|
+
|
67
|
+
@client.register "PRIVMSG" do |msg|
|
68
|
+
parse_command(msg, msg.params.last[1..-1]) if msg.params.last[0] == @prefix
|
69
|
+
end
|
70
|
+
|
71
|
+
@client.register "NOTICE" do |msg|
|
72
|
+
parse_command(msg, msg.params.last)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def send(msg)
|
77
|
+
@client.send(msg)
|
78
|
+
end
|
79
|
+
|
80
|
+
def send_msg(rcpt, msg)
|
81
|
+
@client.send("NOTICE #{rcpt} :#{msg}\r\n")
|
82
|
+
end
|
83
|
+
|
84
|
+
def connected
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
def parse_command(message, msg)
|
89
|
+
command, *params = msg.split(@split_pattern)
|
90
|
+
|
91
|
+
return unless @commands.has_key? command
|
92
|
+
|
93
|
+
@commands[command].call(message, params)
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Some IRC Bot
|
2
|
+
# vim: sw=2 ts=2 ai et
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011 Fritz Grimpen
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
14
|
+
# copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
|
24
|
+
require "libertybot/client"
|
25
|
+
|
26
|
+
module LibertyBot
|
27
|
+
class Bot
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
# Some IRC Bot
|
2
|
+
# vim: sw=2 ts=2 ai et
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011 Fritz Grimpen
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
14
|
+
# copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
|
24
|
+
require "socket"
|
25
|
+
require "libertybot/message"
|
26
|
+
|
27
|
+
module LibertyBot
|
28
|
+
class Client
|
29
|
+
attr_accessor :debugging
|
30
|
+
attr_reader :server, :port, :connected
|
31
|
+
|
32
|
+
def initialize(server, port = 6667)
|
33
|
+
@debugging = false
|
34
|
+
@connected = true
|
35
|
+
@server = server
|
36
|
+
@port = port
|
37
|
+
@handlers = []
|
38
|
+
@socket = []
|
39
|
+
end
|
40
|
+
|
41
|
+
def connect
|
42
|
+
@connected = true
|
43
|
+
@socket = Socket.new(Socket::Constants::AF_INET, Socket::Constants::SOCK_STREAM, 0)
|
44
|
+
begin
|
45
|
+
@socket.connect(Socket::pack_sockaddr_in(@port, @server))
|
46
|
+
rescue Errno::EAFNOSUPPORT
|
47
|
+
@socket = Socket.new(Socket::Constants::AF_INET6, Socket::Constants::SOCK_STREAM, 0)
|
48
|
+
retry
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def register(command = nil, &block)
|
53
|
+
lambda = nil
|
54
|
+
if command
|
55
|
+
lambda = lambda do |message|
|
56
|
+
block.call(message) if message.command == command.to_s
|
57
|
+
end
|
58
|
+
else
|
59
|
+
lambda = block
|
60
|
+
end
|
61
|
+
@handlers << lambda
|
62
|
+
lambda.object_id
|
63
|
+
end
|
64
|
+
|
65
|
+
def unregister(object_id)
|
66
|
+
@handlers.delete_if do |callback|
|
67
|
+
callback.object_id == object_id
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def send(message)
|
72
|
+
connect unless @socket
|
73
|
+
|
74
|
+
if message.is_a? Array
|
75
|
+
message.each do |item|
|
76
|
+
send(item)
|
77
|
+
end
|
78
|
+
return
|
79
|
+
end
|
80
|
+
|
81
|
+
@socket.write(message.to_s)
|
82
|
+
puts message.to_s if @debugging
|
83
|
+
end
|
84
|
+
|
85
|
+
def start
|
86
|
+
connect unless @socket
|
87
|
+
while true
|
88
|
+
message = Message.parse(recv)
|
89
|
+
handle(message)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
protected
|
94
|
+
def recv
|
95
|
+
buffer = ""
|
96
|
+
|
97
|
+
until buffer[-1] == ?\n && buffer[-2] == ?\r
|
98
|
+
buffer += @socket.read 1
|
99
|
+
end
|
100
|
+
|
101
|
+
puts buffer if @debugging
|
102
|
+
buffer
|
103
|
+
end
|
104
|
+
|
105
|
+
def handle(message)
|
106
|
+
@handlers.each do |callback|
|
107
|
+
callback.call(message)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# Some IRC Bot
|
2
|
+
# vim: sw=2 ts=2 ai et
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011 Fritz Grimpen
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
14
|
+
# copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
|
24
|
+
require "libertybot/prefix"
|
25
|
+
|
26
|
+
module LibertyBot
|
27
|
+
class Message
|
28
|
+
attr_reader :prefix, :command, :params
|
29
|
+
|
30
|
+
def initialize(prefix, command, *params)
|
31
|
+
@prefix = Prefix.new(prefix.to_s)
|
32
|
+
@command = command
|
33
|
+
@params = params.flatten
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.parse(message)
|
37
|
+
prefix, command, params = nil, nil, []
|
38
|
+
components = message.split(" ")
|
39
|
+
|
40
|
+
if components.first[0] == ":"
|
41
|
+
prefix = components.first[1..-1]
|
42
|
+
components.shift
|
43
|
+
end
|
44
|
+
|
45
|
+
command = components.first
|
46
|
+
components.shift
|
47
|
+
|
48
|
+
components.each do |component|
|
49
|
+
if params.last.to_s[0] == ":"
|
50
|
+
params << params.pop + " " + component
|
51
|
+
next
|
52
|
+
end
|
53
|
+
|
54
|
+
params << component
|
55
|
+
end
|
56
|
+
|
57
|
+
params << params.pop[1..-1] if params.last[0] == ":"
|
58
|
+
|
59
|
+
self.new(prefix, command, params)
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s
|
63
|
+
str = ""
|
64
|
+
|
65
|
+
str << ":#{@prefix} " unless @prefix.empty?
|
66
|
+
str << @command
|
67
|
+
|
68
|
+
@params.each do |param|
|
69
|
+
str << " "
|
70
|
+
str << ":" if param.index " "
|
71
|
+
str << param
|
72
|
+
end
|
73
|
+
|
74
|
+
str << "\r\n"
|
75
|
+
|
76
|
+
str
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# Some IRC Bot
|
2
|
+
# vim: sw=2 ts=2 ai et
|
3
|
+
#
|
4
|
+
# Copyright (c) 2011 Fritz Grimpen
|
5
|
+
#
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
11
|
+
# furnished to do so, subject to the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
14
|
+
# copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
# SOFTWARE.
|
23
|
+
|
24
|
+
module LibertyBot
|
25
|
+
class Prefix < String
|
26
|
+
def extract
|
27
|
+
nick, user, host = [ nil ] * 3
|
28
|
+
|
29
|
+
user_offset = self.index ?!
|
30
|
+
|
31
|
+
host_offset = self.index ?@
|
32
|
+
host_offset = user_offset + self[(user_offset)..-1].index(?@) if user_offset && self[(user_offset)..-1].index(?@)
|
33
|
+
|
34
|
+
host = self[(host_offset + 1)..-1] if host_offset
|
35
|
+
nick = self[0..(user_offset - 1)] if user_offset
|
36
|
+
|
37
|
+
if user_offset && host_offset
|
38
|
+
user = self[(user_offset + 1)..(host_offset - 1)]
|
39
|
+
elsif host_offset
|
40
|
+
nick = self[0..(host_offset - 1)]
|
41
|
+
elsif user_offset
|
42
|
+
user = self[(user_offset + 1)..-1]
|
43
|
+
else
|
44
|
+
nick = self
|
45
|
+
end
|
46
|
+
|
47
|
+
[ nick, user, host ]
|
48
|
+
end
|
49
|
+
|
50
|
+
def nick
|
51
|
+
extract[0]
|
52
|
+
end
|
53
|
+
|
54
|
+
def user
|
55
|
+
extract[1]
|
56
|
+
end
|
57
|
+
|
58
|
+
def host
|
59
|
+
extract[2]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: libertybot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Fritz Grimpen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email: fritz+libertirc@grimpen.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/libertybot/message.rb
|
21
|
+
- lib/libertybot/prefix.rb
|
22
|
+
- lib/libertybot/bot.rb
|
23
|
+
- lib/libertybot/client.rb
|
24
|
+
- lib/libertybot.rb
|
25
|
+
homepage: http://grimpen.net/libertybot/
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 1.8.11
|
47
|
+
signing_key:
|
48
|
+
specification_version: 3
|
49
|
+
summary: Some IRC Bot
|
50
|
+
test_files: []
|