Jabber WindowGram Client (JWGC)

Introduction Screenshots Installation Downloads
Documentation Browse Source Resources Project Site

Stable Version
-none-

Latest Version
beta5



Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

jid.c

Go to the documentation of this file.
00001 /*
00002  * --------------------------------------------------------------------------
00003  * 
00004  * License
00005  * 
00006  * The contents of this file are subject to the Jabber Open Source License
00007  * Version 1.0 (the "JOSL").  You may not copy or use this file, in either
00008  * source code or executable form, except in compliance with the JOSL. You
00009  * may obtain a copy of the JOSL at http://www.jabber.org/ or at
00010  * http://www.opensource.org/.
00011  * 
00012  * Software distributed under the JOSL is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the JOSL for
00014  * the specific language governing rights and limitations under the JOSL.
00015  * 
00016  * Copyrights
00017  * 
00018  * Portions created by or assigned to Jabber.com, Inc. are Copyright (c)
00019  * 1999-2002 Jabber.com, Inc.  All Rights Reserved.  Contact information for
00020  * Jabber.com, Inc. is available at http://www.jabber.com/.
00021  * 
00022  * Portions Copyright (c) 1998-1999 Jeremie Miller.
00023  * 
00024  * Acknowledgements
00025  * 
00026  * Special thanks to the Jabber Open Source Contributors for their suggestions
00027  * and support of Jabber.
00028  * 
00029  * Alternatively, the contents of this file may be used under the terms of the
00030  * GNU General Public License Version 2 or later (the "GPL"), in which case
00031  * the provisions of the GPL are applicable instead of those above.  If you
00032  * wish to allow use of your version of this file only under the terms of the
00033  * GPL and not to allow others to use your version of this file under the
00034  * JOSL, indicate your decision by deleting the provisions above and replace
00035  * them with the notice and other provisions required by the GPL.  If you do
00036  * not delete the provisions above, a recipient may use your version of this
00037  * file under either the JOSL or the GPL.
00038  * 
00039  * --------------------------------------------------------------------------
00040  */
00041 
00042 /* $Id: jid.c,v 1.9 2004/01/30 03:56:28 jadestorm Exp $ */
00043 
00044 #include "libjabber.h"
00045 
00046 jid 
00047 jid_safe(jid id)
00048 {
00049         unsigned char *str;
00050 
00051         if (strlen(id->server) == 0 || strlen(id->server) > 255)
00052                 return NULL;
00053 
00054         /* lowercase the hostname, make sure it's valid characters */
00055         for (str = id->server; *str != '\0'; str++) {
00056                 *str = tolower(*str);
00057                 if (!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_'))
00058                         return NULL;
00059         }
00060 
00061         /* cut off the user */
00062         if (id->user != NULL && strlen(id->user) > 64)
00063                 id->user[64] = '\0';
00064 
00065         /* check for low and invalid ascii characters in the username */
00066         if (id->user != NULL)
00067                 for (str = id->user; *str != '\0'; str++)
00068                         if (*str <= 32 || *str == ':' || *str == '@' || *str == '<' || *str == '>' || *str == '\'' || *str == '"' || *str == '&')
00069                                 return NULL;
00070 
00071         return id;
00072 }
00073 
00074 jid 
00075 jid_new(xode_pool p, char *idstr)
00076 {
00077         char *server, *resource, *type, *str;
00078         jid id;
00079 
00080         if (p == NULL || idstr == NULL || strlen(idstr) == 0)
00081                 return NULL;
00082 
00083         /* user@server/resource */
00084 
00085         str = xode_pool_strdup(p, idstr);
00086 
00087         id = xode_pool_malloco(p, sizeof(struct jid_struct));
00088         id->p = p;
00089         id->full = xode_pool_strdup(p, idstr);
00090 
00091         resource = strstr(str, "/");
00092         if (resource != NULL) {
00093                 *resource = '\0';
00094                 ++resource;
00095                 if (strlen(resource) > 0)
00096                         id->resource = resource;
00097         }
00098         else {
00099                 resource = str + strlen(str);   /* point to end */
00100         }
00101 
00102         type = strstr(str, ":");
00103         if (type != NULL && type < resource) {
00104                 *type = '\0';
00105                 ++type;
00106                 str = type;     /* ignore the type: prefix */
00107         }
00108 
00109         server = strstr(str, "@");
00110         if (server == NULL || server > resource) {      /* if there's no @, it's
00111                                                          * just the server
00112                                                          * address */
00113                 id->server = str;
00114         }
00115         else {
00116                 *server = '\0';
00117                 ++server;
00118                 id->server = server;
00119                 if (strlen(str) > 0)
00120                         id->user = str;
00121         }
00122 
00123         return jid_safe(id);
00124 }
00125 
00126 void 
00127 jid_set(jid id, char *str, int item)
00128 {
00129         char *old;
00130 
00131         if (id == NULL)
00132                 return;
00133 
00134         /* invalidate the cached copy */
00135         id->full = NULL;
00136 
00137         switch (item) {
00138                 case JID_RESOURCE:
00139                         if (str != NULL && strlen(str) != 0)
00140                                 id->resource = xode_pool_strdup(id->p, str);
00141                         else
00142                                 id->resource = NULL;
00143                         break;
00144                 case JID_USER:
00145                         old = id->user;
00146                         if (str != NULL && strlen(str) != 0)
00147                                 id->user = xode_pool_strdup(id->p, str);
00148                         else
00149                                 id->user = NULL;
00150                         if (jid_safe(id) == NULL)
00151                                 id->user = old; /* revert if invalid */
00152                         break;
00153                 case JID_SERVER:
00154                         old = id->server;
00155                         id->server = xode_pool_strdup(id->p, str);
00156                         if (jid_safe(id) == NULL)
00157                                 id->server = old;       /* revert if invalid */
00158                         break;
00159         }
00160 
00161 }
00162 
00163 char *
00164 jid_full(jid id)
00165 {
00166         xode_spool s;
00167 
00168         if (id == NULL)
00169                 return NULL;
00170 
00171         /* use cached copy */
00172         if (id->full != NULL)
00173                 return id->full;
00174 
00175         s = xode_spool_newfrompool(id->p);
00176 
00177         if (id->user != NULL)
00178                 xode_spooler(s, id->user, "@", s);
00179 
00180         xode_spool_add(s, id->server);
00181 
00182         if (id->resource != NULL)
00183                 xode_spooler(s, "/", id->resource, s);
00184 
00185         id->full = xode_spool_tostr(s);
00186         return id->full;
00187 }
00188 
00189 /*
00190  * parses a /resource?name=value&foo=bar into an xode representing <resource
00191  * name="value" foo="bar"/>
00192  */
00193 xode 
00194 jid_xres(jid id)
00195 {
00196         char *cur, *qmark, *amp, *eq;
00197         xode x;
00198 
00199         if (id == NULL || id->resource == NULL)
00200                 return NULL;
00201 
00202         cur = xode_pool_strdup(id->p, id->resource);
00203         qmark = strstr(cur, "?");
00204         if (qmark == NULL)
00205                 return NULL;
00206         *qmark = '\0';
00207         qmark++;
00208 
00209         x = xode_new_frompool(id->p, cur);
00210 
00211         cur = qmark;
00212         while (cur != '\0') {
00213                 eq = strstr(cur, "=");
00214                 if (eq == NULL)
00215                         break;
00216                 *eq = '\0';
00217                 eq++;
00218 
00219                 amp = strstr(eq, "&");
00220                 if (amp != NULL) {
00221                         *amp = '\0';
00222                         amp++;
00223                 }
00224 
00225                 xode_put_attrib(x, cur, eq);
00226 
00227                 if (amp != NULL)
00228                         cur = amp;
00229                 else
00230                         break;
00231         }
00232 
00233         return x;
00234 }
00235 
00236 /* local utils */
00237 int 
00238 _jid_nullstrcmp(char *a, char *b)
00239 {
00240         if (a == NULL && b == NULL)
00241                 return 0;
00242         if (a == NULL || b == NULL)
00243                 return -1;
00244         return strcmp(a, b);
00245 }
00246 int 
00247 _jid_nullstrcasecmp(char *a, char *b)
00248 {
00249         if (a == NULL && b == NULL)
00250                 return 0;
00251         if (a == NULL || b == NULL)
00252                 return -1;
00253         return strcasecmp(a, b);
00254 }
00255 
00256 int 
00257 jid_cmp(jid a, jid b)
00258 {
00259         if (a == NULL || b == NULL)
00260                 return -1;
00261 
00262         if (_jid_nullstrcmp(a->resource, b->resource) != 0)
00263                 return -1;
00264         if (_jid_nullstrcasecmp(a->user, b->user) != 0)
00265                 return -1;
00266         if (_jid_nullstrcmp(a->server, b->server) != 0)
00267                 return -1;
00268 
00269         return 0;
00270 }
00271 
00272 /* suggested by Anders Qvist <quest@valdez.netg.se> */
00273 int 
00274 jid_cmpx(jid a, jid b, int parts)
00275 {
00276         if (a == NULL || b == NULL)
00277                 return -1;
00278 
00279         if (parts & JID_RESOURCE && _jid_nullstrcmp(a->resource, b->resource) != 0)
00280                 return -1;
00281         if (parts & JID_USER && _jid_nullstrcasecmp(a->user, b->user) != 0)
00282                 return -1;
00283         if (parts & JID_SERVER && _jid_nullstrcmp(a->server, b->server) != 0)
00284                 return -1;
00285 
00286         return 0;
00287 }
00288 
00289 /* makes a copy of b in a's pool, requires a valid a first! */
00290 jid 
00291 jid_append(jid a, jid b)
00292 {
00293         jid next;
00294 
00295         if (a == NULL)
00296                 return NULL;
00297 
00298         if (b == NULL)
00299                 return a;
00300 
00301         next = a;
00302         while (next != NULL) {
00303                 /* check for dups */
00304                 if (jid_cmp(next, b) == 0)
00305                         break;
00306                 if (next->next == NULL)
00307                         next->next = jid_new(a->p, jid_full(b));
00308                 next = next->next;
00309         }
00310         return a;
00311 }
00312 
00313 xode 
00314 jid_nodescan(jid id, xode x)
00315 {
00316         xode cur;
00317         xode_pool p;
00318         jid tmp;
00319 
00320         if (id == NULL || xode_get_firstchild(x) == NULL)
00321                 return NULL;
00322 
00323         p = xode_pool_new();
00324         for (cur = xode_get_firstchild(x); cur != NULL; cur = xode_get_nextsibling(cur)) {
00325                 if (xode_get_type(cur) != XODE_TYPE_TAG)
00326                         continue;
00327 
00328                 tmp = jid_new(p, xode_get_attrib(cur, "jid"));
00329                 if (tmp == NULL)
00330                         continue;
00331 
00332                 if (jid_cmp(tmp, id) == 0)
00333                         break;
00334         }
00335         xode_pool_free(p);
00336 
00337         return cur;
00338 }
00339 
00340 jid 
00341 jid_user(jid a)
00342 {
00343         jid ret;
00344 
00345         if (a == NULL || a->resource == NULL)
00346                 return a;
00347 
00348         ret = xode_pool_malloco(a->p, sizeof(struct jid_struct));
00349         ret->p = a->p;
00350         ret->user = a->user;
00351         ret->server = a->server;
00352 
00353         return ret;
00354 }


Last updated at Tue Dec 18 21:07:42 PST 2007. This site and project hosted by...SourceForge.net Logo
Source Perspective by Fisheye