imaptransport.c 15.7 KB
Newer Older
1 2 3 4
/*
 * IMAP Transport
 *
 * Copyright 2008 Hans Leidekker for CodeWeavers
5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
 */

#define COBJMACROS

#include <stdarg.h>
#include <stdio.h>

#include "windef.h"
#include "winbase.h"
#include "winnt.h"
#include "winuser.h"
#include "objbase.h"
#include "mimeole.h"
#include "wine/debug.h"

#include "inetcomm_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);

typedef struct
{
    InternetTransport InetTransport;
    ULONG refs;
} IMAPTransport;

static HRESULT WINAPI IMAPTransport_QueryInterface(IIMAPTransport *iface, REFIID riid, void **ppv)
{
    TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);

    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IInternetTransport) ||
        IsEqualIID(riid, &IID_IIMAPTransport))
    {
        *ppv = iface;
        IUnknown_AddRef(iface);
        return S_OK;
    }
    *ppv = NULL;
    FIXME("no interface for %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI IMAPTransport_AddRef(IIMAPTransport *iface)
{
    IMAPTransport *This = (IMAPTransport *)iface;
    return InterlockedIncrement((LONG *)&This->refs);
}

static ULONG WINAPI IMAPTransport_Release(IIMAPTransport *iface)
{
    IMAPTransport *This = (IMAPTransport *)iface;
    ULONG refs = InterlockedDecrement((LONG *)&This->refs);
    if (!refs)
    {
        TRACE("destroying %p\n", This);
        if (This->InetTransport.Status != IXP_DISCONNECTED)
            InternetTransport_DropConnection(&This->InetTransport);
        if (This->InetTransport.pCallback) ITransportCallback_Release(This->InetTransport.pCallback);
        HeapFree(GetProcessHeap(), 0, This);
    }
    return refs;
}

static HRESULT WINAPI IMAPTransport_GetServerInfo(IIMAPTransport *iface,
    LPINETSERVER pInetServer)
{
    IMAPTransport *This = (IMAPTransport *)iface;

    TRACE("(%p)\n", pInetServer);
    return InternetTransport_GetServerInfo(&This->InetTransport, pInetServer);
}

static IXPTYPE WINAPI IMAPTransport_GetIXPType(IIMAPTransport *iface)
{
    TRACE("()\n");
    return IXP_IMAP;
}

static HRESULT WINAPI IMAPTransport_IsState(IIMAPTransport *iface,
    IXPISSTATE isstate)
{
    FIXME("(%d): stub\n", isstate);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_InetServerFromAccount(
    IIMAPTransport *iface, IImnAccount *pAccount, LPINETSERVER pInetServer)
{
    IMAPTransport *This = (IMAPTransport *)iface;

    TRACE("(%p, %p)\n", pAccount, pInetServer);
    return InternetTransport_InetServerFromAccount(&This->InetTransport, pAccount, pInetServer);
}

static HRESULT WINAPI IMAPTransport_Connect(IIMAPTransport *iface,
    LPINETSERVER pInetServer, boolean fAuthenticate, boolean fCommandLogging)
{
    IMAPTransport *This = (IMAPTransport *)iface;
    HRESULT hr;

    TRACE("(%p, %s, %s)\n", pInetServer, fAuthenticate ? "TRUE" : "FALSE", fCommandLogging ? "TRUE" : "FALSE");

    hr = InternetTransport_Connect(&This->InetTransport, pInetServer, fAuthenticate, fCommandLogging);
    return hr;
}

static HRESULT WINAPI IMAPTransport_HandsOffCallback(IIMAPTransport *iface)
{
    IMAPTransport *This = (IMAPTransport *)iface;

    TRACE("()\n");
    return InternetTransport_HandsOffCallback(&This->InetTransport);
}

static HRESULT WINAPI IMAPTransport_Disconnect(IIMAPTransport *iface)
{
    FIXME("(): stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_DropConnection(IIMAPTransport *iface)
{
    IMAPTransport *This = (IMAPTransport *)iface;

    TRACE("()\n");
    return InternetTransport_DropConnection(&This->InetTransport);
}

static HRESULT WINAPI IMAPTransport_GetStatus(IIMAPTransport *iface,
    IXPSTATUS *pCurrentStatus)
{
    IMAPTransport *This = (IMAPTransport *)iface;

    TRACE("()\n");
    return InternetTransport_GetStatus(&This->InetTransport, pCurrentStatus);
}

static HRESULT WINAPI IMAPTransport_InitNew(IIMAPTransport *iface,
    LPSTR pszLogFilePath, IIMAPCallback *pCallback)
{
    IMAPTransport *This = (IMAPTransport *)iface;

    TRACE("(%s, %p)\n", debugstr_a(pszLogFilePath), pCallback);

    if (!pCallback)
        return E_INVALIDARG;

    if (pszLogFilePath)
        FIXME("not using log file of %s, use Wine debug logging instead\n",
            debugstr_a(pszLogFilePath));

    IIMAPCallback_AddRef(pCallback);
    This->InetTransport.pCallback = (ITransportCallback *)pCallback;
    This->InetTransport.fInitialised = TRUE;

    return S_OK;
}

static HRESULT WINAPI IMAPTransport_NewIRangeList(IIMAPTransport *iface,
    IRangeList **pprlNewRangeList)
{
    FIXME("(%p): stub\n", pprlNewRangeList);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Capability(IIMAPTransport *iface,
    DWORD *pdwCapabilityFlags)
{
    FIXME("(%p): stub\n", pdwCapabilityFlags);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Select(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %s): stub\n", wParam, lParam, pCBHandler, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Examine(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %s): stub\n", wParam, lParam, pCBHandler, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Create(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %s): stub\n", wParam, lParam, pCBHandler, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Delete(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %s): stub\n", wParam, lParam, pCBHandler, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Rename(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, LPSTR lpszMailboxName, LPSTR lpszNewMailboxName)
{
    FIXME("(%ld, %ld, %p, %s, %s): stub\n", wParam, lParam, pCBHandler,
          debugstr_a(lpszMailboxName), debugstr_a(lpszNewMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Subscribe(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %s): stub\n", wParam, lParam, pCBHandler, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Unsubscribe(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %s): stub\n", wParam, lParam, pCBHandler, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_List(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, LPSTR lpszMailboxNameReference, LPSTR lpszMailboxNamePattern)
{
    FIXME("(%ld, %ld, %p, %s, %s): stub\n", wParam, lParam, pCBHandler,
          debugstr_a(lpszMailboxNameReference), debugstr_a(lpszMailboxNamePattern));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Lsub(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, LPSTR lpszMailboxNameReference, LPSTR lpszMailboxNamePattern)
{
    FIXME("(%ld, %ld, %p, %s, %s): stub\n", wParam, lParam, pCBHandler,
          debugstr_a(lpszMailboxNameReference), debugstr_a(lpszMailboxNamePattern));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Append(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszMailboxName,
    LPSTR lpszMessageFlags, FILETIME ftMessageDateTime, LPSTREAM lpstmMessageToSave)
{
    FIXME("(%ld, %ld, %p, %s, %s, %p): stub\n", wParam, lParam, pCBHandler,
          debugstr_a(lpszMailboxName), debugstr_a(lpszMessageFlags), lpstmMessageToSave);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Close(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler)
{
    FIXME("(%ld, %ld, %p): stub\n", wParam, lParam, pCBHandler);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Expunge(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler)
{
    FIXME("(%ld, %ld, %p): stub\n", wParam, lParam, pCBHandler);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Search(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler, LPSTR lpszSearchCriteria,
    boolean bReturnUIDs, IRangeList *pMsgRange, boolean bUIDRangeList)
{
    FIXME("(%ld, %ld, %p, %s, %d, %p, %d): stub\n", wParam, lParam, pCBHandler,
          debugstr_a(lpszSearchCriteria), bReturnUIDs, pMsgRange, bUIDRangeList);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Fetch(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, IRangeList *pMsgRange, boolean bUIDMsgRange, LPSTR lpszFetchArgs)
{
    FIXME("(%ld, %ld, %p, %p, %d, %s): stub\n", wParam, lParam, pCBHandler, pMsgRange,
          bUIDMsgRange, debugstr_a(lpszFetchArgs));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Store(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, IRangeList *pMsgRange, boolean bUIDRangeList, LPSTR lpszStoreArgs)
{
    FIXME("(%ld, %ld, %p, %p, %d, %s): stub\n", wParam, lParam, pCBHandler, pMsgRange,
          bUIDRangeList, debugstr_a(lpszStoreArgs));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Copy(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, IRangeList *pMsgRange, boolean bUIDRangeList, LPSTR lpszMailboxName)
{
    FIXME("(%ld, %ld, %p, %p, %d, %s): stub\n", wParam, lParam, pCBHandler, pMsgRange,
          bUIDRangeList, debugstr_a(lpszMailboxName));
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Noop(IIMAPTransport *iface,
    WPARAM wParam, LPARAM lParam, IIMAPCallback *pCBHandler)
{
    FIXME("(%ld, %ld, %p): stub\n", wParam, lParam, pCBHandler);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_ResizeMsgSeqNumTable(IIMAPTransport *iface, DWORD dwSizeOfMbox)
{
    FIXME("(%u): stub\n", dwSizeOfMbox);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_UpdateSeqNumToUID(IIMAPTransport *iface,
    DWORD dwMsgSeqNum, DWORD dwUID)
{
    FIXME("(%u, %u): stub\n", dwMsgSeqNum, dwUID);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_RemoveSequenceNum(IIMAPTransport *iface, DWORD dwDeletedMsgSeqNum)
{
    FIXME("(%u): stub\n", dwDeletedMsgSeqNum);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_MsgSeqNumToUID(IIMAPTransport *iface, DWORD dwMsgSeqNum,
    DWORD *pdwUID)
{
    FIXME("(%u, %p): stub\n", dwMsgSeqNum, pdwUID);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_GetMsgSeqNumToUIDArray(IIMAPTransport *iface,
    DWORD **ppdwMsgSeqNumToUIDArray, DWORD *pdwNumberOfElements)
{
    FIXME("(%p, %p): stub\n", ppdwMsgSeqNumToUIDArray, pdwNumberOfElements);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_GetHighestMsgSeqNum(IIMAPTransport *iface, DWORD *pdwHighestMSN)
{
    FIXME("(%p): stub\n", pdwHighestMSN);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_ResetMsgSeqNumToUID(IIMAPTransport *iface)
{
    FIXME("(): stub\n");
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_SetDefaultCBHandler(IIMAPTransport *iface, IIMAPCallback *pCBHandler)
{
    FIXME("(%p): stub\n", pCBHandler);
    return E_NOTIMPL;
}

static HRESULT WINAPI IMAPTransport_Status(IIMAPTransport *iface, WPARAM wParam, LPARAM lParam,
    IIMAPCallback *pCBHandler, LPSTR pszMailboxName, LPSTR pszStatusCmdArgs)
{
    FIXME("(%ld, %ld, %p, %s, %s): stub\n", wParam, lParam, pCBHandler,
          debugstr_a(pszMailboxName), debugstr_a(pszStatusCmdArgs));
    return E_NOTIMPL;
}

static const IIMAPTransportVtbl IMAPTransportVtbl =
{
    IMAPTransport_QueryInterface,
    IMAPTransport_AddRef,
    IMAPTransport_Release,
    IMAPTransport_GetServerInfo,
    IMAPTransport_GetIXPType,
    IMAPTransport_IsState,
    IMAPTransport_InetServerFromAccount,
    IMAPTransport_Connect,
    IMAPTransport_HandsOffCallback,
    IMAPTransport_Disconnect,
    IMAPTransport_DropConnection,
    IMAPTransport_GetStatus,
    IMAPTransport_InitNew,
    IMAPTransport_NewIRangeList,
    IMAPTransport_Capability,
    IMAPTransport_Select,
    IMAPTransport_Examine,
    IMAPTransport_Create,
    IMAPTransport_Delete,
    IMAPTransport_Rename,
    IMAPTransport_Subscribe,
    IMAPTransport_Unsubscribe,
    IMAPTransport_List,
    IMAPTransport_Lsub,
    IMAPTransport_Append,
    IMAPTransport_Close,
    IMAPTransport_Expunge,
    IMAPTransport_Search,
    IMAPTransport_Fetch,
    IMAPTransport_Store,
    IMAPTransport_Copy,
    IMAPTransport_Noop,
    IMAPTransport_ResizeMsgSeqNumTable,
    IMAPTransport_UpdateSeqNumToUID,
    IMAPTransport_RemoveSequenceNum,
    IMAPTransport_MsgSeqNumToUID,
    IMAPTransport_GetMsgSeqNumToUIDArray,
    IMAPTransport_GetHighestMsgSeqNum,
    IMAPTransport_ResetMsgSeqNumToUID,
    IMAPTransport_SetDefaultCBHandler,
    IMAPTransport_Status
};

HRESULT WINAPI CreateIMAPTransport(IIMAPTransport **ppTransport)
{
    HRESULT hr;
    IMAPTransport *This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
    if (!This)
        return E_OUTOFMEMORY;

    This->InetTransport.u.vtblIMAP = &IMAPTransportVtbl;
    This->refs = 0;
    hr = InternetTransport_Init(&This->InetTransport);
    if (FAILED(hr))
    {
        HeapFree(GetProcessHeap(), 0, This);
        return hr;
    }

    *ppTransport = (IIMAPTransport *)&This->InetTransport.u.vtblIMAP;
    IIMAPTransport_AddRef(*ppTransport);

    return S_OK;
}

static HRESULT WINAPI IMAPTransportCF_QueryInterface(LPCLASSFACTORY iface,
    REFIID riid, LPVOID *ppv)
{
    *ppv = NULL;
    if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IClassFactory))
    {
        *ppv = iface;
        IUnknown_AddRef(iface);
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI IMAPTransportCF_AddRef(LPCLASSFACTORY iface)
{
    return 2; /* non-heap based object */
}

static ULONG WINAPI IMAPTransportCF_Release(LPCLASSFACTORY iface)
{
    return 1; /* non-heap based object */
}

static HRESULT WINAPI IMAPTransportCF_CreateInstance(LPCLASSFACTORY iface,
    LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv)
{
    HRESULT hr;
    IIMAPTransport *pImapTransport;

    TRACE("(%p, %s, %p)\n", pUnk, debugstr_guid(riid), ppv);

    *ppv = NULL;

    if (pUnk)
        return CLASS_E_NOAGGREGATION;

    hr = CreateIMAPTransport(&pImapTransport);
    if (FAILED(hr))
        return hr;

    hr = IIMAPTransport_QueryInterface(pImapTransport, riid, ppv);
    IIMAPTransport_Release(pImapTransport);

    return hr;
}

static HRESULT WINAPI IMAPTransportCF_LockServer(LPCLASSFACTORY iface, BOOL fLock)
{
    FIXME("(%d), stub!\n",fLock);
    return S_OK;
}

static const IClassFactoryVtbl IMAPTransportCFVtbl =
{
    IMAPTransportCF_QueryInterface,
    IMAPTransportCF_AddRef,
    IMAPTransportCF_Release,
    IMAPTransportCF_CreateInstance,
    IMAPTransportCF_LockServer
};
static const IClassFactoryVtbl *IMAPTransportCF = &IMAPTransportCFVtbl;

HRESULT IMAPTransportCF_Create(REFIID riid, LPVOID *ppv)
{
    return IClassFactory_QueryInterface((IClassFactory *)&IMAPTransportCF, riid, ppv);
}