Ejemplo n.º 1
0
Archivo: main.go Proyecto: Oooska/irc
//readInput continuously reads line from stdin.
func readInput(client irc.Client) {
	reader := bufio.NewReader(os.Stdin)
	for {
		line, err := reader.ReadString('\n')
		if err != nil {
			log.Fatalf("Cannot read from stdin: %s", err.Error())
		}

		line = strings.TrimSpace(line)
		if len(line) == 0 {
			continue
		}

		msg, err := parseLine(line)
		if err != nil {
			log.Printf("Err: %s\n", err.Error())
		} else {
			client.Send(msg)
		}
	}
}
Ejemplo n.º 2
0
//Start connects to the IRC server, authenticates, and then starts a goroutine to manage the chat
func (c *ircchat) Start() error {
	var err error
	if !c.running {

		saveHandler := func(msg irc.Message) {
			cmd := msg.Command()

			//Check to make sure it's a message worth saving
			if cmd == "PRIVMSG" || cmd == "ACTION" {
				err := persistenceInstance.saveMessage(c.account, msg)
				if err != nil {
					log.Printf("Error saving message: %s", err.Error())
				}
			}
		}

		var client irc.Client
		client, err = irc.NewClient(fmt.Sprintf("%s:%d", c.settings.Address, c.settings.Port), c.settings.SSL)
		if err != nil {
			log.Printf("Error starting chat for %s: %s", c.account.Username(), err.Error())
			return err
		}
		client.AddHandler(irc.Both, saveHandler)
		c.client = client
		err = client.Write(irc.UserMessage(c.account.Username(), "ircwebchathost", "somewhere", "quack"))

		login := c.settings.Login
		if err == nil && login.Nick != "" {
			err = client.Write(irc.NickMessage(c.settings.Login.Nick))
			if err == nil && login.Password != "" {
				err = client.Write(irc.PrivMessage("NickServ", "identify "+login.Password))
			}
		}
		//TODO: Auto join rooms

		if err == nil {
			c.running = true
			go ircManager(*c)
		} else {
			c.Stop()
		}
	}
	return err
}