jabber_bot 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/jabber_bot.rb +113 -0
- metadata +58 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e6ce4dbdf977033c6c50068456e7b69394a4319b
|
4
|
+
data.tar.gz: fb660ad339c082f19a9f2898d9ff6a79b8341b01
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 146a25d9cd43b5b437d6e04e6f09589403028e1a5340cb66db203399587f5ff6ea672ce4e1c0508490df41b48e43d893b378208d80d49735517fc806439e8e3d
|
7
|
+
data.tar.gz: 2943be9329335e8ec2552d76426684c1df0cbe08728983f511d8755360dc7d0016bcf475d1a6651bcfefce92b3b805fe6b2f270848697f95c00eb49985638b5f
|
data/lib/jabber_bot.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'xmpp4r'
|
2
|
+
include Jabber
|
3
|
+
|
4
|
+
class JabberBot
|
5
|
+
|
6
|
+
def initialize(config)
|
7
|
+
@config = config
|
8
|
+
@keep_alive_status = false
|
9
|
+
@commands = {}
|
10
|
+
@jabber = Client.new(JID::new(@config['JID'] + '/' + @config['resource']))
|
11
|
+
#add_default_commands
|
12
|
+
keep_alive
|
13
|
+
end
|
14
|
+
|
15
|
+
def connect
|
16
|
+
begin
|
17
|
+
if not @jabber.is_connected?
|
18
|
+
@keep_alive_status = false
|
19
|
+
@jabber.connect(@config['host'])
|
20
|
+
@jabber.auth(@config['password'])
|
21
|
+
@jabber.send(Presence.new.set_type(:available))
|
22
|
+
end
|
23
|
+
rescue Exception => e
|
24
|
+
puts "Error connecting: #{e} (#{e.class})!"
|
25
|
+
ensure
|
26
|
+
@keep_alive_status = true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def disconnect
|
31
|
+
@keep_alive_status = false;
|
32
|
+
@jabber.close
|
33
|
+
end
|
34
|
+
|
35
|
+
def listen
|
36
|
+
@jabber.add_message_callback do |message|
|
37
|
+
handle_message(message) unless message.body.to_s == ''
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def say(to, message)
|
42
|
+
@jabber.send(Message.new(to, message).set_type(:chat))
|
43
|
+
end
|
44
|
+
|
45
|
+
def add_command(command, syntax, description, public = false, &callback)
|
46
|
+
@commands[command.downcase] = {
|
47
|
+
'syntax' => syntax,
|
48
|
+
'description' => description,
|
49
|
+
'callback' => callback,
|
50
|
+
'public' => public
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def run_command(command, params)
|
55
|
+
return @commands[command]['callback'].call(params)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
def handle_message(message)
|
60
|
+
command = message.body.to_s.split(' ', 2)[0]
|
61
|
+
command = command_available?(command)
|
62
|
+
Thread.new do
|
63
|
+
begin
|
64
|
+
if @config['operators'].include?(message.from.to_s.sub(/\/.+$/, '')) or @commands[command]['public']
|
65
|
+
params = {}
|
66
|
+
params['parameters'] = message.body.to_s.split(' ', 2)[1]
|
67
|
+
params['jabber'] = self
|
68
|
+
params['message'] = message
|
69
|
+
response = run_command(command, params)
|
70
|
+
else
|
71
|
+
response = 'Sorry, this command is not public or you\'re not an operator.'
|
72
|
+
end
|
73
|
+
rescue Exception => e
|
74
|
+
response = "#{e} (#{e.class})!"
|
75
|
+
ensure
|
76
|
+
say(sender, response.to_s)
|
77
|
+
end
|
78
|
+
end unless command == nil
|
79
|
+
end
|
80
|
+
|
81
|
+
def command_available?(input)
|
82
|
+
input = input.downcase.chomp(' ')
|
83
|
+
if @commands.has_key?(input)
|
84
|
+
return input
|
85
|
+
else
|
86
|
+
@commands.each_key do |command|
|
87
|
+
if command.index(input, 0) == 0
|
88
|
+
return command
|
89
|
+
end
|
90
|
+
end
|
91
|
+
return nil
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def keep_alive
|
96
|
+
Thread.new do
|
97
|
+
while true
|
98
|
+
if @keep_alive_status
|
99
|
+
if @jabber.is_connected?
|
100
|
+
begin
|
101
|
+
@jabber.send(Presence.new.set_type(:available))
|
102
|
+
rescue Exception => e
|
103
|
+
puts "Error while keeping alive: #{e} (#{e.class})!"
|
104
|
+
end
|
105
|
+
else
|
106
|
+
connect
|
107
|
+
end
|
108
|
+
end
|
109
|
+
sleep(120)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jabber_bot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krunoslav Husak
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-05-27 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: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Convert numbers e.g. between 0.0 - 1.0 and 0.0 - Math::PI and vice-versa.
|
28
|
+
email: kruno@binel.hr
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/jabber_bot.rb
|
34
|
+
homepage: https://github.com/h00s/jabber_bot
|
35
|
+
licenses:
|
36
|
+
- MIT
|
37
|
+
metadata: {}
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
requirements: []
|
53
|
+
rubyforge_project:
|
54
|
+
rubygems_version: 2.2.2
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: Easily convert numbers between different ranges
|
58
|
+
test_files: []
|