]>
Commit | Line | Data |
---|---|---|
1 | #/usr/bin/python | |
2 | ||
3 | import logging | |
4 | import itertools | |
5 | import os.path | |
6 | import phonenumbers | |
7 | import phonenumbers.geocoder | |
8 | import tornado.locale | |
9 | import tornado.options | |
10 | import tornado.web | |
11 | ||
12 | import ipfire | |
13 | import ipfire.countries | |
14 | ||
15 | from .handlers import * | |
16 | ||
17 | from . import auth | |
18 | from . import blog | |
19 | from . import boot | |
20 | from . import donate | |
21 | from . import download | |
22 | from . import fireinfo | |
23 | from . import iuse | |
24 | from . import location | |
25 | from . import mirrors | |
26 | from . import newsletter | |
27 | from . import nopaste | |
28 | from . import people | |
29 | from . import ui_modules | |
30 | from . import wiki | |
31 | ||
32 | class Application(tornado.web.Application): | |
33 | def __init__(self, config, **kwargs): | |
34 | # Initialize backend | |
35 | self.backend = ipfire.Backend(config) | |
36 | ||
37 | settings = { | |
38 | # Do not compress responses | |
39 | "gzip" : False, | |
40 | ||
41 | # Enable XSRF cookies | |
42 | "xsrf_cookies" : True, | |
43 | ||
44 | # Login | |
45 | "login_url" : "/login", | |
46 | ||
47 | # Setup directory structure | |
48 | "static_path" : self.backend.config.get("global", "static_dir"), | |
49 | "template_path" : self.backend.config.get("global", "templates_dir"), | |
50 | ||
51 | # UI Methods | |
52 | "ui_methods" : { | |
53 | "format_country_name" : self.format_country_name, | |
54 | "format_language_name" : self.format_language_name, | |
55 | "format_month_name" : self.format_month_name, | |
56 | "format_phone_number" : self.format_phone_number, | |
57 | "format_phone_number_to_e164" : self.format_phone_number_to_e164, | |
58 | "format_phone_number_location" : self.format_phone_number_location, | |
59 | "grouper" : grouper, | |
60 | }, | |
61 | ||
62 | # UI Modules | |
63 | "ui_modules" : { | |
64 | # Blog | |
65 | "BlogHistoryNavigation": blog.HistoryNavigationModule, | |
66 | "BlogList" : blog.ListModule, | |
67 | "BlogPost" : blog.PostModule, | |
68 | "BlogPosts" : blog.PostsModule, | |
69 | ||
70 | # Boot | |
71 | "BootMenuConfig" : boot.MenuConfigModule, | |
72 | "BootMenuHeader" : boot.MenuHeaderModule, | |
73 | "BootMenuSeparator" : boot.MenuSeparatorModule, | |
74 | ||
75 | # People | |
76 | "AccountsList" : people.AccountsListModule, | |
77 | "CDR" : people.CDRModule, | |
78 | "Channels" : people.ChannelsModule, | |
79 | "MOS" : people.MOSModule, | |
80 | "Password" : people.PasswordModule, | |
81 | "Registrations" : people.RegistrationsModule, | |
82 | "SIPStatus" : people.SIPStatusModule, | |
83 | ||
84 | # Nopaste | |
85 | "Code" : nopaste.CodeModule, | |
86 | ||
87 | # Fireinfo | |
88 | "FireinfoDeviceTable" : fireinfo.DeviceTableModule, | |
89 | "FireinfoDeviceAndGroupsTable" | |
90 | : fireinfo.DeviceAndGroupsTableModule, | |
91 | ||
92 | # Wiki | |
93 | "WikiDiff" : wiki.WikiDiffModule, | |
94 | "WikiNavbar" : wiki.WikiNavbarModule, | |
95 | "WikiList" : wiki.WikiListModule, | |
96 | ||
97 | # Misc | |
98 | "Map" : ui_modules.MapModule, | |
99 | "ProgressBar" : ui_modules.ProgressBarModule, | |
100 | }, | |
101 | ||
102 | # Call this when a page wasn't found | |
103 | "default_handler_class" : base.NotFoundHandler, | |
104 | } | |
105 | settings.update(kwargs) | |
106 | ||
107 | tornado.web.Application.__init__(self, **settings) | |
108 | ||
109 | authentication_handlers = [ | |
110 | (r"/login", auth.LoginHandler), | |
111 | (r"/logout", auth.LogoutHandler), | |
112 | ] | |
113 | ||
114 | self.add_handlers(r"(dev|www)\.ipfire\.org", [ | |
115 | # Entry site that lead the user to index | |
116 | (r"/", IndexHandler), | |
117 | ||
118 | # Download sites | |
119 | (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }), | |
120 | (r"/download", download.IndexHandler), | |
121 | (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler), | |
122 | ||
123 | # Donate | |
124 | (r"/donate", donate.DonateHandler), | |
125 | (r"/donate/thank-you", donate.ThankYouHandler), | |
126 | (r"/donate/error", donate.ErrorHandler), | |
127 | (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }), | |
128 | ||
129 | # Newsletter | |
130 | (r"/newsletter/subscribe", newsletter.SubscribeHandler), | |
131 | ||
132 | # RSS feed | |
133 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
134 | ||
135 | # Redirect news articles to blog | |
136 | (r"/news/(.*)", handlers.NewsHandler), | |
137 | ||
138 | # Static Pages | |
139 | (r"/chat", StaticHandler, { "template" : "chat.html" }), | |
140 | (r"/features", StaticHandler, { "template" : "features.html" }), | |
141 | (r"/legal", StaticHandler, { "template" : "legal.html" }), | |
142 | (r"/support", StaticHandler, { "template" : "support.html" }), | |
143 | ||
144 | # Handle old pages that have moved elsewhere | |
145 | (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }), | |
146 | (r"/(de|en)/(.*)", LangCompatHandler), | |
147 | ||
148 | # Export arbitrary error pages | |
149 | (r"/error/([45][0-9]{2})", base.ErrorHandler), | |
150 | ||
151 | # Block page | |
152 | (r"/blocked", base.BlockedHandler), | |
153 | ]) | |
154 | ||
155 | # blog.ipfire.org | |
156 | self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [ | |
157 | (r"/", blog.IndexHandler), | |
158 | (r"/authors/(\w+)", blog.AuthorHandler), | |
159 | (r"/compose", blog.ComposeHandler), | |
160 | (r"/drafts", blog.DraftsHandler), | |
161 | (r"/post/([0-9a-z\-\._]+)", blog.PostHandler), | |
162 | (r"/post/([0-9a-z\-\._]+)/delete", blog.DeleteHandler), | |
163 | (r"/post/([0-9a-z\-\._]+)/edit", blog.EditHandler), | |
164 | (r"/post/([0-9a-z\-\._]+)/publish", blog.PublishHandler), | |
165 | (r"/search", blog.SearchHandler), | |
166 | (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler), | |
167 | (r"/years/([0-9]+)", blog.YearHandler), | |
168 | ||
169 | # RSS Feed | |
170 | (r"/feed.xml", blog.FeedHandler), | |
171 | ] + authentication_handlers) | |
172 | ||
173 | # downloads.ipfire.org | |
174 | self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [ | |
175 | (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }), | |
176 | (r"/release/(.*)", download.ReleaseRedirectHandler), | |
177 | (r"/(.*)", download.FileHandler), | |
178 | ]) | |
179 | ||
180 | # mirrors.ipfire.org | |
181 | self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [ | |
182 | (r"/", mirrors.IndexHandler), | |
183 | (r"/mirrors/(.*)", mirrors.MirrorHandler), | |
184 | ]) | |
185 | ||
186 | # planet.ipfire.org | |
187 | self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [ | |
188 | (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }), | |
189 | (r"/post/([A-Za-z0-9_-]+)", handlers.PlanetPostHandler), | |
190 | (r"/user/([a-z0-9_-]+)", handlers.PlanetUserHandler), | |
191 | ||
192 | # RSS | |
193 | (r"/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
194 | (r"/user/([a-z0-9_-]+)/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
195 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
196 | ]) | |
197 | ||
198 | # fireinfo.ipfire.org | |
199 | self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [ | |
200 | (r"/", fireinfo.IndexHandler), | |
201 | ||
202 | # Vendors | |
203 | (r"/vendors", fireinfo.VendorsHandler), | |
204 | (r"/vendors/(pci|usb)/([0-9a-f]{4})", fireinfo.VendorHandler), | |
205 | ||
206 | # Driver | |
207 | (r"/drivers/(.*)", fireinfo.DriverDetail), | |
208 | ||
209 | # Show profiles | |
210 | (r"/profile/random", fireinfo.RandomProfileHandler), | |
211 | (r"/profile/([a-z0-9]{40})", fireinfo.ProfileHandler), | |
212 | ||
213 | # Stats | |
214 | (r"/processors", fireinfo.ProcessorsHandler), | |
215 | (r"/releases", fireinfo.ReleasesHandler), | |
216 | ||
217 | # Send profiles | |
218 | (r"/send/([a-z0-9]+)", fireinfo.ProfileSendHandler), | |
219 | ]) | |
220 | ||
221 | # i-use.ipfire.org | |
222 | self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [ | |
223 | (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }), | |
224 | (r"/profile/([a-f0-9]{40})/([0-9]+).png", iuse.ImageHandler), | |
225 | ]) | |
226 | ||
227 | # boot.ipfire.org | |
228 | BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot") | |
229 | self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [ | |
230 | (r"/", tornado.web.RedirectHandler, { "url" : "https://wiki.ipfire.org/installation/pxe" }), | |
231 | ||
232 | # Configurations | |
233 | (r"/premenu.cfg", boot.PremenuCfgHandler), | |
234 | (r"/menu.gpxe", boot.MenuGPXEHandler), | |
235 | (r"/menu.cfg", boot.MenuCfgHandler), | |
236 | ||
237 | # Static files | |
238 | (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)", | |
239 | tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }), | |
240 | ]) | |
241 | ||
242 | # nopaste.ipfire.org | |
243 | self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [ | |
244 | (r"/", nopaste.CreateHandler), | |
245 | (r"/raw/(.*)", nopaste.RawHandler), | |
246 | (r"/view/(.*)", nopaste.ViewHandler), | |
247 | ] + authentication_handlers) | |
248 | ||
249 | # location.ipfire.org | |
250 | self.add_handlers(r"location(\.dev)?\.ipfire\.org", [ | |
251 | (r"/", location.IndexHandler), | |
252 | (r"/lookup/(.+)/blacklists", location.BlacklistsHandler), | |
253 | (r"/lookup/(.+)", location.LookupHandler), | |
254 | ]) | |
255 | ||
256 | # geoip.ipfire.org | |
257 | self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [ | |
258 | (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }), | |
259 | ]) | |
260 | ||
261 | # talk.ipfire.org | |
262 | self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [ | |
263 | (r"/", tornado.web.RedirectHandler, { "url" : "https://people.ipfire.org/" }), | |
264 | ]) | |
265 | ||
266 | # people.ipfire.org | |
267 | self.add_handlers(r"people(\.dev)?\.ipfire\.org", [ | |
268 | (r"/", people.IndexHandler), | |
269 | (r"/activate/(\w+)/(\w+)", auth.ActivateHandler), | |
270 | (r"/conferences", people.ConferencesHandler), | |
271 | (r"/register", auth.RegisterHandler), | |
272 | (r"/search", people.SearchHandler), | |
273 | (r"/users", people.UsersHandler), | |
274 | (r"/users/(\w+)", people.UserHandler), | |
275 | (r"/users/(\w+)\.jpg", people.AvatarHandler), | |
276 | (r"/users/(\w+)/calls/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})", people.CallHandler), | |
277 | (r"/users/(\w+)/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler), | |
278 | (r"/users/(\w+)/edit", people.UserEditHandler), | |
279 | (r"/users/(\w+)/passwd", people.UserPasswdHandler), | |
280 | (r"/users/(\w+)/ssh-keys", people.SSHKeysIndexHandler), | |
281 | (r"/users/(\w+)/ssh-keys/(SHA256\:.*)/delete", people.SSHKeysDeleteHandler), | |
282 | (r"/users/(\w+)/ssh-keys/(SHA256\:.*)", people.SSHKeysDownloadHandler), | |
283 | (r"/users/(\w+)/ssh-keys/upload", people.SSHKeysUploadHandler), | |
284 | (r"/users/(\w+)/sip", people.SIPHandler), | |
285 | ] + authentication_handlers) | |
286 | ||
287 | # wiki.ipfire.org | |
288 | self.add_handlers(r"wiki(\.dev)?\.ipfire\.org", | |
289 | authentication_handlers + [ | |
290 | ||
291 | # Actions | |
292 | (r"([A-Za-z0-9\-_\/]+)?/_edit", wiki.ActionEditHandler), | |
293 | (r"([A-Za-z0-9\-_\/]+)?/_render", wiki.ActionRenderHandler), | |
294 | (r"([A-Za-z0-9\-_\/]+)?/_(watch|unwatch)", wiki.ActionWatchHandler), | |
295 | (r"/actions/upload", wiki.ActionUploadHandler), | |
296 | ||
297 | # Handlers | |
298 | (r"/recent\-changes", wiki.RecentChangesHandler), | |
299 | (r"/search", wiki.SearchHandler), | |
300 | (r"/watchlist", wiki.WatchlistHandler), | |
301 | ||
302 | # Media | |
303 | (r"([A-Za-z0-9\-_\/]+)?/_files", wiki.FilesHandler), | |
304 | (r"((?!/static)(?:[A-Za-z0-9\-_\/]+)?(?:.*)\.(?:\w+))$", wiki.FileHandler), | |
305 | ||
306 | # Render pages | |
307 | (r"([A-Za-z0-9\-_\/]+)?", wiki.PageHandler), | |
308 | ]) | |
309 | ||
310 | # ipfire.org | |
311 | self.add_handlers(r"ipfire\.org", [ | |
312 | (r".*", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org" }) | |
313 | ]) | |
314 | ||
315 | logging.info("Successfully initialied application") | |
316 | ||
317 | def format_country_name(self, handler, country_code): | |
318 | return ipfire.countries.get_name(country_code) | |
319 | ||
320 | def format_language_name(self, handler, language): | |
321 | _ = handler.locale.translate | |
322 | ||
323 | if language == "de": | |
324 | return _("German") | |
325 | elif language == "en": | |
326 | return _("English") | |
327 | elif language == "es": | |
328 | return _("Spanish") | |
329 | elif language == "fr": | |
330 | return _("French") | |
331 | elif language == "it": | |
332 | return _("Italian") | |
333 | elif language == "nl": | |
334 | return _("Dutch") | |
335 | elif language == "pl": | |
336 | return _("Polish") | |
337 | elif language == "pt": | |
338 | return _("Portuguese") | |
339 | elif language == "ru": | |
340 | return _("Russian") | |
341 | elif language == "tr": | |
342 | return _("Turkish") | |
343 | ||
344 | return language | |
345 | ||
346 | def format_month_name(self, handler, month): | |
347 | _ = handler.locale.translate | |
348 | ||
349 | if month == 1: | |
350 | return _("January") | |
351 | elif month == 2: | |
352 | return _("February") | |
353 | elif month == 3: | |
354 | return _("March") | |
355 | elif month == 4: | |
356 | return _("April") | |
357 | elif month == 5: | |
358 | return _("May") | |
359 | elif month == 6: | |
360 | return _("June") | |
361 | elif month == 7: | |
362 | return _("July") | |
363 | elif month == 8: | |
364 | return _("August") | |
365 | elif month == 9: | |
366 | return _("September") | |
367 | elif month == 10: | |
368 | return _("October") | |
369 | elif month == 11: | |
370 | return _("November") | |
371 | elif month == 12: | |
372 | return _("December") | |
373 | ||
374 | return month | |
375 | ||
376 | def format_phone_number(self, handler, number): | |
377 | if not isinstance(number, phonenumbers.PhoneNumber): | |
378 | try: | |
379 | number = phonenumbers.parse(number, None) | |
380 | except phonenumbers.phonenumberutil.NumberParseException: | |
381 | return number | |
382 | ||
383 | return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL) | |
384 | ||
385 | def format_phone_number_to_e164(self, handler, number): | |
386 | return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164) | |
387 | ||
388 | def format_phone_number_location(self, handler, number): | |
389 | s = [ | |
390 | phonenumbers.geocoder.description_for_number(number, handler.locale.code), | |
391 | phonenumbers.region_code_for_number(number), | |
392 | ] | |
393 | ||
394 | return ", ".join((e for e in s if e)) | |
395 | ||
396 | ||
397 | def grouper(handler, iterator, n): | |
398 | """ | |
399 | Returns groups of n from the iterator | |
400 | """ | |
401 | i = iter(iterator) | |
402 | ||
403 | while True: | |
404 | ret = list(itertools.islice(i, 0, n)) | |
405 | if not ret: | |
406 | break | |
407 | ||
408 | yield ret |