Click here to Skip to main content
15,893,190 members
Articles / Programming Languages / C#

STUN Client

Rate me:
Please Sign up or sign in to vote.
4.83/5 (36 votes)
20 Apr 2007CPOL 328.1K   14.9K   85  
STUN client C# implementation with sample application
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace LumiSoft.Net.Mime.vCard
{
    /// <summary>
    /// Rfc 2426 vCard implementation.
    /// </summary>
    public class vCard
    {
        private ItemCollection            m_pItems          = null;
        private DeliveryAddressCollection m_pAddresses      = null;
        private PhoneNumberCollection     m_pPhoneNumbers   = null;
        private EmailAddressCollection    m_pEmailAddresses = null;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public vCard()
        {
            m_pItems          = new ItemCollection();
            this.Version      = "3.0";
            this.UID          = Guid.NewGuid().ToString();
        }


        #region method ToByte

        /// <summary>
        /// Stores vCard structure to byte[].
        /// </summary>
        /// <returns></returns>
        public byte[] ToByte()
        {
            MemoryStream ms = new MemoryStream();
            ToStream(ms);
            return ms.ToArray();
        }

        #endregion

        #region method ToFile

        /// <summary>
        /// Stores vCard to the specified file.
        /// </summary>
        /// <param name="file">File name with path where to store vCard.</param>
        public void ToFile(string file)
        {
            using(FileStream fs = File.Create(file)){
                ToStream(fs);
            }
        }

        #endregion

        #region method ToStream

        /// <summary>
        /// Stores vCard structure to the specified stream.
        /// </summary>
        /// <param name="stream">Stream where to store vCard structure.</param>
        public void ToStream(Stream stream)
        {
            /* 
                BEGIN:VCARD<CRLF>
                ....
                END:VCARD<CRLF>
            */

            StringBuilder retVal = new StringBuilder();
            retVal.Append("BEGIN:VCARD\r\n");
            foreach(Item item in m_pItems){
                retVal.Append(item.ToItemString() + "\r\n");
            }
            retVal.Append("END:VCARD\r\n");

            byte[] data = System.Text.Encoding.UTF8.GetBytes(retVal.ToString());
            stream.Write(data,0,data.Length);
        }

        #endregion
        

        #region method Parse

        /// <summary>
        /// Parses vCard from the specified file.
        /// </summary>
        /// <param name="file">vCard file with path.</param>
        public void Parse(string file)
        {
            using(FileStream fs = File.OpenRead(file)){
                Parse(fs);
            }
        }

        /// <summary>
        /// Parses vCard from the specified stream.
        /// </summary>
        /// <param name="stream">Stream what contains vCard.</param>
        public void Parse(Stream stream)
        {
            m_pItems.Clear();
            m_pPhoneNumbers = null;
            m_pEmailAddresses = null;

            TextReader r = new StreamReader(stream,System.Text.Encoding.Default);            
            string line = r.ReadLine();
            // Find row BEGIN:VCARD
            while(line != null && line.ToUpper() != "BEGIN:VCARD"){
                line = r.ReadLine();
            }
            // Read frist vCard line after BEGIN:VCARD
            line = r.ReadLine();
            while(line != null && line.ToUpper() != "END:VCARD"){
                StringBuilder item = new StringBuilder();
                item.Append(line);
                // Get next line, see if item continues (folded line).
                line = r.ReadLine();
                while(line != null && (line.StartsWith("\t") || line.StartsWith(" "))){
                    item.Append(line.Substring(1));
                    line = r.ReadLine();
                }

                string[] name_value = item.ToString().Split(new char[]{':'},2);

                // Item syntax: name[*(;parameter)]:value
                string[] name_params = name_value[0].Split(new char[]{';'},2);
                string   name        = name_params[0];
                string   parameters  = "";
                if(name_params.Length == 2){
                    parameters = name_params[1];
                }
                string value = "";
                if(name_value.Length == 2){
                    value = name_value[1];
                }
                m_pItems.Add(name,parameters,value);
            }
        }

        #endregion
                        

        #region Properties Implementation

        /// <summary>
        /// Gets reference to vCard items.
        /// </summary>
        public ItemCollection Items
        {
            get{ return m_pItems; }
        }

        /// <summary>
        /// Gets or sets vCard version. Returns null if VERSION: item doesn't exist.
        /// </summary>
        public string Version
        {
            get{ 
                Item item = m_pItems.GetFirst("VERSION");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("VERSION",value); }
        }

        /// <summary>
        /// Gets or sets name info.  Returns null if N: item doesn't exist.
        /// </summary>
        public Name Name
        {
            get{ 
                Item item = m_pItems.GetFirst("N");
                if(item != null){
                    return Name.Parse(item);
                }
                else{
                    return null;
                }
            }

            set{
                if(value != null){
                    m_pItems.SetDecodedValue("N",value.ToValueString());
                }
                else{
                    m_pItems.SetDecodedValue("N",null);
                }
            }
        }

        /// <summary>
        /// Gets or sets formatted(Display name) name.  Returns null if FN: item doesn't exist.
        /// </summary>
        public string FormattedName
        {
            get{ 
                Item item = m_pItems.GetFirst("FN");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("FN",value); }
        }

        /// <summary>
        /// Gets or sets nick name. Returns null if NICKNAME: item doesn't exist.
        /// </summary>
        public string NickName
        {
            get{ 
                Item item = m_pItems.GetFirst("NICKNAME");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("NICKNAME",value); }
        }

        /// <summary>
        /// Gets or sets person photo. Returns null if PHOTO: item doesn't exist.
        /// </summary>
        public Image Photo
        {
            get{ 
                Item item = m_pItems.GetFirst("PHOTO");
                if(item != null){                    
                    return Image.FromStream(new MemoryStream(System.Text.Encoding.Default.GetBytes(item.DecodedValue)));
                }
                else{
                    return null;
                }
            }

            set{ 
                if(value != null){
                    MemoryStream ms = new MemoryStream();
                    value.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);
                                        
                    m_pItems.SetValue("PHOTO","ENCODING=b;TYPE=JPEG",Convert.ToBase64String(ms.ToArray()));
                }
                else{
                    m_pItems.SetValue("PHOTO",null);
                }
            }
        }

        /// <summary>
        /// Gets or sets birth date. Returns DateTime.MinValue if not set.
        /// </summary>
        public DateTime BirthDate
        {
            get{ 
                Item item = m_pItems.GetFirst("BDAY");
                if(item != null){
                    string date = item.DecodedValue.Replace("-","");
                    string[] dateFormats = new string[]{
                        "yyyyMMdd",
                        "yyyyMMddz"
                    };
                    return DateTime.ParseExact(date,dateFormats,System.Globalization.DateTimeFormatInfo.InvariantInfo,System.Globalization.DateTimeStyles.None);
                }
                else{
                    return DateTime.MinValue;
                }
            }

            set{ 
                if(value != DateTime.MinValue){
                    m_pItems.SetValue("BDAY",value.ToString("yyyyMMdd")); 
                }
                else{
                    m_pItems.SetValue("BDAY",null);
                }
            }
        }
                        
        /// <summary>
        /// Gets addresses collection.
        /// </summary>
        public DeliveryAddressCollection Addresses
        {
            get{
                // Delay collection creation, create it when needed.
                if(m_pAddresses == null){
                    m_pAddresses = new DeliveryAddressCollection(this);
                }

                return m_pAddresses; 
            }
        }

        /// <summary>
        /// Gets phone number collection.
        /// </summary>
        public PhoneNumberCollection PhoneNumbers
        {
            get{
                // Delay collection creation, create it when needed.
                if(m_pPhoneNumbers == null){
                    m_pPhoneNumbers = new PhoneNumberCollection(this);
                }

                return m_pPhoneNumbers; 
            }
        }

        /// <summary>
        /// Gets email addresses collection.
        /// </summary>
        public EmailAddressCollection EmailAddresses
        {
            get{
                // Delay collection creation, create it when needed.
                if(m_pEmailAddresses == null){
                    m_pEmailAddresses = new EmailAddressCollection(this);
                }

                return m_pEmailAddresses; 
            }
        }

        /// <summary>
        /// Gets or sets job title. Returns null if TITLE: item doesn't exist.
        /// </summary>
        public string Title
        {
            get{ 
                Item item = m_pItems.GetFirst("TITLE");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("TITLE",value); }
        }

        /// <summary>
        /// Gets or sets role. Returns null if ROLE: item doesn't exist.
        /// </summary>
        public string Role
        {
            get{ 
                Item item = m_pItems.GetFirst("ROLE");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("ROLE",value); }
        }

        /// <summary>
        /// Gets or sets organization name. Usually this value is: comapny;department;office. Returns null if ORG: item doesn't exist.
        /// </summary>
        public string Organization
        {
            get{ 
                Item item = m_pItems.GetFirst("ORG");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("ORG",value); }
        }

        /// <summary>
        /// Gets or sets note text. Returns null if NOTE: item doesn't exist.
        /// </summary>
        public string NoteText
        {
            get{ 
                Item item = m_pItems.GetFirst("NOTE");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("NOTE",value); }
        }

        /// <summary>
        /// Gets or sets vCard unique ID. Returns null if UID: item doesn't exist.
        /// </summary>
        public string UID
        {
            get{ 
                Item item = m_pItems.GetFirst("UID");
                if(item != null){
                    return item.DecodedValue;
                }
                else{
                    return null;
                }
            }

            set{ m_pItems.SetDecodedValue("UID",value); }
        }

        /// <summary>
        /// Gets or sets vCard home URL.
        /// </summary>
        public string HomeURL
        {
            get{ 
                Item[] items = m_pItems.Get("URL");
                foreach(Item item in items){
                    if(item.ParametersString == "" || item.ParametersString.ToUpper().IndexOf("HOME") > -1){
                        return item.DecodedValue;
                    }
                }
                
                return null;                
            }

            set{
                Item[] items = m_pItems.Get("URL");
                foreach(Item item in items){
                    if(item.ParametersString.ToUpper().IndexOf("HOME") > -1){
                        if(value != null){
                            item.Value = value;
                        }
                        else{
                            m_pItems.Remove(item);
                        }
                        return;
                    }
                }

                if(value != null){
                    // If we reach here, URL;Work  doesn't exist, add it.
                    m_pItems.Add("URL","HOME",value);
                }
            }
        }

        /// <summary>
        /// Gets or sets vCard Work URL.
        /// </summary>
        public string WorkURL
        {
            get{ 
                Item[] items = m_pItems.Get("URL");
                foreach(Item item in items){
                    if(item.ParametersString.ToUpper().IndexOf("WORK") > -1){
                        return item.DecodedValue;
                    }
                }
                
                return null;                
            }

            set{ 
                Item[] items = m_pItems.Get("URL");
                foreach(Item item in items){
                    if(item.ParametersString.ToUpper().IndexOf("WORK") > -1){
                        if(value != null){
                            item.Value = value;
                        }
                        else{
                            m_pItems.Remove(item);
                        }
                        return;
                    }
                }

                if(value != null){
                    // If we reach here, URL;Work  doesn't exist, add it.
                    m_pItems.Add("URL","WORK",value);
                }
            }
        }

        #endregion

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Estonia Estonia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions