Mercurial > libervia-backend
comparison src/plugins/plugin_misc_account.py @ 594:e629371a28d3
Fix pep8 support in src/plugins.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 18 Jan 2013 17:55:35 +0100 |
parents | beaf6bec2fcd |
children | 84a6e83157c2 |
comparison
equal
deleted
inserted
replaced
593:70bae685d05c | 594:e629371a28d3 |
---|---|
26 from twisted.python.procutils import which | 26 from twisted.python.procutils import which |
27 from email.mime.text import MIMEText | 27 from email.mime.text import MIMEText |
28 from twisted.mail.smtp import sendmail | 28 from twisted.mail.smtp import sendmail |
29 | 29 |
30 PLUGIN_INFO = { | 30 PLUGIN_INFO = { |
31 "name": "Account Plugin", | 31 "name": "Account Plugin", |
32 "import_name": "MISC-ACCOUNT", | 32 "import_name": "MISC-ACCOUNT", |
33 "type": "MISC", | 33 "type": "MISC", |
34 "protocols": [], | 34 "protocols": [], |
35 "dependencies": [], | 35 "dependencies": [], |
36 "main": "MiscAccount", | 36 "main": "MiscAccount", |
37 "handler": "no", | 37 "handler": "no", |
38 "description": _(u"""SàT account creation""") | 38 "description": _(u"""SàT account creation""") |
39 } | 39 } |
40 | 40 |
41 #You need do adapt the following consts to your server | 41 #You need do adapt the following consts to your server |
42 _REG_EMAIL_FROM = "NOREPLY@example.net" | 42 _REG_EMAIL_FROM = "NOREPLY@example.net" |
43 _REG_EMAIL_SERVER = "localhost" | 43 _REG_EMAIL_SERVER = "localhost" |
44 _REG_ADMIN_EMAIL = "admin@example.net" | 44 _REG_ADMIN_EMAIL = "admin@example.net" |
45 _NEW_ACCOUNT_SERVER = "localhost" | 45 _NEW_ACCOUNT_SERVER = "localhost" |
46 _NEW_ACCOUNT_DOMAIN = "example.net" | 46 _NEW_ACCOUNT_DOMAIN = "example.net" |
47 _NEW_ACCOUNT_RESOURCE = "libervia" | 47 _NEW_ACCOUNT_RESOURCE = "libervia" |
48 _PROSODY_PATH = None #prosody path (where prosodyctl will be executed from), or None to automaticaly find it | 48 _PROSODY_PATH = None # prosody path (where prosodyctl will be executed from), or None to automaticaly find it |
49 _PROSODYCTL = "prosodyctl" | 49 _PROSODYCTL = "prosodyctl" |
50 | 50 |
51 RESERVED = ['libervia'] | 51 RESERVED = ['libervia'] |
52 | 52 |
53 | |
53 class ProsodyRegisterProtocol(protocol.ProcessProtocol): | 54 class ProsodyRegisterProtocol(protocol.ProcessProtocol): |
54 """ Try to register an account with prosody """ | 55 """ Try to register an account with prosody """ |
55 | 56 |
56 def __init__(self, password, deferred = None): | 57 def __init__(self, password, deferred=None): |
57 self.password = password | 58 self.password = password |
58 self.deferred = deferred | 59 self.deferred = deferred |
59 self.data = '' | 60 self.data = '' |
60 | 61 |
61 def connectionMade(self): | 62 def connectionMade(self): |
62 self.transport.write("%s\n%s" % ((self.password.encode('utf-8'),)*2)) | 63 self.transport.write("%s\n%s" % ((self.password.encode('utf-8'), ) * 2)) |
63 self.transport.closeStdin() | 64 self.transport.closeStdin() |
64 | |
65 | 65 |
66 def outReceived(self, data): | 66 def outReceived(self, data): |
67 self.data += data | 67 self.data += data |
68 | 68 |
69 def errReceived(self, data): | 69 def errReceived(self, data): |
85 _prosody_path = _PROSODY_PATH or '' | 85 _prosody_path = _PROSODY_PATH or '' |
86 | 86 |
87 def __init__(self, host): | 87 def __init__(self, host): |
88 info(_(u"Plugin Account initialization")) | 88 info(_(u"Plugin Account initialization")) |
89 self.host = host | 89 self.host = host |
90 host.bridge.addMethod("registerSatAccount", ".plugin", in_sign='sss', out_sign='', method=self._registerAccount, async = True) | 90 host.bridge.addMethod("registerSatAccount", ".plugin", in_sign='sss', out_sign='', method=self._registerAccount, async=True) |
91 if not self._prosody_path: | 91 if not self._prosody_path: |
92 paths = which(_PROSODYCTL) | 92 paths = which(_PROSODYCTL) |
93 if not paths: | 93 if not paths: |
94 error(_("Can't find %s") % (_PROSODYCTL,)) | 94 error(_("Can't find %s") % (_PROSODYCTL, )) |
95 else: | 95 else: |
96 self._prosody_path = dirname(paths[0]) | 96 self._prosody_path = dirname(paths[0]) |
97 info(_('Prosody path found: %s') % (self._prosody_path,)) | 97 info(_('Prosody path found: %s') % (self._prosody_path, )) |
98 | 98 |
99 def _registerAccount(self, email, password, profile): | 99 def _registerAccount(self, email, password, profile): |
100 | 100 |
101 """ | 101 """ |
102 #Password Generation | 102 #Password Generation |
126 | 126 |
127 #XXX: we use "prosodyctl adduser" because "register" doesn't check conflict | 127 #XXX: we use "prosodyctl adduser" because "register" doesn't check conflict |
128 # and just change the password if the account already exists | 128 # and just change the password if the account already exists |
129 d = defer.Deferred() | 129 d = defer.Deferred() |
130 prosody_reg = ProsodyRegisterProtocol(password, d) | 130 prosody_reg = ProsodyRegisterProtocol(password, d) |
131 prosody_exe = join (self._prosody_path, _PROSODYCTL) | 131 prosody_exe = join(self._prosody_path, _PROSODYCTL) |
132 reactor.spawnProcess(prosody_reg, prosody_exe, [prosody_exe, 'adduser', "%s@%s" % (profile, _NEW_ACCOUNT_DOMAIN)], path=self._prosody_path) | 132 reactor.spawnProcess(prosody_reg, prosody_exe, [prosody_exe, 'adduser', "%s@%s" % (profile, _NEW_ACCOUNT_DOMAIN)], path=self._prosody_path) |
133 | 133 |
134 d.addCallback(self._accountCreated, profile, email, password) | 134 d.addCallback(self._accountCreated, profile, email, password) |
135 return d | 135 return d |
136 | 136 |
143 def email_ok(ignore): | 143 def email_ok(ignore): |
144 print ("Account creation email sent to %s" % email) | 144 print ("Account creation email sent to %s" % email) |
145 | 145 |
146 def email_ko(ignore): | 146 def email_ko(ignore): |
147 #TODO: return error code to user | 147 #TODO: return error code to user |
148 error ("Failed to send email to %s" % email) | 148 error("Failed to send email to %s" % email) |
149 | 149 |
150 body = (u"""Welcome to Libervia, a Salut à Toi project part | 150 body = (u"""Welcome to Libervia, a Salut à Toi project part |
151 | 151 |
152 /!\\ WARNING, THIS IS ONLY A TECHNICAL DEMO, DON'T USE THIS ACCOUNT FOR ANY SERIOUS PURPOSE /!\\ | 152 /!\\ WARNING, THIS IS ONLY A TECHNICAL DEMO, DON'T USE THIS ACCOUNT FOR ANY SERIOUS PURPOSE /!\\ |
153 | 153 |
163 follow SàT news: http://www.goffi.org | 163 follow SàT news: http://www.goffi.org |
164 | 164 |
165 Any feedback welcome | 165 Any feedback welcome |
166 | 166 |
167 Cheers | 167 Cheers |
168 Goffi""" % { 'login': login, 'password': password, 'jid':"%s@%s" % (login, _NEW_ACCOUNT_DOMAIN) }).encode('utf-8') | 168 Goffi""" % {'login': login, 'password': password, 'jid': "%s@%s" % (login, _NEW_ACCOUNT_DOMAIN)}).encode('utf-8') |
169 msg = MIMEText(body, 'plain', 'UTF-8') | 169 msg = MIMEText(body, 'plain', 'UTF-8') |
170 msg['Subject'] = 'Libervia account created' | 170 msg['Subject'] = 'Libervia account created' |
171 msg['From'] = _email_from | 171 msg['From'] = _email_from |
172 msg['To'] = email | 172 msg['To'] = email |
173 | 173 |
174 d = sendmail(_email_host, _email_from, email, msg.as_string()) | 174 d = sendmail(_email_host, _email_from, email, msg.as_string()) |
175 d.addCallbacks(email_ok, email_ko) | 175 d.addCallbacks(email_ok, email_ko) |
176 | |
177 #email to the administrator | 176 #email to the administrator |
178 | 177 |
179 body = (u"""New account created: %(login)s [%(email)s]""" % { 'login': login, 'email': email }).encode('utf-8') | 178 body = (u"""New account created: %(login)s [%(email)s]""" % {'login': login, 'email': email}).encode('utf-8') |
180 msg = MIMEText(body, 'plain', 'UTF-8') | 179 msg = MIMEText(body, 'plain', 'UTF-8') |
181 msg['Subject'] = 'Libervia new account created' | 180 msg['Subject'] = 'Libervia new account created' |
182 msg['From'] = _email_from | 181 msg['From'] = _email_from |
183 msg['To'] = _REG_ADMIN_EMAIL | 182 msg['To'] = _REG_ADMIN_EMAIL |
184 | 183 |