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

« back to all changes in this revision

Viewing changes to gozerplugs/web/direct.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/dispatch.py
 
2
#
 
3
#
 
4
 
 
5
""" dispatch web request onto the plugins dispatcher """
 
6
 
 
7
__copyright__ = 'this file is in the public domain'
 
8
 
 
9
from gozerbot.generic import waitforqueue, handle_exception
 
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 urllib import unquote_plus
 
16
import Queue
 
17
 
 
18
def handle_dispatch(event):
 
19
    """ dispatch web request """
 
20
    input = unquote_plus(event.path)
 
21
    bot = fleet.getfirstbot()
 
22
    ievent = Ircevent()
 
23
    try:
 
24
        what = input.split('?', 1)[1]
 
25
    except IndexError:
 
26
        return ["dispatch what ?", ]
 
27
    if what.startswith("command="):
 
28
        what = what[8:]
 
29
    ievent.txt = what
 
30
    ievent.nick = 'web'
 
31
    ievent.userhost = 'web@web'
 
32
    ievent.channel = 'web'
 
33
    q = Queue.Queue()
 
34
    ievent.queues.append(q)
 
35
    ievent.speed = 3
 
36
    ievent.bot = bot
 
37
    result = []
 
38
    if plugins.woulddispatch(bot, ievent):
 
39
        start_new_thread(plugins.trydispatch, (bot, ievent))
 
40
    else:
 
41
        return ["can't dispatch %s" % what, ]
 
42
    result = waitforqueue(q, 60)
 
43
    if not result:
 
44
        return ["can't dispatch %s" % what, ]
 
45
    return result
 
46
 
 
47
if httpd:
 
48
    httpd.addhandler('dispatch', handle_dispatch)