00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00051 #include <stdlib.h>
00052 #include <jabberd.h>
00053 #include <v2l_config.h>
00054 #include <v2l_conn.h>
00055 #include <v2l_vcard.h>
00056
00057 #define LOG_ERROR_MEM log_error (ZONE, "Unable to allocate memory")
00058
00059 static result _v2l_packets_handler (instance i, dpacket p, void *args);
00060 static int _v2l_check_attr_value (xmlnode node, char *attr_name,
00061 char *attr_value);
00062 static void _v2l_shutdown (void *arg);
00063
00067 #ifdef __cplusplus
00068 extern "C"
00069 #endif
00070 void
00071 xdb_v2l (instance i, xmlnode x)
00072 {
00073 xdbcache xc = NULL;
00074 v2l_Config *self = NULL;
00075
00076 log_debug (ZONE, "Loading xdb_v2l");
00077
00078 self = (v2l_Config *) pmalloco (i->p, sizeof (*self));
00079
00080 if (self == NULL)
00081 {
00082 return;
00083 }
00084
00085 self->poolref = i->p;
00086
00087
00088 xc = xdb_cache (i);
00089 self->config = xdb_get (xc, jid_new (xmlnode_pool (x), "config@-internal"),
00090 "jabberd:xdb_v2l:config");
00091
00092
00093 if (!v2l_config_init (self, self->config))
00094 {
00095
00096 exit (1);
00097 }
00098
00099 register_phandler (i, o_DELIVER, _v2l_packets_handler, (void *) self);
00100 register_shutdown (_v2l_shutdown, (void *) self);
00101
00102 log_debug (ZONE, "xdb_v2l has been successfully initialized");
00103 }
00104
00108 static void
00109 _v2l_shutdown (void *arg)
00110 {
00111 v2l_config_shutdown ((v2l_Config *) arg);
00112 }
00113
00117 static result
00118 _v2l_packets_handler (instance i, dpacket p, void *args)
00119 {
00120 v2l_Config *self = (v2l_Config *) args;
00121 v2l_LdapConn *user_conn;
00122 dpacket dp;
00123
00124 if ((p == NULL) || (p->x == NULL) || (p->id == NULL))
00125 {
00126 log_error (ZONE, "malformed xdb packet");
00127 return r_ERR;
00128 }
00129
00130 if (_v2l_check_attr_value (p->x, "type", "error"))
00131 {
00132 xmlnode_free (p->x);
00133 log_warn (ZONE, "xdb_v2l received an error packet");
00134 return r_DONE;
00135 }
00136
00137 if (!_v2l_check_attr_value (p->x, "ns", NS_VCARD))
00138 {
00139 log_warn (ZONE,
00140 "xdb_v2l received a packet for other namespace than NS_VCARD");
00141 return r_PASS;
00142 }
00143
00144 user_conn = v2l_get_conn (self, p->id->user);
00145
00146 if (user_conn == NULL)
00147 {
00148 log_error (ZONE, "Unable to create connection for \"%s\" user",
00149 p->id->user);
00150 return r_ERR;
00151 }
00152
00153 if (_v2l_check_attr_value (p->x, "type", "set"))
00154 {
00155 xmlnode child = xmlnode_get_firstchild (p->x);
00156
00157 if (v2l_vcard_set (self, user_conn, child) != 1)
00158 {
00159 return r_ERR;
00160 }
00161 }
00162 else
00163 {
00164 xmlnode data = v2l_vcard_get (self, user_conn);
00165
00166 if (data)
00167 {
00168 xmlnode_insert_tag_node (p->x, data);
00169 xmlnode_free (data);
00170 }
00171 }
00172
00173
00174 xmlnode_put_attrib (p->x, "type", "result");
00175 xmlnode_put_attrib (p->x, "to", xmlnode_get_attrib (p->x, "from"));
00176 xmlnode_put_attrib (p->x, "from", jid_full (p->id));
00177
00178 dp = dpacket_new (p->x);
00179
00180 if (dp == NULL)
00181 {
00182 LOG_ERROR_MEM;
00183 return r_ERR;
00184 }
00185
00186 deliver (dp, NULL);
00187
00188 return r_DONE;
00189 }
00190
00191 static int
00192 _v2l_check_attr_value (xmlnode node, char *attr_name, char *value)
00193 {
00194 if ((node == NULL) || (attr_name == NULL) || (value == NULL))
00195 {
00196 log_debug (ZONE, "_v2l_check_attr_value () parameters are not valid");
00197 return 0;
00198 }
00199
00200 return strncmp (xmlnode_get_attrib (node, attr_name), value,
00201 strlen (value)) == 0;
00202 }