1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
30 from BbsType import bbs_type_judge_uri
31 from BbsType import bbs_type_exception
32 from HTMLParserEx import HTMLParserEx
36 from http_sub import HTTPDebugHandler
38 GLADE_FILENAME = "submit_window.glade"
40 cookie_jar = cookielib.CookieJar()
41 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar),
46 raise ValueError, "parameter must not be empty"
51 class ConfirmationHTMLParser(HTMLParserEx):
54 HTMLParserEx.__init__(self)
62 def handle_starttag(self, tag, attr):
66 self.inputs.append(dict(attr))
72 attr_dict = dict(attr)
73 if "http-equiv" in attr_dict \
74 and attr_dict["http-equiv"] == "refresh":
77 def handle_endtag(self, tag):
83 def handle_comment(self, comment):
86 def handle_data(self, data):
89 elif not self.in_head:
90 self.message += data.rstrip()
92 def handle_charref(self, ref):
95 data = unichr(int(ref))
100 def handle_entityref(self, name):
101 if name in htmlentitydefs.name2codepoint:
102 codepoint = htmlentitydefs.name2codepoint[name]
103 self.message += unichr(codepoint)
105 self.message += "&"+name+";"
110 def __init__(self, uri):
112 self.bbs_type = bbs_type_judge_uri.get_type(uri)
113 if not self.bbs_type.is_thread():
114 raise bbs_type_exception.BbsTypeError, \
115 "the uri does not represent thread: " + uri
117 glade_path = os.path.join(config.glade_dir, GLADE_FILENAME)
118 self.widget_tree = gtk.glade.XML(glade_path)
119 self.window = self.widget_tree.get_widget("submit_window")
120 self.entry_name = self.widget_tree.get_widget("entry_name")
121 self.entry_mail = self.widget_tree.get_widget("entry_mail")
122 self.textbuffer = self.widget_tree.get_widget("textview").get_buffer()
124 sigdic = {"on_submit_activate": self.on_submit_activate,
125 "on_close_activate": self.on_close_activate}
127 self.widget_tree.signal_autoconnect(sigdic)
129 title = datfile.get_title_from_dat(self.bbs_type)
131 self.window.set_title(title)
133 def on_close_activate(self, widget):
134 self.window.destroy()
136 def on_submit_activate(self, widget):
137 name = self.entry_name.get_text()
138 mail = self.entry_mail.get_text()
139 msg = self.textbuffer.get_text(
140 self.textbuffer.get_start_iter(), self.textbuffer.get_end_iter())
142 self.post_dict = self.bbs_type.build_post_dict(name, mail, msg)
143 self.post_dict = self.bbs_type.set_extra_post(self.post_dict)
148 for name, value in self.post_dict.iteritems():
150 % (name, value.decode(self.bbs_type.encoding, "replace"))
151 post_encoded = urllib.urlencode(self.post_dict)
153 req = urllib2.Request(self.bbs_type.get_post_uri(), post_encoded)
154 req.add_header("Referer", self.uri)
155 req.add_header("User-agent", config.User_Agent)
157 res = opener.open(req)
158 self.on_response(res)
160 def on_response(self, response):
161 data = response.read()
162 info = response.info()
163 if "Content-Type" in info:
166 "charset=(?P<charset>[a-zA-Z0-9_\-]+)", info["Content-Type"])
168 charset = match.group("charset").lower()
170 if charset in ("x-sjis", "x_sjis", "sjis", "shiftjis", "shift-jis",
171 "shift_jis", "s-jis", "s_jis"):
173 elif charset in ("euc-jp", "euc_jp", "eucjp"):
177 data = data.decode(encoding, "replace")
178 p = ConfirmationHTMLParser()
184 window = gtk.Window()
186 window.set_default_size(500, 500)
188 window.set_title(p.title)
189 textview = gtk.TextView()
190 textview.set_wrap_mode(gtk.WRAP_CHAR)
191 textview.set_editable(False)
192 buf = textview.get_buffer()
193 buf.insert(buf.get_end_iter(), p.message)
195 for input in p.inputs:
196 if "type" in input and input["type"] == "submit":
197 button = gtk.Button(input["value"])
198 button.connect("clicked",
199 lambda widget: self.on_button_submit_clicked(
200 widget, p.inputs, encoding))
201 anchor = buf.create_child_anchor(buf.get_end_iter())
202 textview.add_child_at_anchor(button, anchor)
211 def on_timeout(widget):
213 uri_opener.open_uri(self.bbs_type.get_thread_uri(), True)
215 gobject.timeout_add(2 * 1000, on_timeout, window)
217 def on_button_submit_clicked(self, widget, inputs, encoding):
218 widget.get_toplevel().destroy()
221 if "name" in input and "value" in input:
223 value = input["value"]
225 value = value.encode(encoding, "replace")
226 self.post_dict[name] = value