Exemple #1
0
def upload_files(files):
    uploadConfig = load_config(CONFIG_DIR + "ftp_upload_creds.yaml")

    ftpServer = uploadConfig["ftpServer"]
    ftpPort = uploadConfig["port"]
    ftpUser = uploadConfig["user"]
    ftpPassword = uploadConfig["password"]
    ftpUploadDir = uploadConfig["uploadDir"]

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    try:
        ssh.connect(ftpServer, username=ftpUser, password=ftpPassword, port=ftpPort)
    except paramiko.SSHException:
        log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", ftpServer), "SFTP_upload")
        raise
        # change to appropriate directory for server
    sftp = ssh.open_sftp()
    try:
        sftp.chdir(ftpUploadDir)
    except:
        log_action("ERROR", "Failed to change directory to %s on %s" %(ftpUploadDir, ftpServer), "SFTP_upload")
        raise

    for f in files:
        if not f.endswith(".uploaded"):
            remoteFilePath = os.path.basename(f)
#            if f.endswith('.zip'):
#               if zipfile.is_zipfile(f):
#                   fz = zipfile.ZipFile(f, 'r')
#                   fz.extractall()
#                   # get names of files in archive:
#                   filesInZipFile = fz.namelist()
#                   # rename unzipped files
#                   for fu in filesInZipFile:
#                       if not fu.startswith(gateway):
#                           os.rename(f, gateway + "_" + fu)
#                   fz.close()
#                   # delete zip file
#                   os.remove(localFileName)
#               else:
#                   log_action("ERROR", "Invalid attachment file %s received from %s" % (localFileName, gateway), "Email")

            try:
                sftp.put(f, remoteFilePath)
            except:
                log_action("ERROR", "Error uploading file " + f, "SFTP_upload")
                raise
            else:
                log_action("SUCCESS",  f + " uploaded successfully", "SFTP_upload")
                os.rename(f, f + ".uploaded")

    ssh.close()
    return
Exemple #2
0
def connect_IMAP(yamlConfig):

    IMAPuserName = yamlConfig["emailAddress"]
    IMAPpassword = yamlConfig["password"]
    IMAPserver = yamlConfig["IMAPserver"]

    try:
        connection = imaplib.IMAP4_SSL(IMAPserver)
    except:
        log_action("ERROR", "Failed to establish %s connection to server %s" % ("IMAP", IMAPserver), "Email")
        raise
    try:
        connection.login(IMAPuserName, IMAPpassword)
    except:
        log_action("ERROR", "IMAP authentication failed for user %s on %s" % (IMAPuserName, IMAPserver), "Email")
        raise

    return connection
Exemple #3
0
def forward_email(yamlConfig, to_addr, msg):

    SMTPuserName = yamlConfig["emailAddress"]
    SMTPpassword = yamlConfig["password"]
    SMTPserver = yamlConfig["SMTPserver"]

    try:
        smtpObj = smtplib.SMTP(SMTPserver, 587)
    except:
        log_action("ERROR", "Failed to establish %s connection to server %s" % ("SMTP", SMTPserver), "Email")
        raise
    try:
        smtpObj.login(SMTPuserName, SMTPpassword)
    except:
        log_action("ERROR", "SMTP authentication failed for user %s on %s" % (SMTPuserName, SMTPserver), "Email")
        raise

    smtpObj.sendmail(SMTPuserName, to_addr, msg.as_string())
    smtpObj.quit

    return
Exemple #4
0
def connect_sftp():
    uploadConfig = load_config(CONFIG_DIR + "ftp_upload_creds.yaml")

    ftpServer = uploadConfig["ftpServer"]
    ftpPort = uploadConfig["port"]
    ftpUser = uploadConfig["user"]
    ftpPassword = uploadConfig["password"]
    ftpUploadDir = uploadConfig["uploadDir"]

    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None

    try:
        sftp = open(pysftp.Connection(host=ftpServer, username=ftpUser, password=ftpPassword, port=ftpPort, cnopts=cnopts))
    except:
	log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", ftpServer), "SFTP_upload")
        raise
    # change to appropriate directory for server
    try:
        sftp.cwd(ftpUploadDir)
    except:
        log_action("ERROR", "Failed to change directory to %s on %s" %(ftpUploadDir, ftpServer), "SFTP_upload")
        raise
    return sftp
