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