Module: Lijab::Config

Defined in:
lib/lijab/config.rb

Constant Summary collapse

DEFAULT_OPTIONS =
[
   [:datetime_format, "%H:%M:%S", ["Time formatting (leave empty to disable timestamps)"]],
   [:history_datetime_format, "%Y-%b-%d %H:%M:%S"],
   [:autocomplete_online_first, true, 
    ["When completing contacts try to find matches for online contacts, and if none",
     "is found try to find matches on all of them. Otherwise always match every",
     "contact."]],
   [:show_status_changes, true, ["Show changes in contacts' status"]],
   [:show_groups_in_contact_list, false, ["Group contacts in contact list"]],
   [:ctrl_c_quits, false,
    ["ctrl+c quits the program if enabled, otherwise ctrl+c ignores whatever is",
     "typed and you get a clean prompt, and ctrl+d on a clean line exits lijab,",
     "terminal style."]],
   [:terminal_bell_on_message, true,
    ["Ring the terminal bell on incoming message.",
     "Useful for setting the urgent hint on the terminal window:",
     "Set as so in your ~/.Xdefaults, might have to run xrdb -merge ~/.Xdefaults afterwards",
     "XTerm*bellIsUrgent: true",
     "or",
     "URxvt*urgentOnBell: true",
     "or just look it up on your terminal's man page, don't be lazy."]],
   [:status_priorities,
    {:chat => 55, :available => 50, :away => 40, :xa => 30, :dnd => 20},
    ["Default priority for each status"]],
   [:aliases, {"/h" => "/history", "/exit" => "/quit"},
    ["Command aliases.",
     "<command_alias> : <existing_command>",
     "Commands can be overloaded.",
     "For instance /who could be redefined like so to sort by status by default.",
     "/who : /who status"]]
]
DEFAULT_ACCOUNTS_FILE =
%Q{
   # Accounts go here. Separate each one with ---
   # First one is the default.

   #---
   #:name : an_account                  # the account name
   #:jabberid : [email protected]/lijab  # the resource is optional
   #:password : frosk                   # optional, will prompt if not present
   #:server : localhost                 # optional, will use the jid domain if not present
   #:port : 5222                        # optional
   #:use_ssl : no                       # deprecated in jabber, but might help sometimes
   #:log : yes                          # yes|no ; default no

   #---
   #:name : another_account
   #:jabberid : [email protected]/lijab

   #---
   #:name : gmail_account
   #:jabberid : [email protected]/lijab
   #:server : talk.google.com
   ## might wanna try use_ssl if the normal settings don't work (e.g. in ubuntu afaik)
   ##:port : 5223
   ##:use_ssl : yes
   #:log : yes

}.gsub!(/^ */, '')

Class Method Summary collapse

Class Method Details

.accountObject

.basedirObject

.create_account_dirsObject



99
100
101
102
103
104
105
106
107
# File 'lib/lijab/config.rb', line 99

def 
   @accounts.each do |a|
      a[:dir] = File.join(@dirs[:accounts], @jid.strip.to_s)
      a[:log_dir] = File.join(a[:dir], "logs")
      a[:typed] = File.join(a[:dir], "typed_history")

      [:dir, :log_dir].each { |s| FileUtils.mkdir_p(a[s]) }
   end
end

.dirsObject

.dump_config_file(default = false, clobber = false) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lijab/config.rb', line 51

def dump_config_file(default=false, clobber=false)
   if !File.file?(@files[:config]) || clobber
      File.open(@files[:config], 'w') do |f|
         DEFAULT_OPTIONS.each do |a|
            if a[2]
               f.puts
               a[2].each { |l| f.puts("# #{l}") }
            end
            v = default ? a[1] : @opts[a[0]]
            f.puts(YAML.dump({a[0] => v})[5..-1].chomp)
         end
      end
   end
end

.filesObject

.init(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lijab/config.rb', line 7

def init(args)
   @opts = {}
   @dirs = {}
   @files = {}
   @accounts = []
   @account = nil

   setup_basedir(args[:basedir])
   read_accounts(args[:account])
   read_options()

   @jid = Jabber::JID.new("#{@account[:jabberid]}")
   @jid.resource ||= "lijab#{(0...5).map{rand(10).to_s}.join}"
   @account[:server] ||= @jid.domain

   ()
end

.jidObject

.optsObject

.read_accounts(account) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lijab/config.rb', line 66

def read_accounts()
   File.open(@files[:accounts]) do |f|
      YAML.load_documents(f) { |a| @accounts << a }
   end

   errors = []
   errors << "need at least one account!" if @accounts.empty?

   @accounts.each do |a|
      a[:port] ||= a[:use_ssl] ? 5223 : 5222

      errors << "account #{a} needs a name" unless a.key?(:name)
      errors << "account #{a[:name] || a} needs a jabberid" unless a.key?(:jabberid)
   end

   @account =  ? @accounts.find { |a| a[:name] == } : @accounts[0]

   errors << "no account with name #{} in #{@accounts_file}" if  && !@account

   errors.each do |e|
      STDERR.puts("#{File.basename($0)}: error: #{e}")
   end

   exit(1) unless errors.empty?
end

.read_optionsObject



92
93
94
95
96
97
# File 'lib/lijab/config.rb', line 92

def read_options
   # FIXME: error check / validate

   @opts = Hash[*DEFAULT_OPTIONS.collect { |a| [a[0], a[1]] }.flatten]
   @opts.merge!(YAML.load_file(@files[:config]))
end

.setup_basedir(basedir) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/lijab/config.rb', line 25

def setup_basedir(basedir)
   # xdg? meh
   #xdg = ENV["XDG_CONFIG_HOME"]
   #xdg && File.join(xdg, "lijab") ||

   @basedir = basedir ||
              ENV["LIJABDIR"] ||
              "~/.lijab"
   @basedir = File.expand_path(@basedir)

   unless File.directory?(@basedir)
      puts "Creating #{@basedir} with the default configs"
   end

   %w{accounts commands hooks}.each do |d|
      @dirs[d.to_sym] = path = File.join(@basedir, d)
      FileUtils.mkdir_p(path)
   end

   @files[:accounts] = path = File.join(@basedir, "accounts.yml")
   File.open(path, 'w') { |f| f.puts(DEFAULT_ACCOUNTS_FILE) } unless File.file?(path)

   @files[:config] = File.join(@basedir, "config.yml")
   dump_config_file(true)
end