]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/base.py
lists: Implement some basic code to talk to Mailman
[ipfire.org.git] / src / backend / base.py
CommitLineData
a6dc0bad
MT
1#!/usr/bin/python
2
c5ddbd67 3import configparser
11347e46 4import io
7b05edde 5import location
59468469
MT
6import ssl
7import tempfile
23f84bbc 8import tornado.httpclient
11347e46
MT
9
10from . import accounts
d6c41da2 11from . import asterisk
05e8c602 12from . import blog
26ccb61a 13from . import bugzilla
f0a8b392 14from . import cache
d73bba54 15from . import campaigns
11347e46 16from . import database
11347e46 17from . import fireinfo
34999287 18from . import httpclient
11347e46 19from . import iuse
97e15cf6 20from . import lists
d6df53bf 21from . import messages
11347e46
MT
22from . import mirrors
23from . import netboot
24from . import nopaste
372ef119 25from . import ratelimit
11347e46 26from . import releases
440aba92 27from . import resolver
11347e46 28from . import settings
66d4b1c1 29from . import toots
440aba92 30from . import util
181d08f3 31from . import wiki
2c361abc 32from . import zeiterfassung
d6df53bf 33from .decorators import *
2c361abc 34
11347e46 35DEFAULT_CONFIG = io.StringIO("""
9ed02e3b
MT
36[global]
37debug = false
38
39data_dir =
40static_dir = %(data_dir)s/static
41templates_dir = %(data_dir)s/templates
42""")
43
a6dc0bad 44class Backend(object):
34999287
MT
45 version = 0
46
2cd9af74 47 def __init__(self, configfile, debug=False):
9068dba1
MT
48 # Read configuration file.
49 self.config = self.read_config(configfile)
9ed02e3b
MT
50
51 # Enable debug logging if configured
52 self.debug = debug or self.config.getboolean("global", "debug")
9068dba1
MT
53
54 # Setup database.
55 self.setup_database()
a6dc0bad 56
23f84bbc 57 # Create HTTPClient
34999287
MT
58 self.http_client = httpclient.HTTPClient(self)
59
f0a8b392
MT
60 # Initialize the cache
61 self.cache = cache.Cache(self)
62
4f043f79 63 # Initialize settings first
9068dba1 64 self.settings = settings.Settings(self)
9068dba1
MT
65
66 # Initialize backend modules.
a6dc0bad 67 self.accounts = accounts.Accounts(self)
26ccb61a 68 self.bugzilla = bugzilla.Bugzilla(self)
66862195 69 self.fireinfo = fireinfo.Fireinfo(self)
9068dba1
MT
70 self.iuse = iuse.IUse(self)
71 self.mirrors = mirrors.Mirrors(self)
72 self.netboot = netboot.NetBoot(self)
66862195 73 self.nopaste = nopaste.Nopaste(self)
9068dba1 74 self.releases = releases.Releases(self)
9068dba1 75
0a6875dc 76 self.blog = blog.Blog(self)
181d08f3 77 self.wiki = wiki.Wiki(self)
2c361abc
MT
78 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
79
9068dba1
MT
80 def read_config(self, configfile):
81 cp = configparser.ConfigParser()
9ed02e3b
MT
82
83 # Initialize configuration with some sensible defaults
84 cp.readfp(DEFAULT_CONFIG)
85
86 # Parse file
9068dba1
MT
87 cp.read(configfile)
88
89 return cp
90
91 def setup_database(self):
92 """
93 Sets up the database connection.
94 """
95 credentials = {
96 "host" : self.config.get("database", "server"),
97 "database" : self.config.get("database", "database"),
98 "user" : self.config.get("database", "username"),
99 "password" : self.config.get("database", "password"),
100 }
101
3082f0e9 102 self.db = database.Connection(self, **credentials)
c5ddbd67 103
59468469
MT
104 @lazy_property
105 def ssl_context(self):
106 # Create SSL context
107 context = ssl.create_default_context()
108
109 # Fetch client certificate
110 certificate = self.settings.get("client-certificate", None)
111 key = self.settings.get("client-key", None)
112
113 # Apply client certificate
114 if certificate and key:
115 with tempfile.NamedTemporaryFile(mode="w") as f_cert:
116 f_cert.write(certificate)
117 f_cert.flush()
118
119 with tempfile.NamedTemporaryFile(mode="w") as f_key:
120 f_key.write(key)
121 f_key.flush()
122
123 context.load_cert_chain(f_cert.name, f_key.name)
124
125 return context
126
127 async def load_certificate(self, certfile, keyfile):
128 with self.db.transaction():
129 # Load certificate
130 with open(certfile) as f:
131 self.settings.set("client-certificate", f.read())
132
133 # Load key file
134 with open(keyfile) as f:
135 self.settings.set("client-key", f.read())
136
9fdf4fb7 137 async def run_task(self, task, *args, **kwargs):
c5ddbd67 138 tasks = {
26ccb61a 139 "accounts:delete" : self.accounts._delete,
aee57270
MT
140 "announce-blog-posts" : self.blog.announce,
141 "check-mirrors" : self.mirrors.check_all,
aee57270 142 "cleanup" : self.cleanup,
5bfc6729 143 "get-all-emails" : self.accounts.get_all_emails,
aee57270 144 "launch-campaigns" : self.campaigns.launch_manually,
59468469 145 "load-certificate" : self.load_certificate,
aee57270
MT
146 "run-campaigns" : self.campaigns.run,
147 "scan-files" : self.releases.scan_files,
148 "send-message" : self.messages.send_cli,
149 "send-all-messages" : self.messages.queue.send_all,
aee57270 150 "test-ldap" : self.accounts.test_ldap,
66d4b1c1 151 "toot" : self.toots.toot,
aee57270 152 "update-blog-feeds" : self.blog.update_feeds,
c5ddbd67
MT
153 }
154
155 # Get the task from the list of all tasks
156 func = tasks.get(task, None)
157 if not func:
158 raise ValueError("Unknown task: %s" % task)
159
160 # Run the task
9fdf4fb7 161 r = await func(*args, **kwargs)
c5ddbd67
MT
162
163 # If any error code has been returned,
164 # we will end the program
165 if r:
166 raise SystemExit(r)
d6df53bf 167
19f8de1b
MT
168 @lazy_property
169 def asterisk(self):
170 return asterisk.Asterisk(self)
171
d73bba54
MT
172 @lazy_property
173 def campaigns(self):
174 return campaigns.Campaigns(self)
175
d8b04c72
MT
176 @lazy_property
177 def groups(self):
178 return accounts.Groups(self)
179
97e15cf6
MT
180 @lazy_property
181 def lists(self):
182 return lists.Lists(self)
183
d6df53bf
MT
184 @lazy_property
185 def messages(self):
186 return messages.Messages(self)
611adbfb 187
7b05edde
MT
188 @lazy_property
189 def location(self):
190 return location.Database("/var/lib/location/database.db")
191
e929ed92
MT
192 def get_country_name(self, country_code):
193 country = self.location.get_country(country_code)
194
195 if country:
196 return country.name
197
372ef119
MT
198 @lazy_property
199 def ratelimiter(self):
200 return ratelimit.RateLimiter(self)
201
440aba92
MT
202 @lazy_property
203 def resolver(self):
204 return resolver.Resolver(tries=2, timeout=2, domains=[])
205
611adbfb 206 @lazy_property
66d4b1c1
MT
207 def toots(self):
208 return toots.Toots(self)
8e69850a 209
9fdf4fb7 210 async def cleanup(self):
8e69850a
MT
211 # Cleanup message queue
212 with self.db.transaction():
213 self.messages.queue.cleanup()
214
215 # Cleanup in accounts
216 with self.db.transaction():
217 self.accounts.cleanup()