Back to index

lightning-sunbird  0.9+nobinonly
Functions
nsImapUtils.cpp File Reference
#include "msgCore.h"
#include "nsImapUtils.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsIServiceManager.h"
#include "prsystem.h"
#include "nsEscape.h"
#include "nsIFileSpec.h"
#include "prprf.h"
#include "nsNetCID.h"
#include "nsIMsgAccountManager.h"
#include "nsIMsgIncomingServer.h"
#include "nsIImapIncomingServer.h"
#include "nsMsgBaseCID.h"
#include "nsImapCore.h"
#include "nsMsgUtils.h"
#include "nsImapFlagAndUidState.h"
#include "nsISupportsObsolete.h"

Go to the source code of this file.

Functions

nsresult nsImapURI2Path (const char *rootURI, const char *uriStr, nsFileSpec &pathResult)
nsresult nsImapURI2FullName (const char *rootURI, const char *hostname, const char *uriStr, char **name)
nsresult nsParseImapMessageURI (const char *uri, nsCString &folderURI, PRUint32 *key, char **part)
nsresult nsBuildImapMessageURI (const char *baseURI, PRUint32 key, nsCString &uri)
nsresult nsCreateImapBaseMessageURI (const char *baseURI, char **baseMessageURI)
void AllocateImapUidString (PRUint32 *msgUids, PRUint32 &msgCount, nsImapFlagAndUidState *flagState, nsCString &returnString)
void ParseUidString (const char *uidString, nsMsgKeyArray &keys)
void AppendUid (nsCString &msgIds, PRUint32 uid)

Function Documentation

void AllocateImapUidString ( PRUint32 msgUids,
PRUint32 msgCount,
nsImapFlagAndUidState flagState,
nsCString returnString 
)

Definition at line 361 of file nsImapUtils.cpp.

