#!/usr/bin/env python3 import time from subprocess import Popen, PIPE, STDOUT def extract_signature_content(lines) : gpg_boundary = None gpg_boundary_stroke = 0; lines_to_validate = [] for line in mail_lines : if "application/pgp-signature" in line : if "boundary=\"" in line : extract_boundary = line.split("boundary=\"") gpg_boundary = "--{}".format( extract_boundary[1].strip()[:-1]) if gpg_boundary is not None and gpg_boundary in line : gpg_boundary_stroke+=1 if gpg_boundary_stroke == 1 : if gpg_boundary not in line : lines_to_validate.append(line.replace("\n", "\r\n")) lines_to_validate = lines_to_validate[:-1] return lines_to_validate def munpack_extract(folder, file) : p = Popen(["munpack", "-f", "-t", "-C", folder, file], stdout=PIPE, stdin=PIPE, stderr=STDOUT) out, err = p.communicate() out = out.decode("utf-8", "strict") # En cas d'erreurs dans le depack passer au prochain nouveau mail if "Did" in out : f = out.split(" ") f = f[len(f)-1].replace("\n", "") p = Popen(["rm",new_base+"/"+f], stdout=PIPE, stdin=PIPE, stderr=STDOUT) out, err = p.communicate() return None return out.split("\n") def validate_signature(to_validate) : file = open("tmp", "w") file.writelines(to_validate) file.close() p = Popen(["gpg", "--verify", "signature.asc", "tmp"], stdout=PIPE, stdin=PIPE, stderr=STDOUT) out, err = p.communicate() out = out.decode("utf-8", "strict") return not("BAD" in out or "MAUVAISE" in out) new_base = "mails/" html_base= "/var/www/html" #p = Popen(['offlineimap'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #out, err = p.communicate() p = Popen(["ls",new_base], stdout=PIPE, stdin=PIPE, stderr=STDOUT) out, err = p.communicate() out_decode = out.decode("utf-8", "strict") new_mails = out_decode.split("\n") for new_mail in new_mails : # Pour chaque veritable nouveau mail if not new_mail == "" : print(new_mail) # # Read the mail and extract the pgp signature content # file = open("{}/{}".format(new_base,new_mail), "r") mail_lines = file.readlines(); lines_to_validate = extract_signature_content(mail_lines) # Depacker le mail avec munpack extracted_files = munpack_extract("./", "{}/{}".format(new_base,new_mail)) if extracted_files is None : continue; validation = False for extracted in extracted_files : if "signature.asc" in extracted : validation = validate_signature(lines_to_validate) print("validation {}".format(validation)) ## Les fichiers sans extension vont etre listes dans le but d'obtenir le ## prochain plus grand fichier. #p = Popen(["ls",html_base], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #out, err = p.communicate() #html_content = out.split("\n") #print(html_content) ## Les noms de fichiers sont parcourus a la recherche du plus grand ## numero #greater = 0 #for html_file in html_content : # try : # current = int(html_file) # if current > greater : # greater = current # except : # pass ## Une fois le plus grand numero isole, #print("greater {}".format(greater)) ## Le precedent plus grand va etre mis a jour #f = open(html_base+"/{}".format(greater), 'r') #fn= open(html_base+"/{}new".format(greater), 'w') #prev_lines = f.readlines() #for line in prev_lines : # if 'class="prev"' in line : # fn.write(' \n'.format(greater+1)) # else : # fn.write(line) #f.close() #fn.close() ## move the new file to its right place #p = Popen(["rm",html_base+"/{}".format(greater)], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #out, err = p.communicate() #p = Popen(["mv",html_base+"/{}new".format(greater), # html_base+"/{}".format(greater)], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #out, err = p.communicate() ## now greater is + 1 #greater += 1 ## move index.html to greater #f = open(html_base+"/index.html", 'r') #fn= open(html_base+"/{}".format(greater), 'w') #prev_lines = f.readlines() #for line in prev_lines : # if 'class="next"' in line : # fn.write(' \n'.format("index.html")) # fn.write(' \n'.format(greater-1)) # else : # fn.write(line) #f.close() #fn.close() ## remove the old index.html file #p = Popen(["rm",html_base+"/index.html"], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #out, err = p.communicate() ## now create the new index.html file #f = open(html_base+"/index.html", 'w') #f.write('\n') #f.write(' \n') #f.write(' \n') #f.write(' \n') #f.write(' \n') #f.write(' \n') #f.write(' \n') #f.write(' \n'.format(greater)) #f.write('
\n') ## write the top of the file #for brut in extracted_files : # if not brut == "" : # split = brut.split(" ") # file_name = split[0] # file_type = split[1] # if "plain" in file_type : # p = Popen(["rm",new_base+"/"+file_name], stdout=PIPE, stdin=PIPE, stderr=STDOUT) # out, err = p.communicate() # elif "html" in file_type : # f.write('
\n') # fh = open(new_base+"/"+file_name, 'r') # lines = fh.readlines() # for line in lines : # f.write(line) # fh.close # f.write('
\n') # p = Popen(["rm",new_base+"/"+file_name], stdout=PIPE, stdin=PIPE, stderr=STDOUT) # out, err = p.communicate() # else : # f.write('
\n') # f.write(' chaussette\n'.format(file_name)) # f.write('
\n') # p = Popen(["mv",new_base+"/"+file_name, # html_base+"/"+file_name], stdout=PIPE, stdin=PIPE, stderr=STDOUT) # out, err = p.communicate() # try : # p = Popen(["chmod", "644", html_base+"/"+file_name], stdout=PIPE, stdin=PIPE, stderr=STDOUT) # out, err = p.communicate() # except : # pass ## write the end of the file #f.write('
\n') #f.write(' \n') #f.write('\n') #f.close() ## remove the mail #p = Popen(["rm", new_base+"/"+new_mail], stdout=PIPE, stdin=PIPE, stderr=STDOUT) #out, err = p.communicate()