wantzel.py 9.31 KB
Newer Older
1
2
#encoding: utf-8
"""
Mindiell's avatar
Mindiell committed
3
Bot Wantzel from La Quadrature du Net.
4

Mindiell's avatar
Mindiell committed
5
6
7
8
9
10
11
License : AGPLv3
Doc     : https://wiki.laquadrature.net/Wantzel

TODO:
- Ajouter la gestion des droits pour certaines commandes
- Mettre une valeur par défaut pour les champs concernés
- Afficher les titres des urls fournies sur le canal
12
13
14
15
16
17
18
19
20
21
22
"""

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

import config
from messages import messages

Mindiell's avatar
Mindiell committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def get_cursor():
    """
    This function connects to a database and returns a usable cursor.
    """
    db = MySQLdb.connect(
        host=config.dbserver,
        user=config.dbuser,
        passwd=config.dbpassword,
        db=config.dbname
    )
    if db:
        return db.cursor()
    return None

37
def get_url(message):
Mindiell's avatar
Mindiell committed
38
    """
39
    Retrieve the url in the message.
Mindiell's avatar
Mindiell committed
40
    """
41
42
    # Let's get the url
    result = re.search("(https?[^ ]+)", message)
Mindiell's avatar
Mindiell committed
43
44
    if not result:
        return "http"
45
    url = result.group(1)
Mindiell's avatar
Mindiell committed
46
47
48
49
50
51
52
53
    # Removing anchor if needed
    result = re.search("^([^#]*)", url)
    if result:
        url = result.group(1)
    # Removing trackers
    url = re.sub("[?&](utm_medium|utm_source|utm_campaign|xtor)=[^&]*", "", url)
    return url

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def get_title(message):
    title = ""
    website = ""
    try:
        url = get_url(message)#re.search("(http[^ ]*)", msg).group(1)
        website = re.search("//([^/]*)", url).group(1)
        f = urllib.URLopener().open(url)
        content = f.read()
        title = re.search("<title>([^<]+)</title>", content).group(1)
    except:
        pass
    # Unescaping HTML entities
    if title:
        title = re.sub("&gt;", ">", title)
        title = re.sub("&lt;", "<", title)
        title = re.sub("&quot;", '"', title)
        title = re.sub("&amp;", "&", title)
    return (title, website)

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
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

    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)
Mindiell's avatar
Mindiell committed
107
            Add an article in the database
108
        - stats
Mindiell's avatar
Mindiell committed
109
            Show some statistics about the RP
110
        - kill
Mindiell's avatar
Mindiell committed
111
            Kill an article by giving it a score of -100
