1# Copyright (C) 2001-2018 by the Free Software Foundation, Inc.
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17"""User description class/structure, for ApprovedAddMember and friends."""
18
19
20from types import UnicodeType
21
22
23
24class UserDesc:
25    def __init__(self, address=None, fullname=None, password=None,
26                 digest=None, lang=None):
27        if address is not None:
28            self.address = address
29        if fullname is not None:
30            self.fullname = fullname
31        if password is not None:
32            self.password = password
33        if digest is not None:
34            self.digest = digest
35        if lang is not None:
36            self.language = lang
37
38    def __iadd__(self, other):
39        if getattr(other, 'address', None) is not None:
40            self.address = other.address
41        if getattr(other, 'fullname', None) is not None:
42            self.fullname = other.fullname
43        if getattr(other, 'password', None) is not None:
44            self.password = other.password
45        if getattr(other, 'digest', None) is not None:
46            self.digest = other.digest
47        if getattr(other, 'language', None) is not None:
48            self.language = other.language
49        return self
50
51    def __repr__(self):
52        address = getattr(self, 'address', 'n/a')
53        fullname = getattr(self, 'fullname', 'n/a')
54        password = getattr(self, 'password', 'n/a')
55        digest = getattr(self, 'digest', 'n/a')
56        if digest == 0:
57            digest = 'no'
58        elif digest == 1:
59            digest = 'yes'
60        language = getattr(self, 'language', 'n/a')
61        # Make sure fullname and password are encoded if they're strings
62        if isinstance(fullname, UnicodeType):
63            fullname = fullname.encode('ascii', 'replace')
64        if isinstance(password, UnicodeType):
65            password = password.encode('ascii', 'replace')
66        return '<UserDesc %s (%s) [%s] [digest? %s] [%s]>' % (
67            address, fullname, password, digest, language)
68