File: bbs.rb

package info (click to toggle)
amrita 1.0.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 1,880 kB
  • ctags: 1,363
  • sloc: ruby: 9,159; xml: 978; makefile: 111
file content (409 lines) | stat: -rw-r--r-- 10,053 bytes parent folder | download | duplicates (4)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
require 'amrita/parts'

require 'model'

def p(param)
  $amritabbs_inspect_object << param
  puts param.inspect if $amritabbs_config[:debug_bbs]
end

module BBS
  class Location
    attr_accessor :theme, :template, :board, :thread, :start, :to, :last
    def initialize
      @theme, @template = @board = @thread = @start = @to = @last = nil
    end

    def to_top
      ret = dup
      ret.board = ret.template = ret.thread = nil
      ret.start = ret.to = ret.last = nil
      ret
    end

    def theme_change(theme)
      theme = nil if theme == $amritabbs_config[:default_theme]
      ret = dup
      ret.theme = theme
      ret
    end

    def to_board(board)
      ret = dup
      ret.template = 'board'
      ret.board = board
      ret.thread = ret.start = ret.to = ret.last = nil
      ret
    end

    def to_thread(thread)
      ret = dup
      ret.template = 'thread'
      ret.start = ret.to = ret.last = nil
      ret.thread = thread
      ret
    end

    def get_all
      ret = dup
      ret.start = ret.to = ret.last = nil
      ret
    end

    def get_last(n)
      ret = dup
      ret.last = n
      ret
    end

    def get_range(start, to)
      ret = dup
      ret.start = ret.to = ret.last = nil
      ret.start = start
      ret.to = to
      ret
    end

    def to_s
      ret = "#{$amritabbs_config[:script_name]}?"
      ret += "&theme=#@theme" if @theme
      ret += "&template=#@template" if @template
      ret += "&board=#@board" if @board
      ret += "&thread=#@thread" if @thread
      ret += "&last=#@last" if @last
      ret += "&start=#@start" if @start
      ret += "&to=#@to" if @to
      ret
    end

    def Location::new_from_cgi(cgi)
      ret = new
      ret.theme = cgi['theme'][0]
      ret.theme = nil if ret.theme == ""
      ret.template = cgi['template'][0]
      ret.board = cgi['board'][0]
      ret.thread = cgi['thread'][0]
      ret.last = cgi['last'][0]
      ret.last = ret.last.to_i if ret.last
      ret.start = cgi['start'][0]
      ret.start = ret.start.to_i if ret.start
      ret.to = cgi['to'][0]
      ret.to = ret.to.to_i if ret.to
      ret
    end

    def select(ary)
      if last
        s = last < ary.size ? last : ary.size
        ary[(-1*s)..-1]
      else
        if start
          if to
            ary[(start-1)..(to-1)]
          else
            ary[(start-1)..-1]
          end
        else
          if to
            ary[0..(to-1)]
          else
            ary
          end
        end
      end
    end
  end

  class BBSModel
    include Amrita
    include ExpandByMember

    attr_reader :loc, :data_dir, :template_dir, :view_module

    def initialize(loc, data_dir, template_dir)
      @loc = loc
      @data_dir = data_dir
      @template_dir = template_dir
      setup_view
      setup_handlers
      setup_advertize
    end

    def template_path
      ret = File::join(@theme_dir, loc.template||$amritabbs_config[:default_template]) + ".html"
      unless File::readable? ret
        dir = File::join(@template_dir, $amritabbs_config[:default_theme]) 
        ret = File::join(dir, loc.template||$amritabbs_config[:default_template]) + ".html"
      end
      ret
    end

    def parts_template_path
      File::join(@theme_dir, "parts.html")
    end

    def data_path(board, path)
      case board
      when String
        dir = File::join(@data_dir, board)
        File::join(dir, path)
      when Board
        dir = File::join(@data_dir, board.key)
        File::join(dir, path)
      when nil
        File::join(@data_dir, path)
      else
        raise "can't happen"
      end
    end

    def setup_view
      @theme = loc.theme || $amritabbs_config[:default_theme]
      @theme_dir = File::join(@template_dir, @theme)
      
      require File::join(@theme_dir, "view.rb")
      @view_module = $amritabbs_config[:view_modules][@theme]
      raise "theme #{@theme} not found" unless @view_module
      install_view_module(self)
    end

    def install_view_module(obj, modname=nil)
      modname = obj.type.to_s.gsub(/([A-Z]\w*::)*([A-Z]\w*)/) { $2 } unless modname
      mod = @view_module.const_get(modname)
      obj.extend(mod) if mod
    rescue NameError
      STDERR.puts $!
      nil
    end

    def setup_handlers
      @handlers = [
        ViewHandler.new(self),
        NewArticleHandler.new(self),
        NewThreadHandler.new(self),
      ]
    end

    def process_request(params)
      handler = @handlers.find do |h|
        h.can_handle?(params)
      end
      raise "can't handle this request" unless handler
      handler.process_request(params)
    end

    def show_view
      if File::readable?(parts_template_path)
        t = TemplateFileWithCache.new(parts_template_path)
        t.install_parts_to(@view_module)
      end

      t = TemplateFileWithCache.new(template_path)
      t.use_compiler = $amritabbs_config[:use_compiler] 
      #t.debug_compiler = true
      t.install_parts_to(@view_module)
      t.expand($stdout, self)
    end

    def boardlist
      unless defined? @boardlist
        @boardlist = BoardList.new(self)
      end
      @boardlist
    end

    def current_board
      boardlist.get_board(loc.board)
    end

    def current_thread
      current_board.get_thread(loc.thread)
    end

    def setup_advertize
      @advertize = []
      if $amritabbs_config[:advertize_html] and File::readable?($amritabbs_config[:advertize_html])
        a = Amrita::HtmlParser::parse_file($amritabbs_config[:advertize_html])
        @advertize = a.find_all { |e| e.tagname_symbol == :div and e[:class] == "advertize" }
      end
    end

    def advertize_random
      r = rand(@advertize.size)
      @advertize[r]
    end

    def theme_selecter
      $amritabbs_config[:themes].collect do |t|
        e(:a, :href=>loc.theme_change(t)) { t }
      end
    end

    def whatnew
      ret = {}
      ret[:boards] = []
      all_thread=boardlist.categories.each do |c|
        ret[:boards] += c.boards.collect do |b|
          next if b.url
          link = e(:a, :href=>loc.to_board(b.key)) { b.name }
          path = data_path(b.key, "subject.txt")
          next unless File::readable? path
          mtime = File::stat(path).mtime
          {
            :link => link,
            :mtime => mtime
          }
        end
      end
      ret[:boards].delete_if { |b| b == nil}
      ret[:boards].sort! { |a,b| a[:mtime] <=> b[:mtime] }
      
      ret
    end
  end
  
  class HandlerBase
    attr_reader :model

    def initialize(model)
      @model = model
    end
  end

  class ViewHandler < HandlerBase
    def can_handle?(param)
      param["action"][0] == nil
    end

    def process_request(param)
      model.show_view
    end
  end

  class ModelUpdateHandler < HandlerBase
    def get_thread_key(board)
      Time.new.to_i.to_s
    end

    def lock(&block)
      path = model.data_path(nil, "board.txt")
      File::open(path) do |f|
        f.flock(File::LOCK_EX) 
        begin
          yield(f)
        ensure
          f.flock(File::LOCK_UN) 
        end
      end
    end
    
    def put_subject(board, key, subject)
      path1 = model.data_path(board, "subject.txt")
      path2 = model.data_path(board, "subject.txt.sav")

      unless subject
        File::open(path1) do |f|
          f.each do |l|
            if l =~ /#{key}.dat<>(.*)/
              subject = $1
              break
            end
          end
        end
      end
      File::rename(path1, path2)
      File::open(path1, "w") do |newf|
        newf.puts("#{key}.dat<>#{subject}")
        File::open(path2) do |oldf|
          oldf.each do |l|
            next if l =~ /#{key}/
            newf.print l
          end
        end
      end
    end

    def put_article(board, key, name, mail, message)
      path = model.data_path(board, "#{key}.dat")
      File::open(path, "a") do |f|
        f.puts "#{name}<>#{mail}<>#{Time.new}<>#{message}"
      end
    end

    def sanitize_text(text)
      if text
        ret = text.amrita_sanitize
        ret.gsub!(/http:\S+/) { "<a href=\"#{$~}\">#{$~}</a>" }
        ret.gsub!(/\r\n|\n/, "<br>")
        ret
      else
        text
      end
    end

    def redirect_response(url)
      e(:html)  {
        e(:head) {
          e(:meta, "http-equiv"=>"refresh", :content=>"0;URL=#{url}")
        } +
          e(:body) {
          [
            "wait a second or click here-->",
            e(:a, :href=>url) { url }
          ]
        }
      }
    end
  end

  class NewThreadHandler < ModelUpdateHandler
    def can_handle?(param)
      param["action"][0] == "newthread"
    end

    def process_request(cgi)
      lock do
      board = sanitize_text(cgi['board'][0])
      name = sanitize_text(cgi['from'][0])
      mail = sanitize_text(cgi['mail'][0])
      subject = sanitize_text(cgi['subject'][0])
      message = sanitize_text(cgi['message'][0])
      raise "no data" unless board and board != ""
      raise "no data" unless name and name != ""
      raise "no data" unless subject and subject != ""
      raise "no data" unless message and message != ""

      key = get_thread_key(board)
      put_subject(board, key, subject) unless mail == "sage"
      put_article(board, key, name, mail, message)

      print redirect_response(model.loc.to_board(board))
      end
    end
  end

  class NewArticleHandler < ModelUpdateHandler
    def can_handle?(param)
      param["action"][0] == "newarticle"
    end

    def process_request(cgi)
      lock do
      board = sanitize_text(cgi['board'][0])
      thread = sanitize_text(cgi['thread'][0])
      name = sanitize_text(cgi['from'][0])
      mail = sanitize_text(cgi['mail'][0])
      message = sanitize_text(cgi['message'][0])
      raise "no data" unless board and board != ""
      raise "no data" unless thread and thread != ""
      raise "no data" unless message and message != ""

      put_subject(board, thread, nil) unless mail == "sage"
      put_article(board, thread, name, mail, message)

      print redirect_response(model.loc.to_board(board))
      end
    end
  end
end