📄 pyfetion1.py.py
字号:
#!/usr/bin/env python# -*- coding: utf-8 -*-#Using GPL v2#Author: cocobear.cn@gmail.com#Version:0.1import urllibimport urllib2import sys,reimport binasciiimport hashlibimport socketimport osfrom hashlib import md5from hashlib import sha1from uuid import uuid1FetionVer = "2008"#"SIPP" USED IN HTTP CONNECTIONFetionSIPP= "SIPP"FetionNavURL = "nav.fetion.com.cn"FetionConfigURL = "http://nav.fetion.com.cn/nav/getsystemconfig.aspx"FetionConfigXML = """<config><user mobile-no="%s" /><client type="PC" version="3.2.0540" platform="W5.1" /><servers version="0" /><service-no version="0" /><parameters version="0" /><hints version="0" /><http-applications version="0" /><client-config version="0" /></config>"""FetionLoginXML = """<args><device type="PC" version="0" client-version="3.2.0540" /><caps value="simple-im;im-session;temp-group;personal-group" /><events value="contact;permission;system-message;personal-group" /><user-info attributes="all" /><presence><basic value="%s" desc="" /></presence></args>"""proxy_info = False#uncomment below line if you need proxy"""proxy_info = {'user' : '', 'pass' : '', 'host' : '218.249.83.87', 'port' : 8080 }"""debug = "file"COL_NONE = ""COL_RED = "" if os.name == 'posix': sys_encoding = 'utf-8' if debug != "file": COL_NONE = "\033[0m" COL_RED = "\033[0;31;48m" else: sys_encoding = 'cp936'class PyFetionException(Exception): """Base class for all exceptions raised by this module."""class PyFetionSocketError(PyFetionException): """any socket error""" def __init__(self, msg): self.PyFetion_error = msg self.args = ( msg)class PyFetionResponseException(PyFetionException): """Base class for all exceptions that include SIPC/HTTP error code. """ def __init__(self, code, msg): self.PyFetion_code = code self.PyFetion_error = msg self.args = (code, msg)class PyFetionAuthError(PyFetionResponseException): """Authentication error. Your password error. """class PyFetionSupportError(PyFetionResponseException): """Support error. Your phone number don't support fetion. """class PyFetionRegisterError(PyFetionResponseException): """RegisterError. """class PyFetionSendError(PyFetionResponseException): """Send SMS error """class PyFetion(): __config_data = "" __sipc_url = "" __sipc_proxy = "" __sid = "" mobile_no = "" passwd = "" login_type = "" login_ok = False def __init__(self,mobile_no,passwd,login_type="TCP"): self.mobile_no = mobile_no self.passwd = passwd self.login_type = login_type self.__get_system_config() self.__set_system_config() def login(self,see=False): (self.__ssic,self.__domain) = self.__get_uri() self.__see = see try: self.__register(self.__ssic,self.__domain) except PyFetionRegisterError,e: d_print("Register Failed!") return self.login_ok = True def get_offline_msg(self): self.__SIPC.get("") def add(self,who): self.__SIPC.get("INFO","AddBuddy",who) response = self.__SIPC.send() code = self.__SIPC.get_code(response) if code == 521: d_print("Aleady added.") elif code == 522: d_print("Mobile NO. Don't Have Fetion") self.__SIPC.get("INFO","AddMobileBuddy",who) response = self.__SIPC.send() def get_personal_info(self): self.__SIPC.get("INFO","GetPersonalInfo") self.__SIPC.send() def get_info(self,who=None): """get one's contact info. who should be uri. """ if who == None: who = self.__uri self.__SIPC.get("INFO","GetContactsInfo",who) response = self.__SIPC.send() return response def get_contact_list(self): self.__SIPC.get("INFO","GetContactList") response = self.__SIPC.send() return response def get_uri(self,who): if who == self.mobile_no or who in self.__uri: return self.__uri if who.startswith("sip"): return who l = self.get_contact_list() all = re.findall('uri="(.+?)" ',l) #Get uri from contact list, compare one by one #I can't get other more effect way. for uri in all: #who is the fetion number. if who in uri: return uri ret = self.get_info(uri) no = re.findall('mobile-no="(.+?)" ',ret) #if people show you his mobile number. if no: #who is the mobile number. if no[0] == who: d_print(('who',),locals()) return uri return None def send_msg(self,msg,to=None,flag="SENDMSG"): """see send_sms. if someone's fetion is offline, msg will send to phone, the same as send_sms. """ if not to: to = self.__uri #Fetion now can send use mobile number(09.02.23) #like tel: 13888888888 #but not in sending to PC elif flag != "SENDMSG" and len(to) == 11 and to.isdigit(): to = "tel:"+to else: to = self.get_uri(to) if not to: return -1 self.__SIPC.get(flag,to,msg) response = self.__SIPC.send() code = self.__SIPC.get_code(response) if code == 280: d_print("Send sms/msg OK!") else: d_print(('code',),locals()) def send_sms(self,msg,to=None,long=False): """send sms to someone, if to is None, send self. if long is True, send long sms.(Your phone should support.) to can be mobile number or fetion number """ if long: return self.send_msg(msg,to,"SENDCatSMS") else: return self.send_msg(msg,to,"SENDSMS") def send_schedule_sms(self,msg,time,to=None): if not to: to = self.__uri elif len(to) == 11 and to.isdigit(): to = "tel:"+to else: to = self.get_uri(to) if not to: return -1 self.__SIPC.get("SSSetScheduleSms",msg,time,to) response = self.__SIPC.send() code = self.__SIPC.get_code(response) if code == 486: d_print("Busy Here") return None if code == 200: id = re.search('id="(\d+)"',response).group(1) d_print(('id',),locals(),"schedule_sms id") return id def __register(self,ssic,domain): self.__SIPC = SIPC(self.__sid,self.__domain,self.passwd,self.login_type,self.__http_tunnel,self.__ssic,self.__sipc_proxy,self.__see) response = "" for step in range(1,3): self.__SIPC.get("REG",step,response) response = self.__SIPC.send() code = self.__SIPC.get_code(response) if code == 200: d_print("register successful.") else: raise PyFetionRegisterError(code,response) def __get_system_config(self): global FetionConfigURL global FetionConfigXML url = FetionConfigURL body = FetionConfigXML % self.mobile_no d_print(('url','body'),locals()) self.__config_data = http_send(url,body).read() def __set_system_config(self): sipc_url = re.search("<ssi-app-sign-in>(.*)</ssi-app-sign-in>",self.__config_data).group(1) sipc_proxy = re.search("<sipc-proxy>(.*)</sipc-proxy>",self.__config_data).group(1) http_tunnel = re.search("<http-tunnel>(.*)</http-tunnel>",self.__config_data).group(1) d_print(('sipc_url','sipc_proxy','http_tunnel'),locals()) self.__sipc_url = sipc_url self.__sipc_proxy = sipc_proxy self.__http_tunnel= http_tunnel def __get_uri(self): url = self.__sipc_url+"?mobileno="+self.mobile_no+"&pwd="+urllib.quote(self.passwd) d_print(('url',),locals()) ret = http_send(url,login=True) header = str(ret.info()) body = ret.read() ssic = re.search("ssic=(.*);",header).group(1) sid = re.search("sip:(.*)@",body).group(1) uri = re.search('uri="(.*)" mobile-no',body).group(1) status = re.search('user-status="(\d+)"',body).group(1) domain = "fetion.com.cn" d_print(('ssic','sid','uri','status','domain'),locals(),"Get SID OK") self.__sid = sid self.__uri = uri return (ssic,domain)class SIPC(): global FetionVer global FetionSIPP global FetionLoginXML header = "" body = "" content = "" code = "" ver = "SIP-C/2.0" ID = 1 sid = "" domain = "" passwd = "" see = True __http_tunnel = "" def __init__(self,sid,domain,passwd,login_type,http_tunnel,ssic,sipc_proxy,see): self.sid = sid self.domain = domain self.passwd = passwd self.login_type = login_type self.domain = domain self.sid = sid self.__seq = 1 self.__sipc_proxy = sipc_proxy self.__see = see if self.login_type == "HTTP":
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -