summaryrefslogtreecommitdiff
path: root/miranda-wine/protocols/MSN/msn_srv.cpp
blob: 80fb64e5c06c96194ff9c0655391684610dd7c0d (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
184
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.
Copyright (c) 2003-5 George Hazan.
Copyright (c) 2002-3 Richard Hughes (original version).

Miranda IM: the free icq client for MS Windows
Copyright (C) 2000-2002 Richard Hughes, Roland Rabien & Tristan Van de Vreede

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.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "msn_global.h"

struct ServerGroupItem
{
	char* id;
	char* name; // in UTF8
	int   number;

	ServerGroupItem* next;
};

static ServerGroupItem* sttFirst = NULL;

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_AddGroup - adds new server group to the list

bool MSN_AddGroup( const char* pName, const char* pId )
{
	ServerGroupItem* p = new ServerGroupItem;
	if ( p == NULL )
		return false;

	p->number = -1;
	p->id = strdup( pId );
	p->name = strdup( pName );
	p->next = sttFirst;
	sttFirst = p;
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_DeleteGroup - deletes a group from the list

void MSN_DeleteGroup( const char* pId )
{
	ServerGroupItem* prev = NULL;

	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p->next ) {
		if ( !strcmp( p->id, pId )) {
			if ( prev == NULL ) sttFirst = p->next;
			else                prev->next = p->next;
			free( p->id );
			free( p->name );
			delete p;
			return;
		}

		prev = p;
}	}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_FreeGroups - clears the server groups list

void MSN_FreeGroups()
{
	ServerGroupItem* p1;

	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p1 ) {
		p1 = p->next;

		free( p->id );
		free( p->name );
		delete p;
	}

	sttFirst = NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_GetGroupById - tries to return a group name associated with given UUID

LPCSTR MSN_GetGroupById( const char* pId )
{
	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p->next )
		if ( stricmp( p->id, pId ) == 0 )
			return p->name;

	return NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_GetGroupByName - tries to return a group UUID associated with the given name 

LPCSTR MSN_GetGroupByName( const char* pName )
{
	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p->next )
		if ( strcmp( p->name, pName ) == 0 )
			return p->id;

	return NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_GetGroupByNumber - tries to return a group UUID associated with the given id 

LPCSTR MSN_GetGroupByNumber( int pNumber )
{
	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p->next )
		if ( p->number == pNumber )
			return p->id;

	return NULL;
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_MoveContactToGroup - sends a contact to the specified group 

void MSN_MoveContactToGroup( HANDLE hContact, const char* grpName )
{
	LPCSTR szId;
	char szContactID[ 100 ], szGroupID[ 100 ];
	if ( MSN_GetStaticString( "ID", hContact, szContactID, sizeof szContactID ))
		return;

	if ( MSN_GetStaticString( "GroupID", hContact, szGroupID, sizeof szGroupID ))
		szGroupID[ 0 ] = 0;

	bool bInsert = szGroupID[0] == 0, bDelete = szGroupID[0] != 0;

	if ( grpName != NULL )
	{
		if (( szId = MSN_GetGroupByName( grpName )) == NULL ) {
			MSN_AddServerGroup( grpName );
			if (( szId = MSN_GetGroupByName( grpName )) == NULL )
				return;
		}

		if ( !strcmp( szGroupID, szId ))	bDelete = false;
		else                             bInsert = true;
	}
	else bInsert = false;		

	if ( bInsert )
		msnNsThread->sendPacket( "ADC", "FL C=%s %s", szContactID, szId );

	if ( bDelete )
		msnNsThread->sendPacket( "REM", "FL %s %s", szContactID, szGroupID );
}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SetGroupName - sets a new name to a server group

void MSN_SetGroupName( const char* pId, const char* pNewName )
{
	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p->next ) {
		if ( strcmp( p->id, pId ) == 0 ) {
			free( p->name );
			p->name = strdup( pNewName );
			return;
}	}	}

/////////////////////////////////////////////////////////////////////////////////////////
// MSN_SetGroupNumber - associate a server group with Miranda's group number 

void MSN_SetGroupNumber( const char* pId, int pNumber )
{
	for ( ServerGroupItem* p = sttFirst; p != NULL; p = p->next ) {
		if ( strcmp( p->id, pId ) == 0 ) {
			p->number = pNumber;
			return;
}	}	}