forked from brenthuisman/imapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.py
151 lines (119 loc) · 5.17 KB
/
configuration.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import imaplib
import getpass
import os
from six.moves import configparser
class Account:
def __init__(self, name):
self.name = name
self.host = None
self.port = 993
self.remote_folder = 'INBOX'
self.username = None
self.password = None
self.ssl = False
def get_mailbox(self):
if not self.ssl:
mailbox = imaplib.IMAP4(self.host, self.port)
else:
mailbox = imaplib.IMAP4_SSL(self.host, self.port)
mailbox.login(self.username, self.password)
return mailbox
def get_folder_fist(self):
mailbox = self.get_mailbox()
folder_list = mailbox.list()[1]
try:
mailbox.close()
except Exception as e:
print(e)
mailbox.logout()
result = []
try:
folder_entry: bytes
for folder_entry in folder_list:
folder = folder_entry.decode().replace("/", ".").split(' "." ')[1]
folder = folder.strip().replace('"', '').replace('\r', '').replace('\n', '')
result.append(folder)
except Exception as e:
print("Couldn't clean folder list:")
print(folder_list)
print(e)
return result
class Options:
def __init__(self, args):
self.args = args
self.days = int(os.getenv('IMAPBOX_DAYS', None)) if os.getenv('IMAPBOX_DAYS') else None
self.local_folder = os.getenv('IMAPBOX_LOCAL_FOLDER', '.')
self.wkhtmltopdf = os.getenv('IMAPBOX_WKHTMLTOPDF', None)
self.json = self.load_bool(os.getenv('IMAPBOX_JSON'), True)
self.local_subfolder = self.load_bool(os.getenv('IMAPBOX_LOCAL_SUBFOLDER'), True)
self.accounts: [Account]
self.accounts = []
self.load_config()
def load_bool(self, value, default: bool):
if isinstance(value, bool) and value == True:
return True
if value is None:
return default
if value == '':
return default
if value.lower() in ['true', '1', 't', 'y', 'yes']:
return True
return False
def load_config(self):
config = configparser.ConfigParser(allow_no_value=True)
config.read([
'./config.cfg',
'./test/config.cfg',
os.path.expanduser('~/.config/imapbox/config.cfg'),
'/etc/imapbox/config.cfg',
os.path.join(self.local_folder, 'config.cfg'),
])
if config.has_section('imapbox'):
if config.has_option('imapbox', 'days'):
self.days = config.getint('imapbox', 'days')
if config.has_option('imapbox', 'local_folder'):
self.local_folder = os.path.expanduser(config.get('imapbox', 'local_folder'))
if config.has_option('imapbox', 'local_subfolder'):
self.local_folder = os.path.expanduser(config.get('imapbox', 'local_folder'))
if config.has_option('imapbox', 'wkhtmltopdf'):
self.wkhtmltopdf = os.path.expanduser(config.get('imapbox', 'wkhtmltopdf'))
if config.has_option('imapbox', 'json'):
self.json = self.load_bool(config.get('imapbox', 'json'), True)
for section in config.sections():
if 'imapbox' == section:
continue
if self.args.specific_account and (self.args.specific_account != section):
continue
account = Account(section)
if config.has_option(section, 'host'):
account.host = config.get(section, 'host')
if config.has_option(section, 'username'):
account.username = config.get(section, 'username')
if config.has_option(section, 'port'):
account.port = config.get(section, 'port')
if config.has_option(section, 'password'):
account.password = config.get(section, 'password')
else:
prompt = ('Password for ' + account.username + ':' + account.host + ': ')
account.password = getpass.getpass(prompt=prompt)
if config.has_option(section, 'ssl'):
account.ssl = self.load_bool(config.get(section, 'ssl'), False)
if config.has_option(section, 'remote_folder'):
account.remote_folder = config.get(section, 'remote_folder')
if account.host is None or account.username is None or account.password is None:
print('missing host/username/password for '+section)
continue
self.accounts.append(account)
if self.args.local_folder:
self.local_folder = self.args.local_folder
if self.args.days:
self.days = self.args.days
if self.args.wkhtmltopdf:
self.wkhtmltopdf = self.args.wkhtmltopdf
if self.args.json:
self.json = self.load_bool(self.args.json, True)
if self.args.local_subfolder:
self.local_subfolder = self.args.local_subfolder
print('days: {}, local_folder: {}, wkhtmltopdf: {}, json: {}'.format(self.days, self.local_folder, self.wkhtmltopdf, self.json))