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