1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/* SCCS Id: @(#)mapimail.c 3.4 $Date: 2002/07/24 08:25:20 $ */
/* Copyright (c) Michael Allison, 1997 */
/* NetHack may be freely redistributed. See license for details. */
#ifdef MAX_BODY_SIZE
#undef MAX_BODY_SIZE
#define MAX_BODY_SIZE 2048 /* largest body held in ram in bytes */
#endif
#include "hack.h"
# ifdef LAN_MAIL
#include "win32api.h"
#include <mapi.h>
#define MAPI_MSGID_SIZE 512 /* as recommended */
#define MAPI_LIB_FAIL 1
#define MAPI_FUNC_FAIL 2
HANDLE hLibrary; /* Handle for MAPI32.DLL */
LHANDLE MAPISession; /* Handle for MAPI session */
char MAPIMessageID[MAPI_MSGID_SIZE]; /* Msg ID from provider */
lpMapiMessage MAPIMessage; /* Ptr to message from prov */
struct lan_mail_struct received_msg; /* store received msg here */
#ifdef MAPI_VERBOSE
FILE *dbgfile;
#define MAPIDEBUGFILENAME "mapidebug.log"
#endif
/*
* Prototypes for functions in this file
* (Not in normal NetHack style, but its doubtful that any
* of the code in here is at all portable between platforms)
*/
static long __stdcall start_mailthread(LPVOID ThreadParam);
static boolean MAPI_mail_check(char *);
static boolean MAPI_mail_fetch(char *);
static void MAPI_mail_finish(void);
static int MAPI_mail_context(int *);
static boolean MAPI_mail_init(char *);
static int InitMAPI(void);
static int DeInitMAPI(void);
static void MAPI_mail_abort(unsigned long);
/*
* Declare the function pointers used to access MAPI routines
* from the MAPI32.DLL
*/
LPMAPILOGON fpMAPILogon;
LPMAPILOGOFF fpMAPILogoff;
LPMAPIFINDNEXT fpMAPIFindNext;
LPMAPIREADMAIL fpMAPIReadMail;
LPMAPIFREEBUFFER fpMAPIFreeBuffer;
/*
* Data requiring synchronized access between the
* two thread contexts contained in this file
* (nethack thread and the MAPI-mail thread)
*/
HANDLE mailthread = 0; /* handle for the mail-thread */
unsigned nhmailthread_ID; /* thread ID for mail-thread */
unsigned long nhmail_param; /* value passed to mail-thread */
long mailthread_continue = 0; /* nh -> mapi thread: shut down! */
long mailthread_stopping = 0; /* mapi -> nh thread: MAPI is ill! */
/*
* Data updated by the NetHack thread only
* but read by the MAPI-mail thread.
*
*/
char MAPI_username[120];
boolean debugmapi;
/*
* Data updated by the MAPI-mail thread only
* but read by the NetHack thread.
*/
long mail_fetched = 0;
/*
* Data used only by the MAPI-mail thread.
*/
long mailpasses; /* counts the FindNext calls issued to MAPI */
char msgID[80]; /* message ID of msg under manipulation */
/*===============================================================
* NetHack thread routines
*
* These routines run in the context of the main NetHack thread.
* They are used to provide an interface between the NetHack
* LAN_MAIL code and the MAPI-thread code.
*
* These routines are referenced by shared code in
* sys/share/nhlan.c and they are prototyped in include/extern.h
*===============================================================
*/
boolean mail_check()
{
if (!mailthread_stopping) {
if (mail_fetched > 0)
return TRUE;
} else lan_mail_terminate();
return FALSE;
}
boolean mail_fetch(msg)
struct lan_mail_struct *msg;
{
/* shouldn't need to check mailthread_stopping here
as the data should be valid anyway */
*msg = received_msg;
iflags.lan_mail_fetched = TRUE;
/* clear the marker now so new messages can arrive */
InterlockedDecrement(&mail_fetched);
/* check to see if the MAPI-mail thread is saying "stop" */
if (mailthread_stopping) lan_mail_terminate();
return TRUE;
}
void mail_finish()
{
InterlockedDecrement(&mailthread_continue);
}
void mail_init(uname)
char *uname;
{
/* This routine invokes the _beginthreadex()
* run-time library call to start the execution
* of the MAPI-mail thread. It also performs
* few other preparatory tasks.
*/
if (mailthread_continue) {
/* Impossible - something is really messed up */
/* We better do some sanity checking */
/* Is there a valid thread handle and ID? */
if (mailthread && nhmailthread_ID) {
/* TODO: check 'em via WIN32 call */
/* if valid, do something about them */
/* if not, clear them */
}
}
mailthread = 0;
nhmailthread_ID = 0;
mailthread_continue = 0; /* no interlock needed yet */
mailthread_stopping = 0; /* no interlock needed yet */
#ifdef MAPI_VERBOSE
if (getenv("DEBUG_MAPI")) debugmapi = TRUE;
#endif
if (uname)
strcpy(MAPI_username, uname);
else {
#ifdef MAPI_VERBOSE
if (debugmapi) dbgfile = fopen(MAPIDEBUGFILENAME,"w");
if (dbgfile) {
fprintf(dbgfile,
"mapi initialization bypassed because uname not available\n");
fclose(dbgfile);
}
#endif
return;
}
mailthread = (HANDLE)_beginthreadex(
(void *)0,
(unsigned)0,
start_mailthread,
(void *)&nhmail_param,
(unsigned)0, (unsigned *)&nhmailthread_ID);
#if 0
/* TODO: check for failure of the thread. For now nethack
* doesn't care
*/
if (!mailthread) {
}
#endif
}
/*===============================================================
* MAPI-mail thread routines
*
* These routines run in the context of their own
* MAPI-mail thread. They were placed into their own
* thread, because MAPI calls can sometimes (often?) take
* a horribly long time to return (minutes even).
*
* Each of the routines below are referenced only by other
* routines below, with the exception of start_mailthread(),
* of course, which is started by a run-time library call
* issued from the main NetHack thread.
*===============================================================
*/
/*
* start_mailthread() is the entry point of the MAPI-mail thread.
*
*/
static long __stdcall start_mailthread(LPVOID ThreadParam)
{
char *lu = MAPI_username;
if(MAPI_mail_init(lu)) {
InterlockedIncrement(&mailthread_continue);
while (mailthread_continue) {
mailpasses++;
if (MAPI_mail_check(msgID)) {
if (MAPI_mail_fetch(msgID)) {
/* getting here means success */
}
}
Sleep(MAILTHREADFREQ);
}
#ifdef MAPI_VERBOSE
if (debugmapi && !mailthread_continue) {
fprintf(dbgfile,
"MAPI-thread detected mailthread_continue change.\n");
fprintf(dbgfile,
"NetHack has requested that the MAPI-thread cease.\n");
}
#endif
}
return 0;
}
static int
MAPI_mail_context(mcount)
int *mcount;
{
unsigned long status;
char tmpID[80];
int count = 0;
tmpID[0] = '\0';
MAPIMessageID[0] = '\0';
/* Get the ID of the first unread message */
status = fpMAPIFindNext(MAPISession, 0, 0, 0,
MAPI_UNREAD_ONLY | MAPI_GUARANTEE_FIFO,
0, tmpID);
/* Now loop through them all until we have no more */
while (status == SUCCESS_SUCCESS) {
strcpy(MAPIMessageID, tmpID);
count++;
status = fpMAPIFindNext(MAPISession, 0,
0, MAPIMessageID,
MAPI_UNREAD_ONLY | MAPI_GUARANTEE_FIFO,
0, tmpID);
}
if (status == MAPI_E_NO_MESSAGES) {
/* context is now at last message */
if (mcount) *mcount = count;
return SUCCESS_SUCCESS;
}
return status;
}
static boolean
MAPI_mail_check(mID)
char *mID;
{
unsigned long status;
char tmpID[80];
tmpID[0] = '\0';
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "MAPI_mail_check() ");
#endif
if (mail_fetched) {
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "returning FALSE (buffer occupied)\n");
#endif
return FALSE; /* buffer occupied, don't bother */
}
/* Get the ID of the next unread message if there is one */
status = fpMAPIFindNext(MAPISession, 0, 0, MAPIMessageID,
MAPI_UNREAD_ONLY | MAPI_GUARANTEE_FIFO,
0, tmpID);
if (status == SUCCESS_SUCCESS) {
strcpy(mID, tmpID);
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "returning TRUE\n");
#endif
return TRUE;
}
if (status == MAPI_E_NO_MESSAGES) {
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "returning FALSE\n");
#endif
return FALSE;
}
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,"Error, check_newmail() status: %d\n", status);
MAPI_mail_abort(status);
#endif
return FALSE;
}
static boolean
MAPI_mail_fetch(mID)
char *mID;
{
unsigned long status;
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "MAPI_mail_fetch() ");
#endif
/*
* Update context right away so we don't loop if there
* was a problem getting the message
*/
strcpy(MAPIMessageID, mID);
if (mail_fetched) {
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "returning FALSE (buffer occupied)\n");
#endif
return FALSE; /* buffer occupied */
}
status = fpMAPIReadMail(MAPISession, 0, mID,
MAPI_SUPPRESS_ATTACH | MAPI_PEEK,
0, &MAPIMessage);
if (status == SUCCESS_SUCCESS) {
strncpy(received_msg.subject,
MAPIMessage->lpszSubject,
sizeof(received_msg.subject) - 1);
if((MAPIMessage->lpOriginator->lpszName != (char *)0)
&& MAPIMessage->lpOriginator->lpszName[0] != '\0')
strncpy(received_msg.sender,
MAPIMessage->lpOriginator->lpszName,
sizeof(received_msg.sender) - 1);
else
strncpy(received_msg.sender,
MAPIMessage->lpOriginator->lpszAddress,
sizeof(received_msg.sender) - 1);
strncpy(received_msg.body,
MAPIMessage->lpszNoteText,MAX_BODY_SIZE - 1);
received_msg.body[MAX_BODY_SIZE - 1] = '\0';
received_msg.body_in_ram = TRUE;
status = fpMAPIFreeBuffer(MAPIMessage);
InterlockedIncrement(&mail_fetched);
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile, "returning TRUE\n");
#endif
return TRUE;
}
#ifdef MAPI_VERBOSE
else
if (debugmapi) fprintf(dbgfile,"MAPIRead failed, status = %d\n", status);
if (debugmapi) fprintf(dbgfile, "returning FALSE (failed)\n");
#endif
return FALSE;
}
static void
MAPI_mail_finish()
{
InterlockedIncrement(&mailthread_stopping);
(void) fpMAPILogoff(MAPISession,0,0,0);
(void) DeInitMAPI();
#ifdef MAPI_VERBOSE
if (debugmapi) fclose(dbgfile);
#endif
(void) _endthreadex(0);
}
static void
MAPI_mail_abort(reason)
unsigned long reason;
{
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,
"Terminating MAPI-thread due to error %d.\n", reason);
#endif
MAPI_mail_finish();
}
static boolean
MAPI_mail_init(uname)
char *uname;
{
unsigned long status;
int count = 0;
#ifdef MAPI_VERBOSE
if (debugmapi) dbgfile = fopen(MAPIDEBUGFILENAME,"w");
if (debugmapi) fprintf(dbgfile,"Hello %s, NetHack is initializing MAPI.\n",
uname);
#endif
status = InitMAPI();
if (status) {
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,"Error initializing MAPI %d\n", status);
#endif
return FALSE;
}
status = fpMAPILogon(0,uname,0L,0L,0L,(LPLHANDLE)&MAPISession);
if (status != SUCCESS_SUCCESS) {
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,"Status of MAPI logon is %d\n", status);
#endif
return FALSE;
}
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,
"Stage 1 of MAPI initialization successful.\n");
if (debugmapi) fprintf(dbgfile,"MAPI Session handle: %d\n", MAPISession);
#endif
status = MAPI_mail_context(&count);
if (status == SUCCESS_SUCCESS) {
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,
"Stage 2 of MAPI initialization successful.\n");
if (debugmapi) fprintf(dbgfile,"Detected %d old unread messages.\n",
count);
#endif
return TRUE;
}
#ifdef MAPI_VERBOSE
if (debugmapi) fprintf(dbgfile,
"Error, status of MAPI_mail_context() is %d.\n",
status);
if (debugmapi) fprintf(dbgfile,
"Dismantling MAPI interface and cleaning up.\n");
#endif
MAPI_mail_finish();
return FALSE;
}
int InitMAPI()
{
if (!(hLibrary = LoadLibrary("MAPI32.DLL")))
return MAPI_LIB_FAIL;
if ((fpMAPILogon = (LPMAPILOGON)GetProcAddress(hLibrary,"MAPILogon")) == NULL)
return MAPI_FUNC_FAIL;
if ((fpMAPILogoff = (LPMAPILOGOFF)GetProcAddress(hLibrary,"MAPILogoff")) == NULL)
return MAPI_FUNC_FAIL;
if ((fpMAPIFindNext= (LPMAPIFINDNEXT)GetProcAddress(hLibrary,"MAPIFindNext")) == NULL)
return MAPI_FUNC_FAIL;
if ((fpMAPIReadMail= (LPMAPIREADMAIL)GetProcAddress(hLibrary,"MAPIReadMail")) == NULL)
return MAPI_FUNC_FAIL;
if ((fpMAPIFindNext= (LPMAPIFINDNEXT)GetProcAddress(hLibrary,"MAPIFindNext")) == NULL)
return MAPI_FUNC_FAIL;
if ((fpMAPIFreeBuffer= (LPMAPIFREEBUFFER)GetProcAddress(hLibrary,"MAPIFreeBuffer")) == NULL)
return MAPI_FUNC_FAIL;
#ifdef MAPI_VERBOSE
if (debugmapi) {
fprintf(dbgfile,"Entry Points:\n");
fprintf(dbgfile,"MAPILogon = %p\n",fpMAPILogon);
fprintf(dbgfile,"MAPILogoff = %p\n",fpMAPILogoff);
fprintf(dbgfile,"MAPIFindNext = %p\n",fpMAPIFindNext);
fprintf(dbgfile,"MAPIFreeBuffer = %p\n",fpMAPIReadMail);
fprintf(dbgfile,"MAPIReadMail = %p\n",fpMAPIFreeBuffer);
}
#endif
return 0;
}
int DeInitMAPI()
{
fpMAPILogon = NULL;
fpMAPILogoff= NULL;
fpMAPIFindNext= NULL;
fpMAPIReadMail= NULL;
fpMAPIFreeBuffer = NULL;
FreeLibrary(hLibrary);
return 0;
}
#endif /*LAN_MAIL*/
|