Mon, 08 Feb 2021 22:56:45 +0100
clix.archive: Skip printing body-less messages
These are more and more common in archives, e.g. receipts, errors, OMEMO
etc.
48 | 1 | short_opts.i = "interactive"; |
2 | local split_jid = require "util.jid".split; | |
123
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
3 | local function printor(str) |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
4 | return function(ok, err) |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
5 | if ok then |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
6 | print(str) |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
7 | else |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
8 | print(err:get_error()); |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
9 | end |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
10 | end |
c61fadb9d4fa
clix.roster: Print error resplies
Kim Alvefur <zash@zash.se>
parents:
121
diff
changeset
|
11 | end |
48 | 12 | |
13 | return function (opts, arg) | |
14 | if opts.short_help then | |
15 | print("Show or edit your roster"); | |
16 | return; | |
17 | end | |
18 | if opts.help and not opts.interactive then | |
19 | return 0; | |
20 | end | |
21 | local function on_connect(conn) | |
22 | -- Fake socket object around stdin | |
23 | local stdin = { | |
24 | getfd = function () return 0; end; | |
25 | dirty = function (self) return false; end; | |
26 | settimeout = function () end; | |
27 | send = function (_, d) return #d, 0; end; | |
28 | close = function () end; | |
29 | receive = function (_, patt) | |
30 | local data = io.stdin:read(patt); | |
31 | if data == nil then | |
32 | conn:close(); | |
33 | end | |
34 | if opts.echo then | |
35 | io.write(data, patt == "*l" and "\n" or ""); | |
36 | end | |
37 | return data; | |
38 | end | |
39 | }; | |
40 | local commands = { | |
41 | quit = function(param) conn:close(); end; | |
42 | add = function(param) conn.roster:add_contact(param) end; | |
43 | sub = function(param) conn:send(verse.presence{to=param, type="subscribe"}); end; | |
61
7d62bff46601
clix.roster: Add command for unsubscribing and canceling subscription
Kim Alvefur <zash@zash.se>
parents:
59
diff
changeset
|
44 | unsub = function(param) conn:send(verse.presence{to=param, type="unsubscribe"}); end; |
48 | 45 | auth = function(param) conn:send(verse.presence{to=param, type="subscribed"}); end; |
61
7d62bff46601
clix.roster: Add command for unsubscribing and canceling subscription
Kim Alvefur <zash@zash.se>
parents:
59
diff
changeset
|
46 | unauth = function(param) conn:send(verse.presence{to=param, type="unsubscribed"}); end; |
48 | 47 | del = function(param) conn.roster:delete_contact(param) end; |
48 | setnick = function(param) | |
62
f455dad33efd
clix.roster: Use rest of line as last parameter for nick and group sub-commands.
Kim Alvefur <zash@zash.se>
parents:
61
diff
changeset
|
49 | local jid, nick = param:match("^(%S+)%s+(.+)"); |
48 | 50 | local item = conn.roster.items[jid]; |
51 | if not item then print("no jid "..jid); return; end | |
52 | conn.roster:add_contact(jid, nick, item.groups or {}, printor("saved")); | |
53 | end; | |
54 | addgroup = function(param) | |
62
f455dad33efd
clix.roster: Use rest of line as last parameter for nick and group sub-commands.
Kim Alvefur <zash@zash.se>
parents:
61
diff
changeset
|
55 | local jid, group = param:match("^(%S+)%s+(.+)"); |
48 | 56 | local item = conn.roster.items[jid]; |
57 | local groups = item.groups or {}; | |
58 | table.insert(groups, group); | |
59 | conn.roster:add_contact(jid, item.name, groups, printor("saved")); | |
60 | end; | |
61 | delgroup = function(param) | |
62
f455dad33efd
clix.roster: Use rest of line as last parameter for nick and group sub-commands.
Kim Alvefur <zash@zash.se>
parents:
61
diff
changeset
|
62 | local jid, group = param:match("^(%S+)%s+(.+)"); |
48 | 63 | local item = conn.roster.items[jid]; |
64 | local groups = item.groups; | |
65 | if not groups then return end; | |
66 | for i = 1,#groups do | |
67 | if groups[i] == group then | |
68 | table.remove(groups, i); | |
69 | break | |
70 | end | |
71 | end | |
72 | conn.roster:add_contact(jid, item.name, groups, printor("saved")); | |
73 | end; | |
66
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
74 | list = function(param) |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
75 | if param == "" then |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
76 | param = nil |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
77 | end |
48 | 78 | for jid, item in pairs(conn.roster.items) do |
56
4b5f9a50ae68
clix.roster: Fix nickname fallback
Kim Alvefur <zash@zash.se>
parents:
48
diff
changeset
|
79 | local name, host = item.name or split_jid(jid); |
66
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
80 | local groups = table.concat(item.groups or {}, ", "); |
121
896a25ed6b1b
clix.roster: Turn off pattern matching in the list command (thanks mva)
Kim Alvefur <zash@zash.se>
parents:
110
diff
changeset
|
81 | if not param or ( (name and name:find(param, 1, true)) or jid:find(param, 1, true) ) then |
91
7816f24d0fdc
clix.roster: Switch order of name and jid in the list command
Kim Alvefur <zash@zash.se>
parents:
66
diff
changeset
|
82 | print(jid, name or host, groups); |
66
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
83 | end |
48 | 84 | end |
85 | end; | |
86 | listgroups = function(param) | |
87 | local groups = {}; | |
88 | for jid, item in pairs(conn.roster.items) do | |
89 | for i = 1,#item.groups do | |
90 | groups[item.groups[i]] = ( groups[item.groups[i]] or 0 ) + 1; | |
91 | end | |
92 | end | |
93 | for group, size in pairs(groups) do | |
94 | print(group, size) | |
95 | end | |
96 | end; | |
97 | show = function(param) | |
98 | local item = conn.roster.items[param]; | |
99 | if not item then | |
100 | print("No such contact"); | |
101 | return; | |
102 | end | |
103 | ||
104 | for k,v in pairs(item) do | |
59
46cb6e772e31
clix.roster: Add a separator between groups in the show sub command
Kim Alvefur <zash@zash.se>
parents:
58
diff
changeset
|
105 | print(k,type(v) == "table" and table.concat(v, ", ") or v) |
48 | 106 | end |
107 | end; | |
110
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
108 | export = function() |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
109 | local stored_roster = { [false] = { version = conn.roster.ver } } |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
110 | for jid, item in pairs(conn.roster.items) do |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
111 | stored_roster[jid] = { |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
112 | name = item.name; |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
113 | subscription = item.subscription; |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
114 | groups = { unpack(item.groups) }; |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
115 | } |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
116 | end |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
117 | print("return "..require"util.serialization".serialize(stored_roster)); |
ded3938232f0
clix.roster: Add a sub-command that exports a prosody-style roster data structure
Kim Alvefur <zash@zash.se>
parents:
91
diff
changeset
|
118 | end |
48 | 119 | } |
63
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
120 | function commands.help () |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
121 | print("Roster commands"); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
122 | for cmd in pairs(commands) do |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
123 | print(" * " .. cmd); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
124 | end |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
125 | print("Most commands takes a JID as argument."); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
126 | end |
48 | 127 | local function on_incoming(stdin, text) |
128 | local cmd = text:match("^(%a*)"); | |
129 | local param = text:match("%s+(.*)", #cmd); | |
130 | if commands[cmd] then | |
131 | commands[cmd](param); | |
132 | end | |
133 | end | |
134 | stdin = require "net.server".wrapclient(stdin, "stdin", 0, { | |
135 | onincoming = on_incoming, ondisconnect = function () end | |
136 | }, "*l"); | |
137 | conn:add_plugin("roster"); | |
138 | conn.roster:fetch(function(roster) | |
139 | if not roster then | |
140 | print("There was an error fetching your roster"); | |
141 | conn:close() | |
142 | end | |
143 | ||
144 | if not opts.interactive then | |
58
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
145 | local firstcmd = commands[arg[1] or "list"]; |
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
146 | if firstcmd then |
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
147 | firstcmd(table.concat(arg, " ", 2, #arg)) |
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
148 | end |
48 | 149 | conn:close(); |
150 | end | |
151 | end); | |
152 | local function notif(e) | |
153 | return function(item) | |
154 | return print(("%s %s"):format((item.name and item.name .. " (" .. item.jid .. ")") or item.jid, e)); | |
155 | end | |
156 | end | |
157 | if opts.interactive then | |
158 | conn:hook("roster/item-added", notif("added")); | |
159 | conn:hook("roster/item-changed", notif("changed")); | |
160 | conn:hook("roster/item-removed", notif("removed")); | |
161 | end | |
57
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
162 | if opts.presence then |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
163 | conn:hook("presence", function(presence) |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
164 | if presence.attr.type == "subscribe" then |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
165 | print(("%s wants to subscribe to you"):format(presence.attr.from)); |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
166 | --elseif presence.attr.type == "subscribed" then |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
167 | --print(("%s subscribed to you?"):format(presence.attr.from)); |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
168 | end |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
169 | end) |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
170 | end |
48 | 171 | end |
172 | clix_connect(opts, on_connect); | |
173 | end |