Exemplo n.º 1
0
func Handle(c *sirc.IConn, m *irc.Message) (abort bool) {
	matches := handleRE.FindStringSubmatch(m.Trailing)
	if len(matches) == 0 {
		return
	}

	factoidkey := strings.ToLower(matches[1])

	state.Lock()
	defer state.Unlock()
	if factoid, factoidkey, ok := getfactoidByKey(factoidkey); ok {
		abort = true
		if factoidUsedRecently(factoidkey) {
			return
		}
		if len(matches[2]) > 0 { // someone is being sent a factoid
			c.PrivMsg(m, matches[2], ": ", factoid)
		} else { // otherwise just print the factoid
			c.PrivMsg(m, factoid)
		}

		return
	}

	return
}
Exemplo n.º 2
0
func handleAdmin(c *sirc.IConn, m *irc.Message) bool {
	matches := adminRE.FindStringSubmatch(m.Trailing)
	if len(matches) == 0 {
		return false
	}
	adminState.Lock()
	// lifo defer order
	defer adminState.Save()
	defer adminState.Unlock()

	host := strings.TrimSpace(matches[2])
	switch matches[1] {
	case "addadmin":
		admins[host] = struct{}{}
		c.Notice(m, "Added host successfully")
	case "deladmin":
		delete(admins, host)
		c.Notice(m, "Removed host successfully")
	case "raw":
		nm := irc.ParseMessage(matches[2])
		if nm == nil {
			c.Notice(m, "Could not parse, are you sure you know the irc protocol?")
		} else {
			go c.Write(nm)
		}
	}

	return true
}
Exemplo n.º 3
0
func writeLines(c *sirc.IConn, m *irc.Message, lines []string) {
	l := len(lines)
	t := c.Target(m)

	if l == 0 {
		return
	}

	if l > 5 {
		lines = lines[:5]
	}

	for _, l := range lines {
		c.Write(&irc.Message{
			Command:  irc.PRIVMSG,
			Params:   []string{t},
			Trailing: l,
		})
	}
}
Exemplo n.º 4
0
func handleIRC(ctx context.Context, c *sirc.IConn, m *irc.Message) bool {
	if m.Command == irc.RPL_WELCOME {
		cfg := config.FromContext(ctx).IRC
		for _, ch := range cfg.Channels {
			c.Write(&irc.Message{Command: irc.JOIN, Params: []string{ch}})
		}

		return false
	}

	if m.Command != irc.PRIVMSG {
		return false
	}

	if factoids.Handle(c, m) == true {
		return true
	}
	if analyzer.Handle(c, m) == true {
		return true
	}

	if m.Prefix != nil && len(m.Prefix.Host) > 0 {
		adminState.Lock()
		_, admin := admins[m.Prefix.Host]
		adminState.Unlock()
		if !admin {
			return true
		}
		if factoids.HandleAdmin(c, m) {
			return true
		}

		if handleAdmin(c, m) {
			return true
		}
	}

	return false
}
Exemplo n.º 5
0
func HandleAdmin(c *sirc.IConn, m *irc.Message) (abort bool) {
	matches := adminRE.FindStringSubmatch(m.Trailing)
	if len(matches) == 0 {
		return
	}

	var savestate bool
	abort = true

	command := matches[1]
	factoidkey := strings.ToLower(matches[2])
	newfactoidkey := strings.ToLower(matches[3])
	factoid := matches[3]
	if len(matches[4]) > 0 {
		factoid = matches[3] + matches[4]
	}

	switch command {
	case "add":
		fallthrough
	case "mod":
		state.Lock()
		defer state.Unlock()

		s.Factoids[factoidkey] = factoid
		savestate = true
		c.Notice(m, "Added/Modified successfully")

	case "del":
		state.Lock()
		defer state.Unlock()

	restartdelete:
		if _, ok := s.Factoids[factoidkey]; ok {
			delete(s.Factoids, factoidkey)
			c.Notice(m, "Deleted successfully")
			// clean up the aliases too
			for k, v := range s.Aliases {
				if v == factoidkey {
					delete(s.Aliases, k)
				}
			}
		} else if factoidkey, ok = s.Aliases[factoidkey]; ok {
			c.Notice(m, "Found an alias, deleting the original factoid")
			goto restartdelete
		}

		savestate = true

	case "rename":
		if !alphaRE.MatchString(newfactoidkey) {
			return
		}
		state.Lock()
		defer state.Unlock()

		if _, ok := s.Factoids[newfactoidkey]; ok {
			c.Notice(m, "Renaming would overwrite, please delete first")
			return
		}
		if _, ok := s.Aliases[newfactoidkey]; ok {
			c.Notice(m, "Renaming would overwrite an alias, please delete first")
			return
		}
		if _, ok := s.Factoids[factoidkey]; ok {
			s.Factoids[newfactoidkey] = s.Factoids[factoidkey]
			delete(s.Factoids, factoidkey)
			// rename the aliases too
			for k, v := range s.Aliases {
				if v == factoidkey {
					s.Aliases[k] = newfactoidkey
				}
			}
			savestate = true
			c.Notice(m, "Renamed successfully")
		} else {
			c.Notice(m, "Not present")
		}

	case "addalias":
		fallthrough
	case "modalias":
		if !alphaRE.MatchString(newfactoidkey) {
			return
		}

		state.Lock()
		defer state.Unlock()

		// newfactoidkey is the factoid we are going to add an alias for
		// if itself is an alias, get the original factoid key, that is what
		// getfactoidByKey does
		_, newfactoidkey, ok := getfactoidByKey(newfactoidkey)
		if ok {
			s.Aliases[factoidkey] = newfactoidkey
			savestate = true
			c.Notice(m, "Added/Modified alias for ", newfactoidkey, " successfully")
		} else {
			c.Notice(m, "No factoid with name ", newfactoidkey, " found")
		}

	case "delalias":
		state.Lock()
		defer state.Unlock()

		if _, ok := s.Aliases[factoidkey]; ok {
			c.Notice(m, "Deleted alias successfully")
			delete(s.Aliases, factoidkey)
			savestate = true
		}

	default:
		abort = false
		return
	}

	if savestate {
		state.Save(false)
		tpl.invalidate()
	}

	return
}