Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import smtplib
- import imaplib
- import time
- import threading
- import Queue
- import sys
- import argparse
- import csv
- import random
- import string
- import subprocess as sp
- import os
- import email
- import re
- class mailMan(object):
- """mailMan manages player interactions such as tags reported via text messages or emails"""
- def __init__(self, playerManager):
- super(mailMan, self).__init__()
- self.mail = imaplib.IMAP4_SSL('imap.gmail.com')
- self.mail.login(args.username,args.password)
- self.mail.list()
- # Out: list of "folders" aka labels in gmail.
- self.mail.select("inbox") #connect to inbox.
- def getBody(self, emailMessage):
- maintype = emailMessage.get_content_maintype()
- if maintype == 'multipart':
- for part in emailMessage.get_payload():
- if part.get_content_maintype() == 'text':
- return part.get_payload()
- elif maintype == 'text':
- return emailMessage.get_payload()
- def getUnread(self):
- self.mail.select("inbox") # Select inbox or default namespace
- (retcode, messages) = self.mail.search(None, '(UNSEEN)')
- if retcode == 'OK':
- retlist = []
- for num in messages[0].split(' '):
- print 'Processing :', messages
- typ, data = self.mail.fetch(num,'(RFC822)')
- msg = email.message_from_string(data[0][1])
- typ, data = self.mail.store(num,'-FLAGS','\\Seen')
- if retcode == 'OK':
- for item in str(msg).split('\n'):
- #finds who sent the message
- if re.match("From: *",item):
- print (item[6:], self.getBody(msg))
- retlist.append((item[6:], self.getBody(msg).rstrip())
- #print (item, self.getBody(msg).rstrip())
- class players(object):
- """manages the player"""
- def __init__(self, pDict):
- super(players, self).__init__()
- self.pDict = pDict
- #makes a partucular player a zombie
- def makeZombie(self, pID):
- self.pDict[pID].zombie = True
- #makes a partucular player a zombie
- def makeHuman(self, pID):
- self.pDict[pID].zombie = False
- def chosePZ(self):
- #sp.call('clear', shell=True)
- #os.system('clear')
- picking = True
- while picking:
- selection = raw_input("\nEnter the player ID of the player who you would like to be PZ.\nIf you don't enter anything here a pz will be chosen at random.\n")
- if selection == "":
- selection = random.choice(self.pDict.keys())
- self.pDict[selection].PZero = True
- print "%s (%s) has been set as PZ, are you satisfied with this choice? (Y/N)" % (self.pDict[selection].getAttribs()[0], selection)
- satisfied = raw_input()
- if satisfied.lower() == 'y':
- picking = False
- elif satisfied.lower() != 'n':
- print "Please enter a 'Y' or an 'N'.\n"
- else:
- try:
- self.pDict[selection].PZero = True
- print "%s (%s) has been set as PZ, is this correct? (Y/N)" % (self.pDict[selection].getAttribs()[0], selection)
- satisfied = raw_input()
- if satisfied.lower() == 'y':
- picking = False
- elif satisfied.lower() != 'n':
- print "Please enter a 'Y' or an 'N'.\n"
- except:
- print "That player ID does not exist please enter another one."
- #holds values for a single player
- class player(object):
- """represents players and their values"""
- def __init__(self, name, number, email):
- super(player, self).__init__()
- self.name = name
- self.number = number
- self.email = email
- self.zombie = False
- self.PZero = False
- self.kills = 0
- #returns a tupple of the name, phone number(formated as an email address) and email address of the playes.
- def getAttribs(self):
- return (self.name, self.number, self.email, self.zombie, self.PZero, self.kills)
- #Handles the login and sending of email messages via SMTP
- class messaging(object):
- """Manages all funcitons of the program related to sending messages but not receving them"""
- def __init__(self,fromaddr,playerManager):
- super(messaging, self).__init__()
- self.fromaddr = fromaddr
- self.server = smtplib.SMTP('smtp.gmail.com:587')
- self.server.starttls()
- print "Atempting to long in to %s (username: %s)" % (self.fromaddr, args.username)
- try:
- self.server.login(args.username,args.password)
- print "login worked"
- except Exception, e:
- print "login failed, are your sure your username and password are correct?"
- raise e
- #I should call all of the "send*" functions "text*" but im going to //todo that for now.
- def sendAll(self, msg):
- print "sending messages to players"
- # The actual mail send
- print "sending mail..."
- for key in playerManager.pDict.keys():
- print addr
- self.server.sendmail(self.fromaddr, playerManager.pDict[key].number, msg)
- print "mail sent!"
- def sendZombies(self, msg):
- for key in playerManager.pDict.keys():
- if playerManager.pDict[key].zombie:
- self.server.sendmail(self.fromaddr, playerManager.pDict[key].number, msg)
- def sendHumans(self, msg):
- for key in playerManager.pDict.keys():
- if playerManager.pDict[key].human:
- self.server.sendmail(self.fromaddr, playerManager.pDict[key].number, msg)
- def sendPID(self, pIDList, msg):
- for pIDs in pIDList:
- server.sendmail(self.fromaddr, playerManager.pDict[pIDs].number, msg)
- def sendPZero(self, msg):
- for key in playerManager.pDict.keys():
- if playerManager.pDict[key].PZero:
- self.server.sendmail(self.fromaddr,playerManager.pDict[key].number, msg)
- def emailAll(self, msg):
- print "sending messages to players"
- # The actual mail send
- print "sending mail..."
- for key in playerManager.pDict.keys():
- print addr
- self.server.sendmail(self.fromaddr, playerManager.pDict[key].email, msg)
- print "mail sent!"
- def emailZombies(self, msg):
- for key in playerManager.pDict.keys():
- if playerManager.pDict[key].zombie:
- self.server.sendmail(self.fromaddr, playerManager.pDict[key].email, msg)
- def emailHumans(self, msg):
- for key in playerManager.pDict.keys():
- if playerManager.pDict[key].human:
- self.server.sendmail(self.fromaddr, playerManager.pDict[key].email, msg)
- def emailPID(self, pIDList, msg):
- for pIDs in pIDList:
- server.sendmail(self.fromaddr, playerManager.pDict[pIDs].email, msg)
- def emailPZero(self, msg):
- for key in playerManager.pDict.keys():
- if playerManager.pDict[key].PZero:
- self.server.sendmail(self.fromaddr,playerManager.pDict[key].email, msg)
- #############################################################################
- ##a couple of utility functions that are up here for cleanliness ##
- ##and that realy only main (and truthfuly not even main) has to care about.##
- #############################################################################
- #cleens up the numbers from the .csv only needed by the main.
- def cleenUpNumber(num):
- all=string.maketrans('','')
- nodigs=all.translate(all, string.digits)
- num = num.translate(all, nodigs)
- return num
- #returns a dict of players
- def processFile(fileName):
- carrerDict = {'Alltel Wireless':'@message.Alltel.com', 'Boost Mobile':'@myboostmobile.com', 'AT&T':'@txt.att.net','Sprint':'@messaging.sprintpcs.com','Straight Talk':'@VTEXT.COM', 'T-Mobile':'@tmomail.net','U.S. Cellular':'@email.uscc.net', 'Verizon':'@vtext.com', 'Virgin Mobile':'@vmobl.com'}
- playerTempList=[]
- with open(fileName, 'rb') as csvFile:
- csvData = csv.reader(csvFile, dialect='excel')
- for row in csvData:
- if row[0] != "+9sdG":
- cleenNum = cleenUpNumber(row[2])
- cleenNum = cleenNum+carrerDict[row[3]]
- row.remove(row[2])
- row.insert(2, cleenNum)
- playerTempList.append((row[0], player(row[1],row[2],row[3])))
- playerDict = dict(playerTempList)
- return playerDict
- #main is mostly used to set some stuff up and for debug but honestly not much else.
- def main():
- #create the object to handle sending messages.
- """messenger = messaging(args.account, playerManager)
- playerManager.chosePZ()
- for key in playerManager.pDict.keys():
- print "\n %s" % str(playerManager.pDict[key].getAttribs())
- if playerManager.pDict[key].PZero:
- print"PZero is %s (pID is %s)." % (playerManager.pDict[key].name, key)
- messenger.sendPZero("Hello World, if you get this text Mike Shlanta")"""
- mail = mailMan(playerManager)
- #mail.getBody()
- mail.getUnread()
- #game.run()
- if __name__ == '__main__':
- #because fuck you thats why aslo im lazy so ya...
- parser = argparse.ArgumentParser(description="Helps with adminstration and management of Humans Vs. Zombies games.")
- parser.add_argument('fileName', type=str, help="The name of the .csv file containing the list of players.")
- parser.add_argument('account', type=str, help="The address of the email account you are using i.e. example@gmail.com")
- parser.add_argument('username', type=str, help="The username of the email account you want to login to.")
- parser.add_argument('password', type=str, help="The password of the email account you want to login to.")
- args=parser.parse_args()
- #create the player manager object from the .csv
- playerManager = players(processFile(args.fileName))
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement