Newer
Older
Import / projects / LGN-IP3870 / t / orig / smartmsg.py
from utils import Parser
import ems, config


def NokiaBitmap(data):
	p = Parser(data)
	info = p.get_byte()
	width = p.get_byte()
	height = p.get_byte()
	depth = p.get_byte()
	return ems.Bitmap(width, height, depth, data[4:])

class CLIIcon:
	def __init__(self, data):
		self.raw_data = data
		self.version = ord(data[0])
		self.bitmap = NokiaBitmap(data[1:])
	def get_bitmap(self):
		return self.bitmap

class OperatorLogo:
	def __init__(self, data):
		self.raw_data = data
		self.version = ord(data[0])
		# mobile contry code(2byte), mobile network code(1byte)
		# line feed
		#self.get_byte()
		#self.get_byte()

		#self.get_byte()
		#self.get_byte()
		# 실제 header가 엉기는 경우가 발생하므로 header를
		# 무시하고 뒷 자료부분만 취한다.

		# info/width/height/depth + bitmap
		bitmap_data = '\x00\x48\x0E\x01' + data[-(72*14/8):]
		self.bitmap = NokiaBitmap(bitmap_data)

	def get_bitmap(self):
		return self.bitmap

class Ring:
	def __init__(self, data):
		import os
		ring_file = '/tmp/123356.rng'
		open(ring_file, 'w').write(data)
		imy_file = '/tmp/123456.imy'
		os.system('playimy -convert ' + ring_file + ' ' + imy_file)
		self.imelody = ems.Melody(open(imy_file).read())

	def get_melody(self):
		return self.imelody

def RawData(type, data):
	return ('Raw', (type, data))

def parse_content(type, data):
	if type == 'CLI Icon':
		return type, CLIIcon(data)
	elif type == 'Operator Logo':
		return type, OperatorLogo(data)
	elif type == 'Bitmap':
		return type, Bitmap(data)
	elif type == 'Ring':
		return type, Ring(data)
	elif type == 'Text':
		return type, data
	elif type == 'Multipart':
		return type, Multipart(data)


class Multipart(Parser):
	def __init__(self, data):
		Parser.__init__(self, data)
		self.version = self.get_byte()
		self.contents = []
		while not self.parsing_done():
			content_type = self.get_byte()
			if config.sms_debug:
				print 'Multipart: content type', content_type
			if content_type == 0x00:
				# ISO-8859-1
				content_len = self.get_short()
				if config.sms_debug:
					print 'Multipart: content text len =', content_len
				data = self.get_data(content_len)
				self.contents.append(('Text', data))
			elif content_type == 0x02:
				# Picture
				content_len = self.get_short()
				data = self.get_data(content_len)
				if config.sms_debug:
					print 'Multipart: content picture len=', content_len
				bitmap = NokiaBitmap(data)
				self.contents.append(('Bitmap', bitmap))
			elif content_type == 0x03:
				# Ringing tone
				content_len = self.get_short()
				if config.sms_debug:
					print 'Multipart: content ring len=', content_len
				data = self.get_data(content_len)
				try:
					ring = Ring(data)
					self.contents.append(('Ring', ring))
				except:
					pass
			elif content_type == 0x04:
				# profile name
				content_len = self.get_short()
				data = self.get_data(content_len)
				# Unicode data
			elif content_type == 0x06:
				# Screen Saver
				content_len = self.get_short()
				data = self.get_data(content_len)
				ring = data