diff --git a/src/com/fusionx/lightirc/activity/MainServerListActivity.java b/src/com/fusionx/lightirc/activity/MainServerListActivity.java index f54f2c8d..80cf378c 100644 --- a/src/com/fusionx/lightirc/activity/MainServerListActivity.java +++ b/src/com/fusionx/lightirc/activity/MainServerListActivity.java @@ -24,9 +24,9 @@ import com.fusionx.lightirc.R; import com.fusionx.lightirc.activity.ServerSettingsActivity.BaseServerSettingFragment; import com.fusionx.lightirc.adapters.LightPircBotXArrayAdapter; -import com.fusionx.lightirc.misc.IRCBinder; import com.fusionx.lightirc.misc.LightPircBotX; import com.fusionx.lightirc.services.IRCService; +import com.fusionx.lightirc.services.IRCService.IRCBinder; import android.os.Bundle; import android.os.IBinder; @@ -56,41 +56,46 @@ protected void onCreate(final Bundle savedInstanceState) { getListView().setLongClickable(true); registerForContextMenu(getListView()); - - setListAdapter(new LightPircBotXArrayAdapter(this, getSetServerList())); + + getSetServerList(); } - private LightPircBotX[] getSetServerList() { + private void getSetServerList() { SharedPreferences settings = getSharedPreferences("main", 0); boolean firstRun = settings.getBoolean("firstrun", true); int noOfServers = settings.getInt("noOfServers", 0); - LightPircBotX[] values; + LightPircBotX[] values = null; Editor e = settings.edit(); - if (firstRun || noOfServers == 0) { + if (firstRun) { LightPircBotX freenode = new LightPircBotX(); freenode.mURL = "irc.freenode.net"; freenode.mNick = "LightIRCUser"; freenode.setTitle("Freenode"); freenode.mAutoJoinChannels = new String[] { "#testingircandroid" }; values = new LightPircBotX[] { freenode }; - noOfServers = 1; + for (String s : freenode.toHashMap().keySet()) { e.putString("server_0_" + s, freenode.toHashMap().get(s)); } - e.putString("server_0_autoJoin_channel", "#testingircandroid"); + + e.putBoolean("firstrun", false); + e.putString("server_0_autoJoin_channel_0", "#testingircandroid"); e.putInt("server_0_autoJoin_no", 1); - } else { + e.putInt("noOfServers", 1); + e.commit(); + } else if (noOfServers != 0) { values = new LightPircBotX[noOfServers]; for (int i = 0; i < noOfServers; i++) { - values[i].mURL = settings.getString("server_" + i + "_url", ""); - values[i].mUserName = settings.getString("server_" + i + LightPircBotX bot = new LightPircBotX(); + bot.mURL = settings.getString("server_" + i + "_url", ""); + bot.mUserName = settings.getString("server_" + i + "_userName", ""); - values[i].mNick = settings.getString("server_" + i + "_nick", + bot.mNick = settings.getString("server_" + i + "_nick", ""); - values[i].mServerPassword = settings.getString("server_" + i + bot.mServerPassword = settings.getString("server_" + i + "_serverPassword", ""); - values[i].setTitle(settings.getString( + bot.setTitle(settings.getString( "server_" + i + "_title", "")); String[] s = new String[settings.getInt("server_" + i @@ -99,21 +104,20 @@ private LightPircBotX[] getSetServerList() { s[j] = settings.getString("server_" + i + "_autoJoin_channel_" + j, ""); } - values[i].mAutoJoinChannels = s; + bot.mAutoJoinChannels = s; + values[i] = bot; } } - - e.putBoolean("firstrun", true); - e.putInt("noOfServers", noOfServers); - e.commit(); - - serverList = values; - Intent service = new Intent(this, IRCService.class); - startService(service); - bindService(service, mConnection, 0); - - return values; + if(values != null) { + serverList = values; + + Intent service = new Intent(this, IRCService.class); + startService(service); + bindService(service, mConnection, 0); + + setListAdapter(new LightPircBotXArrayAdapter(this, values)); + } } private ServiceConnection mConnection = new ServiceConnection() { diff --git a/src/com/fusionx/lightirc/adapters/LightPircBotXArrayAdapter.java b/src/com/fusionx/lightirc/adapters/LightPircBotXArrayAdapter.java index 30e6cda1..ab176578 100644 --- a/src/com/fusionx/lightirc/adapters/LightPircBotXArrayAdapter.java +++ b/src/com/fusionx/lightirc/adapters/LightPircBotXArrayAdapter.java @@ -48,7 +48,7 @@ public View getView(int position, View convertView, ViewGroup parent) { View rowView = inflater.inflate(R.layout.listlayout, parent, false); TextView textView = (TextView) rowView.findViewById(R.id.text); - textView.setText(values[position].mURL); + textView.setText(values[position].getTitle()); return rowView; } diff --git a/src/com/fusionx/lightirc/callbacks/ChannelCallbacks.java b/src/com/fusionx/lightirc/callbacks/ChannelCallbacks.java new file mode 100644 index 00000000..0be91f0d --- /dev/null +++ b/src/com/fusionx/lightirc/callbacks/ChannelCallbacks.java @@ -0,0 +1,5 @@ +package com.fusionx.lightirc.callbacks; + +public interface ChannelCallbacks { + public void onChannelWriteNeeded(String message); +} \ No newline at end of file diff --git a/src/com/fusionx/lightirc/callbacks/ServerCallbacks.java b/src/com/fusionx/lightirc/callbacks/ServerCallbacks.java new file mode 100644 index 00000000..3a47f447 --- /dev/null +++ b/src/com/fusionx/lightirc/callbacks/ServerCallbacks.java @@ -0,0 +1,6 @@ +package com.fusionx.lightirc.callbacks; + +public interface ServerCallbacks { + public void onServerWriteNeeded(String message); + public void onNewChannelJoined(String channel, String nick, String buffer); +} \ No newline at end of file diff --git a/src/com/fusionx/lightirc/fragments/ChannelFragment.java b/src/com/fusionx/lightirc/fragments/ChannelFragment.java index 669cdf72..e1049887 100644 --- a/src/com/fusionx/lightirc/fragments/ChannelFragment.java +++ b/src/com/fusionx/lightirc/fragments/ChannelFragment.java @@ -21,11 +21,11 @@ package com.fusionx.lightirc.fragments; -import android.content.BroadcastReceiver; -import android.content.Context; +import android.content.ComponentName; import android.content.Intent; -import android.content.IntentFilter; +import android.content.ServiceConnection; import android.os.Bundle; +import android.os.IBinder; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; @@ -35,8 +35,12 @@ import android.widget.TextView; import com.fusionx.lightirc.R; +import com.fusionx.lightirc.callbacks.ChannelCallbacks; +import com.fusionx.lightirc.services.IRCService; +import com.fusionx.lightirc.services.IRCService.IRCBinder; -public class ChannelFragment extends IRCFragment implements OnKeyListener { +public class ChannelFragment extends IRCFragment implements OnKeyListener, + ChannelCallbacks { String nick; String serverName; @@ -50,23 +54,31 @@ public View onCreateView(final LayoutInflater inflater, nick = getArguments().getString("nick"); serverName = getArguments().getString("serverName"); String buffer = getArguments().getString("buffer"); - - if (buffer != null) { - writeRawToTextView(buffer, rootView); - } else { - writeToTextView(nick + " entered the room", rootView); - } + + writeToTextView(buffer, rootView); TextView textview = (TextView) rootView.findViewById(R.id.editText1); textview.setOnKeyListener(this); - IntentFilter filter = new IntentFilter( - "com.fusionx.lightirc.MESSAGE_FROM_CHANNEL"); - getActivity().registerReceiver(mChannelReciever, filter); + Intent service = new Intent(getActivity(), IRCService.class); + service.putExtra("channel", tabTitle); + getActivity().bindService(service, mConnection, 0); return rootView; } + private ServiceConnection mConnection = new ServiceConnection() { + @Override + public void onServiceConnected(ComponentName className, IBinder service) { + ((IRCBinder) service).getService().setChannelCallbacks( + ChannelFragment.this, tabTitle); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + } + }; + @Override public boolean onKey(View v, int keyCode, KeyEvent event) { EditText t = (EditText) v; @@ -83,8 +95,6 @@ public boolean onKey(View v, int keyCode, KeyEvent event) { intent.putExtra("message", t.getText().toString()); getActivity().sendBroadcast(intent); - writeToTextView(nick + ": " + t.getText().toString()); - // Hacky way to clear but keep the focus on the EditText t.getText().clear(); t.setSelection(0); @@ -97,21 +107,12 @@ public boolean onKey(View v, int keyCode, KeyEvent event) { @Override public void onDestroy() { - getActivity().unregisterReceiver(mChannelReciever); + getActivity().unbindService(mConnection); super.onDestroy(); } - BroadcastReceiver mChannelReciever = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - String channel = intent.getStringExtra("channel"); - if (channel.equals(tabTitle)) { - if (intent.getAction().equals( - "com.fusionx.lightirc.MESSAGE_FROM_CHANNEL")) { - String message = intent.getStringExtra("message"); - writeToTextView(message); - } - } - } - }; + @Override + public void onChannelWriteNeeded(String message) { + writeToTextView(message); + } } diff --git a/src/com/fusionx/lightirc/fragments/IRCFragment.java b/src/com/fusionx/lightirc/fragments/IRCFragment.java index 82d6c824..36edce78 100644 --- a/src/com/fusionx/lightirc/fragments/IRCFragment.java +++ b/src/com/fusionx/lightirc/fragments/IRCFragment.java @@ -14,11 +14,11 @@ public String getTitle() { return tabTitle; } - protected void writeRawToTextView(String text) { - writeRawToTextView(text, getView()); + protected void writeToTextView(String text) { + writeToTextView(text, getView()); } - protected void writeRawToTextView(String text, View rootView) { + protected void writeToTextView(String text, View rootView) { final TextView textView = (TextView) rootView .findViewById(R.id.textview); final ScrollView scrollView = (ScrollView) rootView @@ -26,12 +26,4 @@ protected void writeRawToTextView(String text, View rootView) { textView.append(text); scrollView.fullScroll(View.FOCUS_DOWN); } - - protected void writeToTextView(String text) { - writeRawToTextView(text + "\n"); - } - - protected void writeToTextView(String text, View rootView) { - writeRawToTextView(text + "\n", rootView); - } } \ No newline at end of file diff --git a/src/com/fusionx/lightirc/fragments/ServerFragment.java b/src/com/fusionx/lightirc/fragments/ServerFragment.java index c0c1bb53..808b09b9 100644 --- a/src/com/fusionx/lightirc/fragments/ServerFragment.java +++ b/src/com/fusionx/lightirc/fragments/ServerFragment.java @@ -22,16 +22,14 @@ package com.fusionx.lightirc.fragments; import com.fusionx.lightirc.R; -import com.fusionx.lightirc.misc.IRCBinder; import com.fusionx.lightirc.misc.LightPircBotX; import com.fusionx.lightirc.services.IRCService; +import com.fusionx.lightirc.services.IRCService.IRCBinder; import com.fusionx.lightirc.activity.ServerChannelActivity; +import com.fusionx.lightirc.callbacks.ServerCallbacks; -import android.content.BroadcastReceiver; import android.content.ComponentName; -import android.content.Context; import android.content.Intent; -import android.content.IntentFilter; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; @@ -39,7 +37,7 @@ import android.view.View; import android.view.ViewGroup; -public class ServerFragment extends IRCFragment { +public class ServerFragment extends IRCFragment implements ServerCallbacks { String serverName; @Override @@ -51,6 +49,7 @@ public void onCreate(final Bundle savedInstanceState) { ((ServerChannelActivity) getActivity()).updateTabTitle(this, serverName); Intent service = new Intent(getActivity(), IRCService.class); + service.putExtra("server", true); getActivity().startService(service); getActivity().bindService(service, mConnection, 0); @@ -61,8 +60,9 @@ public void onCreate(final Bundle savedInstanceState) { @Override public void onServiceConnected(ComponentName className, IBinder service) { LightPircBotX light = ((IRCBinder) service).getService().getBot(serverName); + ((IRCBinder) service).getService().setServerCallbacks(ServerFragment.this); if(light.mIsStarted) { - writeRawToTextView(light.mServerBuffer); + writeToTextView(light.mServerBuffer); for(String s : light.mChannelBuffers.keySet()) { ChannelFragment channel = new ChannelFragment(); Bundle bu = new Bundle(); @@ -79,7 +79,6 @@ public void onServiceConnected(ComponentName className, IBinder service) { } else { ((IRCBinder) service).getService().connectToServer(serverName); } - getActivity().unbindService(mConnection); } @Override @@ -89,7 +88,7 @@ public void onServiceDisconnected(ComponentName name) { @Override public void onDestroy() { - getActivity().unregisterReceiver(mServerReciever); + getActivity().unbindService(mConnection); super.onDestroy(); } @@ -99,39 +98,26 @@ public View onCreateView(final LayoutInflater inflater, final View rootView = inflater.inflate(R.layout.fragment_irc_channel, container, false); - IntentFilter filter = new IntentFilter( - "com.fusionx.lightirc.JOIN_NEW_CHANNEL"); - getActivity().registerReceiver(mServerReciever, filter); - filter = new IntentFilter("com.fusionx.lightirc.NOTICE_FROM_SERVER"); - getActivity().registerReceiver(mServerReciever, filter); - filter = new IntentFilter("com.fusionx.lightirc.MOTD_FROM_SERVER"); - getActivity().registerReceiver(mServerReciever, filter); - return rootView; } - BroadcastReceiver mServerReciever = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals( - "com.fusionx.lightirc.JOIN_NEW_CHANNEL")) { - String channelName = intent.getStringExtra("channel"); - String nick = intent.getStringExtra("nick"); - - ChannelFragment channel = new ChannelFragment(); - Bundle b = new Bundle(); - b.putString("channel", channelName); - b.putString("nick", nick); - b.putString("serverName", serverName); - channel.setArguments(b); - - int position = ((ServerChannelActivity) getActivity()).mSectionsPagerAdapter - .addView(channel); - ((ServerChannelActivity) getActivity()).addTab(position); - } else { - String stringToWrite = intent.getStringExtra("stringToWrite"); - writeToTextView(stringToWrite); - } - } - }; -} + @Override + public void onServerWriteNeeded(String message) { + writeToTextView(message); + } + + @Override + public void onNewChannelJoined(String channelName, String nick, String buffer) { + ChannelFragment channel = new ChannelFragment(); + Bundle b = new Bundle(); + b.putString("channel", channelName); + b.putString("nick", nick); + b.putString("serverName", serverName); + b.putString("buffer", buffer); + channel.setArguments(b); + + int position = ((ServerChannelActivity) getActivity()).mSectionsPagerAdapter + .addView(channel); + ((ServerChannelActivity) getActivity()).addTab(position); + } +} \ No newline at end of file diff --git a/src/com/fusionx/lightirc/misc/IRCBinder.java b/src/com/fusionx/lightirc/misc/IRCBinder.java deleted file mode 100644 index 845b3a83..00000000 --- a/src/com/fusionx/lightirc/misc/IRCBinder.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.fusionx.lightirc.misc; - -import android.os.Binder; - -import com.fusionx.lightirc.services.IRCService; - -public class IRCBinder extends Binder { - private final IRCService service; - - public IRCBinder(IRCService service) { - super(); - this.service = service; - } - - public IRCService getService() { - // Return this instance of IRCService so clients can call public methods - return service; - } -} diff --git a/src/com/fusionx/lightirc/services/IRCService.java b/src/com/fusionx/lightirc/services/IRCService.java index 0bb2eb2a..c12d2582 100644 --- a/src/com/fusionx/lightirc/services/IRCService.java +++ b/src/com/fusionx/lightirc/services/IRCService.java @@ -2,12 +2,16 @@ import java.util.HashMap; +import org.pircbotx.Channel; import org.pircbotx.hooks.Listener; import org.pircbotx.hooks.ListenerAdapter; import org.pircbotx.hooks.events.JoinEvent; import org.pircbotx.hooks.events.MessageEvent; import org.pircbotx.hooks.events.MotdEvent; +import org.pircbotx.hooks.events.NickChangeEvent; import org.pircbotx.hooks.events.NoticeEvent; +import org.pircbotx.hooks.events.PartEvent; +import org.pircbotx.hooks.events.QuitEvent; import android.app.Service; import android.content.BroadcastReceiver; @@ -15,34 +19,39 @@ import android.content.Intent; import android.content.IntentFilter; import android.os.AsyncTask; +import android.os.Binder; +import android.os.Handler; import android.os.IBinder; -import com.fusionx.lightirc.misc.IRCBinder; +import com.fusionx.lightirc.callbacks.ChannelCallbacks; +import com.fusionx.lightirc.callbacks.ServerCallbacks; import com.fusionx.lightirc.misc.LightPircBotX; public class IRCService extends Service { - private final IRCBinder mBinder; + private final IRCBinder mBinder = new IRCBinder(); public final HashMap mServerObjects = new HashMap(); - public IRCService() { - mBinder = new IRCBinder(this); + private Handler mHandler = new Handler(); + public HashMap mChannelCallbacks = new HashMap(); + private ServerCallbacks mServerCallbacks = null; + + public class IRCBinder extends Binder { + public IRCService getService() { + return IRCService.this; + } } @Override public IBinder onBind(Intent arg0) { - // mHandler = new Handler(); return mBinder; } - @Override - public void onRebind(Intent arg0) { - // mHandler = new Handler(); - } - @Override public boolean onUnbind(Intent arg0) { - // unregisterReceiverCallbacks(); - // mHandler = null; + if (arg0.getBooleanExtra("server", false)) { + mServerCallbacks = null; + } + mChannelCallbacks.remove(arg0.getStringExtra("channelName")); return true; } @@ -55,18 +64,24 @@ public void onCreate() { @Override public void onDestroy() { + unregisterReceiver(mScreenStateReceiver); } - + public LightPircBotX getBot(String serverName) { return mServerObjects.get(serverName); } - + + private void appendToServerBuffer(String message, String serverName) { + mServerObjects.get(serverName).mServerBuffer += message + "\n"; + } + AsyncTask mBackgroundConnector = new AsyncTask() { @Override protected Void doInBackground(LightPircBotX... serverDetails) { try { serverDetails[0].connect(serverDetails[0].mURL); - serverDetails[0].joinChannel(serverDetails[0].mAutoJoinChannels[0]); + serverDetails[0] + .joinChannel(serverDetails[0].mAutoJoinChannels[0]); } catch (Exception ex) { ex.printStackTrace(); } @@ -76,7 +91,7 @@ protected Void doInBackground(LightPircBotX... serverDetails) { public void connectToServer(String serverName) { LightPircBotX mServerObject = getBot(serverName); - if(!mServerObject.mIsStarted) { + if (!mServerObject.mIsStarted) { // TODO - setup option for this mServerObject.setAutoNickChange(true); @@ -93,70 +108,186 @@ public void connectToServer(String serverName) { } } - BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { + private void callbackToChannelAndAppend(final String channelName, + final String message, final String serverName) { + if (mChannelCallbacks.containsKey(channelName)) { + mHandler.post(new Runnable() { + public void run() { + mChannelCallbacks.get(channelName) + .onChannelWriteNeeded(message); + } + }); + } + + final HashMap buffers = mServerObjects.get(serverName).mChannelBuffers; + final String buffer = buffers.get(channelName) + message; + buffers.put(channelName, buffer); + } + + final BroadcastReceiver mScreenStateReceiver = new BroadcastReceiver() { @Override - public void onReceive(Context context, Intent intent) { + public void onReceive(final Context context, final Intent intent) { if (intent.getAction().equals( "com.fusionx.lightirc.MESSAGE_TO_CHANNEL")) { String channelName = intent.getStringExtra("channel"); String message = intent.getStringExtra("message"); String serverName = intent.getStringExtra("serverName"); - mServerObjects.get(serverName).sendMessage(mServerObjects.get(serverName).getChannel(channelName), message); + LightPircBotX bot = mServerObjects.get(serverName); + + bot.sendMessage(channelName, message); + + String bufferMessage = bot.getNick() + ": " + message + "\n"; + + callbackToChannelAndAppend(channelName, bufferMessage, serverName); } } }; - public class ServerListener extends ListenerAdapter implements - Listener { + public void setChannelCallbacks(final ChannelCallbacks cb, final String channelName) { + mChannelCallbacks.put(channelName, cb); + } + + public void setServerCallbacks(final ServerCallbacks cb) { + mServerCallbacks = cb; + } + + private void tryPostServer(final Runnable run) { + if (mServerCallbacks != null) { + mHandler.post(run); + } + } + + public class ServerListener extends ListenerAdapter + implements Listener { @Override - public void onJoin(final JoinEvent event) throws Exception { + public void onJoin(final JoinEvent event) + throws Exception { if (event.getUser().getNick().equals(event.getBot().getNick())) { - Intent intent = new Intent(); - intent.setAction("com.fusionx.lightirc.JOIN_NEW_CHANNEL"); - intent.putExtra("channel", event.getChannel().getName()); - intent.putExtra("nick", event.getUser().getNick()); - sendBroadcast(intent); - - String welcomeMessage = event.getUser().getNick() + " entered the room\n"; - - mServerObjects.get(event.getBot().getTitle()).mChannelBuffers.put(event.getChannel().getName(), welcomeMessage); + String buffer = event.getUser().getNick() + + " entered the room\n"; + if (!event.getChannel().getTopic().isEmpty()) { + buffer += "The topic is: " + event.getChannel().getTopic() + + " as set forth by " + + event.getChannel().getTopicSetter() + "\n"; + } else { + buffer += "There is no topic for this channel :(\n"; + } + + final String finalBuffer = buffer; + + mServerObjects.get(event.getBot().getTitle()).mChannelBuffers + .put(event.getChannel().getName(), buffer); + + tryPostServer(new Runnable() { + public void run() { + mServerCallbacks.onNewChannelJoined(event.getChannel() + .getName(), event.getUser().getNick(), + finalBuffer); + } + }); } } @Override public void onNotice(final NoticeEvent event) throws Exception { - Intent intent = new Intent(); - intent.setAction("com.fusionx.lightirc.NOTICE_FROM_SERVER"); - intent.putExtra("stringToWrite", event.getMessage()); - sendBroadcast(intent); - mServerObjects.get(event.getBot().getTitle()).mServerBuffer += event.getMessage() + "\n"; + tryPostServer(new Runnable() { + public void run() { + mServerCallbacks.onServerWriteNeeded(event.getMessage() + "\n"); + } + }); + appendToServerBuffer(event.getMessage() + "\n", event.getBot().getTitle()); + } @Override - public void onMotd(final MotdEvent event) throws Exception { - Intent intent = new Intent(); - intent.setAction("com.fusionx.lightirc.MOTD_FROM_SERVER"); - intent.putExtra("stringToWrite", event.getMotd()); - sendBroadcast(intent); - mServerObjects.get(event.getBot().getTitle()).mServerBuffer += event.getMotd() + "\n"; + public void onMotd(final MotdEvent event) + throws Exception { + tryPostServer(new Runnable() { + public void run() { + mServerCallbacks.onServerWriteNeeded(event.getMotd() + "\n"); + } + }); + appendToServerBuffer(event.getMotd() + "\n", event.getBot().getTitle()); } } - public class ChannelListener extends ListenerAdapter implements - Listener { + public class ChannelListener extends ListenerAdapter + implements Listener { + @Override + public void onMessage(final MessageEvent event) + throws Exception { + final String newMessage = event.getUser().getNick() + ": " + + event.getMessage() + "\n"; + + callbackToChannelAndAppend(event.getChannel().getName(), + newMessage, event.getBot().getTitle()); + } + @Override - public void onMessage(MessageEvent event) throws Exception { - String messageToWrite = event.getUser().getNick() + ": " - + event.getMessage(); - Intent intent = new Intent(); - intent.setAction("com.fusionx.lightirc.MESSAGE_FROM_CHANNEL"); - intent.putExtra("channel", event.getChannel().getName()); - intent.putExtra("message", messageToWrite); - sendBroadcast(intent); - String buffer = mServerObjects.get(event.getBot().getTitle()).mChannelBuffers.get(event.getChannel().getName()); - buffer += messageToWrite + "\n"; - mServerObjects.get(event.getBot().getTitle()).mChannelBuffers.put(event.getChannel().getName(), buffer); + public void onQuit(final QuitEvent event) + throws Exception { + if (!event.getUser().getNick().equals(event.getBot().getNick())) { + for (final Channel c : event.getBot().getChannels()) { + if (c.getUsers().contains(event.getUser())) { + final String newMessage = event.getUser().getNick() + + " quit the room\n"; + + callbackToChannelAndAppend(c.getName(), newMessage, + event.getBot().getTitle()); + } + } + + } + } + + @Override + public void onNickChange(final NickChangeEvent event) + throws Exception { + if (!event.getOldNick().equals(event.getBot().getNick())) { + for (final Channel c : event.getBot().getChannels()) { + if (c.getUsers().contains(event.getUser())) { + final String newMessage = event.getOldNick() + + " is now known as " + event.getNewNick() + + "\n"; + + callbackToChannelAndAppend(c.getName(), newMessage, + event.getBot().getTitle()); + } + } + } else { + for (final Channel c : event.getBot().getChannels()) { + final String newMessage = "You (" + event.getOldNick() + + ") are now known as " + event.getNewNick() + "\n"; + + callbackToChannelAndAppend(c.getName(), newMessage, event + .getBot().getTitle()); + } + } + } + + @Override + public void onPart(final PartEvent event) + throws Exception { + if (!event.getUser().getNick().equals(event.getBot().getNick())) { + final String newMessage = event.getUser().getNick() + + " parted from the room\n"; + + callbackToChannelAndAppend(event.getChannel().getName(), + newMessage, event.getBot().getTitle()); + } + } + + @Override + public void onJoin(final JoinEvent event) + throws Exception { + if (!event.getUser().getNick().equals(event.getBot().getNick())) { + final String newMessage = event.getUser().getNick() + + " entered the room\n"; + + callbackToChannelAndAppend(event.getChannel().getName(), + newMessage, event.getBot().getTitle()); + } } } -} +} \ No newline at end of file