opensrs-email 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.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWJmMjU4NDA2N2UzOThiNmJjNWJjY2MyMjAwMDQ3YzAyMTA3NWY5OA==
5
+ data.tar.gz: !binary |-
6
+ MmJjYzFjODk1Y2QxZTQ0YjIwYWMyYjljMmY0NDBmMmU4YjFiYjI0ZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NGEzNzEyZTIyZjIyMWFkZDdmOGQ2MGU2OGZjNDA3NDU3ZGNkMDcxOTJmODRk
10
+ YTQyYTFjN2Q3NmI5YWMxOTcyNTRmNmM2ZjZkYTBjNzQwODM4NjgyZTcyNGFh
11
+ MjAwMTg5MzM4MTcxM2FiMjA4ZTJjMTk0NjdhOTZiYTA1ZGMzZTE=
12
+ data.tar.gz: !binary |-
13
+ OTFiMzViM2IwYWE0ZDRiMGRiYWUzMjVkOThhZjc3MGM5N2ZlMDFmYTJjNWJm
14
+ YTI4NmQ0ZDdhZjcyZDRhZDJiMmZiZTExYzUyMjFlZmU5MmYwMGFhODMyN2Q0
15
+ YmJkZjA4MjRkNGY1OTkyZTA5NzE0YTA0NjM5ZDQ0YTllYzhmMjc=
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in opensrs-email.gemspec
4
+ gemspec
5
+
6
+ gem "rspec", "~> 2.0"
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Striking.ly Inc
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,33 @@
1
+ # Opensrs::Email
2
+
3
+ [![Build Status](https://travis-ci.org/strikingly/opensrs-email.png?branch=master)](https://travis-ci.org/strikingly/opensrs-email)
4
+
5
+ API to manage opensrs domain emails through APP (OpenSRS Email Service Account Provisioning Protocol)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'opensrs-email'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install opensrs-email
20
+
21
+ ## Usage
22
+
23
+ gateway = Opensrs::Email::Gateway.new(OPENSRS_EMAIL_SERVER, OPENSRS_EMAIL_PORT, OPENSRS_EMAIL_USERNAME, OPENSRS_EMAIL_DOMAIN, OPENSRS_EMAIL_PASSWORD)
24
+
25
+ gateway.delete_mailbox_forward_only(mailbox, domain)
26
+
27
+ ## Contributing
28
+
29
+ 1. Fork it
30
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
31
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
32
+ 4. Push to the branch (`git push origin my-new-feature`)
33
+ 5. Create new Pull Request
@@ -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
@@ -0,0 +1,154 @@
1
+ require "opensrs/email/version"
2
+
3
+ require 'opensrs/email/domain'
4
+ require 'opensrs/email/workgroup'
5
+ require 'opensrs/email/mailbox'
6
+
7
+ require 'socket'
8
+ require 'openssl'
9
+ require 'logger'
10
+
11
+ LOGGER = Logger.new(STDOUT)
12
+ LOGGER.level = Logger::INFO
13
+
14
+ module Opensrs
15
+ module Email
16
+ class Gateway
17
+
18
+ include Opensrs::Email::Domain
19
+ include Opensrs::Email::Mailbox
20
+ include Opensrs::Email::Workgroup
21
+
22
+ def initialize(server, port, user, domain, password, version = '3.4', logger = LOGGER)
23
+ @server = server
24
+ @port = port
25
+ @user = user
26
+ @domain = domain
27
+ @password = password
28
+ @version = version
29
+ @logger = logger
30
+ @loggedin = false
31
+
32
+ open_connection
33
+
34
+ response = receive_response
35
+ @logger.info("Initial response:\n#{response}")
36
+
37
+ response = send_version
38
+ @logger.info("Version response:\n#{response}")
39
+
40
+ response = login
41
+ @logger.info("Login response:\n#{response}")
42
+
43
+ if response[:status] == 'OK'
44
+ @loggedin = true
45
+ else
46
+ @loggedin = false
47
+ end
48
+ end
49
+
50
+ def loggedin?
51
+ @loggedin
52
+ end
53
+
54
+ def open_connection
55
+ @connection = TCPSocket.new(@server, @port)
56
+ @socket = OpenSSL::SSL::SSLSocket.new(@connection) if @connection
57
+
58
+ @socket.sync_close = true # synchronise connection close of ssl layer and underlying tcp socket
59
+ @socket.connect
60
+ end
61
+
62
+ def send_version
63
+ command = "VER VER=\"#{@version}\"\r\n.\r\n"
64
+ send_command(command)
65
+ receive_response
66
+ end
67
+
68
+ def login
69
+ command = "LOGIN USER=\"#{@user}\" DOMAIN=\"#{@domain}\" PASSWORD=\"#{@password}\"\r\n.\r\n"
70
+ send_command(command)
71
+ receive_response
72
+ end
73
+
74
+ def quit
75
+ command = "QUIT\r\n.\r\n"
76
+ send_command(command)
77
+ end
78
+
79
+ def close_connection
80
+ @socket.close if @socket and not @socket.closed?
81
+ @connection.close if @connection and not @connection.closed?
82
+
83
+ @socket = @connection = nil
84
+ end
85
+
86
+ def make_command_string(command, attributes)
87
+ cmd = command.to_s
88
+ attr = attributes.map {|k, v|
89
+ "#{k.to_s}=\"#{v.to_s}\""
90
+ } .join(' ')
91
+ cmd << " #{attr}\r\n.\r\n"
92
+ end
93
+
94
+ def call(command, attributes = {})
95
+ if not (MAIL_COMMANDS.include?(command) or
96
+ DOMAIN_COMMANDS.include?(command) or
97
+ WORKGROUP_COMMAND.include?(command))
98
+ raise "Command #{command.to_s} invalid"
99
+ else
100
+ cmd = make_command_string(command, attributes)
101
+ send_command(cmd)
102
+ receive_response
103
+ end
104
+ end
105
+
106
+ def send_command(command)
107
+ if @socket and not @socket.closed?
108
+ @logger.info("Sending command:\n#{command}")
109
+ @socket.write(command)
110
+ end
111
+ end
112
+
113
+ def receive_response
114
+ response = build_response
115
+ parse_response(response)
116
+ end
117
+
118
+ def parse_response(response)
119
+ match_data = /^(OK|ER) (\d+).*/.match(response)
120
+
121
+ status = if match_data and match_data.length > 2 then
122
+ match_data[1]
123
+ else
124
+ nil
125
+ end
126
+
127
+ status_code = if match_data and match_data.length > 2 then
128
+ match_data[2].to_i
129
+ else
130
+ nil
131
+ end
132
+
133
+ return {
134
+ :status => status,
135
+ :status_code => status_code,
136
+ :response_body => response
137
+ }
138
+ end
139
+
140
+ def build_response(partial="")
141
+ line = receive_line
142
+ if line == ".\r\n"
143
+ partial
144
+ else
145
+ build_response(partial + line)
146
+ end
147
+ end
148
+
149
+ def receive_line
150
+ @socket.gets
151
+ end
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,34 @@
1
+ module Opensrs
2
+ module Email
3
+ module Domain
4
+ DOMAIN_COMMANDS = [
5
+ :create_domain,
6
+ :create_domain_alias,
7
+ :delete_domain,
8
+ :delete_domain_alias,
9
+ :create_domain_welcome_email,
10
+ :get_company_domains,
11
+ :delete_domain_welcome_email,
12
+ :get_domain,
13
+ :get_domain_brand,
14
+ :change_domain,
15
+ :set_domain_admin,
16
+ :set_domain_brand,
17
+ :set_domain_catch_all_mailbox,
18
+ :set_domain_disabled_status,
19
+ :get_num_domain_mailboxes,
20
+ :get_domain_mailboxes,
21
+ :get_domain_mailbox_limits,
22
+ :set_domain_mailbox_limits,
23
+ :set_domain_allow_list,
24
+ :set_domain_block_list,
25
+ :get_domain_allow_list,
26
+ :get_domain_block_list
27
+ ]
28
+ def create_domain(domain)
29
+ call(:create_domain, {:domain => domain})
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,59 @@
1
+ module Opensrs
2
+ module Email
3
+ module Mailbox
4
+ MAIL_COMMANDS = [
5
+ :get_mailbox_availability,
6
+ :get_alternate_mailbox_names,
7
+ :create_mailbox,
8
+ :set_mail_admin,
9
+ :set_mail_admin,
10
+ :change_mailbox,
11
+ :create_alias_mailbox,
12
+ :rename_mailbox,
13
+ :set_mailbox_allow_list,
14
+ :set_mailbox_block_list,
15
+ :get_mailbox_allow_list,
16
+ :get_mailbox_block_list,
17
+ :verify_password,
18
+ :get_admin,
19
+ :get_mailbox,
20
+ :get_mailbox_any,
21
+ :get_mailbox_services,
22
+ :set_mailbox_services,
23
+ :get_mailbox_quota,
24
+ :set_mailbox_quota,
25
+ :delete_mailbox,
26
+ :delete_mailbox_any,
27
+ :set_mailbox_autorespond,
28
+ :get_mailbox_autorespond,
29
+ :set_mailbox_forward,
30
+ :get_mailbox_forward,
31
+ :show_available_offerings,
32
+ :show_enabled_offerings,
33
+ :disable_offering,
34
+ :enable_offering,
35
+ :set_mailbox_suspension,
36
+ :get_mailbox_suspension,
37
+ :set_mailbox_status,
38
+ :get_mailbox_status,
39
+ :create_group_alias_mailbox,
40
+ :get_group_alias_mailbox,
41
+ :get_group_alias_mailbox,
42
+ :change_group_alias_mailbox,
43
+ :delete_group_alias_mailbox,
44
+ :create_mailbox_forward_only,
45
+ :get_mailbox_forward_only,
46
+ :change_mailbox_forward_only,
47
+ :delete_mailbox_forward_only,
48
+ ]
49
+
50
+ def create_mailbox_forward_only(mailbox, domain, forward_email, workgroup)
51
+ call(:create_mailbox_forward_only, {:mailbox => mailbox, :domain => domain, :forward_email => forward_email, :workgroup => workgroup})
52
+ end
53
+
54
+ def delete_mailbox_forward_only(mailbox, domain)
55
+ call(:delete_mailbox_forward_only, {:mailbox => mailbox, :domain => domain})
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,5 @@
1
+ module Opensrs
2
+ module Email
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ module Opensrs
2
+ module Email
3
+ module Workgroup
4
+ WORKGROUP_COMMAND = [
5
+ :create_workgroup,
6
+ :get_domain_workgroups,
7
+ :delete_workgroup,
8
+ :set_workgroup_admin
9
+ ]
10
+ end
11
+ end
12
+ 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 'opensrs/email/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "opensrs-email"
8
+ spec.version = Opensrs::Email::VERSION
9
+ spec.authors = ["Li Zhenchao"]
10
+ spec.email = ["zhenchao@strikingly.com"]
11
+ spec.description = %q{opensrs-email}
12
+ spec.summary = %q{API to manage opensrs domain emails through APP (OpenSRS Email Service Account Provisioning Protocol)}
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
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Opensrs::Email::Gateway do
4
+ let! (:gateway) { Opensrs::Email::Gateway.new('admin.test.hostedemail.com', 4449, 'admin', 'domain.com', 'password') }
5
+
6
+ describe "Gateway" do
7
+ it "responds to create_domain" do
8
+ expect(gateway).to respond_to(:create_domain)
9
+ end
10
+
11
+ it "responds to create_mailbox_forward_only" do
12
+ expect(gateway).to respond_to(:create_mailbox_forward_only)
13
+ end
14
+
15
+ it "responds to delete_mailbox_forward_only" do
16
+ expect(gateway).to respond_to(:delete_mailbox_forward_only)
17
+ end
18
+
19
+ it "parses successful response" do
20
+ response_body = "OK 0 Mailbox created\r\n.\r\n"
21
+ response = gateway.parse_response(response_body)
22
+ expect(response[:status]).to eq("OK")
23
+ expect(response[:status_code]).to eq(0)
24
+ end
25
+
26
+ it "parses error response" do
27
+ response_body = "ER 16 Mailbox already existed\r\n.\r\n"
28
+ response = gateway.parse_response(response_body)
29
+ expect(response[:status]).to eq("ER")
30
+ expect(response[:status_code]).to eq(16)
31
+ end
32
+
33
+ it "does not parse status code other than OK and ER" do
34
+ response_body = "BL 33 Invalid status code\r\n.\r\n"
35
+ response = gateway.parse_response(response_body)
36
+ expect(response[:status]).to eq(nil)
37
+ expect(response[:status_code]).to eq(nil)
38
+ end
39
+
40
+ it "builds command string correctly" do
41
+ command = :create_mailbox_forward_only
42
+ attributes = {:mailbox => "test", :domain => "testdomain.com", :forward_email => "john@mail.com", :workgroup => "staff"}
43
+
44
+ cmd = gateway.make_command_string(command, attributes)
45
+
46
+ expect(cmd).to match(/create_mailbox_forward_only/)
47
+
48
+ expect(cmd).to match(/mailbox=\"test\"/)
49
+
50
+ expect(cmd).to match(/domain=\"testdomain[.]com\"/)
51
+
52
+ expect(cmd).to match(/forward_email=\"john@mail.com\"/)
53
+
54
+ expect(cmd).to match(/workgroup=\"staff\"/)
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,2 @@
1
+ require 'opensrs/email.rb'
2
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: opensrs-email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Li Zhenchao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-24 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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
+ description: opensrs-email
42
+ email:
43
+ - zhenchao@strikingly.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - .travis.yml
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/opensrs/email.rb
55
+ - lib/opensrs/email/domain.rb
56
+ - lib/opensrs/email/mailbox.rb
57
+ - lib/opensrs/email/version.rb
58
+ - lib/opensrs/email/workgroup.rb
59
+ - opensrs-email.gemspec
60
+ - spec/email_spec.rb
61
+ - spec/spec_helper.rb
62
+ homepage: ''
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: API to manage opensrs domain emails through APP (OpenSRS Email Service Account
86
+ Provisioning Protocol)
87
+ test_files:
88
+ - spec/email_spec.rb
89
+ - spec/spec_helper.rb