forked from TheChef1337/PircBot2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDccManager.java
More file actions
138 lines (125 loc) · 4.87 KB
/
Copy pathDccManager.java
File metadata and controls
138 lines (125 loc) · 4.87 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
/*
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.util.*;
/**
* This class is used to process DCC events from the server.
*
* @since 1.2.0
* @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 DccManager {
/**
* Constructs a DccManager to look after all DCC SEND and CHAT events.
*
* @param bot The PircBot whose DCC events this class will handle.
*/
DccManager(PircBot bot) {
_bot = bot;
}
/**
* Processes a DCC request.
*
* @return True if the type of request was handled successfully.
*/
boolean processRequest(String nick, String login, String hostname, String request) {
StringTokenizer tokenizer = new StringTokenizer(request);
tokenizer.nextToken();
String type = tokenizer.nextToken();
String filename = tokenizer.nextToken();
switch (type) {
case "SEND": {
long address = Long.parseLong(tokenizer.nextToken());
int port = Integer.parseInt(tokenizer.nextToken());
long size = -1;
try {
size = Long.parseLong(tokenizer.nextToken());
} catch (Exception e) {
// Stick with the old value.
}
DccFileTransfer transfer = new DccFileTransfer(_bot, this, nick, login, hostname, type, filename, address, port, size);
_bot.onIncomingFileTransfer(transfer);
break;
}
case "RESUME": {
int port = Integer.parseInt(tokenizer.nextToken());
long progress = Long.parseLong(tokenizer.nextToken());
DccFileTransfer transfer = null;
synchronized (_awaitingResume) {
for (int i = 0; i < _awaitingResume.size(); i++) {
transfer = (DccFileTransfer) _awaitingResume.elementAt(i);
if (transfer.getNick().equals(nick) && transfer.getPort() == port) {
_awaitingResume.removeElementAt(i);
break;
}
}
}
if (transfer != null) {
transfer.setProgress(progress);
_bot.sendCTCPCommand(nick, "DCC ACCEPT file.ext " + port + " " + progress);
}
break;
}
case "ACCEPT": {
int port = Integer.parseInt(tokenizer.nextToken());
long progress = Long.parseLong(tokenizer.nextToken());
DccFileTransfer transfer = null;
synchronized (_awaitingResume) {
for (int i = 0; i < _awaitingResume.size(); i++) {
transfer = (DccFileTransfer) _awaitingResume.elementAt(i);
if (transfer.getNick().equals(nick) && transfer.getPort() == port) {
_awaitingResume.removeElementAt(i);
break;
}
}
}
if (transfer != null) {
transfer.doReceive(transfer.getFile(), true);
}
break;
}
case "CHAT": {
long address = Long.parseLong(tokenizer.nextToken());
int port = Integer.parseInt(tokenizer.nextToken());
final DccChat chat = new DccChat(_bot, nick, login, hostname, address, port);
new Thread() {
@Override
public void run() {
_bot.onIncomingChatRequest(chat);
}
}.start();
break;
}
default:
return false;
}
return true;
}
/**
* Add this DccFileTransfer to the list of those awaiting possible resuming.
*
* @param transfer the DccFileTransfer that may be resumed.
*/
void addAwaitingResume(DccFileTransfer transfer) {
synchronized (_awaitingResume) {
_awaitingResume.addElement(transfer);
}
}
/**
* Remove this transfer from the list of those awaiting resuming.
*/
void removeAwaitingResume(DccFileTransfer transfer) {
_awaitingResume.removeElement(transfer);
}
private PircBot _bot;
private final Vector _awaitingResume = new Vector();
}