File: mbox.cxx

package info (click to toggle)
mimetic 0.9.6-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,656 kB
  • ctags: 1,746
  • sloc: cpp: 11,240; sh: 9,080; makefile: 115
file content (191 lines) | stat: -rw-r--r-- 5,768 bytes parent folder | download
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/***************************************************************************
    copyright            : (C) 2002-2008 by Stefano Barbato
    email                : stefano@codesink.org

    $Id: mbox.cxx,v 1.3 2008-10-07 11:06:25 tat Exp $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/** \example mbox.cc
 *  extract a Part based on command line parameters
 *  more info on:
 *      mbox -h
 */ 
#include <iostream>
#include <sstream>
#include <memory>
#include <algorithm>
#include <iterator>
#include <fstream>
#include <cassert>
#include <mimetic/mimetic.h>
#include <mimetic/parser/itparser.h>
//#include <mimetic/mbx/mbox.h>

using namespace std;
using namespace mimetic;

unsigned int g_matches = 0;

int g_verbose; // verbose mode on/off
int g_quiet; // quiet mode
int g_entityCount; // num of entities found

void usage()
{
    cout << "mbox [params] [in_file...]" << endl;
    exit(-1);
}


void die_if(bool b, const string& msg)
{
    if(b)
    {
        cerr << "Error: " << msg << endl << endl;
        usage();
    }
}

inline int isnl(char c)
{
    return c == '\n' || c == '\r';
}

void printTabs(int c)
{
    while(c--)
        cout << "    ";
}

void printMimeStructure(MimeEntity* pMe, int tabcount = 0)
{
    ++g_entityCount;
    if(!g_quiet)
    {
        Header& h = pMe->header();
        ContentType ct = h.contentType();
        printTabs(tabcount);
        cout << ct.type() << "/" << ct.subtype() << endl;
        if(g_verbose)
        {
            ContentType::ParamList::iterator bit, eit;
            bit = ct.paramList().begin();
            eit = ct.paramList().end();
            for(; bit != eit; ++bit)
            {
                printTabs(tabcount);
                cout << "param: " << bit->name() << " = " 
                     << bit->value() << endl;
            }
            if(h.hasField(ContentTransferEncoding::label))
            {
                printTabs(tabcount);
                cout << "encoding: " 
                     << h.contentTransferEncoding().mechanism() 
                     << endl;
            }
            if(h.hasField(ContentDisposition::label))
            {
                printTabs(tabcount);
                const ContentDisposition cd = h.contentDisposition();
                cout << "disposition: " << cd.type() << endl;
                ContentDisposition::ParamList::const_iterator 
                    bit, eit;
                bit = cd.paramList().begin();
                eit = cd.paramList().end();
                for(; bit != eit; ++bit)
                {
                    printTabs(tabcount);
                    cout << "param: " << bit->name() << " = " 
                         << bit->value() << endl;
                }
            }
            Header::iterator hbit, heit;
            hbit = pMe->header().begin();
            heit = pMe->header().end();
            for(; hbit != heit; ++hbit)
            {
                printTabs(tabcount);
                cout << "h: " << hbit->name() << " = " 
                     << hbit->value() << endl;
            }
            if(pMe->body().preamble().length())
            {
                printTabs(tabcount);
                cout << "preamble: " << pMe->body().preamble() 
                    << endl;
            }
            if(pMe->body().epilogue().length())
            {
                printTabs(tabcount);
                cout << "epilogue: " << pMe->body().epilogue() 
                    << endl;
            }
            printTabs(tabcount);
            cout << "part size: " << pMe->size() << endl;
            printTabs(tabcount);
            cout << "body size: " << pMe->body().length() << endl;
            cout << endl;
        }
    }
    MimeEntityList::iterator mbit = pMe->body().parts().begin(),
                    meit = pMe->body().parts().end();
    for(;mbit!=meit;++mbit)
        printMimeStructure(*mbit, 1 + tabcount);
}

int main(int argc, char** argv)
{
    std::ios_base::sync_with_stdio(false);

    die_if(argc < 2, "mbox filename required");
    die_if(!File::exists(argv[1]), 
        string("Unable to access file: ") + argv[1]);

    File f(argv[1]);
    File::iterator bit = f.begin(), eit = f.end(), it;
    int count = 0;
    do
    {
        // skip "From blah blah" line
        for(; !isnl(*bit); ++bit)
            ;
        if(isnl(*++bit)) 
            ++bit; // jump 2 chars new lines
        it = utils::find_bm(bit, eit, "From ");
        if(!isnl(*(it-1)))
            continue;
        MimeEntity me(bit, it);
        printMimeStructure(&me);
        bit = it;
        ++count;
    } while(bit != eit);

    cerr << count << " messages parsed" << endl;
    /*
    MboxMailbox mm(argv[1], ~Parser::imHeader);
    copy(mm.begin(), mm.end(), osi);
    cout << endl;
    return 0;

    int count;
    MboxMailbox mm(argv[1], ~Parser::imHeader);
    MboxMailbox::const_iterator mbit = mm.begin(), meit = mm.end();
    for(count = 0; mbit != meit; ++mbit, ++count, ++osi)
    {
        const MimeEntity& e = *mbit;
        *osi = *mbit;
    }
    cerr << count << " messages parsed" << endl;
    */
    return 0;
}