comparison mod_vcard_command/mod_vcard_command.lua @ 3025:5b8983e100da

mod_vcard_command: a simplistic way to get and set vcard data for registered users Use "prosodyctl get user@example.com" to print the XML to stdout, and "prosodyctl set user@example.com /file/name" to set the vcard info from an xml file.
author Stefan `Sec` Zehl <sec@42.org>
date Tue, 08 Oct 2013 16:38:40 +0000
parents
children b2c9b832612b
comparison
equal deleted inserted replaced
3024:f54c80404ad3 3025:5b8983e100da
1 -----------------------------------------------------------
2 -- mod_vcard_command: Manage vcards through prosodyctl
3 -- version 0.02
4 -----------------------------------------------------------
5 -- Copyright (C) 2013 Stefan `Sec` Zehl
6 --
7 -- This project is MIT/X11 licensed. Please see the
8 -- COPYING file in the source package for more information.
9 -----------------------------------------------------------
10
11 if not rawget(_G, "prosodyctl") then
12 module:log("error", "Do not load this module in Prosody");
13 module.host = "*";
14 return;
15 end
16
17
18 -- Workaround for lack of util.startup...
19 _G.bare_sessions = _G.bare_sessions or {};
20
21 local storagemanager = require "core.storagemanager";
22 local datamanager = require "util.datamanager";
23 local xml = require "util.xml";
24 local jid = require "util.jid";
25 local warn = prosodyctl.show_warning;
26 local st = require "util.stanza"
27 -- local vcards = module:open_store("vcard");
28
29 -- Print anything - including nested tables
30 function table_print (tt, indent, done)
31 done = done or {}
32 indent = indent or 0
33 if type(tt) == "table" then
34 for key, value in pairs (tt) do
35 io.write(string.rep (" ", indent)) -- indent it
36 if type (value) == "table" and not done [value] then
37 done [value] = true
38 io.write(string.format("[%s] => table\n", tostring (key)));
39 io.write(string.rep (" ", indent+4)) -- indent it
40 io.write("(\n");
41 table_print (value, indent + 7, done)
42 io.write(string.rep (" ", indent+4)) -- indent it
43 io.write(")\n");
44 else
45 io.write(string.format("[%s] => %s\n",
46 tostring (key), tostring(value)))
47 end
48 end
49 else
50 io.write(tt .. "\n")
51 end
52 end
53
54 -- Make a *one-way* subscription. User will see when contact is online,
55 -- contact will not see when user is online.
56 function vcard_get(user_jid)
57 local user_username, user_host = jid.split(user_jid);
58 if not hosts[user_host] then
59 warn("The host '%s' is not configured for this server.", user_host);
60 return;
61 end
62 storagemanager.initialize_host(user_host);
63 local vCard;
64 vCard = st.deserialize(datamanager.load(user_username, user_host, "vcard"));
65 if vCard then
66 print(vCard);
67 else
68 warn("The user '%s' has no vCard configured.",user_jid);
69 end
70 end
71
72 function vcard_set(user_jid, file)
73 local user_username, user_host = jid.split(user_jid);
74 if not hosts[user_host] then
75 warn("The host '%s' is not configured for this server.", user_host);
76 return;
77 end
78 storagemanager.initialize_host(user_host);
79 local f = io.input(file);
80 local xmldata=io.read("*all");
81 io.close(f);
82
83 local vCard=st.preserialize(xml.parse(xmldata));
84
85 if vCard then
86 datamanager.store(user_username, user_host, "vcard", vCard);
87 else
88 warn("Could not parse the file.");
89 end
90 end
91
92 function vcard_delete(user_jid)
93 local user_username, user_host = jid.split(user_jid);
94 if not hosts[user_host] then
95 warn("The host '%s' is not configured for this server.", user_host);
96 return;
97 end
98 storagemanager.initialize_host(user_host);
99 datamanager.store(user_username, user_host, "vcard", nil);
100 end
101
102 function module.command(arg)
103 local command = arg[1];
104 if not command then
105 warn("Valid subcommands: get | set | delete ");
106 return 0;
107 end
108 table.remove(arg, 1);
109 if command == "get" then
110 vcard_get(arg[1]);
111 return 0;
112 elseif command == "set" then
113 vcard_set(arg[1], arg[2]);
114 return 0;
115 elseif command == "delete" then
116 vcard_delete(arg[1]);
117 return 0;
118 else
119 warn("Unknown command: %s", command);
120 return 1;
121 end
122 return 0;
123 end