Package com.uwyn.drone.modules

Source Code of com.uwyn.drone.modules.HttpError

/*
* Copyright 2002-2005 Uwyn bvba/sprl <info[remove] at uwyn dot com>
* Distributed under the terms of the GNU Lesser General Public
* License, v2.1 or later
*
* $Id: HttpError.java 1953 2005-05-25 09:45:32Z gbevin $
*/
package com.uwyn.drone.modules;

import com.uwyn.drone.Droned;
import com.uwyn.drone.core.AbstractModule;
import com.uwyn.drone.core.Bot;
import com.uwyn.drone.core.Channel;
import com.uwyn.drone.core.exceptions.CoreException;
import com.uwyn.drone.protocol.ServerMessage;
import com.uwyn.rife.tools.HttpUtils;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpError extends AbstractModule
{
  public static final Pattern  URL = Pattern.compile("((?:http)s?://(?:%[\\p{Digit}A-Fa-f][\\p{Digit}A-Fa-f]|[\\-_\\.!~*';\\|/?:@#&=\\+$,\\p{Alnum}])+[\\-~*'\\|/@#&=\\+$\\p{Alnum}])", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
 
  public void channelMessage(Bot bot, Channel channel, String nick, ServerMessage fullMessage)
  throws CoreException
  {
    if (nick.equals(bot.getConnectedNick()) || null == fullMessage.getTrailing() || 0 == fullMessage.getTrailing().length())
    {
      return;
    }

    Matcher url_matcher = URL.matcher(fullMessage.getTrailing());
    if (url_matcher.find())
    {
      String url = url_matcher.group(1).trim();
      try
      {
        URL url_parsed = new URL(url);
        if (url_parsed.getPort() != -1 &&
          url_parsed.getPort() != 80)
        {
          return;
        }
       
        try
        {
          HttpUtils.Request request = new HttpUtils.Request(url);
          request.header("User-Agent", "Drone/"+Droned.getVersion());
          HttpUtils.Page page = request.retrieve(false);
          if (page.getResponseCode() >= 400 && page.getResponseCode() < 500)
          {
            channel.send("That URL gave the following error code: "+page.getResponseCode()+" "+page.getResponseMessage());
          }
        }
        catch (IOException e)
        {
          // drill down to the root cause
          Throwable e2 = e;
          while (e2.getCause() != null &&
               e2.getCause() != e2)
          {
            e2 = e2.getCause();
          }
          channel.send("That URL gave the following error: "+e2.getClass().getName()+", "+e2.getMessage());
        }
      }
      catch (MalformedURLException e)
      {
        channel.send("That URL gave the following error: "+e.getClass().getName()+", "+e.getMessage());
      }
    }
  }
}
TOP

Related Classes of com.uwyn.drone.modules.HttpError

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.