String
   ScriptServer::Compile_(const String &sLanguage, const String &sFilename)
   {
      String sContents = FileUtilities::ReadCompleteTextFile(sFilename);

      if (sContents.IsEmpty())
         return "";

      // Create an instance of the script engine and execute the script.
      CComObject<CScriptSiteBasic>* pBasic;
      CComObject<CScriptSiteBasic>::CreateInstance(&pBasic);

      CComQIPtr<IActiveScriptSite> spUnk;

      if (!pBasic)
         return "ScriptServer:: Failed to create instance of script site.";

      spUnk = pBasic; // let CComQIPtr tidy up for us
      pBasic->Initiate(sLanguage, NULL);
      // pBasic->SetObjectContainer(pObjects);
      pBasic->AddScript(sContents);
      pBasic->Run();
      pBasic->Terminate();

      String sErrorMessage = pBasic->GetLastError();

      if (!sErrorMessage.IsEmpty())
         sErrorMessage = "File: " + sFilename + "\r\n" + sErrorMessage;

      return sErrorMessage;

   }
   bool 
   ScriptServer::DoesFunctionExist_(const String &sProcedure)
   {
      // Create an instance of the script engine and execute the script.
      CComObject<CScriptSiteBasic>* pBasic;
      CComObject<CScriptSiteBasic>::CreateInstance(&pBasic);

      CComQIPtr<IActiveScriptSite> spUnk;

      if (!pBasic)
      {
         ErrorManager::Instance()->ReportError(ErrorManager::High, 5017, "ScriptServer::FireEvent", "Failed to create instance of script site.");
         return false;
      }

      spUnk = pBasic; 
      pBasic->Initiate(script_language_, NULL);
      pBasic->AddScript(script_contents_);
      pBasic->Run();
      bool bExists = pBasic->ProcedureExists(sProcedure);
      pBasic->Terminate();
      
      return bExists;
   }
   void 
   ScriptServer::FireEvent(Event e,  const String &sEventCaller, std::shared_ptr<ScriptObjectContainer> pObjects)
   {
      if (!Configuration::Instance()->GetUseScriptServer())
         return;

	  // JDR: stores the name of the method that is fired in the script. http://www.hmailserver.com/forum/viewtopic.php?f=2&t=25497
	  String event_name_ = _T("Unknown");

      switch (e)
      {
      case EventOnClientConnect:
		 event_name_ = _T("OnClientConnect");
         if (!has_on_client_connect_)
            return;
         break;
      case EventOnAcceptMessage:
		 event_name_ = _T("OnAcceptMessage");
         if (!has_on_accept_message_)
            return;
         break;
      case EventOnMessageDeliver:
	     event_name_ = _T("OnMessageDeliver");
         if (!has_on_deliver_message_)
            return;
         break;
      case EventOnBackupCompleted:
		  event_name_ = _T("OnBackupCompleted");
         if (!has_on_backup_completed_)
            return;
         break;
      case EventOnBackupFailed:
		  event_name_ = _T("OnBackupFailed");
         if (!has_on_backup_failed_)
            return;
         break;
      case EventOnError:
		  event_name_ = _T("OnError");
         if (!has_on_error_)
            return;
         break;
      case EventOnDeliveryStart:
		  event_name_ = _T("OnDeliveryStart");
         if (!has_on_delivery_start_)
            return;
         break;
      case EventOnDeliveryFailed:
		  event_name_ = _T("OnDeliveryFailed");
         if (!has_on_delivery_failed_)
            return;
         break;
      case EventOnExternalAccountDownload:
		  event_name_ = _T("OnExternalAccountDownload");
         if (!has_on_external_account_download_)
            return;
         break;
      case EventOnSMTPData:
		  event_name_ = _T("OnSMTPData");
         if (!has_on_smtpdata_)
            return;
         break;

      case EventCustom:
         break;
      default:
         {
            return;
         }
         
      }


	   LOG_DEBUG("Executing event " + event_name_);

      String sScript;

      // Build the script.
      if (script_language_ == _T("VBScript"))
         sScript = script_contents_ + "\r\n\r\n" + "Call " + sEventCaller + "\r\n";
      else if (script_language_ == _T("JScript"))
         sScript = script_contents_ + "\r\n\r\n" + sEventCaller + ";\r\n";

      CComObject<CScriptSiteBasic>* pBasic;
      CComObject<CScriptSiteBasic>::CreateInstance(&pBasic);
      CComQIPtr<IActiveScriptSite> spUnk;
      
      if (!pBasic)
      {
         ErrorManager::Instance()->ReportError(ErrorManager::High, 5018, "ScriptServer::FireEvent", "Failed to create instance of script site.");
         return;
      }
      
      spUnk = pBasic; // let CComQIPtr tidy up for us
      pBasic->Initiate(script_language_, NULL);
      pBasic->SetObjectContainer(pObjects);
      pBasic->AddScript(sScript);
      pBasic->Run();
      pBasic->Terminate();

      LOG_DEBUG("Event completed");
   }
