/usr/share/doc/libmimetic-doc/examples/engine.cxx is in libmimetic-doc 0.9.8-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 | #include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
#include <sys/types.h>
#include <regex.h>
#ifdef HAS_PCRE
#include <pcreposix.h>
#endif
#include "engine.h"
using namespace std;
using namespace mimetic;
engine::engine(const command_line& cl)
: m_cl(cl), m_pcre(false),
m_match_mode(match_type_none)
{
// set options
if(cl.is_set(p_match_shell) || cl.is_set(p_match_regex))
m_match_mode =
cl.is_set(p_match_shell) ?
match_type_shell :
match_type_regex;
m_match_mode |=
cl.is_set(p_case_insensitive) ? match_flag_case_insensitive : 0;
m_match_mode |=
cl.is_set(p_perl_regex) ? match_flag_perl_mode: 0;
if((m_match_mode & match_type_mask) == match_type_none)
m_match_mode |= match_type_regex; // default
}
int engine::posix_regex_match(const string& text, const string& pattern, int match_mode)
{
int r;
regex_t rex;
r = regcomp(&rex, pattern.c_str(),
( match_mode & match_flag_case_insensitive ? REG_ICASE: 0));
if(r != 0)
{
char buf[256];
regerror(r, &rex, buf, 255);
die(buf);
}
r = regexec(&rex, text.c_str(), 0, 0, 0);
regfree(&rex);
return r == 0;
}
int engine::perl_regex_match(const string& text, const string& pattern, int match_mode )
{
#ifdef HAS_PCRE
#else
die("uuh?");
#endif
return 0;
}
int engine::pattern_match(const string& text, const string& pattern, int match_mode)
{
switch(match_mode & match_type_mask)
{
case match_type_none:
die("match_type_none");
case match_type_exact:
return exact_match(text, pattern, match_mode);
case match_type_regex:
return regex_match(text, pattern, match_mode);
case match_type_shell:
return shell_match(text, pattern, match_mode);
default:
die("uuh?");
}
return 0;
}
int engine::shell_match(const string& text, const string& pattern, int match_mode)
{
die("not impl");
return 0;
}
int engine::regex_match(const string& text, const string& pattern, int match_mode)
{
if(m_pcre)
return engine::perl_regex_match(text, pattern, match_mode);
else
return engine::posix_regex_match(text, pattern, match_mode);
}
void engine::action_attach(MimeEntity& me, parts_hierarchy* ph, const string& fqn)
{
bool isMultipart = me.header().contentType().isMultipart();
bool isTopLevel = !ph->size();
/*
1) me is multipart:
add the attach to me as the last part
2) me is not multipart
create a multipart/mixed with me and the attach childs
and put it in the same level/position of me
3) me is not multipart and is the top level entity
same as 2) but move all me fields to the new top-level
*/
Attachment* pA = new Attachment(fqn);
if(!pA->isValid())
die("attach error");
if(isMultipart)
{
DBG( "isMultipart");
me.body().parts().push_back(pA);
} else {
MimeEntity *mm;
mm = new MultipartMixed;
mm->body().parts().push_back(&me);
mm->body().parts().push_back(pA);
if(!isTopLevel)
{
DBG( "!isTopLevel");
MimeEntity *parent = *ph->begin();
replace(parent->body().parts().begin(),
parent->body().parts().end(),
&me, mm);
} else {
DBG( "isTopLevel");
// add cp fields here
Header::iterator bit, eit, pos;
bit = me.header().begin(), me.header().end();
string name; // field name
pos = mm->header().begin(); // insert before others
for(; bit != eit; ++bit)
{
name = bit->name();
transform(name.begin(), name.end(),
name.begin(), ::tolower);
if(name.find("content-") == 0 || name == "mime-version")
continue;
mm->header().insert(pos, *bit);
}
}
}
}
void engine::action(MimeEntity& me, parts_hierarchy* ph)
{
MimeEntity* parent = (ph->size() ? *ph->begin() : &me);
if(m_cl.is_set(p_add_header))
{
static const char* key = "add-header";
command_line::iterator bit, eit;
bit = m_cl.begin(key), eit = m_cl.end(key);
for(; bit != eit; ++bit)
{
Field f(bit->second);
parent->header().push_back(f);
}
}
if(m_cl.is_set(p_add_part_header)) {
static const char* key = "add-part-header";
command_line::iterator bit, eit;
bit = m_cl.begin(key), eit = m_cl.end(key);
for(; bit != eit; ++bit)
{
Field f(bit->second);
me.header().push_back(f);
}
}
if(m_cl.is_set(p_attach))
{
static const char* key = "attach";
command_line::iterator bit, eit;
bit = m_cl.begin(key), eit = m_cl.end(key);
for(; bit != eit; ++bit)
action_attach(me, ph, bit->second);
}
if(m_cl.is_set(p_print_msg))
cout << *parent;
else if(m_cl.is_set(p_print_part))
cout << me;
}
int engine::exact_match(const string& text, const string& pattern, int match_mode)
{
if(match_mode & match_flag_case_insensitive)
{
istring is(text);
return is == pattern;
} else
return text == pattern;
}
/*
* expr: pat1 [=|~] pat2
* pat1 is the pattern that represents the field name
* pat2 is the pattern that represents the field value
*/
int engine::pattern_field_match(const MimeEntity& me, const string& expr,
int match_mode)
{
int has_value = 0; // left part of the expr
char prev = 0; // previous char
string field_pat, value_pat;
char op;
for(size_t i = 0; i < expr.length(); ++i)
{
if( (expr[i] == '=' || expr[i] == '~') && prev != '\\')
{
has_value++; // right part
op = expr[i];
continue;
}
if(!has_value)
field_pat.append(1, expr[i]);
else
value_pat.append(1, expr[i]);
prev = expr[i];
}
field_pat = remove_external_blanks(field_pat);
value_pat = remove_external_blanks(value_pat);
// first try to find a field that match the field_pat pattern
const Header& h = me.header();
Header::const_iterator bit = h.begin(), eit = h.end();
for( ; bit != eit; ++bit)
{
if(pattern_match(bit->name(), field_pat, match_mode))
{ // we've found a matching field, let's check the value
if(!has_value)
return 1;
else
if(pattern_match(bit->value(), value_pat, match_mode))
return 1;
}
}
return 0;
}
string engine::remove_external_blanks(const string& str) const
{
// a dirty way to trim ext.blanks
string s = str;
for(int i = s.length() - 1; i >= 0; --i)
if(s[i] == ' ')
s.erase(i, 1);
else
break;
while(s.length() && s[0] == ' ')
s.erase(0, 1);
return s;
}
int engine::fixed_field_match(const MimeEntity& me, const string& name, const string& value, int match_mode)
{
if(!me.header().hasField(name))
return 0;
if(value.length() == 0)
return 1; // it exists
const string& field_value = me.header().field(name).value();
return pattern_match(field_value, value, match_mode) ;
}
int engine::has_binary_attach(const MimeEntity& me, const command_line_switch& cls)
{
const Header& h = me.header();
const ContentType& ct = h.contentType();
if(ct.type() == "text" || ct.type() == "multipart" || ct.type() == "message")
return 0;
const ContentTransferEncoding& cte = h.contentTransferEncoding();
if(cte.mechanism() == "base64")
return 1;
return 1;
}
int engine::field_match(const MimeEntity& me, const command_line_switch& cls)
{
const string& name = cls.first, value = cls.second;
if(name == "field")
return pattern_field_match(me, value, m_match_mode);
else if (name == "ifield")
return pattern_field_match(me, value,
m_match_mode | match_flag_case_insensitive);
else
return fixed_field_match(me, name, value,
m_match_mode);
}
int engine::has_field(const MimeEntity& me, const command_line_switch& cls)
{
return me.header().hasField(cls.second);
}
int engine::match_filename(const string& filename, const string& pattern)
{
// convert shell pattern string to regex
string re_pattern;
char c;
for(size_t i = 0; i < pattern.length(); ++i)
{
c = pattern[i];
switch(c)
{
case '?':
re_pattern.append(".");
break;
case '*':
re_pattern.append(".*");
break;
case '[':
case '.':
case '=':
case '<':
case '>':
case '+':
case '_':
case '\\':
case '-':
case ']':
re_pattern.append(1, '\\');
re_pattern.append(1, c);
break;
default:
re_pattern.append(1, c);
}
}
return regex_match(filename, re_pattern, 0);
}
int engine::attach_filename(const MimeEntity& me,const command_line_switch& cls)
{
typedef list<string> filename_list;
const Header& h = me.header();
const ContentType& ct = h.contentType();
const ContentDisposition& cd = h.contentDisposition();
string pattern = cls.second;
filename_list names;
// content-type params
names.push_back(ct.param("name"));
names.push_back(ct.param("filename")); // should not exists
// content-disposition params
names.push_back(cd.param("name"));
names.push_back(cd.param("filename")); // should not exists
filename_list::const_iterator bit = names.begin(), eit = names.end();
for( ; bit != eit; ++bit)
if(match_filename(*bit, pattern))
return 1;
return 0;
}
MimeEntity* engine::match(MimeEntity& me, int level, parts_hierarchy* ph)
{
int matched = 1, child_match = 0, free = 0;
if(ph == 0)
{
ph = new parts_hierarchy;
free++;
}
if(m_cl.is_set(p_recursive))
{
MimeEntityList& parts = me.body().parts();
if( parts.size() )
{
++level;
MimeEntityList::iterator mbit, meit;
mbit = parts.begin(), meit = parts.end();
ph->insert(ph->begin(), &me);
for( ; mbit != meit; ++mbit)
child_match += (match(**mbit, level, ph ) ? 1 : 0);
ph->erase(ph->begin());
}
}
static char *std_fields[] = {
"from", "sender", "to", "sujbect", "cc", "bcc",
"user-agent", "date", "content-type",
"content-transfer-encoding", "content-disposition",
"content-description",
0
};
command_line::const_iterator bit = m_cl.begin(), eit = m_cl.end();
for(; bit != eit; ++bit)
{
const string& name = bit->first, value = bit->second;
if(name == "attach-filename") {
if(!attach_filename(me, *bit))
{
matched = 0;
break;
}
} else if(name == "has-field") {
if(!has_field(me, *bit))
{
matched = 0;
break;
}
} else if(name == "has-binary-attach") {
if(!has_binary_attach(me, *bit))
{
matched = 0;
break;
}
} else if(name == "field") {
if(!field_match(me, *bit))
{
matched = 0;
break;
}
} else if(name == "ifield") {
if(!field_match(me, *bit))
{
matched = 0;
break;
}
} else {
int break_loop = 0;
char **std_name = std_fields;
for( int i = 0 ; std_name[i] ; ++i)
{
if(name == std_name[i])
if(!field_match(me, *bit))
{
matched = 0;
break_loop++;
break;
}
}
if(break_loop)
break;
}
}
if(matched)
action(me, ph);
if(free)
delete ph;
return ( matched || child_match ? &me : 0);
}
|