Annotation of parser3/src/classes/mail.C, revision 1.15

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.15    ! paf         8:        $Id: mail.C,v 1.14 2001/04/10 07:41:46 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.12      paf        21: #include "pa_exec.h"
1.4       paf        22: 
1.1       paf        23: // global var
                     24: 
                     25: VStateless_class *mail_class;
                     26: 
1.7       paf        27: // helpers
                     28: 
1.10      paf        29: // uuencode
                     30: 
                     31: static unsigned char uue_table[64] = {
                     32:   '`', '!', '"', '#', '$', '%', '&', '\'',
                     33:   '(', ')', '*', '+', ',', '-', '.', '/',
                     34:   '0', '1', '2', '3', '4', '5', '6', '7',
                     35:   '8', '9', ':', ';', '<', '=', '>', '?',
                     36:   '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
                     37:   'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
                     38:   'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                     39:   'X', 'Y', 'Z', '[', '\\',']', '^', '_'
                     40: };
1.8       paf        41: static void uuencode(String& result, const char *file_name_cstr, const VFile& vfile) {
1.10      paf        42:        //header
                     43:        result << "content-transfer-encoding: x-uuencode\n" << "\n";
                     44:        result << "begin 644 " << file_name_cstr << "\n";
                     45: 
                     46:        //body
                     47:        const unsigned char *itemp;
                     48: 
                     49:        int index;
                     50:        int count=45;
                     51: 
                     52:        const unsigned char *in=(const unsigned char *)vfile.value_ptr();
                     53:        size_t in_length=vfile.value_size();
                     54: 
                     55:        for(itemp=in; itemp<(in+in_length); itemp+=count) {
                     56:                if((itemp+count)>(in+in_length)) 
                     57:                        count=in_length-(itemp-in);
                     58: 
                     59:                char *buf=(char *)result.pool().malloc(MAX_STRING);
                     60:                char *optr=buf;
                     61:                
                     62:                /*
                     63:                * for UU and XX, encode the number of bytes as first character
                     64:                */
                     65:                *optr++ = uue_table[count];
                     66:                
                     67:                for (index=0; index<=count-3; index+=3) {
                     68:                        *optr++ = uue_table[itemp[index] >> 2];
                     69:                        *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | (itemp[index+1] >> 4)];
                     70:                        *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2) | (itemp[index+2] >> 6)];
                     71:                        *optr++ = uue_table[  itemp[index+2] & 0x3f];
                     72:                }
                     73:                
                     74:                /*
                     75:                * Special handlitempg for itempcomplete litempes
                     76:                */
                     77:                if (index != count) {
                     78:                        if (count - index == 2) {
                     79:                                *optr++ = uue_table[itemp[index] >> 2];
                     80:                                *optr++ = uue_table[((itemp[index  ] & 0x03) << 4) | 
                     81:                                        ( itemp[index+1] >> 4)];
                     82:                                *optr++ = uue_table[((itemp[index+1] & 0x0f) << 2)];
                     83:                                *optr++ = uue_table[0];
                     84:                        }
                     85:                        else if (count - index == 1) {
                     86:                                *optr++ = uue_table[ itemp[index] >> 2];
                     87:                                *optr++ = uue_table[(itemp[index] & 0x03) << 4];
                     88:                                *optr++ = uue_table[0];
                     89:                                *optr++ = uue_table[0];
                     90:                        }
                     91:                }
                     92:                /*
                     93:                * end of line
                     94:                */
                     95:                *optr++ = '\n'; 
                     96:                *optr = 0;
                     97:                result << buf;
                     98:        }
                     99:        
                    100:        //footer
                    101:        result.APPEND_CLEAN((const char *)uue_table, 1/* one char */, 0, 0) << "\n"
                    102:                "end\n";
1.8       paf       103: }
                    104: 
