home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / applic / ncsa / Mac / Telnet2.6 / prerelease / d5 / Telnet 2.6.1d5.src.sit.hqx / Telnet 2.6.1d5 src / source / files / ae.c next >
Encoding:
C/C++ Source or Header  |  1995-01-07  |  7.4 KB  |  309 lines

  1. /*
  2. *    ae.c
  3. *    Code to handle the AppleEvents we recognize
  4. *****************************************************************
  5. *    NCSA Telnet for the Macintosh                                *
  6. *                                                                *
  7. *    National Center for Supercomputing Applications                *
  8. *    Software Development Group                                    *
  9. *    152 Computing Applications Building                            *
  10. *    605 E. Springfield Ave.                                        *
  11. *    Champaign, IL  61820                                        *
  12. *                                                                *
  13. *    Copyright (c) 1986-1994,                                    *
  14. *    Board of Trustees of the University of Illinois                *
  15. ****************************************************************/
  16.  
  17. #include <Gestalt.h>
  18. #include <AppleEvents.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. #include "TelnetHeader.h"
  23. #include "general_resrcdefs.h"
  24. #include "wind.h"                // For connections.proto.h
  25. #include "ae.proto.h"
  26. #include "connections.proto.h"    // For OpenConnectionFromURL proto
  27. #include "Sets.proto.h"            // For readconfig proto
  28. #include "macros.proto.h"        // For loadMacro proto
  29. #include "menuseg.proto.h"        // For HandleQuit proto
  30. #include "debug.h"
  31.  
  32. #ifdef MPW
  33. #pragma segment Files
  34. #endif
  35.  
  36. static void ProcessURLEscapeCodes (char *url, char **end);
  37.  
  38. SIMPLE_UPP(MyHandleODoc, AEEventHandler);
  39. pascal OSErr  MyHandleODoc (AppleEvent *theAppleEvent, AppleEvent* reply, long
  40.                                                         handlerRefCon)
  41. {
  42.     FSSpec        myFSS;
  43.     AEDescList    docList;
  44.     OSErr        err;
  45.     long        index, itemsInList;
  46.     Size        actualSize;
  47.     AEKeyword    keywd;
  48.     DescType    returnedType;
  49.     FInfo        fileInfo;
  50.     
  51.     if ((err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList, &docList)) != noErr)
  52.         return err;
  53.  
  54.     // check for missing parameters
  55.     if ((err = MyGotRequiredParams (theAppleEvent)) != noErr)
  56.         return err;
  57.  
  58.     // count the number of descriptor records in the list
  59.     if ((err = AECountItems (&docList, &itemsInList)) != noErr)
  60.         return err;
  61.  
  62.     for (index = 1; index <= itemsInList; index++) {
  63.             err = AEGetNthPtr (&docList, index, typeFSS, &keywd, &returnedType, 
  64.                                 (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  65.             if (err) return err;
  66.             
  67.             FSpGetFInfo(&myFSS, &fileInfo);        /* make sure it's a data file */
  68.             if (fileInfo.fdCreator == kNCSACreatorSignature && fileInfo.fdType == kNCSASetFileType)
  69.                 readconfig(myFSS);        // Read the actual set
  70.             else if(fileInfo.fdCreator == kNCSACreatorSignature && fileInfo.fdType == 'TEXT')
  71.                 loadMacros(&myFSS);
  72.     }
  73.  
  74.     err = AEDisposeDesc (&docList);
  75.     return noErr;
  76. }
  77.  
  78. SIMPLE_UPP(MyHandlePDoc, AEEventHandler);
  79. pascal OSErr  MyHandlePDoc (AppleEvent *theAppleEvent, AppleEvent *reply, long
  80.                                                         handlerRefCon)
  81. {
  82.     // We don't print files.
  83.     return (errAEEventNotHandled);
  84. }
  85.  
  86. SIMPLE_UPP(MyHandleOApp, AEEventHandler);
  87. pascal OSErr  MyHandleOApp (AppleEvent *theAppleEvent, AppleEvent *reply, long
  88.                                                         handlerRefCon)
  89. {
  90.     // Don't need to do anything for OApp.
  91.     return noErr;
  92. }
  93.  
  94. SIMPLE_UPP(MyHandleQuit, AEEventHandler);
  95. pascal OSErr  MyHandleQuit (AppleEvent *theAppleEvent, AppleEvent *reply, long
  96.                                                         handlerRefCon)
  97. {
  98.     if (HandleQuit())
  99.         return(-128);    // userCancelledErr
  100.             
  101.     return(noErr);    
  102. }
  103.  
  104. SIMPLE_UPP(MyHandleGURL, AEEventHandler);
  105. pascal OSErr  MyHandleGURL (AppleEvent *theAppleEvent, AppleEvent* reply, long
  106.                                                         handlerRefCon)
  107. {
  108.     OSErr        err;
  109.     DescType    returnedType;
  110.     Size        actualSize;
  111.     char        URLString[255];
  112.     char        *beg, *end, *user, *password, *portstring, *host;
  113.  
  114.     if ((err = AEGetParamPtr(theAppleEvent, keyDirectObject, typeChar, &returnedType,
  115.                                 URLString, sizeof(URLString)-1, &actualSize)) != noErr)
  116.         return err;
  117.  
  118.     // check for missing parameters
  119.     if ((err = MyGotRequiredParams(theAppleEvent)) != noErr)
  120.         return err;
  121.  
  122.     URLString[actualSize] = 0;        // Terminate the C string
  123.     beg = &URLString[0];
  124.     end = &URLString[actualSize-1];
  125.  
  126.     // Strip leading spaces
  127.     while ((beg < end) && (*beg == ' '))
  128.         beg++;
  129.  
  130.     // Strip trailing spaces
  131.     while ((end < beg) && (*end == ' '))
  132.         end--;
  133.  
  134.     // Look for (and strip) beginning and ending angle brackets
  135.     if (*beg == '<') {
  136.         if (*end != '>') {    // Leading angle bracket, but no trailing angle bracket
  137.             err = paramErr;
  138.             goto badExit;
  139.             }
  140.         
  141.         // Nuke the brackets
  142.         beg++;
  143.         end--;
  144.         }
  145.  
  146.     // Terminate the string we currently have (for strncmp fn's)
  147.     *(end+1) = 0;
  148.  
  149.     // Look for (and strip) leading "URL:"
  150.     if (!strncmp(beg, "URL:", 4)) {
  151.         beg += 4;
  152.         }
  153.  
  154.     // Look for (and strip) leading "telnet://"
  155.     if (strncmp(beg, "telnet://", 9)) {
  156.         err = paramErr;                    // No leading "telnet://"
  157.         goto badExit;
  158.         }
  159.  
  160.     beg += 9;
  161.  
  162.     // Drop any ending slash
  163.     if (*end == '/') {
  164.         *end = 0;
  165.         end--;
  166.         }
  167.  
  168.     // Process any escaped characters
  169.     ProcessURLEscapeCodes(beg, &end);
  170.  
  171.     // Set up for URL parsing
  172.     password = nil;
  173.     portstring = nil;
  174.     host = nil;
  175.  
  176.     // Assume <user> exists
  177.     user = beg;
  178.     
  179.     // Leading : or @ is a no-no
  180.     if ((*beg == ':') || (*beg == '@')) {
  181.         err = paramErr;
  182.         goto badExit;
  183.         }
  184.  
  185.     // Scan for : or @ or end of string
  186.     while ((beg <= end) && (*beg != ':') && (*beg != '@'))
  187.         beg++;
  188.  
  189.     // If we reached the end, only a host was given
  190.     if (beg > end) {
  191.         host = user;
  192.         user = nil;
  193.         goto goodUrl;
  194.         }
  195.  
  196.     // If : was found, scan in the password (or port)
  197.     if (*beg == ':') {
  198.         *beg++ = 0;
  199.         password = beg;
  200.  
  201.         // Scan for : or @ or end of string
  202.         while ((beg <= end) && (*beg != ':') && (*beg != '@'))
  203.             beg++;
  204.  
  205.         if (*beg == ':') {    // xxxx:yyyy:<anything> is a no-no
  206.             err = paramErr;
  207.             goto badExit;
  208.             }
  209.  
  210.         if (*beg != '@') {    // End of string.  Must be host:port
  211.             host = user;
  212.             user = nil;
  213.             portstring = password;
  214.             password = nil;
  215.             goto goodUrl;
  216.             }
  217.  
  218.         // Have xxx:yyy@<something>
  219.         *beg++ = 0;    // Terminate password string
  220.         }        
  221.     else {        // Found @
  222.         *beg++ = 0;    // Terminate user string
  223.         }
  224.         
  225.     // Ok at this point have xxx:yyy@<something> or xxxx@<something>
  226.     host = beg;
  227.  
  228.     // Scan for : or @ or end of string
  229.     while ((beg <= end) && (*beg != ':') && (*beg != '@'))
  230.         beg++;
  231.  
  232.     if (*beg == '@') { //xxx[:yyyy]@xxx@ is a no-no
  233.         err = paramErr;
  234.         goto badExit;
  235.         }
  236.  
  237.     if (*beg != ':') { // End of string, we have xxxx[:yyyy]@zzzz
  238.         *beg = 0;
  239.         }
  240.     else {
  241.         *beg++ = 0;    // Terminate host string
  242.         portstring = beg;
  243.         }
  244.  
  245. goodUrl:
  246.     OpenConnectionFromURL(host, portstring, user, password);
  247.     err = noErr;
  248.  
  249. badExit:
  250.     return err;    
  251. }
  252.  
  253. /*----------------------------------------------------------------------------
  254.     ProcessURLEscapeCodes
  255.     
  256.     Process "%xx" escape codes in a URL string (replace them by the characters
  257.     they represent).
  258.     
  259.     Entry:    url = URL with escape codes.
  260.             
  261.     Exit:    url = URL with escape codes replaced by the characters they
  262.                 represent.
  263.  
  264.     Copyright ⌐ 1994, Northwestern University.
  265.     Modified 12/94 Jim Browne for NCSA
  266. ----------------------------------------------------------------------------*/
  267.  
  268. static void ProcessURLEscapeCodes (char *url, char **end)
  269. {
  270.     char *p, *q;
  271.     char c1, c2;
  272.     
  273.     p = q = url;
  274.     while (*p != 0) {
  275.         if (*p == '%') {
  276.             c1 = tolower(*(p+1));
  277.             c2 = tolower(*(p+2));
  278.             if (isxdigit(c1) && isxdigit(c2)) {
  279.                 c1 = isdigit(c1) ? c1 - '0' : c1 - 'a' + 10;
  280.                 c2 = isdigit(c2) ? c2 - '0' : c2 - 'a' + 10;
  281.                 *q++ = (c1 << 4) + c2;
  282.                 p += 3;
  283.             } else {
  284.                 *q++ = *p++;
  285.             }
  286.         } else {
  287.             *q++ = *p++;
  288.         }
  289.     }
  290.     *q = 0;
  291.     *end = q-1;
  292. }
  293.  
  294. OSErr MyGotRequiredParams (AppleEvent *theAppleEvent)
  295. {
  296.     DescType    returnedType;
  297.     Size        actualSize;
  298.     OSErr        err;
  299.  
  300.     err = AEGetAttributePtr (theAppleEvent, keyMissedKeywordAttr,
  301.                                     typeWildCard, &returnedType, nil, 0,
  302.                                     &actualSize);
  303.     if (err == errAEDescNotFound)    // you got all the required parameters
  304.             return noErr;
  305.     else if (!err)            // you missed a required parameter
  306.             return errAEEventNotHandled;
  307.     else                    // the call to AEGetAttributePtr failed
  308.             return err;
  309. }