Class: Watobo::Chats

Inherits:
Object
  • Object
show all
Defined in:
lib/watobo/core/chats.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.add(chat, prefs = {}) ⇒ Object



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
# File 'lib/watobo/core/chats.rb', line 180

def self.add(chat, prefs={})
  @chats_lock.synchronize do
    begin
      if chat.request.host then
        @chats << chat

        options = {
          :run_passive_checks => true,
          :notify => true
        }
        options.update prefs

        Watobo::PassiveScanner.add(chat) if options[:run_passive_checks] == true
        # puts "[#{self}] add"

        #@interface.addChat(self, chat) if @interface
        notify(:new, chat) if options[:notify] == true

        if chat.id != 0 then
          Watobo::DataStore.add_chat(chat)
        else
          puts "!!! Could not add chat #{chat.id}"
        end
      end

      # p "!P!"
    rescue => bang
      puts bang
      puts bang.backtrace if $DEBUG
    end
  end
end

.clearEvents(event) ⇒ Object



20
21
22
23
# File 'lib/watobo/core/chats.rb', line 20

def self.clearEvents(event)
  @event_dispatcher_listeners[event] ||= []
  @event_dispatcher_listeners[event].clear
end

.dirs(site, list_opts = {}, &block) ⇒ Object



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
# File 'lib/watobo/core/chats.rb', line 86

def self.dirs(site, list_opts={}, &block)
  opts = { :base_dir => "",
    :include_subdirs => true
  }
  opts.update(list_opts) if list_opts.is_a? Hash
  list = Hash.new
  @chats.each do |chat|
    next if chat.request.site != site
    next if list.has_key?(chat.request.path)
    next if opts[:base_dir] != "" and chat.request.path !~ /^#{Regexp.quote(opts[:base_dir])}/
    subdirs = chat.request.subDirs
    subdirs.each do |dir|
      next if dir.nil?
      next if list.has_key?(dir)
      list[dir] = :path
      if opts[:include_subdirs] == true then
        yield dir if block_given?
      else
        d = dir.gsub(/#{Regexp.quote(opts[:base_dir])}/,"")
        yield dir unless d =~ /\// and block_given?
      # otherwise it is a subdir of base_dir
      end
    end
  end
end

.each(&block) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/watobo/core/chats.rb', line 123

def self.each(&block)
  if block_given?
    @chats_lock.synchronize do
      @chats.map{|c| yield c }
    end
  end
end

.filtered(filter, &block) ⇒ Object

only returns/yields chats wich match filter



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/watobo/core/chats.rb', line 166

def self.filtered(filter, &block)
  #puts filter.to_yaml
  @uniq_chats = {}
  filtered_chats = []
  @chats.each do |chat|
    if match?(chat, filter)
      yield chat if block_given?
    filtered_chats << chat
    end
  end

  filtered_chats
end

.get_by_id(chatid) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/watobo/core/chats.rb', line 112

def self.get_by_id(chatid)
  @chats_lock.synchronize do
    @chats.each do |c|
      if c.id.to_s == chatid.to_s then
      return c
      end
    end
  end
  return nil
end

.in_scope(&block) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/watobo/core/chats.rb', line 143

def self.in_scope(&block)
  scan_prefs = Watobo::Conf::Scanner.to_h
  #puts scan_prefs.to_yaml
  unique_list = Hash.new
  cis = []

  @chats.each do |chat|
    next if scan_prefs[:excluded_chats].include?(chat.id)
    uch = chat.request.uniq_hash

    next if unique_list.has_key?(uch) and scan_prefs[:smart_scan] == true
    unique_list[uch] = nil
    if Watobo::Scope.match_chat? chat
      cis << chat
      yield chat if block_given?
    end
  end
  cis
end

.lengthObject



135
136
137
138
139
140
141
# File 'lib/watobo/core/chats.rb', line 135

def self.length
  l = 0
  @chats_lock.synchronize do
    l = @chats.length
  end
  l
end

.loadObject



38
39
40
# File 'lib/watobo/core/chats.rb', line 38

def self.load

end

.notify(event, *args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/watobo/core/chats.rb', line 25

def self.notify(event, *args)
  if @event_dispatcher_listeners[event]
    @event_dispatcher_listeners[event].each do |m|
      m.call(*args) if m.respond_to? :call
    end
  end
end

.resetObject



33
34
35
36
# File 'lib/watobo/core/chats.rb', line 33

def self.reset
  @chats = []
  @event_dispatcher_listeners = Hash.new
end

.select(site, opts = {}, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/watobo/core/chats.rb', line 42

def self.select(site, opts={}, &block)
  o = {
    :dir => "",
    #:file => nil,
    :method => nil,
    :max_count => 0
  }
  o.update opts
  o[:dir].strip!
  o[:dir].gsub!(/^\//,"")

  matches = []
  @chats.each do |c|
    if c.request.site == site then
      matches.push c if o[:dir] == c.request.dir
      yield c if block_given?
    end
    return matches if o[:max_count] > 0 and matches.length >= o[:max_count]
  end
  return matches

end

.sites(prefs = {}, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/watobo/core/chats.rb', line 65

def self.sites(prefs={}, &block)
  list = Hash.new

  cprefs = { :in_scope => false,
    :ssl => false
  }
  cprefs.update prefs

  Watobo::Chats.each do |chat|
    next if list.has_key?(chat.request.site)
    site = chat.request.site
    next if cprefs[:in_scope] == true and not Watobo::Scope.match_site?(site)
    next if cprefs[:ssl] and not chat.use_ssl?

    yield site if block_given?
    list[site] = nil

  end
  return list.keys
end

.subscribe(event, &callback) ⇒ Object



16
17
18
# File 'lib/watobo/core/chats.rb', line 16

def self.subscribe(event, &callback)
  (@event_dispatcher_listeners[event] ||= []) << callback
end

.to_aObject



131
132
133
# File 'lib/watobo/core/chats.rb', line 131

def self.to_a
  @chats
end