Mon, 01 Apr 2013 21:25:31 +0200
clix.roster: Add a sub-command that exports a prosody-style roster data structure
48 | 1 | short_opts.i = "interactive"; |
2 | local split_jid = require "util.jid".split; | |
3 | local function printor(str) return function() print(str) end end | |
4 | ||
5 | return function (opts, arg) | |
6 | if opts.short_help then | |
7 | print("Show or edit your roster"); | |
8 | return; | |
9 | end | |
10 | if opts.help and not opts.interactive then | |
11 | return 0; | |
12 | end | |
13 | local function on_connect(conn) | |
14 | -- Fake socket object around stdin | |
15 | local stdin = { | |
16 | getfd = function () return 0; end; | |
17 | dirty = function (self) return false; end; | |
18 | settimeout = function () end; | |
19 | send = function (_, d) return #d, 0; end; | |
20 | close = function () end; | |
21 | receive = function (_, patt) | |
22 | local data = io.stdin:read(patt); | |
23 | if data == nil then | |
24 | conn:close(); | |
25 | end | |
26 | if opts.echo then | |
27 | io.write(data, patt == "*l" and "\n" or ""); | |
28 | end | |
29 | return data; | |
30 | end | |
31 | }; | |
32 | local commands = { | |
33 | quit = function(param) conn:close(); end; | |
34 | add = function(param) conn.roster:add_contact(param) end; | |
35 | 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
|
36 | unsub = function(param) conn:send(verse.presence{to=param, type="unsubscribe"}); end; |
48 | 37 | 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
|
38 | unauth = function(param) conn:send(verse.presence{to=param, type="unsubscribed"}); end; |
48 | 39 | del = function(param) conn.roster:delete_contact(param) end; |
40 | 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
|
41 | local jid, nick = param:match("^(%S+)%s+(.+)"); |
48 | 42 | local item = conn.roster.items[jid]; |
43 | if not item then print("no jid "..jid); return; end | |
44 | conn.roster:add_contact(jid, nick, item.groups or {}, printor("saved")); | |
45 | end; | |
46 | 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
|
47 | local jid, group = param:match("^(%S+)%s+(.+)"); |
48 | 48 | local item = conn.roster.items[jid]; |
49 | local groups = item.groups or {}; | |
50 | table.insert(groups, group); | |
51 | conn.roster:add_contact(jid, item.name, groups, printor("saved")); | |
52 | end; | |
53 | 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
|
54 | local jid, group = param:match("^(%S+)%s+(.+)"); |
48 | 55 | local item = conn.roster.items[jid]; |
56 | local groups = item.groups; | |
57 | if not groups then return end; | |
58 | for i = 1,#groups do | |
59 | if groups[i] == group then | |
60 | table.remove(groups, i); | |
61 | break | |
62 | end | |
63 | end | |
64 | conn.roster:add_contact(jid, item.name, groups, printor("saved")); | |
65 | end; | |
66
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
66 | list = function(param) |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
67 | if param == "" then |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
68 | param = nil |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
69 | end |
48 | 70 | 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
|
71 | 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
|
72 | local groups = table.concat(item.groups or {}, ", "); |
13d6d39f5952
clix.roster: List now lets you search on nickname and jid.
Kim Alvefur <zash@zash.se>
parents:
63
diff
changeset
|
73 | if not param or ( (name and name:match(param)) or jid:match(param) ) then |
91
7816f24d0fdc
clix.roster: Switch order of name and jid in the list command
Kim Alvefur <zash@zash.se>
parents:
66
diff
changeset
|
74 | 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
|
75 | end |
48 | 76 | end |
77 | end; | |
78 | listgroups = function(param) | |
79 | local groups = {}; | |
80 | for jid, item in pairs(conn.roster.items) do | |
81 | for i = 1,#item.groups do | |
82 | groups[item.groups[i]] = ( groups[item.groups[i]] or 0 ) + 1; | |
83 | end | |
84 | end | |
85 | for group, size in pairs(groups) do | |
86 | print(group, size) | |
87 | end | |
88 | end; | |
89 | show = function(param) | |
90 | local item = conn.roster.items[param]; | |
91 | if not item then | |
92 | print("No such contact"); | |
93 | return; | |
94 | end | |
95 | ||
96 | 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
|
97 | print(k,type(v) == "table" and table.concat(v, ", ") or v) |
48 | 98 | end |
99 | 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
|
100 | 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
|
101 | 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
|
102 | 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
|
103 | 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
|
104 | 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
|
105 | 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
|
106 | 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
|
107 | } |
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 | 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
|
109 | 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
|
110 | end |
48 | 111 | } |
63
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
112 | function commands.help () |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
113 | print("Roster commands"); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
114 | 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
|
115 | print(" * " .. cmd); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
116 | end |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
117 | 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
|
118 | end |
48 | 119 | local function on_incoming(stdin, text) |
120 | local cmd = text:match("^(%a*)"); | |
121 | local param = text:match("%s+(.*)", #cmd); | |
122 | if commands[cmd] then | |
123 | commands[cmd](param); | |
124 | end | |
125 | end | |
126 | stdin = require "net.server".wrapclient(stdin, "stdin", 0, { | |
127 | onincoming = on_incoming, ondisconnect = function () end | |
128 | }, "*l"); | |
129 | conn:add_plugin("roster"); | |
130 | conn.roster:fetch(function(roster) | |
131 | if not roster then | |
132 | print("There was an error fetching your roster"); | |
133 | conn:close() | |
134 | end | |
135 | ||
136 | 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
|
137 | 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
|
138 | if firstcmd then |
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
139 | 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
|
140 | end |
48 | 141 | conn:close(); |
142 | end | |
143 | end); | |
144 | local function notif(e) | |
145 | return function(item) | |
146 | return print(("%s %s"):format((item.name and item.name .. " (" .. item.jid .. ")") or item.jid, e)); | |
147 | end | |
148 | end | |
149 | if opts.interactive then | |
150 | conn:hook("roster/item-added", notif("added")); | |
151 | conn:hook("roster/item-changed", notif("changed")); | |
152 | conn:hook("roster/item-removed", notif("removed")); | |
153 | end | |
57
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
154 | if opts.presence then |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
155 | 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
|
156 | 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
|
157 | 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
|
158 | --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
|
159 | --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
|
160 | end |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
161 | end) |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
162 | end |
48 | 163 | end |
164 | clix_connect(opts, on_connect); | |
165 | end |