1 /*------------------------------------------------------------------------------
2 * The contents of this file are subject to the Mozilla Public License Version
3 * 1.1 (the "License"); you may not use this file except in compliance with
4 * the License. You may obtain a copy of the License at
5 * http://www.mozilla.org/MPL/
6 * Software distributed under the License is distributed on an "AS IS" basis,
7 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8 * the specific language governing rights and limitations under the License.
9 *
10 * The Original Code is levelonelabs.com code.
11 * The Initial Developer of the Original Code is Level One Labs. Portions
12 * created by the Initial Developer are Copyright (C) 2001 the Initial
13 * Developer. All Rights Reserved.
14 *
15 * Contributor(s):
16 * Scott Oster (ostersc@alum.rpi.edu)
17 * Steve Zingelwicz (sez@po.cwru.edu)
18 * William Gorman (willgorman@hotmail.com)
19 *
20 * Alternatively, the contents of this file may be used under the terms of
21 * either the GNU General Public License Version 2 or later (the "GPL"), or
22 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
23 * in which case the provisions of the GPL or the LGPL are applicable
24 * instead of those above. If you wish to allow use of your version of this
25 * file only under the terms of either the GPL or the LGPL, and not to allow
26 * others to use your version of this file under the terms of the NPL, indicate
27 * your decision by deleting the provisions above and replace them with the
28 * notice and other provisions required by the GPL or the LGPL. If you do not
29 * delete the provisions above, a recipient may use your version of this file
30 * under the terms of any one of the NPL, the GPL or the LGPL.
31 *----------------------------------------------------------------------------*/
32
33 package com.levelonelabs.aim.example;
34
35 import java.util.ArrayList;
36 import java.util.Date;
37
38 import com.levelonelabs.aim.AIMAdapter;
39 import com.levelonelabs.aim.AIMBuddy;
40 import com.levelonelabs.aim.AIMClient;
41 import com.levelonelabs.aim.AIMSender;
42
43
44 /***
45 * Forward messages from one screename to anonther.
46 *
47 * @author Scott Oster
48 *
49 * @created July 23, 2002
50 */
51 public class AIMReceptionist {
52 /***
53 * The main program starts up a aimclient and signs it on. Then registers a
54 * listener that gets called when someone messages the bot. It simply
55 * responds and forwards the message on.
56 *
57 * @param args
58 * username password newusername
59 */
60 public static void main(String[] args) {
61 if (args.length != 3) {
62 System.err.println("USAGE: username password newusername");
63 System.exit(-1);
64 }
65
66 final String username = args[0];
67 final String pass = args[1];
68 final AIMBuddy newName = new AIMBuddy(args[2].toLowerCase());
69
70 //username, password, true indicates to add all that talk to it to the
71 // buddy list
72 final AIMSender aim = new AIMClient(username, pass, "AIM Receiptionist", true);
73 aim.addBuddy(newName);
74 aim.addAIMListener(new AIMAdapter() {
75 //respond to new messages
76 public void handleMessage(AIMBuddy buddy, String request) {
77 aim.sendMessage(buddy, "Sorry " + buddy.getName() + ", " + username
78 + " doesn't use this name anymore, but I'll forward the message.");
79 if (newName.isOnline()) {
80 aim.sendMessage(newName, buddy.getName() + " said: " + request);
81 } else {
82 newName.addMessage(buddy.getName() + " said \"" + request + "\" at " + new Date());
83 }
84 }
85
86
87 //forward any saved messages
88 public void handleBuddySignOn(AIMBuddy buddy, String info) {
89 if (buddy.getName().equalsIgnoreCase(newName.getName()) && buddy.hasMessages()) {
90 ArrayList messages = buddy.getMessages();
91 String message = "";
92
93 //collect the messages
94 for (int i = 0; i < messages.size(); i++) {
95 message += (messages.get(i) + "<BR>");
96 }
97
98 //send the list
99 aim.sendMessage(buddy, message);
100 buddy.clearMessages();
101 }
102 }
103 });
104
105 aim.signOn();
106 }
107 }