Example #1
0
File: main.go Project: Oooska/irc
//parseLine returns an irc.Message object. If the line starts with a forward
//slash, everything after the '/' is converted directly to a server command
//If there is no slash, the first word is taken to be the channel or user to
//send a PRIVMSG to
func parseLine(line string) (msg irc.Message, err error) {
	if line[0] == '/' {
		msg = irc.NewMessage(line[1:])
	} else {
		splitlines := strings.SplitN(line, " ", 2)
		if len(splitlines) > 1 {
			msg = irc.PrivMessage(splitlines[0], splitlines[1])
		} else {
			err = errors.New("Unable to parse input")
		}
	}
	return
}
Example #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
}