Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Account.cc

Go to the documentation of this file.
00001 /* Account.cc - source file for the mailfilter program
00002  * Copyright (c) 2000  Andreas Bauer <baueran@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00017  * USA.
00018  */
00019 
00020 
00021 #include <vector>
00022 #include <string>
00023 #include <ctype.h>
00024 #include "Account.hh"
00025 
00026 
00027 namespace acc {
00028 
00029   Account::~Account() {
00030     vector<Header>::iterator curMessage = headers.begin();
00031     
00032     for(;curMessage != headers.end(); curMessage++)
00033       curMessage->lines.clear();
00034 
00035     headers.clear();
00036   }
00037 
00038 
00039   // Stores the _entire_ header of a message ('str') in the 'headers' variable, as well as the number of the message and its size
00040   // On success it returns 0. It returns -1 if for some reason the header string is unexpected in it's syntax or shape.
00041   int Account::storeMessageHeader(string str, int message, int size) {
00042     Header curHeader;
00043     Line curLine;
00044     curHeader.number = message;
00045     curHeader.size = size;
00046 
00047     int descrStart = 0, descrEnd = 0;
00048     int contentStart = 0, contentEnd = 0;
00049     string head = cleanString(str);
00050 
00051     // Extract information about "Subject: ...", "From: ..." and store it in messageHeaders.section
00052     for (int i = 0; i < (int)head.length(); i++) {
00053       // Description
00054       if ( isupper(head[i]) ) {
00055     descrStart = i;
00056     if ( (descrEnd = head.find(":", descrStart)) == -1)
00057       return(-1);    // encountered malformed e-mail header. If that happens, Mailfilter needs fixing here, obviously.
00058     curLine.descr = head.substr(descrStart, (descrEnd - descrStart));
00059 
00060     // Content
00061     contentStart = descrEnd + 2;
00062     if ( (contentEnd = head.find("\n", contentStart)) == -1 ) // we only store the 1st line (in case there's more)
00063       contentEnd = head.length();
00064     curLine.content = head.substr(contentStart, (contentEnd - contentStart));
00065 
00066     // Store header information for deleting
00067     if (curLine.descr.find("From") == 0)
00068       curHeader.sender = curLine.content.c_str();
00069     else if (curLine.descr.find("Subject") == 0)
00070       curHeader.subject = curLine.content.c_str();
00071     else if (curLine.descr.find("Date") == 0)
00072       curHeader.date = curLine.content.c_str();
00073 
00074     // Move counter
00075     i = contentEnd;
00076 
00077     // Store the current line in the header (descr + content)
00078     curHeader.lines.push_back(curLine);
00079       }
00080       else { 
00081     if ( (i = head.find("\n", i)) == -1 )
00082       i = head.length();
00083       }
00084     }
00085     
00086     // Store the header with all its lines
00087     headers.push_back(curHeader);
00088 
00089     return 0;
00090   }
00091 
00092 
00093   // Removes special line breaks from a string
00094   string Account::cleanString(string dirty) {
00095     for (int i = 0; i <= (int)dirty.length(); i++) {
00096       if ( (dirty[i] == '\r' && dirty[i+1] == '\n') || (dirty[i] == '\n' && dirty[i+1] == '\r') ) {
00097     dirty.replace(i, 2, "\n");
00098       }
00099     }
00100     
00101     return(dirty);
00102   }
00103 
00104 }

Generated at Tue Dec 19 19:21:03 2000 for mailfilter by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000