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
31 from BbsType import bbs_type_judge_uri
32 from BbsType import bbs_type_exception
33 from HTMLParserEx import HTMLParserEx
37 from http_sub import HTTPDebugHandler
39 GLADE_FILENAME = "submit_window.glade"
41 cookie_jar = cookielib.CookieJar()
42 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar),
47 raise ValueError, "parameter must not be empty"
52 class ConfirmationHTMLParser(HTMLParserEx):
55 HTMLParserEx.__init__(self)
63 def handle_starttag(self, tag, attr):
67 self.inputs.append(dict(attr))
73 attr_dict = dict(attr)
74 if "http-equiv" in attr_dict \
75 and attr_dict["http-equiv"] == "refresh":
78 def handle_endtag(self, tag):
84 def handle_comment(self, comment):
87 def handle_data(self, data):
90 elif not self.in_head:
91 self.message += data.rstrip()
93 def handle_charref(self, ref):
96 data = unichr(int(ref))
101 def handle_entityref(self, name):
102 if name in htmlentitydefs.name2codepoint:
103 codepoint = htmlentitydefs.name2codepoint[name]
104 self.message += unichr(codepoint)
106 self.message += "&"+name+";"
111 def __init__(self, uri):
113 self.bbs_type = bbs_type_judge_uri.get_type(uri)
114 if not self.bbs_type.is_thread():
115 raise bbs_type_exception.BbsTypeError, \
116 "the uri does not represent thread: " + uri
118 glade_path = os.path.join(config.glade_dir, GLADE_FILENAME)
119 self.widget_tree = gtk.glade.XML(glade_path)
120 self.window = self.widget_tree.get_widget("submit_window")
121 self.entry_name = self.widget_tree.get_widget("entry_name")
122 self.entry_mail = self.widget_tree.get_widget("entry_mail")
123 self.textbuffer = self.widget_tree.get_widget("textview").get_buffer()
125 sigdic = {"on_submit_activate": self.on_submit_activate,
126 "on_close_activate": self.on_close_activate}
128 self.widget_tree.signal_autoconnect(sigdic)
130 title = datfile.get_title_from_dat(self.bbs_type)
132 self.window.set_title(title)
134 def on_close_activate(self, widget):
135 self.window.destroy()
137 def on_submit_activate(self, widget):
138 name = self.entry_name.get_text()
139 mail = self.entry_mail.get_text()
140 msg = self.textbuffer.get_text(
141 self.textbuffer.get_start_iter(), self.textbuffer.get_end_iter())
143 self.post_dict = self.bbs_type.build_post_dict(name, mail, msg)
144 self.post_dict = self.bbs_type.set_extra_post(self.post_dict)
149 for name, value in self.post_dict.iteritems():
151 % (name, value.decode(self.bbs_type.encoding, "replace"))
152 post_encoded = urllib.urlencode(self.post_dict)
154 req = urllib2.Request(self.bbs_type.get_post_uri(), post_encoded)
155 req.add_header("Referer", self.uri)
156 req.add_header("User-agent", config.User_Agent)
158 res = opener.open(req)
159 self.on_response(res)
161 def on_response(self, response):
162 data = response.read()
163 info = response.info()
164 if "Content-Type" in info:
167 "charset=(?P<charset>[a-zA-Z0-9_\-]+)", info["Content-Type"])
169 charset = match.group("charset").lower()
171 if charset in ("x-sjis", "x_sjis", "sjis", "shiftjis", "shift-jis",
172 "shift_jis", "s-jis", "s_jis"):
174 elif charset in ("euc-jp", "euc_jp", "eucjp"):
178 data = data.decode(encoding, "replace")
179 p = ConfirmationHTMLParser()
185 window = gtk.Window()
187 window.set_default_size(500, 500)
189 window.set_title(p.title)
190 textview = gtk.TextView()
191 textview.set_wrap_mode(gtk.WRAP_CHAR)
192 textview.set_editable(False)
193 buf = textview.get_buffer()
194 buf.insert(buf.get_end_iter(), p.message)
196 for input in p.inputs:
197 if "type" in input and input["type"] == "submit":
198 button = gtk.Button(input["value"])
199 button.connect("clicked",
200 lambda widget: self.on_button_submit_clicked(
201 widget, p.inputs, encoding))
202 anchor = buf.create_child_anchor(buf.get_end_iter())
203 textview.add_child_at_anchor(button, anchor)
212 def on_timeout(widget):
214 uri_opener.open_uri(self.bbs_type.get_thread_uri(), True)
216 gobject.timeout_add(2 * 1000, on_timeout, window)
218 def on_button_submit_clicked(self, widget, inputs, encoding):
219 widget.get_toplevel().destroy()
222 if "name" in input and "value" in input:
224 value = input["value"]
226 value = value.encode(encoding, "replace")
227 self.post_dict[name] = value