141 lines
3.6 KiB
Java
141 lines
3.6 KiB
Java
|
/**
|
||
|
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
|
||
|
* Copyright (C) 2017 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.model;
|
||
|
|
||
|
import java.io.File;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.IOException;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.Iterator;
|
||
|
import java.util.Map;
|
||
|
import java.util.Properties;
|
||
|
|
||
|
import org.april.hebdobot.HebdobotException;
|
||
|
import org.april.hebdobot.util.HebdobotUtils;
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
|
||
|
/**
|
||
|
* The Class UserAliases.
|
||
|
*/
|
||
|
public class UserAliases
|
||
|
{
|
||
|
private static final Logger logger = LoggerFactory.getLogger(UserAliases.class);
|
||
|
|
||
|
private final Map<String, String> aliases;
|
||
|
|
||
|
/**
|
||
|
* Instantiates a new user aliases.
|
||
|
*/
|
||
|
public UserAliases()
|
||
|
{
|
||
|
this.aliases = new HashMap<String, String>();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Instantiates a new user aliases.
|
||
|
*
|
||
|
* @param source
|
||
|
* the source
|
||
|
* @throws HebdobotException
|
||
|
* the hebdobot exception
|
||
|
*/
|
||
|
public UserAliases(final File source) throws HebdobotException
|
||
|
{
|
||
|
this.aliases = new HashMap<String, String>();
|
||
|
|
||
|
try
|
||
|
{
|
||
|
|
||
|
Properties users = HebdobotUtils.loadProperties(source);
|
||
|
|
||
|
for (String realName : users.stringPropertyNames())
|
||
|
{
|
||
|
String[] nicks = users.getProperty(realName).split(",");
|
||
|
|
||
|
for (String nick : nicks)
|
||
|
{
|
||
|
this.aliases.put(nick, realName);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
catch (FileNotFoundException exception)
|
||
|
{
|
||
|
logger.warn("File not found.");
|
||
|
}
|
||
|
catch (IOException exception)
|
||
|
{
|
||
|
throw new HebdobotException("IO error.", exception);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Gets the real name.
|
||
|
*
|
||
|
* @param nick
|
||
|
* the nick
|
||
|
* @return the real name
|
||
|
*/
|
||
|
public String getRealName(final String nick)
|
||
|
{
|
||
|
String result;
|
||
|
|
||
|
result = this.aliases.get(nick);
|
||
|
|
||
|
if (result == null)
|
||
|
{
|
||
|
boolean ended = false;
|
||
|
Iterator<String> iterator = this.aliases.keySet().iterator();
|
||
|
while (!ended)
|
||
|
{
|
||
|
if (iterator.hasNext())
|
||
|
{
|
||
|
String currentNick = iterator.next();
|
||
|
|
||
|
if (nick.toLowerCase().contains(currentNick.toLowerCase()))
|
||
|
{
|
||
|
ended = true;
|
||
|
result = this.aliases.get(currentNick) + " ( " + nick + " )";
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
result = nick;
|
||
|
ended = true;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Put all.
|
||
|
*
|
||
|
* @param source
|
||
|
* the source
|
||
|
*/
|
||
|
public void putAll(final UserAliases source)
|
||
|
{
|
||
|
this.aliases.putAll(source.aliases);
|
||
|
}
|
||
|
}
|