Initial import.

This commit is contained in:
Erik C. Thauvin 2004-02-17 04:04:30 +00:00
commit cad709e484
42 changed files with 5961 additions and 0 deletions

View file

@ -0,0 +1,207 @@
/*
* @(#)CurrencyConverter.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.net.URL;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
/**
* Converts various currencies.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Feb 11, 2004
* @since 1.0
*/
public class CurrencyConverter implements Runnable
{
/**
* The exchange rates table URL.
*/
public static final String EXCHANGE_TABLE_URL = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml";
/**
* The exchange rates.
*/
private static final Map EXCHANGE_RATES = new TreeMap();
/**
* The last exchange rates table publication date.
*/
private static String s_date = "";
/**
* The bot.
*/
private final Mobibot _bot;
/**
* The actual currency _query.
*/
private final String _query;
/**
* The nick of the person who sent the message.
*/
private final String _sender;
/**
* Creates a new CurrencyConverter object.
*
* @param bot The bot.
* @param sender The nick of the person who sent the message.
* @param query The currency query.
* @param date The current date.
*/
public CurrencyConverter(Mobibot bot, String sender, String query, String date)
{
_bot = bot;
_sender = sender;
_query = query;
if (!s_date.equals(date))
{
EXCHANGE_RATES.clear();
}
}
// Converts specified currencies.
public void run()
{
if (EXCHANGE_RATES.isEmpty())
{
try
{
final SAXBuilder builder = new SAXBuilder();
builder.setIgnoringElementContentWhitespace(true);
final Document doc = builder.build(new URL(EXCHANGE_TABLE_URL));
final Element root = doc.getRootElement();
final Namespace ns = root.getNamespace("");
final Element cubeRoot = root.getChild("Cube", ns);
final Element cubeTime = cubeRoot.getChild("Cube", ns);
s_date = cubeTime.getAttribute("time").getValue();
final List cubes = cubeTime.getChildren();
Element cube;
for (int i = 0; i < cubes.size(); i++)
{
cube = (Element) cubes.get(i);
EXCHANGE_RATES.put(cube.getAttribute("currency").getValue(), cube.getAttribute("rate").getValue());
}
EXCHANGE_RATES.put("EUR", "1");
}
catch (JDOMException e)
{
_bot.getLogger().debug("Unable to parse the exchange rates table.", e);
_bot.sendNotice(_sender, "An error has occurred while parsing the exchange rates table.");
}
catch (IOException e)
{
_bot.getLogger().debug("Unable to fetch the exchange rates table.", e);
_bot.sendNotice(_sender,
"An error has occurred while fetching the exchange rates table: " + e.getMessage());
}
}
if (!EXCHANGE_RATES.isEmpty())
{
if (_query.matches("\\d+([,\\d]+)?(\\.\\d+)? [a-z]{3}+ to [a-z]{3}+"))
{
final String[] cmds = _query.split(" ");
if (cmds.length == 4)
{
if (cmds[3].equals(cmds[1]))
{
_bot.sendNotice(_sender, "You're kidding, right?");
}
else
{
try
{
final double amt = Double.parseDouble(cmds[0].replaceAll(",", ""));
final double from = Double.parseDouble((String) EXCHANGE_RATES.get(cmds[1].toUpperCase()));
double to = Double.parseDouble((String) EXCHANGE_RATES.get(cmds[3].toUpperCase()));
_bot.sendNotice(_bot.getChannel(),
NumberFormat.getCurrencyInstance(Locale.US).format(amt).substring(1) + ' ' +
cmds[1].toUpperCase() + " = " +
NumberFormat.getCurrencyInstance(Locale.US).format((amt * to) / from)
.substring(1) + ' ' + cmds[3].toUpperCase());
}
catch (NullPointerException e)
{
_bot.sendNotice(_sender,
"The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
}
}
}
}
else
{
_bot.helpResponse(_sender, Mobibot.CURRENCY_CMD + ' ' + _query);
_bot.sendNotice(_sender, "The supported currencies are: " + EXCHANGE_RATES.keySet().toString());
}
}
else
{
_bot.getLogger().debug("The exchange rate table is empty.");
_bot.sendNotice(_sender, "Sorry, but the exchange rate table is empty.");
}
}
}

View file

@ -0,0 +1,137 @@
/*
* @(#)EntryComment.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
/**
* The class used to store comments associated to a specific entry.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Jan 31, 2004
* @since 1.0
*/
public class EntryComment implements Serializable
{
/**
* The serial version UID.
*/
static final long serialVersionUID = 6957415292233553224L;
/**
* The creation date.
*/
private final Date _date = Calendar.getInstance().getTime();
private String _comment = "";
private String _nick = "";
/**
* Creates a new comment.
*
* @param comment The new comment.
* @param nick The nickname of the comment's author.
*/
public EntryComment(String comment, String nick)
{
_comment = comment;
_nick = nick;
}
/**
* Creates a new comment.
*/
protected EntryComment()
{
; // Required for serialization.
}
/**
* Sets the comment.
*
* @param comment The actual comment.
*/
public final void setComment(String comment)
{
_comment = comment;
}
/**
* Returns the comment.
*
* @return The comment.
*/
public final String getComment()
{
return _comment;
}
/**
* Returns the comment's creation date.
*
* @return The date.
*/
public final Date getDate()
{
return _date;
}
/**
* Sets the nickname of the author of the comment.
*
* @param nick The new nickname.
*/
public final void setNick(String nick)
{
_nick = nick;
}
/**
* Returns the nickname of the author of the comment.
*
* @return The nickname.
*/
public final String getNick()
{
return _nick;
}
}

View file

@ -0,0 +1,289 @@
/*
* @(#)EntryLink.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
* The class used to store link entries.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Jan 31, 2004
* @since 1.0
*/
public class EntryLink implements Serializable
{
/**
* The serial version UID.
*/
static final long serialVersionUID = 3676245542270899086L;
/**
* The creation date.
*/
private final Date _date = Calendar.getInstance().getTime();
/**
* The comments.
*/
private final List _comments = new ArrayList(0);
private String _link = "";
private String _login = "";
private String _nick = "";
private String _title = "No Title";
/**
* Creates a new entry.
*
* @param link The new entry's link.
* @param nick The nickname of the author of the link.
* @param login The login of the author of the link.
*/
public EntryLink(String link, String nick, String login)
{
_link = link;
_nick = nick;
_login = login;
}
/**
* Creates a new entry.
*
* @param link The new entry's link.
* @param title The new entry's title.
* @param nick The nickname of the author of the link.
* @param login The login of the author of the link.
*/
public EntryLink(String link, String title, String nick, String login)
{
_link = link;
_title = title;
_nick = nick;
_login = login;
}
/**
* Creates a new EntryLink object.
*/
protected EntryLink()
{
; // Required for serialization.
}
/**
* Sets a comment.
*
* @param index The comment's index.
* @param comment The actual comment.
* @param nick The nickname of the author of the comment.
*/
public final synchronized void setComment(int index, String comment, String nick)
{
if (index < _comments.size())
{
_comments.set(index, new EntryComment(comment, nick));
}
}
/**
* Returns a comment.
*
* @param index The comment's index.
*
* @return The specific comment.
*/
public final synchronized EntryComment getComment(int index)
{
return ((EntryComment) _comments.get(index));
}
/**
* Returns all the comments.
*
* @return The comments.
*/
public final synchronized EntryComment[] getComments()
{
return ((EntryComment[]) _comments.toArray(new EntryComment[0]));
}
/**
* Returns the total number of comments.
*
* @return The count of comments.
*/
public final synchronized int getCommentsCount()
{
return _comments.size();
}
/**
* Returns the comment's creation date.
*
* @return The date.
*/
public final synchronized Date getDate()
{
return _date;
}
/**
* Sets the comment's link.
*
* @param link The new link.
*/
public final synchronized void setLink(String link)
{
this._link = link;
}
/**
* Returns the comment's link.
*
* @return The link.
*/
public final synchronized String getLink()
{
return _link;
}
/**
* Set the comment's author login.
*
* @param login The new login.
*/
public final synchronized void setLogin(String login)
{
this._login = login;
}
/**
* Return's the comment's author login.
*
* @return The login;
*/
public final synchronized String getLogin()
{
return _login;
}
/**
* Sets the comment's author nickname.
*
* @param nick The new nickname.
*/
public final synchronized void setNick(String nick)
{
this._nick = nick;
}
/**
* Returns the comment's author nickname.
*
* @return The nickname.
*/
public final synchronized String getNick()
{
return _nick;
}
/**
* Sets the comment's title.
*
* @param title The new title.
*/
public final synchronized void setTitle(String title)
{
this._title = title;
}
/**
* Returns the comment's title.
*
* @return The title.
*/
public final synchronized String getTitle()
{
return _title;
}
/**
* Adds a new comment.
*
* @param comment The actual comment.
* @param nick The nickname of the author of the comment.
*
* @return The total number of comments for this entry.
*/
public final synchronized int addComment(String comment, String nick)
{
_comments.add(new EntryComment(comment, nick));
return (_comments.size() - 1);
}
/**
* Deletes a specific comment.
*
* @param index The index of the comment to delete.
*/
public final synchronized void deleteComment(int index)
{
if (index < _comments.size())
{
_comments.remove(index);
}
}
/**
* Returns true if the entry has comments.
*
* @return true if there are comments, false otherwise.
*/
public final synchronized boolean hasComments()
{
return (_comments.size() > 0);
}
}

View file

@ -0,0 +1,163 @@
/*
* @(#)FeedReader.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import org.crazybob.rss.Channel;
import org.crazybob.rss.Item;
import org.crazybob.rss.Parser;
import org.crazybob.rss.UrlLoader;
import org.crazybob.rss.UrlLoader.Response;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
/**
* Reads a RSS feed.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Feb 1, 2004
* @since 1.0
*/
public class FeedReader implements Runnable
{
/**
* The maximum number of feed items to display.
*/
private static final int MAX_ITEMS = 5;
/**
* The tab indent (4 spaces).
*/
private static final String TAB_INDENT = " ";
/**
* The bot.
*/
private final Mobibot _bot;
/**
* The nick of the person who sent the message.
*/
private final String _sender;
/**
* The URL to fetch.
*/
private final String _url;
/**
* Creates a new FeedReader object.
*
* @param bot The bot.
* @param sender The nick of the person who sent the message.
* @param url The URL to fetch.
*/
public FeedReader(Mobibot bot, String sender, String url)
{
_bot = bot;
_sender = sender;
_url = url;
}
/**
* Fetches the Feed's items.
*/
public void run()
{
List items = null;
try
{
final Response response = new UrlLoader().load(_url, _bot.getFeedLastMod());
if (response != null)
{
_bot.setFeedLastMod(response.getLastModified());
final Channel chan = new Parser().parse(new SAXBuilder().build(new StringReader(response.getBody())));
items = chan.getItems();
_bot.setFeedItems(items);
}
}
catch (JDOMException e)
{
_bot.getLogger().debug("Unable to parse the feed.", e);
_bot.sendNotice(_sender, "An error has occurred while parsing the feed.");
}
catch (IOException e)
{
_bot.getLogger().debug("Unable to fetch the feed.", e);
_bot.sendNotice(_sender, "An error has occurred while fetching the feed: " + e.getMessage());
}
if (items == null)
{
items = _bot.getFeedItems();
}
if ((items != null) && (!items.isEmpty()))
{
Item item;
int i = 0;
final Iterator it = items.iterator();
while (it.hasNext() && (i < MAX_ITEMS))
{
item = (Item) it.next();
_bot.sendNotice(_sender, item.getTitle());
_bot.sendNotice(_sender, TAB_INDENT + '<' + item.getLink() + '>');
i++;
}
if (_bot.getFeedLastMod().length() > 0)
{
_bot.sendNotice(_sender, "Last Updated: " + _bot.getFeedLastMod());
}
}
}
}

View file

@ -0,0 +1,170 @@
/*
* @(#)GoogleSearch.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import com.google.soap.search.GoogleSearchFault;
import net.thauvin.google.GoogleSearchBean;
import org.jibble.pircbot.Colors;
/**
* Performs a Google search or spell checking query.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Feb 7, 2004
* @since 1.0
*/
public class GoogleSearch implements Runnable
{
/**
* The maximum number of Google results to display.
*/
private static final int MAX_GOOGLE = 5;
/**
* The Google search bean.
*/
private static final GoogleSearchBean GOOGLE_BEAN = new GoogleSearchBean();
/**
* The tab indent (4 spaces).
*/
private static final String TAB_INDENT = " ";
/**
* The bot.
*/
private final Mobibot _bot;
/**
* The Google API key.
*/
private final String _key;
/**
* The search query.
*/
private final String _query;
/**
* The nick of the person who sent the message.
*/
private final String _sender;
/**
* Spell Checking query flag.
*/
private final boolean _isSpellQuery;
/**
* Creates a new GoogleSearch object.
*
* @param bot The bot.
* @param key The Google API key.
* @param sender The nick of the person who sent the message.
* @param query The Google query
* @param isSpellQuery Set to true if the query is a Spell Checking query
*/
public GoogleSearch(Mobibot bot, String key, String sender, String query, boolean isSpellQuery)
{
_bot = bot;
_key = key;
_sender = sender;
_query = query;
_isSpellQuery = isSpellQuery;
}
/**
* Main processing method.
*/
public void run()
{
GOOGLE_BEAN.setKey(_key);
if (_isSpellQuery)
{
try
{
final String r = GOOGLE_BEAN.getSpellingSuggestion(_query);
if ((r != null) && (r.length() > 0))
{
_bot.sendNotice(_sender, Mobibot.unescapeXml(r));
}
else
{
_bot.sendNotice(_sender, "You've just won our spelling bee contest.");
}
}
catch (GoogleSearchFault e)
{
_bot.getLogger().warn("Unable to spell: " + _query, e);
_bot.sendNotice(_sender, "An error has occurred: " + e.getMessage());
}
}
else
{
try
{
GOOGLE_BEAN.getGoogleSearch(_query, GoogleSearchBean.DEFAULT_START, MAX_GOOGLE,
GoogleSearchBean.DEFAULT_FILTER, GoogleSearchBean.DEFAULT_RESTRICT,
GoogleSearchBean.DEFAULT_SAFE_SEARCH, GoogleSearchBean.DEFAULT_LR);
if (GOOGLE_BEAN.isValidResult())
{
for (int i = 0; i < GOOGLE_BEAN.getResultElementsCount(); i++)
{
_bot.sendNotice(_sender,
GOOGLE_BEAN.getResultElementProperty(i, "title").replaceAll("<([bB]|/[bB])>",
Colors.BOLD));
_bot.sendNotice(_sender, TAB_INDENT + '<' + GOOGLE_BEAN.getResultElementProperty(i, "url") +
'>');
}
}
}
catch (GoogleSearchFault e)
{
_bot.getLogger().warn("Unable to search in Google for: " + _query, e);
_bot.sendNotice(_sender, "An error has occurred: " + e.getMessage());
}
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,160 @@
/*
* @(#)StockQuote.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.IOException;
/**
* Retrieves a stock quote from Yahoo!.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Feb 7, 2004
* @since 1.0
*/
public class StockQuote implements Runnable
{
/**
* The Yahoo! stock quote URL.
*/
private static final String YAHOO_URL = "http://finance.yahoo.com/d/quotes.csv?&f=snl1d1t1c1oghv&e=.csv&s=";
/**
* The bot.
*/
private final Mobibot _bot;
/**
* The nick of the person who sent the message.
*/
private final String _sender;
/**
* The stock symbol.
*/
private final String _symbol;
/**
* Creates a new StockQuote object.
*
* @param bot The bot.
* @param sender The nick of the person who sent the message.
* @param symbol The stock symbol.
*/
public StockQuote(Mobibot bot, String sender, String symbol)
{
_bot = bot;
_sender = sender;
_symbol = symbol;
}
/**
* Returns the specified stock quote.
*/
public void run()
{
try
{
final HttpClient client = new HttpClient();
client.setConnectionTimeout(Mobibot.CONNECT_TIMEOUT);
client.setTimeout(Mobibot.CONNECT_TIMEOUT);
final GetMethod getMethod = new GetMethod(YAHOO_URL + _symbol.toUpperCase());
client.executeMethod(getMethod);
final String[] quote = getMethod.getResponseBodyAsString().split(",");
if (quote.length > 0)
{
if ((quote.length > 3) && (!"\"N/A\"".equalsIgnoreCase(quote[3])))
{
_bot.sendNotice(_bot.getChannel(),
"Symbol: " + quote[0].replaceAll("\"", "") + " [" + quote[1].replaceAll("\"", "") +
']');
if (quote.length > 5)
{
_bot.sendNotice(_bot.getChannel(), "Last Trade: " + quote[2] + " (" + quote[5] + ')');
}
else
{
_bot.sendNotice(_bot.getChannel(), "Last Trade: " + quote[2]);
}
if (quote.length > 4)
{
_bot.sendNotice(_sender,
"Time: " + quote[3].replaceAll("\"", "") + ' ' + quote[4].replaceAll("\"", ""));
}
if (quote.length > 6)
{
_bot.sendNotice(_sender, "Open: " + quote[6]);
}
if (quote.length > 7)
{
_bot.sendNotice(_sender, "Day's Range: " + quote[7] + " - " + quote[8]);
}
if (quote.length > 9)
{
_bot.sendNotice(_sender, "Volume: " + quote[9]);
}
}
else
{
_bot.sendNotice(_sender, "Invalid ticker symbol.");
}
}
else
{
_bot.sendNotice(_sender, "No data returned.");
}
}
catch (IOException e)
{
_bot.getLogger().debug("Unable to retrieve stock quote for: " + _symbol, e);
_bot.sendNotice(_sender, "An error has occurred: " + e.getMessage());
}
}
}

View file

@ -0,0 +1,180 @@
/*
* @(#)Weather.java
*
* Copyright (c) 2004, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the author nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.mobibot;
import net.sf.jweather.metar.Metar;
import net.sf.jweather.metar.SkyCondition;
import net.sf.jweather.metar.WeatherCondition;
import java.util.Iterator;
/**
* Fetches the weather data from a specific station ID.
*
* @author Erik C. Thauvin
* @version $Revision$, $Date$
*
* @created Feb 7, 2004
* @since 1.0
*/
public class Weather implements Runnable
{
/**
* The URL where the stations are listed.
*/
public static final String STATIONS_URL = "http://www.rap.ucar.edu/weather/surface/stations.txt";
/**
* The bot.
*/
private final Mobibot _bot;
/**
* The nick of the person who sent the message.
*/
private final String _sender;
/**
* The station ID.
*/
private final String _station;
/**
* The private message flag.
*/
private final boolean _isPrivate;
/**
* Creates a new Weather object.
*
* @param bot The bot.
* @param sender The nick of the person who sent the message.
* @param station The station ID.
* @param isPrivate Set to true is the response should be send as a private message.
*/
public Weather(Mobibot bot, String sender, String station, boolean isPrivate)
{
_bot = bot;
_sender = sender;
_station = station;
_isPrivate = isPrivate;
}
/**
* Main processing method.
*/
public void run()
{
if (_station.length() == 4)
{
final Metar metar = net.sf.jweather.Weather.getMetar(_station);
if (metar != null)
{
Float result;
_bot.send(_sender, "Station ID: " + metar.getStationID(), _isPrivate);
result = metar.getWindSpeedInMPH();
if (result != null)
{
_bot.send(_sender, "Wind Speed: " + result + " mph, " + metar.getWindSpeedInKnots() + " knots",
_isPrivate);
}
result = metar.getVisibility();
if (result != null)
{
if (!metar.getVisibilityLessThan())
{
_bot.send(_sender, "Visibility: " + result + " mile(s)", _isPrivate);
}
else
{
_bot.send(_sender, "Visibility: < " + result + " mile(s)", _isPrivate);
}
}
result = metar.getPressure();
if (result != null)
{
_bot.send(_sender, "Pressure: " + result + " in Hg", _isPrivate);
}
result = metar.getTemperatureInCelsius();
if (result != null)
{
_bot.send(_sender, "Temperature: " + result + " C, " + metar.getTemperatureInFahrenheit() + " F",
_isPrivate);
}
if (metar.getWeatherConditions() != null)
{
final Iterator it = metar.getWeatherConditions().iterator();
while (it.hasNext())
{
final WeatherCondition weatherCondition = (WeatherCondition) it.next();
_bot.send(_sender, weatherCondition.getNaturalLanguageString(), _isPrivate);
}
}
if (metar.getSkyConditions() != null)
{
final Iterator it = metar.getSkyConditions().iterator();
while (it.hasNext())
{
final SkyCondition skyCondition = (SkyCondition) it.next();
_bot.send(_sender, skyCondition.getNaturalLanguageString(), _isPrivate);
}
}
return;
}
}
_bot.send(_sender,
"For a listing of the supported ICAO weather station IDs please visit: <" + STATIONS_URL + '>',
_isPrivate);
}
}