jabot 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff021f217c892d1015fe7791c05bc2bf095997cc
4
+ data.tar.gz: 902785b742bd60b0f56f1b1d973f608a33de684d
5
+ SHA512:
6
+ metadata.gz: 82fe2c43436564e7c89020ee325f69bd7ff051a93ed66be9ae8cd9f7dfdf108b2d130225b421c28423047a8f20a0cd236b3b787a5af4baaec08399efa39fe4d0
7
+ data.tar.gz: 94ef967101953ade0fe2258cca734d45bc7d4910e9ba7cd38889c646e1397fca2807d5b41eb7b04768b4eb2634a9a1e49fcc19bfef4e7cec6e014148201dc863
data/lib/jabot.rb ADDED
@@ -0,0 +1,94 @@
1
+ #encoding: utf-8
2
+
3
+ require 'jabot/jabber'
4
+ require 'jabot/commands'
5
+ require 'jabot/dsl'
6
+
7
+ module Jabot
8
+
9
+ class Base
10
+ include Jabot::DSL
11
+
12
+ attr_reader :jabber, :commands
13
+ attr_accessor :config
14
+
15
+ def initialize
16
+ @config = {
17
+ username: '',
18
+ password: '',
19
+ clients: []
20
+ }
21
+ @standalone_mode = false
22
+ end
23
+
24
+ def configure(&block)
25
+ @commands = Commands.new
26
+ standard_commands
27
+ instance_eval(&block)
28
+
29
+ @jabber = Jabber.new client_id: @config[:username], client_password: @config[:password]
30
+ @config[:clients].each do |item|
31
+ @jabber.add_remote_client item
32
+ end
33
+ end
34
+
35
+ def start_listen
36
+ @jabber.listen do |message, sender_id|
37
+ begin
38
+ result = @commands.run(message)
39
+ @jabber.send sender_id, result if result.is_a?(String) && !result.empty?
40
+ rescue Commands::CommandNotExists => e
41
+ @jabber.send sender_id, e.message
42
+ end
43
+ end
44
+
45
+ if @standalone_mode
46
+ puts 'Start listening...'
47
+ loop do
48
+ break unless @jabber.is_listen
49
+ end
50
+ end
51
+ end
52
+
53
+ #add standard commands
54
+ def standard_commands
55
+ command :quit do
56
+ @jabber.stop
57
+ end
58
+ end
59
+ end
60
+
61
+ #start jabot
62
+ #
63
+ #Example:
64
+ #Jabot.start do
65
+ # standalone_mode
66
+ #
67
+ # username 'username@jabber.com'
68
+ # password '*****'
69
+ # clients %w{client@jabber.com}
70
+ #
71
+ # command :download_file do |url, save_path|
72
+ # spawn("wget -c -O '#{save_path}' '#{url}'")
73
+ # end
74
+ #
75
+ # command :hello do
76
+ # 'Hello!'
77
+ # end
78
+ #end
79
+ def self.start(&block)
80
+ @base = Base.new
81
+ @base.configure(&block)
82
+ @base.start_listen
83
+ end
84
+
85
+ end
86
+
87
+ module REXML
88
+ class IOSource
89
+ def match(pattern, cons = false)
90
+ @buffer = @buffer.force_encoding('utf-8')
91
+ super(pattern, cons)
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,42 @@
1
+ #encoding: utf-8
2
+
3
+ module Jabot
4
+ class Commands
5
+
6
+ class CommandNotExists < StandardError; end
7
+
8
+ def initialize
9
+ @commands_list = {}
10
+ end
11
+
12
+ def add_command(name, &block)
13
+ @commands_list[name] = block unless command_exists? name
14
+ end
15
+
16
+ def command_exists?(command_name)
17
+ @commands_list.include? command_name
18
+ end
19
+
20
+ def run(command_line)
21
+ c = parse command_line
22
+ unless command_exists? c[:name].downcase
23
+ raise CommandNotExists, "command '#{c[:name]}' not found"
24
+ end
25
+
26
+ begin
27
+ @commands_list[ c[:name].downcase ].call( *c[:args] )
28
+ rescue
29
+ 'Error while executing command'
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
+ end
40
+
41
+ end
42
+ end
data/lib/jabot/dsl.rb ADDED
@@ -0,0 +1,25 @@
1
+
2
+ module Jabot
3
+ module DSL
4
+ def command(name, &block)
5
+ @commands.add_command(name, &block)
6
+ end
7
+
8
+ def username(value)
9
+ @config[:username] = value
10
+ end
11
+
12
+ def password(value)
13
+ @config[:password] = value
14
+ end
15
+
16
+ def clients(list)
17
+ raise StandardError, 'Clients list must be an array' unless list.is_a?(Array)
18
+ @config[:clients] = list
19
+ end
20
+
21
+ def standalone_mode
22
+ @standalone_mode = true
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,82 @@
1
+ #encoding: utf-8
2
+
3
+ require 'xmpp4r/client'
4
+
5
+ module Jabot
6
+
7
+ class Jabber
8
+ include ::Jabber
9
+ ::Jabber.debug = false
10
+
11
+ attr_reader :remote_clients
12
+ attr_reader :is_listen
13
+
14
+ def initialize(options)
15
+ @client_id = options[:client_id]
16
+ @client_password = options[:client_password]
17
+
18
+ @remote_clients = []
19
+ @is_listen = false
20
+
21
+ @client = Client::new JID::new(@client_id + '/bot')
22
+ #@client.on_exception do
23
+ # puts 'Reconnect JabberClient';
24
+ # connect
25
+ #end
26
+
27
+ connect
28
+ end
29
+
30
+ def connect
31
+ @client.connect
32
+ @client.auth @client_password
33
+
34
+ presence = Presence.new(:dnd, 'server running')
35
+ @client.send presence
36
+ end
37
+
38
+ def disconnect
39
+ @is_listen = false
40
+ @client.close
41
+ end
42
+
43
+ def add_remote_client(id)
44
+ @remote_clients << id unless @remote_clients.include?(id)
45
+ end
46
+
47
+ def send_to_all(text)
48
+ @remote_clients.each do |to|
49
+ send to, text
50
+ end
51
+ end
52
+
53
+ def send(to, text)
54
+ message = Message::new to, text
55
+ @client.send message
56
+ end
57
+
58
+ def listen
59
+ @is_listen = true
60
+
61
+ @message_callback = @client.add_message_callback 0, @message_callback do |m|
62
+ if m.type != :error
63
+ sender_id = "#{m.from.node}@#{m.from.domain}"
64
+ #puts' "Message received from "' + sender_id# + ": " + m.body
65
+ unless m.body.nil?
66
+ if @remote_clients.include?(sender_id)
67
+ yield(m.body, sender_id) if block_given?
68
+ else
69
+ #puts 'access denied'
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+
76
+ def stop
77
+ @is_listen = false
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,3 @@
1
+ module Jabot
2
+ VERSION = '0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jabot
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Skat
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: xmpp4r
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Jabber bot with DSL
56
+ email:
57
+ - andrey2004@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/jabot.rb
63
+ - lib/jabot/jabber.rb
64
+ - lib/jabot/dsl.rb
65
+ - lib/jabot/version.rb
66
+ - lib/jabot/commands.rb
67
+ homepage: https://github.com/andrey-skat/jabot
68
+ licenses: []
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.0.0
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Jabber bot with DSL
90
+ test_files: []