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