View Javadoc
1   //////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.oscar.client;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  
24  import net.sf.dexterim.core.Account;
25  import net.sf.dexterim.oscar.entity.AuthenticationCookie;
26  import net.sf.dexterim.oscar.entity.DWord;
27  import net.sf.dexterim.oscar.entity.Flap;
28  import net.sf.dexterim.oscar.entity.Snac;
29  import net.sf.dexterim.oscar.entity.TLV;
30  import net.sf.dexterim.oscar.entity.TLVCollection;
31  import net.sf.dexterim.oscar.entity.TLVFactory;
32  import net.sf.dexterim.oscar.entity.VariableByte;
33  import net.sf.dexterim.oscar.entity.Word;
34  import net.sf.dexterim.oscar.server.RateInfoResponse;
35  import net.sf.dexterim.oscar.util.PasswordRoaster;
36  
37  /***
38   * @author christoph
39   */
40  public class MessageBuilder {
41  
42  	public MessageBuilder() {
43  
44  	}
45  
46  	public byte[] buildCookieRequest(AuthenticationCookie cookie) {
47  		Flap flap = FlapFactory.getInstance().createClientCookieFlap();
48  
49  		TLVCollection tlvs = new TLVCollection();
50  
51  		TLV tlvCookie = TLVFactory.getInstance().createCookie();
52  		tlvCookie.setData(cookie.getCookie());
53  		tlvs.add(tlvCookie);
54  
55  		byte id[] = new DWord(0x00000001).toByteArray();
56  		byte tlvData[] = tlvs.toByteArray();
57  
58  		byte[] data = new byte[id.length + tlvData.length];
59  		System.arraycopy(id, 0, data, 0, id.length);
60  
61  		System.arraycopy(tlvData, 0, data, id.length, tlvData.length);
62  
63  		flap.setData(data);
64  
65  		return flap.toByteArray();
66  	}
67  
68  	/*
69  	 * 00 00 00 01 dword id number 00 06 word TLV.Type(0x06) - authorization
70  	 * cookie xx xx word TLV.Length xx .. array authorization cookie
71  	 */
72  	public byte[] buildIdentRequest(Account account) {
73  		TLVCollection tlvs = buildIdentTLV(account);
74  
75  		Flap flap = FlapFactory.getInstance().createIdentRequestFlap();
76  
77  		byte id[] = new DWord(0x00000001).toByteArray();
78  		byte tlvData[] = tlvs.toByteArray();
79  
80  		byte[] data = new byte[id.length + tlvData.length];
81  		System.arraycopy(id, 0, data, 0, id.length);
82  
83  		System.arraycopy(tlvData, 0, data, id.length, tlvData.length);
84  
85  		flap.setData(data);
86  
87  		return flap.toByteArray();
88  	}
89  
90  	public byte[] buildRateRequest() {
91  		Flap flap = FlapFactory.getInstance().createSnacFlap();
92  
93  		Snac snac = new RateInfoRequest();
94  		flap.setData(snac.toByteArray());
95  
96  		return flap.toByteArray();
97  	}
98  
99  	/***
100 	 * @return
101 	 */
102 	public byte[] buildRateInfoAcknowledge(RateInfoResponse response) {
103 		Flap flap = FlapFactory.getInstance().createSnacFlap();
104 
105 		Snac snac = new RateInfoAcknowledge(response);
106 		flap.setData(snac.toByteArray());
107 
108 		return flap.toByteArray();
109 	}
110 	
111 	/***
112 	 * 
113 	 */
114 	public byte[] buildFamilyVersionRequest() {
115 		Flap flap = FlapFactory.getInstance().createSnacFlap();
116 
117 		Snac snac = new FamilyVersionRequest();
118 		snac.setRequest(0x0000);
119 		
120 		ByteArrayOutputStream familyStream = new ByteArrayOutputStream();
121 		
122 		try {
123 			familyStream.write(new Word(0x13).toByteArray());
124 			familyStream.write(new Word(0x02).toByteArray());
125 			
126 			familyStream.write(new Word(0x02).toByteArray());
127 			familyStream.write(new Word(0x01).toByteArray());
128 			
129 			familyStream.write(new Word(0x03).toByteArray());
130 			familyStream.write(new Word(0x01).toByteArray());
131 			
132 			familyStream.write(new Word(0x15).toByteArray());
133 			familyStream.write(new Word(0x01).toByteArray());
134 			
135 			familyStream.write(new Word(0x04).toByteArray());
136 			familyStream.write(new Word(0x01).toByteArray());
137 			
138 			familyStream.write(new Word(0x06).toByteArray());
139 			familyStream.write(new Word(0x01).toByteArray());
140 			
141 			familyStream.write(new Word(0x09).toByteArray());
142 			familyStream.write(new Word(0x01).toByteArray());
143 			
144 			familyStream.write(new Word(0x0A).toByteArray());
145 			familyStream.write(new Word(0x01).toByteArray());
146 			
147 			familyStream.write(new Word(0x0B).toByteArray());
148 			familyStream.write(new Word(0x01).toByteArray());
149 			
150 		} catch (IOException e) {
151 			e.printStackTrace();
152 		}
153 
154 		snac.setData(familyStream.toByteArray());
155 		flap.setData(snac.toByteArray());
156 
157 		return flap.toByteArray();
158 	}
159 	
160 	protected TLVCollection buildIdentTLV(Account account) {
161 		//TODO: Use configuration file to add localization parameters
162 		TLVCollection tlvs = new TLVCollection();
163 
164 		TLVFactory factory = TLVFactory.getInstance();
165 
166 		TLV uid = factory.createScreenName();
167 		uid.setData(new VariableByte(account.getIdentity().toString()));
168 		tlvs.add(uid);
169 
170 		int[] roastedPwd = PasswordRoaster.getInstance().roast(
171 				(String) account.getPassport());
172 
173 		TLV password = factory.createRoastedPassword();
174 		password.setData(new VariableByte(roastedPwd));
175 		tlvs.add(password);
176 
177 		TLV clientIDString = factory.createClientIdString();
178 		clientIDString.setData(new VariableByte(
179 				"ICQ Inc. - Product of ICQ (TM).2002a.5.37.1.3728.85"));
180 		tlvs.add(clientIDString);
181 
182 		TLV clientID = factory.createClientId();
183 		clientID.setData(new Word(266));
184 		tlvs.add(clientID);
185 
186 		TLV majorVersion = factory.createClientMajorVersion();
187 		majorVersion.setData(new Word(5));
188 		tlvs.add(majorVersion);
189 
190 		TLV minorVersion = factory.createClientMinorVersion();
191 		minorVersion.setData(new Word(37));
192 		tlvs.add(minorVersion);
193 
194 		TLV lesserVersion = factory.createClientLesserVersion();
195 		lesserVersion.setData(new Word(1));
196 		tlvs.add(lesserVersion);
197 
198 		TLV buildNumber = factory.createClientBuildNumber();
199 		buildNumber.setData(new Word(3728));
200 		tlvs.add(buildNumber);
201 
202 		TLV distro = factory.createClientDistributionNumber();
203 		distro.setData(new DWord(85));
204 		tlvs.add(distro);
205 
206 		TLV language = factory.createClientLanguage();
207 		language.setData(new VariableByte("de"));
208 		tlvs.add(language);
209 
210 		TLV country = factory.createClientCountry();
211 		country.setData(new VariableByte("at"));
212 		tlvs.add(country);
213 
214 		return tlvs;
215 	}
216 
217 
218 
219 
220 }