00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "openLogger.h"
00025 #include "cgiClass.h"
00026 #include"ocMail.h"
00027 #include "connectstring"
00028 static char * mailPath = "/usr/sbin/sendmail";
00029 string baseUrl;
00030
00031 #include "page.hpp"
00032 #include "page_control.hpp"
00033 using namespace std;
00034
00035
00036 class mail_functor: public base_functor
00037 {
00038 public:
00039 cgiScript & script;
00040 page & pg;
00041 ocSendMail sendMail;
00042
00043 string bodyStart;
00044 string itemTemplate;
00045 string bodyEnd;
00046 string formName;
00047 string thankYou;
00048
00049
00050 string sendTo;
00051
00052 string mailBody;
00053 string dataSent;
00054
00055 mail_functor(page & ipg,cgiScript & scriptIn):base_functor(ipg)
00056 ,script(scriptIn),pg(ipg),sendMail(mailPath){;}
00057 virtual ~mail_functor(){;}
00058
00059 virtual bool operator()( xmlNode & node )
00060 {
00061 bool bRet = true;
00062
00063 node_attr::iterator x = node.attr.find("items");
00064 if( x!=node.attr.end() )
00065 {
00066 formName=x->second;
00067 }
00068 x = node.attr.find("mailbodystart");
00069 if( x!=node.attr.end())
00070 {
00071 bodyStart=x->second;
00072 }
00073 x = node.attr.find("mailbodyitem");
00074 if( x!=node.attr.end())
00075 {
00076 itemTemplate=x->second;
00077 }
00078 x = node.attr.find("mailbodyend");
00079 if( x!=node.attr.end())
00080 {
00081 bodyEnd=x->second;
00082 }
00083 x = node.attr.find("thankyou");
00084 if( x!=node.attr.end())
00085 {
00086 thankYou=x->second;
00087 }
00088 x = node.attr.find("sendto");
00089 if( x!=node.attr.end())
00090 {
00091 sendTo=x->second;
00092 }
00093
00094 if( script.RequestMethod().upper() == "GET" )
00095 {
00096
00097
00098 showForm();
00099 }
00100 else
00101 {
00102
00103
00104
00105
00106 doMail();
00107 }
00108 return bRet;
00109 }
00110
00111 void showForm( void )
00112 {
00113 script << "<!-- form name = '" << formName << "' -->" << endl;
00114 pg.emitContent( formName, "$content" );
00115 }
00116
00117 void doMail( void )
00118 {
00119 dataSent = "";
00120 mailBody = pg.paragraph_string(bodyStart);
00121 queryStringMap::iterator pos;
00122 cgiInput & args = script.ClientArguments();
00123 string from = sendTo.c_str();
00124 string to = sendTo.c_str();
00125 ocString testForEmail;
00126 for(pos=args.TheMap().begin(); pos!=args.TheMap().end(); ++pos)
00127 {
00128 testForEmail = pos->first;
00129 if( testForEmail.regExMatch( "[eE][mM][aA][iI][lL]" ) && pos->second.length() )
00130 {
00131 from = pos->second.c_str();
00132 }
00133 dataItem( pos->first, pos->second );
00134 }
00135 mailBody += pg.paragraph_string(bodyEnd);
00136 sendMail.openRoute( from.c_str(), to.c_str(), "Customer Request" );
00137 sendMail.setMimeType("text/html");
00138 sendMail.write( mailBody.c_str() );
00139 sendMail.send();
00140 script << thanksMsg()<< endl;
00141 }
00142 ocString find( page_paragraphs & paras, string name )
00143 {
00144 ocString output;
00145
00146 for( int pos = 0; pos < paras.size(); ++pos )
00147 {
00148
00149 if( paras[pos].name() == name )
00150 {
00151 output = paras[pos].content();
00152 break;
00153 }
00154 }
00155
00156 return output;
00157 }
00158 string thanksMsg( void )
00159 {
00160 string message = find( pg.get_page_paragraphs(), thankYou);
00161 ocString thanksMsg = pg.paragraph_string(thankYou);
00162 return thanksMsg.replace("$data",dataSent.c_str()).replace("$message",message.c_str());
00163 }
00164
00165 void dataItem( ocString name,
00166 aString value )
00167 {
00168
00169 if( name.substr(0,2)=="mi")
00170 {
00171 name.parse("_");
00172 ocString datum = pg.paragraph_string(itemTemplate);
00173 string formatted = datum.replace("$label",name.remainder().c_str()).replace("$data",value.c_str());
00174 mailBody += formatted;
00175 dataSent += formatted;
00176 }
00177 }
00178 };
00179
00180
00181
00182 int main(int argc, char ** argv)
00183 {
00184 baseUrl = "contactus.meta";
00185 cgiScript script;
00186 page pg(script);
00187
00188 if( pg.load() )
00189 {
00190 page_control ctrl(pg);
00191 ctrl.addOp ( "contactus", new mail_functor(pg,script) );
00192
00193 ctrl.emit();
00194 }
00195 else
00196 {
00197 script << "<html><head><title>Page Unavailable</title></head>" << endl
00198 << "<body><h1>Sorry</h1>" << endl
00199 << "<p>The page you requested is not available</p></body></html>";
00200 }
00201 return 0;
00202 }