summaryrefslogtreecommitdiffstats
path: root/askbot_notify.py
blob: c2bfd7fb1244bf4f9fb642e1c261c0ab2cf5780b (plain)
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
# This file is part of askbot_notify.
# Copyright (C) 2013 Alexander Sulfrian <alex@spline.inf.fu-berlin.de>
#
# fedmsg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# fedmsg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with fedmsg; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
""" Plugin to notify via phenny on new messages from an askbot instance.

Enable this plugin by editing the ``settings.py`` file in your askbot
instance.

Find MIDDLEWARE_CLASSES and add 'askbot_notify.NOOPMiddleware'
to the tuple like:

    MIDDLEWARE_CLASSES = (
        ...
        'askbot_notify.NOOPMiddleware',
        ...
    )

"""

import socket
import syslog
from django.core import serializers
from django.dispatch import receiver
from askbot.models.signals import new_question_posted
from askbot.utils.html import site_url

def new_question(sender, **kwargs):
    if 'question' in kwargs:
        q = kwargs['question']
        url = site_url(q.get_absolute_url(no_slug=True))
        msg = "Neue Frage von %s: %s (%s)" % (q.author.username, q.get_question_title(), url)

        s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
        try:
            s.connect('/var/run/phenny/socket')
            s.sendall(msg)
            s.close()
        except socket.error:
            syslog.syslog("Dropping message: '%s'" % msg)

new_question_posted.connect(new_question, weak=False)


class NOOPMiddleware(object):
    """ Register our message-emitting plugin with django.

    Django middleware is supposed to provide a bunch of methods to act on the
    request/response pipeline.  We ignore that and instead use middleware as
    a convenient vector to get ourselves into the askbot runtime environment.

    """
    pass