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

191 lines
4.7 KiB
Java
Raw Normal View History

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;
import java.nio.charset.StandardCharsets;
2017-12-25 00:29:57 +01:00
import java.util.HashMap;
import java.util.Iterator;
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;
import fr.devinsy.util.strings.StringList;
2017-12-25 00:29:57 +01:00
/**
* The Class UserAliases.
*/
public class UserAliases extends HashMap<String, String>
2017-12-25 00:29:57 +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()
{
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
{
super();
2017-12-25 00:29:57 +01:00
try
{
StringList lines = new StringList(FileUtils.readLines(source, StandardCharsets.UTF_8));
2017-12-25 00:29:57 +01:00
for (String line : lines)
2017-12-25 00:29:57 +01:00
{
if ((StringUtils.isNotBlank(line)) && (!line.startsWith("#")))
2017-12-25 00:29:57 +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;
String realName = get(nick);
2017-12-25 00:29:57 +01:00
if (realName == null)
2017-12-25 00:29:57 +01:00
{
boolean ended = false;
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;
realName = get(currentNick);
2017-12-25 00:29:57 +01:00
}
}
else
{
realName = nick;
2017-12-25 00:29:57 +01:00
ended = true;
}
}
}
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;
}
/* (non-Javadoc)
* @see java.util.AbstractMap#toString()
2017-12-25 00:29:57 +01:00
*/
@Override
public String toString()
2017-12-25 00:29:57 +01:00
{
String result;
StringList buffer = new StringList();
2017-12-26 01:18:36 +01:00
for (String nick : new StringList(this.keySet()).sort())
{
buffer.appendln(getRealName(nick));
}
result = buffer.toString();
//
return result;
2017-12-25 00:29:57 +01:00
}
}