Module: Lijab::Commands::ContactsCommandMixin

Defined in:
lib/lijab/commands/contacts.rb

Constant Summary collapse

SORTBY =
["status", "alpha"]

Instance Method Summary collapse

Instance Method Details

#completer(line) ⇒ Object


8
9
10
11
12
13
# File 'lib/lijab/commands/contacts.rb', line 8

def completer(line)
   sortby = line.split[1] || ""
   if SORTBY.grep(sortby).empty?
      SORTBY.grep(/^#{Regexp.escape(sortby)}/)
   end
end

#group_contacts(contacts) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lijab/commands/contacts.rb', line 15

def group_contacts(contacts)
   grouped = {}
   contacts.each do |jid,contact|
      groups = contact.roster_item.groups
      if groups.empty?
         (grouped["<no group>"] ||= []) << contact
      else
         groups.each { |g| (grouped[g] ||= []) << contact }
      end
   end

   grouped = grouped.sort_by { |g,c| g }
end

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
# File 'lib/lijab/commands/contacts.rb', line 29

def print_contacts(sort_by_status=false, online_only=false)
   if sort_by_status
      contacts = Main.contacts.sort { |a, b| -(a[1].presence <=> b[1].presence) }
   else
      contacts = Main.contacts.sort_by { |j,c| c.simple_name }
   end

   if Config.opts[:show_groups_in_contact_list]
      grouped = group_contacts(contacts)
   else
      grouped = {nil => contacts.map { |j,c| c }}
   end

   s = []
   grouped.each do |group, contactz|
      if online_only
         next unless contactz.any? { |c| c.online? }
      end

      s << " #{group} ".on_blue if group
      contactz.each do |contact|
         unless online_only && !contact.online?
            main = contact.presence
            s << "* #{contact.simple_name} #{main.pretty(true)} " \
               "(#{main.priority || 0}) [#{main.from || contact.jid}]"

            if online_only && contact.roster_item.presences.length > 1
               contact.roster_item.presences.each do |p|
                  if p.from != main.from
                     s << "    #{p.from} #{p.pretty(true)} (#{p.priority || 0})"
                  end
               end
            end
         end
      end
   end

   Out::put(s.join("\n")) unless s.empty?
end