示例#1
0
// Send sends an IRC message to the server. If the message cannot be converted
// to a raw IRC line, an error is returned.
func (sc *ServerConnection) Send(tags *map[string]ircmsg.TagValue, prefix string, command string, params ...string) error {
	ircmsg := ircmsg.MakeMessage(tags, prefix, command, params...)
	line, err := ircmsg.Line()
	if err != nil {
		return err
	}
	fmt.Fprintf(sc.RawConnection, line)

	// dispatch raw event
	info := eventmgr.NewInfoMap()
	info["server"] = sc
	info["direction"] = "out"
	info["data"] = line
	sc.dispatchRawOut(info)

	// dispatch real event
	info = eventmgr.NewInfoMap()
	info["server"] = sc
	info["direction"] = "out"
	info["tags"] = tags
	info["prefix"] = prefix
	info["command"] = command
	info["params"] = params

	// IRC commands are case-insensitive
	sc.dispatchOut(strings.ToUpper(command), info)

	return nil
}
示例#2
0
// ProcessIncomingLine processes the incoming IRC line.
// This is used when writing a custom event loop.
func (sc *ServerConnection) ProcessIncomingLine(line string) {
	line = strings.Trim(line, "\r\n")

	// ignore empty lines
	if len(line) < 1 {
		return
	}

	// dispatch raw
	rawInfo := eventmgr.NewInfoMap()
	rawInfo["server"] = sc
	rawInfo["direction"] = "in"
	rawInfo["data"] = line

	sc.dispatchRawIn(rawInfo)

	// dispatch events
	message, err := ircmsg.ParseLine(line)

	// convert numerics to names
	cmd := message.Command
	num, err := strconv.Atoi(cmd)
	if err == nil {
		name, exists := Numerics[num]
		if exists {
			cmd = name
		}
	}

	info := eventmgr.NewInfoMap()
	info["server"] = sc
	info["direction"] = "in"
	info["tags"] = message.Tags
	info["prefix"] = message.Prefix
	info["command"] = cmd
	info["params"] = message.Params

	// simplify event
	if sc.SimplifyEvents {
		err = SimplifyEvent(info)

		if err != nil {
			fmt.Println("Could not simplify incoming IRC message, skipping line.")
			fmt.Println("line:", line)
			fmt.Println("error:", err)
			fmt.Println("info:", info)
			return
		}
	}

	// IRC commands are case-insensitive
	sc.dispatchIn(strings.ToUpper(cmd), info)
}
示例#3
0
// Disconnect closes the IRC socket.
// It is used when writing your own event loop.
func (sc *ServerConnection) Disconnect() {
	sc.Connected = false
	sc.RawConnection.Close()
	info := eventmgr.NewInfoMap()
	info["server"] = sc
	sc.dispatchOut("server disconnected", info)
}