[b7d3cc34] | 1 | /* -------------------------------------------------------------------------- |
---|
| 2 | * |
---|
| 3 | * License |
---|
| 4 | * |
---|
| 5 | * The contents of this file are subject to the Jabber Open Source License |
---|
| 6 | * Version 1.0 (the "JOSL"). You may not copy or use this file, in either |
---|
| 7 | * source code or executable form, except in compliance with the JOSL. You |
---|
| 8 | * may obtain a copy of the JOSL at http://www.jabber.org/ or at |
---|
| 9 | * http://www.opensource.org/. |
---|
| 10 | * |
---|
| 11 | * Software distributed under the JOSL is distributed on an "AS IS" basis, |
---|
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the JOSL |
---|
| 13 | * for the specific language governing rights and limitations under the |
---|
| 14 | * JOSL. |
---|
| 15 | * |
---|
| 16 | * Copyrights |
---|
| 17 | * |
---|
| 18 | * Portions created by or assigned to Jabber.com, Inc. are |
---|
| 19 | * Copyright (c) 1999-2002 Jabber.com, Inc. All Rights Reserved. Contact |
---|
| 20 | * information for Jabber.com, Inc. is available at http://www.jabber.com/. |
---|
| 21 | * |
---|
| 22 | * Portions Copyright (c) 1998-1999 Jeremie Miller. |
---|
| 23 | * |
---|
| 24 | * Acknowledgements |
---|
| 25 | * |
---|
| 26 | * Special thanks to the Jabber Open Source Contributors for their |
---|
| 27 | * suggestions and support of Jabber. |
---|
| 28 | * |
---|
| 29 | * Alternatively, the contents of this file may be used under the terms of the |
---|
| 30 | * GNU General Public License Version 2 or later (the "GPL"), in which case |
---|
| 31 | * the provisions of the GPL are applicable instead of those above. If you |
---|
| 32 | * wish to allow use of your version of this file only under the terms of the |
---|
| 33 | * GPL and not to allow others to use your version of this file under the JOSL, |
---|
| 34 | * indicate your decision by deleting the provisions above and replace them |
---|
| 35 | * with the notice and other provisions required by the GPL. If you do not |
---|
| 36 | * delete the provisions above, a recipient may use your version of this file |
---|
| 37 | * under either the JOSL or the GPL. |
---|
| 38 | * |
---|
| 39 | * |
---|
| 40 | * --------------------------------------------------------------------------*/ |
---|
| 41 | |
---|
| 42 | #include "jabber.h" |
---|
| 43 | #include <glib.h> |
---|
| 44 | |
---|
| 45 | static jid jid_safe(jid id) |
---|
| 46 | { |
---|
| 47 | char *str; |
---|
| 48 | |
---|
| 49 | if(strlen(id->server) == 0 || strlen(id->server) > 255) |
---|
| 50 | return NULL; |
---|
| 51 | |
---|
| 52 | /* lowercase the hostname, make sure it's valid characters */ |
---|
| 53 | for(str = id->server; *str != '\0'; str++) |
---|
| 54 | { |
---|
| 55 | *str = tolower(*str); |
---|
| 56 | if(!(isalnum(*str) || *str == '.' || *str == '-' || *str == '_')) return NULL; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /* cut off the user */ |
---|
| 60 | if(id->user != NULL && strlen(id->user) > 64) |
---|
| 61 | id->user[64] = '\0'; |
---|
| 62 | |
---|
| 63 | /* check for low and invalid ascii characters in the username */ |
---|
| 64 | if(id->user != NULL) |
---|
| 65 | for(str = id->user; *str != '\0'; str++) |
---|
| 66 | if(*str <= 32 || *str == ':' || *str == '@' || *str == '<' || *str == '>' || *str == '\'' || *str == '"' || *str == '&') return NULL; |
---|
| 67 | |
---|
| 68 | return id; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | jid jid_new(pool p, char *idstr) |
---|
| 72 | { |
---|
| 73 | char *server, *resource, *type, *str; |
---|
| 74 | jid id; |
---|
| 75 | |
---|
| 76 | if(p == NULL || idstr == NULL || strlen(idstr) == 0) |
---|
| 77 | return NULL; |
---|
| 78 | |
---|
| 79 | /* user@server/resource */ |
---|
| 80 | |
---|
| 81 | str = pstrdup(p, idstr); |
---|
| 82 | |
---|
| 83 | id = pmalloco(p,sizeof(struct jid_struct)); |
---|
| 84 | id->p = p; |
---|
| 85 | |
---|
| 86 | resource = strstr(str,"/"); |
---|
| 87 | if(resource != NULL) |
---|
| 88 | { |
---|
| 89 | *resource = '\0'; |
---|
| 90 | ++resource; |
---|
| 91 | if(strlen(resource) > 0) |
---|
| 92 | id->resource = resource; |
---|
| 93 | }else{ |
---|
| 94 | resource = str + strlen(str); /* point to end */ |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | type = strstr(str,":"); |
---|
| 98 | if(type != NULL && type < resource) |
---|
| 99 | { |
---|
| 100 | *type = '\0'; |
---|
| 101 | ++type; |
---|
| 102 | str = type; /* ignore the type: prefix */ |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | server = strstr(str,"@"); |
---|
| 106 | if(server == NULL || server > resource) |
---|
| 107 | { /* if there's no @, it's just the server address */ |
---|
| 108 | id->server = str; |
---|
| 109 | }else{ |
---|
| 110 | *server = '\0'; |
---|
| 111 | ++server; |
---|
| 112 | id->server = server; |
---|
| 113 | if(strlen(str) > 0) |
---|
| 114 | id->user = str; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | return jid_safe(id); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | char *jid_full(jid id) |
---|
| 121 | { |
---|
| 122 | spool s; |
---|
| 123 | |
---|
| 124 | if(id == NULL) |
---|
| 125 | return NULL; |
---|
| 126 | |
---|
| 127 | /* use cached copy */ |
---|
| 128 | if(id->full != NULL) |
---|
| 129 | return id->full; |
---|
| 130 | |
---|
| 131 | s = spool_new(id->p); |
---|
| 132 | |
---|
| 133 | if(id->user != NULL) |
---|
| 134 | spooler(s, id->user,"@",s); |
---|
| 135 | |
---|
| 136 | spool_add(s, id->server); |
---|
| 137 | |
---|
| 138 | if(id->resource != NULL) |
---|
| 139 | spooler(s, "/",id->resource,s); |
---|
| 140 | |
---|
| 141 | id->full = spool_print(s); |
---|
| 142 | return id->full; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* local utils */ |
---|
| 146 | static int _jid_nullstrcmp(char *a, char *b) |
---|
| 147 | { |
---|
| 148 | if(a == NULL && b == NULL) return 0; |
---|
| 149 | if(a == NULL || b == NULL) return -1; |
---|
| 150 | return strcmp(a,b); |
---|
| 151 | } |
---|
| 152 | static int _jid_nullstrcasecmp(char *a, char *b) |
---|
| 153 | { |
---|
| 154 | if(a == NULL && b == NULL) return 0; |
---|
| 155 | if(a == NULL || b == NULL) return -1; |
---|
| 156 | return g_strcasecmp(a,b); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /* suggested by Anders Qvist <quest@valdez.netg.se> */ |
---|
| 160 | int jid_cmpx(jid a, jid b, int parts) |
---|
| 161 | { |
---|
| 162 | if(a == NULL || b == NULL) |
---|
| 163 | return -1; |
---|
| 164 | |
---|
| 165 | if(parts & JID_RESOURCE && _jid_nullstrcmp(a->resource, b->resource) != 0) return -1; |
---|
| 166 | if(parts & JID_USER && _jid_nullstrcasecmp(a->user, b->user) != 0) return -1; |
---|
| 167 | if(parts & JID_SERVER && _jid_nullstrcmp(a->server, b->server) != 0) return -1; |
---|
| 168 | |
---|
| 169 | return 0; |
---|
| 170 | } |
---|