Exemplo n.º 1
0
func bindEvents(mgr *IrcConnectionManager, e event.EventManager) {
	mgr.conn.AddCallback("*", func(ev *ircevent.Event) {
		e.Trigger(IrcEvent, newEventData(ev))
		e.Trigger(event.EventType("irc."+ev.Code), newEventData(ev))
	})

	mgr.conn.AddCallback("001", func(ev *ircevent.Event) {
		fmt.Println("Connected")
		mgr.status = Connected
		e.Trigger(ConnectEvent, newEventData(ev))
	})

	mgr.conn.AddCallback("ERROR", func(ev *ircevent.Event) {
		if mgr.status != Disconnected {
			mgr.Quit()
		}
		e.Trigger(DisconnectEvent, newEventData(ev))
	})
}
Exemplo n.º 2
0
func newJavascriptVm(m *ScriptManager) *jsVm {
	jsVm := &jsVm{otto.New(), make(map[*timer]*timer), make(chan *timer)}

	getFnName := func(fn otto.Value) (name string) {
		if fn.IsFunction() {
			name = fmt.Sprintf("__Handler%x", sha1.Sum([]byte(fn.String())))
		} else {
			name = fn.String()
		}

		return
	}
	newTimer := func(call otto.FunctionCall, repeat bool) (*timer, otto.Value, error) {
		delay, _ := call.Argument(1).ToInteger()
		if delay <= 0 {
			delay = 1
		}

		res := &timer{nil, time.Duration(delay) * time.Millisecond, repeat, call}
		jsVm.registry[res] = res

		res.t = time.AfterFunc(res.dur, func() {
			jsVm.ready <- res
		})

		val, err := jsVm.ToValue(res)
		if err != nil {
			return nil, otto.UndefinedValue(), err
		}

		return res, val, nil
	}
	clearTimer := func(call otto.FunctionCall) otto.Value {
		ti, _ := call.Argument(0).Export()
		if ti, ok := ti.(*timer); ok {
			ti.t.Stop()
			delete(jsVm.registry, ti)
		}
		return otto.UndefinedValue()
	}

	jsVm.Set("setTimeout", func(call otto.FunctionCall) otto.Value {
		_, v, err := newTimer(call, false)
		if err != nil {
			return otto.UndefinedValue()
		}
		return v
	})
	jsVm.Set("setInterval", func(call otto.FunctionCall) otto.Value {
		_, v, err := newTimer(call, true)
		if err != nil {
			return otto.UndefinedValue()
		}
		return v
	})
	jsVm.Set("clearTimeout", clearTimer)
	jsVm.Set("clearInterval", clearTimer)
	jsVm.Set("Http", &m.httpHelper)
	jsVm.Set("Config", &m.configHelper)
	jsVm.Set("Data", &m.dataHelper)
	jsVm.Set("Irc", &m.ircHelper)
	jsVm.Set("Os", &m.osHelper)
	jsVm.Set("bind", func(call otto.FunctionCall) otto.Value {
		eventType := call.Argument(0).String()
		fn := call.Argument(1)
		fnName := getFnName(fn)
		if fn.IsFunction() {
			m.jsDriver.vm.Set(fnName, func(call otto.FunctionCall) otto.Value {
				fn.Call(call.This, call.ArgumentList)
				return otto.UndefinedValue()
			})
		}
		m.scriptHelper.Bind(Javascript, event.EventType(eventType), fnName)
		val, _ := otto.ToValue(fnName)
		return val
	})
	jsVm.Set("unbind", func(call otto.FunctionCall) otto.Value {
		eventType := call.Argument(0).String()
		fnName := getFnName(call.Argument(1))
		m.scriptHelper.Unbind(Javascript, event.EventType(eventType), fnName)
		return otto.UndefinedValue()
	})
	jsVm.Set("trigger", func(call otto.FunctionCall) otto.Value {
		eventType := call.Argument(0).String()
		data, _ := call.Argument(1).Export()
		if data == nil {
			data = make(map[string]interface{}, 0)
		}
		m.scriptHelper.Trigger(event.EventType(eventType), data.(map[string]interface{}))
		return otto.UndefinedValue()
	})
	jsVm.Set("use", func(call otto.FunctionCall) otto.Value {
		coll := call.Argument(0).String()

		// Todo: get the Database properly
		db := data.NewGenericRepository(m.repo.database, coll)
		obj, _ := jsVm.Object("({})")
		obj.Set("Save", func(call otto.FunctionCall) otto.Value {
			exp, _ := call.Argument(0).Export()
			var model data.GenericModel
			switch t := exp.(type) {
			case data.GenericModel:
				model = t

			case map[string]interface{}:
				model = data.GenericModel(t)
			}
			switch t := model["ID"].(type) {
			case int64:
				model["ID"] = int(t)

			case int:
				model["ID"] = t
			}
			db.Save(model)

			id, _ := jsVm.ToValue(model["ID"])

			return id
		})
		obj.Set("Fetch", func(call otto.FunctionCall) otto.Value {
			i, _ := call.Argument(0).ToInteger()
			val := db.Fetch(int(i))
			v, err := jsVm.ToValue(val)

			if err != nil {
				panic(err)
			}

			return v
		})
		obj.Set("FetchAll", func(call otto.FunctionCall) otto.Value {
			vals := db.FetchAll()
			v, err := jsVm.ToValue(vals)

			if err != nil {
				panic(err)
			}

			return v
		})
		obj.Set("Index", func(call otto.FunctionCall) otto.Value {
			exp, _ := call.Argument(0).Export()
			cols := make([]string, 0)
			for _, val := range exp.([]interface{}) {
				cols = append(cols, val.(string))
			}
			db.Index(cols)

			return otto.UndefinedValue()
		})
		obj.Set("Query", func(call otto.FunctionCall) otto.Value {
			qry, _ := call.Argument(0).Export()
			vals := db.Query(qry)
			v, err := jsVm.ToValue(vals)

			if err != nil {
				panic(err)
			}

			return v
		})

		return obj.Value()
	})

	go jsVm.Loop()

	return jsVm
}