[gaim-migrate @ 3063]
[pidgin-git.git] / src / protocols / icq / chatsession.c
blob781215b70d2d3258beaa4e82954c7e3faf94e176
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
3 /*
4 * $Id: chatsession.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.
25 #include <stdlib.h>
27 #include "icqlib.h"
28 #include "tcp.h"
30 #include "chatsession.h"
32 icq_ChatSession *icq_ChatSessionNew(icq_Link *icqlink)
34 icq_ChatSession *p=(icq_ChatSession *)malloc(sizeof(icq_ChatSession));
36 if (p)
38 p->status=0;
39 p->id=0L;
40 p->icqlink=icqlink;
41 p->tcplink=NULL;
42 p->user_data=NULL;
43 icq_ListInsert(icqlink->d->icq_ChatSessions, 0, p);
46 return p;
49 void icq_ChatSessionDelete(void *p)
51 icq_ChatSession *pchat = (icq_ChatSession *)p;
52 invoke_callback(pchat->icqlink, icq_ChatNotify)(pchat, CHAT_NOTIFY_CLOSE,
53 0, NULL);
55 free(p);
58 int _icq_FindChatSession(void *p, va_list data)
60 DWORD uin=va_arg(data, DWORD);
61 return (((icq_ChatSession *)p)->remote_uin == uin);
64 icq_ChatSession *icq_FindChatSession(icq_Link *icqlink, DWORD uin)
66 return icq_ListTraverse(icqlink->d->icq_ChatSessions,
67 _icq_FindChatSession, uin);
70 void icq_ChatSessionSetStatus(icq_ChatSession *p, int status)
72 p->status=status;
73 if(p->id)
74 invoke_callback(p->icqlink, icq_ChatNotify)(p, CHAT_NOTIFY_STATUS,
75 status, NULL);
78 /* public */
80 /** Closes a chat session.
81 * @param session desired icq_ChatSession
82 * @ingroup ChatSession
84 void icq_ChatSessionClose(icq_ChatSession *session)
86 icq_TCPLink *tcplink = session->tcplink;
88 /* if we're attached to a tcplink, unattach so the link doesn't try
89 * to close us, and then close the tcplink */
90 if (tcplink)
92 tcplink->session=NULL;
93 icq_TCPLinkClose(tcplink);
96 icq_ChatSessionDelete(session);
98 icq_ListRemove(session->icqlink->d->icq_ChatSessions, session);
101 /** Sends chat data to the remote uin.
102 * @param session desired icq_ChatSession
103 * @param data pointer to data buffer, null-terminated
104 * @ingroup ChatSession
106 void icq_ChatSessionSendData(icq_ChatSession *session, const char *data)
108 icq_TCPLink *tcplink = session->tcplink;
109 int data_length = strlen(data);
110 char *buffer;
112 if(!tcplink)
113 return;
115 buffer = (char *)malloc(data_length);
116 strcpy(buffer, data);
117 icq_ChatRusConv_n("kw", buffer, data_length);
119 send(tcplink->socket, buffer, data_length, 0);
121 free(buffer);
124 /** Sends chat data to the remote uin.
125 * @param session desired icq_ChatSession
126 * @param data pointer to data buffer
127 * @param length length of data
128 * @ingroup ChatSession
130 void icq_ChatSessionSendData_n(icq_ChatSession *session, const char *data,
131 int length)
133 icq_TCPLink *tcplink = session->tcplink;
134 char *buffer;
136 if(!tcplink)
137 return;
139 buffer = (char *)malloc(length);
140 memcpy(buffer, data, length);
141 icq_ChatRusConv_n("kw", buffer, length);
143 send(tcplink->socket, buffer, length, 0);
145 free(buffer);