summaryrefslogtreecommitdiff
path: root/escato.pl
blob: 514b4c9b005315be17dbf09fc81b16af3934b1ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env perl

use strict;
use warnings;
use List::Util qw<any>;
use DBI;

exit unless exists $ENV{'TGUTILS_TYPE'};
exit unless $ENV{'TGUTILS_TYPE'} eq 'TEXT';

$/ = undef;
my $text = <>;

# Indicios de que alguien ha cagado
my @DUMP_TRIGGERS = (
    "caga", "jiñ", "vientr", # Verbos
    "pino", "pinaco", "ñordo", "truñ", "chusc", "caca", "mierda" # Sustantivos
    );
my @PHRASES = (
    "Estoy orgulloso de ti, %s.",
    "¡Así se hace %s! 👏👏",
    "¿Otra vez, %s? Tu salud intestinal es admirable, felicidades.",
    );
my $dbh;

init_schema() unless -f 'escato.db';
if (lc($text) =~ /^\@escatobot/) {
    if (any { lc($text) =~ $_ } @DUMP_TRIGGERS) {
        save_dump();
    } else {
        show_dumps();
    }
}

sub show_dumps {
    my ($tg_chat_id, $tg_id, $tg_username) =
        @ENV{'TGUTILS_CHAT_ID', 'TGUTILS_FROM_ID', 'TGUTILS_FROM_USERNAME'};
    $dbh ||= DBI->connect("DBI:SQLite:dbname=escato.db", { AutoCommit => 0, RaiseError => 1 });

    my ($sec, $min, $hour) = localtime();
    print
        $hour < 4 || $hour > 20 ? "Hola, buenas noches. " :
        $hour > 13 ? "Hola, buenas tardes. " :
        "Hola, buenos días. ";

    if ($tg_chat_id eq $tg_id) {
        my ($month, $year) =
            @{tg_id_dumps($tg_id)}{'month', 'year'};

        print "Te cuento, $tg_username. Este mes has cagado $month veces. En total llevas $year truños en lo que vamos de año.\n";
    } else {
        print "Os cuento, shures:\n";
        my $position = 1;
        my @shures;
        foreach my $shur (values %{chat_members($tg_chat_id)}) {
            push @shures, {username => $shur->{'username'},
                           data => tg_id_dumps($shur->{'id'})};
        }
        foreach my $shur (sort { $b->{data}{month} <=> $a->{data}{month} } @shures) {
            printf "%d - @%s ha cagado %d veces este mes, y %d al año.\n",
                $position++, $shur->{username}, $shur->{data}{month}, $shur->{data}{year};
        }
    }
}

sub chat_members {
    my $tg_chat_id = shift;
    $dbh ||= DBI->connect("DBI:SQLite:dbname=escato.db", { AutoCommit => 0, RaiseError => 1 });
    my $sth = $dbh->prepare('SELECT U.id, U.username FROM tg_chat_users CU JOIN tg_users U ON U.id = CU.tg_id WHERE CU.tg_chat_id = ?');
    $sth->execute($tg_chat_id);

    return $sth->fetchall_hashref('id');
}

sub tg_id_dumps {
    my $tg_id = shift;

    $dbh ||= DBI->connect("DBI:SQLite:dbname=escato.db", { AutoCommit => 0, RaiseError => 1 });

    my $sth = $dbh->prepare('SELECT day, count FROM monthly_dumps WHERE tg_id = ?');
    $sth->execute($tg_id);
    my %stats = (total => 0, year => 0, month => 0, day => 0);
    my (undef, undef, undef, $day, $month, $year) = localtime();
    $month++;
    $year += 1900;
    while (my $row = $sth->fetch()) {
        my ($date, $count) = @$row;
        my ($y, $m, $d) = $date =~ /(\d+)-(\d+)-(\d+)/;
        $stats{total} += $count;
        $stats{year} += $count if $y == $year;
        $stats{month} += $count if $m == $month;
        $stats{day} += $count if $d == $day;
    }
    \%stats;
}

sub init_schema {
    $dbh ||= DBI->connect("DBI:SQLite:dbname=escato.db", { AutoCommit => 0, RaiseError => 1 });
    $dbh->prepare('CREATE TABLE tg_users (
                 id INTEGER PRIMARY KEY,
                 username TEXT NOT NULL UNIQUE
                 )')->execute();
    $dbh->prepare('CREATE TABLE tg_chats (
                 id INTEGER PRIMARY KEY
                 )')->execute();
    $dbh->prepare('CREATE TABLE tg_chat_users (
                 tg_chat_id INTEGER NOT NULL,
                 tg_id INTEGER NOT NULL,
                 PRIMARY KEY (tg_chat_id, tg_id),
                 FOREIGN KEY (tg_id) REFERENCES tg_users(id),
                 FOREIGN KEY (tg_chat_id) REFERENCES tg_chats(id)
                 )')->execute();
    $dbh->prepare('CREATE TABLE monthly_dumps (
                 id INTEGER PRIMARY KEY,
                 tg_id INTEGER NOT NULL,
                 day DATE NOT NULL,
                 count INTEGER NOT NULL,
                 FOREIGN KEY (tg_id) REFERENCES tg_users(id)
                 UNIQUE(tg_id, day)
                 )')->execute();
}

sub save_dump {
    my ($tg_chat_id, $tg_id, $tg_username) = @ENV{'TGUTILS_CHAT_ID', 'TGUTILS_FROM_ID', 'TGUTILS_FROM_USERNAME'};

    $dbh ||= DBI->connect("DBI:SQLite:dbname=escato.db", { AutoCommit => 0, RaiseError => 1 });
    $dbh->prepare('INSERT OR REPLACE INTO tg_users VALUES (?, ?)')
        ->execute($tg_id, $tg_username);
    $dbh->prepare('INSERT OR IGNORE INTO tg_chats VALUES (?)')
        ->execute($tg_chat_id);
    $dbh->prepare('INSERT OR IGNORE INTO tg_chat_users VALUES (?, ?)')
        ->execute($tg_chat_id, $tg_id);

    my $sth = $dbh->prepare('SELECT count FROM monthly_dumps
                           WHERE tg_id = ? AND day = DATE("now", "start of day")');
    $sth->execute($tg_id);
    if ($sth->fetch()) {
        $dbh
            ->prepare('UPDATE monthly_dumps SET count = count+1 WHERE
                     tg_id = ? AND day = DATE("now", "start of day")')
            ->execute($tg_id);
    } else {
        $dbh->prepare('INSERT INTO monthly_dumps
                     (tg_id, day, count)
                     VALUES
                     (?, DATE("now", "start of day"), 1)')->execute($tg_id);
    }
    printf $PHRASES[rand @PHRASES], "@" . $tg_username;
}