Example #1
0
File: hal.go Project: kcmerrill/hal
func MessageWorker(id int, msgs chan *message.Message) {
	for {
		/* Grab a message off the channel */
		m := <-msgs

		if user, err := users.Fetch(m.Signature); err == nil {
			/* Quick logging */
			m.From = user.At()

			/* Depending on the type of message, do something with it */
			switch m.Type() {
			case "channel":
				channel.Broadcast(m)
				continue
			case "command":
				if master_signature == m.Signature {
					command.Execute(m)
				} else {
					log.Error("Permission denied")
				}
				continue
			case "direct":
				if u, err := users.Fetch(m.To); err == nil {
					/* We now have a signature! Lets fetch the connection */
					if conn, conn_error := connection.Fetch(u.Signature); conn_error == nil {
						log.Info(m.From + "->" + u.At())
						conn.Write(m)
					} else {
						log.Error(conn_error.Error())
					}
				} else {
					log.Error(err.Error())
				}
			default:
				log.Error("Unknown message type: " + m.Type())
				continue
			}
			log.Debug(fmt.Sprintf("[WORKER#%d] [%s] "+user.At()+"->"+m.To, id, m.Type()))
		} else {
			log.Error("Unknown user with signature " + m.Signature)
		}

	}
}
Example #2
0
func (c *command) Execute() {
	cmd_split := strings.Split(string(c.cmd_raw[1:]), " ")
	for index, word := range cmd_split {
		switch index {
		case 0:
			c.cmd = word
		case 1:
			c.modifier = word
		case 2:
			c.param1 = word
		}
	}

	switch c.cmd {
	default:
		log.Error("Unknown command " + c.cmd)
	case "register":
		u := &users.Info{
			Username:  c.modifier,
			Signature: c.param1,
		}
		if user, err := users.Register(u); err == nil {
			log.Info(user.At() + " registered")
		} else {
			log.Error(err.Error())
		}
	case "leave":
		if user, err := users.Fetch(c.executor); err == nil {
			user.LeaveChannel(c.modifier)
			log.Info(user.At() + " left " + c.modifier)
		} else {
			log.Error("Unknown user " + c.executor)
		}
	case "join":
		if user, err := users.Fetch(c.executor); err == nil {
			user.JoinChannel(c.modifier, 1)
			log.Info(user.At() + " joined " + c.modifier)
		} else {
			log.Error("Unknown user " + c.executor)
		}
	}
}
Example #3
0
func Broadcast(m *message.Message) {
	for connection, _ := range connection.Connections() {
		if user, err := users.Fetch(connection.Signature); err == nil {
			/* User exists ... does it have this channel? */
			if user.HasChannel(m.To) {
				/* Oh snap! User has the channel .. Send them the message */
				connection.Write(m)
			}
		}
	}
}