2014-06-13 22:12:45 +00:00
|
|
|
# coding=utf-8
|
|
|
|
|
2014-08-27 23:39:31 +00:00
|
|
|
"""Send SMS using SMS API (currently only Free Mobile)"""
|
|
|
|
|
2014-06-13 22:12:45 +00:00
|
|
|
import re
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import urllib.error
|
|
|
|
import urllib.request
|
|
|
|
import urllib.parse
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
from hooks import hook
|
|
|
|
|
2014-08-13 13:53:55 +00:00
|
|
|
nemubotversion = 3.4
|
2014-06-13 22:12:45 +00:00
|
|
|
|
2014-09-26 16:00:22 +00:00
|
|
|
from more import Response
|
|
|
|
|
2014-06-13 22:12:45 +00:00
|
|
|
def load(context):
|
|
|
|
global DATAS
|
|
|
|
DATAS.setIndex("name", "phone")
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
def help_full():
|
2014-06-13 22:12:45 +00:00
|
|
|
return "!sms /who/[,/who/[,...]] message: send a SMS to /who/."
|
|
|
|
|
|
|
|
def send_sms(frm, api_usr, api_key, content):
|
|
|
|
content = "<%s> %s" % (frm, content)
|
|
|
|
|
|
|
|
try:
|
|
|
|
req = urllib.request.Request("https://smsapi.free-mobile.fr/sendmsg?user=%s&pass=%s&msg=%s" % (api_usr, api_key, urllib.parse.quote(content)))
|
|
|
|
res = urllib.request.urlopen(req, timeout=5)
|
|
|
|
except socket.timeout:
|
|
|
|
return "timeout"
|
|
|
|
except urllib.error.HTTPError as e:
|
|
|
|
if e.code == 400:
|
|
|
|
return "paramètre manquant"
|
|
|
|
elif e.code == 402:
|
|
|
|
return "paiement requis"
|
|
|
|
elif e.code == 403 or e.code == 404:
|
|
|
|
return "clef incorrecte"
|
|
|
|
elif e.code != 200:
|
|
|
|
return "erreur inconnue (%d)" % status
|
|
|
|
except:
|
|
|
|
return "unknown error"
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2014-08-12 18:10:19 +00:00
|
|
|
@hook("cmd_hook", "sms")
|
2014-06-13 22:12:45 +00:00
|
|
|
def cmd_sms(msg):
|
|
|
|
if len(msg.cmds) <= 2:
|
|
|
|
raise IRCException("À qui veux-tu envoyer ce SMS ?")
|
|
|
|
|
|
|
|
# Check dests
|
|
|
|
cur_epoch = time.mktime(time.localtime());
|
|
|
|
for u in msg.cmds[1].split(","):
|
|
|
|
if u not in DATAS.index:
|
|
|
|
raise IRCException("Désolé, je sais pas comment envoyer de SMS à %s." % u)
|
2014-07-10 21:38:00 +00:00
|
|
|
elif cur_epoch - float(DATAS.index[u]["lastuse"]) < 42:
|
2014-06-13 22:12:45 +00:00
|
|
|
raise IRCException("Un peu de calme, %s a déjà reçu un SMS il n'y a pas si longtemps." % u)
|
|
|
|
|
|
|
|
# Go!
|
|
|
|
fails = list()
|
|
|
|
for u in msg.cmds[1].split(","):
|
|
|
|
DATAS.index[u]["lastuse"] = cur_epoch
|
2014-10-20 19:57:04 +00:00
|
|
|
if msg.to_response[0] == msg.frm:
|
|
|
|
frm = msg.frm
|
2014-06-13 22:12:45 +00:00
|
|
|
else:
|
2014-10-20 19:57:04 +00:00
|
|
|
frm = msg.frm + "@" + msg.to[0]
|
2014-06-13 22:12:45 +00:00
|
|
|
test = send_sms(frm, DATAS.index[u]["user"], DATAS.index[u]["key"], " ".join(msg.cmds[2:]))
|
|
|
|
if test is not None:
|
|
|
|
fails.append( "%s: %s" % (u, test) )
|
|
|
|
|
|
|
|
if len(fails) > 0:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("quelque chose ne s'est pas bien passé durant l'envoi du SMS : " + ", ".join(fails), msg.channel, msg.nick)
|
2014-06-13 22:12:45 +00:00
|
|
|
else:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("le SMS a bien été envoyé", msg.channel, msg.nick)
|
2014-06-13 22:12:45 +00:00
|
|
|
|
|
|
|
apiuser_ask = re.compile(r"(utilisateur|user|numéro|numero|compte|abonne|abone|abonné|account)\s+(est|is)\s+(?P<user>[0-9]{7,})", re.IGNORECASE)
|
|
|
|
apikey_ask = re.compile(r"(clef|key|password|mot de passe?)\s+(?:est|is)?\s+(?P<key>[a-zA-Z0-9]{10,})", re.IGNORECASE)
|
|
|
|
|
2014-08-28 16:05:21 +00:00
|
|
|
@hook("ask_default")
|
2014-06-13 22:12:45 +00:00
|
|
|
def parseask(msg):
|
2014-08-29 14:33:45 +00:00
|
|
|
if msg.text.find("Free") >= 0 and (
|
|
|
|
msg.text.find("API") >= 0 or msg.text.find("api") >= 0) and (
|
|
|
|
msg.text.find("SMS") >= 0 or msg.text.find("sms") >= 0):
|
|
|
|
resuser = apiuser_ask.search(msg.text)
|
|
|
|
reskey = apikey_ask.search(msg.text)
|
2014-06-13 22:12:45 +00:00
|
|
|
if resuser is not None and reskey is not None:
|
|
|
|
apiuser = resuser.group("user")
|
|
|
|
apikey = reskey.group("key")
|
|
|
|
|
|
|
|
test = send_sms("nemubot", apiuser, apikey,
|
|
|
|
"Vous avez enregistré vos codes d'authentification dans nemubot, félicitation !")
|
|
|
|
if test is not None:
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("je n'ai pas pu enregistrer tes identifiants : %s" % test, msg.channel, msg.nick)
|
2014-06-13 22:12:45 +00:00
|
|
|
|
|
|
|
if msg.nick in DATAS.index:
|
|
|
|
DATAS.index[msg.nick]["user"] = apiuser
|
|
|
|
DATAS.index[msg.nick]["key"] = apikey
|
|
|
|
else:
|
|
|
|
ms = ModuleState("phone")
|
|
|
|
ms.setAttribute("name", msg.nick)
|
|
|
|
ms.setAttribute("user", apiuser)
|
|
|
|
ms.setAttribute("key", apikey)
|
|
|
|
ms.setAttribute("lastuse", 0)
|
|
|
|
DATAS.addChild(ms)
|
|
|
|
save()
|
2014-09-18 05:57:06 +00:00
|
|
|
return Response("ok, c'est noté. Je t'ai envoyé un SMS pour tester ;)",
|
2014-06-13 22:12:45 +00:00
|
|
|
msg.channel, msg.nick)
|