summaryrefslogtreecommitdiff
path: root/loader/hide_message.py
blob: 4a4d10ac7aeb6f025b5347ed514d9830a9837453 (plain)
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
#!/usr/bin/env python3
#
# hide_message.py - hide a message (spam etc) in the archives, including
# frontend expiry.
#

import os
import sys

from optparse import OptionParser
from configparser import ConfigParser

import psycopg2

from lib.varnish import VarnishPurger

reasons = [
    None,  # Placeholder for 0
    "virus",
    "violates policies",
    "privacy",
    "corrupt",
]

if __name__ == "__main__":
    optparser = OptionParser()
    optparser.add_option('-m', '--msgid', dest='msgid', help='Messageid to hide')

    (opt, args) = optparser.parse_args()

    if (len(args)):
        print("No bare arguments accepted")
        optparser.print_help()
        sys.exit(1)

    if not opt.msgid:
        print("Message-id must be specified")
        optparser.print_help()
        sys.exit(1)

    cfg = ConfigParser()
    cfg.read('%s/archives.ini' % os.path.realpath(os.path.dirname(sys.argv[0])))
    try:
        connstr = cfg.get('db', 'connstr')
    except:
        connstr = 'need_connstr'

    conn = psycopg2.connect(connstr)
    curs = conn.cursor()

    curs.execute("SELECT id, threadid, hiddenstatus FROM messages WHERE messageid=%(msgid)s", {
        'msgid': opt.msgid,
    })
    if curs.rowcount <= 0:
        print("Message not found.")
        sys.exit(1)

    id, threadid, previous = curs.fetchone()

    # Message found, ask for reason
    reason = 0
    print("Current status: %s" % reasons[previous or 0])
    print("\n".join("%s - %s " % (n, reasons[n]) for n in range(len(reasons))))
    while True:
        reason = input('Reason for hiding message? ')
        try:
            reason = int(reason)
        except ValueError:
            continue

        if reason == 0:
            print("Un-hiding message")
            reason = None
            break
        else:
            try:
                print("Hiding message for reason: %s" % reasons[reason])
            except:
                continue
            break
    if previous == reason:
        print("No change in status, not updating")
        conn.close()
        sys.exit(0)

    curs.execute("UPDATE messages SET hiddenstatus=%(new)s WHERE id=%(id)s", {
        'new': reason,
        'id': id,
    })
    if curs.rowcount != 1:
        print("Failed to update! Not hiding!")
        conn.rollback()
        sys.exit(0)
    conn.commit()

    VarnishPurger(cfg).purge([int(threadid), ])
    conn.close()

    print("Message hidden and varnish purge triggered.")