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  
24  import javax.swing.*;
25  
26  import org.xnap.net.*;
27  import org.xnap.peer.AbstractPeer;
28  import org.xnap.peer.action.*;
29  import org.xnap.peer.action.AbstractWhoisAction;
30  import org.xnap.transfer.*;
31  import org.xnap.util.*;
32  
33  /***
34   * This class represents a PircBot user.
35   */
36  public class PircBotPeer extends AbstractPeer {
37  
38      //--- Data field(s) ---
39  
40      private String host;
41  
42      private PircBotServer server;
43      
44      //--- Constructor(s) ---
45  
46      public PircBotPeer(PircBotServer server, String nick)
47      {
48  		super(nick);
49  
50  		this.server = server;
51      }
52  
53      //--- Method(s) ---
54  
55      public Action[] getActions()
56      {
57  		return new Action[] { 
58  			new WhoisAction(), new ChatAction(), new SendFileAction(),
59  		};
60      }
61  
62      public int getLocalDownloadCount()
63      {
64  		return 0;
65      }
66      
67      public int getLocalUploadCount()
68      {
69  		return 0;
70      }
71  
72      public String getHost()
73      {
74  		return host;
75      }
76  
77      public PircBotServer getServer()
78      {
79  		return server;
80      }
81  
82      public String getStatus()
83      {
84  		return null;
85      }
86  
87      public void setHost(String host)
88      {
89  		this.host = host;
90      }
91  
92      //--- Inner Class(es) ---
93  
94      private class ChatAction extends AbstractChatAction {
95  
96          public void actionPerformed(ActionEvent event) 
97  		{
98  			getServer().getBot().getPrivateChannelByName
99  				(PircBotPeer.this.getName());
100 		}
101 
102     }
103 
104     private class SendFileAction extends AbstractSendFileAction {
105 
106         public void actionPerformed(ActionEvent event) 
107 		{
108 			JFileChooser chooser = new JFileChooser();
109 			if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
110 				DccUpload u = new DccUpload
111 					(PircBotPeer.this, chooser.getSelectedFile());
112 				UploadManager.getInstance().add(u);
113 				u.start();
114 				if (u.getLocalPort() != 0) {
115 					long ip = NetHelper.ipToLongHiFirst
116 						(getServer().getBot().getInetAddress().getAddress());
117 					getServer().getBot().sendCTCPCommand
118 						(getName(), 
119 						 "DCC SEND " + FileHelper.toAscii(u.getFilename())
120 						 + " " + ip
121 						 + " " + u.getLocalPort()
122 						 + " " + u.getFilesize());
123 				}
124 			}
125 		}
126 
127     }
128 
129     /***
130      * Not supported by PircBot.
131      */
132     private class WhoisAction extends AbstractWhoisAction {
133 
134         public void actionPerformed(ActionEvent event) 
135 		{
136 			getServer().getBot().whois(PircBotPeer.this.getName());
137 		}
138 
139     }
140 
141 }