OSDN Git Service

0580cf024ea4fee87c9491d08e72174ee0cf393e
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / BbsType / bbs_type_base.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 copy
20 import time
21 import codecs
22
23 from bbs_type_exception import BbsTypeError
24
25 subject_reg_expr = re.compile("(?P<id>.*).dat<>(?P<title>.*)\((?P<res>\d*)\)")
26 dat_reg_expr = re.compile("(?P<name>.*)<>(?P<mail>.*)<>(?P<date>.*)<>(?P<msg>.*)<>(?P<title>.*)")
27
28
29 class BaseType:
30     """Base of bbs type. Do not instantiate directly"""
31
32     bbs_type = "other"
33     _base_reg = None
34     _cgi_reg = None
35     subject_reg = subject_reg_expr
36     dat_reg = dat_reg_expr
37     encoding = "cp932"
38
39     def __init__(self, uri):
40         self.uri = uri
41         self.host = None
42         self.board = None
43         self.thread = None
44
45         self._parse_uri(uri)
46
47     def _parse_uri(self, uri):
48
49         m = self._base_reg.match(self.uri)
50         if m:
51             self.host = m.group("host")
52             self.board = m.group("board")
53         else:
54             m = self._cgi_reg.match(self.uri)
55             if m:
56                 self.host = m.group("host")
57                 self.board = m.group("board")
58                 self.thread = m.group("thread")
59             else:
60                 raise BbsTypeError, \
61                       "the uri %s does not represent %s" \
62                       % (self.uri, self.bbs_type)
63
64     def is_board(self):
65         return not self.thread
66
67     def is_thread(self):
68         return self.thread
69
70     def is_same_board(self, another):
71         return self.bbs_type == another.bbs_type \
72                and self.host == another.host \
73                and self.board == another.board
74
75     def clone_with_thread(self, thread):
76         if not thread:
77             raise ValueError, "parameter must not be empty"
78         another = copy.copy(self)
79         another.thread = thread
80         another.uri = another.get_thread_uri()
81         return another
82
83     def get_uri_base(self):
84         return "http://" + self.host + "/" + self.board + "/"
85
86     def get_subject_txt_uri(self):
87         return self.get_uri_base() + "subject.txt"
88
89     def get_dat_uri(self):
90         if not self.thread:
91             raise BbsTypeError, "not specified thread"
92         return self.get_uri_base() + "dat/" + self.thread + ".dat"
93
94     def get_thread_uri(self):
95         if not self.thread:
96             raise BbsTypeError, "not specified thread"
97         return "http://" + self.host + "/test/read.cgi/" + \
98                   self.board + "/" + self.thread + "/"
99
100     def get_post_uri(self):
101         return "http://" + self.host + "/test/bbs.cgi"
102
103     def build_post_dict(self, name, mail, msg):
104         post_dict = {}
105         post_dict["bbs"] = self.board
106         post_dict["key"] = self.thread
107         post_dict["time"] = str(int(time.time()))
108         post_dict["submit"] = u"\u66f8\u304d\u8fbc\u3080".encode(
109             "cp932", "replace")
110         post_dict["FROM"] = name.encode("cp932", "replace")
111         post_dict["mail"] = mail.encode("cp932", "replace")
112         post_dict["MESSAGE"] = msg.encode("cp932", "replace")
113         return post_dict
114
115     def set_extra_post(self, post_dict):
116         return post_dict
117
118     def set_extra_dat_request(self, request, thread):
119         return request
120
121     def get_title_from_dat(self, line):
122         if line:
123             line = line.decode(self.encoding, "replace")
124             m = self.dat_reg.match(line)
125             if m:
126                 return m.group("title")
127         return ""
128
129     def get_board_dir_path(self):
130         """Returns board dir path from logs dir downward, not full path"""
131
132         return self.bbs_type + "/" + self.host + "/" + self.board