1.7       paf       105: /// ^mail:send[$attach[$type[uue|mime64] $value[DATA]]] 
                    106: static const String& attach_hash_to_string(Request& r, const String& origin_string, 
                    107:                                                                                   Hash& attach_hash) {
                    108:        Pool& pool=r.pool();
                    109: 
                    110:        Value *vtype=static_cast<Value *>(attach_hash.get(*new(pool) String(pool, "type")));
                    111:        if(!vtype)
                    112:                PTHROW(0, 0,
                    113:                        &origin_string,
                    114:                        "has no $type");
                    115: 
1.8       paf       116:        const VFile *vfile;
                    117:        if(Value *value=static_cast<Value *>(attach_hash.get(*value_name)))
                    118:                vfile=value->as_vfile();
                    119:        else
1.7       paf       120:                PTHROW(0, 0,
                    121:                        &origin_string,
                    122:                        "has no $value");
                    123: 
                    124:        const String *file_name;
                    125:        if(Value *vfile_name=static_cast<Value *>(attach_hash.get(
1.8       paf       126:                *new(pool) String(pool, "file-name")))) // specified $file-name
1.7       paf       127:                file_name=&vfile_name->as_string();
1.8       paf       128:        else // no $file-name
                    129:                if(Value *vfile_name=static_cast<Value *>(vfile->fields().get(*name_name)))
                    130:                        file_name=&vfile_name->as_string(); // VFile knows name
                    131:                else // vfile doesn't know name
                    132:                        file_name=new(pool) String(pool, "noname.dat");
                    133:        const char *file_name_cstr=file_name->cstr(String::UL_FILE_NAME);
1.7       paf       134: 
                    135:        String& result=*new(pool) String(pool);
                    136: 
1.10      paf       137:        // content-type: application/octet-stream
                    138:        result << "content-type: " << r.mime_type_of(file_name_cstr) << "\n";
1.7       paf       139:        // content-disposition: attachment; filename="user_file_name"
1.10      paf       140:        result << "content-disposition: attachment; filename=\"" << file_name_cstr << "\"\n";
1.7       paf       141: 
                    142:        const String& type=vtype->as_string();
                    143:        if(type=="uue") {
1.8       paf       144:                uuencode(result, file_name_cstr, *vfile);
1.7       paf       145:        } else 
                    146:                PTHROW(0, 0,
                    147:                        &type,
                    148:                        "unknown encode type");
                    149:        
                    150:        return result;
                    151: }
                    152: 
1.1       paf       153: 
                    154: struct Mail_info {
                    155:        String *attribute_to_exclude;
                    156:        String *header;
1.2       paf       157:        const String **from, **to;
1.1       paf       158: };
                    159: static void add_header_attribute(const Hash::Key& aattribute, Hash::Val *ameaning, 
                    160:                                                                 void *info) {
                    161: 
                    162:        Value& lmeaning=*static_cast<Value *>(ameaning);
                    163:        Mail_info& mi=*static_cast<Mail_info *>(info);
1.2       paf       164: 
                    165:        // exclude one attribute [body]
1.1       paf       166:        if(aattribute==*mi.attribute_to_exclude)
                    167:                return;
                    168: 
1.2       paf       169:        // fetch from & to from header for SMTP
                    170:        if(mi.from && aattribute=="from")
                    171:                *mi.from=&lmeaning.as_string();
                    172:        if(mi.to && aattribute=="to")
                    173:                *mi.to=&lmeaning.as_string();
                    174: 
                    175:        // append header line
1.10      paf       176:        *mi.header << 
                    177:                aattribute << ":" << 
                    178:                attributed_meaning_to_string(lmeaning, String::UL_MAIL_HEADER) << 
                    179:                "\n";
1.2       paf       180: }
                    181: struct Seq_item {
1.7       paf       182:        const String *part_name;
1.2       paf       183:        Value *part_value;
                    184: };
