#!/usr/bin/env python
# -*- coding: utf-8 -*-
# # # # # # # # # # # # # #
# Génération de password #
# # # # # # # # # # # # # #
from whrandom import choice
import string
jabberuserdir='/var/lib/jabber/jabber.crans.org/'
def GenPasswd(length=8, chars=string.letters + string.digits):
return ''.join([choice(chars) for i in range(length)])
# # # # # # # # # # # # # #
# Parseur de templates #
# # # # # # # # # # # # # #
import re, sys
def parse(text, var_dictiary):
def replace(matchobj):
token = matchobj.group(1)
if var_dictiary.has_key(token):
return var_dictiary.get(token)
else:
return matchobj.group()
#raise NameError, 'parseur : variable inconnue : '+token
return re.sub('\{([^\}]+)\}',replace, text)
def parse_file(file_name, var_dictionary):
file = open(file_name, 'r')
text = file.read();
return parse(text, var_dictionary);
# # # # # # # # # # # # # #
# Modifier le fichier XML #
# # # # # # # # # # # # # #
def XMLChgePassword(XMLString, newPassword):
pattern = "(.*)"
replace = "" + newPassword + ""
XMLString = re.sub(pattern, replace, XMLString)
pattern = "(.*)"
replace = "" + newPassword + ""
return re.sub(pattern, replace, XMLString)
def chgePasswordForUser(user, newPassword):
fileName = jabberuserdir+user+".xml"
file=open(fileName)
xmlString=file.read()
xmlString = XMLChgePassword(xmlString, newPassword)
file=open(fileName,'w')
file.write(xmlString)
file.close()
os.chmod(fileName,0600)
# # # # # # # # # # # # # # #
# Lire le fichier XML #
# # # # # # # # # # # # # # #
def XMLMailForUser(user):
fileName = jabberuserdir+user+".xml"
file=open(fileName)
xmlString=file.read()
return re.sub("([^<>]*)",lambda m:m.group(1),re.search('([^<>]*)',xmlString).group())
# # # # # # # # # # # # # # #
# Envoyer des e-mails #
# # # # # # # # # # # # # # #
SENDMAIL = "/usr/sbin/sendmail" # sendmail location
import os
def sendmail(text):
p = os.popen("%s -t" % SENDMAIL, "w")
p.write(text)
sts = p.close()
if sts != None:
print "Sendmail exit status :", sts
# # # # # # # # # # # # # # #
# ok, maintenant on vas #
# faire des trucs #
# # # # # # # # # # # # # # #
# on regarde la variable des champs renvoys par l'utilisateur
# si elle est dfinit (donc on a reu les rponses un forulaire)
# on execute l'action correspondante ; sinon on affiche la page par defaut