# This file is part of askbot_notify. # Copyright (C) 2013 Alexander Sulfrian # # 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