~ubuntu-branches/ubuntu/saucy/kopete/saucy-proposed

« back to all changes in this revision

Viewing changes to protocols/irc/irccontact_user.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-06-21 02:22:39 UTC
  • Revision ID: package-import@ubuntu.com-20130621022239-63l3zc8p0nf26pt6
Tags: upstream-4.10.80
ImportĀ upstreamĀ versionĀ 4.10.80

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    ircusercontact.cpp - IRC User Contact
 
3
 
 
4
    Copyright (c) 2002      by Nick Betcher <nbetcher@kde.org>
 
5
 
 
6
    Kopete    (c) 2002      by the Kopete developers <kopete-devel@kde.org>
 
7
 
 
8
    *************************************************************************
 
9
    *                                                                       *
 
10
    * This program is free software; you can redistribute it and/or modify  *
 
11
    * it under the terms of the GNU General Public License as published by  *
 
12
    * the Free Software Foundation; either version 2 of the License, or     *
 
13
    * (at your option) any later version.                                   *
 
14
    *                                                                       *
 
15
    *************************************************************************
 
16
*/
 
17
 
 
18
#include "ircaccount.h"
 
19
#include "irccontact.h"
 
20
#include "ircprotocol.h"
 
21
 
 
22
#include "kopetemetacontact.h"
 
23
#include "kopeteview.h"
 
24
 
 
25
#include <kaction.h>
 
26
#include <kdebug.h>
 
27
#include <kfiledialog.h>
 
28
#include <klocale.h>
 
29
 
 
30
#include <qtimer.h>
 
