Annotation of parser3/src/classes/mail.C, revision 1.138
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.138 ! moko 22: volatile const char * IDENT_MAIL_C="$Id: mail.C,v 1.137 2024/11/04 03:53:25 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.89 paf 38:
39: public:
40: MMail();
1.23 paf 41: };
1.1 paf 42:
1.89 paf 43: // global variable
1.7 paf 44:
1.126 moko 45: DECLARE_CLASS_VAR(mail, new MMail);
1.7 paf 46:
1.89 paf 47: // defines for statics
1.1 paf 48:
1.89 paf 49: #define MAIL_NAME "MAIL"
1.70 paf 50:
1.89 paf 51: // statics
52:
53: static const String mail_name(MAIL_NAME);
1.91 paf 54: static const String mail_sendmail_name(SENDMAIL_NAME);
1.22 paf 55:
1.89 paf 56: // helpers
1.47 paf 57:
1.104 paf 58: static void sendmail(
59: Value*
60: #ifndef WIN32
61: vmail_conf
62: #endif
63: , Value* smtp_server_port,
1.114 misha 64: const String& message,
65: const String* from,
66: const String* to,
67: const String*
1.121 moko 68: #ifndef WIN32
1.93 paf 69: options
70: #endif
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.134 moko 74: throw Exception(exception_type, 0, "parameter does not specify 'from' header field");
1.116 misha 75:
76: const char* message_cstr=message.untaint_cstr(String::L_AS_IS);
77:
1.104 paf 78: if(smtp_server_port) {
79: if(!to) // we use only in SMTP RCPT to: {to}
1.134 moko 80: throw Exception(exception_type, 0, "parameter does not specify 'to' header field");
1.4 paf 81:
1.101 paf 82: SMTP smtp;
83: char* server=smtp_server_port->as_string().cstrm();
1.89 paf 84: const char* port=rsplit(server, ':');
1.4 paf 85: if(!port)
1.11 paf 86: port="25";
1.4 paf 87:
1.89 paf 88: smtp.Send(server, port, message_cstr, from->cstrm(), to->cstrm());
1.101 paf 89: return;
90: }
91:
1.120 moko 92: #ifdef WIN32
1.101 paf 93: // win32 without SMTP server configured
1.134 moko 94: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" MAIL_NAME ".SMTP not defined");
1.4 paf 95: #else
1.12 paf 96: // unix
1.70 paf 97: // $MAIN:MAIL.sendmail["/usr/sbin/sendmail -t -i -f postmaster"] default
98: // $MAIN:MAIL.sendmail["/usr/lib/sendmail -t -i -f postmaster"] default
1.12 paf 99:
1.92 paf 100: String* sendmail_command=new String;
1.89 paf 101: if(vmail_conf) {
1.55 paf 102: #ifdef PA_FORCED_SENDMAIL
1.111 misha 103: throw Exception(PARSER_RUNTIME,
1.89 paf 104: 0,
1.122 moko 105: "Parser was configured with --with-sendmail=" PA_FORCED_SENDMAIL
1.87 paf 106: " key, to change sendmail you should reconfigure and recompie it");
1.55 paf 107: #else
1.89 paf 108: if(Value* sendmail_value=vmail_conf->get_hash()->get(mail_sendmail_name))
1.92 paf 109: *sendmail_command<<sendmail_value->as_string();
1.51 paf 110: else
1.134 moko 111: throw Exception(PARSER_RUNTIME, 0, "$" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " not defined");
1.86 paf 112: #endif
1.51 paf 113: } else {
1.86 paf 114: #ifdef PA_FORCED_SENDMAIL
1.96 paf 115: *sendmail_command<<PA_FORCED_SENDMAIL;
1.86 paf 116: #else
1.89 paf 117: String* test=new String("/usr/sbin/sendmail");
1.52 paf 118: if(!file_executable(*test))
1.89 paf 119: test=new String("/usr/lib/sendmail");
1.92 paf 120: *sendmail_command<<*test;
121: *sendmail_command<<" -t -i -f postmaster";
1.86 paf 122: #endif
1.51 paf 123: }
1.91 paf 124: if(options)
1.92 paf 125: *sendmail_command<<" "<<*options;
1.51 paf 126:
1.70 paf 127: // we know sendmail_command here, should replace "postmaster" with "$from" from message
1.89 paf 128: size_t at_postmaster=sendmail_command->pos("postmaster");
129: if(at_postmaster!=STRING_NOT_FOUND) {
1.70 paf 130: String& reconstructed=sendmail_command->mid(0, at_postmaster);
1.71 paf 131: reconstructed << *from;
1.89 paf 132: reconstructed << sendmail_command->mid(at_postmaster+10/*postmaster*/, sendmail_command->length());
1.70 paf 133: sendmail_command=&reconstructed;
134: }
135:
136: // execute it
1.89 paf 137: ArrayString argv;
138: const String* file_spec;
139: size_t after_file_spec=sendmail_command->pos(' ');
140: if(after_file_spec==STRING_NOT_FOUND || after_file_spec==0)
1.52 paf 141: file_spec=sendmail_command;
1.51 paf 142: else {
1.129 moko 143: file_spec=&sendmail_command->mid(0, after_file_spec);
144: sendmail_command->split(argv, after_file_spec+1, " ", String::L_AS_IS);
1.36 parser 145: }
1.51 paf 146:
1.52 paf 147: if(!file_executable(*file_spec))
1.75 paf 148: throw Exception("email.send",
1.55 paf 149: file_spec,
150: "is not executable."
151: #ifdef PA_FORCED_SENDMAIL
1.59 paf 152: " Use configure key \"--with-sendmail=appropriate sendmail command\""
1.58 paf 153: #else
1.122 moko 154: " Set $" MAIN_CLASS_NAME ":" MAIL_NAME "." SENDMAIL_NAME " to appropriate sendmail command"
1.55 paf 155: #endif
156: );
157:
1.89 paf 158: PA_exec_result exec=pa_exec(
1.56 paf 159: // forced_allow
160: #ifdef PA_FORCED_SENDMAIL
161: true
162: #else
163: false
164: #endif
1.57 paf 165: , *file_spec,
1.89 paf 166: 0 /* pass env */,
167: argv,
1.132 moko 168: String::C(message_cstr, strlen(message_cstr)));
169:
1.89 paf 170: if(exec.status || exec.err.length())
1.132 moko 171: 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 172: #endif //WIN32
1.4 paf 173: }
174:
1.7 paf 175: // methods
176:
1.89 paf 177: static void _send(Request& r, MethodParams& params) {
1.119 misha 178: HashStringValue* hash=params.as_hash(0, "message");
179: if(!hash || !hash->count())
180: return;
181: // todo@ check if enough options are specified.
182: // now ^mail:send[^hash::create[]] and ^mail:send[$.print-debug(1)] "work".
1.1 paf 183:
1.91 paf 184: const String* soptions=0;
185: if(Value* voptions=hash->get(MAIL_OPTIONS_NAME))
186: soptions=&voptions->as_string();
187:
1.115 misha 188: bool print_debug=false;
189: if(Value* vdebug=hash->get(MAIL_DEBUG_NAME))
190: print_debug=vdebug->as_bool();
191:
1.138 ! moko 192: Value* vmail_conf=r.main_class.get_element(mail_name);
1.104 paf 193: Value* smtp_server_port=0;
1.138 ! moko 194:
1.104 paf 195: if(vmail_conf) {
1.138 ! moko 196: if(vmail_conf->get_hash()) {
! 197: // $MAIN:MAIL.SMTP[mail.yourdomain.ru[:port]]
! 198: smtp_server_port=vmail_conf->get_hash()->get("SMTP");
! 199: } else {
! 200: if( !vmail_conf->is_string() )
! 201: throw Exception(PARSER_RUNTIME, 0, "$" MAIL_CLASS_NAME ":" MAIL_NAME " is not hash");
! 202: vmail_conf=0;
! 203: }
1.104 paf 204: }
205:
1.89 paf 206: const String* from=0;
207: String* to=0;
1.134 moko 208: 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 209:
1.115 misha 210: if(print_debug)
1.131 moko 211: r.write(message);
1.115 misha 212: else
213: sendmail(vmail_conf, smtp_server_port, message, from, to, soptions);
1.6 paf 214: }
215:
1.24 paf 216: // constructor & configurator
1.23 paf 217:
1.89 paf 218: MMail::MMail(): Methoded(MAIL_CLASS_NAME) {
1.27 paf 219: // ^mail:send{hash}
1.23 paf 220: add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.68 paf 221: }
E-mail: