scripts/jabber/fonctions.py
Antoine Durand-Gasselin aaf52989f6 [jabber] ajout du cgi pour créer un compte jabber
darcs-hash:20090728100655-bd074-9a4cf910f821f6b2dc69d8e9c74fe496ea6a37d7.gz
2009-07-28 12:06:55 +02:00

87 lines
2.7 KiB
Python
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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 = "<password xmlns='jabber:iq:auth'>(.*)</password>"
replace = "<password xmlns='jabber:iq:auth'>" + newPassword + "</password>"
XMLString = re.sub(pattern, replace, XMLString)
pattern = "<password xmlns='jabber:iq:auth' xdbns='jabber:iq:auth'>(.*)</password>"
replace = "<password xmlns='jabber:iq:auth' xdbns='jabber:iq:auth'>" + newPassword + "</password>"
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("<email>([^<>]*)</email>",lambda m:m.group(1),re.search('<email>([^<>]*)</email>',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 renvoyŽs par l'utilisateur
# si elle est dŽfinit (donc on a reu les rŽponses ˆ un forulaire)
# on execute l'action correspondante ; sinon on affiche la page par defaut