首页 > 代码库 > 08年用Python写的一个塞班S60V3上免费发短信的小程序

08年用Python写的一个塞班S60V3上免费发短信的小程序

#-*-coding:utf-8-*-
import appuifw2 as appuifw
import e32
import urllib
import key_codes
import uitricks
import contacts
import messaging
import envy
import socket
import inbox
import time
def cn(str):
    return str.decode("utf-8")

class main:

    def __init__(self):
        self.ap=socket.access_point(1)
        socket.set_default_access_point(self.ap)
        self.names = []
        self.numbers=[]
        self.number=appuifw.Text()
        self.msg=appuifw.Text(0)
        self.box = inbox.Inbox()
        self.box.bind(self.receivemsg)
        self.db = contacts.open()
        for i in self.db:
            self.names.append(self.db[i].title)
            num=self.db[i].find('mobile_number')[0].value
            if len(num)==11:
                self.numbers.append(num)
            elif num[:3]==cn("+86"):
                self.numbers.append(num[3:])
            elif num[:3]==cn("125"):
                self.numbers.append(num[5:])
            else :
                self.numbers.append(num)
        self.names=self.names[2:]
        self.numbers=self.numbers[2:]
        self.number.set_case(appuifw.ELowerCase)

    def hide(self):
        envy.set_app_hidden(1)
        e32.start_exe('z:\\sys\\bin\\phone.exe',"")
        self.writemessage()            
    def send(self,snumber,smessage):
    
        for i in snumber:
            if i=='':
                continue
            try:
                url='http://fetionapi.appspot.com/api/?from=15996945532&pw=459320480&to=%s&msg=%s' %(urllib.quote(i),urllib.quote(smessage))
                answer=urllib.urlopen(url)
                if answer.read()[:3]!='Yes':
                    appuifw.note(cn("网络发送失败,启用短信模式"),"conf",1)
                    messaging.sms_send(i,cn(message),'UCS2')
            except:
                appuifw.note(cn("网络连接失败,启用短信模式"),"conf",1)
                messaging.sms_send(i,cn(smessage),'UCS2')
                continue
		
    
    def search_contact(self):
        
        index=appuifw.multi_selection_list(self.names,style='checkbox',search_field=1)
        for i in index:
            phonenumber=self.numbers[i]
            if   self.number.len()==0:
                self.number.add(phonenumber+';')
            elif self.number.get()[-1]==';':
                self.number.add(phonenumber+';')
            else:
                self.number.add(';'+phonenumber+';')
             
		

    def addreciver(self):
        
        appuifw.app.body=self.number
        appuifw.app.title=cn("添加联系人")
        appuifw.app.menu=[(cn("立即发送"),self.sendnow),(cn("延迟发送"),self.senddelay),(cn("添加联系人"),self.search_contact),(cn("退出程序"),self.app_exit)]
        appuifw.app.body.bind(key_codes.EKeySelect,self.search_contact)
        appuifw.app.body.bind(key_codes.EKeyYes,self.sendnow)
        appuifw.app.exit_key_handler=self.writemessage
        uitricks.set_text(cn("返回"),3009)
       
		
    def writemessage(self):
     
        appuifw.app.body=self.msg
        appuifw.app.title=cn("写短息")
        appuifw.app.menu=[(cn("添加联系人"),self.addreciver),(cn("退出程序"),self.app_exit)]
        appuifw.app.exit_key_handler=self.hide
        self.msg.bind(key_codes.EKeySelect,self.addreciver)
        self.msg.bind(key_codes.EKeyYes,self.addreciver)
        uitricks.set_text(cn("隐藏"),3009)
      
    def sendnow(self):
        if len(self.number.get())==0:
	    appuifw.note(cn("号码不能为空"),'error',1)
	    return
        self.hide()
        sendto=self.getsendnumber()
        self.msg.add(cn("\n"))
        sendmsg=self.msg.get().encode("utf-8")
        self.msg.clear()
        self.send(sendto,sendmsg)
        self.ap.stop()

    def senddelay(self):
        if len(self.number.get())==0:
            appuifw.note(cn("号码不能为空"),'error',1)
            return
        after_time=appuifw.query(cn("输入时延:"),'time')    
        self.hide()
        sendto=self.getsendnumber()
        self.msg.add(cn("\n"))
        sendmsg=self.msg.get().encode("utf-8")
        self.msg.clear()
        e32.ao_sleep(after_time,lambda:self.send(sendto,sendmsg))
        self.ap.stop()
        
    def getsendnumber(self):
        sendnumber=self.number.get().split(';')
        if len(sendnumber[0])<11:
            for j in range(0,len(self.names)-1):
                 if self.names[j]==sendnumber[0]:
                     sendnumber[0]=self.numbers[j]
        self.number.clear()
        return sendnumber
                         
    def app_exit(self):
        appuifw.app.set_exit()
   
    def receivemsg(self,msgid):
        envy.set_app_hidden(0)
        box = inbox.Inbox()
        msgcontent=box.content(msgid)
        msgaddress=box.address(msgid)
        msgtime=box.time(msgid)
        box.set_unread(msgid,0)
        rmsg=appuifw.Text()
        rmsg.add(cn("发件人:%s\n\n%s\n") %(msgaddress,msgcontent))
        rtime=time.strftime("时间:%m-%d-%X",time.localtime(msgtime)).decode("utf-8")
        rmsg.add(rtime)
        rmsg.read_only=1
        appuifw.app.body=rmsg
        appuifw.app.title=cn("收件箱")
        try:
            appuifw.app.menu=[(cn("回复"),lambda:self.reply(msgaddress)),(cn("转发"),lambda:self.transform(msgcontent))]
        except :
            appuifw.note(cn("oh"))
            appuifw.query(traceback.print_exc(),"query")
        e32.start_exe('C:\\sys\\bin\\mysms_0x11111111.exe',"")

    def reply(self,rnumber):
        try: 
            self.number.add(rnumber)
            self.writemessage()
        except Exception,data:
            print Exception,",",data
    def transform(self,rmsg):
        self.msg.add(rmsg)
        self.addreciver()
    

if __name__ == '__main__':
    start=main()
    envy.set_app_system(1)
    e32.ao_sleep(0,start.hide)

08年用Python写的一个塞班S60V3上免费发短信的小程序