Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 289 lines (249 sloc) 8.895 kB
287490d @shypike Split misc.py into misc.py, panic.py and powersup.py.
shypike authored
1 #!/usr/bin/python -OO
2b6adc9 @shypike Update copyright dates.
shypike authored
2 # Copyright 2008-2012 The SABnzbd-Team <team@sabnzbd.org>
287490d @shypike Split misc.py into misc.py, panic.py and powersup.py.
shypike authored
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 """
19 sabnzbd.panic - Send panic message to the browser
20 """
21
22 import os
23 import logging
24 import webbrowser
25 import tempfile
26
27 import sabnzbd
28 import sabnzbd.cfg as cfg
29
30 PANIC_NONE = 0
31 PANIC_PORT = 1
32 PANIC_TEMPL = 2
33 PANIC_QUEUE = 3
34 PANIC_FWALL = 4
35 PANIC_OTHER = 5
36 PANIC_XPORT = 6
37 PANIC_SQLITE = 7
38 PANIC_HOST = 8
39
40 def MSG_BAD_NEWS():
41 return r'''
42 <html>
43 <head>
44 <title>''' + Ta('Problem with') + ''' %s %s</title>
45 </head>
46 <body>
47 <h1><font color="#0000FF"> %s %s</font></h1>
48 <p align="center">&nbsp;</p>
49 <p align="center"><font size="5">
50 <blockquote>
51 %s
52 </blockquote>
53 <br>%s<br>
54 </body>
55 </html>
56 '''
57
58 def MSG_BAD_FWALL():
59 return Ta(r'''
60 SABnzbd is not compatible with some software firewalls.<br>
61 %s<br>
62 Sorry, but we cannot solve this incompatibility right now.<br>
63 Please file a complaint at your firewall supplier.<br>
64 <br>
65 ''')
66
67 def MSG_BAD_PORT():
68 return Ta(r'''
69 SABnzbd needs a free tcp/ip port for its internal web server.<br>
70 Port %s on %s was tried , but it is not available.<br>
71 Some other software uses the port or SABnzbd is already running.<br>
72 <br>
73 Please restart SABnzbd with a different port number.''') + \
74 '''<br>
75 <br>
76 %s<br>
77 &nbsp;&nbsp;&nbsp;&nbsp;%s --server %s:%s<br>
78 <br>''' + \
79 Ta(r'If you get this error message again, please try a different number.<br>')
80
81 def MSG_ILL_PORT():
82 return Ta(r'''
83 SABnzbd needs a free tcp/ip port for its internal web server.<br>
84 Port %s on %s was tried , but the account used for SABnzbd has no permission to use it.<br>
85 On OSX and Linux systems, normal users must use ports above 1023.<br>
86 <br>
87 Please restart SABnzbd with a different port number.''') + \
88 '''<br>
89 <br>
90 %s<br>
91 &nbsp;&nbsp;&nbsp;&nbsp;%s --server %s:%s<br>
92 <br>''' + \
93 Ta(r'If you get this error message again, please try a different number.<br>')
94
95 def MSG_BAD_HOST():
96 return Ta(r'''
97 SABnzbd needs a valid host address for its internal web server.<br>
98 You have specified an invalid address.<br>
99 Safe values are <b>localhost</b> and <b>0.0.0.0</b><br>
100 <br>
101 Please restart SABnzbd with a proper host address.''') + \
102 '''<br>
103 <br>
104 %s<br>
105 &nbsp;&nbsp;&nbsp;&nbsp;%s --server %s:%s<br>
106 <br>
107 '''
108
109 def MSG_BAD_QUEUE():
110 return Ta(r'''
111 SABnzbd detected saved data from an other SABnzbd version<br>
112 but cannot re-use the data of the other program.<br><br>
113 You may want to finish your queue first with the other program.<br><br>
114 After that, start this program with the "--clean" option.<br>
115 This will erase the current queue and history!<br>
116 SABnzbd read the file "%s".''') + \
117 '''<br>
118 <br>
119 %s<br>
120 &nbsp;&nbsp;&nbsp;&nbsp;%s --clean<br>
121 <br>
122 '''
123
124 def MSG_BAD_TEMPL():
125 return Ta(r'''
126 SABnzbd cannot find its web interface files in %s.<br>
127 Please install the program again.<br>
128 <br>
129 ''')
130
131 def MSG_OTHER():
132 return Ta('SABnzbd detected a fatal error:') + '<br>%s<br><br>%s<br>'
133
134 def MSG_OLD_QUEUE():
135 return Ta(r'''
136 SABnzbd detected a Queue and History from an older (0.4.x) release.<br><br>
137 Both queue and history will be ignored and may get lost!<br><br>
138 You may choose to stop SABnzbd and finish the queue with the older program.<br><br>
139 Click OK to proceed to SABnzbd''') + \
140 ('''<br><br><FORM><input type="button" onclick="this.form.action='/.'; this.form.submit(); return false;" value="%s"/></FORM>''' % Ta('OK'))
141
142 def MSG_SQLITE():
143 return Ta(r'''
144 SABnzbd detected that the file sqlite3.dll is missing.<br><br>
145 Some poorly designed virus-scanners remove this file.<br>
146 Please check your virus-scanner, try to re-install SABnzbd and complain to your virus-scanner vendor.<br>
147 <br>
148 ''')
149
150 def panic_message(panic, a=None, b=None):
151 """Create the panic message from templates
152 """
153 if sabnzbd.WIN32:
154 os_str = Ta('Press Startkey+R and type the line (example):')
155 prog_path = '"%s"' % sabnzbd.MY_FULLNAME
156 else:
157 os_str = Ta('Open a Terminal window and type the line (example):')
158 prog_path = sabnzbd.MY_FULLNAME
159
160 if panic == PANIC_PORT:
161 newport = int(b) + 1
162 newport = "%s" % newport
163 msg = MSG_BAD_PORT() % (b, a, os_str, prog_path, a, newport)
164 elif panic == PANIC_XPORT:
165 if int(b) < 1023:
166 newport = 1024
167 else:
168 newport = int(b) + 1
169 newport = "%s" % newport
170 msg = MSG_ILL_PORT() % (b, a, os_str, prog_path, a, newport)
171 elif panic == PANIC_TEMPL:
172 msg = MSG_BAD_TEMPL() % a
173 elif panic == PANIC_QUEUE:
174 msg = MSG_BAD_QUEUE() % (a, os_str, prog_path)
175 elif panic == PANIC_FWALL:
176 if a:
177 msg = MSG_BAD_FWALL() % Ta('It is likely that you are using ZoneAlarm on Vista.<br>')
178 else:
179 msg = MSG_BAD_FWALL() % "<br>"
180 elif panic == PANIC_SQLITE:
181 msg = MSG_SQLITE()
182 elif panic == PANIC_HOST:
183 msg = MSG_BAD_HOST() % (os_str, prog_path, 'localhost', b)
184 else:
185 msg = MSG_OTHER() % (a, b)
186
187 msg = MSG_BAD_NEWS() % (sabnzbd.MY_NAME, sabnzbd.__version__, sabnzbd.MY_NAME, sabnzbd.__version__,
188 msg, Ta('Program did not start!'))
189
190 if sabnzbd.WIN_SERVICE:
191 sabnzbd.WIN_SERVICE.ErrLogger('Panic exit', msg)
192
193 if (not cfg.autobrowser()) or sabnzbd.DAEMON:
194 return
195
196 msgfile, url = tempfile.mkstemp(suffix='.html')
197 os.write(msgfile, msg)
198 os.close(msgfile)
199 return url
200
201
202 def panic_fwall(vista):
203 launch_a_browser(panic_message(PANIC_FWALL, vista))
204
205 def panic_port(host, port):
206 launch_a_browser(panic_message(PANIC_PORT, host, port))
207
208 def panic_host(host, port):
209 launch_a_browser(panic_message(PANIC_HOST, host, port))
210
211 def panic_xport(host, port):
212 launch_a_browser(panic_message(PANIC_XPORT, host, port))
213 logging.error(Ta('You have no permisson to use port %s'), port)
214
215 def panic_queue(name):
216 launch_a_browser(panic_message(PANIC_QUEUE, name, 0))
217
218 def panic_tmpl(name):
219 launch_a_browser(panic_message(PANIC_TEMPL, name, 0))
220
221 def panic_sqlite(name):
222 launch_a_browser(panic_message(PANIC_SQLITE, name, 0))
223
224 def panic_old_queue():
225 msg = MSG_OLD_QUEUE()
226 return MSG_BAD_NEWS() % (sabnzbd.MY_NAME, sabnzbd.__version__, sabnzbd.MY_NAME, sabnzbd.__version__, msg, '')
227
228 def panic(reason, remedy=""):
229 print "\n%s:\n %s\n%s" % (Ta('Fatal error'), reason, remedy)
230 launch_a_browser(panic_message(PANIC_OTHER, reason, remedy))
231
232
233 def launch_a_browser(url, force=False):
234 """Launch a browser pointing to the URL
235 """
236 if not force and not cfg.autobrowser() or sabnzbd.DAEMON:
237 return
238
cfe3b58 @shypike On Windows, the Python runtime starts the wrong browser when using ht…
shypike authored
239 if '::1' in url and not '[::1]' in url:
240 # Get around ideosyncrasy in Python runtime
241 url = url.replace('::1', '[::1]')
242
287490d @shypike Split misc.py into misc.py, panic.py and powersup.py.
shypike authored
243 if cfg.enable_https() and not cfg.https_port.get_int():
244 # Must use https, because http is not available
245 url = url.replace('http:', 'https:')
246
247 logging.info("Lauching browser with %s", url)
248 try:
5863ec6 @shypike Protect launch_a_browser() against None parameter.
shypike authored
249 if url and not url.startswith('http'):
9f534f7 @shypike Prevent hangup of OSX binary after Panic message.
shypike authored
250 url = 'file:///%s' % url
287490d @shypike Split misc.py into misc.py, panic.py and powersup.py.
shypike authored
251 webbrowser.open(url, 2, 1)
252 except:
253 logging.warning(Ta('Cannot launch the browser, probably not found'))
254 logging.info("Traceback: ", exc_info = True)
255
256
257 def error_page_401(status, message, traceback, version):
258 """ Custom handler for 401 error """
259 title = T('Access denied')
260 body = T('Error %s: You need to provide a valid username and password.') % status
261 return r'''
262 <html>
263 <head>
264 <title>%s</title>
265 </head>
266 <body>
267 <br/><br/>
268 <font color="#0000FF">%s</font>
269 </body>
270 </html>
271 ''' % (title, body)
c5d675a @shypike Webserver 404 (NotFound) errors will automatically redirect to the ma…
shypike authored
272
273
274 def error_page_404(status, message, traceback, version):
275 """ Custom handler for 404 error, redirect to main page """
276 import cherrypy
277 return r'''
278 <html>
279 <head>
280 <script type="text/javascript">
281 <!--
282 location.href = "%s"
283 //-->
284 </script>
285 </head>
286 <body><br/></body>
287 </html>
288 ''' % cherrypy.wsgiserver.REDIRECT_URL
Something went wrong with that request. Please try again.