Comparison

clix.lua @ 171:eb7625161d76

clix.lua: Read resouce from config file.
author Trần H. Trung <xmpp:trần.h.trung@trung.fun>
date Fri, 07 Jul 2023 22:11:47 +0700
parent 169:4e67d34c1298
child 173:9b36119c9900
comparison
equal deleted inserted replaced
170:0d561f921c13 171:eb7625161d76
8 local verse = require "verse".init "client" 8 local verse = require "verse".init "client"
9 9
10 -- Global to allow commands to add to it 10 -- Global to allow commands to add to it
11 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type", 11 short_opts = { v = "verbose", q = "quiet", t = "to", f = "from", e = "type",
12 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom", i = "interactive", 12 a = "account", p = "password", r = "resource", o = "presence", c = "chatroom", i = "interactive",
13 f = "file" } 13 f = "file" }
14 14
15 15
16 local commands = "adhoc archive avatar bounce export mirror moderate ping\ 16 local commands = "adhoc archive avatar bounce export mirror moderate ping\
17 presence publish_atom raw receive roster send sendfile sendfilecontent vcard\ 17 presence publish_atom raw receive roster send sendfile sendfilecontent vcard\
18 version watch_pep" 18 version watch_pep"
25 end 25 end
26 return 0; 26 return 0;
27 end 27 end
28 28
29 local accounts = { default = {} }; 29 local accounts = { default = {} };
30 local current_account; 30 local current_account, config_file, err;
31 31
32 local config_path = (os.getenv("XDG_CONFIG_HOME") or (os.getenv("HOME").."/.config")); 32 local home_dir = (os.getenv("XDG_CONFIG_HOME") or os.getenv("HOME"));
33 local config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix"); 33 if home_dir then
34 34 local config_path = (home_dir.."/.config");
35 if not config_file then 35 config_file, err = io.open(config_path.."/.clixrc") or io.open(config_path.."/.clix");
36 print("Unable to open config file... looked for "..config_path.."/.clixrc"); 36 if not config_file then
37 if err then print(err); end 37 print("Unable to open config file... looked for "..config_path.."/.clixrc");
38 os.exit(1); 38 if err then print(err); end
39 os.exit(1);
40 end
41 else
42 config_file, err = io.open("/etc/clixrc") or io.open("/etc/clix");
43 if not config_file then
44 print("Could not find any config file.");
45 if err then print(err); end
46 os.exit(1);
47 end
39 end 48 end
40 49
41 for line in config_file:lines() do 50 for line in config_file:lines() do
42 line = line:match("^%s*(.-)%s*$"); 51 line = line:match("^%s*(.-)%s*$");
43 if line:match("^%[") then 52 if line:match("^%[") then
129 138
130 -- Connect! 139 -- Connect!
131 conn:connect_client(account.jid, account.password, clientkey, serverkey); 140 conn:connect_client(account.jid, account.password, clientkey, serverkey);
132 if type(opts.resource) == "string" then 141 if type(opts.resource) == "string" then
133 conn.resource = opts.resource; 142 conn.resource = opts.resource;
134 end 143 elseif account.resource then
135 144 conn.resource = account.resource;
145 end
146
136 local ok, ret = pcall(verse.loop); 147 local ok, ret = pcall(verse.loop);
137 if not ok and not ret:match("interrupted!$") then 148 if not ok and not ret:match("interrupted!$") then
138 io.stderr:write("Fatal error: ", ret, "\n"); 149 io.stderr:write("Fatal error: ", ret, "\n");
139 return 1; 150 return 1;
140 end 151 end