1.7       paf       185: static void add_part(const Hash::Key& part_name, Hash::Val *part_value, 
1.2       paf       186:                                         void *info) {
                    187:        Seq_item **seq_ref=static_cast<Seq_item **>(info);
1.7       paf       188:        (**seq_ref).part_name=&part_name;
1.2       paf       189:        (**seq_ref).part_value=static_cast<Value *>(part_value);
                    190:        (*seq_ref)++;
                    191: }
                    192: static double key_of_part(const void *item) {
1.7       paf       193:        const char *cstr=static_cast<const Seq_item *>(item)->part_name->cstr();
1.2       paf       194:        char *error_pos;
                    195:        return strtod(cstr, &error_pos);
                    196: }
                    197: static int sort_cmp_string_double_value(const void *a, const void *b) {
                    198:        double va=key_of_part(a);
                    199:        double vb=key_of_part(b);
1.7       paf       200: 
                    201:        // 0 logically equals infinity. so that attachments would go last
                    202:        if(va==0)
                    203:                return +1;
                    204:        if(vb==0)
                    205:                return -1;
                    206: 
1.2       paf       207:        if(va<vb)
                    208:                return -1;
                    209:        else if(va>vb)
                    210:                return +1;
                    211:        else 
                    212:                return 0;
1.1       paf       213: }
1.2       paf       214: static const String& letter_hash_to_string(Request& r, const String& method_name, 
                    215:                                                                                   Hash& letter_hash, int level,
                    216:                                                                                   const String **from, const String **to) {
                    217:        Pool& pool=r.pool();
                    218: 
                    219:        // prepare header: 'hash' without "body"
                    220:        String& result=*new(pool) String(pool);
1.15    ! paf       221:        *from=*to=0;
1.2       paf       222:        Mail_info mail_info={
                    223:                /*excluding*/ body_name,
                    224:                &result,
                    225:                from, to
                    226:        };
                    227:        letter_hash.for_each(add_header_attribute, &mail_info);
                    228: 
                    229:        if(Value *body_element=static_cast<Value *>(letter_hash.get(*body_name))) {
                    230:                if(Hash *body_hash=body_element->get_hash()) {
                    231:                        char *boundary=(char *)pool.malloc(MAX_NUMBER);
1.9       paf       232:                        snprintf(boundary, MAX_NUMBER-5/*lEvEl*/, "lEvEl%d", level);
1.2       paf       233:                        // multi-part
1.10      paf       234:                        result << "content-type: multipart/mixed; boundary=\"" << boundary << "\"\n";
                    235:                        result << "\n" 
1.2       paf       236:                                "This is a multi-part message in MIME format.";
                    237: 
1.3       paf       238:                        // body parts..
                    239:                        // ..collect
1.2       paf       240:                        Seq_item *seq=(Seq_item *)malloc(sizeof(Seq_item)*body_hash->size());
                    241:                        Seq_item *seq_ref=seq;  body_hash->for_each(add_part, &seq_ref);
1.3       paf       242:                        // ..sort
1.2       paf       243:                        _qsort(seq, body_hash->size(), sizeof(Seq_item), 
                    244:                                sort_cmp_string_double_value);
1.3       paf       245:                        // ..insert in 'seq' order
1.2       paf       246:                        for(int i=0; i<body_hash->size(); i++) {
                    247:                                // intermediate boundary
1.10      paf       248:                                result << "\n--" << boundary << "\n";
1.2       paf       249: 
                    250:                                if(Hash *part_hash=seq[i].part_value->get_hash())
1.7       paf       251:                                        if(seq[i].part_name->mid(0, 6/*attach*/)=="attach")
1.10      paf       252:                                                result << attach_hash_to_string(r, *seq[i].part_name, *part_hash);
1.7       paf       253:                                        else 
1.10      paf       254:                                                result << letter_hash_to_string(r, method_name, *part_hash, 
1.7       paf       255:                                                        level+1, 0, 0);
1.2       paf       256:                                else
                    257:                                        PTHROW(0, 0,
1.7       paf       258:                                                seq[i].part_name,
1.2       paf       259:                                                "part is not hash");
                    260:                        }
                    261: 
                    262:                        // finish boundary
1.10      paf       263:                        result << "\n--" << boundary << "--\n";
1.2       paf       264:                } else {
1.10      paf       265:                        result << 
                    266:                                "\n" << // header|body separator
                    267:                                body_element->as_string();  // body
1.2       paf       268:                }
                    269:        } else 
                    270:                PTHROW(0, 0,
                    271:                        &method_name,
                    272:                        "has no $body");
                    273: 
                    274:        return result;
                    275: }
                    276: 
