Annotation of parser3/src/classes/mail.C, revision 1.88.2.16
1.1 paf 1: /** @file
2: Parser: @b mail parser class.
3:
1.88.2.1 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.88.2.16! paf 8: static const char* IDENT_MAIL_C="$Date: 2003/03/08 12:22:05 $";
1.1 paf 9:
10: #include "pa_config_includes.h"
1.88.2.4 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.88.2.16! 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.88.2.14 paf 29: #define MAIL_SENDMAIL_NAME "sendmail"
1.23 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.88.2.9 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.7 paf 41:
1.88.2.6 paf 42: public:
43: MMail();
1.1 paf 44: };
1.70 paf 45:
1.88.2.6 paf 46: // global variable
1.22 paf 47:
1.88.2.12 paf 48: DECLARE_CLASS_VAR(mail, 0/*fictive*/, new MMail);
1.47 paf 49:
1.88.2.6 paf 50: // defines for statics
1.2 paf 51:
1.88.2.6 paf 52: #define MAIL_NAME "MAIL"
1.20 paf 53:
1.88.2.6 paf 54: // statics
55:
56: static StringPtr mail_name(new String(MAIL_NAME));
1.88.2.14 paf 57: static StringPtr mail_sendmail_name(new String(MAIL_SENDMAIL_NAME));
1.2 paf 58:
1.88.2.6 paf 59: // helpers
1.2 paf 60:
1.88.2.6 paf 61: static void sendmail(Request& r, StringPtr method_name,
62: StringPtr message,
63: StringPtr from, StringPtr to) {
1.4 paf 64: Pool& pool=r.pool();
65:
1.88.2.6 paf 66: CharPtr message_cstr=message->cstr(String::UL_UNSPECIFIED);
1.88.2.7 paf 67: Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_base_class->name()).get());
1.12 paf 68:
1.88.2.6 paf 69: const char *exception_type="email.format";
1.85 paf 70: if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from}
1.75 paf 71: throw Exception(exception_type,
1.88.2.5 paf 72: method_name,
1.70 paf 73: "parameter does not specify 'from' header field");
1.85 paf 74:
75: #ifdef _MSC_VER
76: if(!to) // we use only in SMTP RCPT to: {to}
1.75 paf 77: throw Exception(exception_type,
1.88.2.5 paf 78: method_name,
1.70 paf 79: "parameter does not specify 'to' header field");
1.4 paf 80:
1.88.2.6 paf 81: SMTPPtr smtp(new SMTP(method_name));
82: ValuePtr server_port;
1.29 parser 83: // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.88.2.6 paf 84: if(vmail_conf &&
85: (server_port=vmail_conf->get_hash(method_name)->get(StringPtr(new String("SMTP"))))) {
86: CharPtr server=server_port->as_string(&pool)->cstr();
87: const char *port=rsplit(server, ':');
1.4 paf 88: if(!port)
1.11 paf 89: port="25";
1.4 paf 90:
1.88.2.6 paf 91: smtp->Send(server, port, message_cstr, from->cstr(), to->cstr());
1.4 paf 92: } else
1.60 paf 93: throw Exception("parser.runtime",
1.88.2.5 paf 94: method_name,
1.13 paf 95: "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4 paf 96: #else
1.12 paf 97: // unix
1.70 paf 98: // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
99: // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default
1.12 paf 100:
1.88.2.13 paf 101: StringPtr sendmail_command;
102: if(vmail_conf) {
1.55 paf 103: #ifdef PA_FORCED_SENDMAIL
1.86 paf 104: throw Exception("parser.runtime",
1.88.2.5 paf 105: method_name,
1.86 paf 106: "Parser was configured with --with-sendmail="PA_FORCED_SENDMAIL
1.87 paf 107: " key, to change sendmail you should reconfigure and recompie it");
1.55 paf 108: #else
1.88.2.14 paf 109: if(ValuePtr sendmail_value=vmail_conf->get_hash(method_name)->get(mail_sendmail_name))
110: sendmail_command=sendmail_value->as_string(&pool);
1.51 paf 111: else
1.60 paf 112: throw Exception("parser.runtime",
1.88.2.5 paf 113: method_name,
1.88.2.14 paf 114: "$"MAIN_CLASS_NAME":"MAIL_NAME"."MAIL_SENDMAIL_NAME" not defined");
1.86 paf 115: #endif
1.51 paf 116: } else {
1.86 paf 117: #ifdef PA_FORCED_SENDMAIL
1.88.2.13 paf 118: sendmail_command=StringPtr(new String(PA_FORCED_SENDMAIL));
1.86 paf 119: #else
1.88.2.14 paf 120: StringPtr test(new String("/usr/sbin/sendmail"));
121: if(!file_executable(test))
1.88.2.13 paf 122: test=StringPtr(new String("/usr/lib/sendmail"));
1.70 paf 123: test->APPEND_CONST(" -t -i -f postmaster");
1.52 paf 124: sendmail_command=test;
1.86 paf 125: #endif
1.51 paf 126: }
127:
1.70 paf 128: // we know sendmail_command here, should replace "postmaster" with "$from" from message
129: int at_postmaster=sendmail_command->pos("postmaster");
130: if(at_postmaster>0) {
1.88.2.13 paf 131: StringPtr reconstructed=sendmail_command->mid(0, at_postmaster);
132: *reconstructed << *from;
133: *reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->size());
134: sendmail_command=reconstructed;
1.70 paf 135: }
136:
137: // execute it
1.88.2.14 paf 138: ArrayString argv;
1.88.2.13 paf 139: StringPtr file_spec;
1.52 paf 140: int after_file_spec=sendmail_command->pos(" ", 1);
1.51 paf 141: if(after_file_spec<=0)
1.52 paf 142: file_spec=sendmail_command;
1.51 paf 143: else {
1.52 paf 144: size_t pos_after=after_file_spec;
1.88.2.13 paf 145: file_spec=sendmail_command->mid(0, pos_after++);
1.52 paf 146: sendmail_command->split(argv, &pos_after, " ", 1, String::UL_AS_IS);
1.36 parser 147: }
1.51 paf 148:
1.88.2.13 paf 149: if(!file_executable(file_spec))
1.75 paf 150: throw Exception("email.send",
1.55 paf 151: file_spec,
152: "is not executable."
153: #ifdef PA_FORCED_SENDMAIL
1.59 paf 154: " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58 paf 155: #else
1.88.2.14 paf 156: " Set $"MAIN_CLASS_NAME":"MAIL_NAME"."MAIL_SENDMAIL_NAME" to appropriate sendmail command"
1.55 paf 157: #endif
158: );
159:
1.51 paf 160:
1.88.2.13 paf 161: String in(message_cstr); String out; String err;
1.88.2.14 paf 162: PA_exec_result exec=pa_exec(pool,
1.56 paf 163: // forced_allow
164: #ifdef PA_FORCED_SENDMAIL
165: true
166: #else
167: false
168: #endif
1.88.2.14 paf 169: , file_spec,
170: 0 /* pass env */,
171: argv,
172: in);
173: if(exec.status || exec.err->size())
1.75 paf 174: throw Exception("email.send",
1.88.2.5 paf 175: method_name,
1.51 paf 176: "'%s' reported problem: %s (%d)",
1.88.2.11 paf 177: file_spec->cstr().get(),
1.88.2.14 paf 178: exec.err->size()?exec.err->cstr().get():"UNKNOWN",
179: exec.status);
1.4 paf 180: #endif
181: }
182:
1.7 paf 183: // methods
184:
1.88.2.10 paf 185: static void _send(Request& r, StringPtr method_name, MethodParams* params) {
1.1 paf 186: Pool& pool=r.pool();
187:
1.88.2.10 paf 188: ValuePtr vhash=params->as_no_junction(0, "message must not be code");
1.88.2.6 paf 189: HashStringValue* hash=vhash->get_hash(method_name);
1.1 paf 190: if(!hash)
1.60 paf 191: throw Exception("parser.runtime",
1.88.2.5 paf 192: method_name,
1.1 paf 193: "message must be hash");
194:
1.88.2.6 paf 195: StringPtr from(0);
196: StringPtr to(0);
197: StringPtr message=
1.88.2.15 paf 198: GET_SELF(r, VMail).message_hash_to_string(r, method_name, hash, 0, from,
199: #ifdef WIN32
200: true
201: #else
202: false
203: #endif
204: , to);
1.1 paf 205:
1.67 paf 206: //r.write_pass_lang(message);
207: sendmail(r, method_name, message, from, to);
1.6 paf 208: }
209:
1.24 paf 210: // constructor & configurator
1.23 paf 211:
1.88.2.6 paf 212: MMail::MMail(): Methoded(MAIL_CLASS_NAME) {
1.27 paf 213: // ^mail:send{hash}
1.23 paf 214: add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68 paf 215: }
216:
217: void MMail::configure_user(Request& r) {
218: Pool& pool=r.pool();
219:
220: // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.88.2.6 paf 221: if(ValuePtr mail_element=r.main_class->get_element(mail_name, *r.main_class, false))
222: if(mail_element->get_hash(Exception::undefined_source))
223: r.classes_conf.put(name(), mail_element);
1.68 paf 224: else
1.69 paf 225: if( !mail_element->is_string() )
226: throw Exception("parser.runtime",
1.88.2.6 paf 227: Exception::undefined_source,
1.69 paf 228: "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.1 paf 229: }
E-mail: