hebdobot/src/org/april/hebdobot/model/UserAliases.java

191 lines
4.7 KiB
Java

/**
* Copyright (C) 2011-2013 Nicolas Vinot <aeris@imirhil.fr>
* Copyright (C) 2017-2018 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.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.april.hebdobot.HebdobotException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fr.devinsy.strings.StringList;
/**
* The Class UserAliases.
*/
public class UserAliases extends HashMap<String, String>
{
private static final long serialVersionUID = -7563433397065777556L;
private static final Logger logger = LoggerFactory.getLogger(UserAliases.class);
/**
* Instantiates a new user aliases.
*/
public UserAliases()
{
super();
}
/**
* Instantiates a new user aliases.
*
* @param source
* the source
* @throws HebdobotException
* the hebdobot exception
*/
public UserAliases(final File source) throws HebdobotException
{
super();
try
{
StringList lines = new StringList(FileUtils.readLines(source, StandardCharsets.UTF_8));
for (String line : lines)
{
if ((StringUtils.isNotBlank(line)) && (!line.startsWith("#")))
{
String[] tokens = line.split("=");
String realName = tokens[0];
String[] nicks = tokens[1].split(",");
for (String nick : nicks)
{
put(nick, realName);
}
}
}
}
catch (FileNotFoundException exception)
{
logger.warn("File not found.");
}
catch (IOException exception)
{
throw new HebdobotException("IO error.", exception);
}
}
/**
* Instantiates a new user aliases.
*
* @param source
* the source
*/
public UserAliases(final UserAliases source)
{
super(source);
}
/**
* Gets the real name.
*
* @param nick
* the nick
* @return the real name
*/
public String getRealName(final String nick)
{
String result;
String realName = get(nick);
if (realName == null)
{
boolean ended = false;
Iterator<String> iterator = keySet().iterator();
while (!ended)
{
if (iterator.hasNext())
{
String currentNick = iterator.next();
if (nick.toLowerCase().contains(currentNick.toLowerCase()))
{
ended = true;
realName = get(currentNick);
}
}
else
{
realName = nick;
ended = true;
}
}
}
result = realName + " (" + nick + ")";
//
return result;
}
/**
* To line string.
*
* @return the string
*/
public String toLineString()
{
String result;
StringList buffer = new StringList();
for (String nick : new StringList(this.keySet()).sort())
{
buffer.append(getRealName(nick));
buffer.append(", ");
}
buffer.removeLast();
result = buffer.toString();
//
return result;
}
/* (non-Javadoc)
* @see java.util.AbstractMap#toString()
*/
@Override
public String toString()
{
String result;
StringList buffer = new StringList();
for (String nick : new StringList(this.keySet()).sort())
{
buffer.appendln(getRealName(nick));
}
result = buffer.toString();
//
return result;
}
}