(file) Return to EmailListenerDestination.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Handler / EmailListenerDestination

  1 martin 1.21 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.22 //
  3 martin 1.21 // Licensed to The Open Group (TOG) under one or more contributor license
  4             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5             // this work for additional information regarding copyright ownership.
  6             // Each contributor licenses this file to you under the OpenPegasus Open
  7             // Source License; you may not use this file except in compliance with the
  8             // License.
  9 martin 1.22 //
 10 martin 1.21 // Permission is hereby granted, free of charge, to any person obtaining a
 11             // copy of this software and associated documentation files (the "Software"),
 12             // to deal in the Software without restriction, including without limitation
 13             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14             // and/or sell copies of the Software, and to permit persons to whom the
 15             // Software is furnished to do so, subject to the following conditions:
 16 martin 1.22 //
 17 martin 1.21 // The above copyright notice and this permission notice shall be included
 18             // in all copies or substantial portions of the Software.
 19 martin 1.22 //
 20 martin 1.21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.22 //
 28 martin 1.21 //////////////////////////////////////////////////////////////////////////
 29 yi.zhou 1.1  //
 30              //%/////////////////////////////////////////////////////////////////////////////
 31              
 32              #include <Pegasus/Common/Config.h>
 33              #include <Pegasus/Common/PegasusVersion.h>
 34              
 35              #include <iostream>
 36              #include <Pegasus/Handler/CIMHandler.h>
 37              #include <Pegasus/Repository/CIMRepository.h>
 38              #include <Pegasus/Common/Tracer.h>
 39              #include <Pegasus/Common/MessageLoader.h>
 40              #include <Pegasus/Common/System.h>
 41              #include <Pegasus/Common/CIMType.h>
 42 carson.hovey 1.9  #include <Pegasus/IndicationService/IndicationConstants.h>
 43 yi.zhou      1.1  
 44 thilo.boehm  1.23 #include <Pegasus/General/IndicationFormatter.h>
 45                   
 46 carson.hovey 1.9  #if defined(PEGASUS_OS_VMS)
 47                   #include <unistd>
 48                   #include <stdio>
 49                   #include <descrip>
 50                   #include <ssdef>
 51                   #include <maildef>
 52                   #include <mail$routines>
 53                   #include <nam>
 54                   #include <starlet>
 55                   #endif
 56                   
 57                   #if !defined(PEGASUS_OS_HPUX) && !defined(PEGASUS_OS_LINUX) && \
 58                       !defined(PEGASUS_OS_VMS)
 59                   #error "Unsupported Platform"
 60 yi.zhou      1.2  #endif
 61                   
 62 yi.zhou      1.1  #include "EmailListenerDestination.h"
 63                   
 64                   PEGASUS_NAMESPACE_BEGIN
 65                   
 66                   PEGASUS_USING_STD;
 67                   
 68 kumpf        1.10 void EmailListenerDestination::initialize(CIMRepository* repository)
 69 yi.zhou      1.1  {
 70                   }
 71                   
 72                   void EmailListenerDestination::handleIndication(
 73 kumpf        1.10     const OperationContext& context,
 74                       const String nameSpace,
 75                       CIMInstance& indication,
 76                       CIMInstance& handler,
 77                       CIMInstance& subscription,
 78                       ContentLanguageList& contentLanguages)
 79 yi.zhou      1.1  {
 80 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
 81                           "EmailListenerDestination::handleIndication");
 82 carson.hovey 1.9  
 83 kumpf        1.10     String indicationText;
 84 carson.hovey 1.9  
 85 kumpf        1.10     try
 86                       {
 87 marek        1.17         PEG_TRACE ((TRC_INDICATION_GENERATION, Tracer::LEVEL4,
 88 w.otsuka     1.15             "EmailListenerDestination %s:%s.%s processing %s Indication",
 89                              (const char*)(nameSpace.getCString()),
 90                              (const char*)(handler.getClassName().getString().getCString()),
 91                              (const char*)(handler.getProperty(
 92                              handler.findProperty(PEGASUS_PROPERTYNAME_NAME)).
 93                              getValue().toString().getCString()),
 94                              (const char*)(indication.getClassName().getString().
 95                              getCString())));
 96                   
 97 kumpf        1.10         // gets formatted indication message
 98                           indicationText = IndicationFormatter::getFormattedIndText(
 99                               subscription, indication, contentLanguages);
100                   
101                           // get MailTo from handler instance
102                           Array < String > mailTo;
103                           handler.getProperty(handler.findProperty(
104                               PEGASUS_PROPERTYNAME_LSTNRDST_MAILTO)).getValue().get(mailTo);
105                   
106                           // get MailSubject from handler instance
107 kumpf        1.14         String mailSubject;
108 kumpf        1.10         handler.getProperty(handler.findProperty(
109                               PEGASUS_PROPERTYNAME_LSTNRDST_MAILSUBJECT)).getValue().get(
110                                   mailSubject);
111                   
112                           // get MailCc from handler instance
113                           CIMValue mailCcValue;
114                           Array<String> mailCc;
115                   
116                           Uint32 posMailCc = handler.findProperty(
117                               PEGASUS_PROPERTYNAME_LSTNRDST_MAILCC);
118                   
119                           if (posMailCc != PEG_NOT_FOUND)
120                           {
121                               mailCcValue = handler.getProperty(posMailCc).getValue();
122                           }
123                   
124                           if (!mailCcValue.isNull())
125                           {
126                               if ((mailCcValue.getType() == CIMTYPE_STRING) &&
127                                   (mailCcValue.isArray()))
128                               {
129 kumpf        1.10                 mailCcValue.get(mailCc);
130                               }
131                           }
132 yi.zhou      1.1  
133 kumpf        1.10         // Sends the formatted indication to the specified recipients
134                           _sendViaEmail(mailTo, mailCc, mailSubject, indicationText);
135                       }
136                       catch (CIMException& c)
137                       {
138 thilo.boehm  1.20         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1, "CIMException: %s",
139                               (const char*)c.getMessage().getCString()));
140 kumpf        1.10         PEG_METHOD_EXIT();
141                           throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, c.getMessage());
142                       }
143                       catch (Exception& e)
144 yi.zhou      1.1      {
145 thilo.boehm  1.20         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1, "Exception: %s",
146                               (const char*)e.getMessage().getCString()));
147 kumpf        1.10         PEG_METHOD_EXIT();
148                           throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, e.getMessage());
149 yi.zhou      1.1      }
150 kumpf        1.10     catch (...)
151 yi.zhou      1.1      {
152 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
153 kumpf        1.10             "Failed to deliver indication via e-mail.");
154                           PEG_METHOD_EXIT();
155                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
156                               "Handler.EmailListenerDestination.EmailListenerDestination."
157                                   "FAILED_TO_DELIVER_INDICATION_VIA_EMAIL",
158                               "Failed to deliver indication via e-mail."));
159 yi.zhou      1.1      }
160                   
161 carson.hovey 1.9      PEG_METHOD_EXIT();
162 yi.zhou      1.1  }
163                   
164                   void EmailListenerDestination::_sendViaEmail(
165 kumpf        1.10     const Array<String>& mailTo,
166                       const Array<String>& mailCc,
167                       const String& mailSubject,
168                       const String& formattedText)
169 yi.zhou      1.1  {
170 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
171                           "EmailListenerDestination::_sendViaEmail");
172 carson.hovey 1.9  
173                   #if defined(PEGASUS_OS_HPUX) || defined(PEGASUS_OS_LINUX) || \
174                       defined(PEGASUS_OS_VMS)
175 yi.zhou      1.1  
176 kumpf        1.10     String exceptionStr;
177                       FILE* filePtr;
178                       char mailFile[TEMP_NAME_LEN];
179 carson.hovey 1.9  
180                   #ifndef PEGASUS_OS_VMS
181 kumpf        1.10     // Check for proper execute permissions for sendmail
182                       if (access(SENDMAIL_CMD, X_OK) < 0)
183                       {
184 kumpf        1.18         Logger::put_l(Logger::STANDARD_LOG, System::CIMSERVER, Logger::WARNING,
185 kumpf        1.19             MessageLoaderParms(
186                                   "Handler.EmailListenerDestination.EmailListenerDestination."
187                                       "_MSG_EXECUTE_ACCESS_FAILED",
188                                   "Cannot execute $0: $1",
189                                   SENDMAIL_CMD,
190                                   strerror(errno)));
191 kumpf        1.10 
192                           PEG_METHOD_EXIT();
193                           return;
194                       }
195 carson.hovey 1.9  #endif
196                   
197 kumpf        1.10     // open a temporary file to hold the indication mail message
198                       _openFile(&filePtr, mailFile);
199                   
200                       try
201                       {
202                           _buildMailHeader(mailTo, mailCc, mailSubject, filePtr);
203                   
204                           // write indication text to the file
205                           _writeStrToFile(formattedText, filePtr);
206 carson.hovey 1.9  
207 kumpf        1.10         fclose(filePtr);
208                       }
209 kumpf        1.16     catch (CIMException&)
210 kumpf        1.10     {
211                           fclose(filePtr);
212                           unlink(mailFile);
213 yi.zhou      1.1  
214 kumpf        1.10         PEG_METHOD_EXIT();
215                           return;
216                       }
217 yi.zhou      1.1  
218 kumpf        1.10     try
219                       {
220 carson.hovey 1.9  #ifdef PEGASUS_OS_VMS
221 kumpf        1.10         //
222                           // Start mail send process
223                           //
224                           status = mail$send_begin(&send_context, &nulllist, &nulllist);
225                           if (status != SS$_NORMAL)
226                           {
227 marek        1.17             PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
228 kumpf        1.10                 "Routine mail$send_begin failed.");
229                               PEG_METHOD_EXIT();
230                   
231                               throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
232                                   "Handler.EmailListenerDestination.EmailListenerDestination."
233                                       "ROUTINE_MAIL_SEND_BEGIN_FAILED.PEGASUS_OS_VMS",
234                                   "Routine mail$send_begin failed."));
235                           }
236                   #endif
237                           // send the message
238                           _sendMsg(mailFile);
239                       }
240 kumpf        1.16     catch (CIMException&)
241 carson.hovey 1.9      {
242 kumpf        1.10         unlink(mailFile);
243                   
244                           PEG_METHOD_EXIT();
245                           return;
246 yi.zhou      1.1      }
247 kumpf        1.10 
248 carson.hovey 1.9      unlink(mailFile);
249                   
250                       PEG_METHOD_EXIT();
251 yi.zhou      1.1  
252 kumpf        1.10 #else
253 yi.zhou      1.1  
254 marek        1.17     PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
255 kumpf        1.10                          "sendmail is not supported.");
256                       PEG_METHOD_EXIT();
257 yi.zhou      1.1  
258 kumpf        1.10     throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_NOT_SUPPORTED, MessageLoaderParms(
259                           "Handler.EmailListenerDestination.EmailListenerDestination."
260                               "UNSUPPORTED_OPERATION",
261                           "sendmail is not supported."));
262 carson.hovey 1.9  #endif
263 yi.zhou      1.1  
264 kumpf        1.10     PEG_METHOD_EXIT();
265 carson.hovey 1.9  }
266 yi.zhou      1.1  
267 carson.hovey 1.9  void EmailListenerDestination::_buildMailHeader(
268 kumpf        1.10     const Array<String>& mailTo,
269                       const Array<String>& mailCc,
270                       const String& mailSubject,
271                       FILE* filePtr)
272 carson.hovey 1.9  {
273 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
274                           "EmailListenerDestination::_buildMailHeader");
275 yi.zhou      1.1  
276 kumpf        1.10     String exceptionStr;
277 yi.zhou      1.1  
278 kumpf        1.10     String mailToStr = _buildMailAddrStr(mailTo);
279 yi.zhou      1.1  
280 kumpf        1.10     if (mailToStr == String::EMPTY)
281                       {
282                           PEG_METHOD_EXIT();
283                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
284                                "Handler.EmailListenerDestination.EmailListenerDestination."
285                                    "DO_NOT_HAVE_EMAIL_ADDRESS",
286                                "Do not have an e-mail address."));
287                       }
288 w.otsuka     1.15     PEG_TRACE ((TRC_INDICATION_GENERATION, Tracer::LEVEL3,
289                          "EmailListenerDestination sending Indication via email to %s",
290                          (const char*)(mailToStr.getCString())));
291 carson.hovey 1.9  
292                   #ifdef PEGASUS_OS_VMS
293                   
294 kumpf        1.10     //
295                       // Add cc destination to message.
296                       //
297                   
298                       String mailCcStr = _buildMailAddrCcStr(mailCc);
299                   
300                       //
301                       // Write the mailSubject string
302                       //
303                   
304 kumpf        1.14     String mailSubjectStr;
305 kumpf        1.10     mailSubjectStr.append(mailSubject);
306                       CString foo = mailSubjectStr.getCString();
307                   
308                       attribute_itmlst[0].buffer_length = strlen(foo);
309                       attribute_itmlst[0].buffer_address = (long &)foo;
310                   
311                       status = mail$send_add_attribute(
312                           &send_context,
313                           attribute_itmlst,
314                           &nulllist);
315                       if (status != SS$_NORMAL)
316                       {
317 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
318 kumpf        1.10             "Routine mail$send_add_attribute failed.");
319                           PEG_METHOD_EXIT();
320                   
321                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
322                               "Handler.EmailListenerDestination.EmailListenerDestination."
323                                   "ROUTINE_MAIL_SEND_ADD_ATTRIBUTE_FAILED.PEGASUS_OS_VMS",
324                               "Routine mail$send_add_attribute failed."));
325                       }
326                   
327                       //
328                       // Add filename to bodypart of the message
329                       //
330 yi.zhou      1.1  
331 kumpf        1.10     bodypart_itmlst[0].buffer_length = strlen(mailFileVms);
332                       bodypart_itmlst[0].buffer_address = (long &)mailFileVms;
333 yi.zhou      1.1  
334 kumpf        1.10     status = mail$send_add_bodypart(&send_context, bodypart_itmlst, 0);
335                       if (status != SS$_NORMAL)
336                       {
337 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
338 kumpf        1.10             "Routine mail$send_add_bodypart failed..");
339                           PEG_METHOD_EXIT();
340                   
341                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
342                               "Handler.EmailListenerDestination.EmailListenerDestination."
343                                   "ROUTINE_MAIL_SEND_ADD_BODYPART_FAILED.PEGASUS_OS_VMS",
344                               "Routine mail$send_add_bodypart failed."));
345                       }
346 carson.hovey 1.9  
347                   #else
348                   
349 kumpf        1.14     String mailHdrStr;
350 carson.hovey 1.9  
351 kumpf        1.10     // Write the mailToStr to file
352                       mailHdrStr.append("To: ");
353                       mailHdrStr.append(mailToStr);
354                       _writeStrToFile(mailHdrStr, filePtr);
355                   
356                       String mailCcStr = _buildMailAddrStr(mailCc);
357                   
358                       // Write the mailCcStr to file
359                       mailHdrStr = String::EMPTY;
360                   
361                       mailHdrStr.append("Cc: ");
362                       mailHdrStr.append(mailCcStr);
363                       _writeStrToFile(mailHdrStr, filePtr);
364                   
365                       // build from string
366 kumpf        1.14     String fromStr;
367 kumpf        1.10     fromStr.append("From: ");
368                       fromStr.append(System::getEffectiveUserName());
369                       fromStr.append("@");
370                       fromStr.append(System::getFullyQualifiedHostName());
371                   
372                       // Write the fromStr to file
373                       _writeStrToFile(fromStr, filePtr);
374                   
375                       // Write the mailSubject string to file
376 kumpf        1.14     String mailSubjectStr;
377 kumpf        1.10     mailSubjectStr.append("Subject: ");
378                       mailSubjectStr.append(mailSubject);
379                       _writeStrToFile(mailSubjectStr, filePtr);
380 yi.zhou      1.1  #endif
381                   
382 kumpf        1.10     PEG_METHOD_EXIT();
383 yi.zhou      1.1  }
384                   
385 carson.hovey 1.9  String EmailListenerDestination::_buildMailAddrStr(
386 kumpf        1.10     const Array<String>& mailAddr)
387 yi.zhou      1.1  {
388 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
389                           "EmailListenerDestination::_buildMailAddrStr");
390 yi.zhou      1.1  
391 kumpf        1.14     String mailAddrStr;
392 kumpf        1.10     Uint32 mailAddrSize = mailAddr.size();
393 yi.zhou      1.1  
394 kumpf        1.10     for (Uint32 i = 0; i < mailAddrSize; i++)
395                       {
396 carson.hovey 1.9  #if defined(PEGASUS_OS_VMS)
397                   
398 kumpf        1.10         CString mailAddrVms = mailAddr[i].getCString();
399                           //
400                           // Add destination to message
401                           //
402                           address_itmlst[0].buffer_length = strlen(mailAddrVms);
403                           address_itmlst[0].buffer_address = (long &)mailAddrVms;
404                   
405                           status = mail$send_add_address(
406                               &send_context,
407                               address_itmlst,
408                               &nulllist);
409                           if (status != SS$_NORMAL)
410                           {
411 marek        1.17             PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
412 kumpf        1.10                 "Routine mail$send_add_address failed.");
413                               PEG_METHOD_EXIT();
414                   
415                               throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
416                                   "Handler.EmailListenerDestination.EmailListenerDestination."
417                                       "ROUTINE_MAIL_SEND_ADD_ADDRESS_FAILED.PEGASUS_OS_VMS",
418                                   "Routine mail$send_add_address failed."));
419                           }
420                           mailAddrStr = "VMS";
421 carson.hovey 1.9  #else
422 kumpf        1.10         mailAddrStr.append(mailAddr[i]);
423 yi.zhou      1.1  
424 kumpf        1.10         if (i < (mailAddrSize - 1))
425                           {
426                               mailAddrStr.append(",");
427                           }
428                   #endif
429 yi.zhou      1.1      }
430                   
431 kumpf        1.10     PEG_METHOD_EXIT();
432                       return  mailAddrStr;
433 yi.zhou      1.1  }
434                   
435 carson.hovey 1.9  #ifdef PEGASUS_OS_VMS
436                   String EmailListenerDestination::_buildMailAddrCcStr(
437 kumpf        1.10     const Array<String>& mailAddr)
438 yi.zhou      1.1  {
439 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
440                           "EmailListenerDestination::_buildMailAddrCcStr");
441 yi.zhou      1.1  
442 kumpf        1.14     String mailAddrStr;
443 kumpf        1.10     Uint32 mailAddrSize = mailAddr.size();
444 yi.zhou      1.1  
445 kumpf        1.10     for (Uint32 i = 0; i < mailAddrSize; i++)
446 carson.hovey 1.9      {
447 kumpf        1.10         CString mailAddrCcVms = mailAddr[i].getCString();
448                           //
449                           // Add cc destination to message
450                           //
451                           address_cc_itmlst[0].buffer_length = strlen(mailAddrCcVms);
452                           address_cc_itmlst[0].buffer_address = (long &)mailAddrCcVms;
453                   
454                           status = mail$send_add_address(
455                               &send_context,
456                               address_cc_itmlst,
457                               &nulllist);
458                           if (status != SS$_NORMAL)
459                           {
460 marek        1.17             PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
461 kumpf        1.10                 "Routine mail$send_add_address failed (cc).");
462                               PEG_METHOD_EXIT();
463                   
464                               throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
465                                   "Handler.EmailListenerDestination.EmailListenerDestination."
466                                       "ROUTINE_MAIL_SEND_ADD_ADDRESS_FAILED_CC.PEGASUS_OS_VMS",
467                                   "Routine mail$send_add_address failed (cc)."));
468                           }
469 yi.zhou      1.1      }
470                   
471 kumpf        1.10     PEG_METHOD_EXIT();
472                       return mailAddrStr;
473 yi.zhou      1.1  }
474                   
475 carson.hovey 1.9  #endif
476                   
477 yi.zhou      1.1  void EmailListenerDestination::_writeStrToFile(
478 kumpf        1.10     const String& mailHdrStr,
479                       FILE* filePtr)
480 yi.zhou      1.1  {
481 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
482                           "EmailListenerDestination::_writeStrToFile");
483 yi.zhou      1.1  
484 kumpf        1.10     String exceptionStr;
485 yi.zhou      1.1  
486 kumpf        1.10     if (fprintf(filePtr, "%s\n", (const char *) mailHdrStr.getCString()) < 0)
487                       {
488 marek        1.17         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1,
489 kumpf        1.10             "Failed to write the %s to the file: %s.",
490                               (const char *) mailHdrStr.getCString(),
491 marek        1.12             strerror(errno)));
492 kumpf        1.10 
493                           MessageLoaderParms parms(
494                               "Handler.EmailListenerDestination.EmailListenerDestination."
495                                   "_MSG_WRITE_TO_THE_FILE_FAILED",
496                               "Failed to write the $0 to the file: $1.",
497                               mailHdrStr,
498                               strerror(errno));
499 yi.zhou      1.1  
500 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
501 yi.zhou      1.1  
502 kumpf        1.10         PEG_METHOD_EXIT();
503 yi.zhou      1.1  
504 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
505                       }
506 yi.zhou      1.1  
507 kumpf        1.10     PEG_METHOD_EXIT();
508 yi.zhou      1.1  }
509                   
510 yi.zhou      1.4  void EmailListenerDestination::_sendMsg(
511 kumpf        1.10     char* mailFile)
512 yi.zhou      1.1  {
513 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER, "EmailListenerDestination::_sendMsg");
514 yi.zhou      1.1  
515 kumpf        1.10     String exceptionStr;
516                       char sendmailCmd[MAX_SENDMAIL_CMD_LEN];
517                       FILE* sendmailPtr;
518                       struct stat statBuf;
519                   
520                       // Checks the existence of the temp mail file
521                       if (!System::exists(mailFile))
522                       {
523 marek        1.17         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1,
524 kumpf        1.10             "File %s does not exist: %s.",
525                               mailFile,
526 marek        1.12             strerror(errno)));
527 kumpf        1.10 
528                           MessageLoaderParms parms(
529                               "Handler.EmailListenerDestination.EmailListenerDestination."
530                                   "_MSG_FILE_DOES_NOT_EXIST",
531                               "File $0 does not exist: $1.",
532                               mailFile,
533                               strerror(errno));
534 yi.zhou      1.1  
535 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
536 yi.zhou      1.1  
537 kumpf        1.10         PEG_METHOD_EXIT();
538                   
539                           throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
540                       }
541 yi.zhou      1.1  
542 kumpf        1.10     // Checks the length of the file since a zero length file causes
543                       // problems for sendmail()
544                       if (stat(mailFile, &statBuf) != 0)
545                       {
546 marek        1.17         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1,
547 kumpf        1.10             "Can not get file %s status: %s.",
548                               mailFile,
549 marek        1.12             strerror(errno)));
550 kumpf        1.10 
551                           MessageLoaderParms parms(
552                               "Handler.EmailListenerDestination.EmailListenerDestination."
553                                   "_MSG_CAN_NOT_GET_FILE_STATUS",
554                               "Can not get file $0 status: $1.",
555                               mailFile,
556                               strerror(errno));
557 yi.zhou      1.1  
558 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
559 yi.zhou      1.1  
560 kumpf        1.10         PEG_METHOD_EXIT();
561 yi.zhou      1.1  
562 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
563                       }
564 yi.zhou      1.1  
565 kumpf        1.10     if (statBuf.st_size == 0)
566                       {
567 marek        1.17         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1,
568 kumpf        1.10             "File %s does not contain any data.",
569 marek        1.12             mailFile));
570 yi.zhou      1.1  
571 kumpf        1.10         MessageLoaderParms parms(
572                               "Handler.EmailListenerDestination.EmailListenerDestination."
573                                   "_MSG_FILE_DOES_NOT_CONTAIN_DATA",
574                               "File $0 does not contain any data.",
575                               mailFile);
576 yi.zhou      1.1  
577 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
578 yi.zhou      1.1  
579 kumpf        1.10         PEG_METHOD_EXIT();
580 yi.zhou      1.1  
581 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
582                       }
583 yi.zhou      1.1  
584 carson.hovey 1.9  #ifdef PEGASUS_OS_VMS
585 kumpf        1.10     //
586                       // Send the mail message
587                       //
588                       status = mail$send_message(&send_context, nulllist, nulllist);
589                       if (status != SS$_NORMAL)
590                       {
591 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
592 kumpf        1.10             "Routine mail$send_message failed.");
593                           PEG_METHOD_EXIT();
594                   
595                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
596                               "Handler.EmailListenerDestination.EmailListenerDestination."
597                                   "ROUTINE_MAIL_SEND_MESSAGE_FAILED.PEGASUS_OS_VMS",
598                               "Routine mail$send_message failed."));
599                       }
600                   
601                       //
602                       // End mail send process
603                       //
604                       status = mail$send_end(&send_context, nulllist, nulllist);
605                       if (status != SS$_NORMAL)
606                       {
607 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
608 kumpf        1.10             "Routine mail$send_end failed.");
609                           PEG_METHOD_EXIT();
610                   
611                           throw PEGASUS_CIM_EXCEPTION_L(CIM_ERR_FAILED, MessageLoaderParms(
612                               "Handler.EmailListenerDestination.EmailListenerDestination."
613                                   "ROUTINE_MAIL_SEND_END_FAILED.PEGASUS_OS_VMS",
614                               "Routine mail$send_end failed."));
615                       }
616 yi.zhou      1.1  
617 kumpf        1.10 #else
618                       sprintf(sendmailCmd, "%s %s %s", SENDMAIL_CMD, SENDMAIL_CMD_OPTS, mailFile);
619 yi.zhou      1.1  
620 kumpf        1.10     // Open the pipe to send the message
621                       if ((sendmailPtr = popen(sendmailCmd, "r")) == NULL)
622                       {
623 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
624 kumpf        1.10             "popen of sendmail failed.");
625 yi.zhou      1.1  
626 kumpf        1.10         MessageLoaderParms parms(
627                               "Handler.EmailListenerDestination.EmailListenerDestination."
628                                   "_MSG_POPEN_OF_SENDMAIL_FAILED",
629                               "popen of sendmail failed.");
630 yi.zhou      1.1  
631 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
632 yi.zhou      1.1  
633 kumpf        1.10         PEG_METHOD_EXIT();
634 yi.zhou      1.1  
635 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
636                       }
637 yi.zhou      1.1  
638 kumpf        1.10     // Close the pipe
639                       Sint32 retCode = pclose(sendmailPtr);
640                       if (retCode < 0)
641                       {
642 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
643 kumpf        1.10             "No associated stream with this popen command.");
644 yi.zhou      1.1  
645 kumpf        1.10         MessageLoaderParms parms(
646                               "Handler.EmailListenerDestination.EmailListenerDestination."
647                                   "_MSG_NO_ASSOCIATED_STREAM",
648                               "No associated stream with this popen command.");
649 yi.zhou      1.1  
650 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
651 yi.zhou      1.1  
652 kumpf        1.10         PEG_METHOD_EXIT();
653 yi.zhou      1.1  
654 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
655                       }
656                       else if (retCode == SH_EXECUTE_FAILED)
657                       {
658 marek        1.17         PEG_TRACE_CSTRING(TRC_IND_HANDLER, Tracer::LEVEL1,
659 kumpf        1.10             "/usr/bin/sh could not be executed.");
660 yi.zhou      1.1  
661 kumpf        1.10         MessageLoaderParms parms(
662                               "Handler.EmailListenerDestination.EmailListenerDestination."
663                                   "_MSG_SHELL_CAN_NOT_BE_EXECUTED",
664                               "/usr/bin/sh could not be executed.");
665 yi.zhou      1.1  
666 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
667 yi.zhou      1.1  
668 kumpf        1.10         PEG_METHOD_EXIT();
669 yi.zhou      1.1  
670 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
671                       }
672 carson.hovey 1.9  #endif
673 yi.zhou      1.1  
674 kumpf        1.10     PEG_METHOD_EXIT();
675 yi.zhou      1.1  }
676                   
677 yi.zhou      1.4  void EmailListenerDestination::_openFile(
678 kumpf        1.10     FILE** filePtr,
679                       char* mailFile)
680 yi.zhou      1.4  {
681 kumpf        1.10     PEG_METHOD_ENTER(TRC_IND_HANDLER,
682                           "EmailListenerDestination::_openFile");
683 yi.zhou      1.4  
684 kumpf        1.10     String exceptionStr;
685 yi.zhou      1.4  
686 kumpf        1.10     *filePtr = fopen(tmpnam(mailFile), "w");
687 carson.hovey 1.9  #ifdef PEGASUS_OS_VMS
688 kumpf        1.10     strcpy(mailFileVms, mailFile);
689 carson.hovey 1.9  #endif
690 kumpf        1.10     if (*filePtr == NULL)
691                       {
692 marek        1.17         PEG_TRACE((TRC_IND_HANDLER, Tracer::LEVEL1,
693 kumpf        1.10             "fopen of %s failed: %s.", mailFile,
694 marek        1.12             strerror(errno)));
695 kumpf        1.10 
696                           MessageLoaderParms parms(
697                               "Handler.EmailListenerDestination.EmailListenerDestination."
698                                   "_MSG_FAILED_TO_OPEN_THE_FILE",
699                               "fopen of $0 failed: $1.",
700                               mailFile,
701                               strerror(errno));
702 yi.zhou      1.4  
703 kumpf        1.10         exceptionStr.append(MessageLoader::getMessage(parms));
704 yi.zhou      1.4  
705 kumpf        1.10         PEG_METHOD_EXIT();
706 yi.zhou      1.4  
707 kumpf        1.10         throw PEGASUS_CIM_EXCEPTION(CIM_ERR_FAILED, exceptionStr);
708                       }
709 yi.zhou      1.4  
710 kumpf        1.10     PEG_METHOD_EXIT();
711 yi.zhou      1.4  }
712 yi.zhou      1.1  
713 kumpf        1.7  PEGASUS_NAMESPACE_END
714                   
715                   PEGASUS_USING_PEGASUS;
716                   
717                   // This is the entry point into this dynamic module.
718                   
719 kumpf        1.10 extern "C" PEGASUS_EXPORT CIMHandler* PegasusCreateHandler(
720                       const String& handlerName)
721 kumpf        1.7  {
722 kumpf        1.10     if (handlerName == "EmailListenerDestination")
723                       {
724                           return new EmailListenerDestination;
725                       }
726 kumpf        1.7  
727 kumpf        1.10     return 0;
728 yi.zhou      1.1  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2