예제 #1
0
	def runTest( self ):
		x = SipUri()

		self.assertRaises( SipException, self.TestUri, 'sip:' )

		self.TestUri( 'tel:5551212;phone-context=home' )
		self.TestUri( 'sip:host' )
		self.TestUri( 'sip:host:8000' )
		self.TestUri( 'sip:user@host' )
		self.TestUri( ' sip:user@host', 'sip:user@host' )
		self.TestUri( 'sip:user@host ', 'sip:user@host' )
		self.TestUri( ' sip:user@host ', 'sip:user@host' )
		self.TestUri( 'sip:user:password@host' )
		self.TestUri( 'sip:user:password@host:5060' )
		self.TestUri( 'sip:user:pass@host:5060;p0;kp0=a;p1;kp1=b;p2;kp2=c', 'sip:user:pass@host:5060;p0;p1;p2;kp0=a;kp1=b;kp2=c' )
		self.TestUri( 'sip:user:pass@host:5060?h0&h1&h2' )
		self.TestUri( 'sip:user:pass@host:5060?hp0=a&hp1=b&hp2=c', 'sip:user:pass@host:5060?hp2=c&hp1=b&hp0=a' )
		self.TestUri( 'sip:user:pass@host:5060?h0&hp0=a&h1&hp1=b&h2&hp2=c', 'sip:user:pass@host:5060?h0&h1&h2&hp2=c&hp1=b&hp0=a' )
		self.TestUri( 'sip:user:pass@host:5060;p0;kp0=a;p1;kp1=b;p2;kp2=c?h0&hp0=a&h1&hp1=b&h2&hp2=c', 'sip:user:pass@host:5060;p0;p1;p2;kp0=a;kp1=b;kp2=c?h0&h1&h2&hp2=c&hp1=b&hp0=a' )
		self.TestUri( 'sips:user:pass@host:5060;p0;kp0=a;p1;kp1=b;p2;kp2=c?h0&hp0=a&h1&hp1=b&h2&hp2=c', 'sips:user:pass@host:5060;p0;p1;p2;kp0=a;kp1=b;kp2=c?h0&h1&h2&hp2=c&hp1=b&hp0=a' )

		uri = Uri( 'sip:host:8000' )
		uri.hostPort = 'cave:5060'
		assert( uri.hostPort == 'cave:5060' )
		assert( uri.host == 'cave' )
		assert( uri.port == 5060 )
		uri.userPassword = '******'
		assert( uri.userPassword == 'josh:icecream' )
		assert( uri.user == 'josh' )
		assert( uri.password == 'icecream' )
예제 #2
0
class InfoElement(IElement):
	def __init__( self, value = None ):
		super( InfoElement, self ).__init__( value )
		self.uri = None
		self.params = []
		self.kparams = {}
		self.__dict__['__initialized'] = True

		if value:
			self.parse( value )

	def __cmp__( self, other ):
		if self.uri != other.uri:
			return self.uri.__cmp__( other.uri )

		n = Params.__cmp__( self.params, self.kparams, other.params, other.kparams )
		if n != 0:
			return n

		return 0

	def parse( self, value ):
		lvalue = Params.parse( self.params, self.kparams, value )
		if lvalue.startswith( '<' ) and lvalue.endswith( '>' ):
			lvalue = lvalue.lstrip( '<' )
			lvalue = lvalue.rstrip( '>' )
			self.uri = Uri( uri )
		else:
			SipException( 'InfoElement, ' + str(self.value) + ', does not have opening or closing brackets.' )

	def build( self ):
		s = MutableString()

		if self.uri:
			s += '<'
			s += str(self.uri)
			s += '>'

		s += Params.build( self.params, self.kparams )

		return str(s)

	def validate( self, thorough = False  ):
		self.uri.validate( thorough )

	def dump( self ):
		s = MutableString()

		s += '['
		s += 'raw:\'' + str(self.raw) + '\'' + ', '
		s += 'uri:\'' + str(self.uri) + '\'' + ', '
		s += 'params:\'' + str(self.params) + '\'' + ', '
		s += 'kparams:\'' + str(self.kparams) + '\'' + ', '
		s += ']'

		return str(s)
