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.command;
21
22 import java.util.StringTokenizer;
23
24 import org.xnap.XNap;
25 import org.xnap.cmdl.AbstractCommand;
26 import org.xnap.cmdl.Console;
27 import org.xnap.cmdl.SyntaxException;
28 import org.xnap.plugin.opennap.net.msg.MessageHandler;
29 import org.xnap.plugin.opennap.net.msg.MessageListener;
30 import org.xnap.plugin.opennap.net.msg.client.ChannelBanListRequestMessage;
31 import org.xnap.plugin.opennap.net.msg.server.ChannelBanListEntryMessage;
32 import org.xnap.plugin.opennap.net.msg.server.EndChannelBanListMessage;
33 import org.xnap.plugin.opennap.net.msg.server.ServerMessage;
34
35 /***
36 *
37 */
38 public class OpenNapBanListCommand extends AbstractCommand
39 {
40
41
42
43
44
45 private OpenNapConsole nc;
46 private String defaultChannel;
47
48
49
50 public OpenNapBanListCommand(OpenNapConsole nc, String defaultChannel)
51 {
52 this.nc = nc;
53 this.defaultChannel = defaultChannel;
54
55 putValue(CMD, "banlist");
56
57 putValue(PARAMETER, "[channel]");
58 putValue(SHORT_HELP, XNap.tr("Lists the bans for channel."));
59 }
60
61
62
63 public void execute(String command, Console console)
64 throws SyntaxException
65 {
66 StringTokenizer t = new StringTokenizer(command);
67
68
69 t.nextToken();
70
71 if (t.countTokens() > 1) {
72 throw new SyntaxException();
73 }
74
75 Receiver listener = new Receiver(t.hasMoreTokens()
76 ? t.nextToken() : defaultChannel);
77 MessageHandler.subscribe(ChannelBanListEntryMessage.TYPE, listener);
78 MessageHandler.subscribe(EndChannelBanListMessage.TYPE, listener);
79
80 nc.println(XNap.tr("Banned users:"));
81
82 ChannelBanListRequestMessage msg
83 = new ChannelBanListRequestMessage
84 (t.hasMoreTokens() ? t.nextToken() : defaultChannel);
85 msg.setExceptionListener(nc);
86 MessageHandler.send(nc.getServer(), msg);
87 }
88
89
90
91 private class Receiver implements MessageListener
92 {
93
94 String channelName;
95
96 public Receiver(String channelName)
97 {
98 this.channelName = channelName;
99 }
100
101 public void messageReceived(ServerMessage message)
102 {
103 if (message.getServer() != nc.getServer()) {
104 return;
105 }
106
107 if (message instanceof ChannelBanListEntryMessage) {
108 ChannelBanListEntryMessage msg
109 = (ChannelBanListEntryMessage)message;
110 StringBuffer sb = new StringBuffer();
111 sb.append(msg.nick);
112 if (msg.reason != null) {
113 sb.append(" (");
114 sb.append(msg.reason);
115 sb.append(")");
116 }
117 nc.println(sb.toString());
118 }
119 else if (message instanceof EndChannelBanListMessage) {
120 MessageHandler.unsubscribe
121 (ChannelBanListEntryMessage.TYPE, this);
122 MessageHandler.unsubscribe
123 (EndChannelBanListMessage.TYPE, this);
124 nc.println(XNap.tr("End of banned users list"));
125 }
126 }
127
128 }
129
130 }