📄 emaillistenerdestination.cpp
字号:
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================////%/////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Config.h>#include <Pegasus/Common/PegasusVersion.h>#include <iostream>#include <Pegasus/Handler/CIMHandler.h>#include <Pegasus/Repository/CIMRepository.h>#include <Pegasus/Common/Tracer.h>#include <Pegasus/Common/MessageLoader.h>#include <Pegasus/Common/System.h>#include <Pegasus/Common/CIMType.h>#include <Pegasus/Common/IndicationFormatter.h>#include <Pegasus/IndicationService/IndicationConstants.h>#if defined(PEGASUS_OS_VMS)#include <unistd>#include <stdio>#include <descrip>#include <ssdef>#include <maildef>#include <mail$routines>#include <nam>#include <starlet>#endif#if !defined(PEGASUS_OS_HPUX) && !defined(PEGASUS_OS_LINUX) && \ !defined(PEGASUS_OS_VMS)#error "Unsupported Platform"#endif#include "EmailListenerDestination.h"PEGASUS_NAMESPACE_BEGINPEGASUS_USING_STD;void EmailListenerDestination::initialize(CIMRepository* repository){}void EmailListenerDestination::handleIndication( const OperationContext& context, const String nameSpace, CIMInstance& indication, CIMInstance& handler, CIMInstance& subscription, ContentLanguageList& contentLanguages){ PEG_METHOD_ENTER(TRC_IND_HANDLER, "EmailListenerDestination::handleIndication"); String indicationText; try { // gets formatted indication message indicationText = IndicationFormatter::getFormattedIndText( subscription, indication, contentLanguages); // get MailTo from handler instance Array < String > mailTo; handler.getProperty(handler.findProperty( PEGASUS_PROPERTYNAME_LSTNRDST_MAILTO)).getValue().get(mailTo); // get MailSubject from handler instance String mailSubject = String::EMPTY; handler.getProperty(handler.findProperty( PEGASUS_PROPERTYNAME_LSTNRDST_MAILSUBJECT)).getValue().get( mailSubject); // get MailCc from handler instance CIMValue mailCcValue; Array<String> mailCc; Uint32 posMailCc = handler.findProperty( PEGASUS_PROPERTYNAME_LSTNRDST_MAILCC); if (posMailCc != PEG_NOT_FOUND) { mailCcValue = handler.getProperty(posMailCc).getValue(); } if (!mailCcValue.isNull()) { if ((mailCcValue.getType() == CIMTYPE_STRING) && (mailCcValue.isArray())) { mailCcValue.get(mailCc); } } // Sends the formatted indication to the specified recipients _sendViaEmail(mailTo, mailCc, mailSubject, indicationText); } catch (CIMException& c) { PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, c.getMessage()); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, c.getMessage()); } catch (Exception& e) { PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, e.getMessage()); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, e.getMessage()); } catch (...) { PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, "Failed to deliver indication via e-mail."); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms( "Handler.EmailListenerDestination.EmailListenerDestination." "FAILED_TO_DELIVER_INDICATION_VIA_EMAIL", "Failed to deliver indication via e-mail.")); } PEG_METHOD_EXIT();}void EmailListenerDestination::_sendViaEmail( const Array<String>& mailTo, const Array<String>& mailCc, const String& mailSubject, const String& formattedText){ PEG_METHOD_ENTER(TRC_IND_HANDLER, "EmailListenerDestination::_sendViaEmail");#if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_OS_LINUX) || \ defined(PEGASUS_OS_VMS) String exceptionStr; FILE* mailFilePtr; FILE* filePtr; char mailFile[TEMP_NAME_LEN];#ifndef PEGASUS_OS_VMS // Check for proper execute permissions for sendmail if (access(SENDMAIL_CMD, X_OK) < 0) { Tracer::trace(TRC_IND_HANDLER, Tracer::LEVEL4, "Cannot execute %s: %s.", SENDMAIL_CMD, strerror(errno)); MessageLoaderParms parms( "Handler.EmailListenerDestination.EmailListenerDestination." "_MSG_EXECUTE_ACCESS_FAILED", "Cannot execute $0: $1", SENDMAIL_CMD, strerror(errno)); Logger::put_l(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING, "Handler.EmailListenerDestination.EmailListenerDestination." "_MSG_EXECUTE_ACCESS_FAILED", MessageLoader::getMessage(parms)); PEG_METHOD_EXIT(); return; }#endif // open a temporary file to hold the indication mail message _openFile(&filePtr, mailFile); try { _buildMailHeader(mailTo, mailCc, mailSubject, filePtr); // write indication text to the file _writeStrToFile(formattedText, filePtr); fclose(filePtr); } catch (CIMException& c) { fclose(filePtr); unlink(mailFile); PEG_METHOD_EXIT(); return; } try {#ifdef PEGASUS_OS_VMS // // Start mail send process // status = mail$send_begin(&send_context, &nulllist, &nulllist); if (status != SS$_NORMAL) { PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, "Routine mail$send_begin failed."); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms( "Handler.EmailListenerDestination.EmailListenerDestination." "ROUTINE_MAIL_SEND_BEGIN_FAILED.PEGASUS_OS_VMS", "Routine mail$send_begin failed.")); }#endif // send the message _sendMsg(mailFile); } catch (CIMException& c) { unlink(mailFile); PEG_METHOD_EXIT(); return; } unlink(mailFile); PEG_METHOD_EXIT();#else PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, "sendmail is not supported."); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED, MessageLoaderParms( "Handler.EmailListenerDestination.EmailListenerDestination." "UNSUPPORTED_OPERATION", "sendmail is not supported."));#endif PEG_METHOD_EXIT();}void EmailListenerDestination::_buildMailHeader( const Array<String>& mailTo, const Array<String>& mailCc, const String& mailSubject, FILE* filePtr){ PEG_METHOD_ENTER(TRC_IND_HANDLER, "EmailListenerDestination::_buildMailHeader"); String exceptionStr; String mailToStr = _buildMailAddrStr(mailTo); if (mailToStr == String::EMPTY) { PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms( "Handler.EmailListenerDestination.EmailListenerDestination." "DO_NOT_HAVE_EMAIL_ADDRESS", "Do not have an e-mail address.")); }#ifdef PEGASUS_OS_VMS // // Add cc destination to message. // String mailCcStr = _buildMailAddrCcStr(mailCc); // // Write the mailSubject string // String mailSubjectStr = String::EMPTY; mailSubjectStr.append(mailSubject); CString foo = mailSubjectStr.getCString(); attribute_itmlst[0].buffer_length = strlen(foo); attribute_itmlst[0].buffer_address = (long &)foo; status = mail$send_add_attribute( &send_context, attribute_itmlst, &nulllist); if (status != SS$_NORMAL) { PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, "Routine mail$send_add_attribute failed."); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms( "Handler.EmailListenerDestination.EmailListenerDestination." "ROUTINE_MAIL_SEND_ADD_ATTRIBUTE_FAILED.PEGASUS_OS_VMS", "Routine mail$send_add_attribute failed.")); } // // Add filename to bodypart of the message // bodypart_itmlst[0].buffer_length = strlen(mailFileVms); bodypart_itmlst[0].buffer_address = (long &)mailFileVms; status = mail$send_add_bodypart(&send_context, bodypart_itmlst, 0); if (status != SS$_NORMAL) { PEG_TRACE_STRING(TRC_IND_HANDLER, Tracer::LEVEL4, "Routine mail$send_add_bodypart failed.."); PEG_METHOD_EXIT(); throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms( "Handler.EmailListenerDestination.EmailListenerDestination." "ROUTINE_MAIL_SEND_ADD_BODYPART_FAILED.PEGASUS_OS_VMS", "Routine mail$send_add_bodypart failed.")); }#else String mailHdrStr = String::EMPTY; // Write the mailToStr to file mailHdrStr.append("To: "); mailHdrStr.append(mailToStr); _writeStrToFile(mailHdrStr, filePtr); String mailCcStr = _buildMailAddrStr(mailCc); // Write the mailCcStr to file mailHdrStr = String::EMPTY; mailHdrStr.append("Cc: "); mailHdrStr.append(mailCcStr); _writeStrToFile(mailHdrStr, filePtr); // build from string String fromStr = String::EMPTY; fromStr.append("From: ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -