xjab_jconf.c

Go to the documentation of this file.
00001 /*
00002  * $Id: xjab_jconf.c 4518 2008-07-28 15:39:28Z henningw $
00003  *
00004  * eXtended JABber module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 
00025 #include <string.h>
00026 #include <unistd.h>
00027 #include <stdio.h>
00028 #include <sys/types.h>
00029 #include <fcntl.h>
00030 
00031 #include "../../dprint.h"
00032 #include "../../mem/mem.h"
00033 
00034 #include "xjab_jconf.h"
00035 #include "xjab_base.h"
00036 
00037 /**
00038  *
00039  */
00040 xj_jconf xj_jconf_new(str *u)
00041 {
00042    xj_jconf jcf = NULL;
00043    
00044    if(!u || !u->s || u->len<=0)
00045       return NULL;
00046    jcf = (xj_jconf)pkg_malloc(sizeof(t_xj_jconf));
00047    if(jcf == NULL)
00048    {
00049       LM_DBG("no pkg memory.\n");
00050       return NULL;
00051    }
00052 
00053    jcf->uri.s = (char*)pkg_malloc((u->len+1)*sizeof(char));
00054    if(jcf->uri.s == NULL)
00055    {
00056       LM_DBG("no pkg memory!\n");
00057       pkg_free(jcf);
00058       return NULL;
00059    }
00060 
00061    strncpy(jcf->uri.s, u->s, u->len);
00062    jcf->uri.len = u->len;
00063    jcf->uri.s[jcf->uri.len] = 0;
00064 
00065    jcf->jcid = 0;
00066    jcf->status = XJ_JCONF_NULL;
00067    
00068    jcf->room.s = NULL;
00069    jcf->room.len = 0;
00070    jcf->server.s = NULL;
00071    jcf->server.len = 0;
00072    jcf->nick.s = NULL;
00073    jcf->nick.len = 0;
00074    
00075    return jcf;
00076 }
00077 
00078 /**
00079  *
00080  */
00081 int xj_jconf_init_sip(xj_jconf jcf, str *sid, char dl)
00082 {
00083    char *p, *p0;
00084    int n = 0;
00085    if(!jcf || !jcf->uri.s || jcf->uri.len <= 0 
00086          || !sid || !sid->s || sid->len <= 0)
00087       return -1;
00088 #ifdef XJ_EXTRA_DEBUG
00089    LM_DBG("parsing uri\n");
00090 #endif   
00091    p = jcf->uri.s;
00092    while(p<(jcf->uri.s + jcf->uri.len) && *p != '@') 
00093       p++;
00094    if(*p != '@')
00095       goto bad_format;
00096    p0 = p;
00097    
00098    while(p0 > jcf->uri.s)
00099    {
00100       p0--;
00101       if(*p0 == dl)
00102       {
00103          switch(n)
00104          {
00105             case 0:
00106                   jcf->server.s = p0+1;
00107                   jcf->server.len = p - jcf->server.s;
00108                break;
00109             case 1:
00110                   jcf->room.s = p0+1;
00111                   jcf->room.len = p - jcf->room.s;
00112                break;
00113             case 2:
00114                   jcf->nick.s = p0+1;
00115                   jcf->nick.len = p - jcf->nick.s;
00116                break;
00117          }
00118          n++;
00119          p = p0;
00120       }
00121    }
00122    if(n != 2 || p0 != jcf->uri.s)
00123       goto bad_format;
00124 
00125    if(p0 == jcf->uri.s && *p0 != dl)
00126    {
00127       jcf->nick.s = p0;
00128       jcf->nick.len = p - jcf->nick.s;
00129    }
00130    else
00131    {
00132       jcf->nick.s = p = sid->s;
00133       while(p < sid->s + sid->len && *p!='@')
00134       {
00135          if(*p == ':')
00136             jcf->nick.s = p+1;
00137          p++;
00138       }
00139       jcf->nick.len = p - jcf->nick.s;
00140    }
00141 
00142    jcf->jcid = xj_get_hash(&jcf->room, &jcf->server);
00143 #ifdef XJ_EXTRA_DEBUG
00144    LM_DBG("conference id=%d\n", jcf->jcid);
00145 #endif   
00146    return 0;
00147    
00148 bad_format:
00149    LM_ERR("failed to parse uri - bad format\n");
00150    return -2;
00151 }
00152 
00153 /**
00154  *
00155  */
00156 int xj_jconf_init_jab(xj_jconf jcf)
00157 {
00158    char *p, *p0;
00159    if(!jcf || !jcf->uri.s || jcf->uri.len <= 0)
00160       return -1;
00161 #ifdef XJ_EXTRA_DEBUG
00162    LM_DBG("parsing uri\n");
00163 #endif   
00164    p = jcf->uri.s;
00165    while(p<(jcf->uri.s + jcf->uri.len) && *p != '@') 
00166       p++;
00167    if(*p != '@' || p==jcf->uri.s)
00168       goto bad_format;
00169    
00170    p0 = p+1;
00171    
00172    while(p0 < ((jcf->uri.s + jcf->uri.len)) && *p0 != '/')
00173       p0++;
00174    
00175    jcf->server.s = p+1;
00176    jcf->server.len = p0 - jcf->server.s;
00177    jcf->room.s = jcf->uri.s;
00178    jcf->room.len = p - jcf->room.s;
00179    if(p0 < jcf->uri.s + jcf->uri.len)
00180    {
00181       jcf->nick.s = p0+1;
00182       jcf->nick.len = jcf->uri.s + jcf->uri.len - jcf->nick.s;
00183    }
00184    jcf->jcid = xj_get_hash(&jcf->room, &jcf->server);
00185 #ifdef XJ_EXTRA_DEBUG
00186    LM_DBG("conference id=%d\n", jcf->jcid);
00187 #endif
00188    return 0;
00189    
00190 bad_format:
00191    LM_ERR("failed to parse uri - bad format\n");
00192    return -2;
00193 }
00194 
00195 
00196 /**
00197  *
00198  */
00199 int xj_jconf_set_status(xj_jconf jcf, int s)
00200 {
00201    if(!jcf || !jcf->uri.s || jcf->uri.len <= 0)
00202       return -1;
00203    jcf->status = s;
00204    return 0;
00205 }
00206 
00207 /**
00208  *
00209  */
00210 int xj_jconf_cmp(void *a, void *b)
00211 {
00212    int n;
00213    if(a == NULL)
00214        return -1;
00215    if(b == NULL)
00216        return 1;
00217    
00218    // LM_DBG("comparing <%.*s> / <%.*s>\n",((str *)a)->len,
00219    //       ((str *)a)->s, ((str *)b)->len, ((str *)b)->s);
00220    if(((xj_jconf)a)->jcid < ((xj_jconf)b)->jcid)
00221          return -1;
00222    if(((xj_jconf)a)->jcid > ((xj_jconf)b)->jcid)
00223          return 1;
00224    
00225    if(((xj_jconf)a)->room.len < ((xj_jconf)b)->room.len)
00226       return -1;
00227    if(((xj_jconf)a)->room.len > ((xj_jconf)b)->room.len)
00228       return 1;
00229    
00230    if(((xj_jconf)a)->server.len < ((xj_jconf)b)->server.len)
00231       return -1;
00232    if(((xj_jconf)a)->server.len > ((xj_jconf)b)->server.len)
00233       return 1;
00234 
00235    n = strncmp(((xj_jconf)a)->room.s, ((xj_jconf)b)->room.s, 
00236                ((xj_jconf)a)->room.len);
00237    if(n<0)
00238       return -1;
00239    if(n>0)
00240       return 1;
00241    
00242    n = strncmp(((xj_jconf)a)->server.s, ((xj_jconf)b)->server.s, 
00243                ((xj_jconf)a)->server.len);
00244    if(n<0)
00245       return -1;
00246    if(n>0)
00247       return 1;
00248    
00249    return 0;
00250 }
00251 
00252 /**
00253  *
00254  */
00255 int xj_jconf_free(xj_jconf jcf)
00256 {
00257    if(!jcf)
00258       return 0;
00259    
00260    if(jcf->uri.s != NULL)
00261       pkg_free(jcf->uri.s);
00262    pkg_free(jcf);
00263    jcf = NULL;
00264    
00265    return 0;
00266 }
00267 
00268 /**
00269  *
00270  */
00271 int xj_jconf_check_addr(str *addr, char dl)
00272 {
00273    char *p;
00274    int i;
00275 
00276    if(!addr || !addr->s || addr->len <= 0)
00277       return -1;
00278 
00279    p = addr->s;
00280    i= 0;
00281    while((p < addr->s+addr->len) && *p != '@')
00282    {
00283       if(*p==dl)
00284          i++;
00285       p++;
00286    }
00287    if(i==2 && *p=='@')
00288       return 0;
00289 
00290    return -1;
00291 }
00292 

Generated on Fri May 25 00:00:35 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6