~ubuntu-branches/ubuntu/precise/gozerbot/precise

« back to all changes in this revision

Viewing changes to gozerplugs/web/json.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Malcolm
  • Date: 2008-06-02 19:26:39 UTC
  • mfrom: (1.1.3 upstream) (3.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080602192639-3rn65nx4q1sgd6sy
Tags: 0.8.1-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# web/json.py
 
2
#
 
3
#
 
4
 
 
5
""" dispatch web request onto the plugins dispatcher .. return json data """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
from gozerbot.generic import waitforqueue, handle_exception, rlog
 
10
from gozerbot.fleet import fleet
 
11
from gozerbot.ircevent import Ircevent
 
12
from gozerbot.plugins import plugins
 
13
from gozerbot.thr import start_new_thread
 
14
from gozerplugs.plugs.webserver import httpd
 
15
from gozerbot.contrib.simplejson import dumps
 
16
from urllib import unquote_plus
 
17
import Queue
 
18
 
 
19
def handle_json(event):
 
20
    """ dispatch web request .. return json """
 
21
    input = unquote_plus(event.path)
 
22
    bot = fleet.getfirstbot()
 
23
    ievent = Ircevent()
 
24
    try:
 
25
        what = input.split('?', 1)[1]
 
26
    except IndexError:
 
27
        return ["dispatch what ?", ]
 
28
    if what.startswith("command="):
 
29
        what = what[8:]
 
30
    ievent.txt = what
 
31
    ievent.nick = 'web'
 
32
    ievent.userhost = 'web@web'
 
33
    ievent.channel = 'web'
 
34
    q = Queue.Queue()
 
35
    ievent.queues.append(q)
 
36
    ievent.speed = 3
 
37
    ievent.bot = bot
 
38
    result = []
 
39
    if plugins.woulddispatch(bot, ievent):
 
40
        start_new_thread(plugins.trydispatch, (bot, ievent))
 
41
    else:
 
42
        return ["can't dispatch %s" % ievent.txt, ]
 
43
    result = waitforqueue(q, 3)
 
44
    rlog(10, 'json', str(result))
 
45
    try:
 
46
        res = dumps(result)
 
47
    except Exception, ex:
 
48
        handle_exception()
 
49
        res = []
 
50
    return res 
 
51
 
 
52
if httpd:
 
53
    httpd.addhandler('json', handle_json)