summaryrefslogtreecommitdiff
path: root/include/m_contactdir.h
blob: 805fa27d649e2be9a3012fc7ff579ef5a597036c (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
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2005 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

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

#ifndef M_CONTACTDIR_H__
#define M_CONTACTDIR_H__ 1

/* Contactdir module was created on 2005/05/17, 0.4.0.1 

   -- How you use this module as a protocol --
   
   On Load() Register your protocol with the setting name that stores unique IDs, example:
   
	if ( ContactDir_SupportExists() ) g_Atom=ContactDir_Register("ICQ", "UIN");
	
	This will register your protocol and walk the database looking for all contacts on PROTOCOL_NAME which have
	a "UIN" setting and store it in memory (converting to a string as needed) You of course have to 
	provide fallback if the services don't exist, it's an idea to keep existing code for that.
	
	-
	
	When you add a new contact via MS_DB_CONTACT_ADD, you must register it with your protocol atom too, via 
	ContactDir_AddContact(atom, "UIN #", hContact) and when it is deleted ContactDir_RemoveContact(atom, "UIN #")
	
	-
	
	To find a contact, use ContactDir_Lookup(atom, "ICQ #") which will return the hContact.

*/

typedef struct {
	int cbSize;
	char * szProto;
	char * szSetting;
	HANDLE atom; 		// out arg
} CONTACTDIRECTORYDESCRIPTOR;

/*
	wParam: 0
	lParam: (LPARAM) &CONTACTDIRECTORYDESCRIPTOR;
	Affect: Register a given protocol and it's setting name which contains the unique key entry. e.g. ("ICQ", "UIN")
		and return a HANDLE for use with other lookup services.
	Returns: 0 on success, non zero on failure -- a returned handle is in .atom
	Note: The directory will convert dword values into string representations but will not do this for bytes or words
		used as IDs -- the protocol has to convert the IDs itself (:
	Note: See ContactDir_Register() for a quicker way.
	*** WARNING ***: This service does not expect the given module name to have registered as a protocol module, it
		completely bypasses this information.
	Version: 0.4.0.1 (2005/05/17+)
*/
#define MS_CONTACTDIR_REGISTER "ContactDir/Register"

typedef struct {
	int cbSize;
	HANDLE atom;		// Atom handle from MS_CONTACTDIR_REGISTER
	char * szID;		// in: value you wish to find (makes its own copy if needed)
	HANDLE hContact;	// out: hContact associated with szID, if any.
} CONTACTDIRECTORYLOOKUP;

/*

	wParam: 0
	lParam: (LPARAM) &CONTACTDIRECTORYLOOKUP;
	Affect: Given an atom and string ID, will find the associated DB hContact value
	Returns: 0 on success, non zero on failure
	Version: 0.4.0.1 (2005/05/17+)
	Note: ContactDir_Lookup() helper macro might be of use.
*/

#define MS_CONTACTDIR_LOOKUP "ContactDir/Lookup"

/*
	wParam: 0
	lParam: (LPARAM)&CONTACTDIRECTORYLOOKUP;
	Affect: Add a contact to a protocol atom association.
	Returns: 0 on success, non zero on failure
	Version: 0.4.0.1 (2005/05/17+)
	Note: You must call this when you create a contact with MS_DB_CONTACT_ADD, see  ContactDir_AddContact()
*/
#define MS_CONTACTDIR_ADDCONTACT "ContactDir/AddContact"

/*
	wParam: 0
	lParam: (LPARAM)&CONTACTDIRECTORYLOOKUP;
	Affect: Remove a contact to a protocol atom association.
	Returns: 0 on success, non zero on failure
	Version: 0.4.0.1 (2005/05/17+)
	Note: see ContactDir_RemoveContact()
*/
#define MS_CONTACTDIR_REMOVECONTACT "ContactDir/RemoveContact"


/* -- Helper functions -- */

static int ContactDir_SupportExists(void)
{
	return ServiceExists(MS_CONTACTDIR_REGISTER);
}

// Only take as valid if ContactDir_SupportExists() returns true.
static HANDLE ContactDir_Register(char * szProto, char * szSetting)
{
	CONTACTDIRECTORYDESCRIPTOR cd;
	cd.cbSize=sizeof(CONTACTDIRECTORYDESCRIPTOR);
	cd.szProto=szProto;
	cd.szSetting=szSetting;
	cd.atom=NULL;
	CallService(MS_CONTACTDIR_REGISTER, 0, (LPARAM)&cd);
	return cd.atom;
}

static __inline HANDLE ContactDir_Lookup(HANDLE atom, char * szID) 
{
	CONTACTDIRECTORYLOOKUP f;
	f.cbSize=sizeof(f);
	f.atom=atom;
	f.szID=szID;
	f.hContact=NULL;
	CallService(MS_CONTACTDIR_LOOKUP, 0, (LPARAM)&f);
	return f.hContact;
}

static __inline void ContactDir_AddContact(HANDLE atom, char * szID, HANDLE hContact)
{
	CONTACTDIRECTORYLOOKUP c = {0};
	c.cbSize=sizeof(CONTACTDIRECTORYLOOKUP);		
	c.atom=atom;
	c.szID=szID;
	c.hContact=hContact;
	CallService(MS_CONTACTDIR_ADDCONTACT, 0, (LPARAM)&c);
}

static __inline void ContactDir_RemoveContact(HANDLE atom, char * szID)
{
	CONTACTDIRECTORYLOOKUP c = {0};
	c.cbSize=sizeof(CONTACTDIRECTORYLOOKUP);		
	c.atom=atom;
	c.szID=szID;
	c.hContact=NULL;
	CallService(MS_CONTACTDIR_REMOVECONTACT, 0, (LPARAM)&c);
}

#endif /* M_CONTACTDIR_H__ */