summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/netmeeting/netmeetinginvitation.cpp
blob: 3f068c067fcffaca8420d3f9a6cde6faef02453c (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
/*
    msninvitation.cpp

    Copyright (c) 2003 by Olivier Goffart        <ogoffart @ kde.org>

    *************************************************************************
    *                                                                       *
    * 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.                                   *
    *                                                                       *
    *************************************************************************
*/

#include "netmeetinginvitation.h"

#include "kopeteuiglobal.h"

#include "msnchatsession.h"
#include "msnswitchboardsocket.h"
#include "msncontact.h"
#include "kopetemetacontact.h"

#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kglobal.h>


#include <tqtimer.h>
#include <kprocess.h>

NetMeetingInvitation::NetMeetingInvitation(bool incoming, MSNContact *c, TQObject *tqparent)
 : TQObject(tqparent) , MSNInvitation( incoming, NetMeetingInvitation::applicationID() , i18n("NetMeeting") )
{
	m_contact=c;
	oki=false;
}


NetMeetingInvitation::~NetMeetingInvitation()
{
}


TQString NetMeetingInvitation::invitationHead()
{
	TQTimer::singleShot( 10*60000, this, TQT_SLOT( slotTimeout() ) ); //send TIMEOUT in 10 minute if the invitation has not been accepted/refused
	return TQString( MSNInvitation::invitationHead()+
				"Session-Protocol: SM1\r\n"
  				"Session-ID: {6672F94C-45BF-11D7-B4AE-00010A1008DF}\r\n" //FIXME i don't know what is the session id
				"\r\n").utf8();
}

void NetMeetingInvitation::parseInvitation(const TQString& msg)
{
	TQRegExp rx("Invitation-Command: ([A-Z]*)");
	rx.search(msg);
	TQString command=rx.cap(1);
	if( msg.contains("Invitation-Command: INVITE") )
	{
		MSNInvitation::parseInvitation(msg); //for the cookie

		unsigned int result = KMessageBox::questionYesNo( Kopete::UI::Global::mainWidget(),
					i18n("%1 wants to start a chat with NetMeeting; do you want to accept it? " ).tqarg(m_contact->metaContact()->displayName()),
					i18n("MSN Plugin") , i18n("Accept"),i18n("Refuse"));

		MSNChatSession* manager=dynamic_cast<MSNChatSession*>(m_contact->manager());

		if(manager && manager->service())
		{
			if(result==3) // Yes == 3
			{
				TQCString message=TQString(
					"MIME-Version: 1.0\r\n"
					"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n"
					"\r\n"
					"Invitation-Command: ACCEPT\r\n"
					"Invitation-Cookie: " + TQString::number(cookie()) + "\r\n"
					"Session-ID: {6672F94C-45BF-11D7-B4AE-00010A1008DF}\r\n" //FIXME
					"Session-Protocol: SM1\r\n"
					"Launch-Application: TRUE\r\n"
					"Request-Data: IP-Address:\r\n"
					"IP-Address: " + manager->service()->getLocalIP()+ "\r\n"
					"\r\n" ).utf8();


				manager->service()->sendCommand( "MSG" , "N", true, message );
				oki=false;
				TQTimer::singleShot( 10* 60000, this, TQT_SLOT( slotTimeout() ) ); //TIMOUT afte 10 min
			}
			else //No
			{
				manager->service()->sendCommand( "MSG" , "N", true, rejectMessage() );
				emit done(this);
			}
		}
	}
	else if( msg.contains("Invitation-Command: ACCEPT") )
	{
		if( ! incoming() )
		{
			MSNChatSession* manager=dynamic_cast<MSNChatSession*>(m_contact->manager());
			if(manager && manager->service())
			{
				TQCString message=TQString(
					"MIME-Version: 1.0\r\n"
					"Content-Type: text/x-msmsgsinvite; charset=UTF-8\r\n"
					"\r\n"
					"Invitation-Command: ACCEPT\r\n"
					"Invitation-Cookie: " + TQString::number(cookie()) + "\r\n"
					"Session-ID: {6672F94C-45BF-11D7-B4AE-00010A1008DF}\r\n" //FIXME: what is session id?
					"Session-Protocol: SM1\r\n"
					"Launch-Application: TRUE\r\n"
					"Request-Data: IP-Address:\r\n"
					"IP-Address: " + manager->service()->getLocalIP() + "\r\n"
					"\r\n" ).utf8();
				manager->service()->sendCommand( "MSG" , "N", true, message );
			}
			rx=TQRegExp("IP-Address: ([0-9\\:\\.]*)");
			rx.search(msg);
			TQString ip_address = rx.cap(1);
	    	startMeeting(ip_address);
			kdDebug() << k_funcinfo << ip_address << endl;
		}
		else
		{
			rx=TQRegExp("IP-Address: ([0-9\\:\\.]*)");
			rx.search(msg);
			TQString ip_address = rx.cap(1);

			startMeeting(ip_address);
		}
	}
	else //CANCEL
	{
		emit done(this);
	}
}

void NetMeetingInvitation::slotTimeout()
{
	if(oki)
		return;

 	MSNChatSession* manager=dynamic_cast<MSNChatSession*>(m_contact->manager());

	if(manager && manager->service())
	{
		manager->service()->sendCommand( "MSG" , "N", true, rejectMessage("TIMEOUT") );
	}
	emit done(this);

}


void NetMeetingInvitation::startMeeting(const TQString & ip_address)
{
	//TODO: use KProcess
	
	KConfig *config=KGlobal::config();
	config->setGroup("Netmeeting Plugin");
	TQString app=config->readEntry("NetmeetingApplication","ekiga -c callto://%1").tqarg(ip_address);
	
	kdDebug() << k_funcinfo << app <<  endl ;
	
	TQStringList args=TQStringList::split(" ", app);
	
	KProcess p;
	for(TQStringList::Iterator it=args.begin() ; it != args.end() ; ++it)
	{
		p << *it;
	}
	p.start();
}

#include "netmeetinginvitation.moc"