1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.opennap.net;
21
22 import java.awt.event.ActionEvent;
23 import java.io.IOException;
24
25 import javax.swing.Action;
26 import javax.swing.Icon;
27
28 import org.xnap.chat.ChatProvider;
29 import org.xnap.chat.action.AbstractBanAction;
30 import org.xnap.chat.action.AbstractDeOpAction;
31 import org.xnap.chat.action.AbstractJoinAction;
32 import org.xnap.chat.action.AbstractOpAction;
33 import org.xnap.chat.action.AbstractPartAction;
34 import org.xnap.chat.command.*;
35 import org.xnap.chat.command.AbstractPartChannelCommand;
36 import org.xnap.chat.command.JoinChannelCommand;
37 import org.xnap.cmdl.Command;
38 import org.xnap.cmdl.Console;
39 import org.xnap.peer.Peer;
40 import org.xnap.plugin.opennap.OpenNapPlugin;
41 import org.xnap.plugin.opennap.command.OpenNapBanListCommand;
42 import org.xnap.plugin.opennap.command.OpenNapMessageCommand;
43 import org.xnap.plugin.opennap.command.OpenNapRegisterUserCommand;
44 import org.xnap.plugin.opennap.net.msg.MessageHandler;
45 import org.xnap.plugin.opennap.net.msg.MessageListener;
46 import org.xnap.plugin.opennap.net.msg.client.ChannelBanMessage;
47 import org.xnap.plugin.opennap.net.msg.client.ChannelTopicMessage;
48 import org.xnap.plugin.opennap.net.msg.client.*;
49 import org.xnap.plugin.opennap.net.msg.client.PartChannelMessage;
50 import org.xnap.plugin.opennap.net.msg.client.PublicMessage;
51 import org.xnap.plugin.opennap.net.msg.client.RemoveChannelOpMessage;
52 import org.xnap.plugin.opennap.net.msg.client.SetChannelOpMessage;
53 import org.xnap.plugin.opennap.net.msg.server.ErrorMessage;
54 import org.xnap.plugin.opennap.net.msg.server.ServerMessage;
55
56 /***
57 * Represents a single channel on a server.
58 */
59 public class OpenNapChannel extends OpenNapAbstractChannel implements MessageListener
60 {
61
62
63
64
65
66 private boolean joined = false;
67 private int userCount;
68
69 private Action acJoin = new JoinAction();
70 private Action acPart = new PartAction();
71
72
73
74 public OpenNapChannel(OpenNapServer server, String name)
75 {
76 super(server, name);
77
78 acPart.setEnabled(false);
79
80 MessageHandler.subscribe(ErrorMessage.TYPE, this);
81 }
82
83
84
85 public void add(Peer peer)
86 {
87 super.add(peer);
88 setUserCount(super.getPeerCount());
89 }
90
91 public void changeTopic(String newValue) throws IOException
92 {
93 ChannelTopicMessage m = new ChannelTopicMessage(getName(), newValue);
94 m.setExceptionListener(this);
95 MessageHandler.send(server, m);
96 }
97
98 public void close()
99 {
100 if (joined) {
101 MessageHandler.unsubscribe(ErrorMessage.TYPE, this);
102
103 joined = false;
104 MessageHandler.send(server, new PartChannelMessage(getName()));
105 }
106 }
107
108 public Action[] getActions()
109 {
110 return new Action[] { acJoin, acPart, };
111 }
112
113 public Command[] getCommands()
114 {
115 return new Command[] {
116 new OpenNapMessageCommand(this),
117 new OpenNapRegisterUserCommand(this),
118 new OpenNapBanListCommand(this, getName()),
119 new BanCommand(),
120 new IgnoreCommand(),
121 new MeCommand(),
122 new JoinChannelCommand(server, getName()),
123 new PartChannelCommand(),
124 };
125 }
126
127 /***
128 * @see OpenNapPlugin.ICON_16.
129 */
130 public Icon getIcon()
131 {
132 return OpenNapPlugin.ICON_16;
133 }
134
135 public Action[] getPeerActions(Peer peer)
136 {
137 return new Action[] {
138 new OpAction(peer), new DeOpAction(peer),
139 new BanAction(peer),
140 };
141 }
142
143 public int getPeerCount()
144 {
145 return userCount;
146 }
147
148 public ChatProvider getProvider()
149 {
150 return server;
151 }
152
153 public boolean isJoined()
154 {
155 return joined;
156 }
157
158 public void join()
159 {
160 JoinChannelMessage m = new JoinChannelMessage(getName());
161 m.setExceptionListener(this);
162 MessageHandler.send(server, m);
163 }
164
165 public void messageReceived(ServerMessage msg)
166 {
167 if (msg.getServer() != server) {
168 return;
169 }
170
171 errorReceived(((ErrorMessage)msg).message);
172 }
173
174 public void sendMessage(String message)
175 {
176
177 for (int i = 0; i < message.length(); i += 174) {
178 int end = Math.min(i + 174, message.length());
179 PublicMessage m = new PublicMessage(getName(),
180 message.substring(i, end));
181 m.setExceptionListener(this);
182 MessageHandler.send(server, m);
183 }
184
185 }
186
187 public void setJoined(boolean joined, String reason)
188 {
189 if (this.joined != joined) {
190 this.joined = joined;
191 if (joined) {
192 joined();
193 }
194 else {
195 parted(reason);
196 }
197
198 acJoin.setEnabled(!joined);
199 acPart.setEnabled(joined);
200 }
201 }
202
203 public void setUserCount(int userCount)
204 {
205 this.userCount = userCount;
206 }
207
208 public boolean equals(Object o)
209 {
210 if (o instanceof OpenNapChannel) {
211 OpenNapChannel c = (OpenNapChannel)o;
212 return (getProvider() == c.getProvider())
213 && getName().equalsIgnoreCase(c.getName());
214 }
215 return false;
216 }
217
218
219
220 private class BanAction extends AbstractBanAction
221 {
222 private Peer peer;
223
224 public BanAction(Peer peer)
225 {
226 this.peer = peer;
227 }
228
229 public void actionPerformed(ActionEvent event)
230 {
231 ChannelBanMessage msg
232 = new ChannelBanMessage(OpenNapChannel.this.getName(),
233 peer.getName());
234 msg.setExceptionListener(OpenNapChannel.this);
235 MessageHandler.send(server, msg);
236 }
237 }
238
239 private class DeOpAction extends AbstractDeOpAction
240 {
241 private Peer peer;
242
243 public DeOpAction(Peer peer)
244 {
245 this.peer = peer;
246 }
247
248 public void actionPerformed(ActionEvent event)
249 {
250 RemoveChannelOpMessage msg
251 = new RemoveChannelOpMessage(OpenNapChannel.this.getName(),
252 peer.getName());
253 msg.setExceptionListener(OpenNapChannel.this);
254 MessageHandler.send(server, msg);
255 }
256 }
257
258 private class JoinAction extends AbstractJoinAction
259 {
260
261 public void actionPerformed(ActionEvent event)
262 {
263 join();
264 }
265
266 }
267
268 private class OpAction extends AbstractOpAction
269 {
270 private Peer peer;
271
272 public OpAction(Peer peer)
273 {
274 this.peer = peer;
275 }
276
277 public void actionPerformed(ActionEvent event)
278 {
279 SetChannelOpMessage msg
280 = new SetChannelOpMessage(OpenNapChannel.this.getName(),
281 peer.getName());
282 msg.setExceptionListener(OpenNapChannel.this);
283 MessageHandler.send(server, msg);
284 }
285 }
286
287 private class PartAction extends AbstractPartAction
288 {
289 public void actionPerformed(ActionEvent event)
290 {
291 PartChannelMessage msg
292 = new PartChannelMessage(OpenNapChannel.this.getName());
293 msg.setExceptionListener(OpenNapChannel.this);
294 MessageHandler.send(server, msg);
295 }
296 }
297
298
299
300 private class BanCommand extends AbstractBanCommand
301 {
302 public void send(Console console, String channelName, String nick,
303 String reason)
304 {
305 ChannelBanMessage msg
306 = new ChannelBanMessage(channelName, nick, reason);
307 msg.setExceptionListener(OpenNapChannel.this);
308 MessageHandler.send(server, msg);
309 }
310 }
311
312 private class IgnoreCommand extends AbstractIgnoreUserCommand
313 {
314 public void send(Console console, String nick)
315 {
316 AddUserToIgnoreListMessage msg
317 = new AddUserToIgnoreListMessage(nick);
318 msg.setExceptionListener(OpenNapChannel.this);
319 MessageHandler.send(server, msg);
320 }
321 }
322
323 private class MeCommand extends AbstractMeCommand
324 {
325 public void send(Console console, String action)
326 {
327 EmoteMessage msg
328 = new EmoteMessage(OpenNapChannel.this.getName(), action);
329 msg.setExceptionListener(OpenNapChannel.this);
330 MessageHandler.send(server, msg);
331 }
332 }
333
334 private class PartChannelCommand extends AbstractPartChannelCommand
335 {
336 public void part(String channel)
337 {
338 PartChannelMessage msg
339 = new PartChannelMessage((channel != null)
340 ? channel
341 : OpenNapChannel.this.getName());
342 msg.setExceptionListener(OpenNapChannel.this);
343 MessageHandler.send(server, msg);
344 }
345 }
346
347 }