|
1 /* |
|
2 * Copyright (c) 2004 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Validator to check the IMPS id correctness. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // INCLUDE FILES |
|
20 #include <EscapeUtils.h> |
|
21 #include "CnUiIMPSIdValidator.h" |
|
22 |
|
23 |
|
24 //CONSTS |
|
25 /** |
|
26 * Constants to use in validation. |
|
27 * |
|
28 * For future: These could be merged with IMPS Common UI |
|
29 */ |
|
30 const TInt KServerWVUserIdMaxLength = 50; |
|
31 _LIT( KCUImpsId_WhiteSpace, " " ); |
|
32 _LIT( KCUImpsId_At, "@" ); |
|
33 _LIT( KCUImpsId_Dot, "." ); |
|
34 _LIT( KCUImpsId_TwoDots, ".." ); |
|
35 _LIT( KCUImpsId_Slash, "/" ); |
|
36 _LIT( KCUImpsId_Plus, "+" ); |
|
37 _LIT( KCUImpsId_Tabulator, "\t" ); |
|
38 _LIT( KCUImpsId_WVPrefix, "wv:" ); |
|
39 |
|
40 _LIT( KCUImpsId_AtEnc, "%40" ); |
|
41 _LIT( KCUImpsId_SlashEnc, "%2F" ); |
|
42 _LIT( KCUImpsId_PlusEnc, "%2B" ); |
|
43 _LIT( KCUImpsId_TabulatorEnc, "%09" ); |
|
44 _LIT( KCUImpsId_WhiteSpaceEnc, "%20" ); |
|
45 |
|
46 const TInt KCUImpsId_WVPrefixLength = 3; |
|
47 |
|
48 |
|
49 |
|
50 // ================= MEMBER FUNCTIONS ======================= |
|
51 // ----------------------------------------------------------------------------- |
|
52 // CnUiIMPSIdValidator::ValidLoginIdL() |
|
53 // ----------------------------------------------------------------------------- |
|
54 // |
|
55 TBool CnUiIMPSIdValidator::ValidLoginIdL( const TDesC& aPresenceId ) |
|
56 { |
|
57 TBool idCorrect( ETrue ); |
|
58 |
|
59 HBufC* wvId = aPresenceId.AllocLC(); |
|
60 TPtr wvIdPtr = wvId->Des(); |
|
61 |
|
62 TInt maxLength ( KServerWVUserIdMaxLength ); |
|
63 |
|
64 // let's take out the "wv:" from beginning of user id |
|
65 TInt position( KErrNotFound ); |
|
66 if ( KCUImpsId_WVPrefix().CompareF( wvId->Left( KCUImpsId_WVPrefixLength ) ) == 0 ) |
|
67 { |
|
68 wvIdPtr.Delete( 0, KCUImpsId_WVPrefix().Length() ); |
|
69 // we just took out 3 characters from the id, we have to adjust the max length |
|
70 maxLength = maxLength - KCUImpsId_WVPrefixLength; |
|
71 } |
|
72 |
|
73 // where is "@" ? |
|
74 TInt atIndex( wvId->Find( KCUImpsId_At ) ); |
|
75 // we want the domain part without the '@' |
|
76 TPtrC domainPart( wvId->Mid( atIndex + 1 ) ); |
|
77 |
|
78 if ( atIndex > 0 ) |
|
79 { |
|
80 // check if the domain part is empty |
|
81 if ( domainPart.Length() == 0 ) |
|
82 { |
|
83 idCorrect = EFalse; |
|
84 } |
|
85 } |
|
86 else if ( atIndex == 0 ) |
|
87 { |
|
88 // the '@' is the first character |
|
89 idCorrect = EFalse; |
|
90 } |
|
91 |
|
92 |
|
93 // check the correctness of the domain part |
|
94 if ( ( domainPart.Find( KCUImpsId_At() ) ) != KErrNotFound ) |
|
95 { |
|
96 // extra @-mark found in the domain part |
|
97 idCorrect = EFalse; |
|
98 } |
|
99 |
|
100 TInt returnValue ( domainPart.Find( KCUImpsId_Dot() ) ); |
|
101 if ( returnValue != KErrNotFound ) |
|
102 { |
|
103 // the part after the '.' |
|
104 TPtrC partAfterDot ( domainPart.Mid( returnValue + 1 ) ); |
|
105 // the part before the '.' |
|
106 TPtrC partBeforeDot ( domainPart.Left( returnValue ) ); |
|
107 // if the '.' is the last character or the first character after the '@' |
|
108 // the domain part is wrong |
|
109 if ( ( 0 == partAfterDot.Length() ) || ( 0 == partBeforeDot.Length() ) ) |
|
110 { |
|
111 idCorrect = EFalse; |
|
112 } |
|
113 } |
|
114 |
|
115 if ( ( domainPart.Find( KCUImpsId_TwoDots() ) ) != KErrNotFound ) |
|
116 { |
|
117 // there are two sequential dots in the domain part |
|
118 idCorrect = EFalse; |
|
119 } |
|
120 |
|
121 // first we must delete one '@' char if existing |
|
122 position = wvIdPtr.Find( KCUImpsId_At ); |
|
123 if ( position != KErrNotFound ) |
|
124 { |
|
125 wvIdPtr.Delete( position, KCUImpsId_At().Length() ); |
|
126 // @-character was removed from the ID, since it should not be encoded |
|
127 // so the maxlength has to be decreased by it's length |
|
128 maxLength = maxLength - KCUImpsId_At().Length(); |
|
129 } |
|
130 |
|
131 // check the id for forbidden characters |
|
132 |
|
133 if ( CnUiIMPSIdValidator::ForbiddenChars( wvIdPtr ) ) |
|
134 { |
|
135 idCorrect = EFalse; |
|
136 } |
|
137 |
|
138 TInt length( wvIdPtr.Length() ); |
|
139 // no 16bit Unicode chars characters allowed in the user part |
|
140 for ( TInt index = 0; index < length; index++ ) |
|
141 { |
|
142 TUint value = wvIdPtr[index]; |
|
143 // if the character does not fit into 8 bits, it's forbidden |
|
144 if ( value > 255 ) |
|
145 { |
|
146 idCorrect = EFalse; |
|
147 } |
|
148 } |
|
149 |
|
150 // check if the user id is too long when encoded, but only if there are no forbidden chars |
|
151 if ( idCorrect ) |
|
152 { |
|
153 HBufC* encodedUserId = EscapeUtils::EscapeEncodeL( *wvId, EscapeUtils::EEscapeUrlEncoded ); |
|
154 |
|
155 if ( encodedUserId->Length() > maxLength ) |
|
156 { |
|
157 idCorrect = EFalse; |
|
158 } |
|
159 delete encodedUserId; |
|
160 } |
|
161 |
|
162 CleanupStack::PopAndDestroy( wvId ); |
|
163 |
|
164 return idCorrect; |
|
165 } |
|
166 |
|
167 |
|
168 // ----------------------------------------------------------------------------- |
|
169 // CnUiIMPSIdValidator::ValidLoginIdL() |
|
170 // ----------------------------------------------------------------------------- |
|
171 // |
|
172 TBool CnUiIMPSIdValidator::ForbiddenChars( const TDesC& aImpsId ) |
|
173 { |
|
174 TBool returnValue( EFalse ); |
|
175 |
|
176 // no empty chars allowed in the user part |
|
177 if ( ( aImpsId.Find( KCUImpsId_WhiteSpace() ) ) != KErrNotFound ) |
|
178 { |
|
179 returnValue = ETrue; |
|
180 } |
|
181 // no encoded empty chars allowed in the user part |
|
182 if ( ( aImpsId.Find( KCUImpsId_WhiteSpaceEnc() ) ) != KErrNotFound ) |
|
183 { |
|
184 returnValue = ETrue; |
|
185 } |
|
186 |
|
187 // no '+' allowed in the user part |
|
188 if ( ( aImpsId.Find( KCUImpsId_Plus() ) ) != KErrNotFound ) |
|
189 { |
|
190 returnValue = ETrue; |
|
191 } |
|
192 // no encoded '+' allowed in the user part |
|
193 if ( ( aImpsId.Find( KCUImpsId_PlusEnc() ) ) != KErrNotFound ) |
|
194 { |
|
195 returnValue = ETrue; |
|
196 } |
|
197 |
|
198 // no '\t' allowed in the user part |
|
199 if ( ( aImpsId.Find( KCUImpsId_Tabulator() ) ) != KErrNotFound ) |
|
200 { |
|
201 returnValue = ETrue; |
|
202 } |
|
203 // no encoded tabulator allowed in the user part |
|
204 if ( ( aImpsId.Find( KCUImpsId_TabulatorEnc() ) ) != KErrNotFound ) |
|
205 { |
|
206 returnValue = ETrue; |
|
207 } |
|
208 |
|
209 // no '/' allowed in the user part |
|
210 if ( ( aImpsId.Find( KCUImpsId_Slash() ) ) != KErrNotFound ) |
|
211 { |
|
212 returnValue = ETrue; |
|
213 } |
|
214 // no encoded '/' allowed in the user part |
|
215 if ( ( aImpsId.Find( KCUImpsId_SlashEnc() ) ) != KErrNotFound ) |
|
216 { |
|
217 returnValue = ETrue; |
|
218 } |
|
219 |
|
220 // no encoded '@' chars allowed in the user part |
|
221 if ( ( aImpsId.Find( KCUImpsId_AtEnc() ) ) != KErrNotFound ) |
|
222 { |
|
223 returnValue = ETrue; |
|
224 } |
|
225 |
|
226 return returnValue; |
|
227 } |
|
228 |
|
229 // End of File |