onlyoffice_iredmail_helper 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9871762acd906ea08deb24f3f27ad70a705a77381dc78efe717a2c3fa543a644
4
+ data.tar.gz: d6ec13250ef152c8bc686947358c328b60c3124ccc94172134a58840c2d9bf33
5
+ SHA512:
6
+ metadata.gz: 0b9640e4a34616eba140e99dd20f1e25438fbc1a5c09cce770b25f9a5d713af922e965ee8f50453f25d5ec81a306d06ba0b53cad193fe853680970b1ca7239e2
7
+ data.tar.gz: e0cb81aaca023ab6c20f9e3640b27ec4d5ae56140b68ba7010b33902f35ecb3a4265674771d7d86e3185834d307978e9d8fc63caa4fd05ea01c60733d036a8ef
@@ -0,0 +1,249 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'date'
4
+ require 'mail'
5
+ require 'net/imap'
6
+ require 'net/smtp'
7
+ require 'onlyoffice_logger_helper'
8
+ require 'time'
9
+ require 'yaml'
10
+ require_relative 'onlyoffice_iredmail_helper/mailboxes_methods'
11
+ require_relative 'onlyoffice_iredmail_helper/mail_getters'
12
+ require_relative 'onlyoffice_iredmail_helper/message_methods'
13
+ require_relative 'onlyoffice_iredmail_helper/version'
14
+
15
+ # Namespace of Gem
16
+ module OnlyofficeIredmailHelper
17
+ # Class for working with mail
18
+ class IredMailHelper
19
+ include MailboxesMethods
20
+ include MailGetters
21
+ include MessageMethods
22
+ attr_reader :username
23
+
24
+ def initialize(options = {})
25
+ read_defaults
26
+ @domainname = options[:domainname] || @default_domain
27
+ @username = options[:username] || @default_user
28
+ @password = options[:password] || @default_password
29
+ @subject = options[:subject] || @default_subject
30
+ @body = options[:body]
31
+ @mailbox_for_archive = 'checked'
32
+ end
33
+
34
+ # @return [String] inspect info
35
+ def inspect
36
+ "IredMailHelper domain: #{@domainname}, " \
37
+ "user: #{@username}, " \
38
+ "subject: #{@subject}"
39
+ end
40
+
41
+ # Login to email via IMAP
42
+ # @return [nil]
43
+ def login
44
+ @imap = Net::IMAP.new(@domainname)
45
+ @imap.authenticate('LOGIN', @username, @password)
46
+ end
47
+
48
+ # Form a email string
49
+ # @param msg_data [Hash] params
50
+ # @return [String] formed mail message
51
+ def create_msg(msg_data = {})
52
+ <<~END_OF_MESSAGE
53
+ From: #{@username}
54
+ To: #{msg_data[:mailto]}
55
+ Subject: #{msg_data[:subject]}
56
+ Date: #{Time.now.rfc2822}
57
+ Message-Id: "#{Digest::SHA2.hexdigest(Time.now.to_i.to_s)}@#{@username.split('@').last}"
58
+
59
+ #{msg_data[:body]}
60
+ END_OF_MESSAGE
61
+ end
62
+
63
+ # Send mail
64
+ # @param options [Hash] hash with params
65
+ # @return [nil]
66
+ def send_mail(options = {})
67
+ options[:subject] ||= @default_subject
68
+ options[:body] ||= @default_body
69
+ options[:mailto] ||= @default_user
70
+ smtp = Net::SMTP.start(@domainname, 25, @username, @username, @password, :login)
71
+ smtp.send_message create_msg(options), @username, options[:mailto]
72
+ smtp.finish
73
+ end
74
+
75
+ # Delete all messages in inbox
76
+ # @return [nil]
77
+ def delete_all_messages
78
+ login
79
+ @imap.select('INBOX')
80
+ @imap.store(@imap.search(['ALL']), '+FLAGS', [:Deleted]) unless @imap.search(['ALL']).empty?
81
+ OnlyofficeLoggerHelper.log('Delete all messages')
82
+ close
83
+ end
84
+
85
+ # Delete email by subject
86
+ # @param subject [String] email title
87
+ # @return [nil]
88
+ def delete_email_by_subject(subject)
89
+ login
90
+ @imap.select('INBOX')
91
+ id_emails = @imap.search(['SUBJECT', subject.dup.force_encoding('ascii-8bit')])
92
+ @imap.store(id_emails, '+FLAGS', [:Deleted]) unless id_emails.empty?
93
+ close
94
+ end
95
+
96
+ # Get email
97
+ # @param options [Hash] options of get
98
+ # @param times [Integer] count to check
99
+ # @return [Hash] mail
100
+ def mail_by_subject(options = {}, times = 300)
101
+ login
102
+ @imap.select('INBOX')
103
+ start_time = Time.now
104
+ while Time.now - start_time < times
105
+ get_emails_search_or_new(options).each do |message_id|
106
+ mail = get_mail_data(message_id)
107
+ mail_subject_found = mail[:subject].to_s.upcase
108
+ mail_subject_to_be_found = options[:subject].to_s.upcase
109
+ next unless mail_subject_found.include? mail_subject_to_be_found
110
+
111
+ if options[:move_out]
112
+ move_out_message(message_id)
113
+ else
114
+ mark_message_as_seen(message_id)
115
+ end
116
+ return mail
117
+ end
118
+ end
119
+ nil
120
+ end
121
+
122
+ # Check if message exists by params
123
+ # @param options [Hash] options of get
124
+ # @param times [Integer] count to check
125
+ # @return [True, False] result of check
126
+ def check_email_by_subject(options = {}, times = 300, move_out = false)
127
+ login
128
+ @imap.select('INBOX')
129
+ start_time = Time.now
130
+ while Time.now - start_time < times
131
+ get_emails_search_or_new(options).each do |message_id|
132
+ string_found = get_mail_data(message_id, options[:search])[:subject].to_s.upcase.gsub(/\s+/, ' ')
133
+ string_to_be_found = options[:subject].to_s.upcase.gsub(/\s+/, ' ')
134
+ next unless string_found.include? string_to_be_found
135
+
136
+ if move_out
137
+ move_out_message(message_id)
138
+ else
139
+ mark_message_as_seen(message_id)
140
+ end
141
+ return true
142
+ end
143
+ end
144
+ false
145
+ end
146
+
147
+ # Get email by subject
148
+ # @param options [Hash] options of get
149
+ # @param times [Integer] count to check
150
+ # @param move_out [True, False] should message to move out
151
+ # @return [Hash] mail
152
+ def get_email_by_subject(options = {}, times = 300, move_out = false)
153
+ login
154
+ @imap.select('INBOX')
155
+ start_time = Time.now
156
+ while Time.now - start_time < times
157
+ get_emails_search_or_new(options).each do |message_id|
158
+ mail = get_mail_data(message_id)
159
+ string_found = mail[:subject].to_s.upcase.gsub(/\s+/, ' ')
160
+ string_to_be_found = options[:subject].to_s.upcase.gsub(/\s+/, ' ')
161
+ next unless string_found.include? string_to_be_found
162
+
163
+ if move_out
164
+ move_out_message(message_id)
165
+ else
166
+ mark_message_as_seen(message_id)
167
+ end
168
+ return mail
169
+ end
170
+ end
171
+ nil
172
+ end
173
+
174
+ private
175
+
176
+ # Correct close of mail account
177
+ # @return [Void]
178
+ def close
179
+ @imap.close
180
+ @imap.logout
181
+ @imap.disconnect
182
+ end
183
+
184
+ # Move out message to `checked` directory
185
+ # @param message_id [String] id of message
186
+ # @return [Void]
187
+ def move_out_message(message_id)
188
+ create_mailbox(@mailbox_for_archive) unless mailboxes.include?(@mailbox_for_archive)
189
+
190
+ login
191
+ @imap.select('INBOX')
192
+ @imap.copy(message_id, @mailbox_for_archive)
193
+ @imap.store(message_id, '+FLAGS', [:Deleted])
194
+ @imap.expunge
195
+ close
196
+ end
197
+
198
+ # Get email list via search or all unseen
199
+ # @param options [Hash] options to search
200
+ # @return [Array<Mail>] list of mails
201
+ def get_emails_search_or_new(options)
202
+ if options[:search]
203
+ @imap.search([(options[:search_type] || 'BODY').to_s.upcase, options[:search]])
204
+ else
205
+ @imap.search(['UNSEEN'])
206
+ end
207
+ end
208
+
209
+ # Get mail data by mail id
210
+ # @param message_id [Integer] id of message
211
+ # @param search [String] param to search
212
+ # @return [Hash] found data
213
+ def get_mail_data(message_id, search = nil)
214
+ msg = @imap.fetch(message_id, 'RFC822')[0].attr['RFC822']
215
+ mail = Mail.read_from_string(msg)
216
+ subject = mail.subject.force_encoding('utf-8')
217
+ body = mail.text_part
218
+ body = body.body.to_s.gsub(/\s+/, ' ').strip.force_encoding('utf-8') if body
219
+ html_body = mail.html_part
220
+ html_body = html_body.body.to_s.gsub(/\s+/, ' ').strip.force_encoding('utf-8') if html_body
221
+ mark_message_as_seen(message_id, close_connection: false) unless search
222
+ { subject: subject, body: body, html_body: html_body }
223
+ end
224
+
225
+ # Get S3 key and S3 private key
226
+ # @return [Array <String>] list of keys
227
+ def read_defaults
228
+ return if read_env_defaults
229
+
230
+ yaml = YAML.load_file("#{Dir.home}/.gem-onlyoffice_iredmail_helper/config.yml")
231
+ @default_domain = yaml['domain']
232
+ @default_user = yaml['user']
233
+ @default_password = yaml['password'].to_s
234
+ @default_subject = yaml['subject']
235
+ rescue Errno::ENOENT
236
+ raise Errno::ENOENT, 'No config found. Please create ~/.gem-onlyoffice_iredmail_helper/config.yml'
237
+ end
238
+
239
+ # Read keys from env variables
240
+ def read_env_defaults
241
+ return false unless ENV['IREDMAIL_PASSWORD']
242
+
243
+ @default_domain = ENV['IREDMAIL_DOMAIN']
244
+ @default_user = ENV['IREDMAIL_USER']
245
+ @default_password = ENV['IREDMAIL_PASSWORD'].to_s
246
+ @default_subject = ENV['IREDMAIL_SUBJECT']
247
+ end
248
+ end
249
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnlyofficeIredmailHelper
4
+ # Modules to get mail by different parameters
5
+ module MailGetters
6
+ # Search email by specific date and message title
7
+ # @param date [Date] date to search
8
+ # @param subject [String] check if message is start_with this string
9
+ # @param timeout [Integer] How much time to wait in seconds
10
+ # @param range [Integer] range in days to extend specified date
11
+ # @return [Hash, False] mail data and false is none found
12
+ def email_by_date_and_title(date: Date.today,
13
+ subject: nil,
14
+ timeout: 300,
15
+ move_out: true,
16
+ range: 1)
17
+ start_date = (date - range).strftime('%d-%b-%Y')
18
+ end_date = (date + range).strftime('%d-%b-%Y')
19
+
20
+ login
21
+ @imap.select('INBOX')
22
+ start_time = Time.now
23
+ while (Time.now - start_time) < timeout
24
+ @imap.search(['SINCE', start_date, 'BEFORE', end_date]).each do |message_id|
25
+ mail_data = get_mail_data(message_id)
26
+ next unless mail_data[:subject].start_with?(subject)
27
+
28
+ if move_out
29
+ move_out_message(message_id)
30
+ else
31
+ mark_message_as_seen(message_id)
32
+ end
33
+ return mail_data
34
+ end
35
+ end
36
+ false
37
+ end
38
+
39
+ # Get email text body by subject
40
+ # @param options [Hash] options of get
41
+ # @param times [Integer] count to check
42
+ # @return [String] text body
43
+ def get_text_body_email_by_subject(options = {}, times = 300)
44
+ mail = mail_by_subject(options, times)
45
+ return nil unless mail
46
+
47
+ mail[:body]
48
+ end
49
+
50
+ # Get email html body by subject
51
+ # @param options [Hash] options of get
52
+ # @param times [Integer] count to check
53
+ # @return [String] html body
54
+ def get_html_body_email_by_subject(options = {}, times = 300)
55
+ mail = mail_by_subject(options, times)
56
+ return nil unless mail
57
+
58
+ mail[:html_body]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnlyofficeIredmailHelper
4
+ # Methods for working with Mail account Mailboxes
5
+ module MailboxesMethods
6
+ # @return [Array<String>] list of folder names
7
+ def mailboxes
8
+ login
9
+ @imap.select('INBOX')
10
+ folders = @imap.list('%', '%').map(&:name)
11
+ close
12
+ OnlyofficeLoggerHelper.log("Get list of mailboxes: #{folders}")
13
+ folders
14
+ end
15
+
16
+ # Create new mailbox with name
17
+ # @param name [String] name of folder
18
+ # @return [nil]
19
+ def create_mailbox(name)
20
+ login
21
+ @imap.select('INBOX')
22
+ @imap.create(name)
23
+ close
24
+ OnlyofficeLoggerHelper.log("Created new mailbox: #{name}")
25
+ end
26
+
27
+ # Delete mailbox with name
28
+ # @param name [String] name of folder
29
+ # @return [nil]
30
+ def delete_mailbox(name)
31
+ raise("There is no mailbox #{name} to delete") unless mailboxes.include?(name)
32
+
33
+ login
34
+ @imap.select('INBOX')
35
+ @imap.delete(name)
36
+ close
37
+ OnlyofficeLoggerHelper.log("Delete mailbox by name: #{name}")
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnlyofficeIredmailHelper
4
+ # Methods to work with messages
5
+ module MessageMethods
6
+ # Mark message as seen
7
+ # @param message_id [Integer] message to mark
8
+ # @param close_connection [True, False] should connection be closed
9
+ # @return [nil]
10
+ def mark_message_as_seen(message_id, close_connection: true)
11
+ login
12
+ @imap.select('INBOX')
13
+ @imap.store(message_id, '+FLAGS', [:Seen])
14
+ close if close_connection
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnlyofficeIredmailHelper
4
+ # [String] name of gem
5
+ NAME = 'onlyoffice_iredmail_helper'
6
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OnlyofficeIredmailHelper
4
+ # [String] version of gem
5
+ VERSION = '0.2.0'
6
+ end
metadata ADDED
@@ -0,0 +1,223 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onlyoffice_iredmail_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - ONLYOFFICE
8
+ - Pavel Lobashov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-09-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mail
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: onlyoffice_logger_helper
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1'
42
+ - !ruby/object:Gem::Dependency
43
+ name: codecov
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '='
47
+ - !ruby/object:Gem::Version
48
+ version: 0.2.11
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - '='
54
+ - !ruby/object:Gem::Version
55
+ version: 0.2.11
56
+ - !ruby/object:Gem::Dependency
57
+ name: overcommit
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '='
61
+ - !ruby/object:Gem::Version
62
+ version: 0.55.0
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '='
68
+ - !ruby/object:Gem::Version
69
+ version: 0.55.0
70
+ - !ruby/object:Gem::Dependency
71
+ name: rake
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '='
75
+ - !ruby/object:Gem::Version
76
+ version: 13.0.1
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '='
82
+ - !ruby/object:Gem::Version
83
+ version: 13.0.1
84
+ - !ruby/object:Gem::Dependency
85
+ name: rspec
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '='
89
+ - !ruby/object:Gem::Version
90
+ version: 3.9.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '='
96
+ - !ruby/object:Gem::Version
97
+ version: 3.9.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: rubocop
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '='
103
+ - !ruby/object:Gem::Version
104
+ version: 0.91.0
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '='
110
+ - !ruby/object:Gem::Version
111
+ version: 0.91.0
112
+ - !ruby/object:Gem::Dependency
113
+ name: rubocop-performance
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '='
117
+ - !ruby/object:Gem::Version
118
+ version: 1.8.0
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '='
124
+ - !ruby/object:Gem::Version
125
+ version: 1.8.0
126
+ - !ruby/object:Gem::Dependency
127
+ name: rubocop-rake
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '='
131
+ - !ruby/object:Gem::Version
132
+ version: 0.5.1
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '='
138
+ - !ruby/object:Gem::Version
139
+ version: 0.5.1
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop-rspec
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - '='
145
+ - !ruby/object:Gem::Version
146
+ version: 1.43.2
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - '='
152
+ - !ruby/object:Gem::Version
153
+ version: 1.43.2
154
+ - !ruby/object:Gem::Dependency
155
+ name: simplecov
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - '='
159
+ - !ruby/object:Gem::Version
160
+ version: 0.19.0
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - '='
166
+ - !ruby/object:Gem::Version
167
+ version: 0.19.0
168
+ - !ruby/object:Gem::Dependency
169
+ name: yard
170
+ requirement: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - '='
173
+ - !ruby/object:Gem::Version
174
+ version: 0.9.25
175
+ type: :development
176
+ prerelease: false
177
+ version_requirements: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - '='
180
+ - !ruby/object:Gem::Version
181
+ version: 0.9.25
182
+ description: ONLYOFFICE Helper Gem for iRedMail. Used in QA
183
+ email:
184
+ - shockwavenn@gmail.com
185
+ executables: []
186
+ extensions: []
187
+ extra_rdoc_files: []
188
+ files:
189
+ - lib/onlyoffice_iredmail_helper.rb
190
+ - lib/onlyoffice_iredmail_helper/mail_getters.rb
191
+ - lib/onlyoffice_iredmail_helper/mailboxes_methods.rb
192
+ - lib/onlyoffice_iredmail_helper/message_methods.rb
193
+ - lib/onlyoffice_iredmail_helper/name.rb
194
+ - lib/onlyoffice_iredmail_helper/version.rb
195
+ homepage: https://github.com/ONLYOFFICE-QA/onlyoffice_iredmail_helper
196
+ licenses:
197
+ - AGPL-3.0
198
+ metadata:
199
+ bug_tracker_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_iredmail_helper/issues
200
+ changelog_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_iredmail_helper/blob/master/CHANGELOG.md
201
+ documentation_uri: https://www.rubydoc.info/gems/onlyoffice_iredmail_helper
202
+ homepage_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_iredmail_helper
203
+ source_code_uri: https://github.com/ONLYOFFICE-QA/onlyoffice_iredmail_helper
204
+ post_install_message:
205
+ rdoc_options: []
206
+ require_paths:
207
+ - lib
208
+ required_ruby_version: !ruby/object:Gem::Requirement
209
+ requirements:
210
+ - - ">="
211
+ - !ruby/object:Gem::Version
212
+ version: '2.5'
213
+ required_rubygems_version: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ requirements: []
219
+ rubygems_version: 3.1.2
220
+ signing_key:
221
+ specification_version: 4
222
+ summary: ONLYOFFICE Helper Gem for iRedMail
223
+ test_files: []