View Javadoc

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  
34  /***
35   * Module to read headlines using RSS
36   *
37   * @created Nov 6, 2002
38   */
39  package com.levelonelabs.aimbot.modules;
40  
41  
42  import java.util.ArrayList;
43  import java.util.Enumeration;
44  import java.util.Hashtable;
45  
46  import com.levelonelabs.aim.AIMBuddy;
47  import com.levelonelabs.aimbot.AIMBot;
48  import com.levelonelabs.aimbot.BotModule;
49  import com.levelonelabs.rdf.Channel;
50  import com.levelonelabs.rdf.Item;
51  
52  
53  /***
54   * Handles requests to get headlines
55   *
56   * @author Scott Oster
57   *
58   * @created Jan 15, 2003
59   */
60  public class HeadlinesModule extends BotModule {
61      private static ArrayList services;
62      private static final int MAX_NUM_HEADLINES=5;
63      private static Hashtable channels;
64  
65      /***
66       * Initialize the service commands.
67       */
68      static {
69          services=new ArrayList();
70          channels=new Hashtable();
71          String channelName;
72          Channel channel;
73  
74          channelName="slashdot";
75          channel=new Channel(channelName, "http://slashdot.org/slashdot.rdf");
76          services.add(channelName);
77          channels.put(channelName.toLowerCase(), channel);
78  
79          channelName="ars";
80          channel=new Channel(channelName, "http://arstechnica.com/etc/rdf/ars.rdf");
81          services.add(channelName);
82          channels.put(channelName.toLowerCase(), channel);
83  
84          channelName="cnn";
85          channel=new Channel(channelName, "http://www.newsisfree.com/HPE/xml/feeds/15/2315.xml");
86          services.add(channelName);
87          channels.put(channelName.toLowerCase(), channel);
88  
89          channelName="wired";
90          channel=new Channel(channelName, "http://www.wired.com/news_drop/netcenter/netcenter.rdf");
91          services.add(channelName);
92          channels.put(channelName.toLowerCase(), channel);
93  
94          channelName="zdnet";
95          channel=new Channel(channelName, "http://p.moreover.com/cgi-local/page?feed=129&o=rss");
96          services.add(channelName);
97          channels.put(channelName.toLowerCase(), channel);
98  
99          channelName="word";
100         channel=new Channel(channelName, "http://dictionary.reference.com/wordoftheday/wotd.rss");
101         services.add(channelName);
102         channels.put(channelName.toLowerCase(), channel);
103 
104         channelName="jsurf";
105         channel=new Channel(channelName, "http://www.jsurfer.org/backend.php");
106         services.add(channelName);
107         channels.put(channelName.toLowerCase(), channel);
108 
109         channelName="robots";
110         channel=new Channel(channelName, "http://robots.net/rss/articles.xml");
111         services.add(channelName);
112         channels.put(channelName.toLowerCase(), channel);
113 
114         channelName="kuro";
115         channel=new Channel(channelName, "http://www.kuro5hin.org/backend.rdf");
116         services.add(channelName);
117         channels.put(channelName.toLowerCase(), channel);
118 
119         channelName="fark";
120         channel=new Channel(channelName, "http://www.newsisfree.com/HPE/xml/feeds/17/1717.xml");
121         services.add(channelName);
122         channels.put(channelName.toLowerCase(), channel);
123 
124         channelName="jworld";
125         channel=new Channel(channelName, "http://www.newsisfree.com/HPE/xml/feeds/59/959.xml");
126         services.add(channelName);
127         channels.put(channelName.toLowerCase(), channel);
128 
129         channelName="mets";
130         channel=new Channel(channelName, "http://p.moreover.com/cgi-local/page?feed=559&o=rss");
131         services.add(channelName);
132         channels.put(channelName.toLowerCase(), channel);
133 
134         channelName="islanders";
135         channel=new Channel(channelName, "http://p.moreover.com/cgi-local/page?feed=336&o=rss");
136         services.add(channelName);
137         channels.put(channelName.toLowerCase(), channel);
138 
139         channelName="nfl";
140         channel=new Channel(channelName, "http://www.newsisfree.com/HPE/xml/feeds/52/1852.xml");
141         services.add(channelName);
142         channels.put(channelName.toLowerCase(), channel);
143 
144         channelName="deals";
145         channel=new Channel(channelName, "http://www.totaldeals.com/rss/new.asp");
146         services.add(channelName);
147         channels.put(channelName.toLowerCase(), channel);
148 
149         channelName="cnet";
150         channel=new Channel(channelName, "http://www.moreover.com/cgi-local/page?feed=139&o=rss");
151         services.add(channelName);
152         channels.put(channelName.toLowerCase(), channel);
153     }
154 
155     /***
156      * Constructor for HeadlinesModule.
157      *
158      * @param bot
159      */
160     public HeadlinesModule(AIMBot bot) {
161         super(bot);
162     }
163 
164     /***
165      * @see com.levelonelabs.aimbot.BotModule#getName()
166      */
167     public String getName() {
168         return "Headlines Module";
169     }
170 
171 
172     /***
173      * @see com.levelonelabs.aimbot.BotModule#getServices()
174      */
175     public ArrayList getServices() {
176         return services;
177     }
178 
179 
180     /***
181      * @see com.levelonelabs.aimbot.BotModule#help()
182      */
183     public String help() {
184         StringBuffer sb=new StringBuffer();
185         sb.append("Type one of the following site names to see the latest headlines:\n");
186         for(Enumeration enumer=channels.keys(); enumer.hasMoreElements();) {
187             sb.append("<B>"+enumer.nextElement()+"</B>     ");
188         }
189         return sb.toString();
190     }
191 
192 
193     /***
194      * @see com.levelonelabs.aimbot.BotModule#performService(AIMBuddy, String)
195      */
196     public void performService(AIMBuddy buddy, String query) {
197         String channelName=query.trim().toLowerCase();
198         Channel chan=(Channel) channels.get(channelName);
199         if(chan != null) {
200             chan.update();
201             String results="";
202             if(chan.getErrorMessage() != null) {
203                 super.sendMessage(buddy, "\nERROR["+chan.getErrorMessage()+"]");
204                 results="Cached Results:";
205             }
206             ArrayList arr=chan.getItems();
207             if(arr.size() > 0) {
208                 for(int i=0; (i < arr.size()) && (i < HeadlinesModule.MAX_NUM_HEADLINES); i++) {
209                     Item item=(Item) arr.get(i);
210                     results+=("<BR>"+item);
211                 }
212                 super.sendMessage(buddy, results);
213             }
214         } else {
215             super.sendMessage(buddy, "ERROR:\n"+help());
216         }
217     }
218 }