|
@@ -10,9 +10,12 @@ import com.dmdirc.parser.irc.ChannelInfo;
|
10
|
10
|
import com.dmdirc.parser.irc.ClientInfo;
|
11
|
11
|
import com.dmdirc.parser.irc.IRCParser;
|
12
|
12
|
import com.dmdirc.parser.irc.callbacks.interfaces.IChannelMessage;
|
|
13
|
+import com.dmdirc.parser.irc.callbacks.interfaces.IPrivateCTCP;
|
13
|
14
|
import com.dmdirc.parser.irc.callbacks.interfaces.IPrivateMessage;
|
|
15
|
+import com.dmdirc.util.RollingList;
|
14
|
16
|
import com.md87.charliebravo.commands.AuthenticateCommand;
|
15
|
17
|
import com.md87.charliebravo.commands.CalcCommand;
|
|
18
|
+import com.md87.charliebravo.commands.DefineCommand;
|
16
|
19
|
import com.md87.charliebravo.commands.FollowupsCommand;
|
17
|
20
|
import com.md87.charliebravo.commands.GitCommand;
|
18
|
21
|
import com.md87.charliebravo.commands.GoogleCommand;
|
|
@@ -23,19 +26,23 @@ import com.md87.charliebravo.commands.QuitCommand;
|
23
|
26
|
import com.md87.charliebravo.commands.ReloadCommand;
|
24
|
27
|
import com.md87.charliebravo.commands.SetCommand;
|
25
|
28
|
import com.md87.charliebravo.commands.SkillCommand;
|
|
29
|
+import com.md87.charliebravo.commands.SnippetsCommand;
|
26
|
30
|
import com.md87.charliebravo.commands.TranslateCommand;
|
27
|
31
|
import com.md87.charliebravo.commands.WhoisCommand;
|
|
32
|
+import com.md87.util.crypto.ArcFourEncrypter;
|
28
|
33
|
import java.util.ArrayList;
|
29
|
34
|
import java.util.HashMap;
|
30
|
35
|
import java.util.List;
|
31
|
36
|
import java.util.Map;
|
|
37
|
+import java.util.Random;
|
32
|
38
|
import java.util.regex.Matcher;
|
|
39
|
+import net.miginfocom.Base64;
|
33
|
40
|
|
34
|
41
|
/**
|
35
|
42
|
*
|
36
|
43
|
* @author chris
|
37
|
44
|
*/
|
38
|
|
-public class InputHandler implements IChannelMessage, IPrivateMessage {
|
|
45
|
+public class InputHandler implements IChannelMessage, IPrivateMessage, IPrivateCTCP {
|
39
|
46
|
|
40
|
47
|
protected IRCParser parser;
|
41
|
48
|
|
|
@@ -43,7 +50,7 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
43
|
50
|
|
44
|
51
|
protected final List<Command> commands = new ArrayList<Command>();
|
45
|
52
|
protected final Map<String, Response> responses = new HashMap<String, Response>();
|
46
|
|
- protected final Map<String, String> snippets = new HashMap<String, String>();
|
|
53
|
+ protected final Map<String, RollingList<String>> snippets = new HashMap<String, RollingList<String>>();
|
47
|
54
|
|
48
|
55
|
public InputHandler(final Config config) {
|
49
|
56
|
this.config = config;
|
|
@@ -60,8 +67,10 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
60
|
67
|
commands.add(new GitCommand());
|
61
|
68
|
commands.add(new SetCommand());
|
62
|
69
|
commands.add(new SkillCommand());
|
|
70
|
+ commands.add(new SnippetsCommand());
|
63
|
71
|
commands.add(new NewzbinCommand());
|
64
|
72
|
commands.add(new ReloadCommand());
|
|
73
|
+ commands.add(new DefineCommand());
|
65
|
74
|
}
|
66
|
75
|
|
67
|
76
|
public Config getConfig() {
|
|
@@ -76,6 +85,10 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
76
|
85
|
return responses.get(target);
|
77
|
86
|
}
|
78
|
87
|
|
|
88
|
+ public RollingList<String> getSnippets(final String channel) {
|
|
89
|
+ return snippets.get(channel);
|
|
90
|
+ }
|
|
91
|
+
|
79
|
92
|
public IRCParser getParser() {
|
80
|
93
|
return parser;
|
81
|
94
|
}
|
|
@@ -85,6 +98,7 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
85
|
98
|
|
86
|
99
|
parser.getCallbackManager().addCallback("OnChannelMessage", this);
|
87
|
100
|
parser.getCallbackManager().addCallback("OnPrivateMessage", this);
|
|
101
|
+ parser.getCallbackManager().addCallback("OnPrivateCTCP", this);
|
88
|
102
|
}
|
89
|
103
|
|
90
|
104
|
public void onChannelMessage(final IRCParser tParser, final ChannelInfo cChannel,
|
|
@@ -96,8 +110,10 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
96
|
110
|
break;
|
97
|
111
|
} else if (sMessage.matches("^(?i)" + Matcher.quoteReplacement(nick) + "\\?")
|
98
|
112
|
&& snippets.containsKey(cChannel.getName())) {
|
99
|
|
- handleInput(ClientInfo.parseHost(sHost), cChannel.getName(),
|
100
|
|
- snippets.get(cChannel.getName()));
|
|
113
|
+ final RollingList<String> snips = snippets.get(cChannel.getName());
|
|
114
|
+ final String snippet = snips.get(snips.getList().size() - 1);
|
|
115
|
+ handleInput(ClientInfo.parseHost(sHost), cChannel.getName(), snippet);
|
|
116
|
+ snips.remove(snippet);
|
101
|
117
|
break;
|
102
|
118
|
}
|
103
|
119
|
}
|
|
@@ -106,10 +122,14 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
106
|
122
|
.getKeyDomain("snippets").entrySet()) {
|
107
|
123
|
System.out.println("Snippet test: " + snippet.getKey());
|
108
|
124
|
if (sMessage.matches(snippet.getKey())) {
|
109
|
|
- snippets.put(cChannel.getName(), sMessage.replaceFirst(snippet.getKey(),
|
|
125
|
+ if (!snippets.containsKey(cChannel.getName())) {
|
|
126
|
+ snippets.put(cChannel.getName(), new RollingList<String>(10));
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ snippets.get(cChannel.getName()).add(sMessage.replaceFirst(snippet.getKey(),
|
|
130
|
+ snippet.getValue()));
|
|
131
|
+ System.out.println("Snippet: " + sMessage.replaceFirst(snippet.getKey(),
|
110
|
132
|
snippet.getValue()));
|
111
|
|
- System.out.println("Snippet: " + snippets.get(cChannel.getName()));
|
112
|
|
- break;
|
113
|
133
|
}
|
114
|
134
|
}
|
115
|
135
|
}
|
|
@@ -127,6 +147,47 @@ public class InputHandler implements IChannelMessage, IPrivateMessage {
|
127
|
147
|
handleInput(ClientInfo.parseHost(sHost), ClientInfo.parseHost(sHost), sMessage);
|
128
|
148
|
}
|
129
|
149
|
|
|
150
|
+ @SuppressWarnings("unchecked")
|
|
151
|
+ public void onPrivateCTCP(IRCParser tParser, String sType, String sMessage, String sHost) {
|
|
152
|
+ final ClientInfo client = tParser.getClientInfoOrFake(sHost);
|
|
153
|
+ if ("COOKIE".equals(sType)) {
|
|
154
|
+ final String status = (String) client.getMap().get("Cookie");
|
|
155
|
+ final String key1 = (String) client.getMap().get("Key1");
|
|
156
|
+ final String key2 = (String) client.getMap().get("Key2");
|
|
157
|
+ final String idp = (String) client.getMap().get("OpenID-p");
|
|
158
|
+
|
|
159
|
+ if ("GETKEY".equals(sMessage) && "SET".equals(status)) {
|
|
160
|
+ parser.sendCTCP(ClientInfo.parseHost(sHost), "COOKIE", "SETKEY " + key1);
|
|
161
|
+ client.getMap().put("Cookie", "GETKEY");
|
|
162
|
+ } else if (sMessage.startsWith("OFFER")) {
|
|
163
|
+ final String what = sMessage.substring(6);
|
|
164
|
+
|
|
165
|
+ if (config.getConfigfile().getKeyDomain("cookies").containsKey(what)) {
|
|
166
|
+ client.getMap().put("Key1", config.getConfigfile()
|
|
167
|
+ .getKeyDomain("cookies").get(what));
|
|
168
|
+ client.getMap().put("OpenID-p", config.getConfigfile()
|
|
169
|
+ .getKeyDomain("cookie-ids").get(what));
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ final byte[] bytes = new byte[50];
|
|
173
|
+ new Random().nextBytes(bytes);
|
|
174
|
+ final String newkey2 = Base64.encodeToString(bytes, false);
|
|
175
|
+
|
|
176
|
+ client.getMap().put("Cookie", "OFFER");
|
|
177
|
+ client.getMap().put("Key2", newkey2);
|
|
178
|
+ parser.sendCTCP(ClientInfo.parseHost(sHost), "COOKIE", "GET " + newkey2);
|
|
179
|
+ } else if (sMessage.startsWith("SHOW") && "OFFER".equals(status)) {
|
|
180
|
+ final String payload = sMessage.substring(5);
|
|
181
|
+ final String info = new String(new ArcFourEncrypter(key1 + key2)
|
|
182
|
+ .encrypt(Base64.decode(payload)));
|
|
183
|
+
|
|
184
|
+ if (idp.equals(info)) {
|
|
185
|
+ client.getMap().put("OpenID", idp);
|
|
186
|
+ }
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+ }
|
|
190
|
+
|
130
|
191
|
protected void handleInput(final String source, final String target, final String text) {
|
131
|
192
|
new Thread(new Runnable() {
|
132
|
193
|
public void run() {
|