xmpp4r-hipchat 0.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: 603c26e12cea6bcc5d1f05887d4197ec4ac3f911
4
+ data.tar.gz: 929efa05d505f2e317fd5975ef67e758e1465311
5
+ SHA512:
6
+ metadata.gz: 5d5541cfad9e2bfed821bf98f7c7cc74d4fcd109b110c3624fd3f99e1995e534235aca269d96bee2e0fb05f34090b209532d5c71801b6098351aa6d3c1d7645c
7
+ data.tar.gz: 8221a73c118ec531bc890515df20bd2be0dceab26d5a8a33a22a18a8c4af12afe8a3bde2c87be1920f8e36ed31d84c00b43ea85f7cd92a28bb7a05bf71962d53
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Bartosz Kopiński
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bartosz Kopiński
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Xmpp4r::Hipchat
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'xmpp4r-hipchat'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install xmpp4r-hipchat
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,10 @@
1
+ require 'xmpp4r'
2
+ require 'xmpp4r/muc'
3
+ require 'xmpp4r/muc/x/muc'
4
+ require 'xmpp4r/muc/iq/mucowner'
5
+ require 'xmpp4r/muc/iq/mucadmin'
6
+ require 'xmpp4r/dataforms'
7
+ require 'xmpp4r/roster'
8
+ require 'xmpp4r/vcard'
9
+ require 'xmpp4r/hipchat/version'
10
+ require 'xmpp4r/hipchat/client'
@@ -0,0 +1,226 @@
1
+ module Jabber
2
+ module MUC
3
+ module Hipchat
4
+ class Client
5
+ attr_accessor :my_jid, :chat_domain, :conference_domain, :stream
6
+
7
+ def initialize(jid)
8
+ self.my_jid = JID.new(jid)
9
+ self.stream = Client.new(my_jid.strip) # TODO: Error Handling
10
+ Jabber::debuglog "Stream initialized"
11
+ self.chat_domain = my_jid.domain
12
+
13
+ @callbacks = Hash.new { |hash, key| hash[key] = CallbackList.new }
14
+ end
15
+
16
+ def join(jid, password = nil, opts = { history: false })
17
+ room_jid = JID.new(jid)
18
+ xmuc = XMUC.new
19
+ xmuc.password = password
20
+
21
+ if !opts[:history]
22
+ history = REXML::Element.new('history').tap{ |h| h.add_attribute('maxstanzas','0') }
23
+ xmuc.add_element history
24
+ end
25
+
26
+ room_jid.resource = name
27
+ set_presence(:available, room_jid, nil, xmuc) # TODO: Handle all join responses
28
+ end
29
+
30
+ def exit(jid, reason = nil)
31
+ room_jid = JID.new(jid)
32
+ Jabber::debuglog "Exiting #{jid}"
33
+ set_presence(:unavailable, room_jid, reason)
34
+ end
35
+
36
+ def keep_alive password
37
+ if stream.is_disconnected?
38
+ connect(password)
39
+ end
40
+ end
41
+
42
+ def name
43
+ my_jid.resource
44
+ end
45
+
46
+ def name= resource
47
+ my_jid.resource = resource
48
+ end
49
+
50
+ %w(lobby_presence room_presence room_message private_message invite).each do |callback_name|
51
+ define_method("on_#{callback_name}") do |prio = 0, ref = nil, &block|
52
+ @callbacks[callback_name.to_sym].add(prio, ref) do |*args|
53
+ block.call(*args)
54
+ end
55
+ end
56
+ end
57
+
58
+ def set_presence(type, to = nil, reason = nil, xmuc = nil, &block)
59
+ pres = Presence.new(:chat, reason)
60
+ pres.type = type
61
+ pres.to = to if to
62
+ pres.from = my_jid
63
+ pres.add(xmuc) if xmuc
64
+ stream.send(pres) { |r| block.call(r) }
65
+ end
66
+
67
+ def kick(recipients, room_jid)
68
+ iq = Iq.new(:set, room_jid)
69
+ iq.from = my_jid
70
+ iq.add(IqQueryMUCAdmin.new)
71
+ recipients.each do |recipient|
72
+ item = IqQueryMUCAdminItem.new
73
+ item.nick = recipient
74
+ item.role = :none
75
+ iq.query.add(item)
76
+ end
77
+ stream.send_with_id(iq)
78
+ end
79
+
80
+ def invite(recipients, room_jid)
81
+ msg = Message.new
82
+ msg.from = my_jid
83
+ msg.to = room_jid
84
+ x = msg.add(XMUCUser.new)
85
+ recipients.each do |jid|
86
+ x.add(XMUCUserInvite.new(jid))
87
+ end
88
+ stream.send(msg)
89
+ end
90
+
91
+ def send_message(type, jid, text, subject = nil)
92
+ message = Message.new(JID.new(jid), text.to_s)
93
+ message.type = type
94
+ message.from = my_jid
95
+ message.subject = subject
96
+
97
+ @send_thread.join if !@send_thread.nil? && @send_thread.alive?
98
+ @send_thread = Thread.new do
99
+ stream.send(message)
100
+ sleep(0.2)
101
+ end
102
+ end
103
+
104
+ def connect password
105
+ stream.connect
106
+ Jabber::debuglog "Connected to stream"
107
+ stream.auth(password)
108
+ Jabber::debuglog "Authenticated"
109
+ @muc_browser = MUCBrowser.new(stream)
110
+ Jabber::debuglog "MUCBrowser initialized"
111
+ self.conference_domain = @muc_browser.muc_rooms(chat_domain).keys.first
112
+ Jabber::debuglog "No conference domain found" if conference_domain.nil?
113
+ @roster = Roster::Helper.new(stream) # TODO: Error handling
114
+ @vcard = Vcard::Helper.new(stream) # TODO: Error handling
115
+ true
116
+ end
117
+
118
+ def activate_callbacks
119
+ stream.add_presence_callback(150, self){ |presence| handle_presence(presence) }
120
+ stream.add_message_callback(150, self){ |message| handle_message(message) }
121
+ Jabber::debuglog "Callbacks activated"
122
+ end
123
+
124
+ def get_rooms
125
+ iq = Iq.new(:get, conference_domain)
126
+ iq.from = stream.jid
127
+ iq.add(Discovery::IqQueryDiscoItems.new)
128
+
129
+ rooms = []
130
+ stream.send_with_id(iq) do |answer|
131
+ answer.query.each_element('item') do |item|
132
+ details = {}
133
+ item.first.children.each{ |c| details[c.name] = c.text }
134
+ rooms << {
135
+ item: item,
136
+ details: details
137
+ }
138
+ end
139
+ end
140
+ rooms
141
+ end
142
+
143
+ def get_users
144
+ @roster.wait_for_roster
145
+ @roster.items.map do |jid, item|
146
+ {
147
+ jid: item.jid.to_s,
148
+ name: item.iname,
149
+ mention: item.attributes['mention_name'],
150
+ }
151
+ end
152
+ end
153
+
154
+ def get_user_details user_jid
155
+ vcard = @vcard.get(user_jid)
156
+ {
157
+ email: vcard['EMAIL/USERID'],
158
+ title: vcard['TITLE'],
159
+ photo: vcard['PHOTO'],
160
+ }
161
+ end
162
+
163
+ def deactivate_callbacks
164
+ stream.delete_presence_callback(self)
165
+ stream.delete_message_callback(self)
166
+ Jabber::debuglog "Callbacks deactivated"
167
+ end
168
+
169
+ private
170
+
171
+ def handle_presence(presence)
172
+ from_jid = presence.from.strip.to_s
173
+ presence_type = presence.type.to_s
174
+ user_name = presence.from.resource
175
+ if user_name.nil?
176
+ @callbacks[:lobby_presence].process(from_jid, presence_type)
177
+ else
178
+ @callbacks[:room_presence].process(from_jid, user_name, presence_type)
179
+ end
180
+ end
181
+
182
+ def handle_message(message)
183
+ if is_invite?(message)
184
+ handle_invite(message)
185
+ elsif message.type == :chat
186
+ handle_private_message(message)
187
+ elsif message.type == :groupchat
188
+ handle_group_message(message)
189
+ elsif message.type == :error
190
+ handle_error(message)
191
+ end
192
+ end
193
+
194
+ def handle_invite(message)
195
+ room_name = message.children.last.first_element_text('name')
196
+ topic = message.children.last.first_element_text('topic')
197
+ room_jid = message.from.strip.to_s
198
+ user_name = message.from.resource
199
+ @callbacks[:invite].process(room_jid, user_name, room_name, topic)
200
+ end
201
+
202
+ def handle_private_message(message)
203
+ user_jid = message.from.strip.to_s
204
+ message_body = message.body.to_s
205
+ @callbacks[:private_message].process(user_jid, message_body)
206
+ end
207
+
208
+ def handle_group_message(message)
209
+ room_jid = message.from.strip.to_s
210
+ user_name = message.from.resource
211
+ message_body = message.body.to_s
212
+ topic = message.subject.to_s
213
+ @callbacks[:room_message].process(room_jid, user_name, message_body, topic)
214
+ end
215
+
216
+ def handle_error(message)
217
+ false
218
+ end
219
+
220
+ def is_invite?(message)
221
+ !message.x.nil? && message.x.kind_of?(XMUCUser) && message.x.first.kind_of?(XMUCUserInvite)
222
+ end
223
+ end
224
+ end
225
+ end
226
+ end
@@ -0,0 +1,7 @@
1
+ module Jabber
2
+ module MUC
3
+ module Hipchat
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'xmpp4r/hipchat/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'xmpp4r-hipchat'
8
+ spec.version = Jabber::MUC::Hipchat::VERSION
9
+ spec.authors = ['Bartosz Kopiński']
10
+ spec.email = ['bartosz.kopinski@gmail.com']
11
+ spec.description = 'HipChat client extension to xmpp4r'
12
+ spec.summary = 'HipChat client extension to xmpp4r'
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+ spec.add_runtime_dependency 'xmpp4r', ['~> 0.5']
21
+ spec.add_development_dependency 'bundler', '~> 1.4'
22
+ spec.add_development_dependency 'rake'
23
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xmpp4r-hipchat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bartosz Kopiński
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-28 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.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.4'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: HipChat client extension to xmpp4r
56
+ email:
57
+ - bartosz.kopinski@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/xmpp4r/hipchat.rb
68
+ - lib/xmpp4r/hipchat/client.rb
69
+ - lib/xmpp4r/hipchat/version.rb
70
+ - xmpp4r-hipchat.gemspec
71
+ homepage: ''
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.0.3
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: HipChat client extension to xmpp4r
95
+ test_files: []