File: matrix_decrypt.py

package info (click to toggle)
weechat-matrix 0.3.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 808 kB
  • sloc: python: 7,992; makefile: 27
file content (90 lines) | stat: -rwxr-xr-x 2,672 bytes parent folder | download
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
#!/usr/bin/python3
# matrix_decrypt - Download and decrypt an encrypted attachment
# from a matrix server

# Copyright © 2019 Damir Jelić <poljar@termina.org.uk>
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted, provided that the
# above copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
# CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import argparse
import requests
import tempfile
import subprocess

from urllib.parse import urlparse, parse_qs
from nio.crypto import decrypt_attachment


def save_file(data):
    """Save data to a temporary file and return its name."""
    tmp_dir = tempfile.gettempdir()

    with tempfile.NamedTemporaryFile(
        prefix='plumber-',
        dir=tmp_dir,
        delete=False
    ) as f:
        f.write(data)
        f.flush()
        return f.name


def main():
    parser = argparse.ArgumentParser(
        description='Download and decrypt matrix attachments'
    )
    parser.add_argument('url', help='the url of the attachment')
    parser.add_argument('file', nargs='?', help='save attachment to <file>')
    parser.add_argument('--plumber',
                        help='program that gets called with the '
                             'dowloaded file')

    args = parser.parse_args()
    url = urlparse(args.url)
    query = parse_qs(url.query)

    if not query["key"] or not query["iv"] or not query["hash"]:
        print("Missing decryption argument")
        return -1

    key = query["key"][0]
    iv = query["iv"][0]
    hash = query["hash"][0]

    http_url = "https://{}{}".format(url.netloc, url.path)

    request = requests.get(http_url)

    if not request.ok:
        print("Error downloading file")
        return -2

    plumber = args.plumber
    plaintext = decrypt_attachment(request.content, key, hash, iv)

    if args.file is None:
        file_name = save_file(plaintext)
        if plumber is None:
            plumber = "xdg-open"
    else:
        file_name = args.file
        open(file_name, "wb").write(plaintext)

    if plumber is not None:
        subprocess.run([plumber, file_name])

    return 0


if __name__ == "__main__":
    main()