Class: Lijab::Contacts::Contacts

Inherits:
Hash
  • Object
show all
Defined in:
lib/lijab/contacts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(roster) ⇒ Contacts

Returns a new instance of Contacts.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/lijab/contacts.rb', line 159

def initialize(roster)
   super()
   @roster = roster

   # why does everything always has to be so hackish?
   self_ri = Jabber::Roster::Helper::RosterItem.new(Main.client)
   self_ri.jid = Config.jid.strip
   @roster.items[self_ri.jid] = self_ri

   @roster.add_presence_callback(&method(:handle_presence))
   @roster.add_subscription_callback(&method(:handle_subscription))
   @roster.add_subscription_request_callback(&method(:handle_subscription))
   @roster.wait_for_roster

   @subscription_requests = {}
   @short = {}

   @roster.items.each do |jid, item|
      add(jid, Contact.new(jid.node, item))
   end
end

Instance Attribute Details

#rosterObject (readonly)

Returns the value of attribute roster


157
158
159
# File 'lib/lijab/contacts.rb', line 157

def roster
  @roster
end

#subscription_requestsObject (readonly)

Returns the value of attribute subscription_requests


157
158
159
# File 'lib/lijab/contacts.rb', line 157

def subscription_requests
  @subscription_requests
end

Instance Method Details

#[](k) ⇒ Object


181
182
183
184
185
186
187
# File 'lib/lijab/contacts.rb', line 181

def [](k)
   return @short[k] if @short.key?(k)

   k = Jabber::JID.new(k) unless k.is_a?(Jabber::JID)

   super(k) || super(k.strip)
end

#add(jid, contact = nil) ⇒ Object


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/lijab/contacts.rb', line 197

def add(jid, contact=nil)
   if contact
      self[jid] = contact
      if @short.key?(jid.node)
         prev = @short[jid.node]

         self[jid].simple_name = jid.strip.to_s
         self[prev.jid].simple_name = prev.jid.strip.to_s

         @short[prev.jid.strip.to_s] = @short.delete(jid.node)
         @short[jid.strip.to_s] = self[jid]
      else
         @short[jid.node] = self[jid]
      end
   else
      jid = Jabber::JID.new(jid) unless jid.is_a?(Jabber::JID)
      jid.strip!

      @roster.add(jid, nil, true)
   end

   contact || self[jid]
end

#completer(line, end_with_colon = true) ⇒ Object


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/lijab/contacts.rb', line 259

def completer(line, end_with_colon=true)
   if line.include?([email protected])
      matches = @roster.items.values.collect { |ri| ri.presences }.flatten.select do |p|
         p.from.to_s =~ /^#{Regexp.escape(line)}/
      end.map { |p| p.from.to_s }
   else
      if Config.opts[:autocomplete_online_first]
         matches = @short.keys.find_all do |name|
            @short[name].online? && name =~ /^#{Regexp.escape(line)}/
         end
      end
      if matches.empty? || !Config.opts[:autocomplete_online_first]
         matches = @short.keys.find_all { |name| name =~ /^#{Regexp.escape(line)}/ }
      end
   end
   end_with_colon && matches.length == 1 ? "#{matches.first}:" : matches
end

#handle_non_contact_message(msg) ⇒ Object


277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/lijab/contacts.rb', line 277

def handle_non_contact_message(msg)
   # TODO: improve this, maybe show the contact differentiated in /contacts

   jid = msg.from.strip

   ri = Jabber::Roster::Helper::RosterItem.new(Main.client)
   ri.jid = jid

   self[jid] = Contact.new(jid.to_s, ri)
   self[jid].handle_message(msg)
   #add(jid, Contact.new(jid.to_s, ri)).handle_message(msg)
end

#handle_presence(roster_item, old_p, new_p) ⇒ Object


290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/lijab/contacts.rb', line 290

def handle_presence(roster_item, old_p, new_p)
   contact = self[new_p.from]
   if Config.opts[:show_status_changes]
      type = new_p.type
      if type == nil || type == :unavailable && (!contact || !contact.online?)
         Out::presence(new_p.from.to_s, new_p)
      end
   end

   if contact
      contact.roster_item.presences.delete_if { |p| p.type == :unavailable } rescue nil
      contact.presence_changed(old_p, new_p)
   end
end

#handle_subscription(roster_item, presence) ⇒ Object


305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/lijab/contacts.rb', line 305

def handle_subscription(roster_item, presence)
   show = true
   if presence.type == :subscribe
      show = !@subscription_requests.key?(presence.from.strip)
      @subscription_requests[presence.from.strip] = presence
   elsif presence.type == :subscribed
      jid = presence.from.strip

      @roster.add(jid)
      add(jid, Contact.new(jid.node, @roster[jid]))
      #p = Jabber::Presence.new.set_type(:probe)
   end

   Out::subscription(presence.from.to_s, presence.type, presence.status) if show
end

#has_subscription_requests?Boolean

Returns:

  • (Boolean)

251
252
253
# File 'lib/lijab/contacts.rb', line 251

def has_subscription_requests?
   !@subscription_requests.empty?
end

#key?(k) ⇒ Boolean

Returns:

  • (Boolean)

189
190
191
192
193
194
195
# File 'lib/lijab/contacts.rb', line 189

def key?(k)
   return true if @short.key?(k)

   k = Jabber::JID.new(k) unless k.is_a?(Jabber::JID)

   super(k) || super(k.strip)
end

#process_request(jid, action) ⇒ Object


232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/lijab/contacts.rb', line 232

def process_request(jid, action)
   jid = Jabber::JID.new(jid) unless jid.is_a?(Jabber::JID)
   jid.strip!
   if @subscription_requests.include?(jid)

      @subscription_requests.delete(jid)
      case action
      when :accept
         Main.contacts.roster.accept_subscription(jid)
      when :decline
         Main.contacts.roster.decline_subscription(jid)
      end

      true
   else
      false
   end
end

#remove(jid) ⇒ Object


221
222
223
224
225
226
227
228
229
230
# File 'lib/lijab/contacts.rb', line 221

def remove(jid)
   return false unless key?(jid)

   contact = self[jid]
   contact.roster_item.remove()
   @short.delete(contact.simple_name)
   self.delete(contact.jid)

   true
end