Changeset 15172 for trunk


Ignore:
Timestamp:
03/27/13 18:00:31 (12 years ago)
Author:
tbretz
Message:
Changed the Mail class a bit to have the arrays as data members ReadOnly.
Location:
trunk/FACT++
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/InterpreterV8.cc

    r15158 r15172  
    775775        return ThrowException(String::New("Mail must be called as constructor"));
    776776
     777    if (args.Length()!=1 || !args[0]->IsString())
     778        return ThrowException(String::New("Constructor must be called with a single string as argument"));
     779
    777780    HandleScope handle_scope;
    778781
     782    Handle<Array> rec = Array::New();
     783    Handle<Array> att = Array::New();
     784    Handle<Array> bcc = Array::New();
     785    Handle<Array> cc  = Array::New();
     786    Handle<Array> txt = Array::New();
     787    if (rec.IsEmpty() || att.IsEmpty() || bcc.IsEmpty() || cc.IsEmpty() || txt.IsEmpty())
     788        return Undefined();
     789
    779790    Handle<Object> self = args.This();
     791
     792    self->Set(String::New("subject"),     args[0]->ToString(), ReadOnly);
     793    self->Set(String::New("recipients"),  rec, ReadOnly);
     794    self->Set(String::New("attachments"), att, ReadOnly);
     795    self->Set(String::New("bcc"),         bcc, ReadOnly);
     796    self->Set(String::New("cc"),          cc,  ReadOnly);
     797    self->Set(String::New("text"),        txt, ReadOnly);
     798
    780799    self->Set(String::New("send"), FunctionTemplate::New(WrapSendMail)->GetFunction(), ReadOnly);
     800
    781801    return handle_scope.Close(self);
    782802}
    783803
    784 vector<string> InterpreterV8::ValueToArray(const Handle<Value> &val)
     804vector<string> InterpreterV8::ValueToArray(const Handle<Value> &val, bool only)
    785805{
    786806    vector<string> rc;
    787     if (val->IsString())
    788     {
    789         rc.push_back(*String::AsciiValue(val->ToString()));
    790         return rc;
    791     }
    792 
    793     if (val->IsArray())
    794     {
    795         Handle<Array> arr = Handle<Array>::Cast(val);
    796         for (uint32_t i=0; i<arr->Length(); i++)
    797         {
    798             Handle<Value> obj = arr->Get(i);
    799             if (obj.IsEmpty())
    800                 continue;
    801 
    802             if (!obj->IsString())
    803                 return vector<string>();
    804 
    805             rc.push_back(*String::AsciiValue(obj->ToString()));
    806         }
     807
     808    Handle<Array> arr = Handle<Array>::Cast(val);
     809    for (uint32_t i=0; i<arr->Length(); i++)
     810    {
     811        Handle<Value> obj = arr->Get(i);
     812        if (obj.IsEmpty())
     813            continue;
     814
     815        if (obj->IsNull() || obj->IsUndefined())
     816            continue;
     817
     818        if (only && !obj->IsString())
     819            continue;
     820
     821        rc.push_back(*String::AsciiValue(obj->ToString()));
    807822    }
    808823
     
    823838
    824839    const Handle<Value> sub = args.This()->Get(String::New("subject"));
    825     const Handle<Value> rec = args.This()->Get(String::New("recipient"));
     840    const Handle<Value> rec = args.This()->Get(String::New("recipients"));
    826841    const Handle<Value> txt = args.This()->Get(String::New("text"));
    827     const Handle<Value> att = args.This()->Get(String::New("attachment"));
     842    const Handle<Value> att = args.This()->Get(String::New("attachments"));
    828843    const Handle<Value> bcc = args.This()->Get(String::New("bcc"));
    829844    const Handle<Value> cc  = args.This()->Get(String::New("cc"));
    830845
    831     if (sub->IsUndefined())
    832         return ThrowException(String::New("Required property 'subject' missing."));
    833 
    834846    const vector<string> vrec = ValueToArray(rec);
    835     const vector<string> vtxt = ValueToArray(txt);
     847    const vector<string> vtxt = ValueToArray(txt, false);
    836848    const vector<string> vatt = ValueToArray(att);
    837849    const vector<string> vbcc = ValueToArray(bcc);
     
    839851
    840852    if (vrec.size()==0)
    841         return ThrowException(String::New("Required property 'recipient' must be a string or an array of strings."));
     853        return ThrowException(String::New("At least one valid string is required in 'recipients'."));
    842854    if (vtxt.size()==0)
    843         return ThrowException(String::New("Required property 'text' must be a string or an array of strings."));
    844     if (vatt.size()==0 && !att->IsUndefined())
    845         return ThrowException(String::New("Property 'attachment' must be a string or an array of strings."));
    846     if (vbcc.size()==0 && !bcc->IsUndefined())
    847         return ThrowException(String::New("Property 'bcc' must be a string or an array of strings."));
    848     if (vcc.size()==0 && !cc->IsUndefined())
    849         return ThrowException(String::New("Property 'cc' must be a string or an array of strings."));
    850 
    851     if (!sub->IsString())
    852         return ThrowException(String::New("Property 'subject' must be a string."));
     855        return ThrowException(String::New("At least one valid string is required in 'text'."));
    853856
    854857    const string subject = *String::AsciiValue(sub->ToString());
    855 
    856858
    857859    FILE *pipe = popen(("from=no-reply@fact-project.org mailx -~ "+vrec[0]).c_str(), "w");
     
    23892391    fTemplateDescription = desc;
    23902392
    2391 #ifdef HAVE_MAIL
     2393#ifdef HAVE_MAILX
    23922394    Handle<FunctionTemplate> mail = FunctionTemplate::New(ConstructorMail);
    23932395    mail->SetClassName(String::New("Mail"));
  • trunk/FACT++/src/InterpreterV8.h

    r15156 r15172  
    6464    void Thread(int &id, v8::Persistent<v8::Function> func, uint32_t ms);
    6565
    66     std::vector<std::string> ValueToArray(const v8::Handle<v8::Value> &val);
     66    std::vector<std::string> ValueToArray(const v8::Handle<v8::Value> &val, bool only=true);
    6767
    6868    v8::Handle<v8::Value> FuncWait(const v8::Arguments& args);
Note: See TracChangeset for help on using the changeset viewer.