Exemple #5
0
def get_files_using_pysftp(gateway, config, files):

    gatewayServer = config["SFTP"][gateway]["URL"]
    gatewayUser = config["SFTP"][gateway]["user"]
    gatewayPassword = config["SFTP"][gateway]["password"]
    ftpDir = config["SFTP"][gateway]["ftpDir"]
    dateFormat = config["SFTP"][gateway]["dateFormat"]

    cnopts = pysftp.CnOpts()
    cnopts.hostkeys = None

    try:
        with pysftp.Connection(host=gatewayServer, username=gatewayUser, password=gatewayPassword, cnopts=cnopts) as sftp:
            # change to appropriate directory for each server
            try:
                # Alipay makes new date-stamped folders everyday:
                if gateway == "Alipay":
                    ftpDir = ftpDir + "/" + date_format_yesterday(dateFormat)
                    sftp.cwd(ftpDir)
                else:
                    sftp.cwd(ftpDir)
            except:
                log_action("ERROR", "Failed to change directory to %s on %s" %(ftpDir, gatewayServer), "SFTP_download")
                raise

            for f in files:
                if sftp.isfile(f):  # check if file exists on remote location
                    try:            
                        sftp.get(f)
                    except:
                        log_action("ERROR", "Error downloading file " + f, "SFTP_download")
                        raise
                    else:
                        os.rename(f,  gateway + "_" + f)
                else:
                    log_action("ERROR", "File %s not found on server %s" % (f, gatewayServer), "SFTP_download")
    except:
        log_action("ERROR", "Failed to establish %s connection to server %s" % ("SFTP", gatewayServer), "SFTP_download")
        raise

    return
Exemple #6
0
def parse_emails(conn, gateway, config, mailboxConfig):
    fromAddress = config["Email"][gateway]["fromAddress"]

    # switch to the IMAP folder called "INBOX"
    retVal, data = conn.select("INBOX")
    if retVal != "OK":
        log_action("ERROR", "Failed to select Inbox folder, aborting...", "Email")
        sys.exit(1)

    # Unread mails are marked with status "UNSEEN" as per IMAP
    retVal, data = conn.search(None, "FROM", fromAddress, "UNSEEN")
    if retVal != "OK":
        # should this be an ERROR? Depends on whether mail is expected *everytime* this runs
        log_action("ERROR", "No new emails", "Email")
        sys.exit(1)

    for num in data[0].split():
        try:
            retVal, data = conn.fetch(num, '(RFC822)')
        except:
            log_action("ERROR", "ERROR fetching message %s From %s" % (num, fromAddress), "Email")
            raise

	if retVal != 'OK':
            log_action("ERROR", "ERROR getting message %s" % num, "Email")
            continue

        msg = email.message_from_string(data[0][1])

        # Messages with attachments are multipart messages
        if msg.get_content_maintype() == 'multipart':
            for part in msg.walk():
                # multipart are just containers, so we skip them
                if part.get_content_maintype() == 'multipart':
                    continue
#                if part.get('Content-Disposition') is None:
#                    print("Malformed Content-Disposition")
#                    continue
                #save the attachment in the program directory
                mailboxFilename = part.get_filename()
                if mailboxFilename != None:
                    localFileName = gateway + "_" + mailboxFilename
                    uploadedFileName = localFileName + ".uploaded"
                    if not os.path.isfile(uploadedFileName):
                        fp = open(localFileName, 'wb')
                        try:
                           fp.write(part.get_payload(decode=True))
                        except:
                           log_action("ERROR", "Failed to download attachment from email with subject %s" % msg['Subject'], "Email")
                           continue
                        else:
                            # Look at unread messages only
                            conn.uid('store', num, '+FLAGS', '(\\Unread)') 
                            conn.copy(num, 'Archive')
                            conn.expunge()
                        finally:
                           fp.close()
                        if localFileName.endswith('.zip'):
                           if zipfile.is_zipfile(localFileName):
                               fz = zipfile.ZipFile(localFileName, 'r')
                               fz.extractall()
                               # get names of files in archive:
                               filesInZipFile = fz.namelist()
                               # rename unzipped files
                               for f in filesInZipFile:
                                   if not f.startswith(gateway):
                                       # Deal with PayU credit card file separately
                                       if (<YOUR_DOMAIN_NAME> + "_CC_") in localFileName:
                                           fileNamePart1 = f.split("_")[0]
                                           fileNamePart2 = f.split("_")[1]
                                           fileNamePart3 = f.split("_")[2]
                                           os.rename(f, gateway + "_" + fileNamePart1 + "_" + fileNamePart2 + "_CC_" + fileNamePart3)
                                       else:
                                           os.rename(f, gateway + "_" + f)
                               fz.close()
                               # delete zip file
                               os.remove(localFileName)
                           else:
                               log_action("ERROR", "Invalid attachment file %s received from %s" % (localFileName, gateway), "Email")
                    else:
                        log_action("INFO","is already uploaded" % uploadedFileName, "Email")

                else:
                    print("Invalid mailboxFilename")
                # WebMoney sends mail to one email address only, so we fwd those mails from here to whoever needs them:
                if gateway == "WebMoney":
                    toAddress = <OTHER_RCPT>
                    forward_email(mailboxConfig, toAddress, msg)
        else:
            log_action("ERROR", "Not a Multipart message")

    return