「OfflineIMAP」の版間の差分
(同期) |
細 (誤字の訂正) |
||
| (4人の利用者による、間の6版が非表示) | |||
| 1行目: | 1行目: | ||
| − | [[Category:メール |
+ | [[Category:メール取得エージェント]] |
| + | [[Category:OpenPGP]] |
||
[[en:OfflineIMAP]] |
[[en:OfflineIMAP]] |
||
{{Related articles start}} |
{{Related articles start}} |
||
| 7行目: | 8行目: | ||
{{Related articles end}} |
{{Related articles end}} |
||
| − | [ |
+ | [https://github.com/OfflineIMAP/offlineimap3 OfflineIMAP] は IMAP サーバーからメールを同期する Python ユーティリティです。POP3 プロトコルや mbox は使えないため、通常 [[Mutt]] などの MUA と組み合わされて使われます。 |
== インストール == |
== インストール == |
||
| − | {{pkg|offlineimap |
+ | {{pkg|offlineimap}} をインストールしてください。 |
== 設定 == |
== 設定 == |
||
| − | Offlineimap には2つのデフォルト設定ファイルが付属しており、どちらも {{ic|/usr/share/offlineimap/}} にあります。{{ic|offlineimap.conf}} には全ての設定が含まれておりコメントが付与されています。 |
+ | Offlineimap には2つのデフォルト設定ファイルが付属しており、どちらも {{ic|/usr/share/offlineimap/}} にあります。{{ic|offlineimap.conf}} には全ての設定が含まれておりコメントが付与されています。一方、{{ic|offlineimap.conf.minimal}} にはコメントが付いておらず多少の設定しか含まれていません (参照: [[#最小]])。 |
| − | デフォルト設定ファイルのどちらかを {{ic|~/.offlineimaprc}} にコピーしてください。 |
+ | デフォルト設定ファイルのどちらかを {{ic|~/.offlineimaprc}} または {{ic|$XDG_CONFIG_HOME/offlineimap/config}} にコピーしてください。 |
| + | {{note|同じ行内に、オプションや値の後ろにコメントを書くのは無効な構文なので、コメントは別の独立した行に書いて下さい。}} |
||
| − | {{note|Writing a comment after an option/value on the same line is invalid syntax, hence take care that comments are placed on their own separate line.}} |
||
| − | === |
+ | === 最小 === |
{{ic|offlineimap.conf.minimal}} のコメントが付いたバージョンは以下になります: |
{{ic|offlineimap.conf.minimal}} のコメントが付いたバージョンは以下になります: |
||
| 27行目: | 28行目: | ||
{{hc|~/.offlineimaprc|<nowiki> |
{{hc|~/.offlineimaprc|<nowiki> |
||
[general] |
[general] |
||
| + | # 同期するアカウントのリスト。コンマで区切る。 |
||
| − | # List of accounts to be synced, separated by a comma. |
||
accounts = main |
accounts = main |
||
[Account main] |
[Account main] |
||
| + | # ローカルリポジトリの識別子。例: IMAP経由で同期されるmaildir。 |
||
| − | # Identifier for the local repository; e.g. the maildir to be synced via IMAP. |
||
localrepository = main-local |
localrepository = main-local |
||
| + | # リモートリポジトリの識別子。すなわち実際のIMAPで、通常はローカルではありません。 |
||
| − | # Identifier for the remote repository; i.e. the actual IMAP, usually non-local. |
||
remoterepository = main-remote |
remoterepository = main-remote |
||
| − | # Status cache. Default is plain, which eventually becomes huge and slow. |
||
| − | status_backend = sqlite |
||
[Repository main-local] |
[Repository main-local] |
||
| + | # OfflineImapは、Maildir、GmailMaildir、およびIMAPをローカルリポジトリとしてサポートしています。 |
||
| − | # Currently, offlineimap only supports maildir and IMAP for local repositories. |
||
type = Maildir |
type = Maildir |
||
| + | # メールを配置する場所 |
||
| − | # Where should the mail be placed? |
||
| − | localfolders = ~/ |
+ | localfolders = ~/mail |
[Repository main-remote] |
[Repository main-remote] |
||
| + | # リモートレポジトリはIMAPまたはGmailであり、後者は事前に設定されたIMAPです。 |
||
| − | # Remote repos can be IMAP or Gmail, the latter being a preconfigured IMAP. |
||
type = IMAP |
type = IMAP |
||
remotehost = host.domain.tld |
remotehost = host.domain.tld |
||
remoteuser = username |
remoteuser = username |
||
| + | # OfflineImapのバージョンが6.5.4より高い場合、SSL接続に必要です。 |
||
| + | sslcacertfile = /etc/ssl/certs/ca-certificates.crt |
||
</nowiki>}} |
</nowiki>}} |
||
| 57行目: | 58行目: | ||
{{hc|~/.offlineimaprc|2= |
{{hc|~/.offlineimaprc|2= |
||
[Repository main-remote] |
[Repository main-remote] |
||
| + | # InboxとSentフォルダーのみ同期します: |
||
| − | # Synchronize only the folders Inbox and Sent: |
||
folderfilter = lambda foldername: foldername in ["Inbox", "Sent"] |
folderfilter = lambda foldername: foldername in ["Inbox", "Sent"] |
||
... |
... |
||
| 63行目: | 64行目: | ||
他のオプションについては、[http://offlineimap.org/doc/nametrans.html#folderfilter 公式ドキュメント] を見て下さい。 |
他のオプションについては、[http://offlineimap.org/doc/nametrans.html#folderfilter 公式ドキュメント] を見て下さい。 |
||
| + | |||
| + | === カスタムポート === |
||
| + | |||
| + | IMAPサーバーの中には、デフォルトの993ポートではなく、カスタムポートの接続を要求してくるものがあります。そのようにするには、{{ic|~/.offlineimaprc}}の '''remote''' セクションに ''remoteport'' オプションを追加します: |
||
| + | |||
| + | {{hc|~/.offlineimaprc|2= |
||
| + | [Repository main-remote] |
||
| + | remoteport=''1234'' |
||
| + | }} |
||
== 使用方法 == |
== 使用方法 == |
||
| − | offlineimap を実行する前に、ローカルリポジトリ |
+ | offlineimap を実行する前に、ローカルリポジトリに割り当てる親ディレクトリを作成してください: |
$ mkdir ~/Maildir |
$ mkdir ~/Maildir |
||
そして、プログラムを実行してください: |
そして、プログラムを実行してください: |
||
| + | |||
$ offlineimap |
$ offlineimap |
||
メールアカウントが同期されます。何か問題が発生した場合、エラーメッセージをよく見て下さい。OfflineIMAP は基本的に問題について詳しい出力を表示します。最終的な製品からのトレースバックを取得するのに開発者が悩む必要がないようにするためです。 |
メールアカウントが同期されます。何か問題が発生した場合、エラーメッセージをよく見て下さい。OfflineIMAP は基本的に問題について詳しい出力を表示します。最終的な製品からのトレースバックを取得するのに開発者が悩む必要がないようにするためです。 |
||
| + | == ヒントとテクニック == |
||
| − | == Tips and tricks == |
||
=== バックグラウンドで offlineimap を実行 === |
=== バックグラウンドで offlineimap を実行 === |
||
| + | |||
| + | {{Style|コメント付きの設定ファイルをドキュメントとして使うのではなく、テキストに記述する方が良いのでは?}} |
||
大抵のメール転送エージェントはデフォルトで、プログラムを定期的に同期させることで[[デーモン]]としてツールを使うのが前提になっています。offlineimap では、バックグラウンドで作業を制御する設定はあまりありません。 |
大抵のメール転送エージェントはデフォルトで、プログラムを定期的に同期させることで[[デーモン]]としてツールを使うのが前提になっています。offlineimap では、バックグラウンドで作業を制御する設定はあまりありません。 |
||
| 82行目: | 95行目: | ||
紛らわしいことに、バックグラウンドの設定は設定ファイルのあらゆるところに散らばっています: |
紛らわしいことに、バックグラウンドの設定は設定ファイルのあらゆるところに散らばっています: |
||
| − | {{hc|~/.offlineimaprc| |
+ | {{hc|~/.offlineimaprc|2= |
| − | # |
+ | # generalセクション |
[general] |
[general] |
||
| + | # 同時に同期するアカウントの数を制御します |
||
| − | # Controls how many accounts may be synced simultaneously |
||
maxsyncaccounts = 1 |
maxsyncaccounts = 1 |
||
| + | # アカウント識別子 |
||
| − | # In the account identifier |
||
[Account main] |
[Account main] |
||
| + | # 同期の間隔(分) |
||
| − | # Minutes between syncs |
||
autorefresh = 0.5 |
autorefresh = 0.5 |
||
| + | # IMAPフラグにのみ変更があった場合、quick-syncsは更新されません。 |
||
| − | # Quick-syncs do not update if the only changes were to IMAP flags. |
||
| − | # autorefresh=0.5 |
+ | # autorefresh=0.5 と quick=10 を組み合わせると、 |
| + | # タイプに関係なく、各完全更新の間に0.5分の間隔で |
||
| − | # 10 quick refreshes between each full refresh, with 0.5 minutes between every |
||
| + | # 10回のクイック更新が行われます。 |
||
| − | # refresh, regardless of type. |
||
quick = 10 |
quick = 10 |
||
| + | # リモートリポジトリ識別子 |
||
| − | # In the remote repository identifier |
||
[Repository main-remote] |
[Repository main-remote] |
||
| + | # 同期が完了すると接続を閉じる代わりに、offlineimapはサーバーに空のデータを送信して接続を維持します。 |
||
| − | # Instead of closing the connection once a sync is complete, offlineimap will |
||
| + | # この値を60に設定すると、同期(クイックと自動更新の両方)の間に1分間接続を維持しようとします。 |
||
| − | # send empty data to the server to hold the connection open. A value of 60 |
||
| + | # この設定は、autorefresh と holdconnectionopen の両方が設定されていない場合は無効です。 |
||
| − | # attempts to hold the connection for a minute between syncs (both quick and |
||
| − | # autorefresh).This setting has no effect if autorefresh and holdconnectionopen |
||
| − | # are not both set. |
||
keepalive = 60 |
keepalive = 60 |
||
| + | # OfflineIMAPは通常、autorefreshグローバルオプションが指定されている場合、更新の合間にIMAPサーバ接続を閉じます。 |
||
| − | # OfflineIMAP normally closes IMAP server connections between refreshes if |
||
| + | # 接続を維持したい場合は、この設定をtrueに設定してください。 |
||
| − | # the global option autorefresh is specified. If you wish it to keep the |
||
| + | # autorefreshが設定されていない場合、この設定は効果がありません。 |
||
| − | # connection open, set this to true. This setting has no effect if autorefresh |
||
| − | # is not set. |
||
holdconnectionopen = yes |
holdconnectionopen = yes |
||
| + | }} |
||
| − | </nowiki>}} |
||
| + | ログイン時にデーモンを自動的に起動するには、[[systemd/ユーザー]]サービスである{{ic|offlineimap.service}}を{{ic|--user}}フラグ付きで[[起動/有効化]]してください。 |
||
| − | ==== systemd サービス ==== |
||
| + | 複数のアカウントを設定している場合は、maxsyncaccountsパラメータ[https://www.offlineimap.org/configuration/2016/01/29/why-i-m-not-using-maxconnctions.html]を増やすのではなく、{{ic|offlineimap@.service}}を使用することをお勧めします。{{ic|offlineimap@youraccountname.service}}を[[起動/有効化]]するだけです。 |
||
| − | バックグラウンドジョブを実行するように設定したら、以下の systemd サービスで offlineimap を管理することができます: |
||
| + | ==== systemd timer ==== |
||
| − | {{hc|/etc/systemd/system/offlineimap@.service|<nowiki> |
||
| − | [Unit] |
||
| − | Description=Start offlineimap as a daemon |
||
| − | Requires=network-online.target |
||
| − | After=network.target |
||
| + | あるいは、systemd-user [[systemd/タイマー|タイマー]]を使って、{{ic|offlineimap-oneshot.timer}}を{{ic|--user}}フラグで起動/有効化することで、OfflineIMAPを完全に管理することもできます。 |
||
| − | [Service] |
||
| − | User=%i |
||
| − | ExecStart=/usr/bin/offlineimap |
||
| − | KillSignal=SIGUSR2 |
||
| − | Restart=always |
||
| + | このタイマーはデフォルトで15分ごとにOfflineIMAPを実行します。これは、[[ドロップインファイル]]を作成することで変更することができます。例えば、以下では5分ごとにチェックするようにタイマーを修正しています: |
||
| − | [Install] |
||
| − | WantedBy=multi-user.target |
||
| − | </nowiki>}} |
||
| + | {{hc|~/.config/systemd/user/offlineimap-oneshot.timer.d/timer.conf|2= |
||
| − | サービスを作成したら {{ic|offlineimap@''user''.service}} を[[有効化]]してください。 |
||
| + | [Timer] |
||
| + | OnUnitInactiveSec=5m |
||
| + | }} |
||
| + | {{Accuracy|これは、OfflineIMAPのローカルデータベースに矛盾をもたらす可能性があります。}} |
||
| − | {{Note|[[D-Bus]] の操作を含む設定をした場合 (例: [[#Gnome Keyring]])、上記の systemd サービスは直接使うのではなく [[systemd/ユーザー]] に書かれているように systemd のユーザーインスタンスとして使う必要があります。{{ic|DBUS_SESSION_BUS_ADDRESS}} 変数を適切に設定してください。}} |
||
| + | |||
| + | より堅牢な解決法としては、フリーズした場合にOfflineIMAPをkillするウォッチドッグを設定することができます。 |
||
| + | |||
| + | {{hc|~/.config/systemd/user/offlineimap-oneshot.service.d/service.conf|2= |
||
| + | [Service] |
||
| + | WatchdogSec=300 |
||
| + | }} |
||
=== mutt 用に自動的にメールボックスを作成 === |
=== mutt 用に自動的にメールボックスを作成 === |
||
| + | [[Mutt]]は 単にIMAP や Maildir のディレクトリを指し示したり、どのサブディレクトリがメールボックスであるかを推測することはできませんが、 offlineimap は同期するメールボックスを含む muttrc フラグメントを生成することができます。 |
||
| − | [[Mutt]] cannot be simply pointed to an IMAP or maildir directory and be expected to guess which subdirectories happen to be the mailboxes, yet offlineimap can generate a muttrc fragment containing the mailboxes that it syncs. |
||
{{hc|~/.offlineimaprc|<nowiki> |
{{hc|~/.offlineimaprc|<nowiki> |
||
| 151行目: | 161行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| − | + | そして以下の行を{{ic|~/.mutt/muttrc}}に追加します。 |
|
{{hc|~/.mutt/muttrc|<nowiki> |
{{hc|~/.mutt/muttrc|<nowiki> |
||
| 162行目: | 172行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| − | {{ic|account}} |
+ | {{ic|account}}は{{ic|~/.offlineimaprc}}でIMAPアカウントにつけた名前です。 |
=== Gmail の設定 === |
=== Gmail の設定 === |
||
| + | このリモートリポジトリは Gmail をサポートするために特別に設定されており、フォルダ名を大文字から小文字に置き換えたり、その他の細かい追加を行います。この設定では「すべてのメール」フォルダーを同期しないことに注意して下さい。「すべてのメール」は通常不要であり、この同期をスキップすることにより帯域幅コストを抑えるためです: |
||
| − | This remote repository is configured specifically for Gmail support, substituting folder names in uppercase for lowercase, among other small additions. Keep in mind that this configuration does not sync the ''All Mail'' folder, since it is usually unnecessary and skipping it prevents bandwidth costs: |
||
{{hc|~/.offlineimaprc|<nowiki> |
{{hc|~/.offlineimaprc|<nowiki> |
||
| 178行目: | 188行目: | ||
re.sub (' ', '_', foldername.lower())))) |
re.sub (' ', '_', foldername.lower())))) |
||
folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail'] |
folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail'] |
||
| + | # OfflineImap 6.5.4 で必要 |
||
| − | # Necessary as of OfflineIMAP 6.5.4 |
||
sslcacertfile = /etc/ssl/certs/ca-certificates.crt |
sslcacertfile = /etc/ssl/certs/ca-certificates.crt |
||
| + | # https://github.com/OfflineIMAP/offlineimap/issues/573(バージョン7.0.12、7.2.1)の対処に必要 |
||
| + | ssl_version = tls1_2 |
||
</nowiki>}} |
</nowiki>}} |
||
{{Note| |
{{Note| |
||
| + | * Gmailが別の言語に設定されている場合、フォルダ名も翻訳されているように見える場合があります。 例: 「sent_mail」の代わりに「送信済みメール」 |
||
| − | * If you have Gmail set to another language, the folder names may appear translated too, e.g. "verzonden_berichten" instead of "sent_mail". |
||
| + | * バージョン6.3.5以降では、offlineimapはローカルのフォルダに合わせてリモートのフォルダも作成します。そのため、この名前の翻訳ルールの効果を逆転させる名前の翻訳ルールがローカルリポジトリにも必要になるかもしれません。名前の逆翻訳ルールを作りたくない場合は、リモートの設定に次のように記述することで、リモートフォルダの作成を無効にできます: {{ic|<nowiki>createfolders = False</nowiki>}} |
||
| − | * After version 6.3.5, offlineimap also creates remote folders to match your local ones. Thus you may need a nametrans rule for your local repository too that reverses the effects of this nametrans rule. If you don't want to make a reverse nametrans rule, you can disable remote folder creation by putting this in your remote configuration: {{ic|<nowiki>createfolders = False</nowiki>}} |
||
* 2012年10月1日現在、gmail の SSL 証明書のフィンガープリントはいつも同じだとは限りません。このため {{ic|cert_fingerprint}} を使うよりも {{ic|sslcacertfile}} を使って SSL の確認をするほうが良いでしょう ([[#SSL フィンガープリントが一致しない]]を参照)。 |
* 2012年10月1日現在、gmail の SSL 証明書のフィンガープリントはいつも同じだとは限りません。このため {{ic|cert_fingerprint}} を使うよりも {{ic|sslcacertfile}} を使って SSL の確認をするほうが良いでしょう ([[#SSL フィンガープリントが一致しない]]を参照)。 |
||
| + | }} |
||
| + | |||
| + | ==== oama経由のOAuth2アクセストークン ==== |
||
| + | |||
| + | [https://github.com/pdobsan/oama oama] ({{AUR|oama-bin}})は、IMAP/SMTPクライアントにOAuth2認証の更新機能と承認を提供するユーティリティです。 |
||
| + | |||
| + | OfflineIMAPは、設定からPythonコードを呼び出すことができます。したがって、まず{{ic|[general]}}セクションで、以下の行を追加します。 |
||
| + | |||
| + | {{hc|~/.offlineimaprc|2= |
||
| + | [general] |
||
| + | pythonfile = ~/.offlineimap.py |
||
| + | }} |
||
| + | |||
| + | Pythonファイルに以下のコードを追加して、''oama''を経由してOAuth2アクセストークンを取得します: |
||
| + | |||
| + | {{hc|~/.offlineimap.py|2= |
||
| + | import subprocess |
||
| + | |||
| + | def get_token(email_address): |
||
| + | return subprocess.run(["oama", "access", email_address], capture_output=True, text=True).stdout |
||
| + | }} |
||
| + | |||
| + | そして設定ファイルに戻り、Gmailアカウントのrepositoryセクションに以下を追加してください: |
||
| + | |||
| + | {{hc|~/.offlineimaprc|2= |
||
| + | auth_mechanisms = XOAUTH2 |
||
| + | oauth2_client_id = ''YOUR_OAUTH2_CLIENT_ID'' |
||
| + | oauth2_client_secret = ''YOUR_OAUTH2_CLIENT_SECRET'' |
||
| + | oauth2_request_url = <nowiki>https://accounts.google.com/o/oauth2/token</nowiki> |
||
| + | oauth2_access_token_eval = get_token("''YOUR_EMAIL_ADDRESS_FOR_THIS_ACCOUNT''") |
||
}} |
}} |
||
| 198行目: | 240行目: | ||
password [your password] |
password [your password] |
||
| + | |||
| − | Do not forget to give the file appropriate rights like 600 or 700: |
||
| + | ファイルに600や700のような適切な権限を与えることを忘れないでください。 |
||
| + | |||
$ chmod 600 ~/.netrc |
$ chmod 600 ~/.netrc |
||
| + | |||
| + | {{Note|OfflineIMAPは、{{ic|.netrc}}ファイルに複数のアカウントが保存されている場合、パスワードを取得する方法を[https://github.com/OfflineIMAP/offlineimap/issues/295 知りません]。}} |
||
==== GPG を使う ==== |
==== GPG を使う ==== |
||
| 205行目: | 251行目: | ||
GNU Privacy Guard を使うことで暗号化ファイルにパスワードを保存することができます。このセクションの設定を行う前にまず [[GnuPG]] をセットアップしてください。[[GnuPG#gpg-agent|パスワードを入力しなくても]] GPG の秘密鍵をいつでも使えるようにしておく必要があります。 |
GNU Privacy Guard を使うことで暗号化ファイルにパスワードを保存することができます。このセクションの設定を行う前にまず [[GnuPG]] をセットアップしてください。[[GnuPG#gpg-agent|パスワードを入力しなくても]] GPG の秘密鍵をいつでも使えるようにしておく必要があります。 |
||
| − | まずプレーンテキストファイルにメールアカウントのパスワードを入力してください。暗号化されていないパスワードがディスクに書き込まれないように、ファイルの作成は [[tmpfs]] 上の {{ic|700}} パーミッションの安全なディレクトリで行 |
+ | まずプレーンテキストファイルにメールアカウントのパスワードを入力してください。暗号化されていないパスワードがディスクに書き込まれないように、ファイルの作成は [[tmpfs]] 上の {{ic|700}} パーミッションの安全なディレクトリで行って下さい。それから、GnuPGで自身を受信者にしてファイルを[[GnuPG#暗号化と復号化|暗号化]]します: |
| − | |||
| − | $ gpg --default-recipient-self -e ''/path/to/plain/password'' |
||
必要なくなったプレーンテキストファイルは削除してください。暗号化したファイルは最終的に必要な場所 (例: {{ic|~/.offlineimappass.gpg}}) に移動してください。 |
必要なくなったプレーンテキストファイルは削除してください。暗号化したファイルは最終的に必要な場所 (例: {{ic|~/.offlineimappass.gpg}}) に移動してください。 |
||
| 214行目: | 258行目: | ||
{{hc|~/.offlineimap.py|2= |
{{hc|~/.offlineimap.py|2= |
||
| − | #! /usr/bin/env |
+ | #! /usr/bin/env python |
from subprocess import check_output |
from subprocess import check_output |
||
def get_pass(): |
def get_pass(): |
||
| − | return check_output("gpg -dq ~/.offlineimappass.gpg", shell=True). |
+ | return check_output("gpg -dq ~/.offlineimappass.gpg", shell=True).rstrip(b"\n") |
}} |
}} |
||
| 225行目: | 269行目: | ||
{{hc|~/.offlineimaprc|2= |
{{hc|~/.offlineimaprc|2= |
||
[general] |
[general] |
||
| + | # 任意のロードするPythonコードのパス |
||
| − | # Path to file with arbitrary Python code to be loaded |
||
pythonfile = ~/.offlineimap.py |
pythonfile = ~/.offlineimap.py |
||
... |
... |
||
[Repository ''example''] |
[Repository ''example''] |
||
| + | # 暗号化されたパスワードを復号化して読み取る |
||
| − | # Decrypt and read the encrypted password |
||
remotepasseval = get_pass() |
remotepasseval = get_pass() |
||
... |
... |
||
| 246行目: | 290行目: | ||
{{hc|~/.offlineimap.py|2= |
{{hc|~/.offlineimap.py|2= |
||
| − | #! /usr/bin/env |
+ | #! /usr/bin/env python |
from subprocess import check_output |
from subprocess import check_output |
||
| 259行目: | 303行目: | ||
{{hc|~/.offlineimaprc|2= |
{{hc|~/.offlineimaprc|2= |
||
[general] |
[general] |
||
| + | # 任意のロードするPythonコードのパス |
||
| − | # Path to file with arbitrary Python code to be loaded |
||
pythonfile = ~/.offlineimap.py |
pythonfile = ~/.offlineimap.py |
||
... |
... |
||
[Repository Gmail] |
[Repository Gmail] |
||
| + | # 暗号化されたパスワードを復号化して読み取る |
||
| − | # Decrypt and read the encrypted password |
||
remotepasseval = get_pass("Gmail") |
remotepasseval = get_pass("Gmail") |
||
... |
... |
||
| 271行目: | 315行目: | ||
==== Gnome Keyring ==== |
==== Gnome Keyring ==== |
||
| + | リモートリポジトリの設定では、remoteusereval/remotepassevalフィールドを、ユーザー名/パスワードに評価するカスタムPythonコードに設定できます。このコードは、'pythonfile' 設定フィールドで示されたpythonスクリプト上で定義された関数への呼び出しにすることができます。以下のサブセクションに従って{{ic|~/.offlineimap.py}}を作成し、設定で使用します。 |
||
| − | In configuration for remote repositories the remoteusereval/remotepasseval fields can be set to custom python code that evaluates to the username/password. The code can be a call to a function defined in a Python script pointed to by 'pythonfile' config field. Create {{ic|~/.offlineimap.py}} according to either of the two options below and use it in the configuration: |
||
{{bc|<nowiki> |
{{bc|<nowiki> |
||
| 284行目: | 328行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| + | ===== gkgetsecret.py ===== |
||
| − | ===== 方法 1: gnomekeyring の Python モジュールを使う ===== |
||
| + | {{Style|これは単一のサブセクションであるため、上記の導入と統合する必要があります。}} |
||
| − | {{pkg|python2-gnomekeyring}} をインストールしてください。 |
||
| + | {{Pkg|gnome-keyring}}、{{AUR|python2}}、{{AUR|python2-gobject}}、{{Pkg|libsecret}}がインストールされていることを確認して下さい。そして、{{ic|~/.offlineimap.py}}を次の内容で作成します: [https://pastebin.com/wzVfSac8 gkgetsecret.py]。さらに、上記のように{{ic|~/.offlineimaprc}}に{{ic|1=pythonfile = ~/.offlineimap.py}}を設定します。 |
||
| − | {{hc|~/.offlineimap.py|<nowiki> |
||
| − | #! /usr/bin/env python2 |
||
| + | {{Pkg|seahorse}}を使ってパスワードを作成した場合、その説明からパスワードを取得できます。例えば、''Password for me@myworkemail.com'' という説明つきのgnome-keyringに保存された ''work'' レポジトリのパスワードは、{{ic|~/.offlineimaprc}}に以下の行を追加することによって取得できます。 |
||
| − | import gnomekeyring as gkey |
||
| + | [Repository Work] |
||
| − | def set_credentials(repo, user, pw): |
||
| + | ... |
||
| − | KEYRING_NAME = "offlineimap" |
||
| + | remotepasseval = get_pw_from_desc("Password for me@myworkemail.com") |
||
| − | attrs = { "repo": repo, "user": user } |
||
| − | keyring = gkey.get_default_keyring_sync() |
||
| − | gkey.item_create_sync(keyring, gkey.ITEM_NETWORK_PASSWORD, |
||
| − | KEYRING_NAME, attrs, pw, True) |
||
| + | 同様にユーザー名も保存したい設定の場合、 ''secret-tool'' でパスワードを生成する方が、ユーザー名やリポジトリ名などの属性を設定するのに使えるのでよいでしょう。以下のコマンドでパスワードを作成することを検討して下さい。 |
||
| − | def get_credentials(repo): |
||
| + | $ secret-tool store --label "Password for Work Email" username me@myworkemail.com repo Work |
||
| − | keyring = gkey.get_default_keyring_sync() |
||
| − | attrs = {"repo": repo} |
||
| − | items = gkey.find_items_sync(gkey.ITEM_NETWORK_PASSWORD, attrs) |
||
| − | return (items[0].attributes["user"], items[0].secret) |
||
| + | このアカウントのユーザー名とパスワードは{{ic|~/.offlineimaprc}}に以下の行を追加することによって取得できます。 |
||
| − | def get_username(repo): |
||
| − | return get_credentials(repo)[0] |
||
| − | def get_password(repo): |
||
| − | return get_credentials(repo)[1] |
||
| + | [Repository Work] |
||
| − | if __name__ == "__main__": |
||
| + | ... |
||
| − | import sys |
||
| + | remoteusereval = get_val_from_attrs("username", "repo", "Work") |
||
| − | import os |
||
| + | remotepasseval = get_pw_from_attrs("repo", "Work") |
||
| − | import getpass |
||
| − | if len(sys.argv) != 3: |
||
| − | print "Usage: %s <repository> <username>" \ |
||
| − | % (os.path.basename(sys.argv[0])) |
||
| − | sys.exit(0) |
||
| − | repo, username = sys.argv[1:] |
||
| − | password = getpass.getpass("Enter password for user '%s': " % username) |
||
| − | password_confirmation = getpass.getpass("Confirm password: ") |
||
| − | if password != password_confirmation: |
||
| − | print "Error: password confirmation does not match" |
||
| − | sys.exit(1) |
||
| − | set_credentials(repo, username, password) |
||
| − | </nowiki>}} |
||
| − | |||
| − | 証明書を設定するために、シェルから上記のスクリプトを実行してください。 |
||
| − | |||
| − | ===== 方法 2: {{AUR|gnome-keyring-query}} ツールを使う ===== |
||
| − | |||
| − | {{hc|~/.offlineimap.py|<nowiki> |
||
| − | #! /usr/bin/env python2 |
||
| − | # executes gnome-keyring-query get passwd |
||
| − | # and returns the output |
||
| − | |||
| − | import locale |
||
| − | from subprocess import Popen, PIPE |
||
| − | |||
| − | encoding = locale.getdefaultlocale()[1] |
||
| − | |||
| − | def get_password(p): |
||
| − | (out, err) = Popen(["gnome-keyring-query", "get", p], stdout=PIPE).communicate() |
||
| − | return out.decode(encoding).strip() |
||
| − | </nowiki>}} |
||
| − | ==== |
+ | ==== python-keyring ==== |
| + | どのキーリングでも使える一般的な解決策があります。{{Pkg|python-keyring}}をインストールし、~/.offlineimaprcを以下のように変更して下さい。 |
||
| − | There is a general solution that should work for any keyring. Install [http://pypi.python.org/pypi/keyring python2-keyring] from [[AUR]], then change your ~/.offlineimaprc to say something like: |
||
{{bc|<nowiki> |
{{bc|<nowiki> |
||
| 357行目: | 360行目: | ||
[Repository RemoteEmail] |
[Repository RemoteEmail] |
||
remoteuser = username@host.net |
remoteuser = username@host.net |
||
| − | remotepasseval = keyring.get_password(" |
+ | remotepasseval = keyring.get_password("host","username") |
... |
... |
||
</nowiki>}} |
</nowiki>}} |
||
| − | + | そして、 ~/offlineimap.py のどこかに {{ic|import keyring}} を追加して下さい. |
|
| + | あとはパスワードを設定するだけです。次のようなpythonスクリプトか、 |
||
| − | {{bc|$ python2 |
||
| + | |||
| + | {{bc|$ python |
||
>>> import keyring |
>>> import keyring |
||
| − | >>> keyring.set_password(" |
+ | >>> keyring.set_password("host","username", "MYPASSWORD")}} |
| + | |||
| + | あるいはpython-keyringパッケージで提供されているkeyringコマンドを使えます。 |
||
| + | |||
| + | $ keyring --help |
||
| + | $ keyring set host username |
||
| + | Password for 'username' in 'host': |
||
| + | $ keyring get host username |
||
| + | password |
||
| + | これで、平文として保存したり毎回入力したりする必要なく、(kwallet/gnome-)キーリングからパスワードを取り出せるようになります。 |
||
| − | and it will grab the password from your (kwallet/gnome-) keyring instead of having to keep it in plaintext or enter it each time. |
||
==== Emacs EasyPG ==== |
==== Emacs EasyPG ==== |
||
| 373行目: | 386行目: | ||
http://www.emacswiki.org/emacs/OfflineIMAP#toc2 を見てください。 |
http://www.emacswiki.org/emacs/OfflineIMAP#toc2 を見てください。 |
||
| + | ==== KeePassXCとFreedesktop.orgのsecret-service ==== |
||
| − | ==== KeePass / KeePassX ==== |
||
| + | {{Pkg|libsecret}}をインストールし、KeepassXCの設定でFreedesktop.org secret-service統合を有効化、''Database Settings > Secret Service Integration''でエントリを公開し、''Edit Entry > Advanced''設定で{{ic|Title}}追加属性に{{ic|account@name.org}}を追加します。これで、{{ic|secret-tool lookup Title account@name.org}}コマンドでパスワードがコンソールに表示されるはずです。次に、pythonスクリプトを作成します: |
||
| − | AUR から {{AUR|python2-keepass-git}} をインストールして、{{ic|offlineimap.py}} ファイルに以下を追加してください: |
||
| + | {{hc| ~/.script.py|2= |
||
| − | {{bc|<nowiki> |
||
| − | #! /usr/bin/env |
+ | #! /usr/bin/env python |
| − | import os |
+ | import os |
| − | from |
+ | from subprocess import check_output |
| − | |||
| − | def get_keepass_pw(dbpath, title="", username=""): |
||
| − | if os.path.isfile(dbpath): |
||
| − | db = kpdb.Database(dbpath, getpass.getpass("Master password for '" + dbpath + "': ")) |
||
| − | for entry in db.entries: |
||
| − | if (entry.title == title) and (entry.username == username): |
||
| − | return entry.password |
||
| − | else: |
||
| − | print "Error: '" + dbpath + "' does not exist." |
||
| − | return |
||
| + | def get_pass(account): |
||
| − | </nowiki>}} |
||
| + | return check_output("secret-tool lookup Title " + account, shell=True).splitlines()[0].decode("UTF-8") |
||
| − | (Credit to aki--aki: https://gist.github.com/aki--aki/5180359) |
||
| + | }} |
||
| + | {{Pkg|python-secretstorage}}に依存する同等のスクリプトは以下の通りです: |
||
| − | Next, edit your ~/.offlineimaprc: |
||
| + | {{hc| ~/.script.py|2= |
||
| − | {{bc|<nowiki> |
||
| + | #! /usr/bin/env python |
||
| + | import secretstorage |
||
| + | from contextlib import closing |
||
| + | def get_pass(title): |
||
| + | with closing(secretstorage.dbus_init()) as conn: |
||
| + | assert(secretstorage.check_service_availability(conn)) |
||
| + | collection = secretstorage.get_default_collection(conn) |
||
| + | if collection.is_locked(): |
||
| + | collection.unlock() |
||
| + | matches = collection.search_items({"Title": title}) |
||
| + | entry = next(matches) |
||
| + | if entry.is_locked(): |
||
| + | entry.unlock() |
||
| + | return(entry.get_secret()) |
||
| + | }} |
||
| + | |||
| + | {{ic|~/.offlineimaprc}}からこのファイルをロードし、定義された関数を指定します: |
||
| + | |||
| + | {{hc|~/.offlineimaprc|2= |
||
[general] |
[general] |
||
| + | # 任意のロードするPythonコードのパス |
||
| − | # VVV Set this path correctly VVV |
||
| − | pythonfile = / |
+ | pythonfile = ~/.script.py |
... |
... |
||
| − | [Repository |
+ | [Repository Gmail] |
| + | # 暗号化されたパスワードを復号化して読み取る |
||
| − | remoteuser = username@host.net |
||
| + | remotepasseval = get_pass("account@name.org") |
||
| − | # Set the DB path as well as the title and username of the specific entry you'd like to use. |
||
| − | # This will prompt you on STDIN at runtime for the kdb master password. |
||
| − | remotepasseval = get_keepass_pw("/path/to/database.kdb", title="<entry title>", username="<entry username>") |
||
... |
... |
||
| + | }} |
||
| − | </nowiki>}} |
||
| − | |||
| − | Note that as-is, this does not support KDBs with keyfiles, only KDBs with password-only auth. |
||
| − | |||
| − | === Kerberos 認証 === |
||
| − | |||
| − | Install {{AUR|python2-kerberos}} from [[AUR]] and do not specify remotepass in your .offlineimaprc. |
||
| − | OfflineImap figure out the reset all if have a valid Kerberos TGT. |
||
| − | If you have 'maxconnections', it will fail for some connection. Comment 'maxconnections' out will solve this problem. |
||
== トラブルシューティング == |
== トラブルシューティング == |
||
| 422行目: | 435行目: | ||
=== UI の上書きと自動更新の設定 === |
=== UI の上書きと自動更新の設定 === |
||
| + | トラブルシューティングのために、より詳細なUI、バックグラウンド同期なし、場合によってはデバッグレベルでofflineimapを起動すると便利な場合があります: |
||
| − | For the sake of troubleshooting, it is sometimes convenient to launch offlineimap with a more verbose UI, no background syncs and perhaps even a debug level: |
||
$ offlineimap [ -o ] [ -d <debug_type> ] [ -u <ui> ] |
$ offlineimap [ -o ] [ -d <debug_type> ] [ -u <ui> ] |
||
;-o |
;-o |
||
| + | :自動更新、キープアライブ等を無効にします。 |
||
| − | :Disable autorefresh, keepalive, etc. |
||
;-d <debug_type> |
;-d <debug_type> |
||
| − | : |
+ | :''<debug_type>''は{{Ic|imap}}、{{Ic|maildir}}、{{Ic|thread}}のいずれかです。imapとmaildirのデバッグが圧倒的に最も便利です。 |
;-u <ui> |
;-u <ui> |
||
| − | : |
+ | :''<ui>''は{{Ic|CURSES.BLINKENLIGHTS}}、{{Ic|TTY.TTYUI}}、{{Ic|NONINTERACTIVE.BASIC}}、{{Ic|NONINTERACTIVE.QUIET}}、{{Ic|MACHINE.MACHINEUI}}のいずれかです。デバッグ目的ではTTY.TTYUIで十分です。 |
| − | {{Note| |
+ | {{Note|新しいバージョンでは以下を<ui>に使います: {{Ic|blinkenlights}}、{{Ic|ttyui}}、{{Ic|basic}}、{{Ic|quiet}}、{{Ic|machineui}}}} |
=== フォルダが作成できない === |
=== フォルダが作成できない === |
||
| + | [https://alioth-lists.debian.net/pipermail/offlineimap-project/2011-September/010200.html こちら]にあるように、バージョン6.5.3でofflineimapはリモートリポジトリにフォルダを作成できるようになりました。 |
||
| − | In version 6.5.3, offlineimap gained the ability to create folders in the remote repository, as described [http://comments.gmane.org/gmane.mail.imap.offlineimap.general/4784 here]. |
||
| + | これにより、リモートリポジトリで{{Ic|nametrans}}を使用すると以下のようなエラーが発生する可能性があります: |
||
| − | This can lead to errors of the following form when using {{Ic|nametrans}} on the remote repository: |
||
ERROR: Creating folder bar on repository foo-remote |
ERROR: Creating folder bar on repository foo-remote |
||
Folder 'bar'[foo-remote] could not be created. Server responded: ('NO', ['[ALREADYEXISTS] Duplicate folder name bar (Failure)']) |
Folder 'bar'[foo-remote] could not be created. Server responded: ('NO', ['[ALREADYEXISTS] Duplicate folder name bar (Failure)']) |
||
| + | 解決策は、ローカルリポジトリに逆{{Ic|nametrans}}ラムダを提供することです。例: |
||
| − | The solution is to provide an inverse {{Ic|nametrans}} lambda for the local repository, e.g. |
||
{{hc|~/.offlineimaprc|<nowiki> |
{{hc|~/.offlineimaprc|<nowiki> |
||
| 453行目: | 466行目: | ||
</nowiki>}} |
</nowiki>}} |
||
| − | * |
+ | * 正しい逆マッピングを行うには、{{Ic|offlineimap --info}}の出力が役に立つでしょう。 |
| + | * マッピングを更新したら、影響を受けるアカウントの{{Ic|$HOME/.offlineimap/}}下のフォルダを全て削除する必要があるかもしれません。 |
||
| − | * After updating the mapping, it may be necessary to remove all of the folders under {{Ic|$HOME/.offlineimap/}} for the affected accounts. |
||
=== SSL フィンガープリントが一致しない === |
=== SSL フィンガープリントが一致しない === |
||
| 460行目: | 473行目: | ||
ERROR: Server SSL fingerprint 'keykeykey' for hostname 'example.com' does not match configured fingerprint. Please verify and set 'cert_fingerprint' accordingly if not set yet. |
ERROR: Server SSL fingerprint 'keykeykey' for hostname 'example.com' does not match configured fingerprint. Please verify and set 'cert_fingerprint' accordingly if not set yet. |
||
| − | + | これを解決するには、{{ic|~/.offlineimaprc}} ({{ic|1=ssl = yes}}と同じセクション)に以下のいずれかを追加します。 |
|
| + | * リモートサーバーの証明書のフィンガープリントを{{ic|cert_fingerprint}}に追加します。これは、リモートサーバーの証明書が与えられたフィンガープリントに一致するかチェックします。 |
||
| − | * either add {{ic|cert_fingerprint}}, with the certificate fingerprint of the remote server. This checks whether the remote server certificate matches the given fingerprint. |
||
cert_fingerprint = keykeykey |
cert_fingerprint = keykeykey |
||
| + | * もしくは、システムのCA証明書ファイルを{{ic|sslcacertfile}}に追加します。{{Pkg|ca-certificates}}のインストールが必要です。これは、リモートのSSL証明書チェーンを、そのファイルの認証局に対して検証します。 |
||
| − | * or add {{ic|sslcacertfile}} with the path to the system CA certificates file. Needs {{Pkg|ca-certificates}} installed. This validates the remote ssl certificate chain against the Certification Authorities in that file. |
||
| − | + | {{bc|1=sslcacertfile = /etc/ssl/certs/ca-certificates.crt}} |
|
=== Copying message, connection closed === |
=== Copying message, connection closed === |
||
| 476行目: | 489行目: | ||
* [http://lists.alioth.debian.org/mailman/listinfo/offlineimap-project OfflineIMAP 公式メーリングリスト] |
* [http://lists.alioth.debian.org/mailman/listinfo/offlineimap-project OfflineIMAP 公式メーリングリスト] |
||
| − | * [http://pbrisbin.com/posts/mutt_gmail_offlineimap/ Mutt + Gmail + Offlineimap] - |
+ | * [http://pbrisbin.com/posts/mutt_gmail_offlineimap/ Mutt + Gmail + Offlineimap] - brisbinによるシンプルなgmail/muttの設定の概要。cronでofflineimapの同期を保持させています。 |
2025年6月5日 (木) 15:52時点における最新版
OfflineIMAP は IMAP サーバーからメールを同期する Python ユーティリティです。POP3 プロトコルや mbox は使えないため、通常 Mutt などの MUA と組み合わされて使われます。
目次
インストール
offlineimap をインストールしてください。
設定
Offlineimap には2つのデフォルト設定ファイルが付属しており、どちらも /usr/share/offlineimap/ にあります。offlineimap.conf には全ての設定が含まれておりコメントが付与されています。一方、offlineimap.conf.minimal にはコメントが付いておらず多少の設定しか含まれていません (参照: #最小)。
デフォルト設定ファイルのどちらかを ~/.offlineimaprc または $XDG_CONFIG_HOME/offlineimap/config にコピーしてください。
最小
offlineimap.conf.minimal のコメントが付いたバージョンは以下になります:
~/.offlineimaprc
[general] # 同期するアカウントのリスト。コンマで区切る。 accounts = main [Account main] # ローカルリポジトリの識別子。例: IMAP経由で同期されるmaildir。 localrepository = main-local # リモートリポジトリの識別子。すなわち実際のIMAPで、通常はローカルではありません。 remoterepository = main-remote [Repository main-local] # OfflineImapは、Maildir、GmailMaildir、およびIMAPをローカルリポジトリとしてサポートしています。 type = Maildir # メールを配置する場所 localfolders = ~/mail [Repository main-remote] # リモートレポジトリはIMAPまたはGmailであり、後者は事前に設定されたIMAPです。 type = IMAP remotehost = host.domain.tld remoteuser = username # OfflineImapのバージョンが6.5.4より高い場合、SSL接続に必要です。 sslcacertfile = /etc/ssl/certs/ca-certificates.crt
フォルダの同期の選択
特定のフォルダだけを同期したい場合、~/.offlineimaprc でアカウントの remote セクションに folderfilter を使います。例えば、以下の設定では Inbox と Sent フォルダだけが同期されます:
~/.offlineimaprc
[Repository main-remote] # InboxとSentフォルダーのみ同期します: folderfilter = lambda foldername: foldername in ["Inbox", "Sent"] ...
他のオプションについては、公式ドキュメント を見て下さい。
カスタムポート
IMAPサーバーの中には、デフォルトの993ポートではなく、カスタムポートの接続を要求してくるものがあります。そのようにするには、~/.offlineimaprcの remote セクションに remoteport オプションを追加します:
~/.offlineimaprc
[Repository main-remote] remoteport=1234
使用方法
offlineimap を実行する前に、ローカルリポジトリに割り当てる親ディレクトリを作成してください:
$ mkdir ~/Maildir
そして、プログラムを実行してください:
$ offlineimap
メールアカウントが同期されます。何か問題が発生した場合、エラーメッセージをよく見て下さい。OfflineIMAP は基本的に問題について詳しい出力を表示します。最終的な製品からのトレースバックを取得するのに開発者が悩む必要がないようにするためです。
ヒントとテクニック
バックグラウンドで offlineimap を実行
大抵のメール転送エージェントはデフォルトで、プログラムを定期的に同期させることでデーモンとしてツールを使うのが前提になっています。offlineimap では、バックグラウンドで作業を制御する設定はあまりありません。
紛らわしいことに、バックグラウンドの設定は設定ファイルのあらゆるところに散らばっています:
~/.offlineimaprc
# generalセクション [general] # 同時に同期するアカウントの数を制御します maxsyncaccounts = 1 # アカウント識別子 [Account main] # 同期の間隔(分) autorefresh = 0.5 # IMAPフラグにのみ変更があった場合、quick-syncsは更新されません。 # autorefresh=0.5 と quick=10 を組み合わせると、 # タイプに関係なく、各完全更新の間に0.5分の間隔で # 10回のクイック更新が行われます。 quick = 10 # リモートリポジトリ識別子 [Repository main-remote] # 同期が完了すると接続を閉じる代わりに、offlineimapはサーバーに空のデータを送信して接続を維持します。 # この値を60に設定すると、同期(クイックと自動更新の両方)の間に1分間接続を維持しようとします。 # この設定は、autorefresh と holdconnectionopen の両方が設定されていない場合は無効です。 keepalive = 60 # OfflineIMAPは通常、autorefreshグローバルオプションが指定されている場合、更新の合間にIMAPサーバ接続を閉じます。 # 接続を維持したい場合は、この設定をtrueに設定してください。 # autorefreshが設定されていない場合、この設定は効果がありません。 holdconnectionopen = yes
ログイン時にデーモンを自動的に起動するには、systemd/ユーザーサービスであるofflineimap.serviceを--userフラグ付きで起動/有効化してください。
複数のアカウントを設定している場合は、maxsyncaccountsパラメータ[1]を増やすのではなく、offlineimap@.serviceを使用することをお勧めします。offlineimap@youraccountname.serviceを起動/有効化するだけです。
systemd timer
あるいは、systemd-user タイマーを使って、offlineimap-oneshot.timerを--userフラグで起動/有効化することで、OfflineIMAPを完全に管理することもできます。
このタイマーはデフォルトで15分ごとにOfflineIMAPを実行します。これは、ドロップインファイルを作成することで変更することができます。例えば、以下では5分ごとにチェックするようにタイマーを修正しています:
~/.config/systemd/user/offlineimap-oneshot.timer.d/timer.conf
[Timer] OnUnitInactiveSec=5m
より堅牢な解決法としては、フリーズした場合にOfflineIMAPをkillするウォッチドッグを設定することができます。
~/.config/systemd/user/offlineimap-oneshot.service.d/service.conf
[Service] WatchdogSec=300
mutt 用に自動的にメールボックスを作成
Muttは 単にIMAP や Maildir のディレクトリを指し示したり、どのサブディレクトリがメールボックスであるかを推測することはできませんが、 offlineimap は同期するメールボックスを含む muttrc フラグメントを生成することができます。
~/.offlineimaprc
[mbnames] enabled = yes filename = ~/.mutt/mailboxes header = "mailboxes " peritem = "+%(accountname)s/%(foldername)s" sep = " " footer = "\n"
そして以下の行を~/.mutt/muttrcに追加します。
~/.mutt/muttrc
# IMAP: offlineimap set folder = "~/Mail" source ~/.mutt/mailboxes set spoolfile = "+account/INBOX" set record = "+account/Sent\ Items" set postponed = "+account/Drafts"
accountは~/.offlineimaprcでIMAPアカウントにつけた名前です。
Gmail の設定
このリモートリポジトリは Gmail をサポートするために特別に設定されており、フォルダ名を大文字から小文字に置き換えたり、その他の細かい追加を行います。この設定では「すべてのメール」フォルダーを同期しないことに注意して下さい。「すべてのメール」は通常不要であり、この同期をスキップすることにより帯域幅コストを抑えるためです:
~/.offlineimaprc
[Repository gmail-remote]
type = Gmail
remoteuser = user@gmail.com
remotepass = password
nametrans = lambda foldername: re.sub ('^\[gmail\]', 'bak',
re.sub ('sent_mail', 'sent',
re.sub ('starred', 'flagged',
re.sub (' ', '_', foldername.lower()))))
folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']
# OfflineImap 6.5.4 で必要
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
# https://github.com/OfflineIMAP/offlineimap/issues/573(バージョン7.0.12、7.2.1)の対処に必要
ssl_version = tls1_2
oama経由のOAuth2アクセストークン
oama (oama-binAUR)は、IMAP/SMTPクライアントにOAuth2認証の更新機能と承認を提供するユーティリティです。
OfflineIMAPは、設定からPythonコードを呼び出すことができます。したがって、まず[general]セクションで、以下の行を追加します。
~/.offlineimaprc
[general] pythonfile = ~/.offlineimap.py
Pythonファイルに以下のコードを追加して、oamaを経由してOAuth2アクセストークンを取得します:
~/.offlineimap.py
import subprocess
def get_token(email_address):
return subprocess.run(["oama", "access", email_address], capture_output=True, text=True).stdout
そして設定ファイルに戻り、Gmailアカウントのrepositoryセクションに以下を追加してください:
~/.offlineimaprc
auth_mechanisms = XOAUTH2
oauth2_client_id = YOUR_OAUTH2_CLIENT_ID
oauth2_client_secret = YOUR_OAUTH2_CLIENT_SECRET
oauth2_request_url = https://accounts.google.com/o/oauth2/token
oauth2_access_token_eval = get_token("YOUR_EMAIL_ADDRESS_FOR_THIS_ACCOUNT")
パスワードの管理
.netrc
以下の行を ~/.netrc に追加してください:
machine hostname.tld
login [your username]
password [your password]
ファイルに600や700のような適切な権限を与えることを忘れないでください。
$ chmod 600 ~/.netrc
GPG を使う
GNU Privacy Guard を使うことで暗号化ファイルにパスワードを保存することができます。このセクションの設定を行う前にまず GnuPG をセットアップしてください。パスワードを入力しなくても GPG の秘密鍵をいつでも使えるようにしておく必要があります。
まずプレーンテキストファイルにメールアカウントのパスワードを入力してください。暗号化されていないパスワードがディスクに書き込まれないように、ファイルの作成は tmpfs 上の 700 パーミッションの安全なディレクトリで行って下さい。それから、GnuPGで自身を受信者にしてファイルを暗号化します:
必要なくなったプレーンテキストファイルは削除してください。暗号化したファイルは最終的に必要な場所 (例: ~/.offlineimappass.gpg) に移動してください。
次にパスワードを復号化する python 関数を作成します:
~/.offlineimap.py
#! /usr/bin/env python
from subprocess import check_output
def get_pass():
return check_output("gpg -dq ~/.offlineimappass.gpg", shell=True).rstrip(b"\n")
上記のファイルを ~/.offlineimaprc からロードして、定義した関数を指定してください:
~/.offlineimaprc
[general] # 任意のロードするPythonコードのパス pythonfile = ~/.offlineimap.py ... [Repository example] # 暗号化されたパスワードを復号化して読み取る remotepasseval = get_pass() ...
pass を使う
pass は GPG ベースのシンプルなコマンドラインのパスワードマネージャです。
最初にメールアカウントのパスワードを作成:
$ pass insert Mail/account
次にパスワードを復号化する python 関数を作成:
~/.offlineimap.py
#! /usr/bin/env python
from subprocess import check_output
def get_pass(account):
return check_output("pass Mail/" + account, shell=True).splitlines()[0]
上記はマルチアカウントセットアップの例です。前述の pass の引数はカスタマイズできます。
~/.offlineimaprc からファイルをロードして、定義した関数を指定:
~/.offlineimaprc
[general]
# 任意のロードするPythonコードのパス
pythonfile = ~/.offlineimap.py
...
[Repository Gmail]
# 暗号化されたパスワードを復号化して読み取る
remotepasseval = get_pass("Gmail")
...
Gnome Keyring
リモートリポジトリの設定では、remoteusereval/remotepassevalフィールドを、ユーザー名/パスワードに評価するカスタムPythonコードに設定できます。このコードは、'pythonfile' 設定フィールドで示されたpythonスクリプト上で定義された関数への呼び出しにすることができます。以下のサブセクションに従って~/.offlineimap.pyを作成し、設定で使用します。
[general]
pythonfile = ~/.offlineimap.py
[Repository examplerepo]
type = IMAP
remotehost = mail.example.com
remoteusereval = get_username("examplerepo")
remotepasseval = get_password("examplerepo")
gkgetsecret.py
gnome-keyring、python2AUR、python2-gobjectAUR、libsecretがインストールされていることを確認して下さい。そして、~/.offlineimap.pyを次の内容で作成します: gkgetsecret.py。さらに、上記のように~/.offlineimaprcにpythonfile = ~/.offlineimap.pyを設定します。
seahorseを使ってパスワードを作成した場合、その説明からパスワードを取得できます。例えば、Password for me@myworkemail.com という説明つきのgnome-keyringに保存された work レポジトリのパスワードは、~/.offlineimaprcに以下の行を追加することによって取得できます。
[Repository Work]
...
remotepasseval = get_pw_from_desc("Password for me@myworkemail.com")
同様にユーザー名も保存したい設定の場合、 secret-tool でパスワードを生成する方が、ユーザー名やリポジトリ名などの属性を設定するのに使えるのでよいでしょう。以下のコマンドでパスワードを作成することを検討して下さい。
$ secret-tool store --label "Password for Work Email" username me@myworkemail.com repo Work
このアカウントのユーザー名とパスワードは~/.offlineimaprcに以下の行を追加することによって取得できます。
[Repository Work]
...
remoteusereval = get_val_from_attrs("username", "repo", "Work")
remotepasseval = get_pw_from_attrs("repo", "Work")
python-keyring
どのキーリングでも使える一般的な解決策があります。python-keyringをインストールし、~/.offlineimaprcを以下のように変更して下さい。
[general]
pythonfile = /home/user/offlineimap.py
...
[Repository RemoteEmail]
remoteuser = username@host.net
remotepasseval = keyring.get_password("host","username")
...
そして、 ~/offlineimap.py のどこかに import keyring を追加して下さい.
あとはパスワードを設定するだけです。次のようなpythonスクリプトか、
$ python
>>> import keyring
>>> keyring.set_password("host","username", "MYPASSWORD")
あるいはpython-keyringパッケージで提供されているkeyringコマンドを使えます。
$ keyring --help $ keyring set host username Password for 'username' in 'host': $ keyring get host username password
これで、平文として保存したり毎回入力したりする必要なく、(kwallet/gnome-)キーリングからパスワードを取り出せるようになります。
Emacs EasyPG
http://www.emacswiki.org/emacs/OfflineIMAP#toc2 を見てください。
KeePassXCとFreedesktop.orgのsecret-service
libsecretをインストールし、KeepassXCの設定でFreedesktop.org secret-service統合を有効化、Database Settings > Secret Service Integrationでエントリを公開し、Edit Entry > Advanced設定でTitle追加属性にaccount@name.orgを追加します。これで、secret-tool lookup Title account@name.orgコマンドでパスワードがコンソールに表示されるはずです。次に、pythonスクリプトを作成します:
~/.script.py
#! /usr/bin/env python
import os
from subprocess import check_output
def get_pass(account):
return check_output("secret-tool lookup Title " + account, shell=True).splitlines()[0].decode("UTF-8")
python-secretstorageに依存する同等のスクリプトは以下の通りです:
~/.script.py
#! /usr/bin/env python
import secretstorage
from contextlib import closing
def get_pass(title):
with closing(secretstorage.dbus_init()) as conn:
assert(secretstorage.check_service_availability(conn))
collection = secretstorage.get_default_collection(conn)
if collection.is_locked():
collection.unlock()
matches = collection.search_items({"Title": title})
entry = next(matches)
if entry.is_locked():
entry.unlock()
return(entry.get_secret())
~/.offlineimaprcからこのファイルをロードし、定義された関数を指定します:
~/.offlineimaprc
[general]
# 任意のロードするPythonコードのパス
pythonfile = ~/.script.py
...
[Repository Gmail]
# 暗号化されたパスワードを復号化して読み取る
remotepasseval = get_pass("account@name.org")
...
トラブルシューティング
UI の上書きと自動更新の設定
トラブルシューティングのために、より詳細なUI、バックグラウンド同期なし、場合によってはデバッグレベルでofflineimapを起動すると便利な場合があります:
$ offlineimap [ -o ] [ -d <debug_type> ] [ -u <ui> ]
- -o
- 自動更新、キープアライブ等を無効にします。
- -d <debug_type>
- <debug_type>は
imap、maildir、threadのいずれかです。imapとmaildirのデバッグが圧倒的に最も便利です。
- -u <ui>
- <ui>は
CURSES.BLINKENLIGHTS、TTY.TTYUI、NONINTERACTIVE.BASIC、NONINTERACTIVE.QUIET、MACHINE.MACHINEUIのいずれかです。デバッグ目的ではTTY.TTYUIで十分です。
フォルダが作成できない
こちらにあるように、バージョン6.5.3でofflineimapはリモートリポジトリにフォルダを作成できるようになりました。
これにより、リモートリポジトリでnametransを使用すると以下のようなエラーが発生する可能性があります:
ERROR: Creating folder bar on repository foo-remote
Folder 'bar'[foo-remote] could not be created. Server responded: ('NO', ['[ALREADYEXISTS] Duplicate folder name bar (Failure)'])
解決策は、ローカルリポジトリに逆nametransラムダを提供することです。例:
~/.offlineimaprc
[Repository foo-local]
nametrans = lambda foldername: foldername.replace('bar', 'BAR')
[Repository foo-remote]
nametrans = lambda foldername: foldername.replace('BAR', 'bar')
- 正しい逆マッピングを行うには、
offlineimap --infoの出力が役に立つでしょう。 - マッピングを更新したら、影響を受けるアカウントの
$HOME/.offlineimap/下のフォルダを全て削除する必要があるかもしれません。
SSL フィンガープリントが一致しない
ERROR: Server SSL fingerprint 'keykeykey' for hostname 'example.com' does not match configured fingerprint. Please verify and set 'cert_fingerprint' accordingly if not set yet.
これを解決するには、~/.offlineimaprc (ssl = yesと同じセクション)に以下のいずれかを追加します。
- リモートサーバーの証明書のフィンガープリントを
cert_fingerprintに追加します。これは、リモートサーバーの証明書が与えられたフィンガープリントに一致するかチェックします。
cert_fingerprint = keykeykey
- もしくは、システムのCA証明書ファイルを
sslcacertfileに追加します。ca-certificatesのインストールが必要です。これは、リモートのSSL証明書チェーンを、そのファイルの認証局に対して検証します。
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
Copying message, connection closed
ERROR: Copying message -2 [acc: email] connection closed Folder sent [acc: email]: ERROR: while syncing sent [account email] connection closed
上記の問題はローカルとサーバーで同じメッセージが作成されていることが原因です。メールのプロバイダが自動的にローカルクライアントと同じフォルダに送信したメールを保存した場合に発生します。この問題が起こったときはローカルクライアントで送信済みメッセージの保存を無効化してください。
参照
- OfflineIMAP 公式メーリングリスト
- Mutt + Gmail + Offlineimap - brisbinによるシンプルなgmail/muttの設定の概要。cronでofflineimapの同期を保持させています。