Reads a vCard written in the standard 2.0 or 3.0 text formats. This is the primary (standard) vCard format used by most applications.
Inheritance: Thought.vCards.vCardReader
示例#1
0
        /// <summary>
        ///     Writes a card, then reads it back and compares fields.
        /// </summary>
        public static void CycleStandard21(vCard card)
        {

            if (card == null)
                throw new ArgumentNullException("cycle");

            // Create a memory stream to hold the contents of the card.

            MemoryStream stream = new MemoryStream();

            StreamWriter textWriter = new StreamWriter(stream);

            // Create a standard vCard writer and export the
            // card data to the stream.

            vCardStandardWriter writer = new vCardStandardWriter();
            writer.Write(card, textWriter);
            textWriter.Flush();

            // Reset the stream (back to its beginning), then
            // create a stream reader capable of reading text
            // lines from the stream.

            stream.Seek(0, SeekOrigin.Begin);
            StreamReader streamReader = new StreamReader(stream);

            vCardStandardReader standardReader = new vCardStandardReader();
            vCard reloaded = standardReader.Read(streamReader);

            Equals(card, reloaded);

        }
        public void ReadProperty_String_Name_Value()
        {

            // This function tests the parsing function
            // against a basic string like NAME:VALUE.

            vCardStandardReader reader = new vCardStandardReader();

            vCardProperty property = reader.ReadProperty(
                TestName + ":" + TestValue);

            Assert.AreEqual(
                TestName,
                property.Name);

            Assert.AreEqual(
                TestValue,
                property.Value,
                "The parsed value is incorrect.");

            Assert.IsEmpty(
                property.Subproperties,
                "The Subproperties collection should be empty.");

        }
        public void ReadProperty_String_Whitespace()
        {
            vCardStandardReader reader = new vCardStandardReader();
            reader.ReadProperty("  ");

            Assert.AreEqual(
                1,
                reader.Warnings.Count,
                "A string with only whitespace should have caused a warning.");

        }
        public void ReadProperty_String_SingleColon()
        {
            vCardStandardReader reader = new vCardStandardReader();
            reader.ReadProperty(":");

            Assert.AreEqual(
                1,
                reader.Warnings.Count,
                "A single colon should have caused a warning.");

        }
        public void ReadProperty_String_QuotedPrintable()
        {

            const string encodedValue =
                "LABEL;" +
                "HOME;" +
                "ENCODING=QUOTED-PRINTABLE:" +
                "129 15th Street #3=0D=0A" +
                "Minneapolis, MN 55403=0D=0A" +
                "United States of America";

            const string decodedValue =
                "129 15th Street #3\r\n" +
                "Minneapolis, MN 55403\r\n" +
                "United States of America";

            vCardStandardReader reader =
                new vCardStandardReader();

            // Read the property string.  It should
            // be decoded by the reader.

            vCardProperty property =
                reader.ReadProperty(encodedValue);

            Assert.AreEqual(
                "LABEL",
                property.Name,
                "The name of the property should be LABEL.");

            Assert.IsTrue(
                property.Subproperties.Contains("HOME"),
                "The property should have a subproperty called HOME.");

            // Now for the big test.  The loaded property
            // value should be decoded.  It should not have the
            // quoted-printable escape sequences.

            Assert.AreEqual(
                decodedValue,
                property.ToString());

        }
 public void ReadProperty_String_NullParameter()
 {
     vCardStandardReader reader = new vCardStandardReader();
     reader.ReadProperty((string)null);
 }
