Class: Lijab::Contacts::Contact

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

Constant Summary collapse

COLORS =
[:red, :blue, :yellow, :green, :magenta, :cyan].sort_by { rand() }
@@cur_color =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(simple_name, roster_item) ⇒ Contact

Returns a new instance of Contact.


42
43
44
45
46
47
# File 'lib/lijab/contacts.rb', line 42

def initialize(simple_name, roster_item)
   @simple_name = simple_name
   @roster_item = roster_item
   @resource_jid = @roster_item.jid
   @history = HistoryHandler::get(jid())
end

Instance Attribute Details

#colorObject


146
147
148
149
# File 'lib/lijab/contacts.rb', line 146

def color
   @color = COLORS[(@@cur_color = (@@cur_color + 1) % COLORS.length)] unless @color
   @color
end

#historyObject

Returns the value of attribute history


35
36
37
# File 'lib/lijab/contacts.rb', line 35

def history
  @history
end

#roster_itemObject (readonly)

Returns the value of attribute roster_item


37
38
39
# File 'lib/lijab/contacts.rb', line 37

def roster_item
  @roster_item
end

#simple_nameObject

Returns the value of attribute simple_name


35
36
37
# File 'lib/lijab/contacts.rb', line 35

def simple_name
  @simple_name
end

Instance Method Details

#handle_message(msg) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lijab/contacts.rb', line 60

def handle_message(msg)
   @thread = msg.thread

   if msg.body && !msg.body.empty?
      @resource_jid = msg.from
      Out::message_in(@simple_name, msg.body, [color(), :bold])
      @history.log(msg.body, :from)
   end

   if msg.chat_state
      s = ""
      case msg.chat_state
      when :composing
         s = "is typing"
      when :active
         Out::clear_infoline
      when :gone
         s = "went away"
      when :paused
         s = "paused typing"
      end
      Out::infoline("* #{@simple_name} #{s}".red) unless s.empty?
   end
end

#jidObject


138
139
140
# File 'lib/lijab/contacts.rb', line 138

def jid
   @roster_item.jid
end

#online?Boolean

Returns:

  • (Boolean)

142
143
144
# File 'lib/lijab/contacts.rb', line 142

def online?
   @roster_item.online?
end

#presence(jid = nil) ⇒ Object


49
50
51
52
53
54
55
56
57
58
# File 'lib/lijab/contacts.rb', line 49

def presence(jid=nil)
   if jid
      p = @roster_item.presences.select { |p| p.jid == jid }.first
   else
      p = @roster_item.presences.inject(nil) do |max, presence|
         !max.nil? && max.priority.to_i > presence.priority.to_i ? max : presence
      end
   end
   p || Jabber::Presence.new.set_type(:unavailable)
end

#presence_changed(old_p, new_p) ⇒ Object


134
135
136
# File 'lib/lijab/contacts.rb', line 134

def presence_changed(old_p, new_p)
   @resource_jid = @roster_item.jid if old_p && old_p.from == @resource_jid
end

#send_chat_state(state) ⇒ Object


115
116
117
118
119
120
# File 'lib/lijab/contacts.rb', line 115

def send_chat_state(state)
   return if state == @chat_state
   msg = Jabber::Message.new(jid(), nil).set_type(:chat).set_chat_state(state)
   Main.client.send(msg)
   @chat_state = state
end

#send_message(msg, jid = nil) ⇒ Object


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

def send_message(msg, jid=nil)
   if msg.kind_of?(Jabber::Message)
      msg.thread = @thread unless msg.thread
      message = msg
   elsif msg.kind_of?(String) && !msg.empty?
      # TODO: send chat_state only in the first message
      if jid
         @resource_jid = jid
      else
         jid = @resource_jid
      end
      Out::message_out(@simple_name, msg, color())
      message = Jabber::Message.new(jid, msg).set_type(:chat) \
                                                       .set_chat_state(:active) \
                                                       .set_thread(@thread)

      @chat_state_timer.kill if @chat_state_timer && @chat_state_timer.alive?
   end

   message = HooksHandler::handle_pre_send_message(self, message)
   return unless message

   Main.client.send(message)

   HooksHandler::handle_post_send_message(self, message)

   @history.log(message.body, :to)
   @chat_state = :active
end

#to_sObject


151
152
153
# File 'lib/lijab/contacts.rb', line 151

def to_s;
   @simple_name
end

#typed_stuffObject


122
123
124
125
126
127
128
129
130
131
132
# File 'lib/lijab/contacts.rb', line 122

def typed_stuff
   send_chat_state(:composing)

   @chat_state_timer.kill if @chat_state_timer && @chat_state_timer.alive?
   @chat_state_timer = Thread.new do
      sleep(3); return if !Main.connected
      send_chat_state(:paused)
      sleep(10); return if !Main.connected
      send_chat_state(:active)
   end
end