[go: up one dir, main page]

File: pidgin.py

package info (click to toggle)
keysync 0.2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,388 kB
  • ctags: 230
  • sloc: python: 2,102; xml: 802; sh: 143; makefile: 4
file content (117 lines) | stat: -rw-r--r-- 4,318 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''a module for reading and writing Pidgin's OTR key data'''

from __future__ import print_function
import os
import sys
from bs4 import BeautifulSoup

if __name__ == '__main__':
    sys.path.insert(0, "../") # so the main() test suite can find otrapps module
import otrapps.util
from otrapps.otr_private_key import OtrPrivateKeys
from otrapps.otr_fingerprints import OtrFingerprints

class PidginProperties():

    if sys.platform == 'win32':
        path = os.path.join(os.environ.get('APPDATA'), '.purple')
    else:
        path = os.path.expanduser('~/.purple')
    accountsfile = 'accounts.xml'
    keyfile = 'otr.private_key'
    fingerprintfile = 'otr.fingerprints'

    @staticmethod
    def _get_resources(settingsdir):
        '''parse out the XMPP Resource from every Pidgin account'''
        resources = dict()
        accountsfile = os.path.join(settingsdir, PidginProperties.accountsfile)
        if not os.path.exists(accountsfile):
            print('Pidgin WARNING: No usable accounts.xml file found, add XMPP Resource to otr.private_key by hand!')
            return resources
        xml = ''
        for line in open(accountsfile, 'r').readlines():
            xml += line
        for e in BeautifulSoup(xml)(text='prpl-jabber'):
            pidginname = e.parent.parent.find('name').contents[0].split('/')
            name = pidginname[0]
            if len(pidginname) == 2:
                resources[name] = pidginname[1]
            else:
                # Pidgin requires an XMPP Resource, even if its blank
                resources[name] = ''
        return resources

    @staticmethod
    def parse(settingsdir=None):
        if settingsdir == None:
            settingsdir = PidginProperties.path

        kf = os.path.join(settingsdir, PidginProperties.keyfile)
        if os.path.exists(kf):
            keydict = OtrPrivateKeys.parse(kf)
        else:
            keydict = dict()

        fpf = os.path.join(settingsdir, PidginProperties.fingerprintfile)
        if os.path.exists(fpf):
            otrapps.util.merge_keydicts(keydict, OtrFingerprints.parse(fpf))

        resources = PidginProperties._get_resources(settingsdir)
        for name, key in keydict.items():
            if key['protocol'] == 'prpl-jabber' \
                    and 'x' in key.keys() \
                    and name in resources.keys():
                key['resource'] = resources[name]

        return keydict

    @staticmethod
    def write(keydict, savedir):
        if not os.path.exists(savedir):
            raise Exception('"' + savedir + '" does not exist!')

        kf = os.path.join(savedir, PidginProperties.keyfile)
        # Pidgin requires the XMPP resource in the account name field of the
        # OTR private keys file, so fetch it from the existing account info
        if os.path.exists(os.path.join(savedir, PidginProperties.accountsfile)):
            accountsdir = savedir
        elif os.path.exists(os.path.join(PidginProperties.path,
                                         PidginProperties.accountsfile)):
            accountsdir = PidginProperties.path
        else:
            raise Exception('Cannot find "' + PidginProperties.accountsfile
                            + '" in "' + savedir + '"')
        resources = PidginProperties._get_resources(accountsdir)
        OtrPrivateKeys.write(keydict, kf, resources=resources)

        accounts = []
        # look for all private keys and use them for the accounts list
        for name, key in keydict.items():
            if 'x' in key:
                accounts.append(name)
        fpf = os.path.join(savedir, PidginProperties.fingerprintfile)
        OtrFingerprints.write(keydict, fpf, accounts, resources=resources)


if __name__ == '__main__':

    import pprint
    import shutil

    print('Pidgin stores its files in ' + PidginProperties.path)

    if len(sys.argv) == 2:
        settingsdir = sys.argv[1]
    else:
        settingsdir = '../tests/pidgin'

    keydict = PidginProperties.parse(settingsdir)
    pprint.pprint(keydict)

    if not os.path.exists(os.path.join('/tmp', PidginProperties.accountsfile)):
        shutil.copy(os.path.join(settingsdir, PidginProperties.accountsfile),
                    '/tmp')
    PidginProperties.write(keydict, '/tmp')