summaryrefslogtreecommitdiffstats
path: root/kioslaves/imap4/mailheader.cc
blob: 76e03ace992a8cd25aa18ab8fac8c8db18b0a997 (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
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
/***************************************************************************
                          mailheader.cc  -  description
                             -------------------
    begin                : Tue Oct 24 2000
    copyright            : (C) 2000 by Sven Carstens
    email                : s.carstens@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "mailheader.h"
#include "rfcdecoder.h"

mailHeader::mailHeader ()
{
  toAdr.setAutoDelete (true);
  ccAdr.setAutoDelete (true);
  bccAdr.setAutoDelete (true);
  setType ("text/plain");
  gmt_offset = 0;
}

mailHeader::~mailHeader ()
{
}

void
mailHeader::addHdrLine (mimeHdrLine * inLine)
{
  mimeHdrLine *addLine = new mimeHdrLine (inLine);

  const TQCString label(addLine->getLabel());
  TQCString value(addLine->getValue());
  
  if (!qstricmp (label, "Return-Path")) {
	returnpathAdr.parseAddress (value.data ());
	goto out;
  }
  if (!qstricmp (label, "Sender")) {
	senderAdr.parseAddress (value.data ());
	goto out;
  }
  if (!qstricmp (label, "From")) {
	fromAdr.parseAddress (value.data ());
	goto out;
  }
  if (!qstricmp (label, "Reply-To")) {
	replytoAdr.parseAddress (value.data ());
	goto out;
  }
  if (!qstricmp (label, "To")) {
	mailHeader::parseAddressList (value, &toAdr);
	goto out;
  }
  if (!qstricmp (label, "CC")) {
	mailHeader::parseAddressList (value, &ccAdr);
	goto out;
  }
  if (!qstricmp (label, "BCC")) {
	mailHeader::parseAddressList (value, &bccAdr);
	goto out;
  }
  if (!qstricmp (label, "Subject")) {
	_subject = value.simplifyWhiteSpace();
	goto out;
  }
  if (!qstricmp (label.data (), "Date")) {
	mDate = value;
	goto out;
  }
  if (!qstricmp (label.data (), "Message-ID")) {
      int start = value.tqfindRev ('<');
      int end = value.tqfindRev ('>');
      if (start < end)
          messageID = value.mid (start, end - start + 1);
      else {
	  qWarning("bad Message-ID");
          /* messageID = value; */
      }
      goto out;
  }
  if (!qstricmp (label.data (), "In-Reply-To")) {
      int start = value.tqfindRev ('<');
      int end = value.tqfindRev ('>');
      if (start < end)
        inReplyTo = value.mid (start, end - start + 1);
      goto out;
  }

  // everything else is handled by mimeHeader
  mimeHeader::addHdrLine (inLine);
  delete addLine;
  return;
  
 out:
//  cout << label.data() << ": '" << value.data() << "'" << endl;

  //need only to add this line if not handled by mimeHeader       
  originalHdrLines.append (addLine);
}

void
mailHeader::outputHeader (mimeIO & useIO)
{
  static const TQCString __returnPath("Return-Path: ", 14);
  static const TQCString __from      ("From: ", 7);
  static const TQCString __sender    ("Sender: ", 9);
  static const TQCString __replyTo   ("Reply-To: ", 11);
  static const TQCString __to        ("To: ", 5);
  static const TQCString __cc        ("CC: ", 5);
  static const TQCString __bcc       ("BCC: ", 6);
  static const TQCString __subject   ("Subject: ", 10);
  static const TQCString __messageId ("Message-ID: ", 13);
  static const TQCString __inReplyTo ("In-Reply-To: ", 14);
  static const TQCString __references("References: ", 13);
  static const TQCString __date      ("Date: ", 7);

  if (!returnpathAdr.isEmpty())
    useIO.outputMimeLine(__returnPath + returnpathAdr.getStr());
  if (!fromAdr.isEmpty())
    useIO.outputMimeLine(__from + fromAdr.getStr());
  if (!senderAdr.isEmpty())
    useIO.outputMimeLine(__sender + senderAdr.getStr());
  if (!replytoAdr.isEmpty())
    useIO.outputMimeLine(__replyTo + replytoAdr.getStr());

  if (toAdr.count())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__to +
                                    mailHeader::getAddressStr(&toAdr)));
  if (ccAdr.count())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__cc +
                                    mailHeader::getAddressStr(&ccAdr)));
  if (bccAdr.count())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__bcc +
                                    mailHeader::getAddressStr(&bccAdr)));
  if (!_subject.isEmpty())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__subject + _subject));
  if (!messageID.isEmpty())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__messageId + messageID));
  if (!inReplyTo.isEmpty())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__inReplyTo + inReplyTo));
  if (!references.isEmpty())
    useIO.outputMimeLine(mimeHdrLine::truncateLine(__references + references));

  if (!mDate.isEmpty())
    useIO.outputMimeLine(__date + mDate);
  mimeHeader::outputHeader(useIO);
}

int
mailHeader::parseAddressList (const char *inCStr,
                              TQPtrList < mailAddress > *aList)
{
  int advance = 0;
  int skip = 1;
  char *aCStr = (char *) inCStr;

  if (!aCStr || !aList)
    return 0;
  while (skip > 0)
  {
    mailAddress *aAddress = new mailAddress;
    skip = aAddress->parseAddress (aCStr);
    if (skip)
    {
      aCStr += skip;
      if (skip < 0)
        advance -= skip;
      else
        advance += skip;
      aList->append (aAddress);
    }
    else
    {
      delete aAddress;
      break;
    }
  }
  return advance;
}

TQCString
mailHeader::getAddressStr (TQPtrList < mailAddress > *aList)
{
  TQCString retVal;

  TQPtrListIterator < mailAddress > it = TQPtrListIterator < mailAddress > (*aList);
  while (it.current ())
  {
    retVal += it.current ()->getStr ();
    ++it;
    if (it.current ())
      retVal += ", ";
  }
  return retVal;
}