#include "SXextend.h" #include #include #define BUFSIZE 8192 char buf[BUFSIZE]; SXobject inQ = NULL; /* * This handler is called when NetScape opens a "scriptx:" url. */ pascal OSErr doHandleOpenURL(AppleEvent *ev, AppleEvent *rep, long ref) { OSErr myErr; DescType gotType; Size gotSize = BUFSIZE; SXobject obj; long transactionID = 1; long windowID = 0; long flags = 0; myErr = AEGetParamPtr(ev, '----', typeChar, &gotType, buf, BUFSIZE, &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXmakeString("OpenURL")); SXwrite(inQ, SXmakeString(buf)); #if 0 myErr = AEGetParamPtr(ev, 'WIND', typeInteger, &gotType, &windowID, sizeof(long), &gotSize); SXwrite(inQ, SXintToObject(windowID)); myErr = AEGetParamPtr(ev, 'FLGS', typeInteger, &gotType, &flags, sizeof(long), &gotSize); SXwrite(inQ, SXintToObject(flags)); myErr = AEGetParamPtr(ev, 'POST', typeChar, &gotType, buf, BUFSIZE, &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXmakeString(buf)); myErr = AEGetParamPtr(ev, 'MIME', typeChar, &gotType, buf, BUFSIZE, &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXmakeString(buf)); myErr = AEPutParamPtr(rep, keyDirectObject, typeInteger, &transactionID, sizeof(long)); #else myErr = AEPutParamPtr(rep, keyDirectObject, typeTrue, NULL, 0); #endif return noErr; } /* * This handler is called when NetScape echos a url. */ pascal OSErr doHandleEchoURL(AppleEvent *ev, AppleEvent *rep, long ref) { OSErr myErr; DescType gotType; Size gotSize = BUFSIZE; SXobject obj; long windowID; myErr = AEGetParamPtr(ev, '----', typeChar, &gotType, buf, BUFSIZE, &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXmakeString("EchoURL")); SXwrite(inQ, SXmakeString(buf)); myErr = AEGetParamPtr(ev, 'MIME', typeChar, &gotType, buf, BUFSIZE, &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXmakeString(buf)); myErr = AEGetParamPtr(ev, 'WIND', typeInteger, &gotType, &windowID, sizeof(long), &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXintToObject(windowID)); myErr = AEGetParamPtr(ev, 'RFRR', typeChar, &gotType, buf, BUFSIZE, &gotSize); if (myErr != noErr) { buf[0] = '\0'; } else { if (gotSize >= BUFSIZE) gotSize = BUFSIZE - 1; buf[gotSize] = 0; } SXwrite(inQ, SXmakeString(buf)); myErr = AEPutParamPtr(rep, keyDirectObject, typeTrue, NULL, 0); return noErr; } /* * This handler is called when NetScape finished opening a URL for us. */ pascal OSErr doHandleOpenURLResult(AppleEvent *ev, AppleEvent *rep, long ref) { OSErr myErr; DescType gotType; long transactionID = 0; long windowID = 0; Size gotSize = BUFSIZE; SXobject obj; myErr = AEGetParamPtr(ev, '----', typeInteger, &gotType, &transactionID, sizeof(long), &gotSize); myErr = AEGetParamPtr(ev, 'WIND', typeInteger, &gotType, &windowID, sizeof(long), &gotSize); SXwrite(inQ, SXmakeString("OpenURLResult")); SXwrite(inQ, SXintToObject(transactionID)); SXwrite(inQ, SXintToObject(windowID)); myErr = AEPutParamPtr(rep, keyDirectObject, typeTrue, NULL, 0); } /* * This function tells NetScape to open a URL. */ /* OpenURL: Opens a URL. Allows for more options than GetURL event OpenURL string -- URL [to 'FSS '] -- file destination [toWindow integer] -- window iD [flags integer] -- Binary: any combination of 1, 2 and 4 is allowed: 1 and 2 mean force reload the document. 4 is ignored [post data string] -- Form posting data [post type string] -- MIME type of the posting data. Defaults to application/x-www-form-urlencoded [progressApp 'psn '] -- Application that will display progress Result: integer -- ID of the loading window */ SXobject doOpenURL(SXobject urlArg) { char *url; OSErr myErr = 0; AEEventClass theAEEventClass; AEEventID theAEEventID; AppleEvent ev, rep; long theAddressType, theAddressCreator; AEAddressDesc addr; url = (char *)SXstringOf(urlArg); theAddressType = 'APPL'; theAddressCreator = 'MOSS'; theAEEventClass = 'WWW!'; theAEEventID = 'OURL'; myErr = AECreateDesc(typeApplSignature, (Ptr)&theAddressCreator, sizeof(theAddressCreator), &addr); if (myErr != 0) { return SXintToObject(myErr); } myErr = AECreateAppleEvent(theAEEventClass, theAEEventID, &addr, kAutoGenerateReturnID, kAnyTransactionID, &ev); if (myErr != 0) { return SXintToObject(myErr); } AEPutParamPtr(&ev, keyDirectObject, typeChar, url, strlen(url)); myErr = AESend(&ev, &rep, kAENoReply + kAECanInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); return SXintToObject(myErr); } /* * This registers handlers and tells NetScape to invoke them. */ SXobject doInitWeb(SXobject inputQueue, SXobject protocolArg) { char *protocol = (char *)SXstringOf(protocolArg); OSErr myErr = 0; AEEventClass theAEEventClass; AEEventID theAEEventID; AppleEvent ev, rep; long theAddressType, theAddressCreator, theSignature; AEAddressDesc addr; int i; inQ = inputQueue; /* * Install an apple event handler for a message NetScape will send us. */ myErr = AEInstallEventHandler('GURL', 'GURL', doHandleOpenURL, (long)inputQueue, false); if (myErr != 0) { return SXintToObject(myErr); } /* * Register the KAP as the handler of the scriptx: protocol. */ theAddressType = 'APPL'; theAddressCreator = 'MOSS'; theAEEventClass = 'WWW!'; theSignature = 'KAP '; /* First, unregister to clean out registry. */ /* unregister protocol: reverses the effects of Òregister protocolÓ unregister protocol 'sign' -- Application sig. [for protocol string] -- protocol prefix. If none, unregister for all protocols [Result: boolean] -- TRUE if successful */ theAEEventID = 'URGP'; myErr = AECreateDesc(typeApplSignature, (Ptr)&theAddressCreator, sizeof(theAddressCreator), &addr); if (myErr != 0) { return SXintToObject(myErr); } myErr = AECreateAppleEvent(theAEEventClass, theAEEventID, &addr, kAutoGenerateReturnID, kAnyTransactionID, &ev); if (myErr != 0) { return SXintToObject(myErr); } AEPutParamPtr(&ev, keyDirectObject, typeApplSignature, (Ptr)&theSignature, sizeof(theSignature)); AEPutParamPtr(&ev, 'PROT', typeChar, (Ptr)"", 0); myErr = AESend(&ev, &rep, kAENoReply + kAECanInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); /* Next, register the KAP as the scriptx: protocol handler. */ /* register protocol: Registers application as a ÒhandlerÓ for this protocol with a given prefix. The handler will receive ÒOpenURLÓ, or if that fails, ÒGetURLÓ event. register protocol 'sign' -- Application sig for protocol string -- protocol prefix: Òfinger:Ó, ÒfileÓ, Result: boolean -- TRUE if registration has been successful */ theAEEventID = 'RGPR'; myErr = AECreateDesc(typeApplSignature, (Ptr)&theAddressCreator, sizeof(theAddressCreator), &addr); if (myErr != 0) { return SXintToObject(myErr); } myErr = AECreateAppleEvent(theAEEventClass, theAEEventID, &addr, kAutoGenerateReturnID, kAnyTransactionID, &ev); if (myErr != 0) { return SXintToObject(myErr); } AEPutParamPtr(&ev, keyDirectObject, typeApplSignature, (Ptr)&theSignature, sizeof(theSignature)); AEPutParamPtr(&ev, 'PROT', typeChar, (Ptr)protocol, strlen(protocol)); myErr = AESend(&ev, &rep, kAENoReply + kAECanInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); if (myErr != 0) { return SXintToObject(myErr); } /* * Install an apple event handler for a message NetScape will send us. */ myErr = AEInstallEventHandler('WWW?', 'URLE', doHandleEchoURL, (long)inQ, false); if (myErr != 0) { return SXintToObject(myErr); } /* * Register the KAP as the echo application. */ /* First, unregister to clean out registry. */ /* unregister URL echo: cancels URL echo unregister URL echo 'sign' -- application signature */ theAEEventID = 'UNRU'; myErr = AECreateDesc(typeApplSignature, (Ptr)&theAddressCreator, sizeof(theAddressCreator), &addr); if (myErr != 0) { return SXintToObject(myErr); } myErr = AECreateAppleEvent(theAEEventClass, theAEEventID, &addr, kAutoGenerateReturnID, kAnyTransactionID, &ev); if (myErr != 0) { return SXintToObject(myErr); } AEPutParamPtr(&ev, keyDirectObject, typeApplSignature, (Ptr)&theSignature, sizeof(theSignature)); myErr = AESend(&ev, &rep, kAENoReply + kAECanInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); /* Next, register the KAP as the echo application. */ /* register URL echo: Registers the ÒechoÓ application. Each download from now on will be echoed to this application. register URL echo 'sign' -- Application signature */ theAEEventID = 'RGUE'; myErr = AECreateDesc(typeApplSignature, (Ptr)&theAddressCreator, sizeof(theAddressCreator), &addr); if (myErr != 0) { return SXintToObject(myErr); } myErr = AECreateAppleEvent(theAEEventClass, theAEEventID, &addr, kAutoGenerateReturnID, kAnyTransactionID, &ev); if (myErr != 0) { return SXintToObject(myErr); } AEPutParamPtr(&ev, keyDirectObject, typeApplSignature, (Ptr)&theSignature, sizeof(theSignature)); myErr = AESend(&ev, &rep, kAENoReply + kAECanInteract + kAECanSwitchLayer, kAENormalPriority, kAEDefaultTimeout, NULL, NULL); if (myErr != 0) { return SXintToObject(myErr); } /* * Install an apple event handler for a message NetScape will send us. */ myErr = AEInstallEventHandler('WWW?', 'OURR', doHandleOpenURLResult, (long)inQ, false); if (myErr != 0) { return SXintToObject(myErr); } return SXintToObject(myErr); } SXobject webFn(SXobject opObj, SXobject arg1, SXobject arg2, SXobject arg3, SXobject arg4) { int op = SXintFrom(opObj); SXobject ret = SXundefined; switch (op) { case 0: ret = doInitWeb(arg1, arg2); break; case 1: ret = doOpenURL(arg1); break; default: break; } return ret; } SXobject entryPoint(SXobject ld, SXobject grp, SXobject unit) { return SXmakePrimitive(webFn, 1, 3); }