Class: Command::Mail

Inherits:
CommandBase show all
Defined in:
lib/command/mail.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CommandBase

execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeMail

Returns a new instance of Mail.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/command/mail.rb', line 15

def initialize
  super("[<target1> ...] [options]")
  @opt.separator <<-EOS

  ・主にSend to Kindleを使うためのコマンドです。
  ・<target>で指定した小説の電子書籍データメールで送信します。
  ・<target>を省略した場合、新着があった小説を全て送信します。
  ・メールの送信設定は、#{Mailer::SETTING_FILE}ファイルを編集します。
(初めてコマンドを使うときに自動で作成されます)
  ・<target>にhotentryを指定した場合、最新のhotnetryを送信します

  Examples:
narou mail 6         # 新着関係なくメール(送信済みフラグは立つ)

narou update
narou mail           # updateで新着があった小説を全てメール

narou mail --force   # 凍結済以外の全ての小説を強制的にメール(使い方に注意)

  Options:
  EOS

  @opt.on("-f", "--force", "全ての小説を強制的に送信") {
    @options["force"] = true
  }
end

Class Method Details

.oneline_helpObject



11
12
13
# File 'lib/command/mail.rb', line 11

def self.oneline_help
  "変換したEPUB/MOBIをメールで送信します"
end

Instance Method Details

#alter_database_add_column_last_mail_dateObject



132
133
134
135
136
137
138
# File 'lib/command/mail.rb', line 132

def alter_database_add_column_last_mail_date
  database = Database.instance
  database.each do |_, data|
    data["last_mail_date"] ||= Time.now
  end
  database.save_database
end

#execute(argv) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
# File 'lib/command/mail.rb', line 42

def execute(argv)
  super
  send_all = false
  device = Narou.get_device
  database = Database.instance
  begin
    mailer = Mailer.create
  rescue Mailer::SettingNotFound
    install_mailer_setting
    return
  rescue Mailer::SettingUncompleteError => e
    error e.message
    exit Narou::EXIT_ERROR_CODE
  end
  if argv.empty?
    send_all = true
    database.each_key do |id|
      next if Narou.novel_frozen?(id)
      argv << id
    end
  end
  tagname_to_ids(argv)
  argv.each do |target|
    if target == "hotentry"
      ebook_path = Update.get_newest_hotentry_file_path(device)
      display_target = "hotentry"
    else
      ebook_path = Narou.get_ebook_file_path(target, device ? device.ebook_file_ext : ".epub")
      data = Downloader.get_data_by_target(target)
      if send_all && !@options["force"]
        new_arrivals_date = data["new_arrivals_date"] || Time.now
        if data["last_mail_date"] && new_arrivals_date < data["last_mail_date"]
          next   # すでに送信済みなので送信しない
        end
      end
    end
    unless ebook_path
      error "#{target} は存在しません" unless send_all
      next
    end
    unless File.exist?(ebook_path)
      error "まだファイル(#{File.basename(ebook_path)})が無いようです" unless send_all
      next
    end
    if target != "hotentry"
      id = data["id"]
      title = data["title"]
      display_target = "ID:#{id} #{TermColorLight.escape(title)}"
    end
    puts "<bold><green>#{display_target}</green></bold>".termcolor
    print "メールを送信しています"
    exit_mail = false
    mail_result = nil
    Thread.new do
      mail_result = mailer.send(File.basename(ebook_path), ebook_path)
      exit_mail = true
    end
    until exit_mail
      print "."
      sleep(0.5)
    end
    puts
    if mail_result
      puts File.basename(ebook_path) + " をメールで送信しました"
      database[id]["last_mail_date"] = Time.now if target != "hotentry"
    else
      error "#{mailer.error_message}"
      exit Narou::EXIT_ERROR_CODE   # next しても次も失敗する可能性が高いのでここで終了
    end
  end
rescue Interrupt
  puts "メール送信を中断しました"
  exit Narou::EXIT_ERROR_CODE
ensure
  database.save_database if database
end

#install_mailer_settingObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/command/mail.rb', line 119

def install_mailer_setting
  setting_file_path = File.join(Narou.get_preset_dir, Mailer::SETTING_FILE)
  install_path = File.join(Narou.get_root_dir, Mailer::SETTING_FILE)
  FileUtils.cp(setting_file_path, install_path)
  alter_database_add_column_last_mail_date
  puts "created #{install_path}"
  puts "メールの設定用ファイルを作成しました。設定ファイルを書き換えることで mail コマンドが有効になります。"
  puts "注意:次回以降のupdateで新着があった場合に送信可能フラグが立ちます"
  unless Narou.web?
    Helper.open_directory(Narou.get_root_dir, "設定ファイルがあるフォルダを開きますか")
  end
end