Package com.uwyn.drone.modules

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

/*
* 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
*
* Module which lets the bot do a CTCP action.
* The code tries to follow the <a href="http://www.irchelp.org/irchelp/rfc/ctcpspec.html">CTCP specs</a>
*
* $Id: $
*/
package com.uwyn.drone.modules;

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.AttributeCode;
import com.uwyn.drone.protocol.Ctcp;
import com.uwyn.drone.protocol.ServerMessage;
import com.uwyn.drone.protocol.commands.Privmsg;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Do extends AbstractModule
{
  private static final String[]  MESSAGE_COMMANDS = new String[] {"do"};
  private static final Pattern  DO_PATTERN = Pattern.compile("^\\s*(#[^\\s]+)\\s+(.+)\\s*$");
  private static final HashMap  HELPMAP = new HashMap();
 
  static
  {
    HELPMAP.put(null,
          AttributeCode.BOLD + "Do" + AttributeCode.BOLD + " makes the bot do an action on a channel." + AttributeCode.ENDLINE +
          "It can be used to make the bot appear human, or to do some action" + AttributeCode.ENDLINE +
          "something in an anonymous fashion." + AttributeCode.ENDLINE +
          "For more information on a specific command, type        " + AttributeCode.ENDLINE +
          AttributeCode.BOLD + "/msg $botnick help $modulename <command>" + AttributeCode.BOLD + "." + AttributeCode.ENDLINE +
          " " + AttributeCode.ENDLINE +
          AttributeCode.BOLD + "Privmsg commands" + AttributeCode.BOLD + "" + AttributeCode.ENDLINE +
          "    DO   do an action in a message in a channel" + AttributeCode.ENDLINE);
   
    HELPMAP.put("do",
          "Syntax: " + AttributeCode.BOLD + "do <#channel> <action>" + AttributeCode.BOLD + "" + AttributeCode.ENDLINE +
          " " + AttributeCode.ENDLINE +
          "Does the provided action in the channel. Note that this" + AttributeCode.ENDLINE +
          "normally only works if the bot has joined the channel." + AttributeCode.ENDLINE);
  }
 
  public String getName()
  {
    return "DO";
  }
 
  public String getDescription()
  {
    return "Lets the bot do something on a channel.";
  }
 
  public Map getHelpMap()
  {
    return HELPMAP;
  }
 
  public String[] getMessageCommands()
  {
    return MESSAGE_COMMANDS;
  }
 
  public boolean processesChannelMessages()
  {
    return true;
  }
 
  public void messageCommand(Bot bot, String nick, String command, String arguments, ServerMessage fullMessage)
  throws CoreException
  {
    if (command.equals("do"))
    {
      if (null == arguments ||
        0 == arguments.length())
      {
        bot.send(new Privmsg(nick, "You need to provide an argument to the command."));
        return;
      }
     
      Matcher seen_matcher = DO_PATTERN.matcher(arguments);
     
      if (!seen_matcher.matches() ||
        seen_matcher.groupCount() != 2)
      {
        bot.send(new Privmsg(nick, "Invalid syntax '" + command + " " + arguments + "'"));
        return;
      }
     
      // obtain the requested channel
      String channel_name = seen_matcher.group(1).toLowerCase();
      String message = seen_matcher.group(2);
      Channel  channel = bot.getServer().getChannel(channel_name);
      if (null == channel)
      {
        return;
      }
     
      // send the CTCP action message to the channel
      channel.send(Ctcp.escapeAction(message));
    }
  }
}
TOP

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

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.