]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/CTCPInterpretor.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / CTCPInterpretor.java
1 /*****************************************************/\r
2 /*          This java file is a part of the          */\r
3 /*                                                   */\r
4 /*           -  Plouf's Java IRC Client  -           */\r
5 /*                                                   */\r
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */\r
7 /*                                                   */\r
8 /*         All contacts : theplouf@yahoo.com         */\r
9 /*                                                   */\r
10 /*  PJIRC is free software; you can redistribute     */\r
11 /*  it and/or modify it under the terms of the GNU   */\r
12 /*  General Public License as published by the       */\r
13 /*  Free Software Foundation; version 2 or later of  */\r
14 /*  the License.                                     */\r
15 /*                                                   */\r
16 /*  PJIRC is distributed in the hope that it will    */\r
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */\r
18 /*  even the implied warranty of MERCHANTABILITY or  */\r
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */\r
20 /*  General Public License for more details.         */\r
21 /*                                                   */\r
22 /*  You should have received a copy of the GNU       */\r
23 /*  General Public License along with PJIRC; if      */\r
24 /*  not, write to the Free Software Foundation,      */\r
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */\r
26 /*  MA  02111-1307  USA                              */\r
27 /*                                                   */\r
28 /*****************************************************/\r
29 \r
30 package irc;\r
31 \r
32 import irc.dcc.prv.DCCChatServer;\r
33 import irc.dcc.prv.DCCFileHandler;\r
34 \r
35 import java.io.File;\r
36 import java.util.Date;\r
37 \r
38 /**\r
39  * The CTCP interpretor.\r
40  */\r
41 public class CTCPInterpretor extends BasicInterpretor {\r
42         /**\r
43          * The CTCPFilter.\r
44          */\r
45         // protected CTCPFilter _filter;\r
46         protected ServerManager _mgr;\r
47 \r
48         /**\r
49          * Create a new CTCPInterpretor.\r
50          * \r
51          * @param config\r
52          *          global configuration.\r
53          * @param next\r
54          *          next interpretor to use if command is unknown.\r
55          * @param mgr\r
56          *          server manager\r
57          */\r
58         public CTCPInterpretor(IRCConfiguration config, Interpretor next/*\r
59                                                                                                                                                                                                                                                                          * ,CTCPFilter\r
60                                                                                                                                                                                                                                                                          * filter\r
61                                                                                                                                                                                                                                                                          */, ServerManager mgr) {\r
62                 super(config, next);\r
63                 _mgr = mgr;\r
64                 // _filter=filter;\r
65         }\r
66 \r
67         private void send(Server s, String destination, String msg) {\r
68                 s.say(destination, "\1" + msg + "\1");\r
69         }\r
70 \r
71         @Override\r
72         protected void handleCommand(Source source, String cmd, String[] parts, String[] cumul) {\r
73                 try {\r
74                         if (cmd.equals("ctcp")) {\r
75                                 test(cmd, parts, 1);\r
76                                 if (parts[1].toLowerCase(java.util.Locale.ENGLISH).equals("ping")) {\r
77                                         test(cmd, parts, 2);\r
78                                         send(source.getServer(), parts[2], "PING " + (new Date()).getTime());\r
79                                         // _filter.ping(source.getServer(),parts[2]);\r
80                                 } else if (parts[1].toLowerCase(java.util.Locale.ENGLISH).equals("action")) {\r
81                                         test(cmd, parts, 2);\r
82                                         if (source.talkable()) {\r
83                                                 send(source.getServer(), source.getName(), "ACTION " + cumul[2]);\r
84                                                 // _filter.action(source.getServer(),source.getName(),cumul[2]);\r
85                                                 source.action(source.getServer().getNick(), cumul[2]);\r
86                                         } else {\r
87                                                 source.report(getText(IRCTextProvider.INTERPRETOR_NOT_ON_CHANNEL));\r
88                                         }\r
89                                 } else if (parts[1].toLowerCase(java.util.Locale.ENGLISH).equals("dcc")) {\r
90                                         test(cmd, parts, 2);\r
91                                         if (parts[2].toLowerCase(java.util.Locale.ENGLISH).equals("chat") && _ircConfiguration.getB("allowdccchat")) {\r
92                                                 test(cmd, parts, 3);\r
93                                                 // _filter.chat(source.getServer(),parts[3]);\r
94                                                 Server s = source.getServer();\r
95                                                 String nick = parts[3];\r
96                                                 try {\r
97                                                         DCCChatServer cserver = new DCCChatServer(_ircConfiguration, s.getNick(), nick);\r
98                                                         String arg = cserver.openPassive();\r
99                                                         if (arg.length() == 0) {\r
100                                                                 cserver.sendStatusMessage(getText(IRCTextProvider.DCC_UNABLE_PASSIVE_MODE));\r
101                                                         } else {\r
102                                                                 send(s, nick, "DCC CHAT chat " + arg);\r
103                                                         }\r
104                                                         _mgr.newServer(cserver, false);\r
105                                                 } catch (Throwable ex) {\r
106                                                         _ircConfiguration.internalError("dcc chat error", ex, "plouf@pjirc.com");\r
107                                                 }\r
108 \r
109                                         } else if (parts[2].toLowerCase(java.util.Locale.ENGLISH).equals("send")\r
110                                                         && _ircConfiguration.getB("allowdccfile")) {\r
111                                                 String file = null;\r
112                                                 test(cmd, parts, 3);\r
113 \r
114                                                 if (parts.length > 4)\r
115                                                         file = cumul[4];\r
116 \r
117                                                 Server s = source.getServer();\r
118                                                 String nick = parts[3];\r
119                                                 try {\r
120                                                         File f;\r
121                                                         if (file != null)\r
122                                                                 f = new File(file);\r
123                                                         else\r
124                                                                 f = _ircConfiguration.getSecurityProvider().getLoadFile("Send file");\r
125 \r
126                                                         DCCFileHandler handler = new DCCFileHandler(_ircConfiguration, nick, f);\r
127                                                         char guil = 34;\r
128                                                         String filename = f.getName();\r
129                                                         if (filename.indexOf(" ") != -1)\r
130                                                                 filename = guil + filename + guil;\r
131                                                         String arg = filename + " " + handler.send();\r
132                                                         send(s, nick, "DCC SEND " + arg);\r
133                                                         _mgr.newServer(handler, false);\r
134 \r
135                                                 } catch (Throwable ex) {\r
136                                                         _ircConfiguration.internalError("dcc send error", ex, "plouf@pjirc.com");\r
137                                                 }\r
138                                         } else {\r
139                                                 source.report(getText(IRCTextProvider.INTERPRETOR_UNKNOWN_DCC, parts[2]));\r
140                                         }\r
141                                 } else if (parts[1].toLowerCase(java.util.Locale.ENGLISH).equals("raw")) {\r
142                                         test(cmd, parts, 3);\r
143                                         send(source.getServer(), parts[2], cumul[3]);\r
144                                         // _filter.genericSend(source.getServer(),parts[2],cumul[3]);\r
145                                 } else if (parts[1].toLowerCase(java.util.Locale.ENGLISH).equals("sound")) {\r
146                                         test(cmd, parts, 2);\r
147                                         send(source.getServer(), source.getName(), "SOUND " + parts[2]);\r
148                                 } else {\r
149                                         test(cmd, parts, 2);\r
150                                         send(source.getServer(), parts[2], parts[1]);\r
151                                         // _filter.genericSend(source.getServer(),parts[2],parts[1]);\r
152                                 }\r
153                         } else {\r
154                                 super.handleCommand(source, cmd, parts, cumul);\r
155                         }\r
156                 } catch (NotEnoughParametersException ex) {\r
157                         source.report(getText(IRCTextProvider.INTERPRETOR_INSUFFICIENT_PARAMETERS, ex.getMessage()));\r
158                 }\r
159         }\r
160 }\r