Tue, 17 May 2011 20:50:46 +0200
clix.roster: Add command for unsubscribing and canceling subscription
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) | |
41 | local jid, nick = param:match("^(%S+)%s+(%S+)"); | |
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) | |
47 | local jid, group = param:match("^(%S+)%s+(%S+)"); | |
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) | |
54 | local jid, group = param:match("^(%S+)%s+(%S+)"); | |
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 | } | |
95 | local function on_incoming(stdin, text) | |
96 | local cmd = text:match("^(%a*)"); | |
97 | local param = text:match("%s+(.*)", #cmd); | |
98 | if commands[cmd] then | |
99 | commands[cmd](param); | |
100 | end | |
101 | end | |
102 | stdin = require "net.server".wrapclient(stdin, "stdin", 0, { | |
103 | onincoming = on_incoming, ondisconnect = function () end | |
104 | }, "*l"); | |
105 | conn:add_plugin("roster"); | |
106 | conn.roster:fetch(function(roster) | |
107 | if not roster then | |
108 | print("There was an error fetching your roster"); | |
109 | conn:close() | |
110 | end | |
111 | ||
112 | 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
|
113 | 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
|
114 | if firstcmd then |
d5d9f7dffc1f
clix.roster: Don't run any command by default when --interactive
Kim Alvefur <zash@zash.se>
parents:
57
diff
changeset
|
115 | 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
|
116 | end |
48 | 117 | conn:close(); |
118 | end | |
119 | end); | |
120 | local function notif(e) | |
121 | return function(item) | |
122 | return print(("%s %s"):format((item.name and item.name .. " (" .. item.jid .. ")") or item.jid, e)); | |
123 | end | |
124 | end | |
125 | if opts.interactive then | |
126 | conn:hook("roster/item-added", notif("added")); | |
127 | conn:hook("roster/item-changed", notif("changed")); | |
128 | conn:hook("roster/item-removed", notif("removed")); | |
129 | end | |
57
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
130 | if opts.presence then |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
131 | 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
|
132 | 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
|
133 | 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
|
134 | --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
|
135 | --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
|
136 | end |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
137 | end) |
830baad98c9a
clix.roster: Show incomming subscription requests when --presence is used
Kim Alvefur <zash@zash.se>
parents:
56
diff
changeset
|
138 | end |
48 | 139 | end |
140 | clix_connect(opts, on_connect); | |
141 | end |