-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.rb
More file actions
210 lines (176 loc) · 4.9 KB
/
board.rb
File metadata and controls
210 lines (176 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# frozen_string_literal: true
require_relative 'settings'
require_relative 'thread_file'
require_relative 'string_helpers'
require_relative 'extensions'
require 'fileutils'
module Genkai
# 板を表わすクラス。
class Board
class << self
def create(directory_path, title)
Dir.mkdir(directory_path)
Dir.mkdir(directory_path / 'dat')
File.open(directory_path / 'SETTING.TXT', 'w', encoding: 'CP932') do |f|
f.write("BBS_TITLE=#{title}\n")
end
Board.new(directory_path)
end
def remove(directory_path)
FileUtils.rm_rf directory_path
end
# 1〜30文字の英小文字アラビア数字からなる。
def valid_id?(string)
string != 'admin' && string != 'test' &&
(string =~ /\A [a-z0-9]{1,30} \z/x).true?
end
end
include StringHelpers
attr_reader :settings
class NotFoundError < StandardError; end
def initialize(path)
unless File.directory?(path)
raise NotFoundError, "directory #{path} does not exist"
end
@path = path
setting_path = File.join(path, 'SETTING.TXT')
unless File.exist?(setting_path)
raise NotFoundError, "#{setting_path} does not exist"
end
@settings = SettingsFile.new(setting_path)
end
def get_all_threads
pat = File.join(@path, 'dat', '*.dat')
Dir.glob(pat).map { |path| ThreadFile.new(path) }
end
def dat_path(id)
File.join(@path, 'dat', "#{id}.dat")
end
def find_thread(id)
path = dat_path(id)
# XXX: レースがあるので正しくないが、ThreadFile.new がスレッドが
# 存在しなくてもエラーにしないのでこうしている。
if File.exist?(path)
ThreadFile.new(path)
else
nil
end
end
def delete_thread(id)
File.unlink(dat_path(id))
end
def id
File.split(@path)[-1]
end
def local_rules
path = File.join(@path, 'head.txt')
File.read(path, encoding: 'CP932').to_utf8
rescue Errno::ENOENT
''
end
def local_rules=(text)
AtomicWriteFile.open(@path / 'head.txt', 'tmp', encoding: 'CP932') do |f|
f.write(text)
end
end
def thread_stop_message
unescape_1001 File.read(@path / '1000.txt', encoding: 'CP932').to_utf8
rescue Errno::ENOENT
''
end
def thread_stop_message=(text)
escaped = escape_1001(text)
AtomicWriteFile.open(@path / '1000.txt', 'tmp', encoding: 'CP932') do |f|
f.write(escaped)
end
end
def title
settings['BBS_TITLE']
end
def to_2ch_dat_line(post, thread_title = '')
[post.name, post.mail, post.date, post.body, thread_title]
.join('<>')
.concat("\n")
.to_sjis
end
class ThreadCreateError < StandardError; end
def create_thread
unix_time = Time.now.to_i
path = dat_path(unix_time)
raise ThreadCreateError, 'thread already exists' if File.exist? path
ThreadFile.new(path)
end
# ID表示に関するポリシー。
# :no, :force, :optional のいずれかを返す。
def id_policy
if settings['BBS_NO_ID'] == 'checked'
:no
elsif settings['BBS_FORCE_ID'] == 'checked'
:force
else
:optional
end
end
ID_POLICIES = [:no, :force, :optional].freeze
def id_policy=(sym)
raise ArgumentError, 'invalid policy' unless ID_POLICIES.include?(sym)
case sym
when :no
settings['BBS_NO_ID'] = 'checked'
settings.delete('BBS_FORCE_ID')
when :force
settings.delete('BBS_NO_ID')
settings['BBS_FORCE_ID'] = 'checked'
when :optional
settings.delete('BBS_NO_ID')
settings.delete('BBS_FORCE_ID')
end
end
def emoji_only?
settings['EMOJI_ONLY_MODE'] == 'true'
end
def emoji_only=(value)
if !!value
settings['EMOJI_ONLY_MODE'] = 'true'
else
settings['EMOJI_ONLY_MODE'] = 'false'
end
end
def reject_same_content?
settings['REJECT_SAME_CONTENT'] == 'true'
end
def reject_same_content=(value)
if !!value
settings['REJECT_SAME_CONTENT'] = 'true'
else
settings['REJECT_SAME_CONTENT'] = 'false'
end
end
def forbid_nonviewer?
settings['FORBID_NONVIEWER'] == 'true'
end
def forbid_nonviewer=(value)
if !!value
settings['FORBID_NONVIEWER'] = 'true'
else
settings['FORBID_NONVIEWER'] = 'false'
end
end
def default_name
name = settings['BBS_NONAME_NAME']
if name.blank?
'<名無し>'
else
name
end
end
DEFAULT_DELETE_STRING = '<削除>'
def grave_stone
Post.new(DEFAULT_DELETE_STRING,
DEFAULT_DELETE_STRING,
DEFAULT_DELETE_STRING,
DEFAULT_DELETE_STRING,
'')
end
end
end