Skip to content

Commit

Permalink
add scrollback config option
Browse files Browse the repository at this point in the history
  • Loading branch information
quackduck committed Jan 5, 2023
1 parent f6c21e2 commit 00160b8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
4 changes: 4 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type ConfigType struct {
Port int `yaml:"port"`
AltPort int `yaml:"alt_port"`
ProfilePort int `yaml:"profile_port"`
Scrollback int `yaml:"scrollback"`
DataDir string `yaml:"data_dir"`
KeyFile string `yaml:"key_file"`
Admins map[string]string `yaml:"admins"`
Expand Down Expand Up @@ -61,6 +62,7 @@ var (
Port: 2221,
AltPort: 8080,
ProfilePort: 5555,
Scrollback: 16,
DataDir: "devzat-data",
KeyFile: "devzat-sshkey",

Expand Down Expand Up @@ -113,6 +115,8 @@ func init() {
errCheck(err)
}

Backlog = make([]backlogMessage, Config.Scrollback)

if Config.IntegrationConfig != "" {
d, err = os.ReadFile(Config.IntegrationConfig)
errCheck(err)
Expand Down
31 changes: 15 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import (
)

var (
Scrollback = 16

MainRoom = &Room{"#main", make([]*User, 0, 10), sync.Mutex{}}
Rooms = map[string]*Room{MainRoom.name: MainRoom}
Backlog = make([]backlogMessage, 0, Scrollback)
Backlog []backlogMessage
Bans = make([]Ban, 0, 10)
IDsInMinToTimes = make(map[string]int, 10) // TODO: maybe add some IP-based factor to disallow rapid key-gen attempts
AntispamMessages = make(map[string]int)
Expand Down Expand Up @@ -259,10 +257,11 @@ func (r *Room) broadcastNoSlack(senderName, msg string) {
}
r.usersMutex.Unlock()
if r == MainRoom {
Backlog = Backlog[1:]
Backlog = append(Backlog, backlogMessage{time.Now(), senderName, msg + "\n"})
if len(Backlog) > Scrollback {
Backlog = Backlog[len(Backlog)-Scrollback:]
}
//if len(Backlog) > Config.Scrollback {
// Backlog = Backlog[len(Backlog)-Config.Scrollback:]
//}
}
}

Expand Down Expand Up @@ -368,7 +367,7 @@ func newUser(s ssh.Session) *User {
_, isAdmin := Config.Admins[u.id]
if !(isAdmin || isOnAllowlist) {
Log.Println("Rejected " + u.Name + " [" + u.id + "] (not on allowlist)")
u.writeln(Devbot, "You are not on the allowlist of this private server. If this is a mistake, send your id ("+u.id+") to the admin so that they can whitelist you.")
u.writeln(Devbot, "You are not on the allowlist of this private server. If this is a mistake, send your id ("+u.id+") to the admin so that they can add you.")
u.closeQuietly()
return nil
}
Expand Down Expand Up @@ -408,16 +407,16 @@ func newUser(s ssh.Session) *User {
}

if !Config.Private { // sensitive info might be shared on a private server
if len(Backlog) > 0 {
lastStamp := Backlog[0].timestamp
u.rWriteln(fmtTime(u, lastStamp))
for i := range Backlog {
if Backlog[i].timestamp.Sub(lastStamp) > time.Minute {
lastStamp = Backlog[i].timestamp
u.rWriteln(fmtTime(u, lastStamp))
}
u.writeln(Backlog[i].senderName, Backlog[i].text)
var lastStamp time.Time
for i := range Backlog {
if Backlog[i].text == "" { // skip empty entries
continue
}
if i == 0 || Backlog[i].timestamp.Sub(lastStamp) > time.Minute {
lastStamp = Backlog[i].timestamp
u.rWriteln(fmtTime(u, lastStamp))
}
u.writeln(Backlog[i].senderName, Backlog[i].text)
}
}

Expand Down

0 comments on commit 00160b8

Please sign in to comment.