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