File: util.py

package info (click to toggle)
gajim-pgp 1.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 164 kB
  • sloc: python: 934; sh: 29; xml: 11; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 1,578 bytes parent folder | download | duplicates (2)
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
# Copyright (C) 2019 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of the PGP Gajim Plugin.
#
# PGP Gajim Plugin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# PGP Gajim Plugin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PGP Gajim Plugin. If not, see <http://www.gnu.org/licenses/>.

import os
import subprocess

from nbxmpp import Message
from nbxmpp.namespaces import Namespace


def prepare_stanza(stanza: Message, plaintext: str) -> None:
    delete_nodes(stanza, "encrypted", Namespace.ENCRYPTED)
    delete_nodes(stanza, "body")
    stanza.setBody(plaintext)


def delete_nodes(stanza: Message, name: str, namespace: str | None = None) -> None:
    nodes = stanza.getTags(name, namespace=namespace)
    for node in nodes:
        stanza.delChild(node)


def find_gpg():
    def _search(binary: str) -> bool:
        if os.name == "nt":
            gpg_cmd = binary + " -h >nul 2>&1"
        else:
            gpg_cmd = binary + " -h >/dev/null 2>&1"
        if subprocess.call(gpg_cmd, shell=True):  # noqa: S602, SIM103
            return False
        return True

    if _search("gpg2"):
        return "gpg2"

    if _search("gpg"):
        return "gpg"