diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000000000000000000000000000000000000..1e603bfc4c2b9d7fa27261c154e5ae618b88ba0d --- /dev/null +++ b/Readme.md @@ -0,0 +1,27 @@ +# KStats + +## Setup + +1. Create a PostgreSQL user and database for the statsbot +2. Apply the schema (see `schema.sql` to the database) +3. Add channels to the channels table +4. Start the bot + +## Configuration + +The bot takes several environment variables for configuration. +Required variables are marked with * + +| Name | Example | Description | +| ------------------------ | ---------------------------------------------- | ------------------------------------------------------ | +|`KSTATS_IRC_SERVER`* | `irc.rizon.net` | Irc Servername | +|`KSTATS_IRC_PORT`* | `6697` | Irc Port | +|`KSTATS_IRC_SECURE` | `true` | If TLS should be enabled for this connection | +|`KSTATS_IRC_NICK` |`kstats` | Nickname visible on IRC | +|`KSTATS_IRC_IDENT` |`kstats` | Ident visible on IRC | +|`KSTATS_IRC_REALNAME` |`KStats kuschku.de Statistics Bot` | Realname visible on IRC | +|`KSTATS_IRC_SASL_ENABLED` |`true` | If SASL is enabled | +|`KSTATS_IRC_SASL_ACCOUNT` |`kstats` | SASL Username | +|`KSTATS_IRC_SASL_PASSWORD`|`caa5db269fc39` | SASL Password | +|`KSTATS_DATABASE_TYPE`* |`postgres` | Database driver (only postgres is supported currently) | +|`KSTATS_DATABASE_URL`* |`postgresql://kstats:hunter2@localhost/statsbot`| Database URL | diff --git a/main.go b/main.go index 944d4923c68d428e9709953e017d938e4359adb1..ec8d0c96824bc38664d63057dd74b795a467668d 100644 --- a/main.go +++ b/main.go @@ -212,9 +212,8 @@ func handlePrivateMessage(channels map[string]IrcChannel, event girc.Event, clie if len(parameters) == 1 { channelName := parameters[0] if channelData, ok := channels[channelName]; ok { - nick := event.Source.Name - hash := hashName(channelData.Salt, nick) - _, err := db.Exec("DELETE FROM users WHERE hash = $1 AND nick = $2", hash, nick) + hash := hashName(channelData.Salt, event.Source.Name) + _, err := db.Exec("DELETE FROM users WHERE hash = $1", hash) if err != nil { client.Cmd.Reply(event, "An error has occured, please try later again") println(err.Error()) @@ -260,7 +259,7 @@ func logMessage(channelData IrcChannel, event girc.Event, client *girc.Client, c } } for _, user := range users { - _, err := db.Exec("INSERT INTO \"references\" (time, source, target) VALUES ($1, $2, $3)", now, name, user) + _, err := db.Exec("INSERT INTO \"references\" (channel, time, source, target) VALUES ($1, $2, $3, $4)", channelData.Id, now, name, user) if err != nil { println(err.Error()) } diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000000000000000000000000000000000000..c1cd69e6e85b3ea140d4c183e37b8c14b81a0b85 --- /dev/null +++ b/schema.sql @@ -0,0 +1,37 @@ +create table messages ( + id serial not null + constraint messages_pkey primary key, + time timestamp, + channel integer + constraint messages_channels_id_fk references channels on update cascade on delete cascade, + sender text, + words integer, + characters integer, + question boolean, + exclamation boolean, + caps boolean, + aggression boolean, + emoji_happy boolean, + emoji_sad boolean +); + +create index messages_channel_index + on messages (channel); +create index messages_channel_sender_index + on messages (channel, sender); + +create table users ( + hash text not null + constraint users_pkey primary key, + nick text +); + +create table "references" ( + id serial not null + constraint references_pkey primary key, + channel integer + constraint references_channels_id_fk references channels on update cascade on delete cascade, + time timestamp, + source text, + target text +); \ No newline at end of file