]> jfr.im git - irc/Ozafy/borknet_p10_irc_services.git/blob - core/CoreModControl.java
First commit
[irc/Ozafy/borknet_p10_irc_services.git] / core / CoreModControl.java
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
30 /**
31 * Class to load configuration files.
32 * @author Ozafy - ozafy@borknet.org - http://www.borknet.org
33 */
34 public class CoreModControl
35 {
36 /*private ArrayList<Modules> modules = new ArrayList<Modules>();
37 private ArrayList<String> modlist = new ArrayList<String>();*/
38 private HashMap<String, Modules> modules = new HashMap<String,Modules>();
39 private Core C;
40 private String bot = "";
41
42 public CoreModControl(Core C, ArrayList<String> modulelist)
43 {
44 this.C = C;
45 bot = C.get_corenum();
46 for(int n=0; n<modulelist.size(); n++)
47 {
48 try
49 {
50 URL[] urls = null;
51 try
52 {
53 // Convert the file object to a URL
54 File dir = new File(System.getProperty("user.dir")+File.separator+"core"+File.separator+"modules"+File.separator+modulelist.get(n).toLowerCase()+File.separator);
55 URI uri = dir.toURI();
56 URL url = uri.toURL();
57 urls = new URL[]{url};
58 }
59 catch (Exception e)
60 {
61 C.report("[CORE] Error opening module directory");
62 C.debug(e);
63 }
64 // Create a new class loader with the directory
65 ClassLoader clsl = new URLClassLoader(urls);
66 // Load in the class
67 Class cls = clsl.loadClass(modulelist.get(n));
68 // Create a new instance of the new class
69 Modules m = (Modules) cls.newInstance();
70 modules.put(modulelist.get(n).toLowerCase(),m);
71 ArrayList<Object> modcmds = new ArrayList<Object>();
72 ArrayList<String> modcmdn = new ArrayList<String>();
73 CmdLoader cl = new CmdLoader("core/modules/"+modulelist.get(n).toLowerCase()+"/cmds");
74 String[] commandlist = cl.getVars();
75 for(int a=0; a<commandlist.length; a++)
76 {
77 try
78 {
79 // Load in the class
80 Class clss = clsl.loadClass(commandlist[a]);
81 // Create a new instance of the new class
82 modcmds.add(clss.newInstance());
83 modcmdn.add(commandlist[a].toLowerCase());
84 }
85 catch (Exception e)
86 {
87 C.debug(e);
88 }
89 }
90 m.setCmnds(modcmds,modcmdn);
91 m.start(C);
92 }
93 catch (Exception e)
94 {
95 C.debug(e);
96 }
97 }
98 }
99
100 public void rehash(String username, String module)
101 {
102 if(modules.containsKey(module.toLowerCase()))
103 {
104 modules.get(module.toLowerCase()).hstop();
105 modules.remove(module.toLowerCase());
106 load(username,module);
107 }
108 else
109 {
110 C.cmd_notice(bot,username, module + " isn't loaded, so can't be rehashed.");
111 }
112 }
113
114 public void load(String username, String module)
115 {
116 if(!modules.containsKey(module.toLowerCase()))
117 {
118 try
119 {
120 URL[] urls = null;
121 // Convert the file object to a URL
122 File dir = new File(System.getProperty("user.dir")+File.separator+"core"+File.separator+"modules"+File.separator+module.toLowerCase()+File.separator);
123 URI uri = dir.toURI();
124 URL url = uri.toURL();
125 urls = new URL[]{url};
126 // Create a new class loader with the directory
127 ClassLoader clsl = new URLClassLoader(urls);
128 // Load in the class
129 Class cls = clsl.loadClass(initialUpper(module));
130 // Create a new instance of the new class
131 Modules m = (Modules) cls.newInstance();
132 modules.put(module.toLowerCase(),m);
133 ArrayList<Object> modcmds = new ArrayList<Object>();
134 ArrayList<String> modcmdn = new ArrayList<String>();
135 CmdLoader cl = new CmdLoader("core/modules/"+module.toLowerCase()+"/cmds");
136 String[] commandlist = cl.getVars();
137 for(int a=0; a<commandlist.length; a++)
138 {
139 // Load in the class
140 Class clss = clsl.loadClass(commandlist[a]);
141 // Create a new instance of the new class
142 modcmds.add(clss.newInstance());
143 modcmdn.add(commandlist[a].toLowerCase());
144 }
145 m.setCmnds(modcmds,modcmdn);
146 m.start(C);
147 }
148 catch (Exception e)
149 {
150 C.debug(e);
151 C.report("Failed loading module \""+module+"\".");
152 return;
153 }
154 }
155 else
156 {
157 C.cmd_notice(bot,username, module + " is loaded, use the rehash command.");
158 }
159 }
160
161 public void unload(String username, String module)
162 {
163 if(modules.containsKey(module.toLowerCase()))
164 {
165 modules.get(module.toLowerCase()).stop();
166 modules.remove(module.toLowerCase());
167 }
168 else
169 {
170 C.cmd_notice(bot,username, module + " isn't loaded.");
171 }
172 }
173
174 public void stop()
175 {
176 C.report("Stopping modules...");
177 Set<String> keys = modules.keySet();
178 for(String key: keys)
179 {
180 modules.get(key).stop();
181 }
182 modules.clear();
183 C.report("Modules stopped.");
184 }
185
186 private String initialUpper(String s)
187 {
188 if(s.length() <= 1) return s.toUpperCase();
189 char[] letters = s.toCharArray();
190 letters[0] = Character.toUpperCase(letters[0]);
191 return new String(letters);
192 }
193
194 public void parse(ArrayList<String> params)
195 {
196 Set<String> keys = modules.keySet();
197 for(String key: keys)
198 {
199 modules.get(key).parse(params);
200 }
201 }
202
203 public void clean()
204 {
205 Set<String> keys = modules.keySet();
206 for(String key: keys)
207 {
208 modules.get(key).clean();
209 }
210 }
211 }