del 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: afaa566255eb3aacdbafd40be9fb851aea94ce127c66971c2b45d56e2b4727e0
4
+ data.tar.gz: 1ab8ea7c7c52ee69ce24e9bb5d9b80d0ead4a95419a84435d5a1354e739a643a
5
+ SHA512:
6
+ metadata.gz: c4e725a47f715ab3150b0c568914c2f938674236c2dfec677daab4080b621403d2bac9da0468bf7da5e249a3abd745a6532470876f769b574a0855914b68c0c1
7
+ data.tar.gz: c6b53590d00feaafabda49b5c9c0c11a76f1219664cf842d302a2a1f52f6e9ab719306b5f45653dcf97eb4cbb2a8905e0e9362463b395780190735a72eec11cd
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in del.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 mo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Del
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/del`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'del'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install del
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/del.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "del"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/del.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "del/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "del"
8
+ spec.version = Del::VERSION
9
+ spec.authors = ["mo"]
10
+ spec.email = ["mo@mokhan.ca"]
11
+
12
+ spec.summary = %q{Del is a funky robosapien.}
13
+ spec.description = %q{Del is a funky robosapien.}
14
+ spec.homepage = "https://www.mokhan.ca"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_dependency "dotenv", "~> 2.4"
25
+ spec.add_dependency "xmpp4r", "~> 0.5"
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
data/exe/del ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "del"
4
+
5
+ Del.start
data/lib/del.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "dotenv"
2
+ require "xmpp4r"
3
+ require "xmpp4r/muc/helper/mucbrowser"
4
+ require "xmpp4r/muc/helper/simplemucclient"
5
+ require "xmpp4r/roster/helper/roster"
6
+
7
+ require "del/connection"
8
+ require "del/robot"
9
+ require "del/room_repository"
10
+ require "del/user"
11
+ require "del/user_repository"
12
+ require "del/version"
13
+
14
+ module Del
15
+ def self.start
16
+ Dotenv.load(".env.local", Pathname.new(Dir.home).join(".delrc").to_s)
17
+
18
+ del = Robot.new(configuration: {
19
+ host: ENV.fetch("DEL_HOST"),
20
+ jid: ENV.fetch("DEL_JID"),
21
+ muc_domain: ENV.fetch("DEL_MUC_DOMAIN"),
22
+ password: ENV.fetch("DEL_PASSWORD"),
23
+ })
24
+ del.run
25
+ end
26
+ end
@@ -0,0 +1,67 @@
1
+ module Del
2
+ class Connection
3
+ attr_reader :configuration, :rooms, :users
4
+
5
+ def initialize(configuration:, users:, rooms:)
6
+ @configuration = configuration
7
+ @rooms = rooms
8
+ @users = users
9
+ end
10
+
11
+ def connect(robot)
12
+ client.on_exception do |error, connection, error_source|
13
+ disconnect
14
+ end
15
+ client.connect(configuration[:host])
16
+ sleep 0.0001 until client.is_connected?
17
+ client.auth(configuration[:password])
18
+ roster = Jabber::Roster::Helper.new(client, false)
19
+ roster.add_update_callback do |old_item, item|
20
+ users.create(item) if item
21
+ end
22
+ roster.get_roster
23
+ roster.wait_for_roster
24
+ #@mention_name = roster[jid].attributes["mention_name"]
25
+ client.add_message_callback do |message|
26
+ robot.receive(message)
27
+ end
28
+ client.send(Jabber::Presence.new(:chat))
29
+ list_rooms(configuration[:muc_domain]).each do |room|
30
+ rooms.upsert(room)
31
+ end
32
+ end
33
+
34
+ def deliver(jid, message)
35
+ message = Jabber::Message.new(jid, encode_string(message))
36
+ message.type = :chat
37
+ client.send(message)
38
+ end
39
+
40
+ def disconnect
41
+ puts "byte me!"
42
+ client.close
43
+ end
44
+
45
+ private
46
+
47
+ def client
48
+ @client ||= Jabber::Client.new(jid)
49
+ end
50
+
51
+ def jid
52
+ jid = Jabber::JID.new(configuration[:jid])
53
+ jid.resource = "bot"
54
+ jid
55
+ end
56
+
57
+ def list_rooms(muc_domain)
58
+ Jabber::MUC::MUCBrowser.new(client).muc_rooms(muc_domain).map do |jid, name|
59
+ jid.to_s
60
+ end
61
+ end
62
+
63
+ def encode_string(s)
64
+ s.encode("UTF-8", invalid: :replace, undef: :replace)
65
+ end
66
+ end
67
+ end
data/lib/del/robot.rb ADDED
@@ -0,0 +1,31 @@
1
+ module Del
2
+ class Robot
3
+ attr_reader :connection, :users, :rooms
4
+
5
+ def initialize(configuration:)
6
+ @users = UserRepository.new
7
+ @rooms = RoomRepository.new
8
+ @connection = Connection.new(
9
+ configuration: configuration,
10
+ users: users,
11
+ rooms: rooms,
12
+ )
13
+ end
14
+
15
+ def run
16
+ connection.connect(self)
17
+ sleep
18
+ rescue Interrupt
19
+ connection.disconnect
20
+ end
21
+
22
+ def receive(message)
23
+ return if message.type == :error || message.body.nil?
24
+ send_message(message.from, message.body)
25
+ end
26
+
27
+ def send_message(jid, message)
28
+ connection.deliver(jid, message)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,9 @@
1
+ class RoomRepository
2
+ def initialize
3
+ @rooms = Set.new
4
+ end
5
+
6
+ def upsert(room)
7
+ @rooms << room
8
+ end
9
+ end
data/lib/del/user.rb ADDED
@@ -0,0 +1,5 @@
1
+ class User
2
+ def initialize(attributes)
3
+ @attributes = attributes
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class UserRepository
2
+ def initialize
3
+ @users = Set.new
4
+ end
5
+
6
+ def create(item)
7
+ @users << User.new(item.attributes)
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Del
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: del
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: xmpp4r
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: Del is a funky robosapien.
84
+ email:
85
+ - mo@mokhan.ca
86
+ executables:
87
+ - del
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - del.gemspec
101
+ - exe/del
102
+ - lib/del.rb
103
+ - lib/del/connection.rb
104
+ - lib/del/robot.rb
105
+ - lib/del/room_repository.rb
106
+ - lib/del/user.rb
107
+ - lib/del/user_repository.rb
108
+ - lib/del/version.rb
109
+ homepage: https://www.mokhan.ca
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.7.6
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Del is a funky robosapien.
133
+ test_files: []