Esempio n. 4
0
   void 
   ScriptServer::FireEvent(Event e,  const String &sEventCaller, shared_ptr<ScriptObjectContainer> pObjects)
   {
      if (!Configuration::Instance()->GetUseScriptServer())
         return;

	  // JDR: stores the name of the method that is fired in the script. http://www.hmailserver.com/forum/viewtopic.php?f=2&t=25497
	  String m_sEventName = _T("Unknown");

      switch (e)
      {
      case EventOnClientConnect:
		 m_sEventName = _T("OnClientConnect");
         if (!m_bHasOnClientConnect)
            return;
         break;
      case EventOnAcceptMessage:
		 m_sEventName = _T("OnAcceptMessage");
         if (!m_bHasOnAcceptMessage)
            return;
         break;
      case EventOnMessageDeliver:
	     m_sEventName = _T("OnMessageDeliver");
         if (!m_bHasOnDeliverMessage)
            return;
         break;
      case EventOnBackupCompleted:
		  m_sEventName = _T("OnBackupCompleted");
         if (!m_bHasOnBackupCompleted)
            return;
         break;
      case EventOnBackupFailed:
		  m_sEventName = _T("OnBackupFailed");
         if (!m_bHasOnBackupFailed)
            return;
         break;
      case EventOnError:
		  m_sEventName = _T("OnError");
         if (!m_bHasOnError)
            return;
         break;
      case EventOnDeliveryStart:
		  m_sEventName = _T("OnDeliveryStart");
         if (!m_bHasOnDeliveryStart)
            return;
         break;
      case EventOnDeliveryFailed:
		  m_sEventName = _T("OnDeliveryFailed");
         if (!m_bHasOnDeliveryFailed)
            return;
         break;
      case EventOnExternalAccountDownload:
		  m_sEventName = _T("OnExternalAccountDownload");
         if (!m_bHasOnExternalAccountDownload)
            return;
         break;
      case EventOnSMTPData:
		  m_sEventName = _T("OnSMTPData");
         if (!m_bHasOnSMTPData)
            return;
         break;

      case EventCustom:
         break;
      default:
         {
            return;
         }
         
      }

	  // JDR: Added event name to the debug log. http://www.hmailserver.com/forum/viewtopic.php?f=2&t=25497
	  LOG_DEBUG("ScriptServer::FireEvent-" + m_sEventName);

      String sScript;

      // Build the script.
      if (m_sScriptLanguage == _T("VBScript"))
         sScript = m_sScriptContents + "\r\n\r\n" + "Call " + sEventCaller + "\r\n";
      else if (m_sScriptLanguage == _T("JScript"))
         sScript = m_sScriptContents + "\r\n\r\n" + sEventCaller + ";\r\n";

      CComObject<CScriptSiteBasic>* pBasic;
      CComObject<CScriptSiteBasic>::CreateInstance(&pBasic);
      CComQIPtr<IActiveScriptSite> spUnk;
      
      if (!pBasic)
      {
         ErrorManager::Instance()->ReportError(ErrorManager::High, 5018, "ScriptServer::FireEvent", "Failed to create instance of script site.");
         return;
      }
      
      spUnk = pBasic; // let CComQIPtr tidy up for us
      pBasic->Initiate(m_sScriptLanguage, NULL);
      pBasic->SetObjectContainer(pObjects);
      pBasic->AddScript(sScript);
      pBasic->Run();
      pBasic->Terminate();

      LOG_DEBUG("ScriptServer:~FireEvent");
   }