前回は、PDFとTEXTを送り込むための処理の統合を行いました。
今回は、使い勝手がよくなるように改造してみます。 [asin:B07ZWCDYZG:detail] evernote.com trello.com
PDFとTEXTの処理の統合作業はできたものの
Trello の Subject: を修正
前回までの作業で一通りの統合作業は完成したわけですが、実際に色々と動作を設定できるようになってくると、Trelloのタイトルに常に”【Kindle】”って挿入されるのは仕様としておかしい感じがしますね。ですのでkindleへ送られた時だけタイトルに追加するよう動作を変更しましょう。
まずは動作を変えないように、send_trello_title_prefix変数を追加します。
# send-trello (一部抜粋) set "send_trello_title_prefix" "【Kindle】 "; deleteheader "Subject"; addheader :last "Subject" "${send_trello_title_prefix}${subject} @${trello_assign}";
次に、Kindleへ送る時だけこの変数を設定するようにします。実際にsend-kindleを読んでいるのは、docsend_runでした。
# docsend_run (一部抜粋) require ["include", "variables"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; global "kindle_convert"; global "evernote_notebook"; global "evernote_notebook_text"; global "evernote_notebook_pdf"; global "trello_assign"; global "trello_assign_text"; global "trello_assign_pdf"; global "send_trello_title_prefix"; set "docsend_default_custom" "kindle_run"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { if allof ( string :matches "${docsend_running}" "pdf", string :matches "${kindle_convert}" "true" ) { set "kindle_convert" "true"; } else { set "kindle_convert" "false"; } set "send_trello_title_prefix" "【Kindle】 "; include "send-kindle"; } # (以下省略)
もう少し使いやすくしてみる
今のバージョンは、シンプルでよいのですがヘッダーのチェック部分を docsend_config で全て記述しなくてはいけないので、少々記述方法に柔軟性が欠けます。docsend_configでは基本的な設定を記述するものとし、実際に受信するメールのヘッダーチェック等は任意の別の場所で出来るように変更してみます。
まず、docsend_init を修正します。方針としては、docsend_preconfig を実行後、docsend_config を読み取り、docsend_postconfig でその後の処理を実施することにします。
# docsend_init require ["include"]; include "docsend_preconfig"; include "docsend_config"; include "docsend_postconfig";
# docsend_preconfig require ["include", "variables"]; global "docsend_check_from_default"; global "docsend_kindle_run_default"; global "docsend_evernote_run_default"; global "docsend_trello_run_default"; # default動作 set "docsend_check_from_default" "false"; set "docsend_kindle_run_default" "true"; set "docsend_evernote_run_default" "true"; set "docsend_trello_run_default" "true";
# docsend_postconfig require ["include", "variables"]; global "docsend_check_from_text_bool"; global "docsend_check_from_pdf_bool"; global "docsend_kindle_run_text_bool"; global "docsend_kindle_run_pdf_bool"; global "docsend_evernote_run_text_bool"; global "docsend_evernote_run_pdf_bool"; global "docsend_trello_run_text_bool"; global "docsend_trello_run_pdf_bool"; global "docsend_check_from_default"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run_default"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; global "docsend_evernote_run_default"; global "docsend_evernote_run_text"; global "docsend_evernote_run_pdf"; global "docsend_trello_run_default"; global "docsend_trello_run_text"; global "docsend_trello_run_pdf"; set "docsend_check_from_text_bool" "${docsend_check_from_default}"; if not string :matches "${docsend_check_from_text}" "" { set "docsend_check_from_text_bool" "${docsend_check_from_text}"; } set "docsend_check_from_pdf_bool" "${docsend_check_from_default}"; if not string :matches "${docsend_check_from_pdf}" "" { set "docsend_check_from_pdf_bool" "${docsend_check_from_pdf}"; } set "docsend_kindle_run_text_bool" "${docsend_kindle_run_default}"; if not string :matches "${docsend_kindle_run_text}" "" { set "docsend_kindle_run_text_bool" "${docsend_kindle_run_text}"; } set "docsend_kindle_run_pdf_bool" "${docsend_kindle_run_default}"; if not string :matches "${docsend_kindle_run_pdf}" "" { set "docsend_kindle_run_pdf_bool" "${docsend_kindle_run_pdf}"; } set "docsend_evernote_run_text_bool" "${docsend_evernote_run_default}"; if not string :matches "${docsend_evernote_run_text}" "" { set "docsend_evernote_run_text_bool" "${docsend_evernote_run_text}"; } set "docsend_evernote_run_pdf_bool" "${docsend_evernote_run_default}"; if not string :matches "${docsend_evernote_run_pdf}" "" { set "docsend_evernote_run_pdf_bool" "${docsend_evernote_run_pdf}"; } set "docsend_trello_run_text_bool" "${docsend_trello_run_default}"; if not string :matches "${docsend_trekki_run_text}" "" { set "docsend_trello_run_text_bool" "${docsend_trello_run_text}"; } set "docsend_trello_run_pdf_bool" "${docsend_trello_run_default}"; if not string :matches "${docsend_trello_run_pdf}" "" { set "docsend_trello_run_pdf_bool" "${docsend_trello_run_pdf}"; }
次に、docsend_run の名称を docsend_send に変更しておきます。
影響のある部分は、呼び出し元の docsend だけです。
# docsend (一部抜粋) if string :matches "${docsend_default_custom_bool}" "true" { include "docsend_send"; include "trash"; } set "docsend_running" "pdf"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { foreverypart { if header :mime :subtype "Content-Type" "pdf" { include "docsend_send"; break; } }
そして、docsendの実行部分を別ファイルに分割しておきます。先ほど名前を変更しておきましたので、ここで docsend_run の名前を使います。
# docsend require ["include"]; include "docsend_init"; include "docsend_run";
# docsend_run require ["include", "variables", "mime", "foreverypart"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; set "docsend_running" "text"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "docsend_send"; include "trash"; } set "docsend_running" "pdf"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { foreverypart { if header :mime :subtype "Content-Type" "pdf" { include "docsend_send"; break; } } include "trash"; }
ここまで来たらあと一息です。docsend_init内の docsend_config と docsend_postconfig部分を docsend側に移動します。
# docsend require ["include"]; include "docsend_init"; include "docsend_postconfig"; include "docsend_run";
# docsend_init require ["include"]; include "docsend_preconfig"; include "docsend_config";
最後に、docsend_postconfig を docsend_run内へ移動しておきます。
# docsend require ["include"]; include "docsend_init"; include "docsend_run";
# docsend_run require ["include", "variables", "mime", "foreverypart"]; global "docsend_default_custom_bool"; global "docsend_default_custom"; global "docsend_running"; include "docsend_postconfig"; set "docsend_running" "text"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { include "docsend_send"; include "trash"; } set "docsend_running" "pdf"; set "docsend_default_custom" "check_from"; include "docsend_default_custom_bool"; if string :matches "${docsend_default_custom_bool}" "true" { foreverypart { if header :mime :subtype "Content-Type" "pdf" { include "docsend_send"; break; } } include "trash"; }
これで、始めに docsend_init しておき、ヘッダーチェックを書いた後、最後に docsend_run を実行すれば、希望の動作ができることになりました。具体例を示すと、メイン部分を次のように書くことができます。
#test7-1 require ["include", "variables"]; global "subject"; global "docsend_check_from_default"; global "kindle_convert"; global "evernote_notebook"; global "trello_assign"; # 元メールのSubject:を保存 if header :matches "Subject" "*" { set "subject" "${1}"; } include "docsend_init"; # 送信元別の設定 if header :contains "From" "example1.jp" { set "docsend_check_from_default" "true"; set "kindle_convert" "true"; set "evernote_notebook" "テスト"; set "trello_assign" "exampleuser"; } include "docsend_run"; discard;
複数のTrelloボードに対応する
このようなタイトルにしてみましたが、今のままでは基本的に docsend_config に書く内容をメイン部分に記述することになりますので、設定が複雑になるにつれメイン部分に変数定義部分が増殖していくことになります。
具体的には以下に示すような、
# docsend_config (一部抜粋) require ["include", "variables"]; # 以下の変数の値を設定(true/false)することができます global "docsend_check_from_default"; global "docsend_check_from_text"; global "docsend_check_from_pdf"; global "docsend_kindle_run_default"; global "docsend_kindle_run_text"; global "docsend_kindle_run_pdf"; global "docsend_evernote_run_default"; global "docsend_evernote_run_text"; global "docsend_evernote_run_pdf"; global "docsend_trello_run_default"; global "docsend_trello_run_text"; global "docsend_trello_run_pdf"; global "kindle_convert"; global "evernote_notebook"; global "evernote_notebook_text"; global "evernote_notebook_pdf"; global "trello_assign"; global "trello_assign_text"; global "trello_assign_pdf"; global "send_kindle_address"; global "send_evernote_address"; global "send_trello_address";
変数定義やら、設定やらを全部メインに書かなくてはいけなくなりそうですね。さすがにそうなると字面がよろしくない、ということで少し工夫して、個別の設定を別ファイルにしてincludeするようにしてみましょう。
まずは、複数のTrelloボードに対応するため、docsend_configに各ボード毎の送信先emailアドレスにそれぞれ名前を付けておきます。
# docsend_config (一部抜粋) global "send_trello_board_example1"; global "send_trello_board_example2"; global "send_trello_board_example3"; # Trelloボード毎の送信先emailアドレス set "send_trello_board_example1" "example_to_trello1@boards.trello.com"; set "send_trello_board_example2" "example_to_trello2@boards.trello.com"; set "send_trello_board_example3" "example_to_trello3@boards.trello.com"; # 各サービスの送信先emailアドレス set "send_trello_address" "${send_trello_board_example1}";
次に、各設定値に名前をつけて別ファイルにしてみます。今回はdocsend_config_example1 として例をお示しします。
# docsend_config_example1 require ["include", "variables"]; global "docsend_check_from_default"; global "kindle_convert"; global "evernote_notebook"; global "trello_assign"; global "send_trello_address"; global "send_trello_board_example1"; set "docsend_check_from_default" "true"; set "kindle_convert" "true"; set "evernote_notebook" "テスト"; set "trello_assign" "exampleuser"; set "send_trello_address" "${send_trello_board_example1}";
メインのコードは次のようになります。
#test7-2 require ["include", "variables"]; global "subject"; # 元メールのSubject:を保存 if header :matches "Subject" "*" { set "subject" "${1}"; } include "docsend_init"; # 送信元別の設定 if header :contains "From" "example1.jp" { include "docsend_config_example1"; } include "docsend_run"; discard;
各docsend_config_* で個別の設定に名前をつけることができますので、個別に複雑な設定が必要になっても分かりやすくなりました。また、メインは処理について集中して記述することができます。
まとめ
今回は、柔軟に記述できるように、処理部分と設定部分の分離を行いました。
「ひとまとまりの処理について名前を付けて別のファイルに分離していく」というのがコードを整理していく基本的な考え方になっているのが、今回の一連のシリーズを通じて少しでも伝われば幸いです。
[asin:B07ZWCDYZG:detail]
evernote.com
trello.com
前回までの記事はこちらです。 tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp tsh.hatenablog.jp