예제 #3
0
	def parse( self, value ):
		lvalue = Params.parse( self.params, self.kparams, value )
		if lvalue.startswith( '<' ) and lvalue.endswith( '>' ):
			lvalue = lvalue.lstrip( '<' )
			lvalue = lvalue.rstrip( '>' )
			self.uri = Uri( uri )
		else:
			SipException( 'InfoElement, ' + str(self.value) + ', does not have opening or closing brackets.' )
예제 #4
0
	def parse( self, value ):
		name = None
		uri = None

		# Try to parse using angle-brackets.
		n = value.find( '>' )
		if n >= 0:
			dummy = Params.parse( self.params, self.kparams, value[n+1:] )

			nameUri = value[:n]
			n = nameUri.find( '<' )
			if n < 0:
				raise SipException( 'Failed to find the matching left-angle-bracket for address, ' + value + '.' )

			uri = nameUri[n+1:]
			name = nameUri[:n]
		else:
			nameUri = Params.parse( self.params, self.kparams, value )

			uriIndex = 0
			nameUriArray = nameUri.split( '"' )
			if len(nameUriArray) == 1:
				pass
			elif len(nameUriArray) == 3:
				name = nameUriArray[1]
				uriIndex = 2
			else:
				raise SipException( 'Failed to parse address, ' + value + '.' )

			uri = nameUriArray[uriIndex]

		# Display Name
		if name:
			name = name.strip( ' ' )
			self.name = name.strip( '"' )

		# URI
		if uri:
			self.uri = Uri( uri )
		else:
			raise SipException( 'Failed to find URI in address, ' + value + '.' )
예제 #5
0
class Address(IElement):
	def __init__( self, value = None ):
		super( Address, self ).__init__( value )
		self.name =	None
		self.uri = None
		self.params = []
		self.kparams = {}
		self.__dict__['__initialized'] = True

		if value:
			self.parse( value )

	def __cmp__( self, other ):
		if self.name != other.name:
			return self.name.__cmp__( other.name )

		if self.uri != other.uri:
			return self.uri.__cmp__( other.uri )

#FIXME:DEBUG:		return super( Address, self ).__cmp__( other )

		n = Params.__cmp__( self.params, self.kparams, other.params, other.kparams )
		if n != 0:
			return n

		return 0

	def parse( self, value ):
		name = None
		uri = None

		# Try to parse using angle-brackets.
		n = value.find( '>' )
		if n >= 0:
			dummy = Params.parse( self.params, self.kparams, value[n+1:] )

			nameUri = value[:n]
			n = nameUri.find( '<' )
			if n < 0:
				raise SipException( 'Failed to find the matching left-angle-bracket for address, ' + value + '.' )

			uri = nameUri[n+1:]
			name = nameUri[:n]
		else:
			nameUri = Params.parse( self.params, self.kparams, value )

			uriIndex = 0
			nameUriArray = nameUri.split( '"' )
			if len(nameUriArray) == 1:
				pass
			elif len(nameUriArray) == 3:
				name = nameUriArray[1]
				uriIndex = 2
			else:
				raise SipException( 'Failed to parse address, ' + value + '.' )

			uri = nameUriArray[uriIndex]

		# Display Name
		if name:
			name = name.strip( ' ' )
			self.name = name.strip( '"' )

		# URI
		if uri:
			self.uri = Uri( uri )
		else:
			raise SipException( 'Failed to find URI in address, ' + value + '.' )

	def build( self ):
		s = MutableString()

		if self.name:
			s += '"'
			s += self.name
			s += '"'

		if self.uri:
			s += '<'
			s += str(self.uri)
			s += '>'

		s += Params.build( self.params, self.kparams )

		return str(s)

	def validate( self, thorough = False  ):
		self.uri.validate( thorough )

	def dump( self ):
		s = MutableString()

		s += '['
		s += 'raw:\'' + str(self.raw) + '\'' + ', '
		s += 'name:\'' + str(self.name) + '\'' + ', '
		s += 'uri:\'' + str(self.uri) + '\'' + ', '
		s += 'params:\'' + str(self.params) + '\'' + ', '
		s += 'kparams:\'' + str(self.kparams) + '\'' + ', '
		s += ']'

		return str(s)