Class: Sakura::MailAddress

Inherits:
Object
  • Object
show all
Defined in:
lib/sakura/mail_address.rb

Constant Summary collapse

MAIL_URL =
"#{BASE_URL}users/list/".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address, usage) ⇒ MailAddress

Returns a new instance of MailAddress.



82
83
84
85
# File 'lib/sakura/mail_address.rb', line 82

def initialize(address, usage)
  @address = address
  @usage, @quota = usage.split(%r{\s*/\s*|\s+})
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



9
10
11
# File 'lib/sakura/mail_address.rb', line 9

def address
  @address
end

Returns the value of attribute link.



9
10
11
# File 'lib/sakura/mail_address.rb', line 9

def link
  @link
end

#quotaObject

Returns the value of attribute quota.



9
10
11
# File 'lib/sakura/mail_address.rb', line 9

def quota
  @quota
end

#usageObject (readonly)

Returns the value of attribute usage.



9
10
11
# File 'lib/sakura/mail_address.rb', line 9

def usage
  @usage
end

Class Method Details

.allObject



28
29
30
31
32
33
34
35
36
# File 'lib/sakura/mail_address.rb', line 28

def all
  page = Client.current_session.get(MAIL_URL, /メールアドレス/)
  page.find('.input-text.page-limit-selector').select '300件'
  wait_for_loading page

  page.all('.entities-item').map do |element|
    MailAddress.new_from_element(element)
  end
end

.create(local_part, password) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sakura/mail_address.rb', line 12

def create(local_part, password)
  Client.current_session.process(MAIL_URL, /メールアドレス/) do |page|
    page.first(:xpath, '//a[text() = "新規追加"]').click

    page.find(:xpath, '//label[contains(text(), "ユーザ名")]/..//input')
        .fill_in with: local_part
    page.all(:xpath, '//label[contains(text(), "パスワード")]/..//input').each do |e|
      e.fill_in with: password
    end
    page.find(:xpath, '//label[contains(text(), "メールの受信")]/..//*[contains(text(), "受信する")]/../input').choose
    page.find(:xpath, '//button[text() = "作成する"]').click
  end

  true
end

.find(local_part) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sakura/mail_address.rb', line 38

def find(local_part)
  page = Client.current_session.get(MAIL_URL, /メールアドレス/)
  page.find('.input-text.page-limit-selector').select '300件'
  wait_for_loading page

  element = page.find(
    :xpath,
    # rubocop:disable Layout/LineLength
    "//div[contains(@class, \"entities-item\")]//div[@class=\"username\" and contains(text(), \"#{local_part}\")]/../../.."
    # rubocop:enable Layout/LineLength
  )
  MailAddress.new_from_element(element)
end

.headerObject



59
60
61
62
# File 'lib/sakura/mail_address.rb', line 59

def header
  str = tabularize('address', 'usage', 'quota', '%')
  "#{str}\n#{'-' * (str.size + 1)}"
end

.new_from_element(element) ⇒ Object



52
53
54
55
56
57
# File 'lib/sakura/mail_address.rb', line 52

def new_from_element(element)
  MailAddress.new(
    element.find('.username').text.split('@').first,
    element.find('.col-usage').text
  )
end

.tabularize(*args) ⇒ Object



64
65
66
67
68
69
# File 'lib/sakura/mail_address.rb', line 64

def tabularize(*args)
  args[0].ljust(20) <<
    "#{args[1]} /".rjust(15) <<
    args[2].to_s.rjust(10) <<
    "  (#{args[3].to_s.rjust(3)})"
end

Instance Method Details

#deleteObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/sakura/mail_address.rb', line 87

def delete
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    page.accept_confirm do
      page.find('button.is-dangerous').click
    end
  end

  true
end

#delete_forward_to(mail) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
# File 'lib/sakura/mail_address.rb', line 268

def delete_forward_to(mail)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    @forward_list = page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea').value.split(/[\n,]+/)
    page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea')
        .fill_in with: (@forward_list - [mail]).uniq.join("\n")
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @forward_list.delete mail
end

#detailObject



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/sakura/mail_address.rb', line 284

def detail
  # FIXME: The URL won't work when mail addresses are more than 300
  page = Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)

  <<~END_OF_STRING
    usage / quota: #{usage} / #{quota}  (#{percentage(@usage, @quota)})
    forward_to:    #{forward_list(page).join(' ')}
    keep mail:     #{keep(page)}
    virus scan:    #{virus_scan(page)}
    spam filter:   #{spam_filter(page)}
  END_OF_STRING
end

#disable_keepObject



187
188
189
# File 'lib/sakura/mail_address.rb', line 187

