OSDN Git Service

Reduce traceback.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / BbsType / bbs_type_other.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (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 St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import re
19 import os.path
20 import traceback
21
22 from bbs_type_2ch import BaseType
23 from bbs_type_exception import BbsTypeError
24
25 _whitelist = []
26
27 whitelist_path = os.path.expanduser("~/.fukui-no-namari/whitelist")
28
29 def whitelist_load():
30     try:
31         for line in file(whitelist_path):
32             _whitelist.append(line.rstrip())
33     except IOError, e:
34         if e.errno != 2:
35             traceback.print_exc()
36
37 whitelist_load()
38
39 _board_reg_expr = re.compile("http://(?P<host>.+)/(?P<board>[^/]+)(?:/[^/]*){0,1}$")
40
41
42 class TypeOther(BaseType):
43     bbs_type = "other"
44     _board_reg = _board_reg_expr
45
46     def _parse_uri(self, uri):
47
48         def uri_base_match(uri, iru):
49             iru = iru.rstrip("/")
50             if uri.startswith(iru):
51                 uri = uri[len(iru):]
52                 if not uri:
53                     return True
54                 elif uri[0] == "/":
55                     uri = uri[1:]
56                     if not uri:
57                         return True
58                     elif uri.find("/") == -1:
59                         return True
60             return False
61
62         for iru in _whitelist:
63             match = self._board_reg.match(iru)
64             if match:
65                 host = match.group("host")
66                 board = match.group("board")
67                 if uri_base_match(uri, "http://%s/%s" % (host, board)):
68                     self.host = host
69                     self.board = board
70                     return
71                 else:
72                     cgi_base = "http://%s/test/read.cgi/%s/" % (host, board)
73                     if uri.startswith(cgi_base):
74                         match = re.match(
75                             "(?P<thread>[^/]+)/", uri[len(cgi_base):])
76                         if match:
77                             self.host = host
78                             self.board = board
79                             self.thread = match.group("thread")
80                             return
81             else:
82                 print "%s in whitelist is not supported" % iru
83
84         raise BbsTypeError, "the uri %s is not supported" % uri