Class: ImapMogura::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/imap_mogura/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/imap_mogura/cli.rb', line 17

def exit_on_failure?
  true
end

Instance Method Details

#check_configObject



160
161
162
163
164
165
166
# File 'lib/imap_mogura/cli.rb', line 160

def check_config
  config_name = options[:config]

  load_and_handle_config(config_name)

  warn "OK"
end

#filter(host) ⇒ Object

Raises:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/imap_mogura/cli.rb', line 87

def filter(host)
  port = options[:port]
  starttls = options[:starttls]
  use_ssl = options[:use_ssl]
  auth_type = options[:auth_type] if use_ssl
  user = options[:user]
  password = Base64.decode64(options[:password_base64])
  config_name = options[:config]
  all_mailbox = options[:all_mailbox]
  exclude_mailboxes = options[:exclude_mailboxes]
  target_mailbox = options[:target_mailbox] unless all_mailbox
  filter_only_unseen = options[:filter_only_unseen]
  create_directory = options[:create_directory]
  dry_run = options[:dry_run]
  debug = options[:debug]

  raise CustomOptionError, "--all-mailbox (-a) or --target-mailbox (-b) is required" if !all_mailbox && target_mailbox.nil?

  DebugUtil.enable_debug if debug

  search_keys = if filter_only_unseen
                  ["UNSEEN"]
                else
                  ["ALL"]
                end

  with_preparation_ready(config_name, host, port, starttls, use_ssl,
                         auth_info: { auth_type: auth_type, user: user, password: password },
                         excluded_mailboxes: exclude_mailboxes,
                         create_directory: create_directory,
                         dry_run: dry_run) do |imap_handler, rules, options|
    if all_mailbox
      excluded_mailboxes = options[:excluded_mailboxes]

      # figure out target mailboxes
      target_mailboxes = imap_handler.all_mailbox_list.reject { |mailbox| excluded_mailboxes.include?(mailbox) }

      target_mailboxes.each do |mailbox|
        filter_mails(imap_handler, rules, mailbox, search_keys, dry_run: dry_run)
      end
    else
      filter_mails(imap_handler, rules, target_mailbox, search_keys, dry_run: dry_run)
    end
  end
end

#list(host) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/imap_mogura/cli.rb', line 140

def list(host)
  port = options[:port]
  starttls = options[:starttls]
  use_ssl = options[:use_ssl]
  auth_type = options[:auth_type] if use_ssl
  user = options[:user]
  password = Base64.decode64(options[:password_base64])

  imap_handler = IMAPHandler.new(host, port,
                                 starttls: starttls, usessl: use_ssl, certs: nil, verify: true,
                                 auth_info: { auth_type: auth_type, user: user, password: password })

  # output all mailbox list
  puts imap_handler.all_mailbox_list

  imap_handler.close
end

#start(host) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/imap_mogura/cli.rb', line 35

def start(host)
  port = options[:port]
  starttls = options[:starttls]
  use_ssl = options[:use_ssl]
  auth_type = options[:auth_type] if use_ssl
  user = options[:user]
  password = Base64.decode64(options[:password_base64])
  config_name = options[:config]
  target_mailbox = options[:target_mailbox]
  filter_unseen = options[:filter_unseen]
  create_directory = options[:create_directory]
  dry_run = options[:dry_run]
  debug = options[:debug]

  DebugUtil.enable_debug if debug

  search_keys = ["RECENT", *(["UNSEEN"] if filter_unseen)]

  with_preparation_ready(config_name, host, port, starttls, use_ssl,
                         auth_info: { auth_type: auth_type, user: user, password: password },
                         create_directory: create_directory,
                         dry_run: dry_run) do |imap_handler, rules|
    warn "* start monitoring recent mails in \"#{target_mailbox}\""

    monitor_recents_on_mailbox(imap_handler, target_mailbox) do
      # find mails with search keys on the target mailbox and handle them
      imap_handler.find_and_handle_mails(target_mailbox, search_keys) do |message_id|
        warn "a mail is recent on \"#{target_mailbox}\""

        filter_mail(imap_handler, rules, target_mailbox, message_id, dry_run: dry_run)

        imap_handler.close_operation_for_mailbox(target_mailbox)
      end
    end
  end
end