示例#7
0
        /// <summary>
        ///     Loads a new instance of the <see cref="vCard"/> class
        ///     from a text file.
        /// </summary>
        /// <param name="path">
        ///     The path to a text file containing vCard data in
        ///     any recognized vCard format.
        /// </param>
        public vCard(string path)
            : this()
        {
            using (StreamReader streamReader = new StreamReader(path))
            {
                vCardReader reader = new vCardStandardReader();
                reader.ReadInto(this, streamReader);
            }

            //String example = "BEGIN:VCARD\nVERSION:3.0\nN:;Saad;;;\nFN:Saad\nTEL;TYPE=CELL:418-271-3874\nTEL;TYPE=HOME:418-524-7721\nEND:VCARD";
            // using (MemoryStream s = GenerateStreamFromString(example))
            //{
            //    vCardReader reader = new vCardStandardReader();
            //    TextReader streamReader = new StreamReader(s, Encoding.Default);
            //    reader.ReadInto(this, streamReader);
            //}
        }
        public void ReadProperty_String_Name_Subproperty_Subvalue_Value()
        {

            vCardStandardReader reader =
                new vCardStandardReader();

            vCardProperty property =
                reader.ReadProperty("TEL;TYPE=WORK:800-929-5805");

            Assert.AreEqual(
                "TEL",
                property.Name,
                "The name of the property should be TEL");

            Assert.AreEqual(
                1,
                property.Subproperties.Count,
                "There should be exactly one subproperty.");

            Assert.AreEqual(
                "TYPE",
                property.Subproperties[0].Name,
                "The name of the subproperty should be TYPE.");

            Assert.AreEqual(
                "WORK",
                property.Subproperties[0].Value,
                "The value of the subproperty should be WORK.");

            Assert.AreEqual(
                "800-929-5805",
                property.Value,
                "The value of the property is not correct.");

        }
 public void ReadProperty_String_EmptyParameter()
 {
     vCardStandardReader reader = new vCardStandardReader();
     reader.ReadProperty(string.Empty);
 }
示例#10
0
        /// <summary>
        ///     Loads a new instance of the <see cref="vCard"/> class
        ///     from a text file.
        /// </summary>
        /// <param name="path">
        ///     The path to a text file containing vCard data in
        ///     any recognized vCard format.
        /// </param>
        public vCard(string path)
            : this()
        {
            using (StreamReader streamReader = new StreamReader(path))
            {
                vCardReader reader = new vCardStandardReader();
                reader.ReadInto(this, streamReader);
            }

        }