112
113
        """
        # Cleaning user name
Mindiell's avatar
Mindiell committed
114
115
        user = re.search("([^!]*)!", user).group(1)
        print("Message received: %s %s %s" % (user, channel, msg))
116
117
118
119
        # Whatever is done, get the title of an existing url in a message
        title = ""
        if "http" in msg:
            title, website = get_title(msg)
Mindiell's avatar
Mindiell committed
120
121
122
123
124
125
126
        # Never answer to botself
        if user!=config.nickname:
            # If it's a query, bot should answer to the user as the channel
            if "#" not in channel:
                channel = user
            # Help command, specific
            if "wantzel" in msg and ("help" in msg or "aide" in msg):
127
                self.help(user, channel, msg)
Mindiell's avatar
Mindiell committed
128
129
130
131
132
133
            # Find known command
            command = re.search("!(rp[acp]*|kill|help|stats)", msg)
            if command:
                command = command.group(1)
                print("Command: %s" % command)
                if command.startswith("rp"):
134
                    self.rp(command, user, channel, msg, title)
Mindiell's avatar
Mindiell committed
135
136
137
138
139
140
                elif command=="help":
                    self.help(user, channel, msg)
                elif command=="kill":
                    self.kill(user, channel, msg)
                elif command=="stats":
                    self.stats(user, channel, msg)
141
142
        if title and website:
            self.send_message(channel, messages["title"] % (title, website))
143
144
145
146
147

    def on_joined(self, channel):
        """
        Say hello to everyone.
        """
Mindiell's avatar
Mindiell committed
148
149
150
151
152
153
        print("Joined channel %s" % channel)
        # Specific message for specific channel
        if "hello_"+channel[1:] in messages:
            self.send_message(channel, messages["hello_"+channel[1:]])
        else:
            self.send_message(channel, messages["hello"])
154
155
156
157
158
159
160
161

    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")
Mindiell's avatar
Mindiell committed
162
163
164
165
166
167
168
        # Searching for a command after help keyword
        command = re.search("!help (stats|rp|help|kill)", msg)
        if command:
            command = command.group(1)
            self.send_message(user, messages["help_"+command])            
        else:
            self.send_message(channel, messages["help"])
169

170
    def rp(self, command, user, channel, msg, title=""):
171
172
173
174
175
176
        """
        Adding the article in rp database.
        """
        print("rp command %s" % command)
        cite = 0
        note = 0
177
        url = get_url(msg)
178
        print("url: %s" % url)
Mindiell's avatar
Mindiell committed
179
180
181
182
183
        if url=="":
            return
        elif url=="http":
            self.send_message(channel, messages["rp_http"] % user)
            return
184

Mindiell's avatar
Mindiell committed
185
186
        # Looking for such an article in database
        cursor = get_cursor()
187
188
189
190
191
192
193
194
195
196
197
198
199
        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
Mindiell's avatar
Mindiell committed
200
201
            # lang, published, nid, screenshot, title, fetched, seemscite
            print("Adding an article by %s: %s" % (user, url))
202
            result = cursor.execute(
203
204
205
206
207
                """INSERT INTO presse SET 
                url=%s, provenance=%s, cite=%s, note=%s, datec=NOW(), title=%s,
                lang='', published=0, nid=0, screenshot=0, fetched=0, seemscite=0
                """,
                (url, user, cite, note, title)
208
            )
Mindiell's avatar
Mindiell committed
209
            self.send_message(channel, messages["rp_new_article"] % user)
210
211
        else:
            if rows[0][2]!=user:
Mindiell's avatar
Mindiell committed
212
                print("Adding a point by %s on %s" % (user, rows[0][0]))
213
214
215
216
217
                result = cursor.execute(
                    "UPDATE presse SET note=note+1 WHERE id=%s",
                    (rows[0][0], )
                )
            if (rows[0][1]+1)<3:
Mindiell's avatar
Mindiell committed
218
                self.send_message(channel, messages["rp_known_article"] % user)
219
            else:
Mindiell's avatar
Mindiell committed
220
                self.send_message(channel, messages["rp_taken_article"] % user)
221
222
223

    def kill(self, user, channel, msg):
        """
Mindiell's avatar
Mindiell committed
224
        Kill an article by setting its score to -100.
225
        """
Mindiell's avatar
Mindiell committed
226
        #TODO: Gérer les droits de cette commande
227
        print("kill command")
228
        url = get_url(msg)
Mindiell's avatar
Mindiell committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
        print("url: %s" % url)
        if url=="":
            return
        elif url=="http":
            self.send_message(channel, messages["rp_http"] % user)
            return
        # Looking for such an article in database
        cursor = get_cursor()
        cursor.execute("SELECT id, note FROM presse WHERE url=%s", (url, ))
        rows = cursor.fetchall()
        if not rows:
            self.send_message(channel, messages["kill_none"] % url)
        else:
            cursor.execute("UPDATE presse SET note=-100 WHERE id=%s", (rows[0][0], ))
            self.send_message(channel, messages["kill_done"] % url)
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258

    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
Mindiell's avatar
Mindiell committed
259
            result = result + "note>=%s: " % note
260
261
262
263
264
265
266
267
268
269
270
271
            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
Mindiell's avatar
Mindiell committed
272
                    somethingatall = 1
273
274
            if notnull:
                result = result[:-2] + "\n"
Mindiell's avatar
Mindiell committed
275
276
        if somethingatall==0:
            result = messages["stats_bravo"] % periods[-1]
277
278
279
280
281
282
        self.send_message(channel, result)


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