31
/*
 
32
QString IRCContact::user_caption() const
 
33
{
 
34
        return i18n("%1 @ %2").arg(m_nickName).arg(kircEngine()->currentHost());
 
35
}
 
36
 
 
37
void IRCContact::user_updateStatus()
 
38
{
 
39
        Kopete::OnlineStatus newStatus;
 
40
 
 
41
        switch (kircEngine()->connectionState())
 
42
        {
 
43
        case KIRC::Idle:
 
44
                newStatus = m_protocol->m_UserStatusOffline;
 
45
                break;
 
46
 
 
47
        case KIRC::Connecting:
 
48
        case KIRC::Authentifying:
 
49
                if (this == ircAccount()->mySelf())
 
50
                        newStatus = m_protocol->m_UserStatusConnecting;
 
51
                else
 
52
                        newStatus = m_protocol->m_UserStatusOffline;
 
53
                break;
 
54
 
 
55
        case KIRC::Connected:
 
56
//              if (m_isAway)
 
57
//                      newStatus = m_protocol->m_UserStatusAway;
 
58
//              else if (m_isOnline)
 
59
                        newStatus = m_protocol->m_UserStatusOnline;
 
60
                break;
 
61
        case KIRC::Closing:
 
62
                newStatus = m_protocol->m_UserStatusOffline;
 
63
                break;
 
64
        default:
 
65
                newStatus = m_protocol->m_StatusUnknown;
 
66
        }
 
67
 
 
68
        // This may not be created yet ( for myself() on startup )
 
69
        if( ircAccount()->contactManager() )
 
70
        {
 
71
                QValueList<IRCChannelContact*> channels = ircAccount()->contactManager()->findChannelsByMember(this);
 
72
 
 
73
                for( QValueList<IRCChannelContact*>::iterator it = channels.begin(); it != channels.end(); ++it )
 
74
                {
 
75
                        IRCChannelContact *channel = *it;
 
76
                        Kopete::OnlineStatus currentStatus = channel->manager()->contactOnlineStatus(this);
 
77
 
 
78
                        if( currentStatus.internalStatus() > IRCProtocol::Online )
 
79
                        {
 
80
                                if( !(currentStatus.internalStatus() & IRCProtocol::Away) && newStatus == m_protocol->m_UserStatusAway )
 
81
                                {
 
82
                                        channel->manager()->setContactOnlineStatus(
 
83
                                                this, m_protocol->statusLookup(
 
84
                                                        (IRCProtocol::IRCStatus)(currentStatus.internalStatus()+IRCProtocol::Away)
 
85
                                                )
 
86
                                        );
 
87
                                }
 
88
                                else if( (currentStatus.internalStatus() & IRCProtocol::Away) && newStatus == m_protocol->m_UserStatusOnline )
 
89
                                {
 
90
                                        channel->manager()->setContactOnlineStatus(
 
91
                                                this, m_protocol->statusLookup(
 
92
                                                        (IRCProtocol::IRCStatus)(currentStatus.internalStatus()-IRCProtocol::Away)
 
93
                                                )
 
94
                                        );
 
95
                                }
 
96
                                else if( newStatus.internalStatus() < IRCProtocol::Away )
 
97
                                {
 
98
                                        channel->manager()->setContactOnlineStatus( this, newStatus );
 
99
                                }
 
100
                        }
 
101
                }
 
102
        }
 
103
        setOnlineStatus( newStatus );
 
104
}
 
105
 
 
106
IRCUserContact::IRCUserContact(IRCContactManager *contactManager, const QString &nickname, Kopete::MetaContact *m )
 
107
        : IRCContact(contactManager, nickname, m ),
 
108
          m_isAway(false)
 
109
{
 
110
        setFileCapable(true);
 
111
 
 
112
        mOnlineTimer = new QTimer( this );
 
113
        m_isOnline = metaContact()->isTemporary();
 
114
 
 
115
        QObject::connect(mOnlineTimer, SIGNAL(timeout()), this, SLOT(slotUserOffline()) );
 
116
 
 
117
        QObject::connect(kircEngine(), SIGNAL(incomingChannelModeChange(QString,QString,QString)),
 
118
                this, SLOT(slotIncomingModeChange(QString,QString,QString)));
 
119
 
 
120
        actionCtcpMenu = 0L;
 
121
 
 
122
        mInfo.isOperator = false;
 
123
        mInfo.isIdentified = false;
 
124
        mInfo.idle = 0;
 
125
        mInfo.hops = 0;
 
126
        mInfo.away = false;
 
127
 
 
128
        updateStatus();
 
129
}
 
130
 
 
131
void IRCUserContact::sendFile(const KUrl &sourceURL, const QString&, unsigned int)
 
132
{
 
133
        QString filePath;
 
134
 
 
135
        //If the file location is null, then get it from a file open dialog
 
136
        if( !sourceURL.isValid() )
 
137
                filePath = KFileDialog::getOpenFileName(QString::null, "*", 0l  , i18n("Kopete File Transfer"));        //krazy:exclude=nullstrassign for old broken gcc
 
138
        else
 
139
                filePath = sourceURL.path(-1);
 
140
 
 
141
        kDebug(14120) << "File chosen to send:" << filePath;
 
142
 
 
143
        if (!filePath.isEmpty())
 
144
                kircEngine()->CtcpRequest_dcc( m_nickName, filePath, 0, KIRC::Transfer::FileOutgoing);
 
145
}
 
146
 
 
147
void IRCUserContact::slotUserOffline()
 
148
{
 
149
        m_isOnline = false;
 
150
        m_isAway = false;
 
151
        updateStatus();
 
152
 
 
153
        if( !metaContact()->isTemporary() )
 
154
                kircEngine()->writeMessage( QString::fromLatin1("WHOWAS %1").arg(m_nickName) );
 
155
 
 
156
        removeProperty( m_protocol->propUserInfo );
 
157
        removeProperty( m_protocol->propServer );
 
158
        removeProperty( m_protocol->propChannels );
 
159
}
 
160
 
 
161
void IRCUserContact::setAway(bool isAway)
 
162
{
 
163
        m_isAway = isAway;
 
164
        updateStatus();
 
165
}
 
166
 
 
167
void IRCUserContact::incomingUserIsAway(const QString &reason)
 
168
{
 
169
        if( manager( Kopete::Contact::CannotCreate ) )
 
170
        {
 
171
                Kopete::Message msg( (Kopete::Contact*)ircAccount()->myServer(), mMyself,
 
172
                        i18n("%1 is away (%2)").arg( m_nickName ).arg( reason ),
 
173
                        Kopete::Message::Internal, Kopete::Message::RichText, CHAT_VIEW );
 
174
                manager(Kopete::Contact::CanCreate)->appendMessage(msg);
 
175
        }
 
176
}
 
177
 
 
178
void IRCUserContact::userOnline()
 
179
{
 
180
        m_isOnline = true;
 
181
        updateStatus();
 
182
        if (this != ircAccount()->mySelf() && !metaContact()->isTemporary() && ircAccount()->isConnected())
 
183
        {
 
184
                mOnlineTimer->start( 45000, true );
 
185
                ircAccount()->setCurrentCommandSource(0);
 
186
                kircEngine()->whois(m_nickName);
 
187
        }
 
188
 
 
189
        removeProperty( m_protocol->propLastSeen );
 
190
}
 
191
 
 
192
void IRCUserContact::slotUserInfo()
 
193
{
 
194
        if (isChatting())
 
195
        {
 
196
                ircAccount()->setCurrentCommandSource(manager());
 
197
                kircEngine()->whois(m_nickName);
 
198
        }
 
199
}
 
200
 
 
201
void IRCUserContact::slotOp()
 
202
{
 
203
        contactMode( QString::fromLatin1("+o") );
 
204
}
 
205
 
 
206
void IRCUserContact::slotDeop()
 
207
{
 
208
        contactMode( QString::fromLatin1("-o") );
 
209
}
 
210
 
 
211
void IRCUserContact::slotVoice()
 
212
{
 
213
        contactMode( QString::fromLatin1("+v") );
 
214
}
 
215
 
 
216
void IRCUserContact::slotDevoice()
 
217
{
 
218
        contactMode( QString::fromLatin1("-v") );
 
219
}
 
220
 
 
221
void IRCUserContact::slotBanHost()
 
222
{
 
223
        slotKick();
 
224
}
 
225
 
 
226
void IRCUserContact::slotBanUserHost()
 
227
{
 
228
        slotKick();
 
229
}
 
230
 
 
231
void IRCUserContact::slotBanDomain()
 
232
{
 
233
        slotKick();
 
234
}
 
235
 
 
236
void IRCUserContact::slotBanUserDomain()
 
237
{
 
238
        slotKick();
 
239
}
 
240
 
 
241
void IRCUserContact::slotKick()
 
242
{
 
243
        Kopete::ContactPtrList members = mActiveManager->members();
 
244
        QString channelName = static_cast<IRCContact*>(members.first())->nickName();
 
245
        kircEngine()->kick(m_nickName, channelName, QString());
 
246
}
 
247
 
 
248
void IRCUserContact::contactMode(const QString &mode)
 
249
{
 
250
        Kopete::ContactPtrList members = mActiveManager->members();
 
251
        QString channelName = static_cast<IRCContact*>(members.first())->nickName();
 
252
        kircEngine()->mode(channelName, QString::fromLatin1("%1 %2").arg(mode).arg(m_nickName));
 
253
}
 
254
 
 
255
void IRCUserContact::slotCtcpPing()
 
256
{
 
257
        kircEngine()->CtcpRequest_ping(m_nickName);
 
258
}
 
259
 
 
260
void IRCUserContact::slotCtcpVersion()
 
261
{
 
262
        kircEngine()->CtcpRequest_version(m_nickName);
 
263
}
 
264
 
 
265
void IRCUserContact::newWhoIsUser(const QString &username, const QString &hostname, const QString &realname)
 
266
{
 
267
        mInfo.channels.clear();
 
268
        mInfo.userName = username;
 
269
        mInfo.hostName = hostname;
 
270
        mInfo.realName = realname;
 
271
 
 
272
        if( onlineStatus().status() == Kopete::OnlineStatus::Offline )
 
273
        {
 
274
                setProperty( m_protocol->propUserInfo, QString::fromLatin1("%1@%2")
 
275
                        .arg(mInfo.userName).arg(mInfo.hostName) );
 
276
                setProperty( m_protocol->propServer, mInfo.serverName );
 
277
                setProperty( m_protocol->propFullName, mInfo.realName );
 
278
        }
 
279
}
 
280
 
 
281
void IRCUserContact::newWhoIsServer(const QString &servername, const QString &serverinfo)
 
282
{
 
283
//      if( metaContact()->isTemporary() || isOnline() )
 
284
        if( metaContact()->isTemporary() || onlineStatus().status() != Kopete::OnlineStatus::Offline )
 
285
                mInfo.serverInfo = serverinfo;
 
286
        else
 
287
        {
 
288
                // Try to convert first, since server can return depending if
 
289
                // user is online or not:
 
290
                //
 
291
                //   312 mynick othernick localhost.localdomain :FooNet Server
 
292
                //   312 mynick othernick localhost.localdomain :Thu Jun 16 21:00:36 2005
 
293
 
 
294
                QDateTime lastSeen = QDateTime::fromString( serverinfo );
 
295
                if( lastSeen.isValid() )
 
296
                        setProperty( m_protocol->propLastSeen, lastSeen );
 
297
        }
 
298
}
 
299
 
 
300
void IRCUserContact::newWhoIsIdle(unsigned long idle)
 
301
{
 
302
        mInfo.idle = idle;
 
303
}
 
304
 
 
305
void IRCUserContact::newWhoIsOperator()
 
306
{
 
307
        mInfo.isOperator = true;
 
308
}
 
309
 
 
310
void IRCUserContact::newWhoIsIdentified()
 
311
{
 
312
        mInfo.isIdentified = true;
 
313
        setProperty( m_protocol->propIsIdentified, i18n("True") );
 
314
}
 
315
 
 
316
void IRCUserContact::newWhoIsChannels(const QString &channel)
 
317
{
 
318
        mInfo.channels.append( channel );
 
319
}
 
320
 
 
321
void IRCUserContact::whoIsComplete()
 
322
{
 
323
        Kopete::ChatSession *commandSource = ircAccount()->currentCommandSource();
 
324
 
 
325
        updateInfo();
 
326
 
 
327
        if( isChatting() && commandSource &&
 
328
                commandSource == manager(Kopete::Contact::CannotCreate) )
 
329
        {
 
330
                //User info
 
331
                QString msg = i18n("%1 is (%2@%3): %4<br/>")
 
332
                        .arg(m_nickName)
 
333
                        .arg(mInfo.userName)
 
334
                        .arg(mInfo.hostName)
 
335
                        .arg(mInfo.realName);
 
336
 
 
337
                if( mInfo.isIdentified )
 
338
                        msg += i18n("%1 is authenticated with NICKSERV<br/>").arg(m_nickName);
 
339
 
 
340
                if( mInfo.isOperator )
 
341
                        msg += i18n("%1 is an IRC operator<br/>").arg(m_nickName);
 
342
 
 
343
                //Channels
 
344
                msg += i18n("on channels %1<br/>").arg(mInfo.channels.join(" ; "));
 
345
 
 
346
                //Server
 
347
                msg += i18n("on IRC via server %1 ( %2 )<br/>").arg(mInfo.serverName).arg(mInfo.serverInfo);
 
348
 
 
349
                //Idle
 
350
                QString idleTime = formattedIdleTime();
 
351
                msg += i18n("idle: %2<br/>").arg( idleTime.isEmpty() ? QString::number(0) : idleTime );
 
352
 
 
353
                //End
 
354
                ircAccount()->appendMessage(msg, IRCAccount::InfoReply );
 
355
                ircAccount()->setCurrentCommandSource(0);
 
356
        }
 
357
}
 
358
 
 
359
void IRCUserContact::whoWasComplete()
 
360
{
 
361
        if( isChatting() && ircAccount()->currentCommandSource() == manager() )
 
362
        {
 
363
                //User info
 
364
                QString msg = i18n("%1 was (%2@%3): %4\n")
 
365
                        .arg(m_nickName)
 
366
                        .arg(mInfo.userName)
 
367
                        .arg(mInfo.hostName)
 
368
                        .arg(mInfo.realName);
 
369
 
 
370
                msg += i18n("Last Online: %1\n").arg(
 
371
                        KGlobal::locale()->formatDateTime(
 
372
                                property( m_protocol->propLastSeen ).value().toDateTime()
 
373
                        )
 
374
                );
 
375
 
 
376
                ircAccount()->appendMessage(msg, IRCAccount::InfoReply );
 
377
                ircAccount()->setCurrentCommandSource(0);
 
378
        }
 
379
}
 
380
 
 
381
QString IRCUserContact::formattedName() const
 
382
{
 
383
        return mInfo.realName;
 
384
}
 
385
 
 
386
void IRCUserContact::updateInfo()
 
387
{
 
388
        setProperty( m_protocol->propUserInfo, QString::fromLatin1("%1@%2")
 
389
                .arg(mInfo.userName).arg(mInfo.hostName) );
 
390
        setProperty( m_protocol->propServer, mInfo.serverName );
 
391
        setProperty( m_protocol->propChannels, mInfo.channels.join(" ") );
 
392
        setProperty( m_protocol->propHops, QString::number(mInfo.hops) );
 
393
        setProperty( m_protocol->propFullName, mInfo.realName );
 
394
 
 
395
        setIdleTime( mInfo.idle );
 
396
 
 
397
        mInfo.lastUpdate = QTime::currentTime();
 
398
}
 
399
 
 
400
void IRCUserContact::newWhoReply( const QString &channel, const QString &user, const QString &host,
 
401
        const QString &server, bool away, const QString &flags, uint hops, const QString &realName )
 
402
{
 
403
        if( !mInfo.channels.contains( channel ) )
 
404
                mInfo.channels.append( channel );
 
405
 
 
406
        mInfo.userName = user;
 
407
        mInfo.hostName = host;
 
408
        mInfo.serverName = server;
 
409
        mInfo.flags = flags;
 
410
        mInfo.hops = hops;
 
411
        mInfo.realName = realName;
 
412
 
 
413
        setAway(away);
 
414
 
 
415
        updateInfo();
 
416
 
 
417
        if( isChatting() && ircAccount()->currentCommandSource() == manager() )
 
418
        {
 
419
                ircAccount()->setCurrentCommandSource(0);
 
420
        }
 
421
}
 
422
 
 
423
QPtrList<KAction> *IRCUserContact::customContextMenuActions( Kopete::ChatSession *manager )
 
424
{
 
425
        if( manager )
 
426
        {
 
427
                QPtrList<KAction> *mCustomActions = new QPtrList<KAction> ();
 
428
                mActiveManager = manager;
 
429
                Kopete::ContactPtrList members = mActiveManager->members();
 
430
                IRCChannelContact *isChannel = dynamic_cast<IRCChannelContact*>( members.first() );
 
431
 
 
432
                if( !actionCtcpMenu )
 
433
                {
 
434
                        actionCtcpMenu = new KActionMenu(i18n("C&TCP"), 0, this );
 
435
                        actionCtcpMenu->insert( new KAction(i18n("&Version"), 0, this,
 
436
                                SLOT(slotCtcpVersion()), actionCtcpMenu) );
 
437
                        actionCtcpMenu->insert(  new KAction(i18n("&Ping"), 0, this,
 
438
                                SLOT(slotCtcpPing()), actionCtcpMenu) );
 
439
 
 
440
                        actionModeMenu = new KActionMenu(i18n("&Modes"), 0, this, "actionModeMenu");
 
441
                        actionModeMenu->insert( new KAction(i18n("&Op"), 0, this,
 
442
                                SLOT(slotOp()), actionModeMenu, "actionOp") );
 
443
                        actionModeMenu->insert( new KAction(i18n("&Deop"), 0, this,
 
444
                                SLOT(slotDeop()), actionModeMenu, "actionDeop") );
 
445
                        actionModeMenu->insert( new KAction(i18n("&Voice"), 0, this,
 
446
                                SLOT(slotVoice()), actionModeMenu, "actionVoice") );
 
447
                        actionModeMenu->insert( new KAction(i18n("Devoice"), 0, this,
 
448
                                SLOT(slotDevoice()), actionModeMenu, "actionDevoice") );
 
449
                        actionModeMenu->setEnabled( false );
 
450
 
 
451
                        actionKick = new KAction(i18n("&Kick"), 0, this, SLOT(slotKick()), this);
 
452
                        actionKick->setEnabled( false );
 
453
 
 
454
                        actionBanMenu = new KActionMenu(i18n("&Ban"), 0, this, "actionBanMenu");
 
455
                        actionBanMenu->insert( new KAction(i18n("Ban *!*@*.host"), 0, this,
 
456
                                SLOT(slotBanHost()), actionBanMenu ) );
 
457
                        actionBanMenu->insert( new KAction(i18n("Ban *!*@domain"), 0, this,
 
458
                                SLOT(slotBanDomain()), actionBanMenu ) );
 
459
                        actionBanMenu->insert( new KAction(i18n("Ban *!*user@*.host"), 0, this,
 
460
                                 SLOT(slotBanUserHost()), actionBanMenu ) );
 
461
                        actionBanMenu->insert( new KAction(i18n("Ban *!*user@domain"), 0, this,
 
462
                                 SLOT(slotBanUserDomain()), actionBanMenu ) );
 
463
                        actionBanMenu->setEnabled( false );
 
464
 
 
465
                        codecAction = new KCodecAction( i18n("&Encoding"), 0, this, "selectcharset" );
 
466
                        connect( codecAction, SIGNAL(activated(const QTextCodec*)),
 
467
                                this, SLOT(setCodec(const QTextCodec*)) );
 
468
                        codecAction->setCodec( codec() );
 
469
                }
 
470
 
 
471
                mCustomActions->append( actionCtcpMenu );
 
472
                mCustomActions->append( actionModeMenu );
 
473
                mCustomActions->append( actionKick );
 
474
                mCustomActions->append( actionBanMenu );
 
475
                mCustomActions->append( codecAction );
 
476
 
 
477
                if( isChannel )
 
478
                {
 
479
                        bool isOperator = ( manager->contactOnlineStatus( account()->myself() ) == m_protocol->m_UserStatusOp );
 
480
                        actionModeMenu->setEnabled(isOperator);
 
481
                        actionBanMenu->setEnabled(isOperator);
 
482
                        actionKick->setEnabled(isOperator);
 
483
                }
 
484
 
 
485
                return mCustomActions;
 
486
        }
 
487
 
 
488
        mActiveManager = 0L;
 
489
 
 
490
        return 0L;
 
491
}
 
492
 
 
493
void IRCUserContact::slotIncomingModeChange( const QString &channel, const QString &, const QString &mode )
 
494
{
 
495
        IRCChannelContact *chan = ircAccount()->contactManager()->findChannel( channel );
 
496
        if( chan->locateUser( m_nickName ) )
 
497
        {
 
498
                QString user = mode.section(' ', 1, 1);
 
499
                kDebug(14120) << mode << ", " << user << ", " << m_nickName;
 
500
                if( user == m_nickName )
 
501
                {
 
502
                        QString modeChange = mode.section(' ', 0, 0);
 
503
                        if(modeChange == QString::fromLatin1("+o"))
 
504
                                setManagerStatus( chan, m_protocol->m_UserStatusOp.internalStatus() );
 
505
                        else if(modeChange == QString::fromLatin1("-o"))
 
506
                                setManagerStatus( chan, -m_protocol->m_UserStatusOp.internalStatus() );
 
507
                        else if(modeChange == QString::fromLatin1("+v"))
 
508
                                setManagerStatus( chan, m_protocol->m_UserStatusVoice.internalStatus() );
 
509
                        else if(modeChange == QString::fromLatin1("-v"))
 
510
                                setManagerStatus( chan, -m_protocol->m_UserStatusVoice.internalStatus() );
 
511
                }
 
512
        }
 
513
}
 
514
 
 
515
void IRCUserContact::setManagerStatus(IRCChannelContact *channel, int statusAdjustment)
 
516
{
 
517
        Kopete::OnlineStatus currentStatus = channel->manager()->contactOnlineStatus(this);
 
518
        Kopete::OnlineStatus newStatus = m_protocol->statusLookup(
 
519
                (IRCProtocol::IRCStatus)(currentStatus.internalStatus() + statusAdjustment)
 
520
        );
 
521
 
 
522
        channel->manager()->setContactOnlineStatus(this, newStatus);
 
523
}
 
524
*/