-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
imap2gotify.py
executable file
·59 lines (47 loc) · 1.87 KB
/
imap2gotify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
from __future__ import annotations
import logging
import os
import sys
from collections.abc import MutableMapping
import toml
from gotify import Gotify
from imap import Imap
from rules import RulesProcessor
class Imap2Gotify:
def __init__(self, config: MutableMapping):
"""Initialize our Imap2Gotify instance using toml config"""
self.config = config
def run(self):
"""Process unread emails and then go into IDLE loop waiting for
notification of new messages arriving, never to return except
upon KeyboardInterrupt"""
while True:
try:
client = Imap(self.config)
rules = RulesProcessor(self.config)
gotify = Gotify(self.config)
# gets new messages, runs against the rules, marks new that dont
# match a rule as read, sents matched messages to gotify,
# those messages that were succefully sent via gotify are
# then marked as read
new_messages = client.get_unread()
(matched_results, not_matched_results) = rules.check_matches(
new_messages,
)
client.mark_as_read([r.message for r in not_matched_results])
if matched_results:
gotify_sent_messages = gotify.send(matched_results)
client.mark_as_read(gotify_sent_messages)
# Go into idle waiting for new emails
client.wait_for_new()
except KeyboardInterrupt:
break
if __name__ == "__main__":
try:
config = toml.load([os.path.abspath("config/settings.toml")])
except FileNotFoundError:
logging.error("No config/settings.toml file was found, terminating")
sys.exit(1)
processor = Imap2Gotify(config)
processor.run()