wantzel.py 7.37 KB
Newer Older
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
#encoding: utf-8
"""
Bot Wantzel de La Quadrature du Net

Pour utiliser l'accès à une base de données MySQL :
- apt-get install libmysqlclient-dev
- pip install mysql-python
"""

from irc import IrcClientFactory
import json
import MySQLdb
import re
from twisted.internet import reactor
import urllib

import config
from messages import messages

class Wantzel(object):
    """
    Wantzel bot.
    """
    def __init__(self):
        """
        Initialization of bot over IRC.
        """
        self.irc = IrcClientFactory(config)
        self.irc.set_privmsg = self.set_privmsg
        reactor.connectTCP(config.server, config.port, self.irc)

    def set_privmsg(self):
        """
        This method set the methods to call for each callback received from IRC.
        """
        self.irc.client.privmsg = self.on_privmsg
        self.irc.client.joined = self.on_joined
        #self.irc.client.left = self.on_left
        #self.irc.client.noticed = self.on_noticed
        #self.irc.client.modeChanged = self.on_mode_changed
        #self.irc.client.kickedFrom = self.on_kicked_from
        #self.irc.client.nickChanged = self.on_nick_changed
        #self.irc.client.userJoined = self.on_user_joined
        #self.irc.client.userLeft = self.on_user_left
        #self.irc.client.userQuit = self.on_user_quit
        #self.irc.client.userKicked = self.on_user_kicked
        #self.irc.client.action = self.on_action
        #self.irc.client.topicUpdated = self.on_topic_updated
        #self.irc.client.userRenamed = self.on_user_renamed

    def send_message(self, channel, messages):
        """
        Sends a message on specified channel, cutting each line in a new message
        """
        for message in messages.splitlines():
            self.irc.client.msg(channel, message)

    def on_privmsg(self, user, channel, msg):
        """
        Wantzel can understand some commands :
        - help
            Returns a message about how to use the bot.
            If a command is passed after help, the message explains how to use
            the command. 
        - rp(acp)
        - stats
        - quote
        - tc
        - kill
        """
        # Cleaning user name
        user = re.search("([^!]*)!", user).group(1) 
        print("Message reçu: %s %s %s" % (user, channel, msg))
        # Help command, specific
        if "wantzel" in msg and ("help" in msg or "aide" in msg):
            self.help(user, channel, msg)
        # Find command with parameters
        command = re.search("!(rp[acp]*|quote|tc|kill) ", msg)
        if not command:
            # Find command without parameters
            command = re.search("^!(help|stats)$", msg)
        if command:
            print("Commande reçue: %s" % command.group(1))
            command = command.group(1)
            if command.startswith("rp"):
                self.rp(command, user, channel, msg)
            elif command=="help":
                self.help(user, channel, msg)
            elif command=="kill":
                self.kill(user, channel, msg)
            elif command=="stats":
                self.stats(user, channel, msg)
            elif command=="quote":
                self.quote(user, channel, msg)
            elif command=="tc":
                self.tc(user, channel, msg)

    def on_joined(self, channel):
        """
        Say hello to everyone.
        """
        print("Canal %s rejoint" % channel)
        self.send_message("#mytipy", messages["hello"])

    def help(self, user, channel, msg):
        """
        Show global help.
        If a known command is behind the !help command, an adequate message is
        returned.
        """
        print("help command")
        self.send_message(channel, messages["help"])

    def rp(self, command, user, channel, msg):
        """
        Adding the article in rp database.
        """
        print("rp command %s" % command)
        cite = 0
        note = 0
        # On récupère tout ce qui suit la commande !rp
        url = re.search("!rp (.*)", msg).group(1)
        # On nettoie tout ce qui est http
        url = re.search("(https?://)?(.*)", url).group(2)
        # On vire l'ancre si elle existe
        url = re.search("([^#]*)", url).group(0)
        # On se débarasse des trackers
        url = re.sub("", "", url)
        print("url: %s" % url)

        # On vérifie ensuite l'existence de l'url dans la base de données
        db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="site")
        cursor = db.cursor()
        cursor.execute("SELECT id, note, provenance FROM presse WHERE url = %s", (url, ))
        rows = cursor.fetchall()
        if not rows:
            # LQdN is quoted
            if "c" in command:
                cite += 2
            # the article speak about LQdN
            if command.count("p")>1:
                cite += 2
            # Archive this article
            if "a" in command:
                note -= 2
            #TODO: Gérer les autres champs qui n'ont pas de valeur par défaut
            result = cursor.execute(
                "INSERT INTO presse SET url=%s, provenance=%s, cite=%s, note=%s, datec=NOW()",
                (url, user, cite, note)
            )
            self.send_message(channel, messages["merci_nouvel_article"] % user)
        else:
            print("Ajout d'un point par %s sur %s" % (rows[0][2], rows[0][0]))
            if rows[0][2]!=user:
                result = cursor.execute(
                    "UPDATE presse SET note=note+1 WHERE id=%s",
                    (rows[0][0], )
                )
            if (rows[0][1]+1)<3:
                self.send_message(channel, messages["merci_article_connu"] % user)
            else:
                self.send_message(channel, messages["merci_article_repris"] % user)

    def kill(self, user, channel, msg):
        """
        Kill an article by removing 100 points to its score.
        """
        print("kill command")
        pass

    def stats(self, user, channel, msg):
        """
        Returns stats on articles in press review.
        """
        print("stats command")
        db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="site")
        cursor = db.cursor()
        periods = [1, 3, 7, 15]
        notes = [0, 3 ,4]
        notnull = 0
        somethingatall = 0
        result = ""
        for note in notes:
            notnull = 0
            result = result + " note: %s => " % note
            for period in periods:
                cursor.execute(
                    """SELECT COUNT(id) AS cid FROM presse 
                    WHERE nid=0
                    AND datec>(NOW()-INTERVAL %s DAY)
                    AND note>=%s""",
                    (period, note)
                )
                rows = cursor.fetchall()
                if rows[0][0]>0:
                    result = result + "%sj:%s, " % (period, rows[0][0])
                    notnull = 1
                    somethingatall=1
            if notnull:
                result = result[:-2] + "\n"
        if not somethingatall:
            #TODO: Bravo!
            result = "Bravo!"
        self.send_message(channel, result)

    def quote(self, user, channel, msg):
        """
        Record a quote of a VIP during debates.
        """
        print("quote command")
        pass

    def tc(self, user, channel, msg):
        """
        Timecode...
        """
        print("tc command")
        pass

if __name__ == '__main__':
    wantzel = Wantzel()
    reactor.run()