Annotation of parser3/src/classes/mail.C, revision 1.6
1.1 paf 1: /** @file
2: Parser: @b mail parser class.
3:
4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
5:
6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.6 ! paf 8: $Id: mail.C,v 1.5 2001/04/07 13:56:44 paf Exp $
1.1 paf 9: */
10:
11: #include "pa_config_includes.h"
12:
1.6 ! paf 13: #ifdef WIN32
! 14: # include "smtp/smtp.h"
! 15: #endif
! 16:
1.1 paf 17: #include "_mail.h"
18: #include "pa_common.h"
19: #include "pa_request.h"
1.6 ! paf 20: #include "pa_vfile.h"
1.4 paf 21:
1.1 paf 22: // global var
23:
24: VStateless_class *mail_class;
25:
26: // methods
27:
28: struct Mail_info {
29: String *attribute_to_exclude;
30: String *header;
1.2 paf 31: const String **from, **to;
1.1 paf 32: };
33:
34: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning,
35: void *info) {
36:
37: Value& lmeaning=*static_cast<Value *>(ameaning);
38: Mail_info& mi=*static_cast<Mail_info *>(info);
1.2 paf 39:
40: // exclude one attribute [body]
1.1 paf 41: if(aattribute==*mi.attribute_to_exclude)
42: return;
43:
1.2 paf 44: // fetch from & to from header for SMTP
45: if(mi.from && aattribute=="from")
46: *mi.from=&lmeaning.as_string();
47: if(mi.to && aattribute=="to")
48: *mi.to=&lmeaning.as_string();
49:
50: // append header line
51: *mi.header+=aattribute;
52: *mi.header+=":";
53: *mi.header+=attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER);
54: *mi.header+="\n";
55: }
56: struct Seq_item {
57: const String *part_number;
58: Value *part_value;
59: };
60: static void add_part(const Hash::Key& part_number, Hash::Val *part_value,
61: void *info) {
62: Seq_item **seq_ref=static_cast<Seq_item **>(info);
63: (**seq_ref).part_number=&part_number;
64: (**seq_ref).part_value=static_cast<Value *>(part_value);
65: (*seq_ref)++;
66: }
67: static double key_of_part(const void *item) {
68: const char *cstr=static_cast<const Seq_item *>(item)->part_number->cstr();
69: char *error_pos;
70: return strtod(cstr, &error_pos);
71: }
72: static int sort_cmp_string_double_value(const void *a, const void *b) {
73: double va=key_of_part(a);
74: double vb=key_of_part(b);
75: if(va<vb)
76: return -1;
77: else if(va>vb)
78: return +1;
79: else
80: return 0;
1.1 paf 81: }
1.2 paf 82: static const String& letter_hash_to_string(Request& r, const String& method_name,
83: Hash& letter_hash, int level,
84: const String **from, const String **to) {
85: Pool& pool=r.pool();
86:
87: // prepare header: 'hash' without "body"
88: String& result=*new(pool) String(pool);
89: Mail_info mail_info={
90: /*excluding*/ body_name,
91: &result,
92: from, to
93: };
94: letter_hash.for_each(add_header_attribute, &mail_info);
95:
96: if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
97: if(Hash *body_hash=body_element->get_hash()) {
98: char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.3 paf 99: snprintf(boundary, MAX_NUMBER-6/*level_*/, "level_%d", level);
1.2 paf 100: // multi-part
1.3 paf 101: ((result+=
102: "content-type: multipart/mixed;\n"
103: " boundary=\"----=")+=boundary)+="\"\n"
1.2 paf 104: "\n"
105: "This is a multi-part message in MIME format.";
106:
1.3 paf 107: // body parts..
108: // ..collect
1.2 paf 109: Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
110: Seq_item *seq_ref=seq; body_hash->for_each(add_part, &seq_ref);
1.3 paf 111: // ..sort
1.2 paf 112: _qsort(seq, body_hash->size(), sizeof(Seq_item),
113: sort_cmp_string_double_value);
1.3 paf 114: // ..insert in 'seq' order
1.2 paf 115: for(int i=0; i<body_hash->size(); i++) {
116: // intermediate boundary
1.3 paf 117: ((result+="\n------=")+=boundary)+="\n";
1.2 paf 118:
119: if(Hash *part_hash=seq[i].part_value->get_hash())
120: result+=letter_hash_to_string(r, method_name, *part_hash,
121: level+1, 0, 0);
122: else
123: PTHROW(0, 0,
124: seq[i].part_number,
125: "part is not hash");
126: }
127:
128: // finish boundary
1.3 paf 129: ((result+="\n------=")+=boundary)+="--\n";
1.2 paf 130: } else {
131: result+="\n"; // header|body separator
1.4 paf 132: result+=body_element->as_string(); // body
1.2 paf 133: }
134: } else
135: PTHROW(0, 0,
136: &method_name,
137: "has no $body");
138:
139: return result;
140: }
141:
1.4 paf 142:
143: /// @test unix ver
144: static void sendmail(Request& r, const String& method_name,
145: const String& letter,
146: const String *from, const String *to) {
147: Pool& pool=r.pool();
148:
149: #ifdef WIN32
150: if(!from)
151: PTHROW(0, 0,
152: &method_name,
153: "not specified 'from'");
154: if(!to)
155: PTHROW(0, 0,
156: &method_name,
157: "not specified 'to'");
158:
159: char *letter_cstr=letter.cstr();
160: SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5 paf 161: Value *server_port;
1.4 paf 162: // $MAIN:MAIL.SMTP[mail.design.ru]
1.5 paf 163: if(r.mail &&
164: (server_port=static_cast<Value *>(r.mail->get(
165: *new(pool) String(pool, "SMTP"))))) {
166: char *server=server_port->as_string().cstr();
1.4 paf 167: char *port=rsplit(server, ':');
168: if(!port)
1.5 paf 169: port=const_cast<char *>("25");
1.4 paf 170:
171: smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
172: } else
173: PTHROW(0, 0,
174: &method_name,
175: "$MAIN:MAIL.SMTP not defined");
176: #else
177: PTHROW(0, 0,
178: &method_name,
179: "todo");
180: #endif
181: }
182:
1.1 paf 183: static void _send(Request& r, const String& method_name, Array *params) {
184: Pool& pool=r.pool();
185:
186: Value& vhash=*static_cast<Value *>(params->get(0));
187: // forcing [this body type]
188: r.fail_if_junction_(true, vhash, method_name, "message must not be code");
189:
190: Hash *hash=vhash.get_hash();
191: if(!hash)
192: PTHROW(0, 0,
193: &method_name,
194: "message must be hash");
195:
1.2 paf 196: const String *from, *to;
1.4 paf 197: const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1 paf 198:
1.6 ! paf 199: r.write_assign_lang(*new(pool) VString(letter));
! 200: //sendmail(r, method_name, letter, from, to);
! 201: }
! 202:
! 203: /// ^mail:attach[uue|base64;DATA]
! 204: /// ^mail:attach[uue|base64;DATA;user-file-name]
! 205: static void _attach(Request& r, const String& method_name, Array *params) {
! 206: Pool& pool=r.pool();
! 207:
! 208: Value& vtype=*static_cast<Value *>(params->get(0));
! 209: // forcing [this vtype]
! 210: r.fail_if_junction_(true, vtype, method_name, "type must not be code");
! 211:
! 212: Value& vdata=*static_cast<Value *>(params->get(1));
! 213: // forcing [this vtype]
! 214: r.fail_if_junction_(true, vdata, method_name, "data must not be code");
! 215: const VFile& vfile=*vdata.as_vfile();
! 216:
! 217: Value *user_file_name;
! 218: if(params->size()>2) {
! 219: user_file_name=static_cast<Value *>(params->get(0));
! 220: // forcing [this vtype]
! 221: r.fail_if_junction_(true, *user_file_name, method_name,
! 222: "user file name must not be code");
! 223: } else {
! 224: user_file_name=static_cast<Value *>(vfile.fields().get(*name_name));
! 225: }
! 226:
! 227: VHash& result=*new(pool) VHash(pool);
! 228:
! 229: { // content-disposition: attachment; filename='user_file_name'
! 230: VHash& content_disposition=*new(pool) VHash(pool);
! 231: content_disposition.hash().put(*value_name,
! 232: new(pool) VString(*new(pool) String(pool, "attachment")));
! 233: content_disposition.hash().put(
! 234: *new(pool) String(pool, "filename"),
! 235: user_file_name);
! 236: result.hash().put(*new(pool) String(pool, "content-disposition"),
! 237: &content_disposition);
! 238: }
! 239:
! 240: const String& type=vtype.as_string();
! 241: if(type=="uue") {
! 242: { // content-transfer-encoding: x-uuencode
! 243: VString& content_transfer_encoding=*new(pool) VString(
! 244: *new(pool) String(pool, "x-uuencode"));
! 245: result.hash().put(*new(pool) String(pool, "content-transfer-encoding"),
! 246: &content_transfer_encoding);
! 247: }
! 248:
! 249: result.hash().put(*body_name,
! 250: new(pool) String(pool, "todo"));
! 251: } else
! 252: PTHROW(0, 0,
! 253: &type,
! 254: "unknown encode type");
! 255:
! 256: result.set_name(*new(pool) String(pool, "100")); // so that would go last
! 257: r.write_no_lang(result);
1.1 paf 258: }
259:
260: // initialize
261: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
262: // ^mail:send{hash}
263: vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
1.6 ! paf 264:
! 265: // ^mail:encode[uue|base64;DATA]
! 266: // ^mail:encode[uue|base64;DATA;user-file-name]
! 267: vclass.add_native_method("attach", Method::CT_ANY, _attach, 2, 3);
1.1 paf 268: }
E-mail: