瀏覽代碼

Got plugins working. Added test plugin.

Nathaniel van Diepen 10 年之前
父節點
當前提交
5d20c78992
共有 4 個文件被更改,包括 28 次插入7 次删除
  1. 7 0
      app/api/plugins.js
  2. 5 0
      app/www/data/plugins/test/script.js
  3. 1 0
      app/www/index.html
  4. 15 7
      app/www/js/omnomirc.js

+ 7 - 0
app/api/plugins.js

@@ -0,0 +1,7 @@
+var files = fs.readdirSync('./www/data/plugins/');
+log('(function($o){');
+for(var i in files){
+	log('$o.register.plugin("'+files[i]+'");');
+	log('$o.plugin.start("'+files[i]+'");');
+}
+log('})(OmnomIRC);');

+ 5 - 0
app/www/data/plugins/test/script.js

@@ -0,0 +1,5 @@
+hook('message',function(msg,from,room){
+	if(msg =='funny'){
+		$o.chat.send('Not funny',room);
+	}
+});

+ 1 - 0
app/www/index.html

@@ -17,6 +17,7 @@
 		<script type="text/javascript" src="socket.io/socket.io.js"></script>
 		<script type="text/javascript" src="js/omnomirc.js"></script>
 		<script type="text/javascript" src="api/themes.js"></script>
+		<script type="text/javascript" src="api/plugins.js"></script>
 		<script id="theme-script"></script>
 		<link rel="stylesheet" href="css/jquery.contextMenu.css"></link>
 		<link id="theme-style"></link>

+ 15 - 7
app/www/js/omnomirc.js

@@ -336,7 +336,7 @@
 			for(i in hooks){
 				hook = hooks[i];
 				if(hook.hook == name){
-					r = runInSandbox(hook.fn,sandbox,args);
+					r = runInSandbox(hook.fn,sandbox,args,true);
 				}
 				if(r == false){
 					break;
@@ -344,11 +344,14 @@
 			}
 			return r;
 		},
-		runInSandbox = function(fn,sandbox,args){
+		runInSandbox = function(fn,sandbox,args,isFn){
 			args = exists(args)?args:[];
 			sandbox = sandbox instanceof Sandbox?sandbox:new Sandbox(sandbox);
 			var r = false;
 			fn = (fn+'').replace(/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g,'').replace(/\"/g,'\\"').replace(/\n/g,'').replace(/\r/g,'');
+			if(!exists(isFn) || !isFn){
+				fn = 'function(){'+fn+'}';
+			}
 			fn = 'var ret = true;eval("with(this){ret = ('+fn+').apply(this,arguments);}");return ret;';
 			try{
 				r = (new Function(fn)).apply(sandbox,args);
@@ -414,11 +417,11 @@
 			},
 			plugin: function(name){
 				if(!exists(plugins[name])){
-					plugins[name]({
+					plugins[name] = {
 						name: name,
 						loaded: false,
 						started: false
-					});
+					};
 					return true;
 				}
 				return false;
@@ -461,17 +464,18 @@
 					if(plugin.started){
 						$o.plugin.stop(name);
 					}
+					event('Starting plugin '+name);
 					if(!plugin.loaded){
-						$.ajax('',{
+						$.ajax('data/plugins/'+name+'/script.js',{
 							dataType: 'text',
 							success: function(data){
 								plugin.script = data;
-								runWithSandbox(data,pluginSandbox);
+								runInSandbox(data,pluginSandbox);
 								plugin.started = true;
 							}
 						});
 					}else{
-						runWithSandbox(plugin.script,pluginSandbox);
+						runInSandbox(plugin.script,pluginSandbox);
 						plugin.started = true;
 					}
 					return true;
@@ -481,6 +485,7 @@
 			},
 			stop: function(name){
 				if(exists(plugins[name])){
+					event('Stopping plugin '+name);
 					var i;
 					for(i in hooks){
 						if(hooks[i].type == 'plugin' && hooks[i].plugin == name){
@@ -502,6 +507,9 @@
 				}else{
 					return false;
 				}
+			},
+			dir: function(name){
+				return 'data/plugins/'+name+'/';
 			}
 		},
 		hook: function(event,fn){