summaryrefslogtreecommitdiff
path: root/plugins/YahooGroups/src/services.cpp
blob: 16e58a2add5fb3ecc84df3970df7bbfb92fb00ef (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
185
186
187
188
189
190
191
192
193
/*
YahooGroups plugin for Miranda IM

Copyright © 2007 Cristian Libotean

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 "stdafx.h"

#define CLIST_GROUPS "CListGroups"

HANDLE hsYGMove = NULL;
HANDLE hsYGReset = NULL;

int InitServices()
{
	hsYGMove = CreateServiceFunction(MS_YAHOOGROUPS_MOVE, YahooGroupsMoveService);
	hsYGReset = CreateServiceFunction(MS_YAHOOGROUPS_RESET, YahooGroupsResetService);
	
	ReadAvailableGroups();

	return 0;
}

int DestroyServices()
{
	DestroyServiceFunction(hsYGMove);
	DestroyServiceFunction(hsYGReset);

	return 0;
}

void ReadAvailableGroups()
{
	int ok = 1;
	int index = 0;
	
	char group[1024] = {0};
	char tmp[128] = {0};
	
	while (ok)
	{
		mir_snprintf(tmp, "%d", index);
		GetStringFromDatabase(NULL, CLIST_GROUPS, tmp, NULL, group, sizeof(group));
		if (mir_strlen(group) > 0)
		{
			availableGroups.Add(_strdup(group + 1));
			index += 1;
		}
		else{
			ok = 0;
		}
	}
}

int GetNextGroupIndex()
{
	int index = 0;
	int found = 0;
	
	char buffer[1024] = {0};
	char tmp[128] = {0};
	
	while (!found)
	{
		mir_snprintf(tmp, "%d", index++);
		
		if (GetStringFromDatabase(NULL, CLIST_GROUPS, tmp, NULL, buffer, sizeof(buffer)))
		{
			found = 1;
		}
	}
	
	return index - 1;
}

void AddNewGroup(const char *newGroup)
{
	int index = GetNextGroupIndex();
	
	char tmp[128];
	char group[1024];

	*group = 1;
	strncpy_s((group + 1), (_countof(group) - 1), newGroup, _TRUNCATE);
	
	mir_snprintf(tmp, "%d", index);
	const int MAX_SIZE = 1024;
	wchar_t wide[MAX_SIZE] = {0};
	*wide = 1;
	MultiByteToWideChar(currentCodePage, 0, group + 1, -1, wide + 1, MAX_SIZE - 1);
	db_set_ws(NULL, CLIST_GROUPS, tmp, wide);
	
	availableGroups.Add(_strdup(group + 1));
}

void AddContactToGroup(MCONTACT hContact, char *group)
{
	const int MAX_SIZE = 1024;
	wchar_t wide[MAX_SIZE] = {0};
	MultiByteToWideChar(currentCodePage, 0, group, -1, wide, MAX_SIZE);
	db_set_ws(hContact, "CList", "Group", wide);
}

void CreateGroup(char *group)
{
	char *p = group;
	char *sub = group;

	CMStringA buf;
	while ((p = strchr(sub, '\\')))
	{
		*p = 0;
		if (!buf.IsEmpty())
			buf.AppendChar('\\');
		buf.Append(sub);
		
		if (!availableGroups.Contains(buf))
			AddNewGroup(buf);
		
		*p++ = '\\';
		sub = p;
	}
	
	if (sub) {
		if (!buf.IsEmpty())
			buf.AppendChar('\\');
		buf.Append(sub);
		
		if (!availableGroups.Contains(buf))
			AddNewGroup(buf);
	}
}

void ProcessContacts(void (*callback)(MCONTACT, char *), char *protocol)
{
	for (MCONTACT hContact = db_find_first(); hContact; hContact = db_find_next(hContact))
		callback(hContact, protocol);
}

void YahooMoveCallback(MCONTACT hContact, char*)
{
	char protocol[128] = {0};
	GetContactProtocol(hContact, protocol, sizeof(protocol));
	
	if (mir_strlen(protocol) > 0)
	{
		char ygroup[128] = {0};
		
		if (!GetStringFromDatabase(hContact, protocol, "YGroup", NULL, ygroup, sizeof(ygroup)))
		{
			CreateGroup(ygroup);
			AddContactToGroup(hContact, ygroup);
		}
	}
}

void ResetGroupCallback(MCONTACT hContact, char *protocol)
{
	char p[128] = {0};
	
	GetContactProtocol(hContact, p, sizeof(protocol));
	
	if ((!protocol) || (_stricmp(protocol, p) == 0))
	{
		db_unset(hContact, "CList", "Group");
	}
}

INT_PTR YahooGroupsMoveService(WPARAM, LPARAM)
{
	ProcessContacts(YahooMoveCallback, NULL);
	return 0;
}

INT_PTR YahooGroupsResetService(WPARAM, LPARAM)
{
	ProcessContacts(ResetGroupCallback, NULL);
	return 0;
}