#!/usr/bin/ruby -Ke
require "cgi"
require "htmltemplate"
require "bbs"
require "article"
#ログ用データファイル
DATAFILE = "simplebbs.log"
#テンプレート用HTMLファイル
HTMLFILE = "simplebbs.html"
#クッキー接頭辞
COOKIE_PREFIX = "simplebbs-"
cgi = CGI.new()
input = {}
input["name"] = cgi.cookies[COOKIE_PREFIX + "name"].first.to_s
cgi.params.each do |key,val|
input[key] = val.first
end
html = HTMLTemplate.open( HTMLFILE )
bbs = BBS.new( DATAFILE )
message = "ご自由に書き込んで下さい。"
begin
case input["mode"]
when "post"
bbs.post( input["title"], input["name"], input["comment"], cgi.remote_addr )
message = "書き込みありがとうございました。"
input["title"] = ""
else
bbs.get()
end
rescue ArticleError, BBSError
message = $!.to_s
begin
bbs.get()
rescue
end
end
#メッセージの置換
html.gsub!( "(\tmessage\t)", message )
#名前、エラー時のタイトルの置換
[ "name", "title" ].each do |item|
html.gsub!( "(\t#{item}\t)", input[item] || "" )
end
#記事が無いときのブロックを処理
html.block( "noarticle" ) do |block|
bbs.size > 0 ? "" : block.to_s
end
#記事ブロックの処理
html.block( "article" ) do |block|
bbs.collect do |article|
block.dup.gsub!(
"(\tarticle_name\t)", article.name
).gsub!(
"(\tarticle_title\t)", article.title
).gsub!(
"(\tarticle_date\t)", article.date
).gsub!(
"(\tarticle_comment\t)", article.comment
).to_s
end.join( "" )
end
expires = Time.now + 60*60*24*30
cookies = []
cookies.push( CGI::Cookie::new({"name" => COOKIE_PREFIX + "name", "value" => input['name'], 'expires' => expires}) )
cgi.out( "cookie" => cookies ) do
html.to_s
end