]>
Commit | Line | Data |
---|---|---|
431b229b LP |
1 | /** |
2 | # | |
3 | # BorkNet Services Core | |
4 | # | |
5 | ||
6 | # | |
7 | # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org | |
8 | # | |
9 | # This program is free software; you can redistribute it and/or | |
10 | # modify it under the terms of the GNU General Public License | |
11 | # as published by the Free Software Foundation; either version 2 | |
12 | # of the License, or (at your option) any later version. | |
13 | # | |
14 | # This program is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | # GNU General Public License for more details. | |
18 | # | |
19 | # You should have received a copy of the GNU General Public License | |
20 | # along with this program; if not, write to the Free Software | |
21 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | # | |
23 | */ | |
24 | package borknet_services.core; | |
25 | import java.util.*; | |
26 | import java.net.*; | |
27 | import java.io.*; | |
28 | import borknet_services.core.*; | |
29 | import borknet_services.core.commands.*; | |
30 | ||
31 | /** | |
32 | * The server communication class of the Q IRC Bot. | |
33 | * @author Ozafy - ozafy@borknet.org - http://www.borknet.org | |
34 | */ | |
35 | public class CoreCommands | |
36 | { | |
37 | private HashMap<String,Object> cmds = new HashMap<String,Object>(); | |
38 | private Core C; | |
39 | private String numeric = ""; | |
40 | private String bot = ""; | |
41 | public CoreCommands(Core C) | |
42 | { | |
43 | this.C = C; | |
44 | numeric = C.get_numeric(); | |
45 | bot = C.get_corenum(); | |
46 | URL[] urls = null; | |
47 | try | |
48 | { | |
49 | // Convert the file object to a URL | |
50 | File dir = new File(System.getProperty("user.dir")+File.separator+"core"+File.separator+"commands"+File.separator); | |
51 | URI uri = dir.toURI(); | |
52 | URL url = uri.toURL(); | |
53 | urls = new URL[]{url}; | |
54 | CmdLoader cl = new CmdLoader("core/cmds"); | |
55 | String[] commandlist = cl.getVars(); | |
56 | for(int n=0; n<commandlist.length; n++) | |
57 | { | |
58 | // Create a new class loader with the directory | |
59 | ClassLoader clsl = new URLClassLoader(urls); | |
60 | // Load in the class | |
61 | Class cls = clsl.loadClass(commandlist[n]); | |
62 | // Create a new instance of the new class | |
63 | cmds.put(commandlist[n].toLowerCase(),cls.newInstance()); | |
64 | } | |
65 | } | |
66 | catch (Exception e) | |
67 | { | |
68 | C.debug(e); | |
69 | System.exit(1); | |
70 | } | |
71 | } | |
72 | ||
73 | public void privmsg(ArrayList<String> params) | |
74 | { | |
75 | String source = params.get(0); | |
76 | String target = params.get(2); | |
77 | String message = params.get(3); | |
78 | if(!target.equals(numeric) && !target.equals(numeric+bot) && !target.equalsIgnoreCase(C.get_nick()+"@"+C.get_host())) return; | |
79 | CoreDBControl dbc = C.get_dbc(); | |
80 | User user = dbc.getUser(source); | |
81 | if(!C.isDeveloper(user.getAuth())) | |
82 | { | |
83 | return; | |
84 | } | |
85 | String command = ""; | |
86 | try | |
87 | { | |
88 | String[] result = message.split("\\s"); | |
89 | command = result[0].toLowerCase(); | |
90 | } | |
91 | catch(ArrayIndexOutOfBoundsException e) | |
92 | { | |
93 | command = message.toLowerCase(); | |
94 | } | |
95 | if(command.equals("help")) | |
96 | { | |
97 | String cmd = ""; | |
98 | try | |
99 | { | |
100 | String[] result = message.split("\\s"); | |
101 | cmd = result[1].toLowerCase(); | |
102 | } | |
103 | catch(ArrayIndexOutOfBoundsException e) | |
104 | { | |
105 | showCommands(source); | |
106 | return; | |
107 | } | |
108 | if(cmds.containsKey(cmd)) | |
109 | { | |
110 | Cmds ccommand = (Cmds) cmds.get(cmd); | |
111 | ccommand.parse_help(C,bot,source); | |
112 | } | |
113 | else | |
114 | { | |
115 | C.cmd_notice(bot,source,"This command is either unknown, or you need to be opered up to use it."); | |
116 | } | |
117 | return; | |
118 | } | |
119 | if(command.equals("showcommands")) | |
120 | { | |
121 | showCommands(source); | |
122 | return; | |
123 | } | |
124 | if(command.startsWith("\1")) | |
125 | { | |
126 | if(cmds.containsKey(command.replace("\1",""))) | |
127 | { | |
128 | Cmds ccommand = (Cmds) cmds.get(command.replace("\1","")); | |
129 | ccommand.parse_command(C,bot,target,source,message); | |
130 | } | |
131 | } | |
132 | else if(cmds.containsKey(command)) | |
133 | { | |
134 | Cmds ccommand = (Cmds) cmds.get(command); | |
135 | ccommand.parse_command(C,bot,target,source,message); | |
136 | } | |
137 | else | |
138 | { | |
139 | C.cmd_notice(bot,source,"This command is either unknown, or you need to be opered up to use it."); | |
140 | C.cmd_notice(bot,source,"/msg "+C.get_nick()+" showcommands"); | |
141 | } | |
142 | } | |
143 | private void showCommands(String username) | |
144 | { | |
145 | C.cmd_notice(bot,username,"The following commands are available to you:"); | |
146 | C.cmd_notice(bot,username,"For more information on a specific command, type HELP <command>:"); | |
147 | List<String> keys = new ArrayList<String>(cmds.keySet()); | |
148 | Collections.sort(keys); | |
149 | //Set<String> keys = cmds.keySet(); | |
150 | for(String key : keys) | |
151 | { | |
152 | Cmds ccommand = (Cmds) cmds.get(key); | |
153 | ccommand.showcommand(C,bot,username); | |
154 | } | |
155 | C.cmd_notice(bot,username,"End of list."); | |
156 | } | |
157 | } |