1.4       paf       277: static void sendmail(Request& r, const String& method_name, 
                    278:                                         const String& letter, 
                    279:                                         const String *from, const String *to) {
                    280:        Pool& pool=r.pool();
                    281: 
1.12      paf       282:        char *letter_cstr=letter.cstr();
                    283: 
1.15    ! paf       284: #ifdef WIN32
1.4       paf       285:        if(!from)
                    286:                PTHROW(0, 0,
                    287:                        &method_name,
1.15    ! paf       288:                        "has no 'from' header specified");
1.4       paf       289:        if(!to)
                    290:                PTHROW(0, 0,
                    291:                        &method_name,
1.15    ! paf       292:                        "has no 'to' header specified");
1.4       paf       293: 
                    294:        SMTP& smtp=*new(pool) SMTP(pool, method_name);
1.5       paf       295:        Value *server_port;
1.4       paf       296:        // $MAIN:MAIL.SMTP[mail.design.ru]
1.5       paf       297:        if(r.mail && 
                    298:                (server_port=static_cast<Value *>(r.mail->get(
                    299:                        *new(pool) String(pool, "SMTP"))))) {
                    300:                char *server=server_port->as_string().cstr();
1.11      paf       301:                const char *port=rsplit(server, ':');
1.4       paf       302:                if(!port)
1.11      paf       303:                        port="25";
1.4       paf       304: 
                    305:                smtp.Send(server, port, letter_cstr, from->cstr(), to->cstr());
                    306:        } else
                    307:                PTHROW(0, 0,
                    308:                        &method_name,
1.13      paf       309:                        "$"MAIN_CLASS_NAME":"MAIL_NAME".SMTP not defined");
1.4       paf       310: #else
1.12      paf       311:        // unix
1.13      paf       312:        // $MAIN:MAIL.prog1["/usr/sbin/sendmail -t"] default
                    313:        // $MAIN:MAIL.prog2["/usr/lib/sendmail -t"] default
1.12      paf       314:        if(r.mail) {
                    315:                char no_cstr[MAX_NUMBER];
1.13      paf       316:                for(int no=-2; ; no++) {
                    317:                        const String *prog_string;
                    318:                        switch(no) {
                    319:                        case -2: prog_string=new(pool) String(pool, "/usr/sbin/sendmail -t"); break;
                    320:                        case -1: prog_string=new(pool) String(pool, "/usr/lib/sendmail -t"); break;
                    321:                        default: 
                    322:                                {
                    323:                                        String prog_key(pool, "prog");
                    324:                                        snprintf(no_cstr, MAX_NUMBER, "%d", no);
                    325:                                        prog_key << no_cstr;
                    326:                                        if(Value *prog_value=static_cast<Value *>(r.mail->get(prog_key)))
                    327:                                                prog_string=&prog_value->as_string();
                    328:                                        else
                    329:                                                if(no==0)
                    330:                                                        continue;
                    331:                                                else
                    332:                                                        PTHROW(0, 0,
                    333:                                                                &method_name,
                    334:                                                                "$"MAIN_CLASS_NAME":"MAIL_NAME".%s not defined", 
                    335:                                                                prog_key.cstr());
1.12      paf       336:                                }
1.13      paf       337:                        }
                    338:                        // we know prog_string here
                    339:                        Array argv(pool);
                    340:                        const String *file_spec;
                    341:                        int after_file_spec=prog_string->pos(" ", 1);
                    342:                        if(after_file_spec<=0)
                    343:                                file_spec=prog_string;
                    344:                        else {
                    345:                                size_t pos_after=after_file_spec;
                    346:                                file_spec=&prog_string->mid(0, pos_after);
                    347:                                prog_string->split(argv, &pos_after, " ", 1, String::UL_CLEAN);
                    348:                        }
1.12      paf       349: 
1.14      paf       350:                        // skip unavailable default programs
                    351:                        if(no<0 && !file_executable(*file_spec))
1.13      paf       352:                                continue;
                    353: 
                    354:                        String in(pool, letter_cstr); String out(pool); String err(pool);
                    355:                        int exit_status=pa_exec(*file_spec,
                    356:                                0/*default env*/,
                    357:                                &argv,
                    358:                                in, out, err);
                    359:                        if(exit_status || err.size())
1.12      paf       360:                                PTHROW(0, 0,
                    361:                                        &method_name,
1.13      paf       362:                                        "'%s' reported problem: %s (%d)",
                    363:                                                file_spec->cstr(),
                    364:                                                err.size()?err.cstr():"UNKNOWN", 
                    365:                                                exit_status);
                    366:                        break;
1.12      paf       367:                }
                    368:        } else
                    369:                PTHROW(0, 0,
                    370:                        &method_name,
1.13      paf       371:                        "$"MAIN_CLASS_NAME":"MAIL_NAME" not defined");
1.4       paf       372: #endif
                    373: }
                    374: 
1.7       paf       375: 
                    376: // methods
                    377: 
1.1       paf       378: static void _send(Request& r, const String& method_name, Array *params) {
                    379:        Pool& pool=r.pool();
                    380: 
                    381:        Value& vhash=*static_cast<Value *>(params->get(0));
                    382:        // forcing [this body type]
                    383:        r.fail_if_junction_(true, vhash, method_name, "message must not be code");
                    384: 
                    385:        Hash *hash=vhash.get_hash();
                    386:        if(!hash)
                    387:                PTHROW(0, 0,
                    388:                        &method_name,
                    389:                        "message must be hash");
                    390: 
1.2       paf       391:        const String *from, *to;
1.4       paf       392:        const String& letter=letter_hash_to_string(r, method_name, *hash, 0, &from, &to);
1.1       paf       393: 
1.10      paf       394:        //r.write_assign_lang(*new(pool) VString(letter));
                    395:        sendmail(r, method_name, letter, from, to);
1.6       paf       396: }
                    397: 
1.1       paf       398: // initialize
                    399: void initialize_mail_class(Pool& pool, VStateless_class& vclass) {
                    400:        // ^mail:send{hash}
                    401:        vclass.add_native_method("send", Method::CT_STATIC, _send, 1, 1);
                    402: }

E-mail: