aboutsummaryrefslogtreecommitdiff
path: root/src/com/wilko/jaimtest/JaimTest.java
blob: 032af655ed25ff81bf28fb3f36a4ed956cd0013e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
 *   (C) 2002 Paul Wilkinson  wilko@users.sourceforge.net
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * JaimTest.java
 *
 * Created on 3 May 2002, 12:26
 */

package com.wilko.jaimtest;

import com.wilko.jaim.*;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Iterator;

/**
 * @author paulw
 * @version $Revision: 1.13 $
 */
public class JaimTest implements JaimEventListener {

    JaimConnection c;

    boolean quit = false;

    /**
     * Creates new JaimMain
     */
    public JaimTest() {

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: JaimTest <username> <password>");
        } else {
            JaimTest tester = new JaimTest();
            tester.doIt(args[0], args[1]);
        }

    }

    private void doIt(String username, String password) {
        try {
            c = new JaimConnection("toc.oscar.aol.com", 9898);
            c.setDebug(true);               // Send debugging to standard output
            c.connect();

            c.addEventListener(this);
            c.watchBuddy("username");         // Must watch at least one buddy or you will not appear on buddy listings

            c.logIn(username, password, 50000);
            c.addBlock("");     // Set Deny None

            c.setInfo("This buddy is using <a href=\"http://jaimlib.sourceforge.net\">Jaim</a>.");


            c.setIdle(60);      // Pretend we have been idle for a minute
            c.setAway("I am away right now");

            try {
                Thread.sleep(10000);       //Wait for 10 second
            } catch (InterruptedException ie) {
            }

            c.setIdle(0);      // Pretend we have been idle for a minute
            c.setAway("");

            while (!quit) {
                try {
                    Thread.sleep(300000);       //Wait for 5 minutes
                } catch (InterruptedException ie) {
                }
            }
            System.out.println("Disconnecting");
            c.disconnect();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JaimException je) {
            je.printStackTrace();
        }

    }

    /**
     * Receive an event and process it according to its content
     *
     * @param event - The JaimEvent to be processed
     */


    public void receiveEvent(JaimEvent event) {
        TocResponse tr = event.getTocResponse();
        String responseType = tr.getResponseType();
        System.out.println("Type: " + responseType);
        if (responseType.equalsIgnoreCase(BuddyUpdateTocResponse.RESPONSE_TYPE)) {
            receiveBuddyUpdate((BuddyUpdateTocResponse) tr);
        } else if (responseType.equalsIgnoreCase(IMTocResponse.RESPONSE_TYPE)) {
            receiveIM((IMTocResponse) tr);
        } else if (responseType.equalsIgnoreCase(EvilTocResponse.RESPONSE_TYPE)) {
            receiveEvil((EvilTocResponse) tr);
        } else if (responseType.equalsIgnoreCase(GotoTocResponse.RESPONSE_TYPE)) {
            receiveGoto((GotoTocResponse) tr);
        } else if (responseType.equalsIgnoreCase(ConfigTocResponse.RESPONSE_TYPE)) {
            receiveConfig();
        } else if (responseType.equalsIgnoreCase(ErrorTocResponse.RESPONSE_TYPE)) {
            receiveError((ErrorTocResponse) tr);
        } else if (responseType.equalsIgnoreCase(LoginCompleteTocResponse.RESPONSE_TYPE)) {
            System.out.println("Login is complete");
        } else if (responseType.equalsIgnoreCase(ConnectionLostTocResponse.RESPONSE_TYPE)) {
            System.out.println("Connection lost!");
        } else if (responseType.equalsIgnoreCase(ChatInviteTocResponse.RESPONSE_TYPE)) {
            recieveChatInvite((ChatInviteTocResponse) tr);
        } else {
            System.out.println("Unknown TOC Response:" + tr);
        }
    }

    private void receiveError(ErrorTocResponse et) {
        System.out.println("Error: " + et.getErrorDescription());
    }

    private void receiveIM(IMTocResponse im) {
        System.out.println(im.getFrom() + "->" + Utils.stripHTML(im.getMsg()));

        try {

            c.sendIM(im.getFrom(), "Hello " + im.getFrom(), false);

        } catch (IOException e) {
        }
    }

    private void receiveBuddyUpdate(BuddyUpdateTocResponse bu) {
        System.out.println("Buddy update: " + bu.getBuddy());
        if (bu.isOnline()) {
            System.out.println("Online");
        } else {
            System.out.println("Offline");
        }

        if (bu.isAway()) {
            System.out.println("Away");
        }

        System.out.println("evil: " + bu.getEvil());

        System.out.println("Idle: " + bu.getIdleTime());

        System.out.println("On since " + bu.getSignonTime().toString());
    }

    private void receiveEvil(EvilTocResponse er) {
        if (er.isAnonymous()) {
            System.out.println("We have been warned anonymously!");
        } else {
            System.out.println("We have been warned by " + er.getEvilBy());
            try {
                c.sendEvil(er.getEvilBy(), false);     // Let's warn them back
                c.addBlock(er.getEvilBy());          // And block them
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        System.out.println("New warning level is:" + er.getEvilAmount());
    }

    private void receiveGoto(GotoTocResponse gr) {
        System.out.println("Attempting to access " + gr.getURL());
        try {
            InputStream is = c.getURL(gr.getURL());
            if (is != null) {
                InputStreamReader r = new InputStreamReader(is);
                int chr = 0;
                while (chr != -1) {
                    chr = r.read();
                    System.out.print((char) chr);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void recieveChatInvite(ChatInviteTocResponse inviteTocResponse) {
        c.joinChat(inviteTocResponse.getRoomName());
    }

    private void receiveConfig() {
        System.out.println("Config is now valid.");

        try {
            Iterator it = c.getGroups().iterator();
            while (it.hasNext()) {
                Group g = (Group) it.next();
                System.out.println("Group: " + g.getName());
                Enumeration e = g.enumerateBuddies();
                while (e.hasMoreElements()) {
                    Buddy b = (Buddy) e.nextElement();
                    b.setDeny(false);
                    b.setPermit(false);
                    c.watchBuddy(b.getName());
                    if (b.getDeny()) {
                        c.addBlock(b.getName());
                    }
                    if (b.getPermit()) {
                        c.addPermit(b.getName());
                    }
                }
            }
            c.saveConfig();
        } catch (Exception je) {
            je.printStackTrace();
        }
    }


}