示例#11
0
        /// <summary>
        ///     Loads a new instance of the <see cref="vCard"/> class
        ///     from a text reader.
        /// </summary>
        /// <param name="input">
        ///     An initialized text reader.
        /// </param>
        public vCard(TextReader input)
            : this()
        {

            vCardReader reader = new vCardStandardReader();
            reader.ReadInto(this, input);

        }
        public void SamplevCardReadAndWriteTestWithEmailTypeFormat()
        {

            vCard card = new vCard();

            card.EmailAddresses.Add(new vCardEmailAddress() { Address = "*****@*****.**", EmailType = vCardEmailAddressType.Internet, IsPreferred = true, ItemType = ItemType.WORK });


            card.UniqueId = Guid.NewGuid().ToString("N");

            string text = card.ToString();

            vCardStandardWriter writer = new vCardStandardWriter();

            using (StringWriter sw = new StringWriter())
            {

                writer.Write(card, sw);

                sw.Flush();
                text = sw.ToString();
                sw.Close();
            }


            Assert.IsNotNull(text);


            vCardStandardReader reader = new vCardStandardReader();

            using (StringReader sr = new StringReader(text))
            {

                vCard cardFromReader = reader.Read(sr);

                Assert.AreEqual(1, cardFromReader.EmailAddresses.Count);

                var email = cardFromReader.EmailAddresses.First();
                Assert.AreEqual(true, email.IsPreferred);
                Assert.AreEqual(ItemType.WORK, email.ItemType);
                Assert.AreEqual(vCardEmailAddressType.Internet, email.EmailType);
                Assert.AreEqual("*****@*****.**", email.Address);
            }

        }
        public void ShouldReadvCardWithAllCBFieldsFilledOutFromiPhoneNAB()
        {
            string text = @"BEGIN:VCARD
VERSION:3.0
PRODID:-//Apple Inc.//iOS 6.0.1//EN
N:iOS;Nic;;;
FN:Nic iOS
ORG:Ibm;
TITLE:Sales Guy
item1.EMAIL;type=INTERNET;type=pref:[email protected]
EMAIL;type=INTERNET;type=WORK:[email protected]
EMAIL;type=INTERNET;type=WORK:[email protected]
EMAIL;type=INTERNET;type=HOME:[email protected]
EMAIL;type=INTERNET;type=HOME:[email protected]
TEL;type=CELL;type=VOICE;type=pref:(202) 333-4555
TEL;type=IPHONE;type=CELL;type=VOICE:(202) 333-4444
TEL;type=HOME;type=VOICE:(333) 222-2222
TEL;type=WORK;type=VOICE:(809) 555-6666 x444
TEL;type=MAIN:(609) 888-7777
TEL;type=HOME;type=FAX:(555) 444-4443
TEL;type=WORK;type=FAX:33322222222
item2.TEL:(999) 777-7999
item2.X-ABLabel:personal
item3.ADR;type=HOME;type=pref:;;8230 Boone Blvd;Vinna;VA;22182;United States
item3.X-ABADR:us
item4.URL;type=pref:http://facebook.com/max.solender
item4.X-ABLabel:Profile
item5.URL:www.ibm.com
item5.X-ABLabel:_$!<HomePage>!$_
item6.X-MSN:msnname
item6.X-ABLabel:_$!<Other>!$_
item7.X-AIM:aolname
item7.X-ABLabel:_$!<Other>!$_
item8.X-YAHOO:yahooname
item8.X-ABLabel:_$!<Other>!$_
item9.X-JABBER:jabbername
item9.X-ABLabel:_$!<Other>!$_
IMPP;X-SERVICE-TYPE=Skype;type=HOME;type=pref:skype:skypeusernameee
IMPP;X-SERVICE-TYPE=Skype;type=WORK:skype:worksyokeusername
item10.IMPP;X-SERVICE-TYPE=MSN:msnim:msnname
item10.X-ABLabel:_$!<Other>!$_
item11.IMPP;X-SERVICE-TYPE=AIM:aim:aolname
item11.X-ABLabel:_$!<Other>!$_
item12.IMPP;X-SERVICE-TYPE=GoogleTalk:xmpp:gtalkname
item12.X-ABLabel:_$!<Other>!$_
item13.IMPP;X-SERVICE-TYPE=Yahoo:ymsgr:yahooname
item13.X-ABLabel:_$!<Other>!$_
item14.IMPP;X-SERVICE-TYPE=Facebook:xmpp:fbchatname
item14.X-ABLabel:_$!<Other>!$_
item15.IMPP;X-SERVICE-TYPE=Jabber:xmpp:jabbername
item15.X-ABLabel:_$!<Other>!$_
X-SOCIALPROFILE;type=linkedin;x-user=nicatlinkedin:http://www.linkedin.com/in/nicatlinkedin
X-SOCIALPROFILE;type=twitter;x-user=tiffanystone:http://twitter.com/tiffanystone
X-SOCIALPROFILE;type=facebook;x-user=tiffatfacebook:http://www.facebook.com/tiffatfacebook
X-SOCIALPROFILE;type=twitter;x-user=gregabedard:http://twitter.com/gregabedard
END:VCARD";

            vCardStandardReader reader = new vCardStandardReader();
            using (StringReader sr = new StringReader(text))
            {

                vCard c = reader.Read(sr);

                Assert.AreEqual(5, c.EmailAddresses.Count);

                CheckEmail(c.EmailAddresses, "*****@*****.**", ItemType.UNSPECIFIED, vCardEmailAddressType.Internet, true);
                CheckEmail(c.EmailAddresses, "*****@*****.**", ItemType.WORK, vCardEmailAddressType.Internet, false);
                CheckEmail(c.EmailAddresses, "*****@*****.**", ItemType.WORK, vCardEmailAddressType.Internet, false);
                CheckEmail(c.EmailAddresses, "*****@*****.**", ItemType.HOME, vCardEmailAddressType.Internet, false);
                CheckEmail(c.EmailAddresses, "*****@*****.**", ItemType.HOME, vCardEmailAddressType.Internet, false);




                Assert.AreEqual("Sales Guy", c.Title);
                Assert.AreEqual("Ibm", c.Organization);
                Assert.AreEqual("Nic", c.GivenName);
                Assert.AreEqual("iOS", c.FamilyName);

    

                Assert.AreEqual(8, c.Phones.Count);

                CheckPhone(c.Phones, "(202) 333-4555", vCardPhoneTypes.Preferred | vCardPhoneTypes.Cellular | vCardPhoneTypes.Voice, true);
        public void SamlevCardReadAndWriteTestWithContentFromWikipedia()
        {

            string text = @"BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;VALUE=URL;TYPE=GIF:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:(404) 555-1212
ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
EMAIL;TYPE=PREF,INTERNET:[email protected]
REV:2008-04-24T19:52:43Z
END:VCARD";

            vCardStandardReader reader = new vCardStandardReader();

            using (StringReader sr = new StringReader(text))
            {

                vCard cardFromReader = reader.Read(sr);

                Assert.AreEqual(1, cardFromReader.EmailAddresses.Count);

                var email = cardFromReader.EmailAddresses.First();
                Assert.AreEqual(true, email.IsPreferred);
                Assert.AreEqual(ItemType.UNSPECIFIED, email.ItemType);
                Assert.AreEqual(vCardEmailAddressType.Internet, email.EmailType);
                Assert.AreEqual("*****@*****.**", email.Address);


                Assert.AreEqual("Shrimp Man", cardFromReader.Title);
                Assert.AreEqual("Forrest", cardFromReader.GivenName);
                Assert.AreEqual("Gump", cardFromReader.FamilyName);


                Assert.AreEqual(2, cardFromReader.Phones.Count);

                var phone404 = cardFromReader.Phones.FirstOrDefault(x => x.FullNumber == "(404) 555-1212");
                var phone111 = cardFromReader.Phones.FirstOrDefault(x => x.FullNumber == "(111) 555-1212");

                Assert.IsNotNull(phone111);
                Assert.IsNotNull(phone404);

                Assert.IsTrue(phone111.IsWork);
                Assert.IsTrue(phone111.IsVoice);

                Assert.IsTrue(phone404.IsVoice);
                Assert.IsTrue(phone404.IsHome);


            }


            //need to add social Profile types as property to the vCard object and reader/writer
            //need to try and add a bunch of properties in my ipad NAB and email me a .vcf file
            //then generate via my parser and try and import the VCF into my ipad
            //look at creating nuGet package for deploying the bin / dll


        }
        public void ReadProperty_String_Name_Subproperties_Value()
        {

            // This function tests the parser against a property
            // string with two subproperties.

            vCardStandardReader reader =
                new vCardStandardReader();

            vCardProperty property =
                reader.ReadProperty("NAME;SUB1;SUB2:VALUE");

            Assert.AreEqual(
                "NAME",
                property.Name,
                "The Name is incorrect.");

            Assert.AreEqual(
                2,
                property.Subproperties.Count,
                "The Subproperties collection has an incorrect number of items.");

            Assert.AreEqual(
                "SUB1",
                property.Subproperties[0].Name,
                "The Subproperty[0] value is incorrect.");

            Assert.AreEqual(
                "SUB2",
                property.Subproperties[1].Name,
                "The Subproperty[1] value is incorrect.");

            Assert.AreEqual(
                "VALUE",
                property.Value,
                "The parsed value is incorrect.");

        }
        public void ReadProperty_String_Name_Subproperty_Value()
        {

            vCardStandardReader reader =
                new vCardStandardReader();

            vCardProperty property =
                reader.ReadProperty("NAME;SUB:VALUE");

            Assert.AreEqual(
                "NAME",
                property.Name,
                "The Name is incorrect.");

            Assert.AreEqual(
                1,
                property.Subproperties.Count,
                "The Subproperties collection has an incorrect number of items.");

            Assert.AreEqual(
                "SUB",
                property.Subproperties[0].Name,
                "The Subproperty value is incorrect.");

            Assert.AreEqual(
                "VALUE",
                property.Value,
                "The parsed value is incorrect.");

        }
        public void ReadProperty_String_MissingName()
        {
            vCardStandardReader reader = new vCardStandardReader();
            reader.ReadProperty(":VALUE");

            Assert.AreEqual(
                1,
                reader.Warnings.Count,
                "A missing name should have caused a warning.");

        }
        public void ReadProperty_String_MissingColon()
        {

            vCardStandardReader reader = new vCardStandardReader();
            reader.ReadProperty("TEL 911");

            Assert.AreEqual(
                1,
                reader.Warnings.Count,
                "A missing colon should have caused a warning.");

        }
示例#19
0
        public async Task<ActionResult> MailWebhook()
        {
            // Initialize a mandrill client for sending emails
            var mandrill = new Mandrill.MandrillApi(this.mandrillApiKey);
            string sender = null;

            try
            {
                // Get the JSON payload
                string validJson = HttpContext.Request.Form["mandrill_events"].Replace("mandrill_events=", string.Empty);

                if (string.IsNullOrWhiteSpace(validJson))
                {
                    return new HttpStatusCodeResult(400);
                }

                var webhookEvents = JsonConvert.DeserializeObject<List<WebHookEvent>>(validJson);
                if (webhookEvents == null)
                {
                    return new HttpStatusCodeResult(400);
                }

                var results = new List<string>();

                foreach (var webhookEvent in webhookEvents)
                {
                    sender = webhookEvent.Msg.FromEmail;
                    var attachments = webhookEvent.Msg.Attachments;

                    // Verify the security by checking authorized senders
                    if (webhookEvent.Msg.Spf.Result != "pass" && webhookEvent.Msg.Dkim.Valid != true)
                    {
                        throw new Exception("Sender not authorized: SPF / DKIM Check failed.");
                    }

                    // Create an instance of the CRM service that acts in the name of the sender of the email
                    using (var service = new CrmVcardUpdateService(sender))
                    {
                        // Loop through the attachments
                        foreach (var attachment in attachments)
                        {
                            // Get the VCards
                            var filename = this.DecodeBase64Names(attachment.Key);

                            if ((attachment.Value.Type != "text/vcard" && attachment.Value.Type != "text/x-vcard") || !filename.EndsWith(".vcf"))
                            {
                                results.Add(string.Format("{0}: not imported (mime-type: {1})", filename, attachment.Value.Type));
                                continue;
                            }

                            var bytes = attachment.Value.Base64 ? Convert.FromBase64String(attachment.Value.Content) : Encoding.UTF8.GetBytes(attachment.Value.Content);
                            var memoryStream = new MemoryStream(bytes);
                            var vcardReader = new vCardStandardReader();
                            using (var streamreader = new StreamReader(memoryStream))
                            {
                                var vcard = vcardReader.Read(streamreader);

                                // Import the VCards into the CRM
                                var result = service.UpdateContactWithVcard(vcard, filename);
                                results.Add(result);
                            }
                        }
                    }
                }

                // Sends a mail with the results
                var email = new EmailMessage
                {
                    FromEmail = this.emailSendingAddress,
                    To = new List<EmailAddress> { new EmailAddress(sender ?? this.errorEmailRecipient) },
                    Subject = "VCard import finished",
                    Text = "Results:\t\n\t\n" + string.Join("\t\n", results),
                };

                await mandrill.SendMessage(new SendMessageRequest(email));

                return this.View((object)validJson);
            }
            catch (Exception ex)
            {
                // Send a mail with the error
                var email = new EmailMessage
                                {
                                    FromEmail = this.emailSendingAddress,
                                    To = new List<EmailAddress> { new EmailAddress(sender ?? this.errorEmailRecipient) },
                                    Subject = "Error in VCard Import",
                                    Text = JsonConvert.SerializeObject(ex, Formatting.Indented)
                                };

                mandrill.SendMessage(new SendMessageRequest(email));

                throw;
            }
        }