func TestRoomDoesntBroadcastAnnounceMessagesWhenQuiet(t *testing.T) { u := message.NewUser(message.SimpleId("foo")) u.Config = message.UserConfig{ Quiet: true, } ch := NewRoom() defer ch.Close() _, err := ch.Join(u) if err != nil { t.Fatal(err) } // Drain the initial Join message <-ch.broadcast go func() { for msg := range u.ConsumeChan() { if _, ok := msg.(*message.AnnounceMsg); ok { t.Errorf("Got unexpected `%T`", msg) } } }() // Call with an AnnounceMsg and all the other types // and assert we received only non-announce messages ch.HandleMsg(message.NewAnnounceMsg("Ignored")) // Assert we still get all other types of messages ch.HandleMsg(message.NewEmoteMsg("hello", u)) ch.HandleMsg(message.NewSystemMsg("hello", u)) ch.HandleMsg(message.NewPrivateMsg("hello", u, u)) ch.HandleMsg(message.NewPublicMsg("hello", u)) }
// InitCommands injects default commands into a Commands registry. func InitCommands(c *Commands) { c.Add(Command{ Prefix: "/help", Handler: func(room *Room, msg message.CommandMsg) error { op := room.IsOp(msg.From()) room.Send(message.NewSystemMsg(room.commands.Help(op), msg.From())) return nil }, }) c.Add(Command{ Prefix: "/me", Handler: func(room *Room, msg message.CommandMsg) error { me := strings.TrimLeft(msg.Body(), "/me") if me == "" { me = "is at a loss for words." } else { me = me[1:] } room.Send(message.NewEmoteMsg(me, msg.From())) return nil }, }) c.Add(Command{ Prefix: "/exit", Help: "Exit the chat.", Handler: func(room *Room, msg message.CommandMsg) error { msg.From().Close() return nil }, }) c.Alias("/exit", "/quit") c.Add(Command{ Prefix: "/nick", PrefixHelp: "NAME", Help: "Rename yourself.", Handler: func(room *Room, msg message.CommandMsg) error { args := msg.Args() if len(args) != 1 { return ErrMissingArg } u := msg.From() member, ok := room.MemberById(u.Id()) if !ok { return errors.New("failed to find member") } oldId := member.Id() member.SetId(SanitizeName(args[0])) err := room.Rename(oldId, member) if err != nil { member.SetId(oldId) return err } return nil }, }) c.Add(Command{ Prefix: "/names", Help: "List users who are connected.", Handler: func(room *Room, msg message.CommandMsg) error { // TODO: colorize names := room.NamesPrefix("") body := fmt.Sprintf("%d connected: %s", len(names), strings.Join(names, ", ")) room.Send(message.NewSystemMsg(body, msg.From())) return nil }, }) c.Alias("/names", "/list") c.Add(Command{ Prefix: "/theme", PrefixHelp: "[mono|colors]", Help: "Set your color theme.", Handler: func(room *Room, msg message.CommandMsg) error { user := msg.From() args := msg.Args() if len(args) == 0 { theme := "plain" if user.Config.Theme != nil { theme = user.Config.Theme.Id() } body := fmt.Sprintf("Current theme: %s", theme) room.Send(message.NewSystemMsg(body, user)) return nil } id := args[0] for _, t := range message.Themes { if t.Id() == id { user.Config.Theme = &t body := fmt.Sprintf("Set theme: %s", id) room.Send(message.NewSystemMsg(body, user)) return nil } } return errors.New("theme not found") }, }) c.Add(Command{ Prefix: "/quiet", Help: "Silence room announcements.", Handler: func(room *Room, msg message.CommandMsg) error { u := msg.From() u.ToggleQuietMode() var body string if u.Config.Quiet { body = "Quiet mode is toggled ON" } else { body = "Quiet mode is toggled OFF" } room.Send(message.NewSystemMsg(body, u)) return nil }, }) c.Add(Command{ Prefix: "/slap", PrefixHelp: "NAME", Handler: func(room *Room, msg message.CommandMsg) error { var me string args := msg.Args() if len(args) == 0 { me = "slaps themselves around a bit with a large trout." } else { me = fmt.Sprintf("slaps %s around a bit with a large trout.", strings.Join(args, " ")) } room.Send(message.NewEmoteMsg(me, msg.From())) return nil }, }) }