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

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

E-mail: