00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdlib.h>
00018 #include <stdio.h>
00019 #include <unistd.h>
00020
00021 #include <kapplication.h>
00022 #include <kaction.h>
00023 #include <kdebug.h>
00024 #include <qmessagebox.h>
00025
00026 #include "commandentry.h"
00027 #include "editdialog.h"
00028
00029 CommandEntry& CommandEntry::operator = (const CommandEntry& entry)
00030 {
00031 m_id = entry.m_id;
00032 m_name = entry.m_name;
00033 m_path = entry.m_path;
00034 m_waitForCommand = entry.m_waitForCommand;
00035 return *this;
00036 }
00037
00038 bool CommandEntry::exec (ConfigElem* pelem, const char* header, const char* body)
00039 {
00040 kdDebug () << "CommandEntry::exec ();" << endl;
00041 return exec (m_path, pelem, header, body);
00042 }
00043
00044 bool CommandEntry::exec (const QString& path, ConfigElem* pelem, const char* header, const char* body)
00045 {
00046 kdDebug () << "CommandEntry::exec (" << path << ");" << endl;
00047 QString cmd = path;
00048 int pos;
00049 if (pelem)
00050 {
00051 if ((pos = cmd.find ("<user>")) >= 0)
00052 cmd.replace (pos, strlen ("<user>"), pelem->getUser() );
00053 if ((pos = cmd.find ("<server>")) >= 0)
00054 cmd.replace (pos, strlen ("<server>"), pelem->getHost());
00055 if ((pos = cmd.find ("<passwd>")) >= 0)
00056 cmd.replace (pos, strlen ("<passwd>"), pelem->getPassword());
00057 }
00058 else
00059 {
00060 if ((pos = cmd.find ("<user>")) >= 0)
00061 cmd.remove (pos, strlen ("<user>"));
00062 if ((pos = cmd.find ("<server>")) >= 0)
00063 cmd.remove (pos, strlen ("<server>"));
00064 if ((pos = cmd.find ("<passwd>")) >= 0)
00065 cmd.remove (pos, strlen ("<passwd>"));
00066 }
00067 if (header && (pos = cmd.find ("<header>")) >= 0)
00068 {
00069 FILE* tmp = fopen ("/tmp/spamheader", "w");
00070 fputs (header, tmp);
00071 fclose (tmp);
00072 cmd.replace (pos, strlen ("<header>"), "/tmp/spamheader");
00073 }
00074 if (body && (pos = cmd.find ("<body>")) >= 0)
00075 {
00076 FILE* tmp = fopen ("/tmp/spambody", "w");
00077 fputs (body, tmp);
00078 fclose (tmp);
00079 cmd.replace (pos, strlen ("<body>"), "/tmp/spambody");
00080 }
00081 int result = 0;
00082 if (m_waitForCommand) {
00083
00084 FILE* output = popen (cmd, "r");
00085 char buffer [81];
00086 QString str;
00087 while (fgets (buffer, 80, output))
00088 {
00089 str += buffer;
00090 }
00091 KApplication::setOverrideCursor (Qt::arrowCursor);
00092 result = pclose (output);
00093 if (!str.isEmpty ())
00094 {
00095 EditDialog dlg (NULL, cmd, str, false);
00096 dlg.exec ();
00097 }
00098 kdDebug () << cmd << " returned " << result << endl;
00099 }
00100 else
00101 {
00102
00103 int pid;
00104 if ((pid = fork()) == 0)
00105 {
00106
00107 system (cmd);
00108
00109 exit (0);
00110
00111 }
00112 else if (pid == -1)
00113 {
00114 EditDialog dlg (NULL, cmd, "The command failed to execute", false);
00115 dlg.exec ();
00116 }
00117 }
00118 KApplication::restoreOverrideCursor ();
00119 return (result == 0);
00120 }
00121