libkmime

kmime_message.cpp
1 /*
2  kmime_message.cpp
3 
4  KMime, the KDE internet mail/usenet news message library.
5  Copyright (c) 2001 the KMime authors.
6  See file AUTHORS for details
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
15 */
16 
17 #include "kmime_message.h"
18 
19 using namespace KMime;
20 
21 namespace KMime {
22 
23 Message::Message()
24 {
25  s_ubject.setParent(this);
26  d_ate.setParent(this);
27 }
28 
29 Message::~Message() {}
30 
31 void Message::parse()
32 {
33  Content::parse();
34 
35  TQCString raw;
36  if( !(raw=rawHeader(s_ubject.type())).isEmpty() )
37  s_ubject.from7BitString(raw);
38 
39  if( !(raw=rawHeader(d_ate.type())).isEmpty() )
40  d_ate.from7BitString(raw);
41 }
42 
43 
44 void Message::assemble()
45 {
46  Headers::Base *h;
47  TQCString newHead="";
48 
49  //Message-ID
50  if( (h=messageID(false))!=0 )
51  newHead+=h->as7BitString()+"\n";
52 
53  //From
54  h=from(); // "From" is mandatory
55  newHead+=h->as7BitString()+"\n";
56 
57  //Subject
58  h=subject(); // "Subject" is mandatory
59  newHead+=h->as7BitString()+"\n";
60 
61  //To
62  if( (h=to(false))!=0 )
63  newHead+=h->as7BitString()+"\n";
64 
65  //Cc
66  if( (h=cc(false))!=0 )
67  newHead+=h->as7BitString()+"\n";
68 
69  //Reply-To
70  if( (h=replyTo(false))!=0 )
71  newHead+=h->as7BitString()+"\n";
72 
73  //Date
74  h=date(); // "Date" is mandatory
75  newHead+=h->as7BitString()+"\n";
76 
77  //References
78  if( (h=references(false))!=0 )
79  newHead+=h->as7BitString()+"\n";
80 
81  //Organization
82  if( (h=organization(false))!=0 )
83  newHead+=h->as7BitString()+"\n";
84 
85  //UserAgent
86  if( (h=userAgent(false))!=0 )
87  newHead+=h->as7BitString()+"\n";
88 
89  //Mime-Version
90  newHead+="MIME-Version: 1.0\n";
91 
92  //Content-Type
93  newHead+=contentType()->as7BitString()+"\n";
94 
95  //Content-Transfer-Encoding
96  newHead+=contentTransferEncoding()->as7BitString()+"\n";
97 
98  //X-Headers
99  int pos=h_ead.find("\nX-");
100  if(pos>-1) //we already have some x-headers => "recycle" them
101  newHead+=h_ead.mid(pos+1, h_ead.length()-pos-1);
102  else if(h_eaders && !h_eaders->isEmpty()) {
103  for(h=h_eaders->first(); h; h=h_eaders->next()) {
104  if( h->isXHeader() && (strncasecmp(h->type(), "X-KNode", 7)!=0) )
105  newHead+=h->as7BitString()+"\n";
106  }
107  }
108 
109  h_ead=newHead;
110 }
111 
112 
113 void Message::clear()
114 {
115  s_ubject.clear();
116  d_ate.clear();
117  f_lags.clear();
118  Content::clear();
119 }
120 
121 
122 Headers::Base* Message::getHeaderByType(const char *type)
123 {
124  if(strcasecmp("Subject", type)==0) {
125  if(s_ubject.isEmpty()) return 0;
126  else return &s_ubject;
127  }
128  else if(strcasecmp("Date", type)==0){
129  if(d_ate.isEmpty()) return 0;
130  else return &d_ate;
131  }
132  else
133  return Content::getHeaderByType(type);
134 }
135 
136 
137 void Message::setHeader(Headers::Base *h)
138 {
139  bool del=true;
140  if(h->is("Subject"))
141  s_ubject.fromUnicodeString(h->asUnicodeString(), h->rfc2047Charset());
142  else if(h->is("Date"))
143  d_ate.setUnixTime( (static_cast<Headers::Date*>(h))->unixTime() );
144  else {
145  del=false;
146  Content::setHeader(h);
147  }
148 
149  if(del) delete h;
150 }
151 
152 
153 bool Message::removeHeader(const char *type)
154 {
155  if(strcasecmp("Subject", type)==0)
156  s_ubject.clear();
157  else if(strcasecmp("Date", type)==0)
158  d_ate.clear();
159  else
160  return Content::removeHeader(type);
161 
162  return true;
163 }
164 
165 
166 
167 
168 } // namespace KMime
Baseclass of all header-classes.
bool isXHeader()
Check if this header is a X-Header.
virtual const char * type()
Return the type of this header (e.g.
TQCString rfc2047Charset()
Return the charset that is used for RFC2047-encoding.
virtual TQCString as7BitString(bool=true)
Return the encoded header.
virtual void clear()
Delete.
bool is(const char *t)
Check if this header is of type t.
virtual TQString asUnicodeString()
Return the decoded content of the header without the header-type.
Represents a "Date" header.