More consistent and clearer API. New approach for supporting jabberd2
[vcard2ldap.git] / src / v2l_main.c
blob0ac920b1f8d71989a6189e25deb947f9c89eed88
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <jabberd.h>
20 #include <v2l_config.h>
21 #include <v2l_conn.h>
22 #include <v2l_vcard.h>
24 #define LOG_ERROR_MEM log_error(ZONE, "Unable to allocate memory")
26 static result _v2l_packets_handler (instance i, dpacket p, void *args);
27 static short _v2l_check_attr_value (xmlnode node, char *attr_name,
28 char *attr_value);
29 static void _v2l_shutdown (void *arg);
32 * xdb_v2l (), module entry point.
34 void
35 xdb_v2l (instance i, xmlnode x)
37 xdbcache xc = NULL; /* for config request */
38 v2l_Config *self = NULL;
40 log_debug (ZONE, "Loading xdb_v2l");
42 self = pmalloco (i->p, sizeof (*self));
44 if (self == NULL)
46 return;
49 self->poolref = i->p;
51 /* Load config from xdb */
52 xc = xdb_cache (i);
53 self->config = xdb_get (xc, jid_new (xmlnode_pool (x), "config@-internal"),
54 "jabberd:xdb_v2l:config");
56 /* Parse config */
57 if (!v2l_config_init (self, self->config))
59 /* do no try to go on with a badly configured ldap component */
60 exit (1);
63 register_phandler (i, o_DELIVER, _v2l_packets_handler, (void *) self);
64 register_shutdown (_v2l_shutdown, (void *) self);
66 log_debug (ZONE, "xdb_v2l has been successfully initialized");
69 static void
70 _v2l_shutdown (void *arg)
72 v2l_Config *self = (v2l_Config *) arg;
74 v2l_free_allconn ();
76 /* free admin connection */
77 ldap_unbind (self->master_conn->ld);
78 pool_free (self->master_conn->p);
82 * Main callback. Handle xdb packets
84 static result
85 _v2l_packets_handler (instance i, dpacket p, void *args)
87 v2l_Config *self = (v2l_Config *) args;
88 v2l_LdapConn *user_conn;
89 dpacket dp;
91 if ((p == NULL) || (p->x == NULL) || (p->id == NULL))
93 log_error (ZONE, "malformed xdb packet");
94 return r_ERR;
97 if (_v2l_check_attr_value (p->x, "type", "error"))
99 xmlnode_free (p->x);
100 log_warn (ZONE, "xdb_v2l received an error packet");
101 return r_DONE;
104 if (!_v2l_check_attr_value (p->x, "ns", NS_VCARD))
106 log_warn (ZONE,
107 "xdb_v2l received a packet for other namespace than NS_VCARD");
108 return r_PASS;
111 user_conn = v2l_get_conn (self, p->id->user);
113 if (user_conn == NULL)
115 log_error (ZONE, "Unable to create connection for \"%s\" user",
116 p->id->user);
117 return r_ERR;
120 if (_v2l_check_attr_value (p->x, "type", "set"))
122 xmlnode child = xmlnode_get_firstchild (p->x);
124 if (v2l_vcard_set (self, user_conn, child) != 1)
126 return r_ERR;
129 else
131 xmlnode data = v2l_vcard_get (self, user_conn);
133 if (data)
135 xmlnode_insert_tag_node (p->x, data);
136 xmlnode_free (data);
140 /* making XML reply */
141 xmlnode_put_attrib (p->x, "type", "result");
142 xmlnode_put_attrib (p->x, "to", xmlnode_get_attrib (p->x, "from"));
143 xmlnode_put_attrib (p->x, "from", jid_full (p->id));
145 dp = dpacket_new (p->x);
147 if (dp == NULL)
149 LOG_ERROR_MEM;
150 return r_ERR;
153 deliver (dp, NULL);
155 return r_DONE;
158 static short
159 _v2l_check_attr_value (xmlnode node, char *attr_name, char *value)
161 if ((node == NULL) || (attr_name == NULL) || (value == NULL))
163 log_debug (ZONE, "_v2l_check_attr_value() parameters are not valid");
164 return 0;
167 return strncmp (xmlnode_get_attrib (node, attr_name), value,
168 sizeof (char) * strlen (value)) == 0;