001/*
002 * Copyright 2010-2015 Institut Pasteur.
003 * 
004 * This file is part of Icy.
005 * 
006 * Icy is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 * 
011 * Icy is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014 * GNU General Public License for more details.
015 * 
016 * You should have received a copy of the GNU General Public License
017 * along with Icy. If not, see <http://www.gnu.org/licenses/>.
018 */
019package icy.network;
020
021import icy.util.StringUtil;
022
023import org.schwering.irc.lib.IRCEventListener;
024import org.schwering.irc.lib.IRCModeParser;
025import org.schwering.irc.lib.IRCUser;
026
027/**
028 * @author Stephane
029 */
030public abstract class IRCEventListenerImpl implements IRCEventListener
031{
032    /**
033     * Called on message receive event.
034     * 
035     * @param nick
036     *        nickname of the author of message if any (can be null).
037     * @param target
038     *        channel or nickname of destination if any (can be null).
039     * @param text
040     *        message
041     */
042    public abstract void onReceive(String nick, String target, String text);
043
044    public void onConnected()
045    {
046        if (getShowStatusMessages())
047            onReceive(null, null, "Connected");
048    }
049
050    @Override
051    public void onRegistered()
052    {
053        // redirect
054        onConnected();
055    }
056
057    @Override
058    public void onDisconnected()
059    {
060        if (getShowStatusMessages())
061            onReceive(null, null, "Disconnected.");
062    }
063
064    @Override
065    public void onError(String msg)
066    {
067        onReceive(null, null, "Error: " + msg);
068    }
069
070    @Override
071    public void onError(int num, String msg)
072    {
073        onReceive(null, null, "Error #" + num + ": " + msg);
074    }
075
076    @Override
077    public void onInvite(String chan, IRCUser u, String nickPass)
078    {
079        if (getShowStatusMessages())
080            onReceive(null, chan, u.getNick() + " invites " + nickPass + ".");
081    }
082
083    @Override
084    public void onJoin(String chan, IRCUser u)
085    {
086        if (getShowStatusMessages())
087            onReceive(null, chan, u.getNick() + " joins " + chan + ".");
088    }
089
090    @Override
091    public void onKick(String chan, IRCUser u, String nickPass, String msg)
092    {
093        if (getShowStatusMessages())
094            onReceive(null, chan, u.getNick() + " kicks " + nickPass + ".");
095    }
096
097    public void onLeave(String chan, IRCUser u, String msg)
098    {
099        if (getShowStatusMessages())
100        {
101            if (StringUtil.isEmpty(msg))
102                onReceive(null, chan, u.getNick() + " leaves " + chan + ".");
103            else
104                onReceive(null, chan, u.getNick() + " leaves " + chan + "(" + msg + ").");
105        }
106    }
107
108    @Override
109    public void onMode(IRCUser u, String nickPass, String mode)
110    {
111        if (getShowStatusMessages())
112            onReceive(null, null, u.getNick() + " sets modes " + mode + " " + nickPass + ".");
113    }
114
115    @Override
116    public void onMode(String chan, IRCUser u, IRCModeParser mp)
117    {
118        if (getShowStatusMessages())
119            onReceive(null, chan, u.getNick() + " sets mode: " + mp.getLine() + ".");
120    }
121
122    @Override
123    public void onNick(IRCUser u, String nickNew)
124    {
125        if (getShowStatusMessages())
126            onReceive(null, null, u.getNick() + " is now known as " + nickNew + ".");
127    }
128
129    @Override
130    public void onNotice(String target, IRCUser u, String msg)
131    {
132        onReceive(u.getNick(), target, "notice: " + msg);
133    }
134
135    @Override
136    public void onPart(String chan, IRCUser u, String msg)
137    {
138        // redirect
139        onLeave(chan, u, msg);
140    }
141
142    @Override
143    public void onPrivmsg(String chan, IRCUser u, String msg)
144    {
145        onReceive(u.getNick(), chan, msg);
146    }
147
148    @Override
149    public void onQuit(IRCUser u, String msg)
150    {
151        if (getShowStatusMessages())
152        {
153            if (StringUtil.isEmpty(msg))
154                onReceive(null, null, u.getNick() + " quits chat.");
155            else
156                onReceive(null, null, u.getNick() + " quits chat (" + msg + ").");
157        }
158    }
159
160    @Override
161    public void onReply(int num, String value, String msg)
162    {
163        onReceive(null, null, "Reply #" + num + ": " + value + " " + msg);
164    }
165
166    @Override
167    public void onTopic(String chan, IRCUser u, String topic)
168    {
169        onReceive(null, chan, u.getNick() + " changes topic into: " + topic + ".");
170    }
171
172    @Override
173    public void onPing(String p)
174    {
175        // do nothing
176    }
177
178    @Override
179    public void unknown(String a, String b, String c, String d)
180    {
181        onReceive(null, null, a + " " + b + ">" + c + " " + d);
182        // onReceive(d);
183    }
184
185    protected abstract boolean getShowStatusMessages();
186}