瀏覽代碼

Support for irc cookies

master
Chris Smith 15 年之前
父節點
當前提交
1fe1660415

+ 9
- 3
nbproject/build-impl.xml 查看文件

@@ -70,6 +70,7 @@ is divided into following sections:
70 70
         </condition>
71 71
         <condition property="have.sources">
72 72
             <or>
73
+                <available file="${src.src4.dir}"/>
73 74
                 <available file="${src.src3.dir}"/>
74 75
                 <available file="${src.src2.dir}"/>
75 76
                 <available file="${src.src.dir}"/>
@@ -127,6 +128,7 @@ is divided into following sections:
127 128
         <!-- You can override this target in the ../build.xml file. -->
128 129
     </target>
129 130
     <target depends="-pre-init,-init-private,-init-user,-init-project,-do-init" name="-init-check">
131
+        <fail unless="src.src4.dir">Must set src.src4.dir</fail>
130 132
         <fail unless="src.src3.dir">Must set src.src3.dir</fail>
131 133
         <fail unless="src.src2.dir">Must set src.src2.dir</fail>
132 134
         <fail unless="src.src.dir">Must set src.src.dir</fail>
@@ -152,7 +154,7 @@ is divided into following sections:
152 154
     </target>
153 155
     <target name="-init-macrodef-javac">
154 156
         <macrodef name="javac" uri="http://www.netbeans.org/ns/j2se-project/3">
155
-            <attribute default="${src.src3.dir}:${src.src2.dir}:${src.src.dir}:${src.dir}" name="srcdir"/>
157
+            <attribute default="${src.src4.dir}:${src.src3.dir}:${src.src2.dir}:${src.src.dir}:${src.dir}" name="srcdir"/>
156 158
             <attribute default="${build.classes.dir}" name="destdir"/>
157 159
             <attribute default="${javac.classpath}" name="classpath"/>
158 160
             <attribute default="${includes}" name="includes"/>
@@ -171,7 +173,7 @@ is divided into following sections:
171 173
             </sequential>
172 174
         </macrodef>
173 175
         <macrodef name="depend" uri="http://www.netbeans.org/ns/j2se-project/3">
174
-            <attribute default="${src.src3.dir}:${src.src2.dir}:${src.src.dir}:${src.dir}" name="srcdir"/>
176
+            <attribute default="${src.src4.dir}:${src.src3.dir}:${src.src2.dir}:${src.src.dir}:${src.dir}" name="srcdir"/>
175 177
             <attribute default="${build.classes.dir}" name="destdir"/>
176 178
             <attribute default="${javac.classpath}" name="classpath"/>
177 179
             <sequential>
@@ -343,6 +345,7 @@ is divided into following sections:
343 345
     <target depends="init,deps-jar,-pre-pre-compile,-pre-compile,-compile-depend" if="have.sources" name="-do-compile">
344 346
         <j2seproject3:javac/>
345 347
         <copy todir="${build.classes.dir}">
348
+            <fileset dir="${src.src4.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
346 349
             <fileset dir="${src.src3.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
347 350
             <fileset dir="${src.src2.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
348 351
             <fileset dir="${src.src.dir}" excludes="${build.classes.excludes},${excludes}" includes="${includes}"/>
@@ -361,7 +364,7 @@ is divided into following sections:
361 364
     <target depends="init,deps-jar,-pre-pre-compile" name="-do-compile-single">
362 365
         <fail unless="javac.includes">Must select some files in the IDE or set javac.includes</fail>
363 366
         <j2seproject3:force-recompile/>
364
-        <j2seproject3:javac excludes="" includes="${javac.includes}" sourcepath="${src.src3.dir}:${src.src2.dir}:${src.src.dir}:${src.dir}"/>
367
+        <j2seproject3:javac excludes="" includes="${javac.includes}" sourcepath="${src.src4.dir}:${src.src3.dir}:${src.src2.dir}:${src.src.dir}:${src.dir}"/>
365 368
     </target>
366 369
     <target name="-post-compile-single">
367 370
         <!-- Empty placeholder for easier customization. -->
@@ -495,6 +498,9 @@ is divided into following sections:
495 498
             <classpath>
496 499
                 <path path="${javac.classpath}"/>
497 500
             </classpath>
501
+            <fileset dir="${src.src4.dir}" excludes="${excludes}" includes="${includes}">
502
+                <filename name="**/*.java"/>
503
+            </fileset>
498 504
             <fileset dir="${src.src3.dir}" excludes="${excludes}" includes="${includes}">
