001: package dalma.endpoints.irc;
002:
003: import f00f.net.irc.martyr.clientstate.Member;
004: import f00f.net.irc.martyr.commands.InviteCommand;
005: import f00f.net.irc.martyr.commands.JoinCommand;
006: import f00f.net.irc.martyr.commands.KickCommand;
007: import f00f.net.irc.martyr.commands.PartCommand;
008: import f00f.net.irc.martyr.commands.RawCommand;
009: import f00f.net.irc.martyr.commands.MessageCommand;
010: import f00f.net.irc.martyr.commands.TopicCommand;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Enumeration;
015: import java.util.List;
016: import java.io.Serializable;
017:
018: /**
019: * Represents a channel in IRC.
020: *
021: * @author Kohsuke Kawaguchi
022: */
023: public final class Channel extends Session {
024: private final String name;
025:
026: /**
027: * True if the endpoint is a member of this channel.
028: */
029: private boolean joined;
030:
031: public Channel(IRCEndPoint endpoint, String name) {
032: super (endpoint);
033: this .name = name;
034: }
035:
036: /**
037: * Gets the channel name, such as "#dalma"
038: *
039: * @return never null.
040: */
041: public String getName() {
042: return name;
043: }
044:
045: /**
046: * Join this channel.
047: *
048: * @throws IllegalStateException
049: * if we've already joined this channel.
050: */
051: public synchronized void join() {
052: makeSureNotJoined();
053: endpoint.connection.sendCommand(new JoinCommand(name));
054: joined = true;
055: }
056:
057: /**
058: * Join this channel.
059: *
060: * @throws IllegalStateException
061: * if we've already joined this channel.
062: */
063: public synchronized void join(String secret) {
064: makeSureNotJoined();
065: endpoint.connection.sendCommand(new JoinCommand(name, secret));
066: joined = true;
067: }
068:
069: /**
070: * Returns true if we
071: */
072: public synchronized boolean isJoined() {
073: return getChannelInfo() != null;
074: }
075:
076: private synchronized void makeSureNotJoined() {
077: if (joined)
078: throw new IllegalStateException("already joined");
079: }
080:
081: /**
082: * Invites the specified user to this channel.
083: */
084: public void invite(Buddy buddy) {
085: endpoint.connection.sendCommand(new InviteCommand(buddy.name,
086: name));
087: }
088:
089: /**
090: * Kicks out the specified user from this channel.
091: */
092: public void kick(Buddy buddy, String comment) {
093: endpoint.connection.sendCommand(new KickCommand(name,
094: buddy.name, comment));
095: }
096:
097: public void knock(String msg) {
098: makeSureNotJoined();
099: endpoint.connection.sendCommand(new RawCommand(name, msg));
100: }
101:
102: /**
103: * Gets all the people on this channel.
104: */
105: public Collection<Buddy> getMembers() {
106: List<Buddy> buddies = new ArrayList<Buddy>();
107: Enumeration members = getChannelInfo().getMembers();
108: while (members.hasMoreElements()) {
109: Member m = (Member) members.nextElement();
110: Buddy b = endpoint.getBuddy(m.getNick().getNick());
111: buddies.add(b);
112: }
113: return buddies;
114: }
115:
116: /**
117: * Gets the object that represents information about this channel
118: * from martyr.
119: *
120: * @return null
121: * if we aren't joined this channel yet.
122: */
123: private f00f.net.irc.martyr.clientstate.Channel getChannelInfo() {
124: return endpoint.connection.getClientState().getChannel(name);
125: }
126:
127: /**
128: * Gets the channel topic.
129: *
130: * @return
131: * never null.
132: */
133: public String getTopic() {
134: return getChannelInfo().getTopic();
135: }
136:
137: public void setTopic(String newTopic) {
138: endpoint.connection
139: .sendCommand(new TopicCommand(name, newTopic));
140: }
141:
142: public void setMode(Object... modes) {
143: // TODO: implement this method later
144: throw new UnsupportedOperationException();
145: }
146:
147: /**
148: * Sends a message to this {@link Channel}.
149: */
150: public void send(String message) {
151: endpoint.connection.sendCommand(new MessageCommand(name,
152: message));
153: }
154:
155: /**
156: * Sends the 'PART' message and leaves from this channel.
157: */
158: public synchronized void close() {
159: endpoint.connection.sendCommand(new PartCommand(name));
160: joined = false;
161: }
162:
163: protected Object writeReplace() {
164: return new Moniker(endpoint, name);
165: }
166:
167: private static final class Moniker implements Serializable {
168: private final IRCEndPoint endPoint;
169: private final String channelName;
170:
171: public Moniker(IRCEndPoint endPoint, String channelName) {
172: this .endPoint = endPoint;
173: this .channelName = channelName;
174: }
175:
176: private Object readResolve() {
177: return endPoint.getChannel(channelName);
178: }
179:
180: private static final long serialVersionUID = 1L;
181: }
182: }
|