Contents

Introduction

Drupal is a popular Content Management System written in PHP.

<Monkey> I am developing a new site over at Psyc.us and I am using Drupal for the web framework. Drupal is a great open source project and I hope that I can help out both the PSYC and Drupal projects by working towards this integration.

Progress

  • psycnotify.module: notify psyc when there are changes in drupal site
    • As of v1.0 - Notify on comment insert/update/delete
    • As of v1.0.1 Notify on node insert/update/delete
  • psycauth.module: integrate PSYC Authentication into Drupal
    • Not yet, but I'm working on it!

Ideas for improved integration

  • Live chat widget (FLASH?)
  • PSYC-powered Shoutbox (see http://www.last.fm for a good example shoutbox on every page)
  • drupal node per psyc place ... or psyc place per drupal node.
  • unified Authentication
  • Replace RSS with PSYC Newscasting
    • PSYC Feed instead of RSS feed
    • Consume Feeds from the Drupal Aggregator via PSYC

The fruit of this most wonderful tree

Drupal Notify: psycnotify.module

<Monkey 17:45, 6 August 2006 (CEST)> Today I set out to merge a little PSYC into my Drupal. To that end I have started writing a psycnotify.module. Currently the only functionality is to post a psyc notice when there are new or modified comments on any node in Drupal.

This modified version of Wikinotify will work as a Drupal module to notify a PSYC Place when comments are created or edited on the Drupal site.


<?php

/* ------------ psycnotify.module 1.0 ---------- */


/* Drupal hook_help: */
function psycnotify_help($section) {
  if ($section == 'admin/modules#description') {
      return t('Generates notices in PSYC when interesting events happen within Drupal');
  }
}

/* Implement hook_comment: this catches comment event and records them in PSYC */
function psycnotify_comment($comment, $op) {
  if ($op == 'insert' || $op == 'update') {
        $nid = $comment['nid'];
        psycnotify_send($comment['author'],"comment",$op."d",$nid,$comment['comment']);
  }
}

/*
        psycnotify.module 1.0:
        * deliver a change notification to a PSYC place.

        * original written by psyc://goodadvice.pages.de/~fippo
        * flirted up by psyc://psyced.org/~lynX
        * released as freeware. wash your socks with it.
        * adapted into a Drupal module by psyc://psyced.org/~20after4

        More info:
        * http://www.psyc.eu - the answer in messaging and conferencing
        * http://www.psyc.us - psyc blog by 20after4

        --------------------------------------------------------------------------
        this script uses UDP because (a) the wiki is running on the localhost of the
        server (b) you could just as well use TCP (c) then again if you don't mind
        potential fake messages, you can use this across the network too - if the
        network is really congested enough to lose a web notification, is it really
        worth congesting the network even more?

        now comes the two line config zone.

        $psyctarget: if you don't know the uniform of your chatroom, a /status
        command will tell you.

        $psychost: in some cases when you want to use a neat domain name you will
        have to resort to using SRV to map it onto a different host from where your
        webserver resides. since implementing SRV here looks like too much work,
        we simply ask you to provide the ip or hostname of the receiving machine
        where the $psyctarget actually resides on, into $psychost. under normal
        circumstances $psychost has the same hostname as $psyctarget.
*/

function psycnotify_send($user, $type, $action, $node, $summary) {

        $psyctarget = "psyc://dlux.20after4.net/@psyczilla";
        $psychost = "dlux.20after4.net";

        $url = "http://" . $_SERVER['SERVER_NAME'] ."/node/$node";

        // header
        $s = ":_target\t$psyctarget\n"
                . "\n"
                . ":_encoding\tutf-8\n"
                . ":_page\t$url\n"
                . ":_nick_drupal\t$user\n";

        if ($summary) $s .= ":_summary\t$summary\n";

        // method
        $mc = "_notice_update_$type";
        $s .= $mc ."\n";

        // body
        $s .= "(Drupal:[_nick_drupal]) $action a $type on [_page]\n";
        if ($summary) $s .= "saying: [_summary]\n";

        //   $s = iconv("utf-8", "iso-8859-1", $s);

        sendpacket_udp($psychost, ".\n". $s .".\n");
}


function sendpacket_udp($addr,$msg)
{
        $socket = stream_socket_client("udp://" . $addr . ":4404");
        fwrite($socket, $msg);
        fclose($socket);
}
?>

Problems / Limitations

Authentication Hooks

The Authentication API in Drupal 4.7 has a limitation that causes trouble for psyc auth. The problem: Remote auth hooks only get called when a user supplies user@host style username.

  • We want to support psyc://host.tld/~username Uniforms and it seems a lot of other developers have been asking for an authentication hook that doesn't require user@host ids. There is a valid reason for the requirement: Drupal's auth hooks could end up calling several auth modules and each module might be making external TCP connections to validate the user's credentials. This isn't good, especially if the user is using a local ID. I really don't like the way drupal does distributed authentication, however, their API is simple and sufficient for my purpose, if only the hooks actually got called by drupal's authentication core.
    • It turns out that there is a patch in CVS HEAD that addresses this very issue and the discussion can be found at http://drupal.org/node/29147
    • The patch is available here: http://drupal.org/files/issues/patch_47. This fixed the problem once I applied the patch and set up the "drupal.module" settings. Now my psycauth.module gets called on every login attempt. This could be a problem for sites that utilize other authentication methods as well, however, for my purposes (psyc-based auth on www.psyc.us) this will work just fine.

Now that I have that problem worked out I still have to get asynchronous psyc authentication to work within the context of this very synchronous environment.

See also

General discussion on event notification.