mail robot for listening the mail dir
monitor mailbox
check new added email file in new email directory
handle new email file
check to apply account
only if email's subject match 'apply account'
Signed-off-by: Luan Shengde <shdluan(a)163.com>
---
lib/mail-robot.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
create mode 100755 lib/mail-robot.rb
diff --git a/lib/mail-robot.rb b/lib/mail-robot.rb
new file mode 100755
index 0000000..ff1dbd8
--- /dev/null
+++ b/lib/mail-robot.rb
@@ -0,0 +1,45 @@
+#!/usr/bin/env ruby
+# SPDX-License-Identifier: MulanPSL-2.0+
+# Copyright (c) 2020 Huawei Technologies Co., Ltd. All rights reserved.
+# frozen_string_literal: true
+
+require 'json'
+require 'yaml'
+require 'listen'
+require 'mail'
+require 'fileutils'
+
+MAILDIR = '/srv/cci/Maildir/.compass-ci'
+
+def monitor_dir(mail_inbox, mail_drafts)
+ listener = Listen.to(mail_inbox) do |_modified, added, _removed|
+ next if added.empty?
+
+ added.each do |mail_file|
+ begin
+ handle_new_email(mail_file, mail_drafts)
+ rescue StandardError => e
+ puts e.message
+ puts e.backtrace
+ end
+ end
+ end
+ listener.start
+ sleep
+end
+
+def handle_new_email(mail_file, mail_drafts)
+ mail_content = Mail.read(mail_file)
+ check_to_apply_account(mail_content)
+
+ FileUtils.mv(mail_file, mail_drafts)
+end
+
+def check_to_apply_account(mail_content)
+ return unless mail_content.subject == 'apply account'
+
+ assign_uuid = AssignAccount.new(mail_content)
+ assign_uuid.check_to_send_account
+end
+
+monitor_dir("#{MAILDIR}/new", "#{MAILDIR}/cur")
--
2.23.0