forked from TheChef1337/PircBot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDccChat.java
More file actions
226 lines (190 loc) · 6.31 KB
/
Copy pathDccChat.java
File metadata and controls
226 lines (190 loc) · 6.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright Paul James Mutton, 2001-2009, http://www.jibble.org/
This file is part of PircBot.
This software is dual-licensed, allowing you to choose between the GNU
General Public License (GPL) and the www.jibble.org Commercial License.
Since the GPL may be too restrictive for use in a proprietary application,
a commercial license is also provided. Full license information can be
found at http://www.jibble.org/licenses/
*/
package PircBot;
import java.net.*;
import java.io.*;
/**
* This class is used to allow the bot to interact with a DCC Chat session.
*
* @since 0.9c
* @author Paul James Mutton,
* <a href="http://www.jibble.org/">http://www.jibble.org/</a>
* @version 1.5.0 (Build time: Mon Dec 14 20:07:17 2009)
*/
public class DccChat {
/**
* This constructor is used when we are accepting a DCC CHAT request
* from somebody. It attempts to connect to the client that issued the
* request.
*
* @param bot An instance of the underlying PircBot.
* @param nick The nick of the sender.
* @param address The address to connect to.
* @param port The port number to connect to.
*/
DccChat(PircBot bot, String nick, String login, String hostname, long address, int port) {
_bot = bot;
_address = address;
_port = port;
_nick = nick;
_login = login;
_hostname = hostname;
_acceptable = true;
}
/**
* This constructor is used after we have issued a DCC CHAT request to
* somebody. If the client accepts the chat request, then the socket we
* obtain is passed to this constructor.
*
* @param bot An instance of the underlying PircBot.
* @param nick The nick of the user we are sending the request to.
* @param socket The socket which will be used for the DCC CHAT session.
*
* @throws IOException If the socket cannot be read from.
*/
DccChat(PircBot bot, String nick, Socket socket) throws IOException {
_bot = bot;
_nick = nick;
_socket = socket;
_reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
_writer = new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream()));
_acceptable = false;
}
/**
* Accept this DccChat connection.
*
* @throws java.io.IOException
* @since 1.2.0
*
*/
public synchronized void accept() throws IOException {
if (_acceptable) {
_acceptable = false;
int[] ip = _bot.longToIp(_address);
String ipStr = ip[0] + "." + ip[1] + "." + ip[2] + "." + ip[3];
_socket = new Socket(ipStr, _port);
_reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
_writer = new BufferedWriter(new OutputStreamWriter(_socket.getOutputStream()));
}
}
/**
* Reads the next line of text from the client at the other end of our DCC Chat
* connection. This method blocks until something can be returned.
* If the connection has closed, null is returned.
*
* @return The next line of text from the client. Returns null if the
* connection has closed normally.
*
* @throws IOException If an I/O error occurs.
*/
public String readLine() throws IOException {
if (_acceptable) {
throw new IOException("You must call the accept() method of the DccChat request before you can use it.");
}
return _reader.readLine();
}
/**
* Sends a line of text to the client at the other end of our DCC Chat
* connection.
*
* @param line The line of text to be sent. This should not include
* linefeed characters.
*
* @throws IOException If an I/O error occurs.
*/
public void sendLine(String line) throws IOException {
if (_acceptable) {
throw new IOException("You must call the accept() method of the DccChat request before you can use it.");
}
// No need for synchronization here really...
_writer.write(line + "\r\n");
_writer.flush();
}
/**
* Closes the DCC Chat connection.
*
* @throws IOException If an I/O error occurs.
*/
public void close() throws IOException {
if (_acceptable) {
throw new IOException("You must call the accept() method of the DccChat request before you can use it.");
}
_socket.close();
}
/**
* Returns the nick of the other user taking part in this file transfer.
*
* @return the nick of the other user.
*
*/
public String getNick() {
return _nick;
}
/**
* Returns the login of the DCC Chat initiator.
*
* @return the login of the DCC Chat initiator. null if we sent it.
*
*/
public String getLogin() {
return _login;
}
/**
* Returns the hostname of the DCC Chat initiator.
*
* @return the hostname of the DCC Chat initiator. null if we sent it.
*
*/
public String getHostname() {
return _hostname;
}
/**
* Returns the BufferedReader used by this DCC Chat.
*
* @return the BufferedReader used by this DCC Chat.
*/
public BufferedReader getBufferedReader() {
return _reader;
}
/**
* Returns the BufferedReader used by this DCC Chat.
*
* @return the BufferedReader used by this DCC Chat.
*/
public BufferedWriter getBufferedWriter() {
return _writer;
}
/**
* Returns the raw Socket used by this DCC Chat.
*
* @return the raw Socket used by this DCC Chat.
*/
public Socket getSocket() {
return _socket;
}
/**
* Returns the address of the sender as a long.
*
* @return the address of the sender as a long.
*/
public long getNumericalAddress() {
return _address;
}
private PircBot _bot;
private String _nick;
private String _login = null;
private String _hostname = null;
private BufferedReader _reader;
private BufferedWriter _writer;
private Socket _socket;
private boolean _acceptable;
private long _address = 0;
private int _port = 0;
}