This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmpp.py
79 lines (56 loc) · 2.46 KB
/
xmpp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import re
import cgi
import logging
import yaml
from datetime import datetime
import basherc
from google.appengine.api import xmpp
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.webapp.util import run_wsgi_app
# ---
class XMPPHandler(webapp.RequestHandler):
def __init__(self):
self.config = yaml.load(file('basher.yaml', 'r'))
def send_invite(self, message):
logging.info("[basher] message discarded (f=%s)" % message.sender)
message.reply("Oops... please format your message this way : &author name"e&")
message.reply("For example: &Douglas Adams&Don't Panic&")
message.reply("Or, if you want to try to add a date on it, use this new format, with \
the date encoded as YYYYMMMDD at the end.")
message.reply(" -- 20081201 gives the first of december of 2008 --")
message.reply("&Douglas Adams&Don't Panic&20080101")
def post(self):
message = xmpp.Message(self.request.POST)
logging.info("[basher] message is /%s/" % message.body)
quote = basherc.Quote()
reg = re.compile('^\&([^\&]+)\&([^\&]+)\&(\d{8})?$')
m = reg.match(message.body)
if not m:
return self.send_invite(message)
quote.author = message.sender
quote.author_original = m.group(1)
quote.content = m.group(2)
# we're trying to convert the date, here, dude
try:
if m.group(3):
quote.date_quote = datetime.strptime(m.group(3), '%Y%m%d')
quote.put()
logging.info("[basher] message stored")
message.reply("Thank you ! You may want to check %s now." % self.request.host_url)
# sends a Jabber msg to the admins
if self.config['warn_xmpp']:
try :
xmpp.send_message(self.config['warn_xmpp'], "A new quote was added : %s" % quote.content)
except:
loggin.warning("[basher] unable to warn")
except ValueError:
message.reply("The date " + m.group(3) + " is invalid !")
# ---
application = webapp.WSGIApplication(
[('/_ah/xmpp/message/chat/', XMPPHandler)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()