summaryrefslogtreecommitdiff
path: root/src/include/mime.h
blob: 14d65e6ecc71f505b2323da1c05d28ca60abea2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 *  @file mime.h
 *  @brief  Declaration of main mime parser components
 *  @author Andreas Aardal Hanssen
 *  @date Andreas Aardal Hanssen
 */

#ifndef mime_h_included
#define mime_h_included

#include <map>
#include <string>
#include <vector>

#include <stdio.h>

namespace Binc {
  class HeaderItem {
  private:
    mutable std::string key;
    mutable std::string value;

  public:
    inline const std::string &getKey() const
    {
      return key;
    }

    inline const std::string &getValue() const
    {
      return value;
    }

    HeaderItem();
    HeaderItem(const std::string &key, const std::string &value);
  };

  class Header {
  private:
    mutable std::vector<HeaderItem> content;

  public:
    bool getFirstHeader(const std::string &key, HeaderItem &dest) const;
    bool getAllHeaders(const std::string &key, std::vector<HeaderItem> &dest) const;
    void add(const std::string &name, const std::string &content);
    void clear() const;

    Header();
    ~Header();
  };

  class IODevice;
  class MimeDocument;

  class MimePart {
  protected:
  public:
    mutable bool multipart;
    mutable bool messagerfc822;
    mutable std::string subtype;
    mutable std::string boundary;

    mutable unsigned int headerstartoffsetcrlf;
    mutable unsigned int headerlength;

    mutable unsigned int bodystartoffsetcrlf;
    mutable unsigned int bodylength;
    mutable unsigned int nlines;
    mutable unsigned int nbodylines;
    mutable unsigned int size;

  public:
    enum FetchType { FetchBody, FetchHeader, FetchMime };

    mutable Header h;

    mutable std::vector<MimePart> members;

    inline const std::string &getSubType() const
    {
      return subtype;
    }

    inline bool isMultipart() const
    {
      return multipart;
    }

    inline bool isMessageRFC822() const
    {
      return messagerfc822;
    }

    inline unsigned int getSize() const
    {
      return bodylength;
    }

    inline unsigned int getNofLines() const
    {
      return nlines;
    }

    inline unsigned int getNofBodyLines() const
    {
      return nbodylines;
    }

    inline unsigned int getBodyLength() const
    {
      return bodylength;
    }

    inline unsigned int getBodyStartOffset() const
    {
      return bodystartoffsetcrlf;
    }

    void printBody(int fd,
                   Binc::IODevice &output,
                   unsigned int startoffset,
                   unsigned int length) const;
    void printHeader(int fd,
                     Binc::IODevice &output,
                     std::vector<std::string> headers,
                     bool includeheaders,
                     unsigned int startoffset,
                     unsigned int length,
                     std::string &storage) const;
    void printDoc(int fd,
                  Binc::IODevice &output,
                  unsigned int startoffset,
                  unsigned int length) const;
    virtual void clear() const;

    const MimePart *getPart(const std::string &findpart,
                            std::string genpart,
                            FetchType fetchType = FetchBody) const;
    virtual int parseOnlyHeader(const std::string &toboundary) const;
    virtual int parseFull(const std::string &toboundary, int &boundarysize) const;

    MimePart();
    virtual ~MimePart();
  };

  class MimeDocument : public MimePart {
  private:
    mutable bool headerIsParsed;
    mutable bool allIsParsed;

  public:
    void parseOnlyHeader(int fd) const;
    void parseFull(int fd) const;
    void clear() const;

    inline bool isHeaderParsed()
    {
      return headerIsParsed;
    }

    inline bool isAllParsed()
    {
      return allIsParsed;
    }

    MimeDocument();
    ~MimeDocument();
  };

};

#endif