View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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      //--- Constant(s) ---
42      
43      //--- Data field(s) ---
44  
45  	private OpenNapConsole nc;
46  	private String defaultChannel;
47  	
48      //--- Constructor(s) ---
49  
50  	public OpenNapBanListCommand(OpenNapConsole nc, String defaultChannel)
51  	{
52  		this.nc = nc;
53  		this.defaultChannel = defaultChannel;
54  
55  		putValue(CMD, "banlist");
56  		//putValue(ALIASES, new String[] { "reg" });
57  		putValue(PARAMETER, "[channel]");
58  		putValue(SHORT_HELP, XNap.tr("Lists the bans for channel."));
59  	}
60  
61  	//--- Method(s) ---
62  
63  	public void execute(String command, Console console)
64  		throws SyntaxException
65  	{
66  		StringTokenizer t = new StringTokenizer(command);
67  		
68  		// skip command
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  	//--- Inner Class(es) ---
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 }