From 20f83572448187f806095ee0a8cc5b30bff72a93 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 15 Jun 2024 09:43:57 -0400 Subject: added watchlists --- README.md | 15 ++++++++++ main.go | 62 +++++++++++++++++++++++++++++++++++++++ scripts/entry_limit_trigger.pgsql | 19 ------------ scripts/entry_limit_trigger.sql | 27 +++++++++++++++++ types.go | 8 +++++ 5 files changed, 112 insertions(+), 19 deletions(-) delete mode 100644 scripts/entry_limit_trigger.pgsql create mode 100644 scripts/entry_limit_trigger.sql diff --git a/README.md b/README.md index 23617b6..08eec17 100644 --- a/README.md +++ b/README.md @@ -263,6 +263,17 @@ The `limit` parameter limits the number of SQL queries that are used to generate A `limit` value of 0 disables the limit on the amount of rows that are passed to milla.
NOTE: since each milla instance can have its own database, all instances might not necessarily have access to all the data milla is gathering. If you use the same database for all the instances, all instances will have access to all the gathered data.
+## Watchlist + +Watchlists allow you to specify a list of channels to watch. The watched values are given in a list of files, each line of the file specifying a value to watch for. Finally a value is given for the alertchannel where the bot will mirror the message that triggered a match.
+ +```toml +[ircd.devinet_terra.watchlist.security] +watchList = ["#securityfeeds"] +watchFiles = ["/watchfiles/voidbox.list"] +alertChannel = "#milla_alerts" +``` + ### Example Config File ```toml @@ -297,6 +308,10 @@ skipTLSVerify = false useTLS = true plugins = ["/plugins/plugin1.lua", "/plugins/plugin2.lua"] adminOnly = false +[ircd.devinet.watchlist.security] +watchList = ["#securityfeeds"] +watchFiles = ["/watchfiles/voidbox.list"] +alertChannel = "#milla_alerts" [ircd.liberanet] ircServer = "irc.libera.chat" diff --git a/main.go b/main.go index f8dd3bb..3962a9b 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "errors" "flag" "fmt" + "index/suffixarray" "log" "net" "net/http" @@ -1044,6 +1045,50 @@ func scrapeChannel(irc *girc.Client, poolChan chan *pgxpool.Pool, appConfig Toml }) } +func populateWatchListWords(appConfig *TomlConfig) { + for watchlistName, watchlist := range appConfig.WatchLists { + for _, filepath := range watchlist.WatchFiles { + filebytes, err := os.ReadFile(filepath) + if err != nil { + log.Println(err.Error()) + + continue + } + + filestring := string(filebytes) + + words := strings.Split(filestring, "\n") + + watchlist.Words = append(watchlist.Words, words...) + appConfig.WatchLists[watchlistName] = watchlist + } + } + + log.Print(appConfig.WatchLists["security"].Words) +} + +func WatchListHandler(irc *girc.Client, appConfig TomlConfig) { + irc.Handlers.AddBg(girc.PRIVMSG, func(_ *girc.Client, event girc.Event) { + sarray := suffixarray.New([]byte(event.Last())) + + for watchname, watchlist := range appConfig.WatchLists { + for _, channel := range watchlist.WatchList { + if channel == event.Params[0] { + for _, word := range watchlist.Words { + indexes := sarray.Lookup([]byte(word), -1) + if len(indexes) > 0 { + irc.Cmd.Message(watchlist.AlertChannel, fmt.Sprintf("%s: %s", watchname, event.Last())) + log.Printf("%s: %s", watchname, event.Last()) + + break + } + } + } + } + } + }) +} + func runIRC(appConfig TomlConfig) { var OllamaMemory []MemoryElement @@ -1148,6 +1193,23 @@ func runIRC(appConfig TomlConfig) { go scrapeChannel(irc, poolChan, appConfig) } + if len(appConfig.WatchLists) > 0 { + irc.Handlers.AddBg(girc.CONNECTED, func(client *girc.Client, _ girc.Event) { + for _, watchlist := range appConfig.WatchLists { + log.Print("joining ", watchlist.AlertChannel) + client.Cmd.Join(watchlist.AlertChannel) + + for _, channel := range watchlist.WatchList { + client.Cmd.Join(channel) + } + } + }) + + populateWatchListWords(&appConfig) + + go WatchListHandler(irc, appConfig) + } + for { var dialer proxy.Dialer diff --git a/scripts/entry_limit_trigger.pgsql b/scripts/entry_limit_trigger.pgsql deleted file mode 100644 index d7d99c2..0000000 --- a/scripts/entry_limit_trigger.pgsql +++ /dev/null @@ -1,19 +0,0 @@ -CREATE OR REPLACE FUNCTION milla.janitor () - RETURNS TRIGGER - AS $$ -BEGIN - UPDATE - posts - SET - updated_at = now() - WHERE - id = NEW.id; - RETURN new; -END; -$$ -LANGUAGE plpgsql; - -CREATE TRIGGER janitor_trigger - AFTER INSERT ON milla.tables - EXECUTE PROCEDURE milla.janitor (); - diff --git a/scripts/entry_limit_trigger.sql b/scripts/entry_limit_trigger.sql new file mode 100644 index 0000000..a3d4887 --- /dev/null +++ b/scripts/entry_limit_trigger.sql @@ -0,0 +1,27 @@ +create function remove_old_entries() + returns trigger + as $$ +begin + if( + select + COUNT(*) + from + table_name) > 10000 then + delete from table_name + where id in( + select + id + from + table_name + order by + id asc + limit 1000); + end if; + return null; +end; +$$ +language plpgsql; + +create trigger remove_old_entries_trigger + after insert on table_name for each row + execute procedure remove_old_entries(); diff --git a/types.go b/types.go index 98738bd..266eccc 100644 --- a/types.go +++ b/types.go @@ -28,6 +28,13 @@ type LuaLstates struct { Cancel context.CancelFunc } +type WatchList struct { + AlertChannel string `toml:"alertChannel"` + WatchList []string `toml:"watchList"` + WatchFiles []string `toml:"watchFiles"` + Words []string `toml:"watchWords"` +} + type TomlConfig struct { IrcServer string `toml:"ircServer"` IrcNick string `toml:"ircNick"` @@ -57,6 +64,7 @@ type TomlConfig struct { WebIRCAddress string `toml:"webIRCAddress"` Plugins []string `toml:"plugins"` CustomCommands map[string]CustomCommand `toml:"customCommands"` + WatchLists map[string]WatchList `toml:"watchList"` LuaStates map[string]LuaLstates Temp float64 `toml:"temp"` RequestTimeout int `toml:"requestTimeout"` -- cgit v1.2.3