autoremote 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ebe641cad96a7e7a75fd16bdaa9bbbec0405edb6
4
+ data.tar.gz: 79fe1ad622754f2e4de3f7d09e73a87b8590df03
5
+ SHA512:
6
+ metadata.gz: cd3e45d529755266758d133c809e8aa53ea60636c345e1567678e207124f4efec68fed867dd01068ddb88b448d0aadbf8ba26d50d356bc69e2598507e21bd215
7
+ data.tar.gz: f37f8b79c77f40f2cc318cea425aeb89e71285ac9e62e6e63e164dd52ecbec69f0761eafe43613dfd0053feef1fbe73f7d62a03fc1d97e81b1b6ae8cf215eb45
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in autoremote.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
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.
@@ -0,0 +1,54 @@
1
+ # Autoremote
2
+
3
+ A library that makes it easier to interact with your other autoremote devices.
4
+ This project incudes both a library and an executable that uses the library.
5
+
6
+ Devices are saved with sqlite3 in ~/.autoremote/devices.db
7
+
8
+ If you don't know how to get your personal key [follow this link](http://joaoapps.com/autoremote/personal/)
9
+
10
+ ## Installation
11
+
12
+ $ gem install autoremote
13
+
14
+ ## Usage
15
+
16
+ ### Executable
17
+ $ autoremote add NAME KEY Save device"
18
+ $ autoremote remove NAME Removes device"
19
+ $ autoremote list [WITHKEY] Lists all devices"
20
+ $ autoremote message NAME MESSAGE Send a message to a device"
21
+ $ autoremote register NAME HOST Register this computer to the device"
22
+
23
+ ### Library
24
+
25
+ ```ruby
26
+ require 'autoremote'
27
+
28
+ # This will save a device for use later
29
+ AutoRemote.addDevice( name, key )
30
+
31
+ # Removes a device
32
+ AutoRemote.removeDevice( name )
33
+
34
+ # List all saved devices
35
+ AutoRemote.list
36
+
37
+ # Send a message to a device
38
+ # The parameter device can either be a Device object or the name of the device
39
+ AutoRemote.sendMessage( device, message )
40
+
41
+ # Register on the device.
42
+ # This has the same effect as following the guide on http://joaoapps.com/autoremote/linux/)
43
+ # device can either be a Device object or the name of the device
44
+ # host can be either a hostname or ip-address, but they have to be public (i.e. reachable from the internet)
45
+ AutoRemote.registerOnDevice( device, host )
46
+ ```
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/autoremote/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
Binary file
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'autoremote/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "autoremote"
8
+ spec.version = AutoRemote::VERSION
9
+ spec.authors = ["Yurij"]
10
+ spec.email = ["yurij@yurijware.com"]
11
+ spec.summary = %q{A library for interacting with autoremote devices http://joaoapps.com/autoremote/}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.7"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+
23
+ spec.add_runtime_dependency 'activerecord', '~> 4.2', '~> 4.2'
24
+ spec.add_runtime_dependency 'sqlite3', '~> 1.3', '~> 1.3'
25
+ end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+ require 'autoremote'
3
+
4
+ def to_bool( string )
5
+ return false if string.class == NilClass || string.empty? || string.downcase =~ (/^(false|f|no|n|nej|0)$/i)
6
+ return true if string.downcase =~ (/^(true|t|yes|y|ja|j|1)$/i)
7
+ raise ArgumentError, "Invalid value: #{string}"
8
+ end
9
+
10
+ ## Prints help
11
+ def print_help
12
+ puts "AutoRemote v" + AutoRemote::VERSION
13
+ puts "\nArguments:"
14
+ puts " add DEVICE KEY Save device"
15
+ puts " remove DEVICE Removes device"
16
+ puts " list [WITHKEY] Lists all devices"
17
+ puts " message DEVICE MESSAGE Send a message to a device"
18
+ puts " register DEVICE HOST Register this computer to the device"
19
+ exit()
20
+ end
21
+
22
+ arg0 = ARGV[0].downcase if ARGV[0]
23
+
24
+ if arg0 == "help" || arg0 == "-h" || arg0 == "--help"
25
+ print_help
26
+
27
+ elsif arg0 == "add" && ARGV[1] && ARGV[2]
28
+ puts "Adding device"
29
+ begin
30
+ AutoRemote.addDevice( ARGV[1], ARGV[2] )
31
+ puts "Device added successfully"
32
+ rescue Exception => e
33
+ puts "Error: #{e.message}"
34
+ end
35
+
36
+ elsif arg0 == "remove" && ARGV[1]
37
+ puts "Removing device"
38
+ begin
39
+ AutoRemote.removeDevice( ARGV[1] )
40
+ puts "Device removed successfully"
41
+ rescue Exception => e
42
+ puts "Error: #{e.message}"
43
+ end
44
+
45
+ elsif arg0 == "list"
46
+ withkey = to_bool(ARGV[1])
47
+ puts "Listing devices"
48
+
49
+ AutoRemote.listDevices.each do|device|
50
+ print "Name: #{device.name}"
51
+ print " Key: #{device.key}" if withkey
52
+ puts
53
+ end
54
+
55
+ elsif arg0 == "message" && ARGV[1] && ARGV[2]
56
+ begin
57
+ AutoRemote::sendMessage( ARGV[1], ARGV[2] )
58
+ puts "Message sent successfully"
59
+ rescue Exception => e
60
+ puts "Error: #{e.message}"
61
+ end
62
+
63
+ elsif arg0 == "register" && ARGV[1] && ARGV[2]
64
+ begin
65
+ AutoRemote::registerOnDevice( ARGV[1], ARGV[2] )
66
+ puts "Device registered successfully"
67
+ rescue Exception => e
68
+ puts "Error: #{e.message}"
69
+ end
70
+
71
+ else
72
+ print_help
73
+ end
@@ -0,0 +1,129 @@
1
+ require "autoremote/version"
2
+ require "autoremote/exceptions"
3
+ # require File.expand_path( File.join( File.dirname( __FILE__ ), 'autoremote', 'version' ) )
4
+ # require File.expand_path( File.join( File.dirname( __FILE__ ), 'autoremote', 'exceptions' ) )
5
+ require 'sqlite3'
6
+ require 'active_record'
7
+ require 'net/http'
8
+
9
+ ## Establish the database connection
10
+ ActiveRecord::Base.establish_connection(
11
+ :adapter => 'sqlite3',
12
+ :database => ENV['HOME'] + '/.autoremote/devices.db'
13
+ )
14
+
15
+ class Device < ActiveRecord::Base
16
+ end
17
+
18
+ ## Create the database table
19
+ ActiveRecord::Schema.define do
20
+ break if ActiveRecord::Base.connection.table_exists? 'devices'
21
+ create_table :devices do |table|
22
+ table.column :name, :string
23
+ table.column :key, :string
24
+ end
25
+ end
26
+
27
+ module AutoRemote
28
+ REGCMD = "curl \"http://autoremotejoaomgcd.appspot.com/registerpc?key=%YOUR_KEY%&name=%DISPLAY_NAME%&id=%UNIQUE_ID%&type=linux&publicip=%PUBLIC_HOST%&localip=$(sudo ifconfig eth0 |grep \"inet addr\" |awk '{print $2}' |awk -F: '{print $2}')\""
29
+ MSGURL = "http://autoremotejoaomgcd.appspot.com/sendmessage?key=%YOUR_KEY%&message=%MESSAGE%&sender=%SENDER_ID%"
30
+ VALIDATIONURL = "http://autoremotejoaomgcd.appspot.com/sendmessage?key=%YOUR_KEY%"
31
+
32
+ ## Add a device
33
+ def AutoRemote::addDevice( name, key )
34
+ ## Check if the name is taken
35
+ if Device.find_by_name( name ) || Device.find_by_key(key)
36
+ raise self::DeviceAlreadyExist#
37
+ end
38
+
39
+ ## Validate key
40
+ url = URI.parse( VALIDATIONURL.sub( /%YOUR_KEY%/, key ) )
41
+ result = Net::HTTP.start(url.host, url.port) {|http|
42
+ http.request( Net::HTTP::Get.new( url.to_s ) )
43
+ }
44
+ if result.body != "OK"
45
+ raise self::InvalidKey
46
+ end
47
+
48
+ ## Save the device
49
+ Device.create(:name => name, :key => key)
50
+ return true
51
+ end
52
+
53
+ ## Remove a specific device
54
+ def AutoRemote::removeDevice( name )
55
+ if device = Device.find_by_name(name)
56
+
57
+ ## Remove the device
58
+ return Device.delete(device.id)
59
+ else
60
+ raise self::DeviceNotFound
61
+ end
62
+ end
63
+
64
+ ## Returns a list with all devices
65
+ def AutoRemote::listDevices
66
+ return Device.order("name").all
67
+ end
68
+
69
+ ## Returns one specific device
70
+ def AutoRemote::getDevice( name )
71
+ return Device.find_by_name( name )
72
+ end
73
+
74
+ ## Send a message to a device
75
+ def AutoRemote::sendMessage( device, message )
76
+ if ! device.kind_of?( Device ) && ! ( device = Device.find_by_name( device ) )
77
+ raise self::DeviceNotFound
78
+ elsif ! message.kind_of?( String )
79
+ raise TypeError, "Message must be a string"
80
+ end
81
+
82
+ hostname = `hostname`.strip
83
+
84
+ ## Send the message
85
+ url = URI.parse( MSGURL.sub( /%YOUR_KEY%/, device.key ).sub( /%MESSAGE%/, message ).sub( /%SENDER_ID%/, hostname ) )
86
+ result = Net::HTTP.start(url.host, url.port) {|http|
87
+ http.request( Net::HTTP::Get.new( url.to_s ) )
88
+ }
89
+
90
+ ## Check result
91
+ if result.body != "OK"
92
+ raise self::InvalidKey
93
+ end
94
+
95
+ return true
96
+ end
97
+
98
+ ## Register on the device
99
+ def AutoRemote::registerOnDevice( device, remotehost )
100
+
101
+ if ! device.kind_of?( Device ) && ! ( device = Device.find_by_name( device ) )
102
+ raise self::DeviceNotFound
103
+ elsif ! remotehost.kind_of?( String ) || remotehost.length < 5
104
+ raise ArgumentError, "remotehost must be a string of 5 chars or more"
105
+ end
106
+
107
+ hostname = `hostname`.strip
108
+
109
+ ## Perform the registration
110
+ cmd = REGCMD.sub( /%YOUR_KEY%/, device.key ).sub(/%DISPLAY_NAME%/, hostname ).sub(/%UNIQUE_ID%/, hostname ).sub(/%PUBLIC_HOST%/, remotehost )
111
+ result = system(cmd)
112
+ puts
113
+
114
+ ## Check result
115
+ if ! result
116
+ raise self::AutoRemoteException, "Something went wrong when registering on the device"
117
+ end
118
+
119
+ return true
120
+ end
121
+
122
+ ## Define alases for some methods
123
+ class << AutoRemote
124
+ alias :saveDevice :addDevice
125
+ alias :deleteDevice :removeDevice
126
+ alias :sendMsg :sendMessage
127
+ alias :regOnDevice :registerOnDevice
128
+ end
129
+ end
@@ -0,0 +1,23 @@
1
+ module AutoRemote
2
+ class AutoRemoteException < StandardError
3
+ end
4
+
5
+ class DeviceNotFound < AutoRemoteException
6
+ def message
7
+ "Device doesn't exist"
8
+ end
9
+ end
10
+
11
+ class DeviceAlreadyExist < AutoRemoteException
12
+ def message
13
+ "Device already exist"
14
+ end
15
+ end
16
+
17
+ class InvalidKey < AutoRemoteException
18
+ def message
19
+ "The key is invalid"
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,3 @@
1
+ module AutoRemote
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: autoremote
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Yurij
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ description:
70
+ email:
71
+ - yurij@yurijware.com
72
+ executables:
73
+ - autoremote
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - autoremote-devices.db
83
+ - autoremote.gemspec
84
+ - bin/autoremote
85
+ - lib/autoremote.rb
86
+ - lib/autoremote/exceptions.rb
87
+ - lib/autoremote/version.rb
88
+ homepage: ''
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.4.5
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: A library for interacting with autoremote devices http://joaoapps.com/autoremote/
112
+ test_files: []