Advertisement
Guest User

Whatsapee

a guest
Jan 11th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.17 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import argparse, sys, os, csv
  4. from Yowsup.Common.utilities import Utilities
  5. from Yowsup.Common.debugger import Debugger
  6. from Yowsup.Common.constants import Constants
  7. from Yowsup.Registration.v2.existsrequest import WAExistsRequest as WAExistsRequestV2
  8. from Yowsup.Registration.v2.coderequest import WACodeRequest as WACodeRequestV2
  9. from Yowsup.Registration.v2.regrequest import WARegRequest as WARegRequestV2
  10. from Yowsup.Contacts.contacts import WAContactsSyncRequest
  11. from Yowsup.connectionmanager import YowsupConnectionManager
  12.  
  13. import threading,time, base64
  14.  
  15.  
  16. DEFAULT_CONFIG = "yowsup.conf"
  17. COUNTRIES_CSV = "countries.csv"
  18.  
  19. # Message execution
  20. def executeMessage(rid, message):
  21.     if not (rid in whitelist):
  22.         return
  23.     if message == "Hi":
  24.         message_send(rid, "Hi \n I'm WhatsAPee")
  25.     return
  26.  
  27. def dissectPhoneNumber(phoneNumber):
  28.     try:
  29.         with open(COUNTRIES_CSV, 'r') as csvfile:
  30.             reader = csv.reader(csvfile, delimiter=',')
  31.             for row in reader:
  32.                 if len(row) == 3:
  33.                     country, cc, mcc = row
  34.                 else:
  35.                     country,cc = row
  36.                     mcc = "000"
  37.                 try:
  38.                     if phoneNumber.index(cc) == 0:
  39.                         print("Detected cc: %s"%cc)
  40.                         return (cc, phoneNumber[len(cc):])
  41.  
  42.                 except ValueError:
  43.                     continue
  44.                
  45.     except:
  46.         pass
  47.     return False
  48.  
  49. def getCredentials(config = DEFAULT_CONFIG):
  50.     if os.path.isfile(config):
  51.         print("Found config")
  52.         f = open(config)
  53.        
  54.         phone = ""
  55.         idx = ""
  56.         pw = ""
  57.         cc = ""
  58.        
  59.         try:
  60.             for l in f:
  61.                 line = l.strip()
  62.                 if len(line) and line[0] not in ('#',';'):
  63.                    
  64.                     prep = line.split('#', 1)[0].split(';', 1)[0].split('=', 1)
  65.                    
  66.                     varname = prep[0].strip()
  67.                     val = prep[1].strip()
  68.                    
  69.                     if varname == "phone":
  70.                         phone = val
  71.                     elif varname == "id":
  72.                         idx = val
  73.                     elif varname =="password":
  74.                         pw =val
  75.                     elif varname == "cc":
  76.                         cc = val
  77.  
  78.             return (cc, phone, idx, pw);
  79.         except:
  80.             pass
  81.     else:
  82.         sys.exit("Found no config")
  83.     return 0   
  84.    
  85. def onAuthSuccess(username):
  86.     print("Authed %s" % username)
  87.     methodsInterface.call("ready")
  88.  
  89. def onAuthFailed(username, err):
  90.     sys.exit("Auth Failed!")
  91.  
  92. def onDisconnected(reason):
  93.     sys.exit("Disconnected because %s" %reason)
  94.  
  95. def onPresenceUpdated(self, jid, lastSeen):
  96.     formattedDate = datetime.datetime.fromtimestamp(long(time.time()) - lastSeen).strftime('%d-%m-%Y %H:%M')
  97.     onMessageReceived(0, jid, "LAST SEEN RESULT: %s"%formattedDate, long(time.time()), False, None, False)
  98.  
  99. def onMessageSent(jid, messageId):
  100.     formattedDate = datetime.datetime.fromtimestamp(sentCache[messageId][0]).strftime('%d-%m-%Y %H:%M')
  101.     print("%s [%s]:%s"%(username, formattedDate, sentCache[messageId][1]))
  102.  
  103. def onMessageReceived(messageId, jid, messageContent, timestamp, wantsReceipt, pushName, isBroadcast):
  104.     if jid[:jid.index('@')] != phoneNumber:
  105.         return
  106.     formattedDate = datetime.datetime.fromtimestamp(timestamp).strftime('%d-%m-%Y %H:%M')
  107.     print("%s [%s]:%s"%(jid, formattedDate, messageContent))
  108.     executeMessage(jid, messageContent)
  109.  
  110. #Autopings einschalten
  111. ycm = YowsupConnectionManager()
  112. ycm.setAutoPong(True)
  113.  
  114.  
  115. credentials = getCredentials(DEFAULT_CONFIG)
  116. countryCode, login, identity, password = credentials
  117. identity = Utilities.processIdentity(identity)
  118. password = base64.b64decode(bytes(password.encode('utf-8')))
  119.  
  120. if countryCode:
  121.     phoneNumber = login[len(countryCode):]
  122. else:
  123.     dissected = dissectPhoneNumber(login)
  124.     if not dissected:
  125.         sys.exit("ERROR. Couldn't detect cc, you have to manually place it your config")
  126.     countryCode, phoneNumber = dissected
  127.  
  128. username=phoneNumber + "@s.whatsapp.net"
  129.  
  130. whitelist=["69911301815@s.whatsapp.net"]
  131.  
  132. signalsInterface = ycm.getSignalsInterface()
  133. methodsInterface = ycm.getMethodsInterface()
  134.  
  135. signalsInterface.registerListener("auth_success", onAuthSuccess)
  136. signalsInterface.registerListener("auth_fail", onAuthFailed)
  137. signalsInterface.registerListener("message_received", onMessageReceived)
  138. signalsInterface.registerListener("receipt_messageSent", onMessageSent)
  139. signalsInterface.registerListener("presence_updated", onPresenceUpdated)
  140. signalsInterface.registerListener("disconnected", onDisconnected)
  141.  
  142.  
  143. methodsInterface.call("auth_login", (username, password))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement