1 /*
2 Kopete Oscar Protocol
3 userinfotask.h - Handle sending and receiving info requests for users
4
5 Copyright (c) 2004-2005 Matt Rogers <mattr@kde.org>
6
7 Kopete (c) 2002-2005 by the Kopete developers <kopete-devel@kde.org>
8
9 *************************************************************************
10 * *
11 * This library is free software; you can redistribute it and/or *
12 * modify it under the terms of the GNU Lesser General Public *
13 * License as published by the Free Software Foundation; either *
14 * version 2 of the License, or (at your option) any later version. *
15 * *
16 *************************************************************************
17 */
18 #include "userinfotask.h"
19
20 #include <kdebug.h>
21
22 #include "buffer.h"
23 #include "connection.h"
24 #include "transfer.h"
25 #include "userdetails.h"
26
UserInfoTask(Task * parent)27 UserInfoTask::UserInfoTask( Task* parent )
28 : Task( parent )
29 {
30 }
31
~UserInfoTask()32 UserInfoTask::~UserInfoTask()
33 {
34 }
35
forMe(const Transfer * transfer) const36 bool UserInfoTask::forMe( const Transfer * transfer ) const
37 {
38 const SnacTransfer* st = dynamic_cast<const SnacTransfer*>( transfer );
39 if ( !st )
40 return false;
41
42 if ( st->snacService() == 0x0002 && st->snacSubtype() == 0x0006 )
43 {
44 if ( !m_contactSequenceMap.contains( st->snacRequest() ) )
45 return false;
46 else
47 {
48 //kDebug(OSCAR_RAW_DEBUG) << "Found sequence. taking packet";
49 return true;
50 }
51 }
52 else
53 return false;
54 }
55
take(Transfer * transfer)56 bool UserInfoTask::take( Transfer * transfer )
57 {
58 if ( forMe( transfer ) )
59 {
60 setTransfer( transfer );
61 Oscar::DWORD seq = 0;
62 SnacTransfer* st = dynamic_cast<SnacTransfer*>( transfer );
63 if ( st )
64 seq = st->snacRequest();
65
66 if ( seq != 0 )
67 {
68 //AFAIK location info packets always have user info
69 Buffer* b = transfer->buffer();
70 UserDetails ud;
71 ud.fill( b );
72 m_sequenceInfoMap[seq] = ud;
73 emit gotInfo( seq );
74
75 QList<TLV> list = b->getTLVList();
76 QList<TLV>::iterator it = list.begin(), itEnd = list.end();
77 QString profile;
78 QString away;
79 for ( ; it != itEnd; ++it )
80 {
81 switch( ( *it ).type )
82 {
83 case 0x0001: //profile text encoding
84 kDebug(OSCAR_RAW_DEBUG) << "text encoding is " << QString( ( *it ).data );
85 break;
86 case 0x0002: //profile text
87 kDebug(OSCAR_RAW_DEBUG) << "The profile is '" << QString( ( *it ).data ) << "'";
88 profile = QString( ( *it ).data ); // aim always seems to use us-ascii encoding
89 emit receivedProfile( m_contactSequenceMap[seq], profile );
90 break;
91 case 0x0003: //away message encoding
92 kDebug(OSCAR_RAW_DEBUG) << "Away message encoding is " << QString( ( *it ).data );
93 break;
94 case 0x0004: //away message
95 kDebug(OSCAR_RAW_DEBUG) << "Away message is '" << QString( ( *it ).data ) << "'";
96 away = QString( (*it ).data ); // aim always seems to use us-ascii encoding
97 emit receivedAwayMessage( m_contactSequenceMap[seq], away );
98 break;
99 case 0x0005: //capabilities
100 break;
101 default: //unknown
102 kDebug(14151) << "Unknown user info type " << ( *it ).type;
103 break;
104 };
105 }
106 list.clear();
107 }
108 setTransfer( 0 );
109 return true;
110 }
111 return false;
112 }
113
onGo()114 void UserInfoTask::onGo()
115 {
116 if ( m_contactSequenceMap[m_seq].isEmpty() )
117 {
118 kDebug( OSCAR_RAW_DEBUG ) << "Info requested for empty contact!";
119 return;
120 }
121
122 FLAP f = { 0x02, 0, 0 };
123 SNAC s = { 0x0002, 0x0005, 0, m_seq };
124 Buffer* buffer = new Buffer();
125
126 buffer->addWord( m_typesSequenceMap[m_seq] );
127 buffer->addBUIN( m_contactSequenceMap[m_seq].toLocal8Bit() );
128
129 Transfer* t = createTransfer( f, s, buffer );
130 send( t );
131 }
132
requestInfoFor(const QString & contact,unsigned int types)133 void UserInfoTask::requestInfoFor( const QString& contact, unsigned int types )
134 {
135 Oscar::DWORD seq = client()->snacSequence();
136 kDebug(OSCAR_RAW_DEBUG) << "setting sequence " << seq << " for contact " << contact;
137 m_contactSequenceMap[seq] = contact;
138 m_typesSequenceMap[seq] = types;
139 m_seq = seq;
140 onGo();
141 }
142
getInfoFor(Oscar::DWORD sequence) const143 UserDetails UserInfoTask::getInfoFor( Oscar::DWORD sequence ) const
144 {
145 return m_sequenceInfoMap[sequence];
146 }
147
148