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
29 from BbsType import bbs_type_judge_uri
30 from BbsType import bbs_type_exception
31 from HTMLParserEx import HTMLParserEx
34 from http_sub import HTTPDebugHandler
36 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
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)
59 def handle_starttag(self, tag, attr):
63 self.inputs.append(dict(attr))
65 attr_dict = dict(attr)
66 if "http-equiv" in attr_dict \
67 and attr_dict["http-equiv"] == "refresh":
70 def handle_endtag(self, tag):
73 def handle_comment(self, comment):
76 def handle_data(self, data):
79 def handle_charref(self, ref):
82 data = unichr(int(ref))
87 def handle_entityref(self, name):
88 if name in htmlentitydefs.name2codepoint:
89 codepoint = htmlentitydefs.name2codepoint[name]
90 self.message += unichr(codepoint)
92 self.message += "&"+name+";"
97 def __init__(self, uri):
99 self.bbs_type = bbs_type_judge_uri.get_type(uri)
100 if not self.bbs_type.is_thread():
101 raise bbs_type_exception.BbsTypeError, \
102 "the uri does not represent thread: " + uri
104 glade_path = os.path.join(GLADE_DIR, GLADE_FILENAME)
105 self.widget_tree = gtk.glade.XML(glade_path)
106 self.window = self.widget_tree.get_widget("submit_window")
107 self.entry_name = self.widget_tree.get_widget("entry_name")
108 self.entry_mail = self.widget_tree.get_widget("entry_mail")
109 self.textbuffer = self.widget_tree.get_widget("textview").get_buffer()
111 sigdic = {"on_submit_activate": self.on_submit_activate,
112 "on_close_activate": self.on_close_activate}
114 self.widget_tree.signal_autoconnect(sigdic)
116 title = datfile.get_title_from_dat(self.bbs_type)
118 self.window.set_title(title)
120 def on_close_activate(self, widget):
121 self.window.destroy()
123 def on_submit_activate(self, widget):
124 name = self.entry_name.get_text()
125 mail = self.entry_mail.get_text()
126 msg = self.textbuffer.get_text(
127 self.textbuffer.get_start_iter(), self.textbuffer.get_end_iter())
129 self.post_dict = self.bbs_type.build_post_dict(name, mail, msg)
130 self.post_dict = self.bbs_type.set_extra_post(self.post_dict)
135 post_encoded = urllib.urlencode(self.post_dict)
138 req = urllib2.Request(self.bbs_type.get_post_uri(), post_encoded)
139 req.add_header("Referer", self.uri)
141 res = opener.open(req)
142 self.on_response(res)
144 def on_response(self, response):
145 data = response.read()
146 info = response.info()
147 if "Content-Type" in info:
150 "charset=(?P<charset>[a-zA-Z0-9_\-]+)", info["Content-Type"])
152 charset = match.group("charset").lower()
154 if charset in ("x-sjis", "x_sjis", "sjis", "shiftjis", "shift-jis",
155 "shift_jis", "s-jis", "s_jis"):
157 elif charset in ("euc-jp", "euc_jp", "eucjp"):
161 data = data.decode(encoding, "replace")
162 p = ConfirmationHTMLParser()
168 window = gtk.Window()
170 window.set_default_size(500, 500)
171 textview = gtk.TextView()
172 textview.set_wrap_mode(gtk.WRAP_CHAR)
173 textview.set_editable(False)
174 buf = textview.get_buffer()
175 buf.insert(buf.get_end_iter(), p.message)
177 for input in p.inputs:
178 if "type" in input and input["type"] == "submit":
179 button = gtk.Button(input["value"])
180 button.connect("clicked",
181 lambda widget: self.on_button_submit_clicked(
182 widget, p.inputs, encoding))
183 anchor = buf.create_child_anchor(buf.get_end_iter())
184 textview.add_child_at_anchor(button, anchor)
193 def on_timeout(widget):
195 uri_opener.open_uri(self.bbs_type.get_thread_uri(), True)
197 gobject.timeout_add(2 * 1000, on_timeout, window)
199 def on_button_submit_clicked(self, widget, inputs, encoding):
200 widget.get_toplevel().destroy()
203 if "name" in input and "value" in input:
205 value = input["value"]
207 value = value.encode(encoding, "replace")
208 self.post_dict[name] = value