File: model.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 (210 lines) | stat: -rw-r--r-- 4,739 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

require 'view'

module BBS
  class ModelObject
    include Amrita
    attr_reader :bbs

    def initialize(bbs)
      if ModelObject.ancestors.include?(ExpandByMember)
        p type.ancestors
        p ModelObject.ancestors
      end
      @bbs = bbs
      bbs.install_view_module(self)
    end

    def loc
      bbs.loc
    end

    def theme_selecter
      $amritabbs_config[:themes].collect do |t|
        { 
          :theme => if t == bbs.loc.theme or (bbs.loc.theme == nil and t == $amritabbs_config[:default_theme])
                      "*#{t}*"
                    else
                      a(:href=>bbs.loc.theme_change(t)) { t } 
                    end
        }
      end
    end
  end

  class ThemeSelecter < ModelObject
    def initialize
    end
  end

  class BoardList < ModelObject
    include CommonView::BoardList
    attr_reader :categories, :last_update

    def initialize(bbs)
      super
      @categories = []
      path = bbs.data_path(nil, "board.txt")
      load_from_file(path)
    end

    def get_board(key)
      @categories.each do |c|
        c.boards.each do |b|
          return b if b.key == key
        end
      end
      nil
    end

    def load_from_file(path)
      @current_category = nil
      a = []
      File::open(path) do |f|
        f.each do |l|
          l.chomp!
          if l.size > 0
            a << l
          else
            a << nil
          end
          if a.size == 3
            add_item(*a)
            a = []
          end
        end
      end

      @last_update = File::stat(path).mtime
    end

    def add_item(name, url, key)
      if url or key
        @current_category.add_boad(Board.new(bbs, name, url, key))
      else
        @current_category = Category.new(bbs, name)
        @categories << @current_category
      end
    end
  end

  class Category < ModelObject
    include CommonView::Category
    attr_reader :category, :boards

    def initialize(bbs, name)
      super(bbs)
      @category = name
      @boards = []
    end

    def add_boad(b)
      @boards << b
    end
  end

  class Board < ModelObject
    include ExpandByMember
    include CommonView::Board
    attr_reader :name, :url, :key

    def initialize(bbs, name, url, key)
      super(bbs)
      @name, @url, @key = name, url, key
    end

    def link
      if @url 
        a(:href=>@url) { @name }
      else
        url = loc.to_board(@key).to_s
        a(:href=>url) { @name }
      end
    end

    def each_thread(&block)
      path = bbs.data_path(@key, "subject.txt")
      n = 1
      File::open(path) do |f|
        ff = f.each do |l|
          fname, title = *l.split("<>")
          next unless File::readable?(bbs.data_path(@key, fname))
          yield(n, fname, title)
          n = n + 1
        end
      end
    end

    def get_thread(threadid)
      fn = threadid + ".dat"
      each_thread do |n, fname, title|
        return BBSThread.new(bbs, self, n, fname, title) if fn == fname
      end
      nil
    end
  end

  class BBSThreadTitle < ModelObject
    include CommonView::BBSThreadTitle
    attr_reader :title, :num, :has_summary, :threadid
    def initialize(bbs, board, num, has_summary, fname, title)
      super(bbs)
      @board, @fname, @title, @num, @has_summary = 
        board, fname, title, num, has_summary
      @threadid = @fname.gsub(".dat","")
    end
  end

  class BBSThread < ModelObject
    include ExpandByMember
    include CommonView::BBSThread
    attr_reader :title, :board, :num, :articles, :threadid

    def initialize(bbs, board, num, fname, title)
      super(bbs)
      @board, @fname, @title, @num = 
        board, fname, title, num
      @threadid = @fname.gsub(".dat","")
      load_data_file
    end

    def load_data_file
      path = bbs.data_path(board.key, "subject.txt")
      File::open(path) do |f|
        f.each do |l|
          if l =~ /#{@threadid}.dat<>(.*)$/
            @title= $1
            break
          end
        end
      end

      path = bbs.data_path(board.key, @fname)
      #IO::popen("nkf -e #{path}") do |f|
      File::open(path) do |f|
        num = 0
        @articles = f.collect do |l|
          num = num + 1
          a = l.chomp.split("<>").collect { |x| x == "" ? nil : x }
          Article.new(bbs, num, *a[0..3])
        end
        @articles_num = num
        @articles = loc.select(@articles)
      end
    end

  end

  class Article < ModelObject
    include CommonView::Article
      attr_reader :num, :name, :mail, :date, :text

    def initialize(bbs, num, name=nil, mail=nil, date=nil, text=nil)
      super(bbs)
      @num, @name, @mail, @date =
        num, name, mail, date
      @text = SanitizedString.new(text) if text
      init_view
    end
  end
end