summaryrefslogtreecommitdiffstats
path: root/kresources/scalix/kioslave/scalix.cpp
blob: 2a0c06cddf7098540578dfb689990293c6d310d7 (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
/*
    This file is part of KDE.

    Copyright (C) 2007 Trolltech ASA. All rights reserved.

    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 <tqapplication.h>
#include <tqeventloop.h>

#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kdebug.h>
#include <kdeversion.h>
#include <kio/global.h>
#include <klocale.h>

#include <kdepimmacros.h>

#include <stdlib.h>

#include "scalix.h"

extern "C" {
  KDE_EXPORT int kdemain( int argc, char **argv );
}

static const KCmdLineOptions options[] =
{
  { "+protocol", I18N_NOOP( "Protocol name" ), 0 },
  { "+pool", I18N_NOOP( "Socket name" ), 0 },
  { "+app", I18N_NOOP( "Socket name" ), 0 },
  KCmdLineLastOption
};

int kdemain( int argc, char **argv )
{
  putenv( strdup( "SESSION_MANAGER=" ) );
  KApplication::disableAutoDcopRegistration();

  KCmdLineArgs::init( argc, argv, "kio_scalix", 0, 0, 0, 0 );
  KCmdLineArgs::addCmdLineOptions( options );
  KApplication app( false, false );

  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
  Scalix slave( args->arg( 0 ), args->arg( 1 ), args->arg( 2 ) );
  slave.dispatchLoop();

  return 0;
}

Scalix::Scalix( const TQCString &protocol, const TQCString &pool, const TQCString &app )
  : SlaveBase( protocol, pool, app )
{
}

void Scalix::get( const KURL &url )
{
  mimeType( "text/plain" );

  TQString path = url.path();

  if ( path.tqcontains( "/freebusy/" ) ) {
    retrieveFreeBusy( url );
  } else {
    error( KIO::ERR_SLAVE_DEFINED, i18n( "Unknown path. Known path is '/freebusy/'" ) );
  }
}

void Scalix::put( const KURL& url, int, bool, bool )
{
  TQString path = url.path();

  if ( path.tqcontains( "/freebusy/" ) ) {
    publishFreeBusy( url );
  } else {
    error( KIO::ERR_SLAVE_DEFINED, i18n( "Unknown path. Known path is '/freebusy/'" ) );
  }
}

void Scalix::retrieveFreeBusy( const KURL &url )
{
  /**
   * The url is of the following form:
   *  scalix://user:password@host/freebusy/user@domain.ifb
   */

  // Extract user@domain (e.g. everything between '/freebusy/' and '.ifb')
  const TQString requestUser = url.path().mid( 10, url.path().length() - 14 );

  TQByteArray packedArgs;
  TQDataStream stream( packedArgs, IO_WriteOnly );

  const TQString argument = TQString( "BEGIN:VFREEBUSY\nATTENDEE:MAILTO:%1\nEND:VFREEBUSY" ).arg( requestUser );
  const TQString command = TQString( "X-GET-ICAL-FREEBUSY {%1}" ).arg( argument.length() );

  stream << (int) 'X' << 'E' << command << argument;

  TQString imapUrl = TQString( "imap://%1@%3/" ).arg( url.pass().isEmpty() ?
                                                   url.user() : url.user() + ":" + url.pass() )
                                                .arg( url.host() );

  mFreeBusyData = TQString();

  KIO::SimpleJob *job = KIO::special( imapUrl, packedArgs, false );
  connect( job, TQT_SIGNAL( infoMessage( KIO::Job*, const TQString& ) ),
           this, TQT_SLOT( slotInfoMessage( KIO::Job*, const TQString& ) ) );
  connect( job, TQT_SIGNAL( result( KIO::Job* ) ),
           this, TQT_SLOT( slotRetrieveResult( KIO::Job* ) ) );

  tqApp->eventLoop()->enterLoop();
}

void Scalix::publishFreeBusy( const KURL &url )
{
  /**
   * The url is of the following form:
   *  scalix://user:password@host/freebusy/path/to/calendar/user@domain
   */
  TQString requestUser, calendar;
  TQString path = url.path();

  // extract user name
  int lastSlash = path.findRev( '/' );
  if ( lastSlash != -1 )
    requestUser = path.mid( lastSlash + 1 );

  // extract calendar name
  int secondSlash = path.find( '/', 1 );
  if ( secondSlash != -1 )
    calendar = path.mid( secondSlash + 1, lastSlash - secondSlash - 1 );

  if ( requestUser.isEmpty() || calendar.isEmpty() ) {
    error( KIO::ERR_SLAVE_DEFINED, i18n( "No user or calendar given!" ) );
    return;
  };

  // read freebusy information
  TQByteArray data;
  while ( true ) {
    dataReq();

    TQByteArray buffer;
    const int newSize = readData(buffer);
    if ( newSize < 0 ) {
      // read error: network in unknown state so disconnect
      error( KIO::ERR_COULD_NOT_READ, i18n("KIO data supply error.") );
      return;
    }

    if ( newSize == 0 )
      break;

    unsigned int oldSize = data.size();
    data.resize( oldSize + buffer.size() );
    memcpy( data.data() + oldSize, buffer.data(), buffer.size() );
  }

  TQByteArray packedArgs;
  TQDataStream stream( packedArgs, IO_WriteOnly );

  const TQString argument = TQString::fromUtf8( data );
  const TQString command = TQString( "X-PUT-ICAL-FREEBUSY Calendar {%1}" ).arg( argument.length() );

  stream << (int) 'X' << 'E' << command << argument;

  TQString imapUrl = TQString( "imap://%1@%3/" ).arg( url.pass().isEmpty() ?
                                                   url.user() : url.user() + ":" + url.pass() )
                                                .arg( url.host() );

  KIO::SimpleJob *job = KIO::special( imapUrl, packedArgs, false );
  connect( job, TQT_SIGNAL( result( KIO::Job* ) ),
           this, TQT_SLOT( slotPublishResult( KIO::Job* ) ) );

  tqApp->eventLoop()->enterLoop();
}

void Scalix::slotInfoMessage( KIO::Job *job, const TQString &data )
{
  if ( job->error() ) {
    // error is handled in slotResult
    return;
  }

  mFreeBusyData = data;
}


void Scalix::slotRetrieveResult( KIO::Job *job )
{
  if ( job->error() ) {
    error( KIO::ERR_SLAVE_DEFINED, job->errorString() );
  } else {
    data( mFreeBusyData.utf8() );
    finished();
  }

  tqApp->eventLoop()->exitLoop();
}

void Scalix::slotPublishResult( KIO::Job *job )
{
  if ( job->error() ) {
    error( KIO::ERR_SLAVE_DEFINED, job->errorString() );
  } else {
    finished();
  }

  tqApp->eventLoop()->exitLoop();
}

#include "scalix.moc"