예제 #1
0
 def _create_sip_account(self, username, password, email_address, display_name, timezone=None):
     red = '#cc0000'
     if timezone is None and sys.platform != 'win32':
         try:
             timezone = open('/etc/timezone').read().strip()
         except (OSError, IOError):
             try:
                 timezone = '/'.join(os.readlink('/etc/localtime').split('/')[-2:])
             except (OSError, IOError):
                 pass
     enrollment_data = dict(username=username.lower().encode('utf-8'),
                            password=password.encode('utf-8'),
                            email=email_address.encode('utf-8'),
                            display_name=display_name.encode('utf-8'),
                            tzinfo=timezone)
     try:
         settings = SIPSimpleSettings()
         response = urllib2.urlopen(settings.server.enrollment_url, urllib.urlencode(dict(enrollment_data)))
         response_data = cjson.decode(response.read().replace(r'\/', '/'))
         response_data = defaultdict(lambda: None, response_data)
         if response_data['success']:
             from blink import Blink
             try:
                 certificate_path = None
                 passport = response_data['passport']
                 if passport is not None:
                     certificate_path = Blink().save_certificates(response_data['sip_address'], passport['crt'], passport['key'], passport['ca'])
             except (GNUTLSError, IOError, OSError):
                 pass
             account_manager = AccountManager()
             try:
                 account = Account(response_data['sip_address'])
             except DuplicateIDError:
                 account = account_manager.get_account(response_data['sip_address'])
             account.enabled = True
             account.display_name = display_name or None
             account.auth.password = password
             account.sip.outbound_proxy = response_data['outbound_proxy']
             account.nat_traversal.msrp_relay = response_data['msrp_relay']
             account.xcap.xcap_root = response_data['xcap_root']
             account.tls.certificate = certificate_path
             account.server.conference_server = response_data['conference_server']
             account.server.settings_url = response_data['settings_url']
             account.save()
             account_manager.default_account = account
             call_in_gui_thread(self.accept)
         elif response_data['error'] == 'user_exists':
             call_in_gui_thread(self.username_editor.addException, username)
         else:
             call_in_gui_thread(setattr, self.create_status_label, 'value', Status(response_data['error_message'], color=red))
     except (cjson.DecodeError, KeyError):
         call_in_gui_thread(setattr, self.create_status_label, 'value', Status('Illegal server response', color=red))
     except urllib2.URLError, e:
         call_in_gui_thread(setattr, self.create_status_label, 'value', Status('Failed to contact server: %s' % e.reason, color=red))
예제 #2
0
 def _create_sip_account(self,
                         username,
                         password,
                         email_address,
                         display_name,
                         timezone=None):
     red = '#cc0000'
     if timezone is None and sys.platform != 'win32':
         try:
             timezone = open('/etc/timezone').read().strip()
         except (OSError, IOError):
             try:
                 timezone = '/'.join(
                     os.readlink('/etc/localtime').split('/')[-2:])
             except (OSError, IOError):
                 pass
     enrollment_data = dict(username=username.lower().encode('utf-8'),
                            password=password.encode('utf-8'),
                            email=email_address.encode('utf-8'),
                            display_name=display_name.encode('utf-8'),
                            tzinfo=timezone)
     try:
         settings = SIPSimpleSettings()
         response = urllib2.urlopen(settings.server.enrollment_url,
                                    urllib.urlencode(dict(enrollment_data)))
         response_data = cjson.decode(response.read().replace(r'\/', '/'))
         response_data = defaultdict(lambda: None, response_data)
         if response_data['success']:
             try:
                 passport = response_data['passport']
                 if passport is not None:
                     certificate_path = self._save_certificates(
                         response_data['sip_address'], passport['crt'],
                         passport['key'], passport['ca'])
                 else:
                     certificate_path = None
             except (GNUTLSError, IOError, OSError):
                 certificate_path = None
             account_manager = AccountManager()
             try:
                 account = Account(response_data['sip_address'])
             except DuplicateIDError:
                 account = account_manager.get_account(
                     response_data['sip_address'])
             account.enabled = True
             account.display_name = display_name or None
             account.auth.password = password
             account.sip.outbound_proxy = response_data['outbound_proxy']
             account.nat_traversal.msrp_relay = response_data['msrp_relay']
             account.xcap.xcap_root = response_data['xcap_root']
             account.tls.certificate = certificate_path
             account.server.conference_server = response_data[
                 'conference_server']
             account.server.settings_url = response_data['settings_url']
             account.save()
             account_manager.default_account = account
             call_in_gui_thread(self.accept)
         elif response_data['error'] == 'user_exists':
             call_in_gui_thread(self.username_editor.addException, username)
         else:
             call_in_gui_thread(
                 setattr, self.create_status_label, 'value',
                 Status(response_data['error_message'], color=red))
     except (cjson.DecodeError, KeyError):
         call_in_gui_thread(setattr, self.create_status_label, 'value',
                            Status('Illegal server response', color=red))
     except urllib2.URLError, e:
         call_in_gui_thread(
             setattr, self.create_status_label, 'value',
             Status('Failed to contact server: %s' % e.reason, color=red))
