1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
4 * $Id: tcpchathandle.c 2096 2001-07-31 01:00:39Z warmenhoven $
6 * Copyright (C) 1998-2001, Denis V. Dmitrienko <denis@null.net> and
7 * Bill Soudan <soudan@kde.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include "stdpackets.h"
30 #include "chatsession.h"
32 void icq_HandleChatAck(icq_TCPLink
*plink
, icq_Packet
*p
, int port
)
34 icq_TCPLink
*pchatlink
;
35 icq_ChatSession
*pchat
;
38 pchatlink
=icq_TCPLinkNew(plink
->icqlink
);
39 pchatlink
->type
=TCP_LINK_CHAT
;
42 /* once the ack packet has been processed, create a new chat session */
43 pchat
=icq_ChatSessionNew(plink
->icqlink
);
46 pchat
->remote_uin
=plink
->remote_uin
;
47 pchat
->tcplink
=pchatlink
;
49 invoke_callback(plink
->icqlink
, icq_RequestNotify
)(plink
->icqlink
, p
->id
,
50 ICQ_NOTIFY_CHATSESSION
, sizeof(icq_ChatSession
), pchat
);
52 icq_ChatSessionSetStatus(pchat
, CHAT_STATUS_CONNECTING
);
53 icq_TCPLinkConnect(pchatlink
, plink
->remote_uin
, port
);
55 pchatlink
->session
=pchat
;
57 p2
=icq_TCPCreateChatInfoPacket(pchatlink
, plink
->icqlink
->icq_Nick
,
58 0x00ffffff, 0x00000000);
59 icq_TCPLinkSend(pchatlink
, p2
);
63 void icq_HandleChatHello(icq_TCPLink
*plink
)
66 /* once the hello packet has been processed and we know which uin this
67 * link is for, we can link up with a chat session */
68 icq_ChatSession
*pchat
=icq_FindChatSession(plink
->icqlink
,
75 icq_ChatSessionSetStatus(pchat
, CHAT_STATUS_WAIT_NAME
);
79 icq_FmtLog(plink
->icqlink
, ICQ_LOG_WARNING
,
80 "unexpected chat hello received from %d, closing link\n",
82 icq_TCPLinkClose(plink
);
87 void icq_TCPOnChatReqReceived(icq_Link
*icqlink
, DWORD uin
,
88 const char *message
, DWORD id
)
90 /* use the current system time for time received */
92 struct tm
*ptime
=localtime(&t
);
94 #ifdef TCP_PACKET_TRACE
95 printf("chat request packet received from %lu { sequence=%lx, message=%s }\n",
97 #endif /* TCP_PACKET_TRACE */
99 invoke_callback(icqlink
,icq_RecvChatReq
)(icqlink
, uin
, ptime
->tm_hour
,
100 ptime
->tm_min
, ptime
->tm_mday
, ptime
->tm_mon
+1, ptime
->tm_year
+1900,
103 /* don't send an acknowledgement to the remote client!
104 * GUI is responsible for sending acknowledgement once user accepts
105 * or denies using icq_TCPSendChatAck */
108 void icq_TCPChatUpdateFont(icq_ChatSession
*psession
, const char *font
,
109 WORD encoding
, DWORD style
, DWORD size
)
111 icq_Link
*icqlink
= psession
->icqlink
;
112 int packet_len
, fontlen
;
115 buffer
= malloc(packet_len
= (2 + (fontlen
= strlen(font
) + 1)) +
118 *((DWORD
*)&buffer
[1]) = style
;
120 *((DWORD
*)&buffer
[6]) = size
;
122 *((WORD
*)&buffer
[11]) = fontlen
;
123 strcpy(&buffer
[13], font
);
125 icq_RusConv("wk", &buffer
[13]);
127 *((WORD
*)&buffer
[13 + fontlen
]) = encoding
;
129 invoke_callback(icqlink
, icq_ChatNotify
)(psession
, CHAT_NOTIFY_DATA
,
135 void icq_TCPChatUpdateColors(icq_ChatSession
*psession
, DWORD foreground
,
138 icq_Link
*icqlink
= psession
->icqlink
;
142 *((DWORD
*)&buffer
[1]) = foreground
;
144 *((DWORD
*)&buffer
[6]) = background
;
146 invoke_callback(icqlink
, icq_ChatNotify
)(psession
,
147 CHAT_NOTIFY_DATA
, sizeof(buffer
), buffer
);
150 void icq_TCPProcessChatPacket(icq_Packet
*p
, icq_TCPLink
*plink
)
155 DWORD fg
, bg
, fontstyle
, fontsize
;
156 WORD port1
, encoding
;
157 icq_Packet
*presponse
;
158 icq_ChatSession
*pchat
=(icq_ChatSession
*)plink
->session
;
159 const char *font
, *user
;
163 code
=icq_PacketRead32(p
);
164 remote_uin
=icq_PacketRead32(p
);
166 if(code
==0x00000006 || code
==0x00070004)
172 if(code
== 0x00070004)
174 ip1
= icq_PacketRead32(p
);
175 ip2
= icq_PacketRead32(p
);
177 port1
= icq_PacketRead16(p
);
178 fontsize
= icq_PacketRead32(p
);
179 fontstyle
= icq_PacketRead32(p
);
180 font
= icq_PacketReadString(p
);
181 encoding
= icq_PacketRead16(p
);
185 ip1
= icq_PacketRead32(p
);
186 ip2
= icq_PacketRead32(p
);
187 port1
= icq_PacketRead16(p
);
189 fontsize
= icq_PacketRead32(p
);
190 fontstyle
= icq_PacketRead32(p
);
191 font
= icq_PacketReadString(p
);
192 encoding
= icq_PacketRead16(p
);
195 icq_TCPChatUpdateFont(pchat
, font
, encoding
, fontstyle
, fontsize
);
196 icq_ChatSessionSetStatus(pchat
, CHAT_STATUS_READY
);
197 plink
->mode
|=TCP_LINK_MODE_RAW
;
200 if(remote_uin
>0xffffff00)
202 remote_uin
=icq_PacketRead32(p
);
203 user
= icq_PacketReadString(p
);
204 icq_PacketRead16(p
); /* Unknown */;
205 fg
= icq_PacketRead32(p
);
206 bg
= icq_PacketRead32(p
);
207 icq_TCPChatUpdateColors(pchat
, fg
, bg
);
209 presponse
=icq_TCPCreateChatInfo2Packet(plink
, plink
->icqlink
->icq_Nick
,
210 0x00ffffff, 0x00000000);
211 icq_PacketSend(presponse
, plink
->socket
);
212 icq_PacketDelete(presponse
);
213 icq_ChatSessionSetStatus(pchat
, CHAT_STATUS_WAIT_FONT
);
217 user
= icq_PacketReadString(p
);
218 fg
= icq_PacketRead32(p
);
219 bg
= icq_PacketRead32(p
);
220 icq_TCPChatUpdateColors(pchat
, fg
, bg
);
225 if((code
= icq_PacketRead32(p
)) == 0x00070004)
228 ip1
= icq_PacketRead32(p
);
229 ip2
= icq_PacketRead32(p
);
231 port1
= icq_PacketRead16(p
);
232 fontsize
= icq_PacketRead32(p
);
233 fontstyle
= icq_PacketRead32(p
);
234 font
= icq_PacketReadString(p
);
235 encoding
= icq_PacketRead16(p
);
237 else if(code
== 0x00000006)
240 ip1
= icq_PacketRead32(p
);
241 ip2
= icq_PacketRead32(p
);
242 port1
= icq_PacketRead16(p
);
244 fontsize
= icq_PacketRead32(p
);
245 fontstyle
= icq_PacketRead32(p
);
246 font
= icq_PacketReadString(p
);
247 encoding
= icq_PacketRead16(p
);
250 icq_TCPChatUpdateFont(pchat
, font
, encoding
, fontstyle
, fontsize
);
251 presponse
=icq_TCPCreateChatFontInfoPacket(plink
);
252 icq_PacketSend(presponse
, plink
->socket
);
253 icq_PacketDelete(presponse
);
254 /* notify app that chat connection has been established */
255 icq_ChatSessionSetStatus(pchat
, CHAT_STATUS_READY
);
256 plink
->mode
|=TCP_LINK_MODE_RAW
;