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!, execute!, #force_change_settings_function, help, #hook_call, #load_local_settings, #tagname_to_ids

Constructor Details

#initializeMail



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 "\n  \u30FB\u4E3B\u306BSend to Kindle\u3092\u4F7F\u3046\u305F\u3081\u306E\u30B3\u30DE\u30F3\u30C9\u3067\u3059\u3002\n  \u30FB<target>\u3067\u6307\u5B9A\u3057\u305F\u5C0F\u8AAC\u306E\u96FB\u5B50\u66F8\u7C4D\u30C7\u30FC\u30BF\u30E1\u30FC\u30EB\u3067\u9001\u4FE1\u3057\u307E\u3059\u3002\n  \u30FB<target>\u3092\u7701\u7565\u3057\u305F\u5834\u5408\u3001\u65B0\u7740\u304C\u3042\u3063\u305F\u5C0F\u8AAC\u3092\u5168\u3066\u9001\u4FE1\u3057\u307E\u3059\u3002\n  \u30FB\u30E1\u30FC\u30EB\u306E\u9001\u4FE1\u8A2D\u5B9A\u306F\u3001\#{Mailer::SETTING_FILE}\u30D5\u30A1\u30A4\u30EB\u3092\u7DE8\u96C6\u3057\u307E\u3059\u3002\n(\u521D\u3081\u3066\u30B3\u30DE\u30F3\u30C9\u3092\u4F7F\u3046\u3068\u304D\u306B\u81EA\u52D5\u3067\u4F5C\u6210\u3055\u308C\u307E\u3059)\n  \u30FB<target>\u306Bhotentry\u3092\u6307\u5B9A\u3057\u305F\u5834\u5408\u3001\u6700\u65B0\u306Ehotnetry\u3092\u9001\u4FE1\u3057\u307E\u3059\n\n  Examples:\nnarou mail 6         # \u65B0\u7740\u95A2\u4FC2\u306A\u304F\u30E1\u30FC\u30EB(\u9001\u4FE1\u6E08\u307F\u30D5\u30E9\u30B0\u306F\u7ACB\u3064)\n\nnarou update\nnarou mail           # update\u3067\u65B0\u7740\u304C\u3042\u3063\u305F\u5C0F\u8AAC\u3092\u5168\u3066\u30E1\u30FC\u30EB\n\nnarou mail --force   # \u51CD\u7D50\u6E08\u4EE5\u5916\u306E\u5168\u3066\u306E\u5C0F\u8AAC\u3092\u5F37\u5236\u7684\u306B\u30E1\u30FC\u30EB(\u4F7F\u3044\u65B9\u306B\u6CE8\u610F)\n\n  Options:\n  EOS\n\n  @opt.on(\"-f\", \"--force\", \"\u5168\u3066\u306E\u5C0F\u8AAC\u3092\u5F37\u5236\u7684\u306B\u9001\u4FE1\") {\n    @options[\"force\"] = true\n  }\nend\n"

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



136
137
138
139
140
141
142
# File 'lib/command/mail.rb', line 136

def alter_database_add_column_last_mail_date
  database = Database.instance
  database.each_value 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
118
119
120
121
# 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_paths = [Update.get_newest_hotentry_file_path(device)]
      display_target = "hotentry"
    else
      ebook_paths = Narou.get_ebook_file_paths(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_paths[0]
      error "#{target} は存在しません" unless send_all
      next
    end
    unless File.exist?(ebook_paths[0])
      error "まだファイル(#{File.basename(ebook_paths[0])})が無いようです" unless send_all
      next
    end
    if target == "hotentry"
      id = "hotentry"
    else
      id = data["id"]
      title = data["title"]
      display_target = "ID:#{id} #{TermColorLight.escape(title)}"
    end
    puts "<bold><green>#{display_target}</green></bold>".termcolor
    print "メールを送信しています"
    ebook_paths.each do |ebook_path|
      exit_mail = false
      mail_result = nil
      Thread.new do
        mail_result = mailer.send(id, 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
  end
rescue Interrupt
  puts "メール送信を中断しました"
  exit Narou::EXIT_INTERRUPT
ensure
  database.save_database if database
end

#install_mailer_settingObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/command/mail.rb', line 123

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