예제 #3
0
                call_in_gui_thread(self.accept)
            elif response_data['error'] == 'user_exists':
                call_in_gui_thread(self.username_editor.addException, username)
            else:
                call_in_gui_thread(
                    setattr, self.create_status_label, 'value',
                    Status(response_data['error_message'], color=red))
        except (cjson.DecodeError, KeyError):
            call_in_gui_thread(setattr, self.create_status_label, 'value',
                               Status('Illegal server response', color=red))
        except urllib2.URLError, e:
            call_in_gui_thread(
                setattr, self.create_status_label, 'value',
                Status('Failed to contact server: %s' % e.reason, color=red))
        finally:
            call_in_gui_thread(self.setEnabled, True)

    @staticmethod
    def _save_certificates(sip_address, crt, key, ca):
        crt = crt.strip() + os.linesep
        key = key.strip() + os.linesep
        ca = ca.strip() + os.linesep
        X509Certificate(crt)
        X509PrivateKey(key)
        X509Certificate(ca)
        makedirs(ApplicationData.get('tls'))
        certificate_path = ApplicationData.get(
            os.path.join('tls', sip_address + '.crt'))
        certificate_file = open(certificate_path, 'w')
        os.chmod(certificate_path, 0600)
        certificate_file.write(crt + key)
예제 #4
0
    def _create_sip_account(self, username, password, email_address, display_name, timezone=None):
        red = "#cc0000"
        if timezone is None and sys.platform != "win32":
            try:
                timezone = open("/etc/timezone").read().strip()
            except (OSError, IOError):
                try:
                    timezone = "/".join(os.readlink("/etc/localtime").split("/")[-2:])
                except (OSError, IOError):
                    pass
        enrollment_data = dict(
            username=username.lower().encode("utf-8"),
            password=password.encode("utf-8"),
            email=email_address.encode("utf-8"),
            display_name=display_name.encode("utf-8"),
            tzinfo=timezone,
        )
        try:
            settings = SIPSimpleSettings()
            response = urllib2.urlopen(settings.server.enrollment_url, urllib.urlencode(dict(enrollment_data)))
            response_data = cjson.decode(response.read().replace(r"\/", "/"))
            response_data = defaultdict(lambda: None, response_data)
            if response_data["success"]:
                from blink import Blink

                try:
                    certificate_path = None
                    passport = response_data["passport"]
                    if passport is not None:
                        certificate_path = Blink().save_certificates(
                            response_data["sip_address"], passport["crt"], passport["key"], passport["ca"]
                        )
                except (GNUTLSError, IOError, OSError):
                    pass
                account_manager = AccountManager()
                try:
                    account = Account(response_data["sip_address"])
                except AccountExists:
                    account = account_manager.get_account(response_data["sip_address"])
                account.enabled = True
                account.display_name = display_name
                account.auth.password = password
                account.sip.outbound_proxy = response_data["outbound_proxy"]
                account.nat_traversal.msrp_relay = response_data["msrp_relay"]
                account.xcap.xcap_root = response_data["xcap_root"]
                account.tls.certificate = certificate_path
                account.server.settings_url = response_data["settings_url"]
                account.save()
                account_manager.default_account = account
                call_in_gui_thread(self.accept)
            elif response_data["error"] == "user_exists":
                call_in_gui_thread(self.username_editor.addException, username)
            else:
                call_in_gui_thread(
                    setattr, self.create_status_label, "value", Status(response_data["error_message"], color=red)
                )
        except (cjson.DecodeError, KeyError):
            call_in_gui_thread(setattr, self.create_status_label, "value", Status("Illegal server response", color=red))
        except urllib2.URLError, e:
            call_in_gui_thread(
                setattr, self.create_status_label, "value", Status("Failed to contact server: %s" % e.reason, color=red)
            )
예제 #5
0
                account_manager.default_account = account
                call_in_gui_thread(self.accept)
            elif response_data["error"] == "user_exists":
                call_in_gui_thread(self.username_editor.addException, username)
            else:
                call_in_gui_thread(
                    setattr, self.create_status_label, "value", Status(response_data["error_message"], color=red)
                )
        except (cjson.DecodeError, KeyError):
            call_in_gui_thread(setattr, self.create_status_label, "value", Status("Illegal server response", color=red))
        except urllib2.URLError, e:
            call_in_gui_thread(
                setattr, self.create_status_label, "value", Status("Failed to contact server: %s" % e.reason, color=red)
            )
        finally:
            call_in_gui_thread(self.setEnabled, True)

    @run_in_gui_thread
    def handle_notification(self, notification):
        handler = getattr(self, "_NH_%s" % notification.name, Null)
        handler(notification)

    def _NH_SIPAccountManagerDidAddAccount(self, notification):
        account = notification.data.account
        self.sip_address_editor.addException(notification.data.account.id)

    def _NH_SIPAccountManagerDidRemoveAccount(self, notification):
        self.sip_address_editor.removeException(notification.data.account.id)

    def open_for_add(self):
        self.add_account_button.click()