summaryrefslogtreecommitdiffstats
path: root/korn/tdeio_single_subject.cpp
blob: 022dac49378a8f939443d4009e16cb31d1bea3f6 (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
/*
 * Copyright (C) 2004, Mart Kelder (mart.kde@hccnet.nl)
 *
 * 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.
 *
 * This program 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 program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "tdeio_single_subject.h"

#include "mailsubject.h"
#include "tdeio_proto.h"
#include "stringid.h"

#include <kdebug.h>
#include <tdelocale.h>
#include <kurl.h>
#include <tdeio/global.h>
#include <tdeio/scheduler.h>
#include <tdeio/job.h>
#include <tdeio/jobclasses.h>
#include <krfcdate.h>

#include <tqregexp.h>
#include <tqcstring.h>
#include <tqstring.h>

KIO_Single_Subject::KIO_Single_Subject( TQObject * parent, const char * name,
		    KURL &kurl, TDEIO::MetaData &metadata, const KIO_Protocol * protocol, TDEIO::Slave *& slave,
		    const TQString &url, const long size ) 
		: TQObject( parent, name )
{
	_kurl = new KURL( kurl );
	_metadata = new TDEIO::MetaData( metadata );
	_protocol = protocol;
	_name = new TQString( url );
	_size = size;
	_message = new TQString;
	
	init( slave );
}

KIO_Single_Subject::~KIO_Single_Subject( )
{
	if( _job )
		TDEIO::Scheduler::cancelJob( _job );
	delete _kurl;
	delete _metadata;
	delete _name;
	delete _message;
}

void KIO_Single_Subject::init( TDEIO::Slave *& slave)
{
	_job = TDEIO::get( *_kurl, false, false );
	_job->addMetaData( *_metadata );
	
	connect( _job, TQT_SIGNAL( result( TDEIO::Job* ) ), this, TQT_SLOT( slotResult( TDEIO::Job* ) ) );
	connect( _job, TQT_SIGNAL( data         (TDEIO::Job *, const TQByteArray &) ),
	         this, TQT_SLOT( slotData(TDEIO::Job *, const TQByteArray &) ) );
	
	if( _protocol->connectionBased( ) && slave )
		TDEIO::Scheduler::assignJobToSlave( slave , _job );
	else
		TDEIO::Scheduler::scheduleJob( _job );
		 
}

void KIO_Single_Subject::parseMail( TQString * message, KornMailSubject *subject, bool fullMessage )
{
	TQTextStream stream( message, IO_ReadOnly );
	TQString line;
	TQRegExp rx_sender( "^[fF]rom: " ); //Ex: From: ...
	TQRegExp rx_sender_has_name1( "^[fF]rom:\\s*(\\w+[\\w\\s]*)\\<" ); //Ex: From: A name<email@domein.country>
	TQRegExp rx_sender_has_name2( "^[fF]rom:\\s*\\\"\\s*(\\w+[\\w\\s]*)\\\""); //Ex: From: "A name"<a@invalid>
	TQRegExp rx_subject( "^[sS]ubject: " ); //Ex: Subject: ...
	TQRegExp rx_date  ( "^[dD]ate: ");
	bool inheader = true;
	int fieldnumber = 0;

	while ( ! stream.atEnd() )
	{
		line = stream.readLine();
		if( line.isEmpty() && fieldnumber >= 2 )
			inheader = false;
		
		if( inheader )
		{
			if( rx_sender.search( line ) == 0 )
			{
				if( rx_sender_has_name1.search( line ) == 0 )
					subject->setSender( rx_sender_has_name1.cap( 1 )  );
				else if(rx_sender_has_name2.search( line ) == 0)
					subject->setSender( rx_sender_has_name2.cap( 1 ) );
				else
					subject->setSender( line.remove( rx_sender ) );
				++fieldnumber;
			}
			else if( rx_subject.search( line ) == 0 )
			{
				subject->setSubject( line.remove( rx_subject ) );
				++fieldnumber;
			}
			else if( rx_date.search( line ) == 0 )
			{
				subject->setDate( KRFCDate::parseDate( line.right( line.length() - 6 ) ) );
				++fieldnumber;
			}
		}
	}

	subject->setHeader( *message, fullMessage );
}

void KIO_Single_Subject::slotData( TDEIO::Job* job, const TQByteArray& data )
{
	if( job != _job )
		kdWarning() << i18n( "Got invalid job; something strange happened?" ) << endl;
	if( !data.isEmpty() )
		_message->append( data );
}

//TDEIO::Scheduler::disconnectSlave missing  if connection stops
void KIO_Single_Subject::slotResult( TDEIO::Job *job )
{
	if( job != _job )
		kdWarning() << i18n( "Got invalid job; something strange happened?" ) << endl;
		
	if( job->error() )
	{
		kdWarning() << i18n( "Error when fetching %1: %2" ).arg( *_name ).arg( job->errorString() ) << endl;
	} else {
		KornMailSubject * mailSubject = new KornMailSubject( new KornStringId( *_name ), 0 );
		parseMail( _message, mailSubject, _protocol->fullMessage() );
		mailSubject->decodeHeaders();
		mailSubject->setSize( _size );
		emit readSubject( mailSubject );
	}
	
	_job = 0;
	
	emit finished( this );
}

#include "tdeio_single_subject.moc"