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



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

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



11
12
13
14
# File 'lib/watobo/core/chats.rb', line 11

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

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



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

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



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

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



157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/watobo/core/chats.rb', line 157

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



103
104
105
106
107
108
109
110
111
112
# File 'lib/watobo/core/chats.rb', line 103

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/watobo/core/chats.rb', line 134

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



126
127
128
129
130
131
132
# File 'lib/watobo/core/chats.rb', line 126

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

.loadObject



29
30
31
# File 'lib/watobo/core/chats.rb', line 29

def self.load

end

.notify(event, *args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/watobo/core/chats.rb', line 16

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



24
25
26
27
# File 'lib/watobo/core/chats.rb', line 24

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

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/watobo/core/chats.rb', line 33

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watobo/core/chats.rb', line 56

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



7
8
9
# File 'lib/watobo/core/chats.rb', line 7

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

.to_aObject



122
123
124
# File 'lib/watobo/core/chats.rb', line 122

def self.to_a
  @chats
end