annotate mod_storage_ldap/ldap/vcard.lib.lua @ 809:1d51c5e38faa

Add LDAP plugin suite
author rob@hoelz.ro
date Sun, 02 Sep 2012 15:35:50 +0200
parents
children f160166612c2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
809
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
1 -- vim:sts=4 sw=4
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
2
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
3 -- Prosody IM
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
4 -- Copyright (C) 2008-2010 Matthew Wild
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
5 -- Copyright (C) 2008-2010 Waqas Hussain
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
6 -- Copyright (C) 2012 Rob Hoelz
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
7 --
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
8 -- This project is MIT/X11 licensed. Please see the
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
9 -- COPYING file in the source package for more information.
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
10 --
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
11
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
12 local st = require 'util.stanza';
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
13
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
14 local VCARD_NS = 'vcard-temp';
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
15
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
16 local builder_methods = {};
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
17
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
18 function builder_methods:addvalue(key, value)
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
19 self.vcard:tag(key):text(value):up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
20 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
21
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
22 function builder_methods:addregularfield(tagname, format_section)
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
23 local record = self.record;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
24 local format = self.format;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
25 local vcard = self.vcard;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
26
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
27 if not format[format_section] then
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
28 return;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
29 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
30
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
31 local tag = vcard:tag(tagname);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
32
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
33 for k, v in pairs(format[format_section]) do
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
34 tag:tag(string.upper(k)):text(record[v]):up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
35 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
36
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
37 vcard:up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
38 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
39
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
40 function builder_methods:addmultisectionedfield(tagname, format_section)
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
41 local record = self.record;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
42 local format = self.format;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
43 local vcard = self.vcard;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
44
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
45 if not format[format_section] then
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
46 return;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
47 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
48
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
49 for k, v in pairs(format[format_section]) do
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
50 local tag = vcard:tag(tagname);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
51
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
52 if type(k) == 'string' then
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
53 tag:tag(string.upper(k)):up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
54 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
55
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
56 for k2, v2 in pairs(v) do
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
57 if type(v2) == 'boolean' then
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
58 tag:tag(string.upper(k2)):up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
59 else
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
60 tag:tag(string.upper(k2)):text(record[v2]):up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
61 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
62 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
63
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
64 vcard:up();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
65 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
66 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
67
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
68 function builder_methods:build()
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
69 local record = self.record;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
70 local format = self.format;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
71
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
72 self:addvalue( 'VERSION', '2.0');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
73 self:addvalue( 'FN', record[format.displayname]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
74 self:addregularfield( 'N', 'name');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
75 self:addvalue( 'NICKNAME', record[format.nickname]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
76 self:addregularfield( 'PHOTO', 'photo');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
77 self:addvalue( 'BDAY', record[format.birthday]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
78 self:addmultisectionedfield('ADR', 'address');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
79 self:addvalue( 'LABEL', nil); -- we don't support LABEL...yet.
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
80 self:addmultisectionedfield('TEL', 'telephone');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
81 self:addmultisectionedfield('EMAIL', 'email');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
82 self:addvalue( 'JABBERID', record.jid);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
83 self:addvalue( 'MAILER', record[format.mailer]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
84 self:addvalue( 'TZ', record[format.timezone]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
85 self:addregularfield( 'GEO', 'geo');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
86 self:addvalue( 'TITLE', record[format.title]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
87 self:addvalue( 'ROLE', record[format.role]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
88 self:addregularfield( 'LOGO', 'logo');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
89 self:addvalue( 'AGENT', nil); -- we don't support AGENT...yet.
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
90 self:addregularfield( 'ORG', 'org');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
91 self:addvalue( 'CATEGORIES', nil); -- we don't support CATEGORIES...yet.
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
92 self:addvalue( 'NOTE', record[format.note]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
93 self:addvalue( 'PRODID', nil); -- we don't support PRODID...yet.
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
94 self:addvalue( 'REV', record[format.rev]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
95 self:addvalue( 'SORT-STRING', record[format.sortstring]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
96 self:addregularfield( 'SOUND', 'sound');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
97 self:addvalue( 'UID', record[format.uid]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
98 self:addvalue( 'URL', record[format.url]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
99 self:addvalue( 'CLASS', nil); -- we don't support CLASS...yet.
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
100 self:addregularfield( 'KEY', 'key');
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
101 self:addvalue( 'DESC', record[format.description]);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
102
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
103 return self.vcard;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
104 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
105
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
106 local function new_builder(params)
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
107 local vcard_tag = st.stanza('vCard', { xmlns = VCARD_NS });
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
108
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
109 local object = {
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
110 vcard = vcard_tag,
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
111 __index = builder_methods,
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
112 };
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
113
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
114 for k, v in pairs(params) do
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
115 object[k] = v;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
116 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
117
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
118 setmetatable(object, object);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
119
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
120 return object;
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
121 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
122
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
123 local _M = {};
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
124
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
125 function _M.create(params)
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
126 local builder = new_builder(params);
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
127
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
128 return builder:build();
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
129 end
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
130
1d51c5e38faa Add LDAP plugin suite
rob@hoelz.ro
parents:
diff changeset
131 return _M;