Commit acef5b6f acef5b6f3dae4a8a584603ad8947227967bf4736 by Wojciech Polak

Add Secret class to libmu_cpp.

1 parent 1f49d2c9
...@@ -45,7 +45,11 @@ main () ...@@ -45,7 +45,11 @@ main ()
45 45
46 cout << "\tscheme <" << url.get_scheme () << ">" << endl; 46 cout << "\tscheme <" << url.get_scheme () << ">" << endl;
47 cout << "\tuser <" << url.get_user () << ">" << endl; 47 cout << "\tuser <" << url.get_user () << ">" << endl;
48 cout << "\tpasswd <" << url.get_passwd () << ">" << endl; 48
49 Secret sec = url.get_secret ();
50 cout << "\tpasswd <" << sec.password () << ">" << endl;
51 sec.password_unref ();
52
49 cout << "\tauth <" << url.get_auth () << ">" << endl; 53 cout << "\tauth <" << url.get_auth () << ">" << endl;
50 cout << "\thost <" << url.get_host () << ">" << endl; 54 cout << "\thost <" << url.get_host () << ">" << endl;
51 cout << "\tport " << url.get_port () << endl; 55 cout << "\tport " << url.get_port () << endl;
......
...@@ -37,6 +37,7 @@ MU_CXX_INCLUDES = \ ...@@ -37,6 +37,7 @@ MU_CXX_INCLUDES = \
37 mutil.h\ 37 mutil.h\
38 pop3.h\ 38 pop3.h\
39 registrar.h\ 39 registrar.h\
40 secret.h\
40 stream.h\ 41 stream.h\
41 url.h 42 url.h
42 43
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
35 #include <mailutils/cpp/mutil.h> 35 #include <mailutils/cpp/mutil.h>
36 #include <mailutils/cpp/pop3.h> 36 #include <mailutils/cpp/pop3.h>
37 #include <mailutils/cpp/registrar.h> 37 #include <mailutils/cpp/registrar.h>
38 #include <mailutils/cpp/secret.h>
38 #include <mailutils/cpp/stream.h> 39 #include <mailutils/cpp/stream.h>
39 #include <mailutils/cpp/url.h> 40 #include <mailutils/cpp/url.h>
40 41
......
1 /*
2 GNU Mailutils -- a suite of utilities for electronic mail
3 Copyright (C) 2009 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA
19 */
20
21 #ifndef _MUCPP_SECRET_H
22 #define _MUCPP_SECRET_H
23
24 #include <string>
25 #include <errno.h>
26 #include <mailutils/secret.h>
27 #include <mailutils/cpp/error.h>
28
29 namespace mailutils
30 {
31
32 class Secret
33 {
34 protected:
35 mu_secret_t secret;
36 bool owner;
37
38 public:
39 Secret (const std::string&);
40 Secret (const char* str, size_t len);
41 Secret (const mu_secret_t);
42 ~Secret ();
43
44 std::string password ();
45 void password_unref ();
46 };
47
48 }
49
50 #endif // not _MUCPP_SECRET_H
51
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
27 #include <errno.h> 27 #include <errno.h>
28 #include <mailutils/url.h> 28 #include <mailutils/url.h>
29 #include <mailutils/cpp/error.h> 29 #include <mailutils/cpp/error.h>
30 #include <mailutils/cpp/secret.h>
30 31
31 namespace mailutils 32 namespace mailutils
32 { 33 {
...@@ -46,11 +47,11 @@ class Url ...@@ -46,11 +47,11 @@ class Url
46 long get_port (); 47 long get_port ();
47 std::string get_scheme (); 48 std::string get_scheme ();
48 std::string get_user (); 49 std::string get_user ();
49 std::string get_passwd ();
50 std::string get_auth (); 50 std::string get_auth ();
51 std::string get_host (); 51 std::string get_host ();
52 std::string get_path (); 52 std::string get_path ();
53 std::vector<std::string> get_query (); 53 std::vector<std::string> get_query ();
54 Secret& get_secret ();
54 55
55 std::string to_string (); 56 std::string to_string ();
56 friend std::ostream& operator << (std::ostream&, Url&); 57 friend std::ostream& operator << (std::ostream&, Url&);
......
...@@ -42,6 +42,7 @@ libmu_cpp_la_SOURCES = \ ...@@ -42,6 +42,7 @@ libmu_cpp_la_SOURCES = \
42 mutil.cc\ 42 mutil.cc\
43 pop3.cc\ 43 pop3.cc\
44 registrar.cc\ 44 registrar.cc\
45 secret.cc\
45 stream.cc\ 46 stream.cc\
46 url.cc 47 url.cc
47 48
......
1 /*
2 GNU Mailutils -- a suite of utilities for electronic mail
3 Copyright (C) 2009 Free Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301 USA
19 */
20
21 #include <mailutils/cpp/secret.h>
22
23 using namespace mailutils;
24
25 //
26 // Secret
27 //
28
29 Secret :: Secret (const std::string& str)
30 {
31 int status = mu_secret_create (&secret, str.c_str (), str.size ());
32 if (status)
33 throw Exception ("Secret::Secret", status);
34
35 this->owner = true;
36 }
37
38 Secret :: Secret (const char* str, size_t len)
39 {
40 int status = mu_secret_create (&secret, str, len);
41 if (status)
42 throw Exception ("Secret::Secret", status);
43
44 this->owner = true;
45 }
46
47 Secret :: Secret (const mu_secret_t secret)
48 {
49 if (secret == 0)
50 throw Exception ("Secret::Secret", EINVAL);
51
52 this->secret = secret;
53 this->owner = false;
54 }
55
56 Secret :: ~Secret ()
57 {
58 if (this->owner)
59 mu_secret_destroy (&secret);
60 }
61
62 std::string
63 Secret :: password ()
64 {
65 return std::string (mu_secret_password (secret));
66 }
67
68 void
69 Secret :: password_unref ()
70 {
71 mu_secret_password_unref (secret);
72 }
73
...@@ -95,16 +95,16 @@ Url :: get_user () ...@@ -95,16 +95,16 @@ Url :: get_user ()
95 return std::string (buf ? buf : ""); 95 return std::string (buf ? buf : "");
96 } 96 }
97 97
98 std::string 98 Secret&
99 Url :: get_passwd () 99 Url :: get_secret ()
100 { 100 {
101 const char* buf = NULL; 101 mu_secret_t c_secret;
102 int status = mu_url_sget_passwd (url, &buf); 102 int status = mu_url_get_secret (url, &c_secret);
103 if (status == MU_ERR_NOENT) 103 if (status == MU_ERR_NOENT)
104 return ""; 104 return *new Secret ("");
105 else if (status) 105 else if (status)
106 throw Exception ("Url::get_passwd", status); 106 throw Exception ("Url::get_secret", status);
107 return std::string (buf ? buf : ""); 107 return *new Secret (c_secret);
108 } 108 }
109 109
110 std::string 110 std::string
......