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.pircbot;
21  
22  import java.awt.event.ActionEvent;
23  import java.util.HashSet;
24  
25  import javax.swing.Action;
26  import javax.swing.Icon;
27  
28  import org.xnap.chat.AbstractChannel;
29  import org.xnap.chat.ChatProvider;
30  import org.xnap.chat.action.AbstractBanAction;
31  import org.xnap.chat.action.AbstractDeOpAction;
32  import org.xnap.chat.action.AbstractDeVoiceAction;
33  import org.xnap.chat.action.AbstractKickAction;
34  import org.xnap.chat.action.AbstractOpAction;
35  import org.xnap.chat.action.AbstractVoiceAction;
36  import org.xnap.cmdl.Command;
37  import org.xnap.gui.action.ChangeTopicAction;
38  import org.xnap.gui.action.TopicChangable;
39  import org.xnap.peer.Peer;
40  
41  public class PircBotChannel extends AbstractChannel 
42      implements TopicChangable {
43      
44      //--- Constant(s) ---
45      
46      //--- Data field(s) ---
47  
48      private PircBotServer server;
49  
50      /***
51       * Peer objects that have operator status.
52       */
53      private HashSet opPeers = new HashSet();
54  
55      /***
56       * Peer objects that have voice status.
57       */
58      private HashSet voicePeers = new HashSet();
59  
60  	private boolean joined = false;
61  
62      //--- Constructor(s) ---
63  
64      public PircBotChannel(PircBotServer server, String name)
65      {
66  		super(name);
67  
68  		this.server = server;
69      }
70  
71      //--- Method(s) ---
72  
73      public void close() 
74      {
75  		getServer().getBot().partChannel(getName());
76  		getServer().getBot().removeChannel(this);
77      }
78  
79      public void changeTopic(String topic) 
80      {
81  		getServer().getBot().setTopic(getName(), topic);
82      }
83  
84      /***
85       * @see PircBotPlugin.ICON_16.
86       */
87      public Icon getIcon()
88      {
89  		return PircBotPlugin.ICON_16;
90      }
91  
92      public Action[] getActions() 
93      {
94  		return new Action[] { new ChangeTopicAction(this) };
95      }
96  
97  	public Command[] getCommands()
98  	{
99  		return new Command[] { 
100 			new PircBotMessageCommand(getServer()),
101 		};
102 	}
103 
104     public Action[] getPeerActions(Peer peer)
105     {
106 		return new Action[] { 
107 			new BanAction(peer), new KickAction(peer),
108 			new OpAction(peer), new DeOpAction(peer),
109 			new VoiceAction(peer), new DeVoiceAction(peer),
110 		};
111     }
112 
113     public String getPrefix(Peer peer)
114     {
115 		String prefix = "";
116 		if (voicePeers.contains(peer)) {
117 			prefix += "+";
118 		}
119 		if (opPeers.contains(peer)) {
120 			prefix += "@";
121 		}
122 		return prefix;
123     }
124 
125     public ChatProvider getProvider()
126     {
127 		return server;
128     }
129 
130     public PircBotServer getServer()
131     {
132 		return server;
133     }
134 
135 	public boolean isJoined()
136 	{
137 		return joined;
138 	}
139 
140     public boolean isLocal(Peer peer)
141 	{
142 		return getServer().getLocalPeer().equals(peer);
143 	}
144 
145     public void sendMessage(String message)
146     {
147 		getServer().getBot().sendMessage(getName(), message);
148 		messageReceived(getServer().getLocalPeer(), message);
149     }
150 
151 	public void setJoined(boolean joined, String reason)
152 	{
153 		if (this.joined != joined) {
154 			this.joined = joined;
155 			if (joined) {
156 				joined();
157 			}
158 			else {
159 				parted(reason);
160 			}
161 		}
162 	}
163 
164     public void setOp(Peer peer, boolean isOperator)
165     {
166 		if (isOperator) {
167 			opPeers.add(peer);
168 		}
169 		else {
170 			opPeers.remove(peer);
171 		}
172 		peerChanged(peer);
173     }
174 
175     public void setVoice(Peer peer, boolean hasVoice)
176     {
177 		if (hasVoice) {
178 			voicePeers.add(peer);
179 		}
180 		else {
181 			voicePeers.remove(peer);
182 		}
183 		peerChanged(peer);
184     }
185 
186     //--- Inner Class(es) ---
187 
188     private class DeOpAction extends AbstractDeOpAction
189     {
190 	
191 		private Peer peer;
192 
193 		public DeOpAction(Peer peer)
194 		{
195 			this.peer = peer;
196 		}
197 
198         public void actionPerformed(ActionEvent event) 
199 		{
200 			getServer().getBot().deOp(getName(), peer.getName());
201 		}
202 
203     }
204 
205     private class DeVoiceAction extends AbstractDeVoiceAction
206     {
207 	
208 		private Peer peer;
209 
210 		public DeVoiceAction(Peer peer)
211 		{
212 			this.peer = peer;
213 		}
214 
215         public void actionPerformed(ActionEvent event) 
216 		{
217 			getServer().getBot().deVoice(getName(), peer.getName());
218 		}
219 
220     }
221 
222     private class BanAction extends AbstractBanAction
223     {
224 	
225 		private Peer peer;
226 
227 		public BanAction(Peer peer)
228 		{
229 			this.peer = peer;
230 		}
231 
232         public void actionPerformed(ActionEvent event) 
233 		{
234 			getServer().getBot().ban(getName(), peer.getHost());
235 		}
236 
237     }
238 
239     private class KickAction extends AbstractKickAction
240     {
241 	
242 		private Peer peer;
243 
244 		public KickAction(Peer peer)
245 		{
246 			this.peer = peer;
247 		}
248 
249         public void actionPerformed(ActionEvent event) 
250 		{
251 			getServer().getBot().kick(getName(), peer.getName());
252 		}
253 
254     }
255 
256     private class OpAction extends AbstractOpAction
257     {
258 	
259 		private Peer peer;
260 
261 		public OpAction(Peer peer)
262 		{
263 			this.peer = peer;
264 		}
265 
266         public void actionPerformed(ActionEvent event) 
267 		{
268 			getServer().getBot().op(getName(), peer.getName());
269 		}
270 
271     }
272 
273     private class VoiceAction extends AbstractVoiceAction
274     {
275 	
276 		private Peer peer;
277 
278 		public VoiceAction(Peer peer)
279 		{
280 			this.peer = peer;
281 		}
282 
283         public void actionPerformed(ActionEvent event) 
284 		{
285 			getServer().getBot().voice(getName(), peer.getName());
286 		}
287 
288     }
289 
290 }