jabberd2  2.2.16
c2s/sm.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #include "c2s.h"
00022 
00024 static void _sm_generate_id(sess_t sess, bres_t res, const char *type) {
00025     char str[3094];   /* JID=3071 chars max + time = 12 chars max + type = 10 chars max + terminator = 3094 */
00026 
00027     snprintf(str, 3094, "%s%d%s", type, (int) time(NULL), jid_full(res->jid));
00028     str[3093] = '\0';
00029 
00030     shahash_r(str, res->sm_request);
00031 }
00032 
00034 static nad_t _sm_build_route(sess_t sess, bres_t res, const char *action, const char *target, char *id) {
00035     nad_t nad;
00036     int ns, ans;
00037 
00038     nad = nad_new();
00039 
00040     ns = nad_add_namespace(nad, uri_COMPONENT, NULL);
00041     nad_append_elem(nad, ns, "route", 0);
00042 
00043     nad_append_attr(nad, -1, "to", sess->smcomp?sess->smcomp:((char *) res->jid->domain));
00044     nad_append_attr(nad, -1, "from", sess->c2s->id);
00045 
00046     ans = nad_add_namespace(nad, uri_SESSION, "sc");
00047     nad_append_elem(nad, ans, "session", 1);
00048 
00049     if(res->c2s_id[0] != '\0')
00050         nad_append_attr(nad, ans, "c2s", res->c2s_id);
00051     if(res->sm_id[0] != '\0')
00052         nad_append_attr(nad, ans, "sm", res->sm_id);
00053 
00054     nad_append_attr(nad, -1, "action", action);
00055 
00056     if(target != NULL)
00057         nad_append_attr(nad, -1, "target", target);
00058     if(id != NULL)
00059         nad_append_attr(nad, -1, "id", id);
00060 
00061     log_debug(ZONE, "built new route nad for %s action %s target %s id %s", jid_full(res->jid), action, target, id);
00062 
00063     return nad;
00064 }
00065 
00066 void sm_start(sess_t sess, bres_t res) {
00067     _sm_generate_id(sess, res, "start");
00068 
00069     sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "start", jid_full(res->jid), res->sm_request));
00070 }
00071 
00072 void sm_end(sess_t sess, bres_t res) {
00073     sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "end", NULL, NULL));
00074 }
00075 
00076 void sm_create(sess_t sess, bres_t res) {
00077     _sm_generate_id(sess, res, "create");
00078 
00079     sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "create", jid_user(res->jid), res->sm_request));
00080 }
00081 
00082 void sm_delete(sess_t sess, bres_t res) {
00083     sx_nad_write(sess->c2s->router, _sm_build_route(sess, res, "delete", jid_user(res->jid), NULL));
00084 }
00085 
00086 void sm_packet(sess_t sess, bres_t res, nad_t nad) {
00087     int ns;
00088 
00089     ns = nad_add_namespace(nad, uri_COMPONENT, NULL);
00090     nad_wrap_elem(nad, 0, ns, "route");
00091 
00092     nad_set_attr(nad, 0, -1, "to", sess->smcomp?sess->smcomp:((char *) res->jid->domain), 0);
00093     nad_set_attr(nad, 0, -1, "from", sess->c2s->id, 0);
00094 
00095     ns = nad_append_namespace(nad, 1, uri_SESSION, "sc");
00096 
00097     nad_set_attr(nad, 1, ns, "c2s", res->c2s_id, 0);
00098     if(res->c2s_id[0] != '\0')
00099         nad_set_attr(nad, 1, ns, "sm", res->sm_id, 0);
00100 
00101     sx_nad_write(sess->c2s->router, nad);
00102 }