home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2002 March / PCWMAR02.iso / software / windowsxp / ftgateoffice / ftgateoffice.exe / Main / command.fts < prev    next >
Encoding:
Text File  |  2001-11-29  |  6.3 KB  |  302 lines

  1. <%
  2. // command.fts
  3. // PURPOSE:
  4. // This script implements some of the functionality of the FTGate command processor.
  5. //
  6. // USE:
  7. // to use this you need to create a mailbox called command and add the following to the 
  8. // script command box
  9. // command.fts
  10. //
  11. // WARNING:
  12. // This script is supplied for reference only, it should be considered untested
  13. // with no warranty either written or implied. Use is strictly at you own risk.
  14. //
  15.  
  16.     #include <systemconst.fts>
  17.     var log=new server.log
  18.     log.write(3,"Executing command processor.fts")
  19.     log.write(3,"Message from \""+message.header.fromname+"\"<"+message.header.fromaddress+">");
  20.  
  21.     var outmsg=new server.newmessage;
  22.     outmsg.new()
  23.     outmsg.write.sender(mailbox.address)
  24.     outmsg.write.recipient(message.header.fromaddress)
  25.     outmsg.write("From: "+mailbox.description+" <"+mailbox.address+">\n");
  26.     outmsg.write("To: "+message.header.fromname+" <"+message.header.fromaddress+">\n");
  27.     outmsg.write("Subject: RE:"+message.header.subject+"\n\n");
  28.  
  29.     // 
  30.     // declare any variables needed in the system
  31.     //
  32.     const AdminPassword="password"
  33.     var AdministratorMode=false;
  34.     var IsMime=false;
  35.     var CurrentDomain
  36.     var CurrentMailbox
  37.  
  38.     //
  39.     // now we declare the functions that we will need for the command processor
  40.     //
  41.  
  42.  
  43.     function write(msg)
  44.     {
  45.         outmsg.write(msg+"\n");
  46.         log.write(3,msg);
  47.     }
  48.  
  49. function IsAdministrator(password)
  50. {
  51. //    write("Checking "+password+"=="+AdminPassword)
  52.     AdministratorMode=(password==AdminPassword);
  53.     return AdministratorMode;
  54. }
  55.  
  56. function NextText()
  57. {
  58.     var Text;
  59.     if (IsMime)
  60.     {
  61.         if (!message.mime.nexttext())
  62.             Text=false;
  63.         else
  64.             Text=message.mime.text
  65.     }
  66.     else
  67.     {
  68.         if (!message.nexttext())
  69.             Text=false;
  70.         else
  71.             Text=message.text
  72.     }
  73.     return Text;
  74. }
  75.  
  76. function ProcessMailbox(inname,password)
  77. {
  78.     CurrentDomain="";
  79.     CurrentMailbox="";
  80.     // first open the mailbox
  81.     var ok=false
  82.     var d=new server.domain
  83.     var mailboxname=string(inname)
  84.     var i=mailboxname.indexof("@",1)
  85.     var domainname=mailbox.domain
  86.     if (i!=-1)
  87.     {
  88.         domainname=mailboxname.slice(i+1,mailbox.length)
  89.         mailboxname=mailboxname.slice(0,i)
  90.     }
  91.     if (d.domainname=domainname)
  92.     {
  93.         var mbx=new d.mailbox
  94.         if (mbx.name=mailboxname)
  95.         {
  96.             if (AdministratorMode || mbx.verifypassword(password))
  97.             {
  98.                 write("opened mailbox \""+mailboxname+"\"")
  99.                 CurrentMailbox=mbx.name
  100.                 CurrentDomain=d.domainname
  101.                 ok=true
  102.             }
  103.             else
  104.             {
  105.                 write("failed to open mailbox \""+mailboxname+"\" - password error")
  106.                 ok=false;
  107.             }
  108.         }
  109.         else
  110.         {
  111.             write("failed to open mailbox \""+mailboxname+"\"")
  112.             ok=false;
  113.         }
  114.     }
  115.     else
  116.     {
  117.         write("failed to open domain \""+domainname+"\"")
  118.         ok=false;
  119.     }
  120.  
  121.     return ok;
  122. }
  123.  
  124.  
  125. function ProcessMessage()
  126. {
  127.     var Text;
  128.     while (1)
  129.     {
  130.         Text=NextText();
  131.         if (!Text)
  132.             break;
  133. //        write("Text=\""+Text+"\"");
  134.         // now decide what we want to do
  135.         // look for comments
  136.         if (Text.Slice(0,2)=="\\")
  137.             continue;
  138.         if (Text.slice(0,13)=="administrator")
  139.         {
  140.             write("Administrator Access Requested ");
  141.             if (IsAdministrator(Text.slice(14,Text.length-1)))
  142.                 write("admin mode granted")
  143.             else
  144.             {
  145.                 write("admin mode DENIED")
  146.                 break;
  147.             }
  148.             continue
  149.         }
  150.         // now look at the other commands
  151.         if (Text.slice(0,12)=="open mailbox")
  152.         {
  153.             // first parameter is mailbox
  154.             // with an optional password
  155.             var mailbox=Text.slice(13,text.length-1)
  156.             var s=mailbox.indexof(",",1)
  157.             if (s!=-1)
  158.             {
  159.                 write(text.slice(0,13+s))
  160.                 // they added a password
  161.                 // so split it and access the mailbox
  162.                 var password=mailbox.slice(s+1,mailbox.length)
  163.                 mailbox=mailbox.slice(0,s)
  164.                 if (!ProcessMailbox(mailbox,password))
  165.                     break;
  166.             }
  167.             else
  168.             {
  169.                 write(text)
  170.                 // no password
  171.                 if (!AdministratorMode)
  172.                 {
  173.                     write("access DENIED")
  174.                     break;
  175.                 }
  176.                 if (!ProcessMailbox(mailbox,""))
  177.                     break;
  178.             }
  179.             continue
  180.         }
  181.         if (Text.slice(0,9)=="subscribe")
  182.         {
  183.             if (CurrentMailbox!="" && CurrentDomain!="")
  184.             {
  185.                 var d=new server.domain
  186.                 d.domainname=CurrentDomain
  187.                 var mbx=new d.mailbox
  188.                 mbx.name=CurrentMailbox
  189.                 if (mbx.type==mbxTypeList)
  190.                 {
  191.                     // we can subscribe the named person
  192.                     // format = subscribe address,[name]
  193.                     var address=Text.slice(10,Text.length-1)
  194.                     var s=address.indexof(",",1)
  195.                     var name=""
  196.                     if (s!=-1)
  197.                     {
  198.                         name=address.slice(s+1,address.length)
  199.                         address=address.slice(0,s)
  200.                     }
  201.                     if (mbx.member.new(address,name))
  202.                         write("Subscribed <"+address+"> \""+Name+"\"")
  203.                     else
  204.                         write("Subscribe failed <"+address+"> \""+Name+"\"")
  205.  
  206.                 }
  207.                 else
  208.                 {
  209.                     write("Mailbox is not a list mailbox");
  210.                 }
  211.                 continue;
  212.             }
  213.             else
  214.             {
  215.                 write("No open mailbox");
  216.                 break;
  217.             }
  218.         }
  219.         if (Text.slice(0,11)=="unsubscribe")
  220.         {
  221.             if (CurrentMailbox!="" && CurrentDomain!="")
  222.             {
  223.                 var d=new server.domain
  224.                 d.domainname=CurrentDomain
  225.                 var mbx=new d.mailbox
  226.                 mbx.name=CurrentMailbox
  227.                 if (mbx.type==mbxTypeList)
  228.                 {
  229.                     // we can subscribe the named person
  230.                     // format = subscribe address,[name]
  231.                     var address=Text.slice(12,Text.length-1)
  232.                     if (mbx.member.delete(address))
  233.                         write("Unsubscribed <"+address+">")
  234.                     else
  235.                         write("Unsubscribe failed <"+address+">")
  236.  
  237.                 }
  238.                 else
  239.                 {
  240.                     write("Mailbox is not a list mailbox");
  241.                 }
  242.                 continue;
  243.             }
  244.             else
  245.             {
  246.                 write("No open mailbox");
  247.                 break;
  248.             }
  249.         }
  250.  
  251.     }
  252.     return 0
  253. }
  254.  
  255.     // 
  256.     // this is the main section
  257.     // we read the message and process the lines in it
  258.     // align the message with the start of the instructions
  259.     // and then process it
  260.     //
  261.  
  262.     // is it mime or not
  263.     if (message.header.ismime)
  264.     {
  265.         // yes - look for a plain text section
  266.         // and process it
  267.         if (message.mime.firstsection())
  268.         {
  269.             while (1)
  270.             {
  271.                 if (message.mime.contenttype=="text/plain")
  272.                 {
  273.                     // this is the bit to process
  274.                     IsMime=true
  275.                     ProcessMessage();
  276.                     break;
  277.                 }
  278.                 if (!message.mime.nextsection())
  279.                     break;
  280.             }
  281.         }
  282.     }
  283.     else
  284.     {
  285.         // this is plain text
  286.         // skip the header and then process it
  287.         var InHeader=true;
  288.  
  289.         message.firsttext()
  290.         while (message.text)
  291.         {
  292.             if (InHeader && message.text=="\n")
  293.             {
  294.                 IsMime=false;
  295.                 ProcessMessage()
  296.                 break;
  297.             }
  298.             message.nexttext()
  299.         }
  300.     }
  301.     outmsg.close()
  302. %>