Module: Lijab::Main

Defined in:
lib/lijab/main.rb

Class Method Summary collapse

Class Method Details

.clear_status_messageObject



166
167
168
# File 'lib/lijab/main.rb', line 166

def clear_status_message
   set_status(@status)
end

.clientObject

.connectedObject

.contactsObject

.parse_argsObject



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

def parse_args
   options = {:debug => false}
   begin
      op = OptionParser.new do |opts|
         opts.banner = "usage: lijab [-h | -V | [-a ACCOUNTNAME] [-d BASEDIR] [-D]]\n\n"
         opts.on("-D", "--[no-]debug",
                 "output xmpp debug information to stderr") { |v| options[:debug] = v }
         opts.on("-d", "--basedir BASEDIR",
                 "configs base directory") { |v| options[:basedir] = v }
         opts.on("-a", "--account ACCOUNTNAME",
                 "the name of the account to connect to") { |v| options[:account] = v }
         opts.on("-V", "--version", "print version information") do |v|
            puts "lijab #{Lijab::VERSION}"
            exit(0)
         end
      end
      begin
         op.parse!
      rescue OptionParser::ParseError => e
         puts "#{e}\n\n#{op.banner.chomp}"
         exit(1)
      end
   rescue OptionParser::MissingArgument
      puts "lijab: error: #{$!}\n\n#{op}"
      exit 1
   end
   options
end

.presenceObject

.quitObject



230
231
232
233
234
235
236
237
238
239
240
# File 'lib/lijab/main.rb', line 230

def quit
   begin
      @client.close if @connected
   rescue
   end
   InputHandler::save_typed_history
   Config::dump_config_file(false, true)
   save_session()
   puts "\nexiting..."
   exit 0
end

.read_saved_sessionObject



215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/lijab/main.rb', line 215

def read_saved_session
   path = File.join(Config.[:dir], "session_data.yml")

   if File.file?(path)
      o = YAML.load_file(path)
   else
      o = {:status => {:type => :available, :priority => 51}}
   end

   @presence = Jabber::Presence.new.set_type(o[:status][:type]) \
                                   .set_show(o[:status][:show]) \
                                   .set_status(o[:status][:status]) \
                                   .set_priority(o[:status][:priority])
end

.reconnectObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/lijab/main.rb', line 138

def reconnect
   do_sleep = 1
   loop do
      do_sleep.downto(1) do |i|
         Out::infoline("trying reconnect in #{i*5} seconds...")
         sleep(5)
      end
      do_sleep = [do_sleep*2, 10].min

      begin
         setup_client()
         Out::clear_infoline
         break
      rescue SystemCallError, SocketError
      end
   end
end

.run!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lijab/main.rb', line 48

def run!
   args = parse_args()
   Jabber::debug = args[:debug]

   Config::init(args)
   read_saved_session()

   @connected = false

   print ANSI.title("lijab -- #{Config.jid.strip}") ; STDOUT.flush

   begin
      setup_client()
   rescue SystemCallError, SocketError
      Out::error("couldn't connect")
      reconnect()
   end

   Commands::init
   InputHandler::init
end

.save_sessionObject



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/lijab/main.rb', line 203

def save_session
   return unless @presence

   o = {:status => {:type => @presence.type,
                    :show => @presence.show,
                    :status => @presence.status,
                    :priority => @presence.priority}}
   File.open(File.join(Config.[:dir], "session_data.yml"), 'w') do |f|
      f.puts(YAML.dump(o))
   end
end

.set_priority(priority) ⇒ Object



170
171
172
# File 'lib/lijab/main.rb', line 170

def set_priority(priority)
   @client.send(@presence.set_priority(priority))
end

.set_status(status, msg = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/lijab/main.rb', line 156

def set_status(status, msg=nil)
   type = status == :invisible ? :unavailable : nil
   priority = Config.opts[:status_priorities][status]
   status = nil if [:available, :invisible].include?(status)

   @presence.set_type(type).set_show(status).set_status(msg).set_priority(priority)

   @client.send(@presence)
end

.setup_after_connectObject



70
71
72
# File 'lib/lijab/main.rb', line 70

def setup_after_connect
   HooksHandler::init
end

.setup_clientObject



74
75
76
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/lijab/main.rb', line 74

def setup_client
   return unless @monitor.try_enter
   begin
      @client = Jabber::Client.new(Config.jid)

      @client.on_exception do |e,stream,from|
         @connected = false

         case from
         when :disconnected
            Out::error("disconnected")
            HooksHandler::handle_disconnect
            reconnect()
         else
            # death before lost messages!
            raise e || "exception raised from #{from}"
         end
      end

      @client.add_message_callback do |msg|
         if Main.contacts.key?(msg.from)
            Main.contacts[msg.from].handle_message(msg)
         else
            Main.contacts.handle_non_contact_message(msg)
         end
      end

      Out::put("connecting...".yellow, true)

      @client.use_ssl = Config.[:use_ssl]
      @client.connect(Config.[:server], Config.[:port])

      loop do
         begin
            if !Config.[:password]
               print "#{Config.[:name]} account password: "
               system("stty -echo") # FIXME
               STDIN.read_nonblock(9999999) rescue nil
               Config.[:password] = gets.chomp
               system("stty echo")
               puts
            end

            @client.auth(Config.[:password])
            break
         rescue Jabber::ClientAuthenticationFailure
            Out::error("couldn't authenticate: wrong password?", false)
            Config.[:password] = nil
         end
      end

      @contacts = Contacts::Contacts.new(Jabber::Roster::Helper.new(@client))
      @client.send(@presence)
      @connected = true

      setup_after_connect()
      HooksHandler::handle_connect

      Out::put("connected!".green, true)
   ensure
      @monitor.exit
   end
end