1 module dldap.result;
2 
3 import dldap.c.ldap_h;
4 import dldap.c.ldap_int;
5 
6 import dldap.connection;
7 import dldap.constants;
8 
9 struct LDAPMessage
10 {
11 	private
12 	{
13 		ldapmsg* message;
14 		LDAPConnection connection;
15 	}
16 
17 
18 	package
19 	{
20 		this(ldapmsg* msg, LDAPConnection conn)
21 		{
22 			message = msg;
23 			connection = conn;
24 
25 			type = message.lm_msgtype.to!LDAPResultType;
26 			dn = connection.getDN(message);
27 			attributes = connection.getAttributes(msg);
28 		}
29 	}
30 
31 	public
32 	{
33 		LDAPResultType type;
34 		string dn;
35 		string[string] attributes;
36 
37 		string opIndex(string attr)
38 		{
39 			return attributes[attr];
40 		}
41 	}
42 
43 	~this()
44 	{
45 	}
46 }
47 
48 struct LDAPResult
49 {
50 	private ldapmsg *result;
51 	private LDAPConnection connection;
52 
53 	int opApply(int delegate(ref LDAPMessage msg) dg)
54 	{
55 		int r = 0;
56 		for (
57 				ldapmsg* msg = connection.firstMessage(result);
58 				msg != null;
59 				msg = connection.nextMessage(msg))
60 		{
61 			auto m = LDAPMessage(msg, connection);
62 
63 			if (m.type == LDAPResultType.result)
64 				continue;
65 
66 			r = dg(m);
67 
68 			if (r) break;
69 		}
70 
71 		return r;
72 	}
73 
74 	this(LDAPConnection conn, ldapmsg* res)
75 	{
76 		result = res;
77 		connection = conn;
78 	}
79 
80 	~this()
81 	{
82 		ldap_msgfree(result);
83 	}
84 }