{
  PRUint32 startSequence = (msgCount > 0) ? msgUids[0] : 0xFFFFFFFF;
  PRUint32 curSequenceEnd = startSequence;
  PRUint32 total = msgCount;
  PRInt32  curFlagStateIndex = -1;

  for (PRUint32 keyIndex=0; keyIndex < total; keyIndex++)
  {
    PRUint32 curKey = msgUids[keyIndex];
    PRUint32 nextKey = (keyIndex + 1 < total) ? msgUids[keyIndex + 1] : 0xFFFFFFFF;
    PRBool lastKey = (nextKey == 0xFFFFFFFF);

    if (lastKey)
      curSequenceEnd = curKey;

    if (!lastKey)
    {
      if (nextKey == curSequenceEnd + 1)
      {
        curSequenceEnd = nextKey;
        curFlagStateIndex++;
        continue;
      }
      if (flagState)
      {
        if (curFlagStateIndex == -1)
        {
          PRBool foundIt;
          flagState->GetMessageFlagsFromUID(curSequenceEnd, &foundIt, &curFlagStateIndex);
          NS_ASSERTION(foundIt, "flag state missing key");
        }
        curFlagStateIndex++;
        PRUint32 nextUidInFlagState;
        nsresult rv = flagState->GetUidOfMessage(curFlagStateIndex, &nextUidInFlagState);
        if (NS_SUCCEEDED(rv) && nextUidInFlagState == nextKey)
        {
          curSequenceEnd = nextKey;
          continue;
        }
      }
    }
    if (curSequenceEnd > startSequence)
    {
      returnString.AppendInt(startSequence);
      returnString += ':';
      returnString.AppendInt(curSequenceEnd);
      startSequence = nextKey;
      curSequenceEnd = startSequence;
      curFlagStateIndex = -1;
    }
    else
    {
      startSequence = nextKey;
      curSequenceEnd = startSequence;
      returnString.AppendInt(msgUids[keyIndex]);
      curFlagStateIndex = -1;
    }
    // check if we've generated too long a string - if there's no flag state,
    // it means we just need to go ahead and generate a too long string
    // because the calling code won't handle breaking up the strings.
    if (flagState && returnString.Length() > 950) 
    {
      msgCount = keyIndex;
      break;
    }
    // If we are not the last item then we need to add the comma 
    // but it's important we do it here, after the length check 
    if (!lastKey) 
      returnString += ','; 
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void AppendUid ( nsCString msgIds,
PRUint32  uid 
)

Definition at line 465 of file nsImapUtils.cpp.

{
  char buf[20];
  PR_snprintf(buf, sizeof(buf), "%u", uid);
  msgIds.Append(buf);
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsBuildImapMessageURI ( const char *  baseURI,
PRUint32  key,
nsCString uri 
)

Definition at line 224 of file nsImapUtils.cpp.

{
  
  uri.Append(baseURI);
  uri.Append('#');
  uri.AppendInt(key);
  return NS_OK;
}
nsresult nsCreateImapBaseMessageURI ( const char *  baseURI,
char **  baseMessageURI 
)

Definition at line 233 of file nsImapUtils.cpp.

{
  if(!baseMessageURI)
    return NS_ERROR_NULL_POINTER;
  
  nsCAutoString tailURI(baseURI);
  
  // chop off mailbox:/
  if (tailURI.Find(kImapRootURI) == 0)
    tailURI.Cut(0, PL_strlen(kImapRootURI));
  
  nsCAutoString baseURIStr(kImapMessageRootURI);
  baseURIStr += tailURI;
  
  *baseMessageURI = ToNewCString(baseURIStr);
  if(!*baseMessageURI)
    return NS_ERROR_OUT_OF_MEMORY;
  
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsImapURI2FullName ( const char *  rootURI,
const char *  hostname,
const char *  uriStr,
char **  name 
)

Definition at line 160 of file nsImapUtils.cpp.

{
    nsAutoString uri; uri.AssignWithConversion(uriStr);
    nsAutoString fullName;
    if (uri.Find(rootURI) != 0)
      return NS_ERROR_FAILURE;
    uri.Right(fullName, uri.Length() - strlen(rootURI));
    uri = fullName;
    PRInt32 hostStart = uri.Find(hostname);
    if (hostStart <= 0) 
      return NS_ERROR_FAILURE;
    uri.Right(fullName, uri.Length() - hostStart);
    uri = fullName;
    PRInt32 hostEnd = uri.FindChar('/');
    if (hostEnd <= 0) 
      return NS_ERROR_FAILURE;
    uri.Right(fullName, uri.Length() - hostEnd - 1);
    if (fullName.IsEmpty())
      return NS_ERROR_FAILURE;
    *name = ToNewCString(fullName);
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsImapURI2Path ( const char *  rootURI,
const char *  uriStr,
nsFileSpec pathResult 
)

Definition at line 61 of file nsImapUtils.cpp.

{
  nsresult rv;
  
  nsAutoString sbdSep;
  nsCOMPtr<nsIURL> url;
 
  rv = nsGetMailFolderSeparator(sbdSep);
  if (NS_FAILED(rv)) 
    return rv;
  
  url = do_CreateInstance(NS_STANDARDURL_CONTRACTID, &rv);
  if (NS_FAILED(rv)) return rv;
  
  nsCAutoString uri(uriStr);
  if (uri.Find(rootURI) != 0)     // if doesn't start with rootURI
    return NS_ERROR_FAILURE;
  
  if ((PL_strcmp(rootURI, kImapRootURI) != 0) &&
    (PL_strcmp(rootURI, kImapMessageRootURI) != 0)) 
  {
    pathResult = nsnull;
    rv = NS_ERROR_FAILURE; 
  }
  
  // Set our url to the string given
  rv = url->SetSpec(nsDependentCString(uriStr));
  if (NS_FAILED(rv)) return rv;

  // Set the folder to the url path
  nsCAutoString folder;
  rv = url->GetPath(folder);
  // can't have leading '/' in path
  if (folder.CharAt(0) == '/')
    folder.Cut(0, 1);
  // Now find the server from the URL
  nsCOMPtr<nsIMsgIncomingServer> server;
  nsCOMPtr<nsIMsgAccountManager> accountManager = 
    do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
  if(NS_FAILED(rv)) return rv;
  
  rv = accountManager->FindServerByURI(url, PR_FALSE,
    getter_AddRefs(server));

  if (NS_FAILED(rv)) return rv;
  
  if (server) 
  {
    nsCOMPtr<nsIFileSpec> localPath;
    rv = server->GetLocalPath(getter_AddRefs(localPath));
    if (NS_FAILED(rv)) return rv;
    
    rv = localPath->GetFileSpec(&pathResult);
    if (NS_FAILED(rv)) return rv;
    
    // This forces the creation of the parent server directory
    // so that we don't get imapservername.sbd instead
    // when the host directory has been deleted. See bug 210683
    nsFileSpec tempPath(pathResult.GetNativePathCString(), PR_TRUE); 
    pathResult.CreateDirectory();
  }
  
  if (NS_FAILED(rv)) 
  {
    pathResult = nsnull;
    return rv;
  }
  
  if (!folder.IsEmpty())
  {
    nsCAutoString parentName = folder;
    nsCAutoString leafName = folder;
    PRInt32 dirEnd = parentName.FindChar('/');
    
    // FIXME : This would break with multibyte encodings such as 
    // Shift_JIS, Big5 because '0x5c' in the second byte of a multibyte
    // character would be mistaken for '/'. 
    while(dirEnd > 0)
    {
      parentName.Right(leafName, parentName.Length() - dirEnd -1);
      parentName.Truncate(dirEnd);
      NS_MsgHashIfNecessary(parentName);
      parentName.AppendWithConversion(sbdSep);
      pathResult += parentName.get();
      // this fixes a strange purify warning.
      parentName = leafName.get();
      dirEnd = parentName.FindChar('/');
    }
    if (!leafName.IsEmpty()) 
    {
      NS_MsgHashIfNecessary(leafName);
      pathResult += leafName.get();
    }
  }
  
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsParseImapMessageURI ( const char *  uri,
nsCString folderURI,
PRUint32 key,
char **  part 
)

Definition at line 185 of file nsImapUtils.cpp.

{
  if(!key)
    return NS_ERROR_NULL_POINTER;
  
  nsCAutoString uriStr(uri);
  PRInt32 keySeparator = uriStr.RFindChar('#');
  if(keySeparator != -1)
  {
    PRInt32 keyEndSeparator = uriStr.FindCharInSet("/?&", 
      keySeparator); 
    nsAutoString folderPath;
    uriStr.Left(folderURI, keySeparator);
    folderURI.Cut(4, 8);    // cut out the _message part of imap-message:
    nsCAutoString keyStr;
    if (keyEndSeparator != -1)
      uriStr.Mid(keyStr, keySeparator+1, 
      keyEndSeparator-(keySeparator+1));
    else
      uriStr.Right(keyStr, uriStr.Length() - (keySeparator + 1));
    PRInt32 errorCode;
    *key = keyStr.ToInteger(&errorCode);
    
    if (part && keyEndSeparator != -1)
    {
      PRInt32 partPos = uriStr.Find("part=", PR_FALSE, keyEndSeparator);
      if (partPos != -1)
      {
        nsCString partSubStr;
        uriStr.Right(partSubStr, uriStr.Length() - keyEndSeparator);
        *part = ToNewCString(partSubStr);
        
      }
    }
  }
  return NS_OK;
  
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ParseUidString ( const char *  uidString,
nsMsgKeyArray &  keys 
)

Definition at line 435 of file nsImapUtils.cpp.

{
  // This is in the form <id>,<id>, or <id1>:<id2>
  char curChar = *uidString;
  PRBool isRange = PR_FALSE;
  int32 curToken;
  int32 saveStartToken=0;

  for (const char *curCharPtr = uidString; curChar && *curCharPtr;)
  {
    const char *currentKeyToken = curCharPtr;
    curChar = *curCharPtr;
    while (curChar != ':' && curChar != ',' && curChar != '\0')
      curChar = *curCharPtr++;

    // we don't need to null terminate currentKeyToken because atoi 
    // stops at non-numeric chars.
    curToken = atoi(currentKeyToken); 
    if (isRange)
    {
      while (saveStartToken < curToken)
        keys.Add(saveStartToken++);
    }
    keys.Add(curToken);
    isRange = (curChar == ':');
    if (isRange)
      saveStartToken = curToken + 1;
  }
}

Here is the caller graph for this function: