Debian Codegraph @

xmpp-vala/src/module/jid.vala

raw
1 namespace Xmpp {
2
3 public class Jid {
4 public string? localpart;
5 public string domainpart;
6 public string? resourcepart;
7
8 public Jid bare_jid {
9 owned get { return is_bare() ? this : new Jid.intern(null, localpart, domainpart, null); }
10 }
11
12 public Jid domain_jid {
13 owned get { return is_domain() ? this : new Jid.intern(domainpart, null, domainpart, null); }
14 }
15
16 private string jid;
17
18 public Jid(string jid) throws InvalidJidError {
19 int slash_index = jid.index_of("/");
20 int at_index = jid.index_of("@");
21 if (at_index > slash_index && slash_index != -1) at_index = -1;
22 string resourcepart = slash_index < 0 ? null : jid.slice(slash_index + 1, jid.length);
23 string localpart = at_index < 0 ? null : jid.slice(0, at_index);
24 string domainpart;
25 if (at_index < 0) {
26 if (slash_index < 0) {
27 domainpart = jid;
28 } else {
29 domainpart = jid.slice(0, slash_index);
30 }
31 } else {
32 if (slash_index < 0) {
33 domainpart = jid.slice(at_index + 1, jid.length);
34 } else {
35 domainpart = jid.slice(at_index + 1, slash_index);
36 }
37 }
38
39 this.components(localpart, domainpart, resourcepart);
40 }
41
42 private Jid.intern(owned string? jid, owned string? localpart, owned string domainpart, owned string? resourcepart) {
43 this.jid = (owned) jid;
44 this.localpart = (owned) localpart;
45 this.domainpart = (owned) domainpart;
46 this.resourcepart = (owned) resourcepart;
47 }
48
49 public Jid.components(string? localpart, string domainpart, string? resourcepart) throws InvalidJidError {
50 // TODO verify and normalize all parts
51 if (domainpart.length == 0) throw new InvalidJidError.EMPTY_DOMAIN("Domain is empty");
52 if (localpart != null && localpart.length == 0) throw new InvalidJidError.EMPTY_LOCAL("Localpart is empty but non-null");
53 if (resourcepart != null && resourcepart.length == 0) throw new InvalidJidError.EMPTY_RESOURCE("Resource is empty but non-null");
54 string domain = domainpart[domainpart.length - 1] == '.' ? domainpart.substring(0, domainpart.length - 1) : domainpart;
55 if (domain.contains("xn--")) {
56 domain = idna_decode(domain);
57 }
58 this.localpart = prepare(localpart, ICU.PrepType.RFC3920_NODEPREP);
59 this.domainpart = prepare(domain, ICU.PrepType.RFC3491_NAMEPREP);
60 this.resourcepart = prepare(resourcepart, ICU.PrepType.RFC3920_RESOURCEPREP);
61 idna_verify(this.domainpart);
62 }
63
64 private static string idna_decode(string src) throws InvalidJidError {
65 ICU.ErrorCode status = ICU.ErrorCode.ZERO_ERROR;
66 ICU.IDNAInfo info;
67 char[] dest = new char[src.length * 2];
68 ICU.IDNA.openUTS46(ICU.IDNAOptions.DEFAULT, ref status).nameToUnicodeUTF8(src, -1, dest, out info, ref status);
69 if (status == ICU.ErrorCode.INVALID_CHAR_FOUND) {
70 throw new InvalidJidError.INVALID_CHAR("Found invalid character");
71 } else if (status.is_failure() || info.errors > 0) {
72 throw new InvalidJidError.UNKNOWN(@"Unknown error: $(status.errorName())");
73 }
74 return (string) dest;
75 }
76
77 private static void idna_verify(string src) throws InvalidJidError {
78 ICU.ErrorCode status = ICU.ErrorCode.ZERO_ERROR;
79 ICU.IDNAInfo info;
80 char[] dest = new char[src.length * 2];
81 ICU.IDNA.openUTS46(ICU.IDNAOptions.DEFAULT, ref status).nameToASCII_UTF8(src, -1, dest, out info, ref status);
82 if (status == ICU.ErrorCode.INVALID_CHAR_FOUND) {
83 throw new InvalidJidError.INVALID_CHAR("Found invalid character");
84 } else if (status.is_failure() || info.errors > 0) {
85 throw new InvalidJidError.UNKNOWN(@"Unknown error: $(status.errorName())");
86 }
87 }
88
89 private static string? prepare(string? src, ICU.PrepType type, bool strict = false) throws InvalidJidError {
90 if (src == null) return src;
91 try {
92 ICU.ParseError error;
93 ICU.ErrorCode status = ICU.ErrorCode.ZERO_ERROR;
94 ICU.PrepProfile profile = ICU.PrepProfile.openByType(type, ref status);
95 ICU.String src16 = ICU.String.from_string(src);
96 int32 dest16_capacity = src16.len() * 2 + 1;
97 ICU.String dest16 = ICU.String.alloc(dest16_capacity);
98 long dest16_length = profile.prepare(src16, src16.len(), dest16, dest16_capacity, strict ? ICU.PrepOptions.DEFAULT : ICU.PrepOptions.ALLOW_UNASSIGNED, out error, ref status);
99 if (status == ICU.ErrorCode.INVALID_CHAR_FOUND) {
100 throw new InvalidJidError.INVALID_CHAR("Found invalid character");
101 } else if (status == ICU.ErrorCode.STRINGPREP_PROHIBITED_ERROR) {
102 throw new InvalidJidError.INVALID_CHAR("Found prohibited character");
103 } else if (status != ICU.ErrorCode.ZERO_ERROR) {
104 throw new InvalidJidError.UNKNOWN(@"Unknown error: $(status.errorName())");
105 } else if (dest16_length < 0) {
106 throw new InvalidJidError.UNKNOWN("Unknown error");
107 }
108 return dest16.to_string();
109 } catch (ConvertError e) {
110 throw new InvalidJidError.INVALID_CHAR(@"Conversion error: $(e.message)");
111 }
112 }
113
114 public Jid with_resource(string? resourcepart) throws InvalidJidError {
115 return new Jid.components(localpart, domainpart, resourcepart);
116 }
117
118 public bool is_domain() {
119 return localpart == null && resourcepart == null;
120 }
121
122 public bool is_bare() {
123 return resourcepart == null;
124 }
125
126 public bool is_full() {
127 return localpart != null && resourcepart != null;
128 }
129
130 public string to_string() {
131 if (jid == null) {
132 if (localpart != null && resourcepart != null) {
133 jid = @"$localpart@$domainpart/$resourcepart";
134 } else if (localpart != null) {
135 jid = @"$localpart@$domainpart";
136 } else if (resourcepart != null) {
137 jid = @"$domainpart/$resourcepart";
138 } else {
139 jid = domainpart;
140 }
141 }
142 return jid;
143 }
144
145 public bool equals_bare(Jid? jid) {
146 return jid != null && equals_bare_func(this, jid);
147 }
148
149 public bool equals(Jid? jid) {
150 return jid != null && equals_func(this, jid);
151 }
152
153 public static new bool equals_bare_func(Jid jid1, Jid jid2) {
154 return jid1.localpart == jid2.localpart && jid1.domainpart == jid2.domainpart;
155 }
156
157 public static bool equals_func(Jid jid1, Jid jid2) {
158 return equals_bare_func(jid1, jid2) && jid1.resourcepart == jid2.resourcepart;
159 }
160
161 public static new uint hash_bare_func(Jid jid) {
162 return jid.bare_jid.to_string().hash();
163 }
164
165 public static new uint hash_func(Jid jid) {
166 return jid.to_string().hash();
167 }
168 }
169
170 public errordomain InvalidJidError {
171 EMPTY_DOMAIN,
172 EMPTY_RESOURCE,
173 EMPTY_LOCAL,
174 INVALID_CHAR,
175 UNKNOWN
176 }
177
178 }