/* ============================================================================ * Douglas Thrift's Web Contact License * * Copyright (C) 2002, Douglas Thrift. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: * * "This product includes software developed by Douglas Thrift * (http://computers.douglasthrift.net/webcontact.html)." * * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "Douglas Thrift" and "Douglas Thrift's Web Contact" must not be * used to endorse or promote products derived from this software without * specific prior written permission. For written permission, please visit * http://www.douglasthrift.net/contact.html for contact information. * * 5. Products derived from this software may not be called "Douglas Thrift's * Web Contact", nor may "Douglas Thrift's Web Contact" appear in their * name, without prior written permission. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================ */ // Windows XP FAQ Poll // // Douglas Thrift // // IMAPHandler.cpp #include "IMAPHandler.h" #ifdef _WIN32 #define PATH_SEPARATOR "\\" #define ENV_SEPARATOR ';' #else #define PATH_SEPARATOR "/" #define ENV_SEPARATOR ':' #endif IMAPHandler::IMAPHandler(const string& host, unsigned port) { setJarPath(); options[0].optionString = jarPath; memset(&vm_args, 0, sizeof (vm_args)); vm_args.version = JNI_VERSION_1_2; vm_args.nOptions = 1; vm_args.options = options; status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args); if (status != JNI_ERR) { cls = env->FindClass("IMAPHandler"); if (cls != 0) { mid = env->GetMethodID(cls, "", "(Ljava/lang/String;IZ)V"); if (mid != 0) { jstring hostJ = env->NewStringUTF(host.c_str()); jint portJ = port; #ifdef _DEBUG jboolean debugJ = debug; #else jboolean debugJ = false; #endif obj = env->NewObject(cls, mid, hostJ, portJ, debugJ); } } } } IMAPHandler::~IMAPHandler() { if (status != JNI_ERR) { jvm->DestroyJavaVM(); } } bool IMAPHandler::successful() { bool answer; if (cls != 0) { mid = env->GetMethodID(cls, "successful", "()Z"); if (mid != 0) { jboolean answerJ = env->CallBooleanMethod(obj, mid); answer = answerJ != 0; } } return answer; } void IMAPHandler::setJarPath() { const string jarFile = "IMAPHandler.jar"; string jarFilePath = jarFile; ifstream fin(jarFilePath.c_str()); if (!fin.is_open()) { unsigned end = program.rfind(PATH_SEPARATOR); if (end != string::npos) { string path = program.substr(0, end); jarFilePath = path + PATH_SEPARATOR + jarFile; fin.open(jarFilePath.c_str()); if (!fin.is_open()) { cerr << program << ": Could not find required file: " << jarFile << "\n"; exit(1); } else { fin.close(); } } else { string envPath = getenv("PATH"); if (debug) cerr << "envPath = " << envPath << "\n"; unsigned begin = 0; do { unsigned end = envPath.find(ENV_SEPARATOR, begin); string path = envPath.substr(begin, end); if (debug) cerr << "path = " << path << "\n"; jarFilePath = path + PATH_SEPARATOR + jarFile; fin.open(jarFilePath.c_str()); if (fin.is_open()) { fin.close(); break; } begin = end != string::npos ? end + 1 : string::npos; } while (begin < envPath.length()); if (begin == string::npos) { cerr << program << ": Could not find required file: " << jarFile << "\n"; exit(1); } } } else { fin.close(); } string jarPath = "-Djava.class.path=" + jarFilePath; this->jarPath = new char[jarPath.length()]; strcpy(this->jarPath, jarPath.c_str()); } string IMAPHandler::imap(char* imap) { string result; if (cls != 0) { mid = env->GetMethodID(cls, imap, "()Ljava/lang/String;"); if (mid != 0) { jstring resultJ = (jstring)env->CallObjectMethod(obj, mid); const char* resultC = env->GetStringUTFChars(resultJ, 0); result = resultC; env->ReleaseStringUTFChars(resultJ, resultC); } } return result; } string IMAPHandler::imap(char* imap, const string& args) { string result; if (cls != 0) { mid = env->GetMethodID(cls, imap, "(Ljava/lang/String;)Ljava/lang/String;"); if (mid != 0) { jstring argsJ = env->NewStringUTF(args.c_str()); jstring resultJ = (jstring)env->CallObjectMethod(obj, mid, argsJ); const char* resultC = env->GetStringUTFChars(resultJ, 0); result = resultC; env->ReleaseStringUTFChars(resultJ, resultC); } } return result; } string IMAPHandler::imap(char* imap, const string& args, const string& message) { string result; if (cls != 0) { mid = env->GetMethodID(cls, imap, "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); if (mid != 0) { jstring argsJ = env->NewStringUTF(args.c_str()); jstring messageJ = env->NewStringUTF(message.c_str()); jstring resultJ = (jstring)env->CallObjectMethod(obj, mid, argsJ, messageJ); const char* resultC = env->GetStringUTFChars(resultJ, 0); result = resultC; env->ReleaseStringUTFChars(resultJ, resultC); } } return result; }