Sun, 14 Aug 2011 21:27:41 +0200
clix.roster: Add a help command to list available sub-commands.
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 | list = function() | |
67 | 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
|
68 | local name, host = item.name or split_jid(jid); |
48 | 69 | print(name or host, jid, table.concat(item.groups or {}, ", ")); |
70 | end | |
71 | end; | |
72 | listgroups = function(param) | |
73 | local groups = {}; | |
74 | for jid, item in pairs(conn.roster.items) do | |
75 | for i = 1,#item.groups do | |
76 | groups[item.groups[i]] = ( groups[item.groups[i]] or 0 ) + 1; | |
77 | end | |
78 | end | |
79 | for group, size in pairs(groups) do | |
80 | print(group, size) | |
81 | end | |
82 | end; | |
83 | show = function(param) | |
84 | local item = conn.roster.items[param]; | |
85 | if not item then | |
86 | print("No such contact"); | |
87 | return; | |
88 | end | |
89 | ||
90 | 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
|
91 | print(k,type(v) == "table" and table.concat(v, ", ") or v) |
48 | 92 | end |
93 | end; | |
94 | } | |
63
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
95 | function commands.help () |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
96 | print("Roster commands"); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
97 | 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
|
98 | print(" * " .. cmd); |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
99 | end |
bbbef9f4a798
clix.roster: Add a help command to list available sub-commands.
Kim Alvefur <zash@zash.se>
parents:
62
diff
changeset
|
100 | 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
|
101 | end |
48 | 102 | local function on_incoming(stdin, text) |
103 | local cmd = text:match("^(%a*)"); | |
104 | local param = text:match("%s+(.*)", #cmd); | |
105 | if commands[cmd] then | |
106 | commands[cmd](param); | |
107 | end | |
108 | end | |
109 | stdin = require "net.server".wrapclient(stdin, "stdin", 0, { | |
110 | onincoming = on_incoming, ondisconnect = function () end | |
111 | }, "*l"); | |
112 | conn:add_plugin("roster"); | |
113 | conn.roster:fetch(function(roster) | |
114 | if not roster then | |
115 | print("There was an error fetching your roster"); | |
116 | conn:close() | |
117 | end | |
118 | ||
119 | 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
|
120 | 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
|
121 | if firstcmd then |
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
122 | 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
|
123 | end |
48 | 124 | conn:close(); |
125 | end | |
126 | end); | |
127 | local function notif(e) | |
128 | return function(item) | |
129 | return print(("%s %s"):format((item.name and item.name .. " (" .. item.jid .. ")") or item.jid, e)); | |
130 | end | |
131 | end | |
132 | if opts.interactive then | |
133 | conn:hook("roster/item-added", notif("added")); | |
134 | conn:hook("roster/item-changed", notif("changed")); | |
135 | conn:hook("roster/item-removed", notif("removed")); | |
136 | end | |
57
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
137 | if opts.presence then |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
138 | 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
|
139 | 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
|
140 | 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
|
141 | --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
|
142 | --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
|
143 | end |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
144 | end) |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
145 | end |
48 | 146 | end |
147 | clix_connect(opts, on_connect); | |
148 | end |