2009-12-17 15:27:57 -05:00
|
|
|
/*
|
|
|
|
Yaaic - Yet Another Android IRC Client
|
|
|
|
|
|
|
|
Copyright 2009 Sebastian Kaspari
|
|
|
|
|
|
|
|
This file is part of Yaaic.
|
|
|
|
|
|
|
|
Yaaic is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Yaaic 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Yaaic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.yaaic.irc;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2010-03-02 12:42:44 -05:00
|
|
|
import android.util.Log;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
import org.jibble.pircbot.PircBot;
|
2010-03-02 12:42:44 -05:00
|
|
|
import org.jibble.pircbot.User;
|
2009-12-17 15:27:57 -05:00
|
|
|
|
|
|
|
import org.yaaic.Yaaic;
|
|
|
|
import org.yaaic.model.Broadcast;
|
2010-03-02 13:40:05 -05:00
|
|
|
import org.yaaic.model.Channel;
|
2009-12-17 15:27:57 -05:00
|
|
|
import org.yaaic.model.Server;
|
|
|
|
import org.yaaic.model.Status;
|
|
|
|
|
|
|
|
public class IRCConnection extends PircBot
|
|
|
|
{
|
|
|
|
private IRCService service;
|
|
|
|
private Server server;
|
|
|
|
|
2010-03-02 12:42:44 -05:00
|
|
|
// XXX: Print all IRC events to the debug console
|
|
|
|
private static final boolean DEBUG_EVENTS = true;
|
|
|
|
public static final String TAG = "Yaaic/IRCConnection";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new connection
|
|
|
|
*
|
|
|
|
* @param service
|
|
|
|
* @param serverId
|
|
|
|
*/
|
2009-12-17 15:27:57 -05:00
|
|
|
public IRCConnection(IRCService service, int serverId)
|
|
|
|
{
|
|
|
|
this.server = Yaaic.getInstance().getServerById(serverId);
|
|
|
|
this.service = service;
|
|
|
|
|
|
|
|
this.setName("Yaaic");
|
|
|
|
this.setLogin("Yaaic");
|
|
|
|
this.setAutoNickChange(true);
|
|
|
|
this.setVersion("Yaaic - Yet another Android IRC client - http://www.yaaic.org");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On connect
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onConnect()
|
|
|
|
{
|
2010-03-02 13:40:05 -05:00
|
|
|
debug("Connect", "");
|
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
server.setStatus(Status.CONNECTED);
|
|
|
|
|
|
|
|
service.sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
|
|
|
|
}
|
|
|
|
|
2010-03-02 12:42:44 -05:00
|
|
|
/**
|
|
|
|
* On channel action
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onAction(String sender, String login, String hostname, String target, String action)
|
|
|
|
{
|
|
|
|
debug("Action", target + " " + sender + " " + action);
|
2010-03-02 13:40:05 -05:00
|
|
|
|
|
|
|
server.getChannel(target).addMessage("* " + sender + " " + action);
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Channel Info
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onChannelInfo(String channel, int userCount, String topic)
|
|
|
|
{
|
|
|
|
debug("ChannelInfo", channel + " " + userCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Deop
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 14:31:54 -05:00
|
|
|
protected void onDeop(String target, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 14:31:54 -05:00
|
|
|
debug("Deop", target + " " + recipient + "(" + sourceNick + ")");
|
|
|
|
|
|
|
|
server.getChannel(target).addMessage(sourceNick + " deoped " + recipient);
|
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:31:54 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On DeVoice
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 14:31:54 -05:00
|
|
|
protected void onDeVoice(String target, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 14:31:54 -05:00
|
|
|
debug("DeVoice", target + " " + recipient + "(" + sourceNick + ")");
|
|
|
|
|
|
|
|
server.getChannel(target).addMessage(sourceNick + " devoiced " + recipient);
|
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:31:54 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Invite
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 14:31:54 -05:00
|
|
|
protected void onInvite(String targetNick, String sourceNick, String sourceLogin, String sourceHostname, String target)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 14:31:54 -05:00
|
|
|
debug("Invite", target + " " + targetNick + "(" + sourceNick + ")");
|
|
|
|
|
|
|
|
server.getChannel(target).addMessage(sourceNick + " invited " + targetNick);
|
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:31:54 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Join
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:43:27 -05:00
|
|
|
protected void onJoin(String target, String sender, String login, String hostname)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:43:27 -05:00
|
|
|
debug("Join", target + " " + sender);
|
2010-03-02 13:40:05 -05:00
|
|
|
|
|
|
|
if (sender.equals(getNick())) {
|
|
|
|
// We joined a new channel
|
2010-03-02 13:43:27 -05:00
|
|
|
server.addChannel(new Channel(target));
|
2010-03-05 08:25:33 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_NEW);
|
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-05 08:25:33 -05:00
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 13:43:27 -05:00
|
|
|
} else {
|
|
|
|
server.getChannel(target).addMessage(sender + " joined");
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 13:40:05 -05:00
|
|
|
}
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Kick
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:47:43 -05:00
|
|
|
protected void onKick(String target, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:47:43 -05:00
|
|
|
debug("Kick", target + " " + recipientNick + "(" + kickerNick + ")");
|
|
|
|
|
|
|
|
if (recipientNick.equals(getNick())) {
|
|
|
|
// We are kicked
|
|
|
|
server.removeChannel(target);
|
2010-03-05 08:25:33 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_REMOVE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-05 08:25:33 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 13:47:43 -05:00
|
|
|
} else {
|
|
|
|
server.getChannel(target).addMessage(kickerNick + " kicked " + recipientNick);
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 13:47:43 -05:00
|
|
|
}
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Message
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:47:43 -05:00
|
|
|
protected void onMessage(String target, String sender, String login, String hostname, String message)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:47:43 -05:00
|
|
|
debug("Message", target + " " + sender + " " + message);
|
2010-03-02 13:48:12 -05:00
|
|
|
|
2010-03-02 14:34:05 -05:00
|
|
|
server.getChannel(target).addMessage("<" + sender + "> " + message);
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Mode
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:48:59 -05:00
|
|
|
protected void onMode(String target, String sourceNick, String sourceLogin, String sourceHostname, String mode)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:48:59 -05:00
|
|
|
debug("Mode", target + " " + sourceNick + " " + mode);
|
|
|
|
|
|
|
|
server.getChannel(target).addMessage(sourceNick + " sets mode " + mode);
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Nick Change
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onNickChange(String oldNick, String login, String hostname, String newNick)
|
|
|
|
{
|
|
|
|
debug("Nick", oldNick + " " + newNick);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Notice
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice)
|
|
|
|
{
|
|
|
|
debug("Notice", sourceNick + " " + notice);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Op
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:52:49 -05:00
|
|
|
protected void onOp(String target, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:52:49 -05:00
|
|
|
debug("Op", target + " " + recipient + "(" + sourceNick + ")");
|
|
|
|
|
|
|
|
server.getChannel(target).addMessage(sourceNick + " oped " + recipient);
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Part
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:54:34 -05:00
|
|
|
protected void onPart(String target, String sender, String login, String hostname)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:54:34 -05:00
|
|
|
debug("Part", target + " " + sender);
|
|
|
|
|
|
|
|
if (sender.equals(getNick())) {
|
|
|
|
// We pareted a channel
|
|
|
|
server.removeChannel(target);
|
2010-03-05 08:25:33 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_REMOVE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-05 08:25:33 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 13:54:34 -05:00
|
|
|
} else {
|
|
|
|
server.getChannel(target).addMessage(sender + " parted");
|
2010-03-02 14:28:32 -05:00
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 13:54:34 -05:00
|
|
|
}
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Private Message
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onPrivateMessage(String sender, String login, String hostname, String message)
|
|
|
|
{
|
|
|
|
debug("PrivateMessage", sender + " " + message);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Quit
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onQuit(String sourceNick, String sourceLogin, String sourceHostname, String reason)
|
|
|
|
{
|
|
|
|
debug("Quit", sourceNick);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Topic
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 13:59:37 -05:00
|
|
|
protected void onTopic(String target, String topic, String setBy, long date, boolean changed)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 13:59:37 -05:00
|
|
|
debug("Topic", target + " " + setBy + " " + topic);
|
|
|
|
|
|
|
|
if (changed) {
|
|
|
|
server.getChannel(target).addMessage(setBy + " sets topic: " + topic);
|
|
|
|
} else {
|
|
|
|
server.getChannel(target).addMessage("Topic: " + topic);
|
|
|
|
}
|
|
|
|
|
2010-03-02 14:28:32 -05:00
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:28:32 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On User List
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void onUserList(String channel, User[] users)
|
|
|
|
{
|
|
|
|
debug("UserList", channel + " (" + users.length + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* On Voice
|
|
|
|
*/
|
|
|
|
@Override
|
2010-03-02 14:31:54 -05:00
|
|
|
protected void onVoice(String target, String sourceNick, String sourceLogin, String sourceHostname, String recipient)
|
2010-03-02 12:42:44 -05:00
|
|
|
{
|
2010-03-02 14:31:54 -05:00
|
|
|
debug("Voice", target + " " + recipient + "(" + sourceNick + ")");
|
|
|
|
|
|
|
|
server.getChannel(target).addMessage(sourceNick + " voiced " + recipient);
|
|
|
|
|
|
|
|
Intent intent = new Intent(Broadcast.CHANNEL_MESSAGE);
|
2010-03-05 13:47:19 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_SERVER, server.getId());
|
2010-03-02 14:31:54 -05:00
|
|
|
intent.putExtra(Broadcast.EXTRA_CHANNEL, target);
|
|
|
|
service.sendBroadcast(intent);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
/**
|
|
|
|
* On disconnect
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onDisconnect()
|
|
|
|
{
|
|
|
|
server.setStatus(Status.DISCONNECTED);
|
|
|
|
service.sendBroadcast(new Intent(Broadcast.SERVER_UPDATE));
|
|
|
|
}
|
|
|
|
|
2010-03-02 12:42:44 -05:00
|
|
|
/**
|
|
|
|
* Print an event to the debug console
|
|
|
|
*/
|
|
|
|
private void debug(String event, String params)
|
|
|
|
{
|
|
|
|
if (DEBUG_EVENTS) {
|
2010-03-02 13:40:05 -05:00
|
|
|
Log.d(TAG, "(" + server.getTitle() + ") [" + event + "]: " + params);
|
2010-03-02 12:42:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-17 15:27:57 -05:00
|
|
|
/**
|
|
|
|
* Quits from the IRC server with default reason.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void quitServer()
|
|
|
|
{
|
|
|
|
quitServer("Yaaic - Yet another Android IRC client - http://www.yaaic.org");
|
|
|
|
}
|
|
|
|
}
|