Ejemplo n.º 1
0
func setup(reg irc.HandlerRegistry) {
	reg.AddHandler("PRIVMSG", func(conn *irc.Conn, line irc.Line) {
		if len(line.Args) != 2 {
			// malformed line?
			return
		}
		dst := line.Args[0]
		text := line.Args[1]

		if strings.HasPrefix(text, CommandPrefix) &&
			len(text) > len(CommandPrefix) &&
			func() bool { r, _ := utf8.DecodeRuneInString(text[len(CommandPrefix):]); return unicode.IsLetter(r) }() {
			// this is a command
			words := strings.SplitN(text, " ", 2)
			cmd, arg := words[0][len(CommandPrefix):], ""
			if len(words) > 1 {
				arg = words[1]
			}
			reply, isPrivate := dst, false
			if !isChannelName(reply) {
				reply, isPrivate = line.Src.Nick, true
			}
			pluginReg.Dispatch("COMMAND", conn, line, cmd, arg, reply, isPrivate)
		} else if isChannelName(dst) {
			pluginReg.Dispatch("PRIVMSG", conn, line, dst, text)
		} else if dst == conn.Me().Nick {
			pluginReg.Dispatch("WHISPER", conn, line, text)
		} else {
			fmt.Println("Unknown destination on PRIVMSG:", line.Raw)
		}
	})
	reg.AddHandler(irc.ACTION, func(conn *irc.Conn, line irc.Line) {
		dst := line.Dst
		text := line.Args[0]
		isPrivate := !isChannelName(dst)
		pluginReg.Dispatch("ACTION", conn, line, dst, text, isPrivate)
	})
}
Ejemplo n.º 2
0
func newConnection(reg irc.HandlerRegistry) {
	reg.AddHandler("PART", func(conn *irc.Conn, line irc.Line) {
		if len(line.Args) < 1 {
			return
		}
		if line.Src.Nick == "" {
			return
		}
		dst := line.Args[0]
		if lines, ok := channels[dst]; ok {
			delete(lines, line.Src.Nick)
		}
	})
	reg.AddHandler("QUIT", func(conn *irc.Conn, line irc.Line) {
		if line.Src.Nick == "" {
			return
		}
		for _, lines := range channels {
			delete(lines, line.Src.Nick)
		}
	})
	reg.AddHandler("KICK", func(conn *irc.Conn, line irc.Line) {
		if len(line.Args) < 2 {
			return
		}
		dst := line.Args[0]
		nick := line.Args[1]
		if lines, ok := channels[dst]; ok {
			delete(lines, nick)
		}
	})
	reg.AddHandler("NICK", func(conn *irc.Conn, line irc.Line) {
		if len(line.Args) < 1 {
			return
		}
		src := line.Src.Nick
		nick := line.Args[0]
		for _, lines := range channels {
			if line, ok := lines[src]; ok {
				lines[nick] = line
				delete(lines, src)
			}
		}
	})
}