/* urlrecv.c
 *
 * Wimp function library
 *  Joseph Heenan, 1998.
 *
 * Implements:
 *
 *  + the Acorn URI protocol, as defined in <http://www.acorn.com/browser/uri/funcspec.html>
 *  + the ANT URL protocol, as defined in <http://www.ant.co.uk/support/tech/notes/url.html>
 *
 * $Log: urlrecv,v $
 * Revision 1.1  1998/08/13 15:28:59  joseph
 * Added setinfover and url/uri receiving and sending
 *
 *
 */


#include <string.h>
#include <stdlib.h>

#include "kernel.h"
#include "wimplib.h"
#include "wimpclib.h"

#include "urldefs.h"


/* Called when wantACK wimp message with type Wimp_MOpenUrl or URI_MProcess is received.
 *
 * NB. Modifies your_ref & other fields in mess->hdr
 */
void url_recvbroadcast( WimpMessage *mess, url_handler_func handler )
{
  url_message *msg = (url_message *) mess;
  char *url;

  if ( mess->hdr.action_code == URI_MProcess )
  {
    uri_processmessage *msg = (uri_processmessage *) mess;
    int checkonly = msg->flags & 0x01;

    if ( (*handler)(msg->uri, checkonly) )
    {
      /* url handled (or can be handled */
      mess->hdr.your_ref    = mess->hdr.my_ref;
      mess->hdr.action_code = URI_MProcessAck;
      E_CHECK( wimp_send_message( Wimp_EUserMessageAcknowledge, mess, mess->hdr.sender, 0, NULL ) );
    }
    return;
  }
  else if ( mess->hdr.action_code != Wimp_MOpenUrl )
    return;

  if ( msg->data.indirect.tag == 0 )
  {
    unsigned int offset = msg->data.indirect.url.offset;
    /* indirect message */
    if ( offset == 0 )
      return; /* NULL url */
    else if ( offset < 236 )
      /* offset inside message block */
      url = (char *)&msg->data + offset;
    else if ( offset >= 0x01800000 )
      /* pointer into shared memory */
      url = msg->data.indirect.url.ptr;
    else
      return; /* reserved value. */
  }
  else
  {
    /* url must be inside body of message */
    url = msg->data.url;
  }

  if ( (*handler)( url, 0 ) )
  {
    /* url handled, ack message */
    mess->hdr.your_ref = mess->hdr.my_ref;
    E_CHECK( wimp_send_message( Wimp_EUserMessageAcknowledge, mess, mess->hdr.sender, 0, NULL ) );
  }
}

