Source code: com/jabberwookie/tests/ClientConnect.java
1 /*
2 * Main.java
3 *
4 * Created on April 25, 2002, 7:08 PM
5 */
6
7 package com.jabberwookie.tests;
8
9 import java.net.Socket;
10 import java.io.IOException;
11 import java.util.Hashtable;
12 import java.util.Enumeration;
13
14 import com.ssttr.xml.XMLElement;
15
16 import com.jabberwookie.Client2Server;
17 import com.jabberwookie.IQListener;
18 import com.jabberwookie.MessageListener;
19 import com.jabberwookie.PresenceListener;
20
21 import com.jabberwookie.ns.jabber.Const;
22 import com.jabberwookie.ns.jabber.Chunk;
23 import com.jabberwookie.ns.jabber.IQ;
24 import com.jabberwookie.ns.jabber.Message;
25 import com.jabberwookie.ns.jabber.Presence;
26
27 import com.jabberwookie.ns.jabber.iq.IQRegister;
28 import com.jabberwookie.ns.jabber.iq.IQRoster;
29
30 /**
31 * This class is part Client2Server test and part usage example.
32 * So if you're trying to figure out how this all works, take
33 * a look inside.
34 * @author smeiners
35 */
36 public class ClientConnect
37 implements MessageListener, PresenceListener, IQListener
38 {
39 private Client2Server c2s;
40
41 public static void main(String args[])
42 {
43 if( args.length < 5 )
44 {
45 System.out.println("usage: <user name> <resource> <password> <server name> <server port>");
46 return;
47 }
48 else
49 new ClientConnect(args[0],args[1],args[2],args[3],Integer.parseInt(args[4]));
50 }
51
52 public ClientConnect (String userName, String resource,
53 String password, String serverName, int serverPort)
54 {
55 if( ! login(userName, resource, password, serverName, serverPort) )
56 {
57 System.out.println("Could not login.");
58 System.exit(1);
59 }
60
61 System.out.println("Logged in!");
62 }
63
64 /**
65 * This will attempt to login to the Jabber server. If it fails
66 * because the given username does not exist it will attempt
67 * to create that user.
68 * @param userName
69 * @param resource
70 * @param password
71 * @param serverName
72 * @param serverPort
73 * @return
74 */
75 public boolean login
76 (String userName, String resource, String password, String serverName, int serverPort)
77 {
78 try
79 {
80 // First, open a socket to the server.
81 Socket s = new Socket (serverName, serverPort);
82 // Then create a Stream object.
83 c2s = new Client2Server( s.getInputStream(), s.getOutputStream() );
84 // Then open the Jabber stream.
85 c2s.open(serverName,60);
86 // And make sure to set ourselves up to listen for incoming packets.
87 c2s.setAllListeners(this);
88
89 // Now try to login using any method the server supports.
90 switch( c2s.loginAny(userName, resource, password, 30) )
91 {
92 case Client2Server.LOGIN_BAD_PASS:
93 System.out.println("Bad password.");
94 return false;
95 case Client2Server.LOGIN_BAD_UID:
96 // oops, that user doesn't exist, let's create them.
97 if( ! registerUser(userName,password) )
98 return false;
99 // and now that they exist, try again.
100 else if( c2s.loginAny(userName, resource, password, 30) < Client2Server.LOGIN_OK )
101 return false;
102
103 break;
104 case Client2Server.LOGIN_FAILED:
105 System.out.println("Login failed: unknown.");
106 return false;
107 case Client2Server.LOGIN_OK:
108 // sucess!!
109 break;
110 case Client2Server.LOGIN_PASS_EXP:
111 System.out.println("Your password has expired, please change it now.");
112 break;
113 }
114
115 // set our presence
116 c2s.send( new Presence(Const.CHAT,"Available",1) );
117
118 // request the user's roster. this has to be done
119 // after we set our presence, otherwise we'll never get it.
120 c2s.send( IQRoster.createGetRequest(), 30 );
121
122 return true;
123 }
124 catch( IOException x )
125 {
126 // hmm, a connection problem.
127 x.printStackTrace();
128 return false;
129 }
130 }
131
132 public void incomingIQ (IQ iq)
133 {
134 // we got an <iq> packet
135 XMLElement el = iq.getChild(0);
136 if( el instanceof IQRoster )
137 {
138 // it's our roster!
139 for( Enumeration items = el.enumerateChildren(); items.hasMoreElements(); )
140 {
141 System.out.println( "Roster Item: " + (IQRoster.Item)items.nextElement() );
142 }
143 }
144 }
145
146 public void incomingMessage (Message message)
147 {
148 // yay! someone sent us a message!
149 System.out.println("Message: " + message);
150 }
151
152 public void incomingPresence (Presence presence)
153 {
154 // we got a <presence> packet
155 final String type = presence.getType();
156
157 // check to see if it's valid, some users send bad presence data
158 if( type == null || type.length() < 1 )
159 return;
160
161 if( type.equals(Const.SUBSCRIBE) )
162 {
163 // someone wants to subscribe to our presence
164 // so we slightly re-write the packet to send back
165 presence.setType(Const.SUBSCRIBED);
166 System.out.println(presence.getFrom() + " wants to subscribe to your presence, allowing.");
167 }
168
169 else if( type.equals(Const.UNSUBSCRIBE) )
170 {
171 // someone wants to unsuscribe from our presence
172 // so we slightly re-write the packet to send back
173 presence.setType(Const.UNSUBSCRIBED);
174 System.out.println(presence.getFrom() + " wants to unsubscribe to your presence, allowing.");
175 }
176
177 else
178 {
179 // just a normal presence notification
180 System.out.println("Presence:" + presence);
181 return;
182 }
183
184 // make sure to return it to the sender and not ourselves
185 presence.setTo( presence.getFrom() );
186
187 try
188 { c2s.send (presence); }
189 catch( IOException x )
190 { x.printStackTrace(); }
191 }
192
193 private boolean registerUser(String userName, String password)
194 {
195 System.out.println("You are not registered, attempting to do so now.");
196 try
197 {
198 Hashtable info = IQRegister.getRequiredRegInfo(c2s);
199 info.put(Const.USERNAME,userName);
200 info.put(Const.PASSWORD,password);
201
202 System.out.println("The Server wants the following info:");
203 String key, value;
204 for( Enumeration e = info.keys(); e.hasMoreElements(); )
205 {
206 key = (String)e.nextElement();
207 value = (String)info.get(key);
208
209 System.out.println(key + "=" + value + ".");
210
211 // the server could ask for more than these three
212 // parameters, but unless we ask the user we'll have
213 // no idea what to give it.
214 if( key.equals(Const.USERNAME) )
215 info.put(key,userName);
216 else if( key.equals(Const.PASSWORD) )
217 info.put(key,password);
218 // the default jabberd config *requires* an
219 // email address, so we'll placate it.
220 else if( key.equals(Const.EMAIL) )
221 info.put(key, "bogus@email.address" );
222 }
223
224 Chunk chunk = c2s.send
225 ( IQRegister.createSetRequest(c2s.getServerName(), info), 30 );
226
227 // verify sucess
228 return ( chunk != null && chunk.getType().equals(Const.RESULT) );
229 }
230 catch( IOException x )
231 { return false; }
232 }
233 }