Beispiel #1
0
func (i *Irc) SendMessage(channel, message string) {
	for len(message) > 0 {
		m := irc.Msg{
			Cmd:  "PRIVMSG",
			Args: []string{channel, message},
		}
		_, err := m.RawString()
		if err != nil {
			mtl := err.(irc.MsgTooLong)
			m.Args[1] = message[:mtl.NTrunc]
			message = message[mtl.NTrunc:]
		} else {
			message = ""
		}

		if throttle == nil {
			ratePerSec := i.config.RatePerSec
			throttle = time.Tick(time.Second / time.Duration(ratePerSec))
		}

		<-throttle

		i.Client.Out <- m
	}
}
Beispiel #2
0
Datei: win.go Projekt: aoeu/acme
func (w *win) send(t string) {
	if len(t) > 0 && t[len(t)-1] != '\n' {
		t = t + "\n"
	}
	if strings.HasPrefix(t, meCmd) {
		act := strings.TrimLeft(t[len(meCmd):], " \t")
		act = strings.TrimRight(act, "\n")
		if act == "\n" {
			t = "\n"
		} else {
			t = actionPrefix + " " + act + "\x01"
		}
	}

	msg := ""
	if w == serverWin {
		if msg = t; msg == "\n" {
			msg = ""
		}
	} else {
		msg = w.privMsgString(*nick, t)

		// Always tack on a newline.
		// In the case of a /me command, the
		// newline will be missing, it is added
		// here.
		if len(msg) > 0 && msg[len(msg)-1] != '\n' {
			msg = msg + "\n"
		}
	}
	w.writeData([]byte(msg + prompt))

	w.pAddr += utf8.RuneCountInString(msg)
	w.eAddr = w.pAddr + utf8.RuneCountInString(prompt)
	defer w.Addr("#%d", w.pAddr)

	if *debug {
		log.Printf("sent:\n\t[%s]\n\tnum runes=%d\n\tpAddr=%d\n\teAddr=%d\n\n",
			msg, utf8.RuneCountInString(msg), w.pAddr, w.eAddr)
	}

	if t == "\n" {
		return
	}
	if w == serverWin {
		t = strings.TrimLeft(t, " \t")
		if msg, err := irc.ParseMsg(t); err != nil {
			log.Println("Failed to parse message: " + err.Error())
		} else {
			client.Out <- msg
		}
	} else {
		for len(t) > 0 {
			m := irc.Msg{
				Cmd:  "PRIVMSG",
				Args: []string{w.target, t},
			}
			_, err := m.RawString()
			if err != nil {
				mtl := err.(irc.MsgTooLong)
				m.Args[1] = t[:mtl.NTrunc]
				t = t[mtl.NTrunc:]
			} else {
				t = ""
			}
			client.Out <- m
		}
	}
}