191 lines
4.3 KiB
Java
191 lines
4.3 KiB
Java
/**
|
|
* Copyright (C) 2018-2021 Christian Pierre MOMON <cmomon@april.org>
|
|
*
|
|
* This file is part of (April) Hebdobot.
|
|
*
|
|
* Hebdobot is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* Hebdobot is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with Hebdobot. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
package org.april.hebdobot.bot.hooks;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
|
|
import org.april.hebdobot.HebdobotException;
|
|
import org.april.hebdobot.bot.Hebdobot;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import fr.devinsy.strings.StringList;
|
|
|
|
/**
|
|
* The class Hooker, a hook manager.
|
|
*/
|
|
public class Hooker
|
|
{
|
|
private static final Logger logger = LoggerFactory.getLogger(Hooker.class);
|
|
|
|
private ArrayList<Hook> hooks;
|
|
|
|
/**
|
|
* Instantiates a new hook manager.
|
|
*/
|
|
public Hooker()
|
|
{
|
|
this.hooks = new ArrayList<>(30);
|
|
}
|
|
|
|
/**
|
|
* Adds the.
|
|
*
|
|
* @param hook
|
|
* the hook
|
|
*/
|
|
public void add(final Hook hook)
|
|
{
|
|
if (hook != null)
|
|
{
|
|
this.hooks.add(hook);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Attempt help.
|
|
*
|
|
* @param message
|
|
* the message
|
|
* @return the string list
|
|
* @throws HebdobotException
|
|
* the hebdobot exception
|
|
*/
|
|
public StringList attemptHelp(final String message) throws HebdobotException
|
|
{
|
|
StringList result;
|
|
|
|
boolean ended = false;
|
|
Iterator<Hook> iterator = this.hooks.iterator();
|
|
result = null;
|
|
while (!ended)
|
|
{
|
|
if (iterator.hasNext())
|
|
{
|
|
Hook hook = iterator.next();
|
|
StringList help = hook.attemptHelp(message);
|
|
|
|
if (help != null)
|
|
{
|
|
ended = true;
|
|
result = help;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ended = true;
|
|
result = null;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Attempt process.
|
|
*
|
|
* @param bot
|
|
* the bot
|
|
* @param channel
|
|
* the channel
|
|
* @param sender
|
|
* the sender
|
|
* @param login
|
|
* the login
|
|
* @param hostname
|
|
* the hostname
|
|
* @param message
|
|
* the message
|
|
* @return true, if successful
|
|
* @throws HebdobotException
|
|
* the hebdobot exception
|
|
*/
|
|
public boolean attemptProcess(final Hebdobot bot, final String channel, final String sender, final String login, final String hostname,
|
|
final String message) throws HebdobotException
|
|
{
|
|
boolean result;
|
|
|
|
boolean ended = false;
|
|
Iterator<Hook> iterator = this.hooks.iterator();
|
|
result = false;
|
|
while (!ended)
|
|
{
|
|
if (iterator.hasNext())
|
|
{
|
|
Hook hook = iterator.next();
|
|
boolean hookResult = hook.attemptProcess(bot, channel, sender, login, hostname, message);
|
|
|
|
if (hookResult)
|
|
{
|
|
ended = true;
|
|
result = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ended = true;
|
|
result = false;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Clear.
|
|
*/
|
|
public void clear()
|
|
{
|
|
this.hooks.clear();
|
|
}
|
|
|
|
public StringList getHookNames()
|
|
{
|
|
StringList result;
|
|
|
|
result = new StringList(this.hooks.size());
|
|
|
|
for (Hook hook : this.hooks)
|
|
{
|
|
result.add(hook.getName());
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Size.
|
|
*
|
|
* @return the int
|
|
*/
|
|
public int size()
|
|
{
|
|
int result;
|
|
|
|
result = this.hooks.size();
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|