Skip to content

Commit 20f8357

Browse files
added watchlists
1 parent 556c839 commit 20f8357

File tree

5 files changed

+112
-19
lines changed

5 files changed

+112
-19
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,17 @@ The `limit` parameter limits the number of SQL queries that are used to generate
263263
A `limit` value of 0 disables the limit on the amount of rows that are passed to milla.<br/>
264264
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.<br/>
265265

266+
## Watchlist
267+
268+
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.<br/>
269+
270+
```toml
271+
[ircd.devinet_terra.watchlist.security]
272+
watchList = ["#securityfeeds"]
273+
watchFiles = ["/watchfiles/voidbox.list"]
274+
alertChannel = "#milla_alerts"
275+
```
276+
266277
### Example Config File
267278

268279
```toml
@@ -297,6 +308,10 @@ skipTLSVerify = false
297308
useTLS = true
298309
plugins = ["/plugins/plugin1.lua", "/plugins/plugin2.lua"]
299310
adminOnly = false
311+
[ircd.devinet.watchlist.security]
312+
watchList = ["#securityfeeds"]
313+
watchFiles = ["/watchfiles/voidbox.list"]
314+
alertChannel = "#milla_alerts"
300315

301316
[ircd.liberanet]
302317
ircServer = "irc.libera.chat"

main.go

+62
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"flag"
1010
"fmt"
11+
"index/suffixarray"
1112
"log"
1213
"net"
1314
"net/http"
@@ -1044,6 +1045,50 @@ func scrapeChannel(irc *girc.Client, poolChan chan *pgxpool.Pool, appConfig Toml
10441045
})
10451046
}
10461047

1048+
func populateWatchListWords(appConfig *TomlConfig) {
1049+
for watchlistName, watchlist := range appConfig.WatchLists {
1050+
for _, filepath := range watchlist.WatchFiles {
1051+
filebytes, err := os.ReadFile(filepath)
1052+
if err != nil {
1053+
log.Println(err.Error())
1054+
1055+
continue
1056+
}
1057+
1058+
filestring := string(filebytes)
1059+
1060+
words := strings.Split(filestring, "\n")
1061+
1062+
watchlist.Words = append(watchlist.Words, words...)
1063+
appConfig.WatchLists[watchlistName] = watchlist
1064+
}
1065+
}
1066+
1067+
log.Print(appConfig.WatchLists["security"].Words)
1068+
}
1069+
1070+
func WatchListHandler(irc *girc.Client, appConfig TomlConfig) {
1071+
irc.Handlers.AddBg(girc.PRIVMSG, func(_ *girc.Client, event girc.Event) {
1072+
sarray := suffixarray.New([]byte(event.Last()))
1073+
1074+
for watchname, watchlist := range appConfig.WatchLists {
1075+
for _, channel := range watchlist.WatchList {
1076+
if channel == event.Params[0] {
1077+
for _, word := range watchlist.Words {
1078+
indexes := sarray.Lookup([]byte(word), -1)
1079+
if len(indexes) > 0 {
1080+
irc.Cmd.Message(watchlist.AlertChannel, fmt.Sprintf("%s: %s", watchname, event.Last()))
1081+
log.Printf("%s: %s", watchname, event.Last())
1082+
1083+
break
1084+
}
1085+
}
1086+
}
1087+
}
1088+
}
1089+
})
1090+
}
1091+
10471092
func runIRC(appConfig TomlConfig) {
10481093
var OllamaMemory []MemoryElement
10491094

@@ -1148,6 +1193,23 @@ func runIRC(appConfig TomlConfig) {
11481193
go scrapeChannel(irc, poolChan, appConfig)
11491194
}
11501195

1196+
if len(appConfig.WatchLists) > 0 {
1197+
irc.Handlers.AddBg(girc.CONNECTED, func(client *girc.Client, _ girc.Event) {
1198+
for _, watchlist := range appConfig.WatchLists {
1199+
log.Print("joining ", watchlist.AlertChannel)
1200+
client.Cmd.Join(watchlist.AlertChannel)
1201+
1202+
for _, channel := range watchlist.WatchList {
1203+
client.Cmd.Join(channel)
1204+
}
1205+
}
1206+
})
1207+
1208+
populateWatchListWords(&appConfig)
1209+
1210+
go WatchListHandler(irc, appConfig)
1211+
}
1212+
11511213
for {
11521214
var dialer proxy.Dialer
11531215

scripts/entry_limit_trigger.pgsql

-19
This file was deleted.

scripts/entry_limit_trigger.sql

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
create function remove_old_entries()
2+
returns trigger
3+
as $$
4+
begin
5+
if(
6+
select
7+
COUNT(*)
8+
from
9+
table_name) > 10000 then
10+
delete from table_name
11+
where id in(
12+
select
13+
id
14+
from
15+
table_name
16+
order by
17+
id asc
18+
limit 1000);
19+
end if;
20+
return null;
21+
end;
22+
$$
23+
language plpgsql;
24+
25+
create trigger remove_old_entries_trigger
26+
after insert on table_name for each row
27+
execute procedure remove_old_entries();

types.go

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ type LuaLstates struct {
2828
Cancel context.CancelFunc
2929
}
3030

31+
type WatchList struct {
32+
AlertChannel string `toml:"alertChannel"`
33+
WatchList []string `toml:"watchList"`
34+
WatchFiles []string `toml:"watchFiles"`
35+
Words []string `toml:"watchWords"`
36+
}
37+
3138
type TomlConfig struct {
3239
IrcServer string `toml:"ircServer"`
3340
IrcNick string `toml:"ircNick"`
@@ -57,6 +64,7 @@ type TomlConfig struct {
5764
WebIRCAddress string `toml:"webIRCAddress"`
5865
Plugins []string `toml:"plugins"`
5966
CustomCommands map[string]CustomCommand `toml:"customCommands"`
67+
WatchLists map[string]WatchList `toml:"watchList"`
6068
LuaStates map[string]LuaLstates
6169
Temp float64 `toml:"temp"`
6270
RequestTimeout int `toml:"requestTimeout"`

0 commit comments

Comments
 (0)