Run operator shell commands in a chat's directory on the server
chat_shell_run starts $SHELL -lc in the chat session's directory in its own process group, streams merged output to every watcher of the session, keeps a 1 MiB tail and persists each run; chat_shell_stop interrupts the group and kills it after a grace period. chat_shell_share queues an ordinary prompt with the command and a bounded output tail.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// chatShellOutput is the bounded tail of one operator command's merged output.
|
||||
// Offsets are absolute bytes of everything the command wrote, so a client can
|
||||
// tell a continuation from a gap. The buffer may grow to twice its limit before
|
||||
// compacting, which keeps a flooding command from copying the tail per write.
|
||||
type chatShellOutput struct {
|
||||
limit int
|
||||
data []byte
|
||||
start int64
|
||||
total int64
|
||||
emitted int64
|
||||
}
|
||||
|
||||
func newChatShellOutput(limit int) *chatShellOutput {
|
||||
return &chatShellOutput{limit: limit}
|
||||
}
|
||||
|
||||
func (o *chatShellOutput) write(p []byte) {
|
||||
o.data = append(o.data, p...)
|
||||
o.total += int64(len(p))
|
||||
if len(o.data) <= 2*o.limit {
|
||||
return
|
||||
}
|
||||
cut := runeStartAfter(o.data, len(o.data)-o.limit)
|
||||
o.data = append([]byte(nil), o.data[cut:]...)
|
||||
o.start += int64(cut)
|
||||
}
|
||||
|
||||
// tail returns at most max retained bytes ending at the newest byte, starting
|
||||
// on a character boundary, with the absolute offset of its first byte.
|
||||
func (o *chatShellOutput) tail(max int) (string, int64) {
|
||||
from := 0
|
||||
if len(o.data) > max {
|
||||
from = runeStartAfter(o.data, len(o.data)-max)
|
||||
}
|
||||
return validChatShellText(o.data[from:]), o.start + int64(from)
|
||||
}
|
||||
|
||||
// pending returns the output not yet emitted, at most max bytes (the newest
|
||||
// ones when more is waiting: the skipped bytes are a visible gap to clients).
|
||||
// An incomplete trailing character is held back unless final is set.
|
||||
func (o *chatShellOutput) pending(max int, final bool) (text string, offset, end int64, ok bool) {
|
||||
from := o.emitted
|
||||
if from < o.start {
|
||||
from = o.start
|
||||
}
|
||||
available := o.data[from-o.start:]
|
||||
if !final {
|
||||
available = available[:completeUTF8Prefix(available)]
|
||||
}
|
||||
if len(available) == 0 {
|
||||
return "", 0, 0, false
|
||||
}
|
||||
end = from + int64(len(available))
|
||||
if len(available) > max {
|
||||
skip := runeStartAfter(available, len(available)-max)
|
||||
available = available[skip:]
|
||||
from += int64(skip)
|
||||
}
|
||||
o.emitted = end
|
||||
return validChatShellText(available), from, end, true
|
||||
}
|
||||
|
||||
// runeStartAfter moves index forward (at most three bytes) to the first byte
|
||||
// that starts a character, so a cut never splits one.
|
||||
func runeStartAfter(data []byte, index int) int {
|
||||
for step := 0; step < utf8.UTFMax-1 && index < len(data) && !utf8.RuneStart(data[index]); step++ {
|
||||
index++
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
// completeUTF8Prefix is the length of data without a trailing partial
|
||||
// character that a later read may complete.
|
||||
func completeUTF8Prefix(data []byte) int {
|
||||
for back := 1; back <= utf8.UTFMax-1 && back <= len(data); back++ {
|
||||
if utf8.RuneStart(data[len(data)-back]) {
|
||||
if !utf8.FullRune(data[len(data)-back:]) {
|
||||
return len(data) - back
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return len(data)
|
||||
}
|
||||
|
||||
// validChatShellText keeps binary output representable in a JSON frame.
|
||||
func validChatShellText(data []byte) string {
|
||||
return strings.ToValidUTF8(string(data), "�")
|
||||
}
|
||||
Reference in New Issue
Block a user