trunkのcomments plugin
posted by jun-g at Tue, 12 Dec 2006 03:00 JST
先日のtrackback pluginのパッチは無事に取り込まれた模様。よかった。
そのパッチを作る際、trunkのcomments.pyを軽く見たんやけど、なんかメール送信時の非ASCII文字を受け付けない問題とか色々修正されてるっぽかったので、近々試してみないとなぁと思ってたら、shunuhsさんがtrunkに入れ替えた上にパッチを書いて、しかもtb spam用のプラグインまで作ってた。素晴らしいっす。仕事早いっす。動作実績があると心強いので、早速僕もtrunkのcomments/trackback pluginに入れ替える事にした。最近コメントスパムに悩まされていたので、ついでにMagicWord pluginも導入することに。
が、その前にshunuhsさんが「メールが飛ばない」と言われている問題を調べてみる事にした。テスト環境のcomments.pyをtrunkのものに入れ替えて試してみると、pyblosxomのエラーログに
2006-12-12 00:07:34,909 [ERROR] comments: error sending email: ['Traceback (most recent call last):\n', ' File "/pub/www/pyblosxom/plugins/comments.py", line 524, in send_email\n msg=body)\n', ' File "/usr/local/lib/python2.4/smtplib.py", line 692, in sendmail\n (code,resp) = self.data(msg)\n', ' File "/usr/local/lib/python2.4/smtplib.py", line 485, in data\n q = quotedata(msg)\n', ' File "/usr/local/lib/python2.4/smtplib.py", line 193, in quotedata\n re.sub(r\'(?:\\r\\n|\\n|\\r(?!\\n))\', CRLF, data))\n', ' File "/usr/local/lib/python2.4/sre.py", line 142, in sub\n return _compile(pattern, 0).sub(repl, string, count)\n', 'TypeError: expected string or buffer\n']
というのが出てた。どうもメール送信時にエラーになってるっぽい。で、なんとなくピンときて入れた修正が以下。
--- comments.py.orig Tue Dec 12 01:52:47 2006
+++ comments.py Tue Dec 12 02:36:47 2006
@@ -521,7 +521,7 @@
server = smtplib.SMTP(config['comment_smtp_server'])
server.sendmail(from_addr=email,
to_addrs=config['comment_smtp_to'],
- msg=body)
+ msg=body.as_string())
server.quit()
except Exception, e:
予想適中、無事にメール送信できるようになった。しかーし!現状のcomments.pyの作りだと、メールヘッダに設定されるべき「From」「To」「Subject」がメール本文に記載されるという超手抜き仕様。現状だとこんな本文のメールが来る。
From: from_at_aemonfreaks.com To: to_at_daemonfreaks.com Date: Mon, 11 Dec 2006 16:20:32 -0000 Subject: comment by TEST Name: TEST URL: Hostname: localhost.daemonfreaks.local (192.168.0.21) Entry URL: http://localhost/blog/200611060000 Comment location: /pub/www/pyblosxom/entries/comments/200611060000-1165854032.63.cmt Hello, world.
しかも件名が空。イケてない。先頭の4行は明らかにメールヘッダにあるべき。なので、以前自分で修正したやつと同様の修正を今回も入れる事にした。修正内容は以下のとおり。
--- comments.py.orig Tue Dec 12 01:52:47 2006
+++ comments.py Tue Dec 12 01:48:48 2006
@@ -475,13 +475,9 @@
comment_dir = os.path.join(config['comment_dir'], entry['absolute_path'])
# create the message
+ from email.Header import Header
from email.MIMEText import MIMEText
message = []
- message.append("From: %s" % email)
- message.append("To: %s" % config["comment_smtp_to"])
- message.append("Date: %s" % formatdate(float(comment['pubDate'])))
- message.append("Subject: comment by %s" % author)
- message.append("")
message.append("Name: %s" % author)
if comment.has_key('email'):
message.append("Email: %s" % comment['email'])
@@ -498,7 +494,13 @@
body = '\n'.join(message)
body = MIMEText(body.encode('utf-8'), 'plain', 'utf-8')
-
+ subj = "comment by %s" % author
+ subj = Header(subj.encode("utf-8"), "utf-8")
+ body["Subject"] = subj
+ body["From"] = config["comment_smtp_from"]
+ body["To"] = config["comment_smtp_to"]
+ body["Date"] = formatdate(float(comment['pubDate']))
+
if (config.has_key('comment_mta_cmd')):
argv = [config['comment_mta_cmd'],
'-s',
@@ -506,7 +508,7 @@
config['comment_smtp_to']]
# TODO: switch to subprocess when we can require python 2.4
process = popen2.Popen3(argv, capturestderr=True)
- process.tochild.write(body)
+ process.tochild.write(body.as_string())
process.tochild.close()
process.wait()
stdout = process.fromchild.read()
@@ -521,7 +523,7 @@
server = smtplib.SMTP(config['comment_smtp_server'])
server.sendmail(from_addr=email,
to_addrs=config['comment_smtp_to'],
- msg=body)
+ msg=body.as_string())
server.quit()
except Exception, e:
これで「Subject」「From」「To」(「Date」はオマケ)が正しくメールヘッダに設定されるようになった。ちなみに「comment_mta_cmd」を使用してメール送信を行う場合の動作は未確認なので、どなたか試してみてください…。
と、ここまで調べたところで時間切れになってしまった。続きは明日以降で。Magic Wordとtb_spam.pyの動作確認をしてから全部導入することにしよう。