Exemple #1
0
func (pm *PluginManager) InitUtilsJSBridge() {
	bridge := &pmUtilsJSBridge{
		GetPage: func(call otto.FunctionCall) otto.Value {
			var err error
			switch {
			case len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString():
				if page, err := utils.GetPage(call.Argument(0).String()); err == nil {
					if val, err := pm.js.ToValue(page); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] GetPage errored: %s\n", err)
			case len(call.ArgumentList) == 3 && call.ArgumentList[0].IsString() && call.ArgumentList[1].IsString() && call.ArgumentList[2].IsString():
				if page, err := utils.GetPageWithAuth(call.Argument(0).String(), call.Argument(1).String(), call.Argument(2).String()); err == nil {
					if val, err := pm.js.ToValue(page); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] GetPageWithAuth errored: %s\n", err)
			}
			return otto.FalseValue()
		},
		ExtractURL: func(call otto.FunctionCall) otto.Value {
			var err error
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				if url, err := utils.ExtractURL(call.Argument(0).String()); err == nil {
					if val, err := pm.js.ToValue(url); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] ExtractURL errored: %s\n", err)
			}
			return otto.FalseValue()
		},
		ExtractTitle: func(call otto.FunctionCall) otto.Value {
			var err error
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsString() {
				if title, err := utils.ExtractTitle(call.Argument(0).String()); err == nil {
					if val, err := pm.js.ToValue(title); err == nil {
						return val
					}
				}
				pm.log.Printf("[UTILS] ExtractTitle errored: %s\n", err)
			}
			return otto.FalseValue()
		},
		Sleep: func(call otto.FunctionCall) otto.Value {
			var err error
			if len(call.ArgumentList) == 1 && call.ArgumentList[0].IsNumber() {
				if i, err := call.Argument(0).ToInteger(); err == nil {
					utils.Sleep(i)
					return otto.TrueValue()
				}
				pm.log.Printf("[UTILS] Sleep errored: %s\n", err)
			}
			return otto.FalseValue()
		},
	}
	pm.js.Set("UTILS", bridge)
}
Exemple #2
0
func (pm *PluginManager) ImportPlugin(who, url, name string, overwrite bool) (err error) {
	pm.log.Printf("%s is importing a plugin from: %s, called %s.js. Overwrite is %v\n", who, url, name, overwrite)
	var plugin string
	if plugin, err = utils.GetPage(url); err == nil {
		js := otto.New()
		js.Set("RegisterCommand", func(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() })
		js.Set("RegisterCallback", func(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() })
		js.Set("log", func(call otto.FunctionCall) otto.Value { return otto.UndefinedValue() })
		_, err = js.Run(plugin)
		if err == nil {
			js = nil
			_, err = os.Stat(pm.cfg.Irc.PluginsDir)
			if err == nil {
				file := filepath.Join(pm.cfg.Irc.PluginsDir, name+".js")
				_, err = os.Stat(file)
				if err != nil && os.IsNotExist(err) {
					// Write plugin to file
					err = ioutil.WriteFile(file, []byte(plugin), 0644)
				} else if err == nil && overwrite {
					err = ioutil.WriteFile(file, []byte(plugin), 0644)
				} else if err == nil {
					err = errors.New("Will not overwrite existing plugin")
				}
			} else if os.IsNotExist(err) {
				pm.log.Printf("Plugin directory does not exist: %s\n", err)
			} else {
				pm.log.Printf("Error accessing plugin directory: %s\n", err)
			}
		}
	}
	if err != nil {
		pm.log.Printf("Error importing plugin: %s\n", err)
		return
	}
	pm.InitJS()
	return nil
}