]>
jfr.im git - irc/Ozafy/borknet_p10_irc_services.git/blob - core/CoreModControl.java
3 # BorkNet Services Core
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
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.
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.
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.
24 package borknet_services
. core
;
28 import borknet_services
. core
.*;
31 * Class to load configuration files.
32 * @author Ozafy - ozafy@borknet.org - http://www.borknet.org
34 public class CoreModControl
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
>();
40 private String bot
= "" ;
42 public CoreModControl ( Core C
, ArrayList
< String
> modulelist
)
45 bot
= C
. get_corenum ();
46 for ( int n
= 0 ; n
< modulelist
. size (); n
++)
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
};
61 C
. report ( "[CORE] Error opening module directory" );
64 // Create a new class loader with the directory
65 ClassLoader clsl
= new URLClassLoader ( urls
);
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
++)
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 ());
90 m
. setCmnds ( modcmds
, modcmdn
);
100 public void rehash ( String username
, String module
)
102 if ( modules
. containsKey ( module
. toLowerCase ()))
104 modules
. get ( module
. toLowerCase ()). hstop ();
105 modules
. remove ( module
. toLowerCase ());
106 load ( username
, module
);
110 C
. cmd_notice ( bot
, username
, module
+ " isn't loaded, so can't be rehashed." );
114 public void load ( String username
, String module
)
116 if (! modules
. containsKey ( module
. toLowerCase ()))
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
);
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
++)
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 ());
145 m
. setCmnds ( modcmds
, modcmdn
);
151 C
. report ( "Failed loading module \" " + module
+ " \" ." );
157 C
. cmd_notice ( bot
, username
, module
+ " is loaded, use the rehash command." );
161 public void unload ( String username
, String module
)
163 if ( modules
. containsKey ( module
. toLowerCase ()))
165 modules
. get ( module
. toLowerCase ()). stop ();
166 modules
. remove ( module
. toLowerCase ());
170 C
. cmd_notice ( bot
, username
, module
+ " isn't loaded." );
176 C
. report ( "Stopping modules..." );
177 Set
< String
> keys
= modules
. keySet ();
178 for ( String key
: keys
)
180 modules
. get ( key
). stop ();
183 C
. report ( "Modules stopped." );
186 private String
initialUpper ( String s
)
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
);
194 public void parse ( ArrayList
< String
> params
)
196 Set
< String
> keys
= modules
. keySet ();
197 for ( String key
: keys
)
199 modules
. get ( key
). parse ( params
);
205 Set
< String
> keys
= modules
. keySet ();
206 for ( String key
: keys
)
208 modules
. get ( key
). clean ();