Esempio n. 1
0
func Publishers(publishMsgs <-chan models.Message, config *models.Config) {
	log.Print("Initializing Publishers")
	var chans = make(map[string]chan models.Message)
	for _, connector := range config.Connectors {
		if connector.Active {
			chans[connector.ID] = make(chan models.Message)
			c := connectors.MakeConnector(connector.Type).(connectors.Connector)
			go c.Publish(chans[connector.ID], connector)
		}
	}
	for {
		message := <-publishMsgs
		for _, route := range config.Routes {
			match := true
			if _, chk := chans[route.Publish.ConnectorID]; !chk {
				match = false
			}
			matchRouteFull(&match, message.Out.Text+" "+message.Out.Detail, route.Match.Message)
			matchRouteSimple(&match, message.In.ConnectorType, route.Match.ConnectorType)
			matchRouteSimple(&match, message.In.ConnectorID, route.Match.ConnectorID)
			matchRouteTags(&match, message.In.Tags, route.Match.Tags)
			matchRouteSimple(&match, message.In.Target, route.Match.Target)
			matchRouteSimple(&match, message.In.User, route.Match.User)
			if match {
				message.Out.Target = route.Publish.Target
				chans[route.Publish.ConnectorID] <- message
			}
		}
	}
}
Esempio n. 2
0
func Commands(commandMsgs <-chan models.Message, publishMsgs chan<- models.Message, config *models.Config) {
	for {
		message := <-commandMsgs
		aliasCommands(&message, config)
		messages := splitCommands(message)
		for _, m := range messages {
			if m.In.Process {
				aliasCommands(&m, config)
				staticCommands(m, publishMsgs, config)
				for _, connector := range config.Connectors {
					if connector.Active {
						canRun := false
						if connector.Users == "" || connector.Users == "*" {
							canRun = true
						} else {
							users := strings.Split(connector.Users, ",")
							for _, u := range users {
								if u == m.In.User || u == m.In.Target {
									canRun = true
								}
							}
						}
						if canRun {
							c := connectors.MakeConnector(connector.Type).(connectors.Connector)
							go c.Command(m, publishMsgs, connector)
						}
					}
				}
			} else {
				publishMsgs <- m
			}
		}
	}
}
Esempio n. 3
0
func Help(message models.Message, publishMsgs chan<- models.Message, config *models.Config) {
	help := make([]string, 0)
	for _, alias := range config.Aliases {
		if !alias.HideHelp {
			if alias.Help != "" {
				help = append(help, alias.Help)
			} else {
				help = append(help, alias.Match)
			}
		}
	}
	for _, connector := range config.Connectors {
		if connector.Active {
			canRun := false
			if connector.Users == "" || connector.Users == "*" {
				canRun = true
			} else {
				users := strings.Split(connector.Users, ",")
				for _, u := range users {
					if u == message.In.User {
						canRun = true
					}
				}
			}
			if canRun {
				c := connectors.MakeConnector(connector.Type).(connectors.Connector)
				help = append(help, c.Help(connector)...)
			}
		}
	}
	sort.Strings(help)
	var lasthelp = ""
	var newhelp = make([]string, 0)
	for _, h := range help {
		if h != lasthelp {
			newhelp = append(newhelp, h)
		}
		lasthelp = h
	}
	if len(newhelp) > 0 {
		message.Out.Text = "Help for " + config.BotName + "..."
		message.Out.Detail = strings.Join(newhelp, "\n")
		publishMsgs <- message

	}
}