ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/WinXPFAQPoll/IMAPHandler.cpp
Revision: 104
Committed: 2003-03-31T20:18:26-08:00 (22 years, 2 months ago) by douglas
File size: 6583 byte(s)
Log Message:
Initial revision

File Contents

# Content
1 /* ============================================================================
2 * Douglas Thrift's Web Contact License
3 *
4 * Copyright (C) 2002, Douglas Thrift. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. The end-user documentation included with the redistribution, if any, must
17 * include the following acknowledgment:
18 *
19 * "This product includes software developed by Douglas Thrift
20 * (http://computers.douglasthrift.net/webcontact.html)."
21 *
22 * Alternately, this acknowledgment may appear in the software itself, if
23 * and wherever such third-party acknowledgments normally appear.
24 *
25 * 4. The names "Douglas Thrift" and "Douglas Thrift's Web Contact" must not be
26 * used to endorse or promote products derived from this software without
27 * specific prior written permission. For written permission, please visit
28 * http://www.douglasthrift.net/contact.html for contact information.
29 *
30 * 5. Products derived from this software may not be called "Douglas Thrift's
31 * Web Contact", nor may "Douglas Thrift's Web Contact" appear in their
32 * name, without prior written permission.
33 *
34 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
35 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
40 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
41 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
43 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 * ============================================================================
45 */
46 // Windows XP FAQ Poll
47 //
48 // Douglas Thrift
49 //
50 // IMAPHandler.cpp
51
52 #include "IMAPHandler.h"
53
54 #ifdef _WIN32
55 #define PATH_SEPARATOR "\\"
56 #define ENV_SEPARATOR ';'
57 #else
58 #define PATH_SEPARATOR "/"
59 #define ENV_SEPARATOR ':'
60 #endif
61
62 IMAPHandler::IMAPHandler(const string& host, unsigned port)
63 {
64 setJarPath();
65
66 options[0].optionString = jarPath;
67 memset(&vm_args, 0, sizeof (vm_args));
68 vm_args.version = JNI_VERSION_1_2;
69 vm_args.nOptions = 1;
70 vm_args.options = options;
71
72 status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
73
74 if (status != JNI_ERR)
75 {
76 cls = env->FindClass("IMAPHandler");
77
78 if (cls != 0)
79 {
80 mid = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;IZ)V");
81
82 if (mid != 0)
83 {
84 jstring hostJ = env->NewStringUTF(host.c_str());
85 jint portJ = port;
86 #ifdef _DEBUG
87 jboolean debugJ = debug;
88 #else
89 jboolean debugJ = false;
90 #endif
91
92 obj = env->NewObject(cls, mid, hostJ, portJ, debugJ);
93 }
94 }
95 }
96 }
97
98 IMAPHandler::~IMAPHandler()
99 {
100 if (status != JNI_ERR)
101 {
102 jvm->DestroyJavaVM();
103 }
104 }
105
106 bool IMAPHandler::successful()
107 {
108 bool answer;
109
110 if (cls != 0)
111 {
112 mid = env->GetMethodID(cls, "successful", "()Z");
113
114 if (mid != 0)
115 {
116 jboolean answerJ = env->CallBooleanMethod(obj, mid);
117
118 answer = answerJ != 0;
119 }
120 }
121
122 return answer;
123 }
124
125 void IMAPHandler::setJarPath()
126 {
127 const string jarFile = "IMAPHandler.jar";
128 string jarFilePath = jarFile;
129
130 ifstream fin(jarFilePath.c_str());
131 if (!fin.is_open())
132 {
133 unsigned end = program.rfind(PATH_SEPARATOR);
134 if (end != string::npos)
135 {
136 string path = program.substr(0, end);
137
138 jarFilePath = path + PATH_SEPARATOR + jarFile;
139
140 fin.open(jarFilePath.c_str());
141 if (!fin.is_open())
142 {
143 cerr << program << ": Could not find required file: "
144 << jarFile << "\n";
145 exit(1);
146 }
147 else
148 {
149 fin.close();
150 }
151 }
152 else
153 {
154 string envPath = getenv("PATH");
155
156 if (debug) cerr << "envPath = " << envPath << "\n";
157
158 unsigned begin = 0;
159 do
160 {
161 unsigned end = envPath.find(ENV_SEPARATOR, begin);
162 string path = envPath.substr(begin, end);
163
164 if (debug) cerr << "path = " << path << "\n";
165
166 jarFilePath = path + PATH_SEPARATOR + jarFile;
167
168 fin.open(jarFilePath.c_str());
169 if (fin.is_open())
170 {
171 fin.close();
172 break;
173 }
174
175 begin = end != string::npos ? end + 1 : string::npos;
176 }
177 while (begin < envPath.length());
178
179 if (begin == string::npos)
180 {
181 cerr << program << ": Could not find required file: "
182 << jarFile << "\n";
183 exit(1);
184 }
185 }
186 }
187 else
188 {
189 fin.close();
190 }
191
192 string jarPath = "-Djava.class.path=" + jarFilePath;
193
194 this->jarPath = new char[jarPath.length()];
195 strcpy(this->jarPath, jarPath.c_str());
196 }
197
198 string IMAPHandler::imap(char* imap)
199 {
200 string result;
201
202 if (cls != 0)
203 {
204 mid = env->GetMethodID(cls, imap, "()Ljava/lang/String;");
205
206 if (mid != 0)
207 {
208 jstring resultJ = (jstring)env->CallObjectMethod(obj, mid);
209
210 const char* resultC = env->GetStringUTFChars(resultJ, 0);
211 result = resultC;
212 env->ReleaseStringUTFChars(resultJ, resultC);
213 }
214 }
215
216 return result;
217 }
218
219 string IMAPHandler::imap(char* imap, const string& args)
220 {
221 string result;
222
223 if (cls != 0)
224 {
225 mid = env->GetMethodID(cls, imap,
226 "(Ljava/lang/String;)Ljava/lang/String;");
227
228 if (mid != 0)
229 {
230 jstring argsJ = env->NewStringUTF(args.c_str());
231
232 jstring resultJ = (jstring)env->CallObjectMethod(obj, mid, argsJ);
233
234 const char* resultC = env->GetStringUTFChars(resultJ, 0);
235 result = resultC;
236 env->ReleaseStringUTFChars(resultJ, resultC);
237 }
238 }
239
240 return result;
241 }
242
243 string IMAPHandler::imap(char* imap, const string& args, const string& message)
244 {
245 string result;
246
247 if (cls != 0)
248 {
249 mid = env->GetMethodID(cls, imap,
250 "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
251
252 if (mid != 0)
253 {
254 jstring argsJ = env->NewStringUTF(args.c_str());
255 jstring messageJ = env->NewStringUTF(message.c_str());
256
257 jstring resultJ = (jstring)env->CallObjectMethod(obj, mid, argsJ,
258 messageJ);
259
260 const char* resultC = env->GetStringUTFChars(resultJ, 0);
261 result = resultC;
262 env->ReleaseStringUTFChars(resultJ, resultC);
263 }
264 }
265
266 return result;
267 }