jabot 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jabot.rb +17 -17
- data/lib/jabot/commands.rb +20 -20
- data/lib/jabot/dsl.rb +13 -14
- data/lib/jabot/jabber.rb +36 -40
- data/lib/jabot/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 115cb996f99108b9fc761fe4bef7f03f26a0b6ee
|
4
|
+
data.tar.gz: 4141684ea21bd308543c594acffe4d290841a95a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e4aa4e355c67f4ca36cc3c8b8c11fb148b8d98a50dc8ad9284745ff9cb0445022272f59f9a333c7f97ea249793b2f9db140ca355c7012ca7cae2ee757996b537
|
7
|
+
data.tar.gz: 26d8f741a7a19b5a59077fc4e52736de6bb2c440f5c98a1865433e3b409f5894843e04bc3f52bb3cb2d292c931441bfbe8b163c578c77170e4187d33d6b15af2
|
data/lib/jabot.rb
CHANGED
@@ -6,23 +6,23 @@ require 'jabot/dsl'
|
|
6
6
|
|
7
7
|
module Jabot
|
8
8
|
|
9
|
-
|
9
|
+
class Base
|
10
10
|
include Jabot::DSL
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
attr_reader :jabber, :commands
|
13
|
+
attr_accessor :config
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def initialize
|
16
|
+
@config = {
|
17
|
+
username: '',
|
18
|
+
password: '',
|
19
19
|
clients: []
|
20
|
-
|
20
|
+
}
|
21
21
|
@standalone_mode = false
|
22
22
|
end
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
def configure(&block)
|
25
|
+
@commands = Commands.new
|
26
26
|
standard_commands
|
27
27
|
instance_eval(&block)
|
28
28
|
|
@@ -32,7 +32,7 @@ module Jabot
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
|
35
|
+
def start_listen
|
36
36
|
@jabber.listen do |message, sender_id|
|
37
37
|
begin
|
38
38
|
result = @commands.run(message)
|
@@ -52,11 +52,11 @@ module Jabot
|
|
52
52
|
|
53
53
|
#add standard commands
|
54
54
|
def standard_commands
|
55
|
-
command :
|
56
|
-
@jabber.
|
55
|
+
command :stop do
|
56
|
+
@jabber.disconnect
|
57
57
|
end
|
58
58
|
end
|
59
|
-
|
59
|
+
end
|
60
60
|
|
61
61
|
#start jabot
|
62
62
|
#
|
@@ -76,10 +76,10 @@ module Jabot
|
|
76
76
|
# 'Hello!'
|
77
77
|
# end
|
78
78
|
#end
|
79
|
-
|
80
|
-
|
79
|
+
def self.start(&block)
|
80
|
+
@base = Base.new
|
81
81
|
@base.configure(&block)
|
82
|
-
|
82
|
+
@base.start_listen
|
83
83
|
end
|
84
84
|
|
85
85
|
end
|
data/lib/jabot/commands.rb
CHANGED
@@ -1,42 +1,42 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
|
3
3
|
module Jabot
|
4
|
-
|
4
|
+
class Commands
|
5
5
|
|
6
6
|
class CommandNotExists < StandardError; end
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
def initialize
|
9
|
+
@commands_list = {}
|
10
|
+
end
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def add_command(name, &block)
|
13
|
+
@commands_list[name] = block unless command_exists? name
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
def command_exists?(command_name)
|
17
|
+
@commands_list.include? command_name
|
18
18
|
end
|
19
19
|
|
20
|
-
|
20
|
+
def run(command_line)
|
21
21
|
c = parse command_line
|
22
22
|
unless command_exists? c[:name].downcase
|
23
23
|
raise CommandNotExists, "command '#{c[:name]}' not found"
|
24
24
|
end
|
25
25
|
|
26
26
|
begin
|
27
|
-
|
27
|
+
@commands_list[c[:name].downcase].call(*c[:args])
|
28
28
|
rescue
|
29
29
|
'Error while executing command'
|
30
30
|
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def parse(command_line)
|
34
|
-
args = command_line.split
|
35
|
-
{
|
36
|
-
:name => args.shift.to_sym,
|
37
|
-
:args => args
|
38
|
-
}
|
39
31
|
end
|
40
32
|
|
41
|
-
|
33
|
+
def parse(command_line)
|
34
|
+
args = command_line.split
|
35
|
+
{
|
36
|
+
:name => args.shift.to_sym,
|
37
|
+
:args => args
|
38
|
+
}
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
42
|
end
|
data/lib/jabot/dsl.rb
CHANGED
@@ -1,25 +1,24 @@
|
|
1
|
-
|
2
1
|
module Jabot
|
3
|
-
|
4
|
-
|
2
|
+
module DSL
|
3
|
+
def command(name, &block)
|
5
4
|
@commands.add_command(name, &block)
|
6
|
-
|
5
|
+
end
|
7
6
|
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
def username(value)
|
8
|
+
@config[:username] = value
|
9
|
+
end
|
11
10
|
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
def password(value)
|
12
|
+
@config[:password] = value
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
def clients(list)
|
16
|
+
raise StandardError, 'Clients list must be an array' unless list.is_a?(Array)
|
17
|
+
@config[:clients] = list
|
19
18
|
end
|
20
19
|
|
21
20
|
def standalone_mode
|
22
21
|
@standalone_mode = true
|
23
22
|
end
|
24
|
-
|
23
|
+
end
|
25
24
|
end
|
data/lib/jabot/jabber.rb
CHANGED
@@ -4,19 +4,19 @@ require 'xmpp4r/client'
|
|
4
4
|
|
5
5
|
module Jabot
|
6
6
|
|
7
|
-
|
8
|
-
|
7
|
+
class Jabber
|
8
|
+
include ::Jabber
|
9
9
|
::Jabber.debug = false
|
10
10
|
|
11
|
-
|
11
|
+
attr_reader :remote_clients
|
12
12
|
attr_reader :is_listen
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def initialize(options)
|
15
|
+
@client_id = options[:client_id]
|
16
|
+
@client_password = options[:client_password]
|
17
17
|
|
18
|
-
|
19
|
-
|
18
|
+
@remote_clients = []
|
19
|
+
@is_listen = false
|
20
20
|
|
21
21
|
@client = Client::new JID::new(@client_id + '/bot')
|
22
22
|
#@client.on_exception do
|
@@ -24,44 +24,44 @@ module Jabot
|
|
24
24
|
# connect
|
25
25
|
#end
|
26
26
|
|
27
|
-
|
27
|
+
connect
|
28
28
|
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
def connect
|
31
|
+
@client.connect
|
32
|
+
@client.auth @client_password
|
33
33
|
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
presence = Presence.new(:dnd, 'server running')
|
35
|
+
@client.send presence
|
36
|
+
end
|
37
37
|
|
38
|
-
|
38
|
+
def disconnect
|
39
39
|
@is_listen = false
|
40
|
-
|
41
|
-
|
40
|
+
@client.close
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
def add_remote_client(id)
|
44
|
+
@remote_clients << id unless @remote_clients.include?(id)
|
45
|
+
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
47
|
+
def send_to_all(text)
|
48
|
+
@remote_clients.each do |to|
|
49
|
+
send to, text
|
50
|
+
end
|
51
|
+
end
|
52
52
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
53
|
+
def send(to, text)
|
54
|
+
message = Message::new to, text
|
55
|
+
@client.send message
|
56
|
+
end
|
57
57
|
|
58
|
-
|
59
|
-
|
58
|
+
def listen
|
59
|
+
@is_listen = true
|
60
60
|
|
61
61
|
@message_callback = @client.add_message_callback 0, @message_callback do |m|
|
62
|
-
|
63
|
-
|
64
|
-
|
62
|
+
if m.type != :error
|
63
|
+
sender_id = "#{m.from.node}@#{m.from.domain}"
|
64
|
+
#puts' "Message received from "' + sender_id# + ": " + m.body
|
65
65
|
unless m.body.nil?
|
66
66
|
if @remote_clients.include?(sender_id)
|
67
67
|
yield(m.body, sender_id) if block_given?
|
@@ -73,10 +73,6 @@ module Jabot
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
|
77
|
-
@is_listen = false
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
76
|
+
end
|
81
77
|
|
82
78
|
end
|
data/lib/jabot/version.rb
CHANGED