comparison frontends/src/jp/cmd_invitation.py @ 2215:5831e8fcc5f8

jp (invitation): invitation command first draft
author Goffi <goffi@goffi.org>
date Wed, 29 Mar 2017 19:42:44 +0200
parents
children 0d27d95652a7
comparison
equal deleted inserted replaced
2214:4e06cd44e667 2215:5831e8fcc5f8
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 import base
22 from sat.core.i18n import _
23 from sat_frontends.jp.constants import Const as C
24 from functools import partial
25
26 __commands__ = ["Invitation"]
27
28
29 class Create(base.CommandBase):
30
31 def __init__(self, host):
32 base.CommandBase.__init__(self, host, 'create', use_profile=False, use_output=C.OUTPUT_DICT, help=_(u'create and send an invitation'))
33 self.need_loop=True
34
35 def add_parser_options(self):
36 self.parser.add_argument("-j", "--jid", type=base.unicode_decoder, default='', help='jid of the invitee (default: generate one)')
37 self.parser.add_argument("-P", "--password", type=base.unicode_decoder, default='', help='password of the invitee profile/XMPP account (default: generate one)')
38 self.parser.add_argument("-n", "--name", type=base.unicode_decoder, default='', help='name of the invitee')
39 self.parser.add_argument("-N", "--host-name", type=base.unicode_decoder, default='', help='name of the host')
40 self.parser.add_argument("-e", "--email", type=base.unicode_decoder, default='', help='email to send the invitation to (if --no-email is set, email will just be saved)')
41 self.parser.add_argument("--no-email", action="store_true", help='do NOT send invitation email')
42 self.parser.add_argument("-l", "--lang", type=base.unicode_decoder, default='', help='main language spoken by the invitee')
43 self.parser.add_argument("-u", "--url", type=base.unicode_decoder, default='', help='template to construt the URL')
44 self.parser.add_argument("-s", "--subject", type=base.unicode_decoder, default='', help='subject of the invitation email (default: generic subject)')
45 self.parser.add_argument("-b", "--body", type=base.unicode_decoder, default='', help='body of the invitation email (default: generic body)')
46 self.parser.add_argument("-x", "--extra", metavar=('KEY', 'VALUE'), type=base.unicode_decoder, action='append', nargs=2, default=[], help='extra data to associate with invitation/invitee')
47 self.parser.add_argument("-p", "--profile", type=base.unicode_decoder, default='', help="profile doing the invitation (default: don't associate profile")
48
49 def invitationCreateCb(self, invitation_data):
50 self.output(invitation_data)
51 self.host.quit(C.EXIT_OK)
52
53 def invitationCreateEb(self, failure_):
54 self.disp(u"can't create invitation: {reason}".format(
55 reason=failure_), error=True)
56 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
57
58 def start(self):
59 extra = dict(self.args.extra)
60 email = self.args.email
61 if self.args.no_email:
62 if email:
63 extra['email'] = email
64 else:
65 if not email:
66 self.parser.error(_(u'you need to specify an email address to send email invitation'))
67
68 self.host.bridge.invitationCreate(
69 self.args.jid,
70 self.args.password,
71 self.args.name,
72 self.args.host_name,
73 email,
74 self.args.lang,
75 self.args.url,
76 self.args.subject,
77 self.args.body,
78 extra,
79 self.args.profile,
80 callback=self.invitationCreateCb,
81 errback=self.invitationCreateEb)
82
83
84 class Get(base.CommandBase):
85
86 def __init__(self, host):
87 base.CommandBase.__init__(self, host, 'get', use_profile=False, use_output=C.OUTPUT_DICT, help=_(u'get invitation data'))
88 self.need_loop=True
89
90 def add_parser_options(self):
91 self.parser.add_argument("id", type=base.unicode_decoder,
92 help=_(u"invitation UUID"))
93 self.parser.add_argument("-j", "--with-jid", action="store_true", help=_(u"start profile session and retrieve jid"))
94
95 def output_data(self, data, jid_=None):
96 if jid_ is not None:
97 data['jid'] = jid_
98 self.output(data)
99 self.host.quit()
100
101 def invitationGetCb(self, invitation_data):
102 if self.args.with_jid:
103 profile = invitation_data[u'guest_profile']
104 def session_started(dummy):
105 self.host.bridge.asyncGetParamA(
106 u'JabberID',
107 u'Connection',
108 profile_key=profile,
109 callback=lambda jid_: self.output_data(invitation_data, jid_),
110 errback=partial(self.errback,
111 msg=_(u"can't retrieve jid: {}"),
112 exit_code=C.EXIT_BRIDGE_ERRBACK))
113
114 self.host.bridge.profileStartSession(
115 invitation_data[u'password'],
116 profile,
117 callback=session_started,
118 errback=partial(self.errback,
119 msg=_(u"can't start session: {}"),
120 exit_code=C.EXIT_BRIDGE_ERRBACK))
121 else:
122 self.output_data(invitation_data)
123
124 def start(self):
125 self.host.bridge.invitationGet(
126 self.args.id,
127 callback=self.invitationGetCb,
128 errback=partial(self.errback,
129 msg=_(u"can't get invitation data: {}"),
130 exit_code=C.EXIT_BRIDGE_ERRBACK))
131
132
133 class Invitation(base.CommandBase):
134 subcommands = (Create, Get)
135
136 def __init__(self, host):
137 super(Invitation, self).__init__(host, 'invitation', use_profile=False, help=_(u'invitation of user(s) without XMPP account'))