499 505
                 <filename name="**/*.java"/>
500 506
             </fileset>

+ 3
- 3
nbproject/genfiles.properties 查看文件

@@ -1,8 +1,8 @@
1
-build.xml.data.CRC32=0e8d4183
1
+build.xml.data.CRC32=aaeff047
2 2
 build.xml.script.CRC32=d4296ee3
3 3
 build.xml.stylesheet.CRC32=958a1d3e
4 4
 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
5 5
 # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
6
-nbproject/build-impl.xml.data.CRC32=0e8d4183
7
-nbproject/build-impl.xml.script.CRC32=3c4579ea
6
+nbproject/build-impl.xml.data.CRC32=aaeff047
7
+nbproject/build-impl.xml.script.CRC32=9c9a087f
8 8
 nbproject/build-impl.xml.stylesheet.CRC32=65b8de21

+ 1
- 0
nbproject/project.properties 查看文件

@@ -77,4 +77,5 @@ src.dir=src
77 77
 src.src.dir=externals/dmdirc/src
78 78
 src.src2.dir=externals/evetool/src
79 79
 src.src3.dir=externals/dmdirc-plugins/calc/src
80
+src.src4.dir=externals/util/src
80 81
 test.src.dir=test

+ 1
- 0
nbproject/project.xml 查看文件

@@ -6,6 +6,7 @@
6 6
             <name>charliebravo</name>
7 7
             <minimum-ant-version>1.6.5</minimum-ant-version>
8 8
             <source-roots>
9
+                <root id="src.src4.dir"/>
9 10
                 <root id="src.src3.dir"/>
10 11
                 <root id="src.src2.dir"/>
11 12
                 <root id="src.src.dir"/>

+ 68
- 7
src/com/md87/charliebravo/InputHandler.java 查看文件

@@ -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() {

+ 21
- 0
src/com/md87/charliebravo/commands/AuthenticateCommand.java 查看文件

@@ -9,7 +9,10 @@ import com.dmdirc.util.Downloader;
9 9
 import com.md87.charliebravo.Command;
10 10
 import com.md87.charliebravo.InputHandler;
11 11
 import com.md87.charliebravo.Response;
12
+import com.md87.util.crypto.ArcFourEncrypter;
12 13
 import java.util.List;
14
+import java.util.Random;
15
+import net.miginfocom.Base64;
13 16
 
14 17
 /**
15 18
  *
@@ -29,14 +32,32 @@ public class AuthenticateCommand implements Command {
29 32
             if (result.isEmpty() || !result.get(0).trim().equalsIgnoreCase("Success")) {
30 33
                 response.sendMessage("I could not authenticate that token", true);
31 34
             } else {
35
+                final byte[] bytes = new byte[50];
36
+                new Random().nextBytes(bytes);
37
+                final String key1 = Base64.encodeToString(bytes, false);
38
+                final int id = handler.getConfig().getConfigfile().getKeyDomain("cookies").size();
39
+                
32 40
                 final String openid = result.get(1).trim();
33 41
                 handler.getParser().getClientInfoOrFake(response.getSource())
34 42
                         .getMap().put("OpenID", openid);
43
+                handler.getParser().getClientInfoOrFake(response.getSource())
44
+                        .getMap().put("Key1", key1);
45
+                handler.getParser().getClientInfoOrFake(response.getSource())
46
+                        .getMap().put("Cookie", "SET");
35 47
                 handler.getConfig().setOption(openid, "internal.lastseen",
36 48
                         System.currentTimeMillis());
37 49
                 handler.getConfig().setOption(openid, "internal.lastuser",
38 50
                         handler.getParser().getClientInfoOrFake(response.getSource()).toString());
39 51
                 response.sendMessage("You are now authenticated as " + openid, true);
52
+
53
+                handler.getConfig().getConfigfile().getKeyDomain("cookies")
54
+                        .put(String.valueOf(id), key1);
55
+                handler.getConfig().getConfigfile().getKeyDomain("cookie-ids")
56
+                        .put(String.valueOf(id), openid);
57
+
58
+                handler.getParser().sendCTCP(response.getSource(), "COOKIE",
59
+                        "SET " + id + " "+ Base64.encodeToString(
60
+                        new ArcFourEncrypter(key1).encrypt(openid.getBytes()), false));
40 61
             }
41 62
         }
42 63
 

Loading…
取消
儲存