smtpo.cpp

来自「Shorthand是一个强大的脚本语言」· C++ 代码 · 共 89 行

CPP
89
字号
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/smtpo.cpp 2     8/28/02 6:27a Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// smtpo.cpp: ShortHand SMTP object
///////////////////////////////////////////////////////////////////////////////
#include "smtp.h"
#include "smtpo.h"
#include "except.h"
#include "value.h"
#include "module.h"

ShhSMTP::ShhSMTP(ShhModule& module, const YYLTYPE& location)
: ShhObject(module, "SMTP", location)
{
    m_port = 25;
}

ShhSMTP::~ShhSMTP()
{
}

void ShhSMTP::constructor(ShhValueList* args)
{
    if (args == NULL || args->size() == 0 || !args->get(0)->isString())
        throw new ShhObjectException("SMTP constructor requires server name as first argument");

    args->get(0)->toString(m_host);

    if (args->size() > 1)
    {
        m_port = args->get(1)->toInt();
    }

    ShhValue port_value(m_port);

    // assign read-only properties
    setProperty("HOST",  *(args->get(0)) );
    setProperty("PORT",  port_value);
}

bool ShhSMTP::hasMethod(const char* name)
{
    if (stricmp(name, "send") == 0) return true;
    else return false;
}

ShhValue ShhSMTP::executeMethod(const char* name, ShhValueList* args)
{
    ShhValue result;
    if (stricmp(name, "send") == 0)
    {
        result = send(args);
    }
    return result;
}

int ShhSMTP::send(ShhValueList* args)
{
    if (args == NULL || args->size() < 4)
        throw new ShhObjectException("SMTP.send() requires 4 arguments");
    
    string from, to, subject, body;
    args->get(0)->toString(from);
    args->get(1)->toString(to);
    args->get(2)->toString(subject);
    args->get(3)->toString(body);

    setProperty("ERROR", "");
    
    CSMTPSession smtp(m_host, m_port);
    int code = smtp.connect();
    if (code == 0)
        code = smtp.sendMessage(from, to, subject, body);
    if (code != 0)
    {
        ShhValue error(m_module.scratch().strdup(smtp.getErrorText()));
        setProperty("ERROR", error);
        return code;
    }

    smtp.disconnect();
    return 0;
}


⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?