Annotation of parser3/src/classes/mail.C, revision 1.93
1.1 paf 1: /** @file
2: Parser: @b mail parser class.
3:
1.89 paf 4: Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.54 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.73 paf 6: */
1.1 paf 7:
1.93 ! paf 8: static const char* IDENT_MAIL_C="$Date: 2003/11/04 12:46:49 $";
1.1 paf 9:
10: #include "pa_config_includes.h"
1.89 paf 11: #include "pa_vmethod_frame.h"
1.1 paf 12:
13: #include "pa_common.h"
14: #include "pa_request.h"
1.6 paf 15: #include "pa_vfile.h"
1.12 paf 16: #include "pa_exec.h"
1.45 paf 17: #include "pa_charsets.h"
18: #include "pa_charset.h"
1.67 paf 19: #include "pa_uue.h"
1.89 paf 20: #include "pa_vmail.h"
1.50 paf 21:
22: #ifdef _MSC_VER
23: # include "smtp/smtp.h"
24: #endif
1.4 paf 25:
1.23 paf 26: // defines
1.1 paf 27:
1.23 paf 28: #define MAIL_CLASS_NAME "mail"
1.91 paf 29: #define SENDMAIL_NAME "sendmail"
1.25 paf 30:
1.67 paf 31: // consts
1.25 paf 32:
1.67 paf 33: const int ATTACHMENT_WEIGHT=100;
1.25 paf 34:
1.23 paf 35: // class
36:
1.89 paf 37: class MMail: public Methoded {
1.24 paf 38: public: // Methoded
1.67 paf 39: bool used_directly() { return false; }
1.68 paf 40: void configure_user(Request& r);
1.89 paf 41:
42: public:
43: MMail();
1.23 paf 44: };
1.1 paf 45:
1.89 paf 46: // global variable
1.7 paf 47:
1.89 paf 48: DECLARE_CLASS_VAR(mail, 0/*fictive*/, new MMail);
1.7 paf 49:
1.89 paf 50: // defines for statics
1.1 paf 51:
1.89 paf 52: #define MAIL_NAME "MAIL"
1.70 paf 53:
1.89 paf 54: // statics
55:
56: static const String mail_name(MAIL_NAME);
1.91 paf 57: static const String mail_sendmail_name(SENDMAIL_NAME);
1.22 paf 58:
1.89 paf 59: // helpers
1.47 paf 60:
1.89 paf 61: static void sendmail(Request& r,
62: const String& message,
1.91 paf 63: const String* from, const String* to,
1.93 ! paf 64: const String*
! 65: #ifndef _MSC_VER
! 66: options
! 67: #endif
! 68: ) {
1.89 paf 69: const char* message_cstr=message.cstr(String::L_UNSPECIFIED);
70: Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_base_class->name()));
1.2 paf 71:
1.89 paf 72: const char* exception_type="email.format";
1.85 paf 73: if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from}
1.75 paf 74: throw Exception(exception_type,
1.89 paf 75: 0,
1.70 paf 76: "parameter does not specify 'from' header field");
1.85 paf 77:
78: #ifdef _MSC_VER
79: if(!to) // we use only in SMTP RCPT to: {to}
1.75 paf 80: throw Exception(exception_type,
1.89 paf 81: 0,
1.70 paf 82: "parameter does not specify 'to' header field");
1.4 paf 83:
1.89 paf 84: SMTP smtp;
85: Value* server_port;
1.29 parser 86: // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.89 paf 87: if(vmail_conf &&
1.90 paf 88: (server_port=vmail_conf->get_hash()->get(String::Body("SMTP")))) {
1.89 paf 89: char* server=server_port->as_string().cstrm();
90: const char* port=rsplit(server, ':');
1.4 paf 91: if(!port)
1.11 paf 92: port="25";
1.4 paf 93:
1.89 paf 94: smtp.Send(server, port, message_cstr, from->cstrm(), to->cstrm());
1.4 paf 95: } else
1.60 paf 96: throw Exception("parser.runtime",
1.89 paf 97: 0,
1.13 paf 98: "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4 paf 99: #else
1.12 paf 100: // unix
1.70 paf 101: // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
102: // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default
1.12 paf 103:
1.92 paf 104: String* sendmail_command=new String;
1.89 paf 105: if(vmail_conf) {
1.55 paf 106: #ifdef PA_FORCED_SENDMAIL
1.86 paf 107: throw Exception("parser.runtime",
1.89 paf 108: 0,
1.86 paf 109: "Parser was configured with --with-sendmail="PA_FORCED_SENDMAIL
1.87 paf 110: " key, to change sendmail you should reconfigure and recompie it");
1.55 paf 111: #else
1.89 paf 112: if(Value* sendmail_value=vmail_conf->get_hash()->get(mail_sendmail_name))
1.92 paf 113: *sendmail_command<<sendmail_value->as_string();
1.51 paf 114: else
1.60 paf 115: throw Exception("parser.runtime",
1.89 paf 116: 0,
1.91 paf 117: "$"MAIN_CLASS_NAME":"MAIL_NAME"."SENDMAIL_NAME" not defined");
1.86 paf 118: #endif
1.51 paf 119: } else {
1.86 paf 120: #ifdef PA_FORCED_SENDMAIL
1.92 paf 121: sendmail_command<<PA_FORCED_SENDMAIL;
1.86 paf 122: #else
1.89 paf 123: String* test=new String("/usr/sbin/sendmail");
1.52 paf 124: if(!file_executable(*test))
1.89 paf 125: test=new String("/usr/lib/sendmail");
1.92 paf 126: *sendmail_command<<*test;
127: *sendmail_command<<" -t -i -f postmaster";
1.86 paf 128: #endif
1.51 paf 129: }
1.91 paf 130: if(options)
1.92 paf 131: *sendmail_command<<" "<<*options;
1.51 paf 132:
1.70 paf 133: // we know sendmail_command here, should replace "postmaster" with "$from" from message
1.89 paf 134: size_t at_postmaster=sendmail_command->pos("postmaster");
135: if(at_postmaster!=STRING_NOT_FOUND) {
1.70 paf 136: String& reconstructed=sendmail_command->mid(0, at_postmaster);
1.71 paf 137: reconstructed << *from;
1.89 paf 138: reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->length());
1.70 paf 139: sendmail_command=&reconstructed;
140: }
141:
142: // execute it
1.89 paf 143: ArrayString argv;
144: const String* file_spec;
145: size_t after_file_spec=sendmail_command->pos(' ');
146: if(after_file_spec==STRING_NOT_FOUND || after_file_spec==0)
1.52 paf 147: file_spec=sendmail_command;
1.51 paf 148: else {
1.52 paf 149: size_t pos_after=after_file_spec;
150: file_spec=&sendmail_command->mid(0, pos_after++);
1.89 paf 151: sendmail_command->split(argv, pos_after, " ", String::L_AS_IS);
1.36 parser 152: }
1.51 paf 153:
1.52 paf 154: if(!file_executable(*file_spec))
1.75 paf 155: throw Exception("email.send",
1.55 paf 156: file_spec,
157: "is not executable."
158: #ifdef PA_FORCED_SENDMAIL
1.59 paf 159: " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58 paf 160: #else
1.91 paf 161: " Set $"MAIN_CLASS_NAME":"MAIL_NAME"."SENDMAIL_NAME" to appropriate sendmail command"
1.55 paf 162: #endif
163: );
164:
1.51 paf 165:
1.89 paf 166: String in(message_cstr); String out; String err;
167: PA_exec_result exec=pa_exec(
1.56 paf 168: // forced_allow
169: #ifdef PA_FORCED_SENDMAIL
170: true
171: #else
172: false
173: #endif
1.57 paf 174: , *file_spec,
1.89 paf 175: 0 /* pass env */,
176: argv,
177: in);
178: if(exec.status || exec.err.length())
1.75 paf 179: throw Exception("email.send",
1.89 paf 180: 0,
1.51 paf 181: "'%s' reported problem: %s (%d)",
182: file_spec->cstr(),
1.89 paf 183: exec.err.length()?exec.err.cstr():"UNKNOWN",
184: exec.status);
1.4 paf 185: #endif
186: }
187:
1.7 paf 188: // methods
189:
1.89 paf 190: static void _send(Request& r, MethodParams& params) {
191: Value& vhash=params.as_no_junction(0, "message must not be code");
192: HashStringValue* hash=vhash.get_hash();
1.1 paf 193: if(!hash)
1.60 paf 194: throw Exception("parser.runtime",
1.89 paf 195: 0,
1.1 paf 196: "message must be hash");
197:
1.91 paf 198: const String* soptions=0;
199: if(Value* voptions=hash->get(MAIL_OPTIONS_NAME))
200: soptions=&voptions->as_string();
201:
1.89 paf 202: const String* from=0;
203: String* to=0;
204: const String& message=
205: GET_SELF(r, VMail).message_hash_to_string(r, hash, 0, from,
206: #ifdef WIN32
207: true
1.84 paf 208: #else
1.89 paf 209: false
1.84 paf 210: #endif
1.89 paf 211: , to);
1.1 paf 212:
1.67 paf 213: //r.write_pass_lang(message);
1.91 paf 214: sendmail(r, message, from, to, soptions);
1.6 paf 215: }
216:
1.24 paf 217: // constructor & configurator
1.23 paf 218:
1.89 paf 219: MMail::MMail(): Methoded(MAIL_CLASS_NAME) {
1.27 paf 220: // ^mail:send{hash}
1.23 paf 221: add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68 paf 222: }
223:
224: void MMail::configure_user(Request& r) {
225:
226: // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.89 paf 227: if(Value* mail_element=r.main_class.get_element(mail_name, r.main_class, false))
228: if(mail_element->get_hash())
229: r.classes_conf.put(name(), mail_element);
1.68 paf 230: else
1.69 paf 231: if( !mail_element->is_string() )
232: throw Exception("parser.runtime",
233: 0,
234: "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.1 paf 235: }
E-mail: