Skip to content

Commit 3b8e644

Browse files
authored
Merge pull request #341 from shazow/term-bot
TERM=bot mode
2 parents 096b548 + a3f4186 commit 3b8e644

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

chat/message/user.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ func (u *User) render(m Message) string {
161161
switch m := m.(type) {
162162
case PublicMsg:
163163
if u == m.From() {
164+
if !cfg.Echo {
165+
return ""
166+
}
164167
out += m.RenderSelf(cfg)
165168
} else {
166169
out += m.RenderFor(cfg)
@@ -226,6 +229,7 @@ type UserConfig struct {
226229
Highlight *regexp.Regexp
227230
Bell bool
228231
Quiet bool
232+
Echo bool // Echo shows your own messages after sending, disabled for bots
229233
Timeformat *string
230234
Timezone *time.Location
231235
Theme *Theme
@@ -237,6 +241,7 @@ var DefaultUserConfig UserConfig
237241
func init() {
238242
DefaultUserConfig = UserConfig{
239243
Bell: true,
244+
Echo: true,
240245
Quiet: false,
241246
}
242247

host.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,20 @@ func (h *Host) isOp(conn sshd.Connection) bool {
9090

9191
// Connect a specific Terminal to this host and its room.
9292
func (h *Host) Connect(term *sshd.Terminal) {
93-
term.SetEnterClear(true) // We provide our own echo rendering
9493
id := NewIdentity(term.Conn)
9594
user := message.NewUserScreen(id, term)
9695
cfg := user.Config()
97-
cfg.Theme = &h.theme
96+
97+
apiMode := strings.ToLower(term.Term()) == "bot"
98+
99+
if apiMode {
100+
cfg.Theme = message.MonoTheme
101+
cfg.Echo = false
102+
} else {
103+
term.SetEnterClear(true) // We provide our own echo rendering
104+
cfg.Theme = &h.theme
105+
}
106+
98107
user.SetConfig(cfg)
99108

100109
// Load user config overrides from ENV
@@ -151,9 +160,11 @@ func (h *Host) Connect(term *sshd.Terminal) {
151160
}
152161

153162
// Successfully joined.
154-
term.SetPrompt(GetPrompt(user))
155-
term.AutoCompleteCallback = h.AutoCompleteFunction(user)
156-
user.SetHighlight(user.Name())
163+
if !apiMode {
164+
term.SetPrompt(GetPrompt(user))
165+
term.AutoCompleteCallback = h.AutoCompleteFunction(user)
166+
user.SetHighlight(user.Name())
167+
}
157168

158169
// Should the user be op'd on join?
159170
if h.isOp(term.Conn) {
@@ -190,15 +201,22 @@ func (h *Host) Connect(term *sshd.Terminal) {
190201

191202
m := message.ParseInput(line, user)
192203

193-
if m, ok := m.(*message.CommandMsg); ok {
194-
// Other messages render themselves by the room, commands we'll
195-
// have to re-echo ourselves manually.
196-
user.HandleMsg(m)
204+
if !apiMode {
205+
if m, ok := m.(*message.CommandMsg); ok {
206+
// Other messages render themselves by the room, commands we'll
207+
// have to re-echo ourselves manually.
208+
user.HandleMsg(m)
209+
}
197210
}
198211

199212
// FIXME: Any reason to use h.room.Send(m) instead?
200213
h.HandleMsg(m)
201214

215+
if apiMode {
216+
// Skip the remaining rendering workarounds
217+
continue
218+
}
219+
202220
cmd := m.Command()
203221
if cmd == "/nick" || cmd == "/theme" {
204222
// Hijack /nick command to update terminal synchronously. Wouldn't

sshd/terminal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func NewTerminal(conn *ssh.ServerConn, ch ssh.NewChannel) (*Terminal, error) {
103103
return nil, err
104104
}
105105
term := Terminal{
106-
Terminal: *terminal.NewTerminal(channel, "Connecting..."),
106+
Terminal: *terminal.NewTerminal(channel, ""),
107107
Conn: sshConn{conn},
108108
Channel: channel,
109109

0 commit comments

Comments
 (0)