Mercurial > prosody-modules
comparison mod_auth_dovecot/mod_auth_dovecot.lua @ 310:b3bcd1913c85
mod_auth_dovecot: Switch to using upvalue 'conn' instead of provider.c throughout (thanks Adrien Clerc)
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 08 Jan 2011 16:54:33 +0000 |
| parents | 4c3abf1a9b5a |
| children | f663ea45436f |
comparison
equal
deleted
inserted
replaced
| 309:5ec9125575fc | 310:b3bcd1913c85 |
|---|---|
| 15 | 15 |
| 16 local prosody = _G.prosody; | 16 local prosody = _G.prosody; |
| 17 local socket_path = module:get_option_string("dovecot_auth_socket", "/var/run/dovecot/auth-login"); | 17 local socket_path = module:get_option_string("dovecot_auth_socket", "/var/run/dovecot/auth-login"); |
| 18 | 18 |
| 19 function new_default_provider(host) | 19 function new_default_provider(host) |
| 20 local provider = { name = "dovecot", c = nil, request_id = 0 }; | 20 local provider = { name = "dovecot", request_id = 0 }; |
| 21 log("debug", "initializing dovecot authentication provider for host '%s'", host); | 21 log("debug", "initializing dovecot authentication provider for host '%s'", host); |
| 22 | |
| 23 local conn; | |
| 22 | 24 |
| 23 -- Closes the socket | 25 -- Closes the socket |
| 24 function provider.close(self) | 26 function provider.close(self) |
| 25 if (provider.c ~= nil) then | 27 if conn then |
| 26 provider.c:close(); | 28 conn:close(); |
| 27 end | 29 conn = nil; |
| 28 provider.c = nil; | 30 end |
| 29 end | 31 end |
| 30 | 32 |
| 31 -- The following connects to a new socket and send the handshake | 33 -- The following connects to a new socket and send the handshake |
| 32 function provider.connect(self) | 34 function provider.connect(self) |
| 33 -- Destroy old socket | 35 -- Destroy old socket |
| 34 provider:close(); | 36 provider:close(); |
| 35 | 37 |
| 36 provider.c = socket.unix(); | 38 conn = socket.unix(); |
| 37 | 39 |
| 38 -- Create a connection to dovecot socket | 40 -- Create a connection to dovecot socket |
| 39 log("debug", "connecting to dovecot socket at '%s'", socket_path); | 41 log("debug", "connecting to dovecot socket at '%s'", socket_path); |
| 40 local r, e = provider.c:connect(socket_path); | 42 local r, e = conn:connect(socket_path); |
| 41 if (not r) then | 43 if (not r) then |
| 42 log("warn", "error connecting to dovecot socket at '%s'. error was '%s'. check permissions", socket_path, e); | 44 log("warn", "error connecting to dovecot socket at '%s'. error was '%s'. check permissions", socket_path, e); |
| 43 provider:close(); | 45 provider:close(); |
| 44 return false; | 46 return false; |
| 45 end | 47 end |
| 95 return true; | 97 return true; |
| 96 end | 98 end |
| 97 | 99 |
| 98 -- Wrapper for send(). Handles errors | 100 -- Wrapper for send(). Handles errors |
| 99 function provider.send(self, data) | 101 function provider.send(self, data) |
| 100 local r, e = provider.c:send(data); | 102 local r, e = conn:send(data); |
| 101 if (not r) then | 103 if (not r) then |
| 102 log("warn", "error sending '%s' to dovecot. error was '%s'", data, e); | 104 log("warn", "error sending '%s' to dovecot. error was '%s'", data, e); |
| 103 provider:close(); | 105 provider:close(); |
| 104 return false; | 106 return false; |
| 105 end | 107 end |
| 106 return true; | 108 return true; |
| 107 end | 109 end |
| 108 | 110 |
| 109 -- Wrapper for receive(). Handles errors | 111 -- Wrapper for receive(). Handles errors |
| 110 function provider.receive(self) | 112 function provider.receive(self) |
| 111 local r, e = provider.c:receive(); | 113 local r, e = conn:receive(); |
| 112 if (not r) then | 114 if (not r) then |
| 113 log("warn", "error receiving data from dovecot. error was '%s'", socket, e); | 115 log("warn", "error receiving data from dovecot. error was '%s'", socket, e); |
| 114 provider:close(); | 116 provider:close(); |
| 115 return false; | 117 return false; |
| 116 end | 118 end |
| 117 return r; | 119 return r; |
| 118 end | 120 end |
| 119 | 121 |
| 120 function provider.send_auth_request(self, username, password) | 122 function provider.send_auth_request(self, username, password) |
| 121 if (provider.c == nil) then | 123 if not conn then |
| 122 if (not provider:connect()) then | 124 if not provider:connect() then |
| 123 return nil, "Auth failed. Dovecot communications error"; | 125 return nil, "Auth failed. Dovecot communications error"; |
| 124 end | 126 end |
| 125 end | 127 end |
| 126 | 128 |
| 127 -- Send auth data | 129 -- Send auth data |
