1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\r
10 /* PJIRC is free software; you can redistribute */
\r
11 /* it and/or modify it under the terms of the GNU */
\r
12 /* General Public License as published by the */
\r
13 /* Free Software Foundation; version 2 or later of */
\r
16 /* PJIRC is distributed in the hope that it will */
\r
17 /* be useful, but WITHOUT ANY WARRANTY; without */
\r
18 /* even the implied warranty of MERCHANTABILITY or */
\r
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
\r
20 /* General Public License for more details. */
\r
22 /* You should have received a copy of the GNU */
\r
23 /* General Public License along with PJIRC; if */
\r
24 /* not, write to the Free Software Foundation, */
\r
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
\r
26 /* MA 02111-1307 USA */
\r
28 /*****************************************************/
\r
32 import java.util.Enumeration;
\r
33 import java.util.Hashtable;
\r
51 public Nick(String n, String m, char[][] modes, char[] prefix) {
\r
53 Mode = new ModeHandler(m, modes, prefix);
\r
62 * Whois information.
\r
64 public String Whois;
\r
68 public ModeHandler Mode;
\r
74 public class Channel extends IRCSource implements ReplyServerListener {
\r
75 private String _name;
\r
76 private String _topic;
\r
77 private ModeHandler _mode;
\r
78 private ListenerGroup _listeners;
\r
79 private Hashtable _nicks;
\r
82 * Create a new Channel.
\r
85 * the global configuration.
\r
89 * the source server.
\r
91 public Channel(IRCConfiguration config, String name, IRCServer s) {
\r
95 _mode = new ModeHandler(s.getChannelModes(), s.getNickModes());
\r
96 _listeners = new ListenerGroup();
\r
97 _nicks = new Hashtable();
\r
98 s.addReplyServerListener(this);
\r
99 if (_ircConfiguration.getASLMaster())
\r
100 getIRCServer().execute("WHO " + _name);
\r
101 setInterpretor(new ChannelInterpretor(config));
\r
105 public void release() {
\r
106 ((IRCServer) _server).removeReplyServerListener(this);
\r
111 * Add a channel listener.
\r
115 * @deprecated use addChannelListener2 instead.
\r
118 public void addChannelListener(ChannelListener lis) {
\r
119 _listeners.addListener(lis);
\r
123 * Remove a channel listener.
\r
126 * listener to remove.
\r
127 * @deprecated use removeChannelListener2 instead.
\r
130 public void removeChannelListener(ChannelListener lis) {
\r
131 _listeners.removeListener(lis);
\r
135 * Add a channel listener.
\r
140 public void addChannelListener2(ChannelListener2 lis) {
\r
141 _listeners.addListener(lis);
\r
145 * Remove a channel listener.
\r
148 * listener to remove.
\r
150 public void removeChannelListener2(ChannelListener2 lis) {
\r
151 _listeners.removeListener(lis);
\r
155 public String getType() {
\r
160 public String getName() {
\r
165 public boolean talkable() {
\r
170 public void leave() {
\r
171 getIRCServer().leaveChannel(getName());
\r
175 * Test wether this channel has the given nickname.
\r
178 * nickname to test.
\r
179 * @return true if nick is present on this channel, false otherwise.
\r
181 public boolean hasNick(String nick) {
\r
182 return _nicks.get(nick.toLowerCase(java.util.Locale.ENGLISH)) != null;
\r
186 * Notify this channel the given nick has joined, with the given options.
\r
193 public void joinNick(String nick, String mode) {
\r
194 _nicks.put(nick.toLowerCase(java.util.Locale.ENGLISH), new Nick(nick, mode, getIRCServer().getChannelModes(),
\r
195 getIRCServer().getNickModes()));
\r
196 if (_ircConfiguration.getASLMaster())
\r
197 getIRCServer().execute("WHO " + nick);
\r
198 _listeners.sendEvent("nickJoin", nick, mode, this);
\r
202 * Notify this channel it should reset its nick list.
\r
204 public void resetNicks() {
\r
206 _listeners.sendEvent("nickReset", this);
\r
210 * Notify this channel it should change its hole nick list.
\r
215 * new modes. There is a one to one mapping between nicks and modes.
\r
217 public void setNicks(String[] nicks, String[] modes) {
\r
218 for (int i = 0; i < nicks.length; i++)
\r
219 _nicks.put(nicks[i].toLowerCase(java.util.Locale.ENGLISH), new Nick(nicks[i], modes[i], getIRCServer()
\r
220 .getChannelModes(), getIRCServer().getNickModes()));
\r
221 _listeners.sendEvent("nickSet", nicks, modes, this);
\r
225 * Notify this channel the given nick has left the channel.
\r
232 public void partNick(String nick, String reason) {
\r
233 _nicks.remove(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
234 _listeners.sendEvent("nickPart", nick, reason, this);
\r
238 * Notify this channel the given nick has been kicked.
\r
243 * the nick who kicked nick.
\r
247 public void kickNick(String nick, String by, String reason) {
\r
248 _nicks.remove(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
249 _listeners.sendEvent("nickKick", new Object[] { nick, by, reason, this });
\r
253 * Notify this channel the given nick has quit.
\r
256 * the nick who quit.
\r
260 public void quitNick(String nick, String reason) {
\r
261 _nicks.remove(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
262 _listeners.sendEvent("nickQuit", nick, reason, this);
\r
266 * Get all the nicks in this channel.
\r
268 * @return array of all nick names.
\r
270 public String[] getNicks() {
\r
271 String[] ans = new String[_nicks.size()];
\r
272 Enumeration e = _nicks.elements();
\r
274 while (e.hasMoreElements())
\r
275 ans[i++] = ((Nick) e.nextElement()).Name;
\r
280 * Get the nick mode associated with the given nick.
\r
283 * nickname to get mode.
\r
284 * @return nick mode or null if nick not found.
\r
286 public String getNickMode(String nick) {
\r
287 Nick n = (Nick) _nicks.get(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
290 return n.Mode.getMode();
\r
294 * Notify this channel its topic has changed.
\r
299 * nickname who changed topic.
\r
301 public void setTopic(String topic, String by) {
\r
303 _listeners.sendEvent("topicChanged", topic, by, this);
\r
307 * Notify this channel a nick mode has changed.
\r
312 * the applied mode.
\r
314 * the nick who changed mode.
\r
316 public void applyUserMode(String nick, String mode, String from) {
\r
317 Nick n = (Nick) _nicks.get(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
319 n.Mode.apply(mode);
\r
320 _listeners.sendEvent("nickModeApply", new Object[] { nick, mode, from, this });
\r
324 * Notify this channel its mode has changed.
\r
329 * user who changed mode.
\r
331 public void applyMode(String mode, String from) {
\r
333 _listeners.sendEvent("modeApply", mode, from, this);
\r
337 * Get this channel mode.
\r
339 * @return channel mode.
\r
341 public String getMode() {
\r
342 return _mode.getMode();
\r
346 * Get this channel topic.
\r
348 * @return channel topic.
\r
350 public String getTopic() {
\r
355 * Notify this channel a nick has been renamed.
\r
362 public void changeNick(String oldNick, String newNick) {
\r
363 Nick n = (Nick) _nicks.get(oldNick.toLowerCase(java.util.Locale.ENGLISH));
\r
364 _nicks.remove(oldNick.toLowerCase(java.util.Locale.ENGLISH));
\r
366 _nicks.put(newNick.toLowerCase(java.util.Locale.ENGLISH), n);
\r
368 _listeners.sendEvent("nickChanged", oldNick, newNick, this);
\r
371 private void learn(String nick, String whois) {
\r
372 Nick n = (Nick) _nicks.get(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
376 _listeners.sendEvent("nickWhoisUpdated", nick, whois, this);
\r
380 * Get bufferised whois data for the given nick.
\r
384 * @return whois data for nick, or "" if not found.
\r
386 public String whois(String nick) {
\r
387 Nick n = (Nick) _nicks.get(nick.toLowerCase(java.util.Locale.ENGLISH));
\r
394 public Boolean replyReceived(String prefix, String id, String params[], IRCServer server) {
\r
395 if (id.equals("352")) {
\r
396 String name = params[params.length - 1];
\r
397 int pos = name.indexOf(" ");
\r
399 name = name.substring(pos + 1);
\r
400 String nick = params[5];
\r
403 return Boolean.FALSE;
\r