예제 #1
0
func Fuzz(data []byte) int {
	c := parser.ParseLine(string(data))
	if c == nil {
		return 0
	}
	return 1
}
예제 #2
0
파일: client.go 프로젝트: rburchell/gobo
func (this *IrcClient) Run(host string) {
	var scanner *bufio.Scanner
	var reconnDelay int

	pingControlChan := make(chan int)
	defer func() {
		// Shut down pinger
		pingControlChan <- 0
	}()

	go pinger(this, pingControlChan)

	for {
		for this.conn == nil {
			var err error
			if this.conn == nil {
				this.connected = false
				if reconnDelay > 0 {
					if reconnDelay > 60 {
						reconnDelay = 60
					}

					fmt.Printf("Delaying reconnection attempt by %d seconds\n", reconnDelay)
					timer := time.NewTimer(time.Second * time.Duration(reconnDelay))
					<-timer.C
				}

				this.conn, err = net.DialTimeout("tcp", host, time.Second*5)

				if err != nil {
					// if we're having trouble connecting at all, the problem is
					// likely not a transient one (e.g. catestrophic server
					// failure, DNS failure, ...) so increase the timeout
					// quicker.
					println("Error connecting: " + err.Error())
					reconnDelay += 2
				} else {
					// TODO: handle 443:
					// :weber.freenode.net 433 * qt_gerrit :Nickname is already in use.
					this.WriteLine(fmt.Sprintf("NICK %s", this.nick))
					this.WriteLine(fmt.Sprintf("USER %s * * :%s", this.user, this.realname))
					scanner = bufio.NewScanner(this.conn)
				}
			}
		}

		this.conn.SetReadDeadline(time.Now().Add(60 * 5 * time.Second))
		ret := scanner.Scan()
		if ret == false {
			if scanner.Err() != nil {
				println("Error reading line: " + scanner.Err().Error())
			} else {
				println("Error reading line: EOF")
			}
			reconnDelay += 1
			scanner = nil
			this.conn = nil
			continue
		}

		buffer := scanner.Text()
		bufstring := string(buffer)
		println("IN: ", bufstring)
		command := parser.ParseLine(bufstring)

		switch command.Command {
		case "PING":
			this.WriteLine(fmt.Sprintf("PONG :%s", command.Parameters[0]))
		case "ERROR":
			// something is probably very wrong (e.g. a ban/kill)
			// wait a while longer to reconnect because of this
			reconnDelay += 8
		case OnConnected:
			// only reset delay on a full, successful connection. if we're
			// banned, we'll successfully establish a socket connection, but
			// there's no sense in hammering the server with reconnect attempts.
			reconnDelay = 0
			this.handleConnected()
		case OnKick:
			for _, channel := range this.irc_channels {
				if channel == command.Parameters[0] {
					if command.Parameters[1] == this.nick {
						this.WriteLine(fmt.Sprintf("JOIN %s", channel))
					}
					break
				}
			}
		}

		this.CommandChannel <- command
	}
}