return (*target)->BufUsed;;
}
+
+
+static void AddRecipient(StrBuf *Target,
+ StrBuf *UserName,
+ StrBuf *EmailAddress,
+ StrBuf *EncBuf)
+{
+ int QuoteMe = 0;
+
+ if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
+ if (strchr(ChrPtr(UserName), ',') != NULL) QuoteMe = 1;
+
+ if (QuoteMe) StrBufAppendBufPlain(Target, HKEY("\""), 0);
+ StrBufRFC2047encode(&EncBuf, UserName);
+ StrBufAppendBuf(Target, EncBuf, 0);
+ if (QuoteMe) StrBufAppendBufPlain(Target, HKEY("\" "), 0);
+ else StrBufAppendBufPlain(Target, HKEY(" "), 0);
+
+ if (StrLength(EmailAddress) > 0){
+ StrBufAppendBufPlain(Target, HKEY("<"), 0);
+ StrBufAppendBuf(Target, EmailAddress, 0); /* TODO: what about IDN???? */
+ StrBufAppendBufPlain(Target, HKEY(">"), 0);
+ }
+}
+
+
+/**
+ * \brief QP encode parts of an email TO/CC/BCC vector, and strip/filter invalid parts
+ * \param Recp Source list of email recipients
+ * \param UserName Temporary buffer for internal use; Please provide valid buffer.
+ * \param EmailAddress Temporary buffer for internal use; Please provide valid buffer.
+ * \param EncBuf Temporary buffer for internal use; Please provide valid buffer.
+ * \returns encoded & sanitized buffer with the contents of Recp; Caller owns this memory.
+ */
+StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
+ StrBuf *UserName,
+ StrBuf *EmailAddress,
+ StrBuf *EncBuf)
+{
+ StrBuf *Target;
+ int need_to_encode;
+
+ const char *pch, *pche;
+ const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
+
+ if ((Recp == NULL) || (StrLength(Recp) == 0))
+ return NULL;
+
+ need_to_encode = 0;
+ pch = ChrPtr(Recp);
+ pche = pch + StrLength(Recp);
+
+ if (!CheckEncode(pch, -1, pche))
+ return NewStrBufDup(Recp);
+
+ Target = NewStrBufPlain(NULL, StrLength(Recp));
+
+ while ((pch != NULL) && (pch < pche))
+ {
+ int ColonOk = 0;
+
+ while (isspace(*pch)) pch++;
+ UserStart = UserEnd = EmailStart = EmailEnd = NULL;
+
+ if ((*pch == '"') || (*pch == '\'')) {
+ UserStart = pch + 1;
+
+ UserEnd = strchr(UserStart, *pch);
+ if (UserEnd == NULL)
+ break; ///TODO: Userfeedback??
+ EmailStart = UserEnd + 1;
+ while (isspace(*EmailStart))
+ EmailStart++;
+ if (UserEnd == UserStart) {
+ UserStart = UserEnd = NULL;
+ }
+
+ if (*EmailStart == '<') {
+ EmailStart++;
+ EmailEnd = strchr(EmailStart, '>');
+ if (EmailEnd == NULL)
+ EmailEnd = strchr(EmailStart, ',');
+
+ }
+ else {
+ EmailEnd = strchr(EmailStart, ',');
+ }
+ if (EmailEnd == NULL)
+ EmailEnd = pche;
+ pch = EmailEnd + 1;
+ ColonOk = 1;
+ }
+ else {
+ int gt = 0;
+ UserStart = pch;
+ EmailEnd = strchr(UserStart, ',');
+ if (EmailEnd == NULL) {
+ EmailEnd = strchr(pch, '>');
+ pch = NULL;
+ if (EmailEnd != NULL) {
+ gt = 1;
+ EmailEnd --;
+ }
+ else {
+ EmailEnd = pche;
+ }
+ }
+ else {
+
+ pch = EmailEnd + 1;
+ while ((EmailEnd > UserStart) &&
+ ((*EmailEnd == ',') ||
+ (*EmailEnd == '>') ||
+ (isspace(*EmailEnd))))
+ {
+ if (*EmailEnd == '>')
+ gt = 1;
+ EmailEnd--;
+ }
+ if (EmailEnd == UserStart)
+ break;
+ }
+ if (gt) {
+ EmailStart = strchr(UserStart, '<');
+ if ((EmailStart == NULL) || (EmailStart > EmailEnd))
+ break;
+ UserEnd = EmailStart - 1;
+ EmailStart ++;
+ if (UserStart >= UserEnd)
+ UserStart = UserEnd = NULL;
+ At = strchr(EmailStart, '@');
+ }
+ else { /* this is a local recipient... no domain, just a realname */
+ At = strchr(EmailStart, '@');
+ if (At == NULL) {
+ UserEnd = EmailEnd;
+ EmailEnd = NULL;
+ }
+ else {
+ EmailStart = UserStart;
+ UserStart = NULL;
+ }
+ }
+ }
+
+
+ if (UserStart != NULL)
+ StrBufPlain(UserName, UserStart, UserEnd - UserStart);
+ else
+ FlushStrBuf(UserName);
+ if (EmailStart != NULL)
+ StrBufPlain(EmailAddress, EmailStart, EmailEnd - EmailStart);
+ else
+ FlushStrBuf(EmailAddress);
+
+ AddRecipient(Target, UserName, EmailAddress, EncBuf);
+
+
+
+ if (*pch == ',')
+ pch ++;
+ while (isspace(*pch))
+ pch ++;
+ }
+ return Target;
+}
+
+
/**
* @ingroup StrBuf
* @brief replaces all occurances of 'search' by 'replace'
int fromstdin = 0;
-
+int parse_email = 0;
static void TestRevalidateStrBuf(StrBuf *Buf)
{
CU_ASSERT(strlen(ChrPtr(Buf)) == StrLength(Buf));
}
+static void TestEncodeEmail(void)
+{
+ StrBuf *Target;
+ StrBuf *Source;
+ StrBuf *UserName = NewStrBuf();
+ StrBuf *EmailAddress = NewStrBuf();
+ StrBuf *EncBuf = NewStrBuf();
+
+ Source = NewStrBuf();
+
+// Source = NewStrBufPlain(HKEY("Art Cancro <ajc@uncensored.citadel.org>, Art Cancro <ajc@uncensored.citadel.org>"));
+
+ Source = NewStrBufPlain(HKEY("\"Alexandra Weiz, Restless GmbH\" <alexandra.weiz@boblbee.de>, \"NetIN\" <editor@netin.co.il>, \" יריב ברקאי, מולטימדי\" <info@immembed.com>"));
+ Target = StrBufSanitizeEmailRecipientVector(
+ Source,
+ UserName,
+ EmailAddress,
+ EncBuf
+ );
+
+ TestRevalidateStrBuf(Target);
+ printf("the source:>%s<\n", ChrPtr(Source));
+ printf("the target:>%s<\n", ChrPtr(Target));
+ FreeStrBuf(&Target);
+ FreeStrBuf(&UserName);
+ FreeStrBuf(&EmailAddress);
+ FreeStrBuf(&EncBuf);
+
+ FreeStrBuf(&Source);
+}
+
+static void TestEncodeEmailSTDIN(void)
+{
+ int fdin = 0;// STDIN
+ const char *Err;
+ StrBuf *Target;
+ StrBuf *Source;
+ StrBuf *UserName = NewStrBuf();
+ StrBuf *EmailAddress = NewStrBuf();
+ StrBuf *EncBuf = NewStrBuf();
+
+ Source = NewStrBuf();
+
+ while (fdin == 0) {
+
+ StrBufTCP_read_line(Source, &fdin, 0, &Err);
+ printf("the source:>%s<\n", ChrPtr(Source));
+ Target = StrBufSanitizeEmailRecipientVector(
+ Source,
+ UserName,
+ EmailAddress,
+ EncBuf
+ );
+
+ TestRevalidateStrBuf(Target);
+ printf("the target:>%s<\n", ChrPtr(Target));
+ FreeStrBuf(&Target);
+ }
+ FreeStrBuf(&UserName);
+ FreeStrBuf(&EmailAddress);
+ FreeStrBuf(&EncBuf);
+
+ FreeStrBuf(&Source);
+}
+
+
static void AddStrBufSimlpeTests(void)
{
CU_pTest pTest = NULL;
pGroup = CU_add_suite("TestStringBufConversions", NULL, NULL);
- if (!fromstdin) {
- pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
- pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
- pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
- pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
+ if (!parse_email) {
+ if (!fromstdin) {
+ pTest = CU_add_test(pGroup, "testRFC822Decode", TestRFC822Decode);
+ pTest = CU_add_test(pGroup, "testRFC822Decode1", TestRFC822Decode);
+ pTest = CU_add_test(pGroup, "testRFC822Decode2", TestRFC822Decode);
+ pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822Decode);
+ }
+ else
+ pTest = CU_add_test(pGroup, "testRFC822DecodeSTDIN", TestRFC822DecodeStdin);
+ }
+ else {
+ if (!fromstdin) {
+ pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmail);
+ }
+ else
+ pTest = CU_add_test(pGroup, "TestParseEmailSTDIN", TestEncodeEmailSTDIN);
}
- else
- pTest = CU_add_test(pGroup, "testRFC822Decode3", TestRFC822DecodeStdin);
}
{
int a;
- while ((a = getopt(argc, argv, "i")) != EOF)
+ while ((a = getopt(argc, argv, "@i")) != EOF)
switch (a) {
+ case '@':
+ parse_email = 1;
+ break;
case 'i':
fromstdin = 1;
if (CU_TRUE == Run) {
//CU_console_run_tests();
- printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
+ printf("\nTests completed with return value %d.\n", CU_basic_run_tests());
- ///CU_automated_run_tests();
+ ///CU_automated_run_tests();
}
CU_cleanup_registry();