BaseEntity.cs :  » Bloggers » dasBlog » Lesnikowski » Pawel » Mail » Pop3 » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Bloggers » dasBlog 
dasBlog » Lesnikowski » Pawel » Mail » Pop3 » BaseEntity.cs
using System;
using System.Collections;

namespace Lesnikowski.Pawel.Mail.Pop3{
  public class BaseEntity
  {
    protected string header; //all header 
    public string contentType;
    public string contentTransferEncoding;
    public string charset;
        public string boundary;
        public bool hasAttachments=false;
        public ArrayList attachments=new ArrayList();

    public BaseEntity(string buffer)
    {
      GetHeader(buffer);
            this.contentType      =this.GetValueFromHeader("Content-Type: ");
            //Try again? Not per spec...
            if (this.contentType == null || this.contentType.Length == 0)
            {
                this.contentType = this.GetValueFromHeader("Content-type: ");
            }
            this.contentTransferEncoding=this.GetValueFromHeader("Content-Transfer-Encoding: ");

      if (contentType.IndexOf("text/plain")!=-1 || contentType.IndexOf("text/html")!=-1)
        this.charset=GetValueEqualFromHeader2("charset=");
    }

        internal bool ParseMixedMessage(string buffer, string boundary)
        {
            int start=0,end;
        
            for(;;)
            {
                start=buffer.IndexOf(boundary,start);
        
                if (start==-1)
                    break;
        
                end=buffer.IndexOf(boundary,start+1);

                if (end==-1)
                    break;

                // remove boundary from buffer
                start += boundary.Length;

                Attachment a=new Attachment( buffer.Substring(start,end-start-2) );

                this.attachments.Add(a);
            }
            return true;
        }

    protected void GetHeader(string buffer) 
    {
      int end=buffer.IndexOf("\r\n\r\n");

      if (end==-1)
        throw new Exception("can't get header");
      
      header=buffer.Substring(0, end);
      header+="\r\n\r\n";
    }

    protected string GetValueFromHeader(string key)
    {
      int start=header.ToLower().IndexOf(key.ToLower());
      if(start<0)
        return "";
      start=header.IndexOf(' ',start);
      if (start<0)
        return "";
      int end=header.IndexOf("\r\n",start);
      if (end<=0)
        return "";
      return QuotedCoding.Decode(header.Substring(start+1, end-start-1).Trim());
    }

    /// <summary>
    /// z 'ala="123"'
    /// </summary>
    /// <param name="key">ala=</param>
    /// <returns>123</returns>
    protected string GetValueEqualFromHeader(string key)
    {
      int start=header.IndexOf(key);
      if (start==-1)
        return "";
      start=header.IndexOf('"',start);
      int end=header.IndexOf('"',start+1);
      if (start==-1 || end==-1)
        return "";
      return QuotedCoding.Decode(header.Substring(start+1,end-start-1));
    }

    /// <summary>
    /// z 'ala="123"'
    /// </summary>
    /// <param name="key">ala=</param>
    /// <returns>123</returns>
    protected string GetValueEqualFromHeader2(string key)
    {
      bool noQuotes = false;
      int end = -1;
      int start = header.IndexOf(key);
      if (start == -1)
        return "";
      start = header.IndexOf('"', start);
      if (start == -1)
      {
        noQuotes = true;
        start = header.IndexOf('=', header.IndexOf(key));
      }
      if (noQuotes)
      {
        end = header.IndexOf("\r\n", start + 1);
      }
      else
      {
        end = header.IndexOf('"', start + 1);
      }
      if (start == -1 || end == -1)
        return "";
      return QuotedCoding.Decode(header.Substring(start + 1, end - start - 1));
    }

  }
}





www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.