84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type EventType int
|
|
|
|
const (
|
|
eventHomeActivity = iota
|
|
eventLogin
|
|
eventLogout
|
|
)
|
|
|
|
// TODO may compute flavor externally based on type + username...
|
|
type Event struct {
|
|
Username string
|
|
Type EventType
|
|
Flavor string
|
|
}
|
|
|
|
func (e Event) String() string {
|
|
return e.Flavor
|
|
}
|
|
|
|
func NewLogger(w io.Writer) *log.Logger {
|
|
// TODO consider https://github.com/lestrrat-go/file-rotatelogs
|
|
return log.New(w, "", log.LstdFlags)
|
|
}
|
|
|
|
func cli(args []string) int {
|
|
// TODO less hardcoded
|
|
lf, err := os.OpenFile("bustle.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
if err != nil {
|
|
log.Printf("failed to open log file: %s\n", err)
|
|
return 2
|
|
}
|
|
logger := NewLogger(lf)
|
|
|
|
rawEvents := make(chan Event)
|
|
throttler := NewThrottler(time.Hour)
|
|
|
|
hw, err := NewHomeWatcher(rawEvents)
|
|
if err != nil {
|
|
log.Printf("failed to create hw: %s\n", err)
|
|
return 1
|
|
}
|
|
defer hw.Close()
|
|
|
|
// need a channel of raw events that can take home watcher events + eventual other events like
|
|
// logging in
|
|
// listener grabs each raw event and checks throttler
|
|
// if not throttled, event is written to log file
|
|
|
|
go hw.Watch()
|
|
for {
|
|
// TODO prob use a select with an error channel, like fsnotify does
|
|
event, ok := <-rawEvents
|
|
if !ok {
|
|
log.Println("event error")
|
|
break
|
|
}
|
|
if throttler.Throttled(event.Username) {
|
|
continue
|
|
}
|
|
throttler.Touch(event.Username)
|
|
logger.Println(event)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func main() {
|
|
os.Exit(cli(os.Args))
|
|
}
|
|
|
|
/*
|
|
note to self: restarting the bustled nightly will fix a lot of presence problems. Instead of worrying about people creating directories and logging off, i can just do a daily restart. i /do/ want to capture people logging in, though, but that's not as hard as keeping track of files.
|
|
|
|
*/
|