def disable_keep
  false
end

#disable_virus_scanObject



158
159
160
# File 'lib/sakura/mail_address.rb', line 158

def disable_virus_scan
  false
end

#enable_keepObject



183
184
185
# File 'lib/sakura/mail_address.rb', line 183

def enable_keep
  true
end

#enable_virus_scanObject



154
155
156
# File 'lib/sakura/mail_address.rb', line 154

def enable_virus_scan
  true
end

#forward_list(page = nil) ⇒ Object



246
247
248
249
250
251
252
253
254
# File 'lib/sakura/mail_address.rb', line 246

def forward_list(page = nil)
  if @forward_list.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)
    @forward_list = page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea').value.split(/[\n,]+/)
  end

  @forward_list
end

#forward_to(mail) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
# File 'lib/sakura/mail_address.rb', line 256

def forward_to(mail)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    @forward_list = page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea').value.split(/[\n,]+/)
    page.find(:xpath, '//label[contains(text(), "転送先アドレス")]/..//textarea')
        .fill_in with: (@forward_list + [mail]).uniq.join("\n")
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @forward_list << mail
end

#keep(page = nil) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/sakura/mail_address.rb', line 162

def keep(page = nil)
  if @keep.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)
    @keep = page.find('[name="mailReceiveType"]:checked').value == '1'
  end

  @keep
end

#keep=(value) ⇒ Object



172
173
174
175
176
177
178
179
180
181
# File 'lib/sakura/mail_address.rb', line 172

def keep=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    text = value ? '受信する' : '転送専用'
    page.find(:xpath, "//label[contains(text(), \"メールの受信\")]/..//*[contains(text(), \"#{text}\")]/../input").choose
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @keep = value
end

#password=(value) ⇒ Object



124
125
126
127
128
129
130
131
132
# File 'lib/sakura/mail_address.rb', line 124

def password=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/password/#{@address}", /#{@address}のパスワード設定/) do |page|
    page.all(:xpath, '//label[contains(text(), "パスワード")]/..//input').each do |e|
      e.fill_in with: value
    end
    page.find(:xpath, '//button[text() = "変更する"]').click
  end
end

#spam_filter(page = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/sakura/mail_address.rb', line 191

def spam_filter(page = nil)
  if @spam_filter.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)

    case page.find("[name='spamFilterType']:checked").value
    when '1'
      @spam_filter = :disable
    when '2'
      case page.find("[name='spamFilterAction']").value
      when '1'
        @spam_filter = :quarantine
      when '2'
        @spam_filter = :discard
      when '3'
        @spam_filter = :mark
      end
    when '3'
      @spam_filter = :precise
    end
  end

  @spam_filter
end

#spam_filter=(value) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/sakura/mail_address.rb', line 216

def spam_filter=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    text = nil
    action = nil
    case value.to_sym
    when :disable
      text = '利用しない'
    when :quarantine
      text = '簡易' # "迷惑メールフィルタ" doesn't work
      action = '「迷惑メール」フォルダに保存 [推奨]'
    when :discard
      text = '簡易' # "迷惑メールフィルタ" doesn't work
      action = 'メールを破棄'
    when :mark
      text = '簡易' # "迷惑メールフィルタ" doesn't work
      action = 'フィルターのみ利用'
    when :precise
      '高精度迷惑メールフィルタ'
    end

    page.find(:xpath,
              "//label[contains(text(), \"迷惑メールフィルタ\")]/..//*[contains(text(), \"#{text}\")]/../input").choose
    page.find("[name='spamFilterAction']").select action if action
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @spam_filter = value
end

#to_sObject



280
281
282
# File 'lib/sakura/mail_address.rb', line 280

def to_s
  self.class.tabularize(@address, @usage, @quota, percentage(@usage, @quota))
end

#virus_scan(page = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
# File 'lib/sakura/mail_address.rb', line 134

def virus_scan(page = nil)
  if @virus_scan.nil?
    # FIXME: The URL won't work when mail addresses are more than 300
    page ||= Client.current_session.get(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/)
    @virus_scan = page.find('[name="usesMailVirusCheck"]:checked').value == '1'
  end

  @virus_scan
end

#virus_scan=(value) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/sakura/mail_address.rb', line 144

def virus_scan=(value)
  # FIXME: The URL won't work when mail addresses are more than 300
  Client.current_session.process(MAIL_URL + "1/edit/#{@address}", /#{@address}の設定/) do |page|
    page.find("[name='usesMailVirusCheck'][value='#{value ? 1 : 0}']").choose
    page.find(:xpath, '//button[text() = "保存する"]').click
  end

  @virus_scan = value
end