Annotation of parser3/src/classes/mail.C, revision 1.137
1.1 paf 1: /** @file
2: Parser: @b mail parser class.
3:
1.137 ! moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.136 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.73 paf 6: */
1.1 paf 7:
8: #include "pa_config_includes.h"
1.89 paf 9: #include "pa_vmethod_frame.h"
1.1 paf 10:
11: #include "pa_common.h"
12: #include "pa_request.h"
1.6 paf 13: #include "pa_vfile.h"
1.12 paf 14: #include "pa_exec.h"
1.45 paf 15: #include "pa_charsets.h"
16: #include "pa_charset.h"
1.67 paf 17: #include "pa_uue.h"
1.89 paf 18: #include "pa_vmail.h"
1.50 paf 19:
1.103 paf 20: #include "smtp.h"
1.4 paf 21:
1.137 ! moko 22: volatile const char * IDENT_MAIL_C="$Id: mail.C,v 1.136 2023/09/26 20:49:06 moko Exp $";
1.118 moko 23:
1.23 paf 24: // defines
1.1 paf 25:
1.23 paf 26: #define MAIL_CLASS_NAME "mail"
1.91 paf 27: #define SENDMAIL_NAME "sendmail"
1.25 paf 28:
1.67 paf 29: // consts
1.25 paf 30:
1.67 paf 31: const int ATTACHMENT_WEIGHT=100;
1.25 paf 32:
1.23 paf 33: // class
34:
1.89 paf 35: class MMail: public Methoded {
1.24 paf 36: public: // Methoded
1.67 paf 37: bool used_directly() { return false; }
1.68 paf 38: void configure_user(Request& r);
1.89 paf 39:
40: public:
41: MMail();
1.23 paf 42: };
1.1 paf 43:
1.89 paf 44: // global variable
1.7 paf 45:
1.126 moko 46: DECLARE_CLASS_VAR(mail, new MMail);
1.7 paf 47:
1.89 paf 48: // defines for statics
1.1 paf 49:
1.89 paf 50: #define MAIL_NAME "MAIL"
1.70 paf 51:
1.89 paf 52: // statics
53:
54: static const String mail_name(MAIL_NAME);
1.91 paf 55: static const String mail_sendmail_name(SENDMAIL_NAME);
1.22 paf 56:
1.89 paf 57: // helpers
1.47 paf 58:
1.104 paf 59: static void sendmail(
60: Value*
61: #ifndef WIN32
62: vmail_conf
63: #endif
64: , Value* smtp_server_port,
1.114 misha 65: const String& message,
66: const String* from,
67: const String* to,
68: const String*
1.121 moko 69: #ifndef WIN32
1.93 paf 70: options
71: #endif
72: ) {
1.89 paf 73: const char* exception_type="email.format";
1.85 paf 74: if(!from) // we use in sendmail -f {from} && SMTP MAIL from: {from}
1.134 moko 75: throw Exception(exception_type, 0, "parameter does not specify 'from' header field");
1.116 misha 76:
77: const char* message_cstr=message.untaint_cstr(String::L_AS_IS);
78:
1.104 paf 79: if(smtp_server_port) {
80: if(!to) // we use only in SMTP RCPT to: {to}
1.134 moko 81: throw Exception(exception_type, 0, "parameter does not specify 'to' header field");
1.4 paf 82:
1.101 paf 83: SMTP smtp;
84: char* server=smtp_server_port->as_string().cstrm();
1.89 paf 85: const char* port=rsplit(server, ':');
1.4 paf 86: if(!port)
1.11 paf 87: port="25";
1.4 paf 88:
1.89 paf 89: smtp.Send(server, port, message_cstr, from->cstrm(), to->cstrm());
1.101 paf 90: return;
91: }
92:
1.120 moko 93: #ifdef WIN32
1.101 paf 94: // win32 without SMTP server configured
1.134 moko 95: throw Exception(PARSER_RUNTIME, 0, "$" 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.92 paf 101: String* sendmail_command=new String;
1.89 paf 102: if(vmail_conf) {
1.55 paf 103: #ifdef PA_FORCED_SENDMAIL
1.111 misha 104: throw Exception(PARSER_RUNTIME,
1.89 paf 105: 0,
1.122 moko 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.89 paf 109: if(Value* sendmail_value=vmail_conf->get_hash()->get(mail_sendmail_name))
1.92 paf 110: *sendmail_command<<sendmail_value->as_string();
1.51 paf 111: else
1.134 moko 112: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " not defined");
1.86 paf 113: #endif
1.51 paf 114: } else {
1.86 paf 115: #ifdef PA_FORCED_SENDMAIL
1.96 paf 116: *sendmail_command<<PA_FORCED_SENDMAIL;
1.86 paf 117: #else
1.89 paf 118: String* test=new String("/usr/sbin/sendmail");
1.52 paf 119: if(!file_executable(*test))
1.89 paf 120: test=new String("/usr/lib/sendmail");
1.92 paf 121: *sendmail_command<<*test;
122: *sendmail_command<<" -t -i -f postmaster";
1.86 paf 123: #endif
1.51 paf 124: }
1.91 paf 125: if(options)
1.92 paf 126: *sendmail_command<<" "<<*options;
1.51 paf 127:
1.70 paf 128: // we know sendmail_command here, should replace "postmaster" with "$from" from message
1.89 paf 129: size_t at_postmaster=sendmail_command->pos("postmaster");
130: if(at_postmaster!=STRING_NOT_FOUND) {
1.70 paf 131: String& reconstructed=sendmail_command->mid(0, at_postmaster);
1.71 paf 132: reconstructed << *from;
1.89 paf 133: reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->length());
1.70 paf 134: sendmail_command=&reconstructed;
135: }
136:
137: // execute it
1.89 paf 138: ArrayString argv;
139: const String* file_spec;
140: size_t after_file_spec=sendmail_command->pos(' ');
141: if(after_file_spec==STRING_NOT_FOUND || after_file_spec==0)
1.52 paf 142: file_spec=sendmail_command;
1.51 paf 143: else {
1.129 moko 144: file_spec=&sendmail_command->mid(0, after_file_spec);
145: sendmail_command->split(argv, after_file_spec+1, " ", String::L_AS_IS);
1.36 parser 146: }
1.51 paf 147:
1.52 paf 148: if(!file_executable(*file_spec))
1.75 paf 149: throw Exception("email.send",
1.55 paf 150: file_spec,
151: "is not executable."
152: #ifdef PA_FORCED_SENDMAIL
1.59 paf 153: " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58 paf 154: #else
1.122 moko 155: " Set $" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " to appropriate sendmail command"
1.55 paf 156: #endif
157: );
158:
1.89 paf 159: PA_exec_result exec=pa_exec(
1.56 paf 160: // forced_allow
161: #ifdef PA_FORCED_SENDMAIL
162: true
163: #else
164: false
165: #endif
1.57 paf 166: , *file_spec,
1.89 paf 167: 0 /* pass env */,
168: argv,
1.132 moko 169: String::C(message_cstr, strlen(message_cstr)));
170:
1.89 paf 171: if(exec.status || exec.err.length())
1.132 moko 172: throw Exception("email.send", 0, "'%s' reported problem: %s (%d)", file_spec->cstr(), exec.err.length() ? exec.err.cstr() : "UNKNOWN", exec.status);
1.121 moko 173: #endif //WIN32
1.4 paf 174: }
175:
1.7 paf 176: // methods
177:
1.89 paf 178: static void _send(Request& r, MethodParams& params) {
1.119 misha 179: HashStringValue* hash=params.as_hash(0, "message");
180: if(!hash || !hash->count())
181: return;
182: // todo@ check if enough options are specified.
183: // now ^mail:send[^hash::create[]] and ^mail:send[$.print-debug(1)] "work".
1.1 paf 184:
1.91 paf 185: const String* soptions=0;
186: if(Value* voptions=hash->get(MAIL_OPTIONS_NAME))
187: soptions=&voptions->as_string();
188:
1.115 misha 189: bool print_debug=false;
190: if(Value* vdebug=hash->get(MAIL_DEBUG_NAME))
191: print_debug=vdebug->as_bool();
192:
1.128 moko 193: Value* vmail_conf=static_cast<Value*>(r.classes_conf.get(mail_class->type()));
1.104 paf 194: Value* smtp_server_port=0;
195: if(vmail_conf) {
196: // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
1.124 moko 197: smtp_server_port=vmail_conf->get_hash()->get("SMTP");
1.104 paf 198: }
199:
1.89 paf 200: const String* from=0;
201: String* to=0;
1.134 moko 202: const String& message = GET_SELF(r, VMail).message_hash_to_string(r, hash, from, smtp_server_port ? true : false /*send by SMTP=strip to?*/, to);
1.1 paf 203:
1.115 misha 204: if(print_debug)
1.131 moko 205: r.write(message);
1.115 misha 206: else
207: sendmail(vmail_conf, smtp_server_port, message, from, to, soptions);
1.6 paf 208: }
209:
1.24 paf 210: // constructor & configurator
1.23 paf 211:
1.89 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:
219: // $MAIN:MAIL[$SMTP[mail.design.ru]]
1.123 moko 220: if(Value* mail_element=r.main_class.get_element(mail_name)) {
1.89 paf 221: if(mail_element->get_hash())
1.128 moko 222: r.classes_conf.put(type(), mail_element);
1.68 paf 223: else
1.69 paf 224: if( !mail_element->is_string() )
1.134 moko 225: throw Exception(PARSER_RUNTIME, 0, "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
1.123 moko 226: }
1.1 paf 227: }
E-mail: