Added option to disable chat
This commit is contained in:
@@ -10,13 +10,13 @@ import (
|
||||
)
|
||||
|
||||
|
||||
func CacheStream( path string, readStream func() ( io.ReadCloser, error ), expires int ) ( *bytes.Buffer, error ) {
|
||||
func CacheStream( path string, readStream func() ( io.ReadCloser, error ), expires time.Duration ) ( *bytes.Buffer, error ) {
|
||||
|
||||
cache, err := os.Stat( path )
|
||||
|
||||
// Check if cache exists and not expired
|
||||
if err == nil {
|
||||
expired := cache.ModTime().Add( 60 * 1e9 )
|
||||
expired := cache.ModTime().Add( expires * 1e9 )
|
||||
if time.Now().Before( expired ) {
|
||||
f, err := os.Open( path )
|
||||
if err == nil {
|
||||
@@ -33,8 +33,7 @@ func CacheStream( path string, readStream func() ( io.ReadCloser, error ), expir
|
||||
|
||||
err = os.MkdirAll( filepath.Dir( path ), 0750 )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, err }
|
||||
|
||||
writeBuff := bytes.NewBuffer( []byte{} )
|
||||
|
||||
@@ -66,3 +65,57 @@ func CacheStream( path string, readStream func() ( io.ReadCloser, error ), expir
|
||||
|
||||
return writeBuff, nil
|
||||
}
|
||||
|
||||
func ChangedStream( path string, readStream func() ( io.Reader, error ), dataModTime time.Time ) ( *bytes.Buffer, error ) {
|
||||
|
||||
cache, err := os.Stat( path )
|
||||
|
||||
// Check if cache exists and not expired
|
||||
if err == nil {
|
||||
if dataModTime.Before( cache.ModTime() ) {
|
||||
f, err := os.Open( path )
|
||||
if err == nil {
|
||||
defer f.Close()
|
||||
log.Printf( "Reading from file: %s", path )
|
||||
writeBuff := bytes.NewBuffer( []byte{} )
|
||||
_, err = io.Copy( writeBuff, f )
|
||||
if err == nil {
|
||||
return writeBuff, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
err = os.MkdirAll( filepath.Dir( path ), 0750 )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
writeBuff := bytes.NewBuffer( []byte{} )
|
||||
|
||||
// Get the reader that return new data
|
||||
s, err := readStream()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = io.Copy( writeBuff, s )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
f, err := os.OpenFile( path, os.O_CREATE | os.O_WRONLY | os.O_TRUNC, 0644 )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
data := writeBuff.Bytes()
|
||||
_, err = io.Copy( f, bytes.NewReader( data ) )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return writeBuff, nil
|
||||
}
|
||||
|
93
utils/system.go
Normal file
93
utils/system.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
)
|
||||
|
||||
var JSON_SETTINGS string = filepath.Join( WORKDIR, "settings.json" )
|
||||
|
||||
var settingsTime = time.Unix( 0, 0 )
|
||||
|
||||
type SysSettings struct {
|
||||
IgnoredChats map[int64] bool `json:"IgnoredChats"`
|
||||
}
|
||||
|
||||
var Settings = SysSettings{ IgnoredChats: map[int64] bool{} }
|
||||
|
||||
func rwSettings() {
|
||||
|
||||
buff, err := ChangedStream( JSON_SETTINGS, writeSettings, settingsTime )
|
||||
if err != nil {
|
||||
log.Panic( err )
|
||||
return
|
||||
}
|
||||
|
||||
conf := SysSettings{}
|
||||
err = json.Unmarshal( buff.Bytes(), &conf )
|
||||
if err != nil {
|
||||
log.Panic( err )
|
||||
return
|
||||
}
|
||||
|
||||
Settings = conf
|
||||
}
|
||||
|
||||
func writeSettings() ( io.Reader, error ) {
|
||||
b, err := json.Marshal( Settings )
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return bytes.NewBuffer( b ), nil
|
||||
}
|
||||
|
||||
func SystemControl( tgMesg *tgbotapi.Message ) ( string, bool ) {
|
||||
|
||||
rwSettings()
|
||||
|
||||
processed := false
|
||||
mesg := ""
|
||||
|
||||
if tgMesg.Text[0] == '/' {
|
||||
processed = true
|
||||
}
|
||||
|
||||
chatId := tgMesg.Chat.ID
|
||||
|
||||
if Settings.IgnoredChats == nil {
|
||||
Settings.IgnoredChats = map[int64] bool{}
|
||||
}
|
||||
|
||||
if strings.Contains( tgMesg.Text, "/golifehk disable" ) {
|
||||
mesg = fmt.Sprintf( "OK" )
|
||||
Settings.IgnoredChats[ chatId ] = true
|
||||
processed = true
|
||||
}
|
||||
|
||||
if strings.Contains( tgMesg.Text, "/golifehk enable" ) {
|
||||
mesg = fmt.Sprintf( "OK" )
|
||||
Settings.IgnoredChats[ chatId ] = false
|
||||
processed = true
|
||||
}
|
||||
|
||||
if processed {
|
||||
settingsTime = time.Now()
|
||||
rwSettings()
|
||||
}
|
||||
|
||||
//// Begin processing settings
|
||||
|
||||
// ignore chats if enabled
|
||||
if Settings.IgnoredChats[ chatId ] {
|
||||
processed = true
|
||||
}
|
||||
|
||||
return mesg, processed
|
||||
}
|
Reference in New Issue
Block a user