Created
November 28, 2018 14:12
-
-
Save maldevel/a5e0155240732c1e8b1637ce567302a8 to your computer and use it in GitHub Desktop.
Generate message id for gmail.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static std::string _generateMessageID(const char *sender, SIZE_T senderLength) { | |
GUID pGuiId; | |
WCHAR sGuiId[64] = { 0 }; | |
WCHAR sTrimId[64] = { 0 }; | |
std::string messageid; | |
int strFromGuiSize = 0; | |
char *senderCopy = 0; | |
int domainSize = 50; | |
char domain[50] = { 0 }; | |
char *context = 0; | |
char *tmp = 0; | |
char *sTrimIdA = 0; | |
size_t messageIDSize = 0; | |
char *messageID; | |
//copy sender email | |
if ((senderCopy = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, senderLength + 1)) == NULL) { | |
return ""; | |
} | |
if (strncpy_s(senderCopy, senderLength + 1, sender, _TRUNCATE) != 0) { | |
HeapFree(GetProcessHeap(), 0, senderCopy); | |
senderCopy = NULL; | |
return ""; | |
} | |
//get first token | |
if (helper::next_token(senderCopy, "@", &context) == NULL) { | |
HeapFree(GetProcessHeap(), 0, senderCopy); | |
senderCopy = NULL; | |
return ""; | |
} | |
//Get email domain | |
if ((tmp = helper::next_token(NULL, "@", &context)) == NULL) { | |
HeapFree(GetProcessHeap(), 0, senderCopy); | |
senderCopy = NULL; | |
return ""; | |
} | |
if (strncpy_s(domain, domainSize, tmp, _TRUNCATE) != 0) { | |
HeapFree(GetProcessHeap(), 0, senderCopy); | |
senderCopy = NULL; | |
return ""; | |
} | |
HeapFree(GetProcessHeap(), 0, senderCopy); | |
senderCopy = NULL; | |
//Create a GUID, a unique 128-bit integer. | |
if (CoCreateGuid(&pGuiId) != S_OK) { | |
return ""; | |
} | |
//Convert a globally unique identifier (GUID) into a string of printable characters. | |
if ((strFromGuiSize = StringFromGUID2(pGuiId, sGuiId, _countof(sGuiId))) == 0) { | |
return ""; | |
} | |
//Remove { and } from generated GUID | |
if (wmemmove_s(sTrimId, 64, sGuiId + 1, strFromGuiSize - 3) != 0) { | |
return ""; | |
} | |
sTrimId[strFromGuiSize - 3] = '\0'; | |
//Convert GUID to ascii | |
sTrimIdA = helper::Wchar_To_Char(sTrimId, 64); | |
if (sTrimIdA == NULL) { | |
return ""; | |
} | |
//messageID will store the final message-id value | |
messageIDSize = strlen(sTrimIdA) + 1 + strlen(domain) + 1; | |
if ((messageID = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, messageIDSize)) == NULL) { | |
HeapFree(GetProcessHeap(), 0, sTrimIdA); | |
sTrimIdA = NULL; | |
return ""; | |
} | |
//copy trimmed guid to messageid e.g. xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx | |
if (_snprintf_s(messageID, messageIDSize, _TRUNCATE, "%s", sTrimIdA) == -1) { | |
HeapFree(GetProcessHeap(), 0, sTrimIdA); | |
sTrimIdA = NULL; | |
HeapFree(GetProcessHeap(), 0, messageID); | |
messageID = NULL; | |
return ""; | |
} | |
//concat @ e.g. xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@ | |
if (_snprintf_s(messageID + strlen(sTrimIdA), messageIDSize - strlen(sTrimIdA), _TRUNCATE, "%s", "@") == -1) { | |
HeapFree(GetProcessHeap(), 0, sTrimIdA); | |
sTrimIdA = NULL; | |
HeapFree(GetProcessHeap(), 0, messageID); | |
messageID = NULL; | |
return ""; | |
} | |
//concat domain e.g. xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx@example.com | |
if (_snprintf_s(messageID + strlen(sTrimIdA) + 1, messageIDSize - strlen(sTrimIdA) - 1, _TRUNCATE, "%s", domain) == -1) { | |
HeapFree(GetProcessHeap(), 0, sTrimIdA); | |
sTrimIdA = NULL; | |
HeapFree(GetProcessHeap(), 0, messageID); | |
messageID = NULL; | |
return ""; | |
} | |
HeapFree(GetProcessHeap(), 0, sTrimIdA); | |
sTrimIdA = NULL; | |
messageid = std::string(messageID); | |
HeapFree(GetProcessHeap(), 0, messageID); | |
messageID = NULL; | |
return messageid; | |
} | |
static std::string _buildMessageID(const char *format, const char *from) { | |
size_t _size = 0; | |
char *result; | |
std::string msgid; | |
if ((msgid = _generateMessageID(from, strlen(from))) == "") { | |
return ""; | |
} | |
_size = strlen(format) + msgid.length(); | |
if ((result = (char*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, _size + 1)) == NULL) { | |
return ""; | |
} | |
if (_snprintf_s(result, _size + 1, _TRUNCATE, format, msgid.c_str()) == -1) { | |
HeapFree(GetProcessHeap(), 0, result); | |
result = NULL; | |
return ""; | |
} | |
msgid = std::string(result); | |
HeapFree(GetProcessHeap(), 0, result); | |
result = NULL; | |
return msgid; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment