<extension>
  <title>MindTouch PageBus Extension</title>
  <copyright>Copyright (c) 2006-2009 MindTouch, Inc.</copyright>
  <uri.help>http://wiki.developer.mindtouch.com/MindTouch_Deki/Extensions/PageBus</uri.help>
  <label>PageBus</label>
  <namespace>pagebus</namespace>
  <description>This extension contains functions for interacting with the PageBus.</description>

  <function>
    <name>alert</name>
    <description>Show messages on a channel in an alert window.</description>
    <param name="subscribe" type="str" optional="true">Subscribe to channel. (default: "*")</param>
    <param name="title" type="str" optional="true">Dialog title. (default: nil)</param>
    <return>
      <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
        <head>
          <script type="text/javascript">
            DekiWiki.subscribe(<eval:js>args.subscribe ?? '*'</eval:js>, null, function(c, m, d) { var s = ''; for(var f in m) s += f + ': ' + m[f] + '\n'; alert(<eval:js>args.title ?? 'Alert Box'</eval:js>  + '\nchannel: [' + c + ']\n' + s); });
          </script>
        </head>
      </html>
    </return>
  </function>

  <function>
    <name>republish</name>
    <description>Republish messages to another channel.</description>
    <param name="subscribe" type="str">Subscribe to channel.</param>
    <param name="publish" type="str">Publish on channel.</param>
    <param name="copy" type="map" optional="true">Fields to copy. (default: all)</param>
    <param name="add" type="map" optional="true">Fields to add. (default: none)</param>
    <return>
      <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
        <head>
          <script type="text/javascript">
            DekiWiki.subscribe(<eval:js>args.subscribe</eval:js>, null, function(c, m, d) {
              var result = m;
              if(d.copy) {
                result = { };
                for(var key in d.copy) result[key] = m[d.copy[key]];
              }
              if(d.add) for(var key in d.add) result[key] = d.add[key];
              DekiWiki.publish(<eval:js>args.publish</eval:js>, result);
            }, { copy: <eval:js>args.copy</eval:js>, add: <eval:js>args.add</eval:js> });
          </script>
        </head>
      </html>
    </return>
  </function>

  <function>
    <name>filter</name>
    <description>Filter messages before copying them to another channel.</description>
    <param name="subscribe" type="str">Subscribe to channel.</param>
    <param name="publish" type="str">Publish on channel.</param>
    <param name="field" type="str">Field to test.</param>
    <param name="pattern" type="str">Regular expression pattern to use for test.</param>
    <return>
      <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
        <head>
          <script type="text/javascript">
            DekiWiki.subscribe(<eval:js>args.subscribe</eval:js>, null, function(c, m, d) {
              if(DekiWiki.hasValue(m[<eval:js>args.field</eval:js>]) &amp;&amp; d.regex.test(m[<eval:js>args.field</eval:js>])) {
                DekiWiki.publish(<eval:js>args.publish</eval:js>, m);
              }
            }, { regex: new RegExp(<eval:js>args.pattern</eval:js>) });
          </script>
        </head>
      </html>
    </return>
  </function>

  <function>
    <name>publish</name>
    <description>Publish a message on a channel.</description>
    <param name="channel" type="str">Channel to publish mesage on.</param>
    <param name="message" type="any">Message to send.</param>
    <return>
      <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
        <tail>
          <script type="text/javascript">
            DekiWiki.publish(<eval:js>args.channel</eval:js>, <eval:js>args.message</eval:js>);
          </script>
        </tail>
      </html>
    </return>
  </function>

  <function>
    <name>publishmany</name>
    <description>Publish a list of messages on a channel.</description>
    <param name="channel" type="str">Channel to publish mesages on.</param>
    <param name="messages" type="list">List of messages to send.</param>
    <return>
      <html xmlns:eval="http://mindtouch.com/2007/dekiscript">
        <tail>
          <script type="text/javascript">
            <eval:foreach var="message" in="args.messages">DekiWiki.publish(<eval:js>args.channel</eval:js>, <eval:js>message</eval:js>);</eval:foreach>
          </script>
        </tail>
      </html>
    </return>
  </function>
</extension>