root/trunk/src/jid.c

Revision 680, 5.1 kB (checked in by jajcus, 3 years ago)

- dates in the copyright header updated

Line 
1 /* $Id: jid.c,v 1.14 2004/04/13 17:44:07 jajcus Exp $ */
2
3 /*
4  *  (C) Copyright 2002-2006 Jacek Konieczny [jajcus(a)jajcus,net]
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License Version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "ggtrans.h"
21 #include <stdio.h>
22 #include <stringprep.h>
23 #include <idna.h>
24 #include "jid.h"
25 #include "jabber.h"
26 #include "ctype.h"
27 #include "debug.h"
28
29 int jid_is_my(const char *jid){
30 int i,at,slash;
31 int just_digits;
32 int ret;
33 char *inbuf,*outbuf;
34
35         if (!jid) return 0;
36
37         just_digits=1;
38         at=-1;
39         slash=-1;
40         /* split into parts, check for numeric username */
41         for(i=0;jid[i];i++){
42                 if (jid[i]=='/' && slash<0) slash=i;
43                 else if (jid[i]=='@')
44                         if (!just_digits){
45                                 debug(L_("Non-digits before '@' in jid: %s"),jid);
46                                 return 0;
47                         }
48                         else at=i;
49                 else if (!isdigit(jid[i]) && just_digits) just_digits=0;
50         }
51
52         if (slash>=0 && slash<at){
53                 g_warning(N_("slash<at (%i<%i) in %s"),slash,at,jid);
54                 return 0;
55         }
56
57         if ((slash<0 && strlen(jid+at+1)>1023) || slash-at-1>1023){
58                 g_warning(N_("JID domain too long"));
59                 return 0;
60         }
61
62         if (slash<0)
63                 inbuf=g_strdup(jid+at+1);
64         else
65                 inbuf=g_strndup(jid+at+1,slash-at-1);
66
67         ret=idna_to_unicode_8z8z(inbuf,&outbuf,IDNA_ALLOW_UNASSIGNED);
68
69         if (ret!=IDNA_SUCCESS){
70                 g_warning(N_("JID domain doesn't pass ToUnicode"));
71                 ret=0;
72         }
73         else{
74                 ret=!strcmp(outbuf,my_name);
75                 if (!ret){
76                         debug(L_("Bad hostname (%s) in JID: %s"),outbuf,jid);
77                 }
78         }
79
80         g_free(inbuf);
81         g_free(outbuf);
82
83         return ret;
84 }
85
86 int jid_is_me(const char *jid){
87 int i,slash;
88 int ret;
89 char *inbuf,*outbuf;
90
91         if (!jid) return 0;
92
93         slash=-1;
94         for(i=0;jid[i];i++){
95                 if (jid[i]=='/' && slash<0) slash=i;
96                 else if (jid[i]=='@') return 0;
97         }
98
99         if ((slash<0 && strlen(jid)>1023) || slash>1023){
100                 g_warning(N_("JID domain too long"));
101                 return 0;
102         }
103
104         if (slash<0)
105                 inbuf=g_strdup(jid);
106         else
107                 inbuf=g_strndup(jid,slash);
108
109         ret=idna_to_unicode_8z8z(inbuf,&outbuf,IDNA_ALLOW_UNASSIGNED);
110
111         if (ret!=IDNA_SUCCESS){
112                 g_warning(N_("JID domain doesn't pass ToUnicode"));
113                 ret=0;
114         }
115         else{
116                 ret=!strcmp(outbuf,my_name);
117         }
118
119         g_free(inbuf);
120         g_free(outbuf);
121
122         return ret;
123 }
124
125
126 int jid_has_uin(const char *jid){
127 char *p;
128
129         p=strchr(jid,'@');
130         if (p) return 1;
131         return 0;
132 }
133
134 int jid_get_uin(const char *jid){
135
136         return atoi(jid);
137 }
138
139 const char *jid_get_resource(const char *jid){
140 const char *p;
141
142         p=strchr(jid,'/');
143         if (p) p++;
144         return p;
145 }
146
147 char * jid_my_registered(){
148
149         if (bare_domain)
150                 return g_strdup(my_name);
151         else
152                 return g_strdup_printf("%s/registered",my_name);
153 }
154
155 char * jid_build(long unsigned int uin){
156         return g_strdup_printf("%lu@%s",uin,my_name);
157 }
158
159 char * jid_build_full(long unsigned int uin){
160         return g_strdup_printf("%lu@%s/GG",uin,my_name);
161 }
162
163 char * jid_normalized(const char *jid,int full){
164 int i,at,slash,ret;
165 char node[1024],domain[1024],resource[1024];
166 char *domainbuf;
167
168         if (!jid) return NULL;
169
170         slash=-1;
171         at=-1;
172         /* split into parts */
173         for(i=0;jid[i];i++){
174                 if (jid[i]=='@' && at<0) at=i;
175                 if (jid[i]=='/' && slash<0) slash=i;
176         }
177
178         if (slash>=0 && slash<at){
179                 g_warning(N_("slash<at (%i<%i) in %s"),slash,at,jid);
180                 return NULL;
181         }
182         if (slash==at+1){
183                 g_warning(N_("empty domain in %s"),jid);
184                 return NULL;
185         }
186
187         /* node */
188         if (at>0){
189                 if (at>1023){
190                         g_warning(N_("node too long in %s"),jid);
191                         return NULL;
192                 }
193                 memcpy(node,jid,at);
194                 node[at]='\000';
195         }
196         else node[0]='\000';
197
198         /* domain */
199         if (slash>0){
200                 if (slash-at>1024){
201                         g_warning(N_("domain too long in %s"),jid);
202                         return NULL;
203                 }
204                 memcpy(domain,jid+at+1,slash-at-1);
205                 domain[slash-at-1]='\000';
206         }
207         else{
208                 if (strlen(jid+at+1)>1023){
209                         g_warning(N_("domain too long in %s"),jid);
210                         return NULL;
211                 }
212                 strcpy(domain,jid+at+1);
213         }
214
215         /* resource */
216         if (slash>0){
217                 if (strlen(jid+slash+1)>1023){
218                         g_warning(N_("resource too long in %s"),jid);
219                         return NULL;
220                 }
221                 strcpy(resource,jid+slash+1);
222         }
223         else resource[0]='\000';
224
225         if (node[0]){
226                 ret=stringprep_xmpp_nodeprep(node,1024);
227                 if (ret!=0){
228                         g_warning(N_("bad node: %s"),node);
229                         return NULL;
230                 }
231         }
232         if (resource[0]){
233                 ret=stringprep_xmpp_resourceprep(resource,1024);
234                 if (ret!=0){
235                         g_warning(N_("bad node: %s"),resource);
236                         return NULL;
237                 }
238         }
239         ret=idna_to_unicode_8z8z(domain,&domainbuf,IDNA_ALLOW_UNASSIGNED);
240         if (ret!=IDNA_SUCCESS){
241                 g_warning(N_("bad domain: %s"),domain);
242                 return NULL;
243         }
244         strcpy(domain,domainbuf);
245         g_free(domainbuf);
246
247         if (!full || !resource[0]){
248                 if (node[0])
249                         return g_strconcat(node,"@",domain,NULL);
250                 else
251                         return g_strdup(domain);
252         }
253         else{
254                 if (node[0])
255                         return g_strconcat(node,"@",domain,"/",resource,NULL);
256                 else
257                         return g_strconcat(domain,"/",resource,NULL);
258         }
259 }
Note: See TracBrowser for help on using the browser.