/*
* 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: Regexp.java 1763 2005-04-14 12:03:57Z gbevin $
*/
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.ServerMessage;
import com.uwyn.rife.tools.StringUtils;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Regexp extends AbstractModule
{
private final static Pattern DETECT = Pattern.compile("^\\!s\\/(.*)\\/(.*)\\/(g?)\\s*");
private final static String[] COMMANDS = new String[] {"PART", "QUIT"};
private HashMap mLastMessages = new HashMap();
public String[] getRawCommands()
{
return COMMANDS;
}
private String getKey(Bot bot, String nick)
{
return bot.getName()+"\t"+nick;
}
public void rawCommand(Bot bot, String nick, String command, ServerMessage fullMessage)
throws CoreException
{
String key = getKey(bot, nick);
if (command.equals("QUIT"))
{
mLastMessages.remove(key);
}
else if (command.equals("PART"))
{
HashMap last_channels = null;
SoftReference reference = (SoftReference)mLastMessages.get(key);
if (last_channels != null)
{
last_channels.remove(fullMessage.getParameters().get(0));
}
}
}
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;
}
String key = getKey(bot, nick);
HashMap last_channels = null;
SoftReference reference = (SoftReference)mLastMessages.get(key);
if (reference !=null)
{
last_channels = (HashMap)reference.get();
}
Matcher detect_matcher = DETECT.matcher(fullMessage.getTrailing());
if (detect_matcher.matches())
{
if (null == last_channels)
{
return;
}
String last_log = (String)last_channels.get(channel.getName());
if (null == last_log)
{
return;
}
try
{
Pattern log_pattern = Pattern.compile(detect_matcher.group(1));
Matcher log_matcher = log_pattern.matcher(last_log);
String result = null;
if ("g".equals(detect_matcher.group(3)))
{
result = log_matcher.replaceAll(detect_matcher.group(2));
}
else
{
result = log_matcher.replaceFirst(detect_matcher.group(2));
}
if (!last_log.equals(result))
{
if (result.length() > 120)
{
result = result.substring(0, 120);
}
channel.send(nick+": "+result);
}
}
catch (PatternSyntaxException e)
{
List list = StringUtils.split(e.getMessage(), "\n");
Iterator list_it = list.iterator();
boolean first = true;
while (list_it.hasNext())
{
if (first)
{
channel.send(nick+": your regular expression was not valid, "+list_it.next());
}
else
{
channel.send(nick+": "+list_it.next());
}
first = false;
}
}
}
else
{
if (null == last_channels)
{
last_channels = new HashMap();
mLastMessages.put(key, new SoftReference(last_channels));
}
last_channels.put(channel.getName(), fullMessage.getTrailing());
}
}
}