Skip to content

Commit

Permalink
Send system notification with cooldown via notify-send
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcPer committed Jun 27, 2021
1 parent 2bfb65a commit 999a54d
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import (
"fmt"
"net"
"os"
"os/exec"
"strings"
"sync"
"time"
)

const ChatPort = 6776
const serverUser = "server"
const notifyCooldown = 20 * time.Second

var clientsLock sync.RWMutex
var notifyLock sync.Mutex

const (
fontReset = "\033[0m"
Expand All @@ -33,13 +36,15 @@ type peer struct {
clients []*peer
prompt string
promptDelete string
lastNotify time.Time
}

type packet struct {
sourceUsr string
sourceId string
destUsr string
msg string
hideNotif bool
}

func New(username string, server bool) peer {
Expand Down Expand Up @@ -178,6 +183,7 @@ func (p *peer) processQueues() {
continue
}
}
go p.notify(pk)
var color string
fmt.Print(p.promptDelete, strings.Repeat(" ", len(p.promptDelete)), p.promptDelete)
if pk.sourceUsr == serverUser {
Expand Down Expand Up @@ -253,13 +259,28 @@ FAIL_CMD:
}

func (p *peer) sendInfoMsg(destUsr string) {
p.inbox <- packet{sourceUsr: serverUser, destUsr: destUsr, msg: "Connected users:"}
p.inbox <- packet{sourceUsr: serverUser, destUsr: destUsr, msg: "- " + p.username}
p.inbox <- packet{hideNotif: true, sourceUsr: serverUser, destUsr: destUsr, msg: "Connected users:"}
p.inbox <- packet{hideNotif: true, sourceUsr: serverUser, destUsr: destUsr, msg: "- " + p.username}
clientsLock.RLock()
defer clientsLock.RUnlock()
for _, c := range p.clients {
if c.username != "" {
p.inbox <- packet{sourceUsr: serverUser, destUsr: destUsr, msg: "- " + c.username}
p.inbox <- packet{hideNotif: true, sourceUsr: serverUser, destUsr: destUsr, msg: "- " + c.username}
}
}
}

func (p *peer) notify(pk packet) {
if pk.msg == "" || pk.hideNotif {
return
}
notifyLock.Lock()
defer notifyLock.Unlock()
t := time.Now()
if t.Sub(p.lastNotify) > notifyCooldown {
p.lastNotify = t
cmd := fmt.Sprintf("command -v notify-send && notify-send 'lanchat: %v> %v'", pk.sourceUsr, pk.msg)
c := exec.Command("bash", "-c", cmd)
c.Run()
}
}

0 comments on commit 999a54d

Please sign in to comment.