summaryrefslogtreecommitdiffstats
path: root/libkmime/kmime_mdn.cpp
blob: 702f5f8111fef642de4a86fbc97ff51bd31762f4 (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
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
/*  -*- c++ -*-
    kmime_mdn.cpp

    This file is part of KMime, the KDE internet mail/usenet news message library.
    Copyright (c) 2002 Marc Mutz <mutz@kde.org>

    KMime is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License, version 2, as
    published by the Free Software Foundation.

    KMime is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this library with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#include "kmime_mdn.h"

#include "kmime_version.h"
#include "kmime_util.h"

#include <klocale.h>
#include <kdebug.h>

#include <tqcstring.h>

#include <unistd.h> // gethostname

namespace KMime {

namespace MDN {

  static const struct {
    DispositionType dispositionType;
    const char * string;
    const char * description;
  } dispositionTypes[] = {
    { Displayed, "displayed",
      I18N_NOOP("The message sent on ${date} to ${to} with subject "
		"\"${subject}\" has been displayed. This is no guarantee that "
		"the message has been read or understood.") },
    { Deleted, "deleted",
      I18N_NOOP("The message sent on ${date} to ${to} with subject "
		"\"${subject}\" has been deleted unseen. This is no guarantee "
		"that the message will not be \"undeleted\" and nonetheless "
		"read later on.") },
    { Dispatched, "dispatched",
      I18N_NOOP("The message sent on ${date} to ${to} with subject "
		"\"${subject}\" has been dispatched. This is no guarantee "
		"that the message will not be read later on.") },
    { Processed, "processed",
      I18N_NOOP("The message sent on ${date} to ${to} with subject "
		"\"${subject}\" has been processed by some automatic means.") },
    { Denied, "denied",
      I18N_NOOP("The message sent on ${date} to ${to} with subject "
		"\"${subject}\" has been acted upon. The sender does not wish "
		"to disclose more details to you than that.") },
    { Failed, "failed",
      I18N_NOOP("Generation of a Message Disposition Notification for the "
		"message sent on ${date} to ${to} with subject \"${subject}\" "
		"failed. Reason is given in the Failure: header field below.") }
  };

  static const int numDispositionTypes
           = sizeof dispositionTypes / sizeof *dispositionTypes;


  static const char * stringFor( DispositionType d ) {
    for ( int i = 0 ; i < numDispositionTypes ; ++i )
      if ( dispositionTypes[i].dispositionType == d )
	return dispositionTypes[i].string;
    return 0;
  }


  //
  // disposition-modifier
  //
  static const struct {
    DispositionModifier dispositionModifier;
    const char * string;
  } dispositionModifiers[] = {
    { Error, "error" },
    { Warning, "warning" },
    { Superseded, "superseded" },
    { Expired, "expired" },
    { MailboxTerminated, "mailbox-terminated" }
  };

  static const int numDispositionModifiers
       = sizeof dispositionModifiers / sizeof * dispositionModifiers;


  static const char * stringFor( DispositionModifier m ) {
    for ( int i = 0 ; i < numDispositionModifiers ; ++i )
      if ( dispositionModifiers[i].dispositionModifier == m )
	return dispositionModifiers[i].string;
    return 0;
  }

  //
  // action-mode (part of disposition-mode)
  //

  static const struct {
    ActionMode actionMode;
    const char * string;
  } actionModes[] = {
    { ManualAction, "manual-action" },
    { AutomaticAction, "automatic-action" }
  };

  static const int numActionModes = sizeof actionModes / sizeof *actionModes;

  static const char * stringFor( ActionMode a ) {
    for ( int i = 0 ; i < numActionModes ; ++i )
      if ( actionModes[i].actionMode == a )
	return actionModes[i].string;
    return 0;
  }


  //
  // sending-mode (part of disposition-mode)
  //

  static const struct {
    SendingMode sendingMode;
    const char * string;
  } sendingModes[] = {
    { SentManually, "MDN-sent-manually" },
    { SentAutomatically, "MDN-sent-automatically" }
  };

  static const int numSendingModes = sizeof sendingModes / sizeof *sendingModes;

  static const char * stringFor( SendingMode s ) {
    for ( int i = 0 ; i < numSendingModes ; ++i )
      if ( sendingModes[i].sendingMode == s )
	return sendingModes[i].string;
    return 0;
  }

  static TQCString dispositionField( DispositionType d, ActionMode a, SendingMode s,
				    const TQValueList<DispositionModifier> & m ) {

    // mandatory parts: Disposition: foo/baz; bar
    TQCString result = "Disposition: ";
    result += stringFor( a );
    result += "/";
    result += stringFor( s );
    result += "; ";
    result += stringFor( d );

    // optional parts: Disposition: foo/baz; bar/mod1,mod2,mod3
    bool first = true;
    for ( TQValueList<DispositionModifier>::const_iterator mt = m.begin() ;
	  mt != m.end() ; ++mt ) {
      if ( first ) {
	result += "/";
	first = false;
      } else {
	result += ",";
      }
      result += stringFor( *mt );
    }
    return result + "\n";
  }

  static TQCString finalRecipient( const TQString & recipient ) {
    if ( recipient.isEmpty() )
      return TQCString();
    else
      return "Final-Recipient: rfc822; "
	+ encodeRFC2047String( recipient, "utf-8" ) + "\n";
  }

  static TQCString orginalRecipient( const TQCString & recipient ) {
    if ( recipient.isEmpty() )
      return TQCString();
    else
      return "Original-Recipient: " + recipient + "\n";
  }

  static TQCString originalMessageID( const TQCString & msgid ) {
    if ( msgid.isEmpty() )
      return TQCString();
    else
      return "Original-Message-ID: " + msgid + "\n";
  }

  static TQCString reportingUAField() {
    char hostName[256];
    if ( gethostname( hostName, 255 ) )
      hostName[0] = '\0'; // gethostname failed: pretend empty string
    else
      hostName[255] = '\0'; // gethostname may have returned 255 chars (man page)
    return TQCString("Reporting-UA: ") + hostName
      + "; KMime " KMIME_VERSION_STRING "\n";
  }

  TQCString dispositionNotificationBodyContent( const TQString & r,
					       const TQCString & o,
					       const TQCString & omid,
					       DispositionType d,
					       ActionMode a,
					       SendingMode s,
					       const TQValueList<DispositionModifier> & m,
					       const TQString & special )
  {
    // in Perl: chomp(special) 
    TQString spec;
    if ( special.endsWith("\n") )
      spec = special.left( special.length() - 1 );
    else
      spec = special;

    // std headers:
    TQCString result = reportingUAField();
    result += orginalRecipient( o );
    result += finalRecipient( r );
    result += originalMessageID( omid );
    result += dispositionField( d, a, s, m );

    // headers that are only present for certain disposition {types,modifiers}:
    if ( d == Failed )
      result += "Failure: " + encodeRFC2047String( spec, "utf-8" ) + "\n";
    else if ( m.tqcontains( Error ) )
      result += "Error: " + encodeRFC2047String( spec, "utf-8" ) + "\n";
    else if ( m.tqcontains( Warning ) )
      result += "Warning: " + encodeRFC2047String( spec, "utf-8" ) + "\n";

    return result;
  }

  TQString descriptionFor( DispositionType d,
			  const TQValueList<DispositionModifier> & ) {
    for ( int i = 0 ; i < numDispositionTypes ; ++i )
      if ( dispositionTypes[i].dispositionType == d )
	return i18n( dispositionTypes[i].description );
    kdWarning() << "KMime::MDN::descriptionFor(): No such disposition type: "
		<< (int)d << endl;
    return TQString::null;
  }

} // namespace MDN
} // namespace KMime