12 "github.com/shazow/ssh-chat/set"
15 const messageBuffer = 5
16 const messageTimeout = 5 * time.Second
17 const reHighlight = `\b(%s)\b`
19 var ErrUserClosed = errors.New("user closed")
21 // User definition, implemented set Item interface and io.Writer
35 replyTo *User // Set when user gets a /msg, for replying.
38 func NewUser(identity Identifier) *User {
41 Config: DefaultUserConfig,
43 msg: make(chan Message, messageBuffer),
44 done: make(chan struct{}),
47 u.SetColorIdx(rand.Int())
52 func NewUserScreen(identity Identifier, screen io.WriteCloser) *User {
53 u := NewUser(identity)
59 // Rename the user with a new Identifier.
60 func (u *User) SetId(id string) {
61 u.Identifier.SetId(id)
62 u.SetColorIdx(rand.Int())
65 // ReplyTo returns the last user that messaged this user.
66 func (u *User) ReplyTo() *User {
72 // SetReplyTo sets the last user to message this user.
73 func (u *User) SetReplyTo(user *User) {
79 // ToggleQuietMode will toggle whether or not quiet mode is enabled
80 func (u *User) ToggleQuietMode() {
83 u.Config.Quiet = !u.Config.Quiet
86 // SetColorIdx will set the colorIdx to a specific value, primarily used for
88 func (u *User) SetColorIdx(idx int) {
92 // Block until user is closed
93 func (u *User) Wait() {
97 // Disconnect user, stop accepting messages
98 func (u *User) Close() {
99 u.closeOnce.Do(func() {
103 // close(u.msg) TODO: Close?
108 // Consume message buffer into the handler. Will block, should be called in a
110 func (u *User) Consume() {
115 case m, ok := <-u.msg:
124 // Consume one message and stop, mostly for testing
125 func (u *User) ConsumeOne() Message {
129 // Check if there are pending messages, used for testing
130 func (u *User) HasMessages() bool {
140 // SetHighlight sets the highlighting regular expression to match string.
141 func (u *User) SetHighlight(s string) error {
142 re, err := regexp.Compile(fmt.Sprintf(reHighlight, s))
147 u.Config.Highlight = re
152 func (u *User) render(m Message) string {
156 switch m := m.(type) {
158 return m.RenderFor(cfg) + Newline
160 u.SetReplyTo(m.From())
161 return m.Render(cfg.Theme) + Newline
163 return m.Render(cfg.Theme) + Newline
167 // HandleMsg will render the message to the screen, blocking.
168 func (u *User) HandleMsg(m Message) error {
170 _, err := u.screen.Write([]byte(r))
172 logger.Printf("Write failed to %s, closing: %s", u.Name(), err)
178 // Add message to consume by user
179 func (u *User) Send(m Message) error {
184 case <-time.After(messageTimeout):
185 logger.Printf("Message buffer full, closing: %s", u.Name())
192 func (u *User) Ignore(other Identifier) error {
194 return errors.New("user is nil.")
197 if other.Id() == u.Id() {
198 return errors.New("cannot ignore self.")
201 if u.Ignored.In(other.Id()) {
202 return errors.New("user already ignored.")
205 u.Ignored.Add(set.Itemize(other.Id(), other))
209 func (u *User) Unignore(id string) error {
211 return errors.New("user is nil.")
214 return u.Ignored.Remove(id)
217 // Container for per-user configurations.
218 type UserConfig struct {
219 Highlight *regexp.Regexp
225 // Default user configuration to use
226 var DefaultUserConfig UserConfig
229 DefaultUserConfig = UserConfig{
234 // TODO: Seed random?