]>
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 | "WikiNavbar" : wiki.WikiNavbarModule, | |
94 | "WikiList" : wiki.WikiListModule, | |
95 | ||
96 | # Misc | |
97 | "Map" : ui_modules.MapModule, | |
98 | "ProgressBar" : ui_modules.ProgressBarModule, | |
99 | }, | |
100 | ||
101 | # Call this when a page wasn't found | |
102 | "default_handler_class" : base.NotFoundHandler, | |
103 | } | |
104 | settings.update(kwargs) | |
105 | ||
106 | tornado.web.Application.__init__(self, **settings) | |
107 | ||
108 | authentication_handlers = [ | |
109 | (r"/login", auth.LoginHandler), | |
110 | (r"/logout", auth.LogoutHandler), | |
111 | ] | |
112 | ||
113 | self.add_handlers(r"(dev|www)\.ipfire\.org", [ | |
114 | # Entry site that lead the user to index | |
115 | (r"/", IndexHandler), | |
116 | ||
117 | # Download sites | |
118 | (r"/downloads", tornado.web.RedirectHandler, { "url" : "/download" }), | |
119 | (r"/download", download.IndexHandler), | |
120 | (r"/download/([0-9a-z\-\.]+)", download.ReleaseHandler), | |
121 | ||
122 | # Donate | |
123 | (r"/donate", donate.DonateHandler), | |
124 | (r"/donate/thank-you", donate.ThankYouHandler), | |
125 | (r"/donate/error", donate.ErrorHandler), | |
126 | (r"/donation", tornado.web.RedirectHandler, { "url" : "/donate" }), | |
127 | ||
128 | # Newsletter | |
129 | (r"/newsletter/subscribe", newsletter.SubscribeHandler), | |
130 | ||
131 | # RSS feed | |
132 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
133 | ||
134 | # Redirect news articles to blog | |
135 | (r"/news/(.*)", handlers.NewsHandler), | |
136 | ||
137 | # Static Pages | |
138 | (r"/chat", StaticHandler, { "template" : "chat.html" }), | |
139 | (r"/features", StaticHandler, { "template" : "features.html" }), | |
140 | (r"/legal", StaticHandler, { "template" : "legal.html" }), | |
141 | (r"/support", StaticHandler, { "template" : "support.html" }), | |
142 | ||
143 | # Handle old pages that have moved elsewhere | |
144 | (r"/imprint", tornado.web.RedirectHandler, { "url" : "/legal" }), | |
145 | (r"/(de|en)/(.*)", LangCompatHandler), | |
146 | ||
147 | # Export arbitrary error pages | |
148 | (r"/error/([45][0-9]{2})", base.ErrorHandler), | |
149 | ||
150 | # Block page | |
151 | (r"/blocked", base.BlockedHandler), | |
152 | ]) | |
153 | ||
154 | # blog.ipfire.org | |
155 | self.add_handlers(r"blog(\.dev)?\.ipfire\.org", [ | |
156 | (r"/", blog.IndexHandler), | |
157 | (r"/authors/(\w+)", blog.AuthorHandler), | |
158 | (r"/compose", blog.ComposeHandler), | |
159 | (r"/drafts", blog.DraftsHandler), | |
160 | (r"/post/([0-9a-z\-\._]+)", blog.PostHandler), | |
161 | (r"/post/([0-9a-z\-\._]+)/delete", blog.DeleteHandler), | |
162 | (r"/post/([0-9a-z\-\._]+)/edit", blog.EditHandler), | |
163 | (r"/post/([0-9a-z\-\._]+)/publish", blog.PublishHandler), | |
164 | (r"/search", blog.SearchHandler), | |
165 | (r"/tags/([0-9a-z\-\.]+)", blog.TagHandler), | |
166 | (r"/years/([0-9]+)", blog.YearHandler), | |
167 | ||
168 | # RSS Feed | |
169 | (r"/feed.xml", blog.FeedHandler), | |
170 | ] + authentication_handlers) | |
171 | ||
172 | # downloads.ipfire.org | |
173 | self.add_handlers(r"downloads?(\.dev)?\.ipfire\.org", [ | |
174 | (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }), | |
175 | (r"/release/(.*)", download.ReleaseRedirectHandler), | |
176 | (r"/(.*)", download.FileHandler), | |
177 | ]) | |
178 | ||
179 | # mirrors.ipfire.org | |
180 | self.add_handlers(r"mirrors(\.dev)?\.ipfire\.org", [ | |
181 | (r"/", mirrors.IndexHandler), | |
182 | (r"/mirrors/(.*)", mirrors.MirrorHandler), | |
183 | ]) | |
184 | ||
185 | # planet.ipfire.org | |
186 | self.add_handlers(r"planet(\.dev)?\.ipfire\.org", [ | |
187 | (r"/", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/" }), | |
188 | (r"/post/([A-Za-z0-9_-]+)", handlers.PlanetPostHandler), | |
189 | (r"/user/([a-z0-9_-]+)", handlers.PlanetUserHandler), | |
190 | ||
191 | # RSS | |
192 | (r"/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
193 | (r"/user/([a-z0-9_-]+)/rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
194 | (r"/news.rss", tornado.web.RedirectHandler, { "url" : "https://blog.ipfire.org/feed.xml" }), | |
195 | ]) | |
196 | ||
197 | # fireinfo.ipfire.org | |
198 | self.add_handlers(r"fireinfo(\.dev)?\.ipfire\.org", [ | |
199 | (r"/", fireinfo.IndexHandler), | |
200 | ||
201 | # Vendors | |
202 | (r"/vendors", fireinfo.VendorsHandler), | |
203 | (r"/vendors/(pci|usb)/([0-9a-f]{4})", fireinfo.VendorHandler), | |
204 | ||
205 | # Driver | |
206 | (r"/drivers/(.*)", fireinfo.DriverDetail), | |
207 | ||
208 | # Show profiles | |
209 | (r"/profile/random", fireinfo.RandomProfileHandler), | |
210 | (r"/profile/([a-z0-9]{40})", fireinfo.ProfileHandler), | |
211 | ||
212 | # Stats | |
213 | (r"/processors", fireinfo.ProcessorsHandler), | |
214 | (r"/releases", fireinfo.ReleasesHandler), | |
215 | ||
216 | # Send profiles | |
217 | (r"/send/([a-z0-9]+)", fireinfo.ProfileSendHandler), | |
218 | ]) | |
219 | ||
220 | # i-use.ipfire.org | |
221 | self.add_handlers(r"i-use(\.dev)?\.ipfire\.org", [ | |
222 | (r"/", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org/" }), | |
223 | (r"/profile/([a-f0-9]{40})/([0-9]+).png", iuse.ImageHandler), | |
224 | ]) | |
225 | ||
226 | # boot.ipfire.org | |
227 | BOOT_STATIC_PATH = os.path.join(self.settings["static_path"], "netboot") | |
228 | self.add_handlers(r"boot(\.dev)?\.ipfire\.org", [ | |
229 | (r"/", tornado.web.RedirectHandler, { "url" : "https://wiki.ipfire.org/installation/pxe" }), | |
230 | ||
231 | # Configurations | |
232 | (r"/premenu.cfg", boot.PremenuCfgHandler), | |
233 | (r"/menu.gpxe", boot.MenuGPXEHandler), | |
234 | (r"/menu.cfg", boot.MenuCfgHandler), | |
235 | ||
236 | # Static files | |
237 | (r"/(boot\.png|pxelinux\.0|menu\.c32|vesamenu\.c32)", | |
238 | tornado.web.StaticFileHandler, { "path" : BOOT_STATIC_PATH }), | |
239 | ]) | |
240 | ||
241 | # nopaste.ipfire.org | |
242 | self.add_handlers(r"nopaste(\.dev)?\.ipfire\.org", [ | |
243 | (r"/", nopaste.CreateHandler), | |
244 | (r"/raw/(.*)", nopaste.RawHandler), | |
245 | (r"/view/(.*)", nopaste.ViewHandler), | |
246 | ] + authentication_handlers) | |
247 | ||
248 | # location.ipfire.org | |
249 | self.add_handlers(r"location(\.dev)?\.ipfire\.org", [ | |
250 | (r"/", location.IndexHandler), | |
251 | (r"/lookup/(.+)/blacklists", location.BlacklistsHandler), | |
252 | (r"/lookup/(.+)", location.LookupHandler), | |
253 | ]) | |
254 | ||
255 | # geoip.ipfire.org | |
256 | self.add_handlers(r"geoip(\.dev)?\.ipfire\.org", [ | |
257 | (r"/", tornado.web.RedirectHandler, { "url" : "https://location.ipfire.org/" }), | |
258 | ]) | |
259 | ||
260 | # talk.ipfire.org | |
261 | self.add_handlers(r"talk(\.dev)?\.ipfire\.org", [ | |
262 | (r"/", tornado.web.RedirectHandler, { "url" : "https://people.ipfire.org/" }), | |
263 | ]) | |
264 | ||
265 | # people.ipfire.org | |
266 | self.add_handlers(r"people(\.dev)?\.ipfire\.org", [ | |
267 | (r"/", people.IndexHandler), | |
268 | (r"/conferences", people.ConferencesHandler), | |
269 | (r"/search", people.SearchHandler), | |
270 | (r"/users", people.UsersHandler), | |
271 | (r"/users/(\w+)", people.UserHandler), | |
272 | (r"/users/(\w+)\.jpg", people.AvatarHandler), | |
273 | (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), | |
274 | (r"/users/(\w+)/calls(?:/(\d{4}-\d{2}-\d{2}))?", people.CallsHandler), | |
275 | (r"/users/(\w+)/edit", people.UserEditHandler), | |
276 | (r"/users/(\w+)/passwd", people.UserPasswdHandler), | |
277 | (r"/users/(\w+)/ssh-keys", people.SSHKeysIndexHandler), | |
278 | (r"/users/(\w+)/ssh-keys/(SHA256\:.*)/delete", people.SSHKeysDeleteHandler), | |
279 | (r"/users/(\w+)/ssh-keys/(SHA256\:.*)", people.SSHKeysDownloadHandler), | |
280 | (r"/users/(\w+)/ssh-keys/upload", people.SSHKeysUploadHandler), | |
281 | (r"/users/(\w+)/sip", people.SIPHandler), | |
282 | ] + authentication_handlers) | |
283 | ||
284 | # wiki.ipfire.org | |
285 | self.add_handlers(r"wiki(\.dev)?\.ipfire\.org", | |
286 | authentication_handlers + [ | |
287 | ||
288 | # Handlers | |
289 | (r"/recent\-changes", wiki.RecentChangesHandler), | |
290 | (r"/search", wiki.SearchHandler), | |
291 | ||
292 | # Render pages | |
293 | (r"([A-Za-z0-9\-_\/]+)?", wiki.PageHandler), | |
294 | ]) | |
295 | ||
296 | # ipfire.org | |
297 | self.add_handlers(r"ipfire\.org", [ | |
298 | (r".*", tornado.web.RedirectHandler, { "url" : "https://www.ipfire.org" }) | |
299 | ]) | |
300 | ||
301 | logging.info("Successfully initialied application") | |
302 | ||
303 | def format_country_name(self, handler, country_code): | |
304 | return ipfire.countries.get_name(country_code) | |
305 | ||
306 | def format_language_name(self, handler, language): | |
307 | _ = handler.locale.translate | |
308 | ||
309 | if language == "de": | |
310 | return _("German") | |
311 | elif language == "en": | |
312 | return _("English") | |
313 | elif language == "es": | |
314 | return _("Spanish") | |
315 | elif language == "fr": | |
316 | return _("French") | |
317 | elif language == "it": | |
318 | return _("Italian") | |
319 | elif language == "nl": | |
320 | return _("Dutch") | |
321 | elif language == "pl": | |
322 | return _("Polish") | |
323 | elif language == "pt": | |
324 | return _("Portuguese") | |
325 | elif language == "ru": | |
326 | return _("Russian") | |
327 | elif language == "tr": | |
328 | return _("Turkish") | |
329 | ||
330 | return language | |
331 | ||
332 | def format_month_name(self, handler, month): | |
333 | _ = handler.locale.translate | |
334 | ||
335 | if month == 1: | |
336 | return _("January") | |
337 | elif month == 2: | |
338 | return _("February") | |
339 | elif month == 3: | |
340 | return _("March") | |
341 | elif month == 4: | |
342 | return _("April") | |
343 | elif month == 5: | |
344 | return _("May") | |
345 | elif month == 6: | |
346 | return _("June") | |
347 | elif month == 7: | |
348 | return _("July") | |
349 | elif month == 8: | |
350 | return _("August") | |
351 | elif month == 9: | |
352 | return _("September") | |
353 | elif month == 10: | |
354 | return _("October") | |
355 | elif month == 11: | |
356 | return _("November") | |
357 | elif month == 12: | |
358 | return _("December") | |
359 | ||
360 | return month | |
361 | ||
362 | def format_phone_number(self, handler, number): | |
363 | if not isinstance(number, phonenumbers.PhoneNumber): | |
364 | try: | |
365 | number = phonenumbers.parse(number, None) | |
366 | except phonenumbers.phonenumberutil.NumberParseException: | |
367 | return number | |
368 | ||
369 | return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL) | |
370 | ||
371 | def format_phone_number_to_e164(self, handler, number): | |
372 | return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.E164) | |
373 | ||
374 | def format_phone_number_location(self, handler, number): | |
375 | s = [ | |
376 | phonenumbers.geocoder.description_for_number(number, handler.locale.code), | |
377 | phonenumbers.region_code_for_number(number), | |
378 | ] | |
379 | ||
380 | return ", ".join((e for e in s if e)) | |
381 | ||
382 | ||
383 | def grouper(handler, iterator, n): | |
384 | """ | |
385 | Returns groups of n from the iterator | |
386 | """ | |
387 | i = iter(iterator) | |
388 | ||
389 | while True: | |
390 | ret = list(itertools.islice(i, 0, n)) | |
391 | if not ret: | |
392 | break | |
393 | ||
394 | yield ret |