Commit b2a91252 b2a91252acdd910a3ac5e866c0ecc578d86c4f94 by Wojciech Polak

Add Folder class to libmu_cpp. Provide new examples lsf.cc and msg-send.cc.

* examples/cpp/lsf.cc, examples/cpp/msg-send.cc,
include/mailutils/cpp/folder.h, libmu_cpp/folder.cc: New files.
1 parent 24d5aa4c
......@@ -22,8 +22,10 @@ CXX_EXAMPLES = \
http\
iconv\
listop\
lsf\
mailcap\
mimetest\
msg-send\
murun\
sfrom\
url-parse
......@@ -43,8 +45,10 @@ addr_SOURCES = addr.cc
http_SOURCES = http.cc
iconv_SOURCES = iconv.cc
listop_SOURCES = listop.cc
lsf_SOURCES = lsf.cc
mailcap_SOURCES = mailcap.cc
mimetest_SOURCES = mimetest.cc
msg_send_SOURCES = msg-send.cc
murun_SOURCES = murun.cc
sfrom_SOURCES = sfrom.cc
url_parse_SOURCES = url-parse.cc
......@@ -59,5 +63,6 @@ LDADD =\
${MU_LIB_MH}\
${MU_LIB_MAILDIR}\
${MU_LIB_AUTH}\
${MU_LIB_MAILER}\
@MU_AUTHLIBS@\
${MU_LIB_MAILUTILS}
......
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2009 Free Software Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include <iostream>
#include <mailutils/cpp/mailutils.h>
using namespace std;
using namespace mailutils;
static int
enumfun (mu_folder_t folder, struct mu_list_response *resp, void *data)
{
printf ("%c%c %c %4d %s\n",
(resp->type & MU_FOLDER_ATTRIBUTE_DIRECTORY) ? 'd' : '-',
(resp->type & MU_FOLDER_ATTRIBUTE_FILE) ? 'f' : '-',
resp->separator,
resp->level,
resp->name);
return 0;
}
int
ls_folders (const string& fname, const string& ref, void* pattern, int level)
{
try {
Folder folder (fname);
folder.open ();
List list = folder.enumerate (ref, pattern, 0, level, enumfun, NULL);
cout << "Number of folders: " << list.count () << endl;
folder.close ();
}
catch (Exception& e)
{
if (e.status () == MU_ERR_NOENT) {
cout << "No folders matching " << ref << " " << pattern
<< " in " << fname << endl;
}
else {
cerr << e.method () << ": " << e.what () << endl;
return 1;
}
}
return 0;
}
int
main (int argc, char *argv[])
{
string folder;
string ref;
char *pattern = "*";
int level = 0;
switch (argc)
{
case 5:
level = atoi (argv[4]);
case 4:
pattern = argv[3];
case 3:
ref = argv[2];
case 2:
folder = argv[1];
break;
default:
error ("usage: lsf folder [ref] [pattern] [recursion-level]\n");
return 1;
}
register_all_mbox_formats ();
return ls_folders (folder, ref, pattern, level);
}
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2009 Free Software Foundation, Inc.
GNU Mailutils 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 3, or (at your option)
any later version.
GNU Mailutils 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 GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include <iostream>
#include <mailutils/cpp/mailutils.h>
using namespace std;
using namespace mailutils;
const char USAGE[] =
"usage: mailer [-hd] [-m mailer] [-f from] [to]..."
;
const char HELP[] =
" -h print this helpful message\n"
" -m a mailer URL (default is \"sendmail:\")\n"
" -f the envelope from address (default is from user environment)\n"
" to a list of envelope to addresses (default is from message)\n"
"\n"
"An RFC2822 formatted message is read from stdin and delivered using\n"
"the mailer.\n"
;
int
main (int argc, char *argv[])
{
int opt;
int optdebug = 0;
char *optmailer = "sendmail:";
char *optfrom = 0;
while ((opt = getopt (argc, argv, "hdm:f:")) != -1)
{
switch (opt)
{
case 'h':
cout << USAGE << endl << HELP;
return 0;
case 'd':
optdebug++;
break;
case 'm':
optmailer = optarg;
break;
case 'f':
optfrom = optarg;
break;
default:
cerr << USAGE << endl;
exit (1);
}
}
/* Register mailers. */
register_all_mailer_formats ();
Address from;
Address to;
if (optfrom)
{
from = Address (optfrom);
}
if (argv[optind])
{
char **av = argv + optind;
to = Address ((const char **) av, -1);
}
try {
StdioStream in (stdin, MU_STREAM_SEEKABLE);
in.open ();
Message msg;
msg.set_stream (in);
Mailer mailer (optmailer);
if (optdebug)
{
Debug debug = mailer.get_debug ();
debug.set_level (MU_DEBUG_LEVEL_UPTO (MU_DEBUG_PROT));
}
mailer.open ();
mailer.send_message (msg, from, to);
mailer.close ();
}
catch (Exception& e) {
cerr << e.method () << ": " << e.what () << endl;
exit (1);
}
return 0;
}
......@@ -23,6 +23,7 @@ MU_CXX_INCLUDES = \
debug.h\
error.h\
filter.h\
folder.h\
header.h\
iterator.h\
list.h\
......
......@@ -38,10 +38,14 @@ class Address
friend class Mailer;
public:
Address ();
Address (const std::string&);
Address (const char *sv[], size_t len);
Address (const mu_address_t);
~Address ();
Address& operator = (const Address&);
size_t get_count ();
bool is_group (size_t n);
......
......@@ -23,6 +23,7 @@
#include <string>
#include <mailutils/errno.h>
#include <mailutils/error.h>
namespace mailutils
{
......@@ -64,6 +65,22 @@ class Exception
}
};
inline int
verror (const char* fmt, va_list ap)
{
return mu_verror (fmt, ap);
}
inline int
error (const char* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
mu_verror (fmt, ap);
va_end (ap);
return 0;
}
}
#endif /* not _MUCPP_ERROR_H */
......
/*
GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA
*/
#ifndef _MUCPP_FOLDER_H
#define _MUCPP_FOLDER_H
#include <mailutils/folder.h>
#include <mailutils/cpp/list.h>
#include <mailutils/cpp/stream.h>
namespace mailutils
{
class Folder
{
protected:
mu_folder_t folder;
public:
Folder (const std::string& name);
Folder (const mu_folder_t);
Folder& operator = (const Folder&);
~Folder ();
void open ();
void open (int flag);
void close ();
List& list (const std::string& dirname, void* pattern, size_t max_level);
List& enumerate (const std::string& name, void* pattern,
int flags, size_t max_level,
mu_folder_enumerate_fp enumfun, void* enumdata);
Stream& get_stream ();
void set_stream (const Stream& stream);
};
}
#endif // not _MUCPP_FOLDER_H
......@@ -23,6 +23,7 @@
#include <string>
#include <mailutils/mailer.h>
#include <mailutils/cpp/debug.h>
#include <mailutils/cpp/message.h>
#include <mailutils/cpp/address.h>
......@@ -39,10 +40,13 @@ class Mailer
Mailer (const mu_mailer_t);
~Mailer ();
void open ();
void open (int flags);
void close ();
void send_message (const Message& msg, const Address& from,
const Address& to);
Debug& get_debug ();
};
}
......
......@@ -23,6 +23,7 @@
#include <mailutils/cpp/body.h>
#include <mailutils/cpp/error.h>
#include <mailutils/cpp/filter.h>
#include <mailutils/cpp/folder.h>
#include <mailutils/cpp/header.h>
#include <mailutils/cpp/iterator.h>
#include <mailutils/cpp/list.h>
......
......@@ -47,6 +47,7 @@ class Message
Header& get_header ();
Body& get_body ();
Stream& get_stream ();
void set_stream (const Stream& stream);
bool is_multipart ();
size_t size ();
......
......@@ -68,7 +68,8 @@ unregistrar_record (const mu_record_t record)
}
inline void register_all_mbox_formats ()
inline void
register_all_mbox_formats ()
{
mu_register_all_mbox_formats ();
}
......
......@@ -49,7 +49,9 @@ class Stream
// Friends
friend class FilterStream;
friend class FilterProgStream;
friend class Folder;
friend class Mailcap;
friend class Message;
friend class Pop3;
public:
......
......@@ -29,6 +29,7 @@ libmu_cpp_la_SOURCES = \
body.cc\
debug.cc\
filter.cc\
folder.cc\
header.cc\
iterator.cc\
list.cc\
......
......@@ -28,6 +28,11 @@ using namespace mailutils;
// Address
//
Address :: Address ()
{
addr = NULL;
}
Address :: Address (const std::string& str)
{
int status = mu_address_create (&addr, str.c_str ());
......@@ -35,6 +40,13 @@ Address :: Address (const std::string& str)
throw Exception ("Address::Address", status);
}
Address :: Address (const char *sv[], size_t len)
{
int status = mu_address_createv (&addr, sv, len);
if (status)
throw Exception ("Address::Address", status);
}
Address :: Address (const mu_address_t addr)
{
if (addr == 0)
......@@ -45,7 +57,20 @@ Address :: Address (const mu_address_t addr)
Address :: ~Address ()
{
mu_address_destroy (&addr);
if (addr)
mu_address_destroy (&addr);
}
Address&
Address :: operator = (const Address& a)
{
if (this != &a)
{
if (this->addr)
mu_address_destroy (&this->addr);
this->addr = mu_address_dup (a.addr);
}
return *this;
}
bool
......
/*
GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2009 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301 USA
*/
#include <mailutils/cpp/folder.h>
#include <mailutils/cpp/error.h>
#include <errno.h>
using namespace mailutils;
//
// Folder
//
Folder :: Folder (const std::string& name)
{
int status = mu_folder_create (&folder, name.c_str ());
if (status)
throw Exception ("Folder::Folder", status);
}
Folder :: Folder (const mu_folder_t folder)
{
if (folder == 0)
throw Exception ("Folder::Folder", EINVAL);
this->folder = folder;
}
Folder&
Folder :: operator = (const Folder& f)
{
if (this != &f)
{
if (this->folder)
mu_folder_destroy (&this->folder);
this->folder = f.folder;
}
return *this;
}
Folder :: ~Folder ()
{
mu_folder_destroy (&folder);
}
void
Folder :: open ()
{
int status = mu_folder_open (folder, MU_STREAM_READ);
if (status)
throw Exception ("Folder::open", status);
}
void
Folder :: open (int flag)
{
int status = mu_folder_open (folder, flag);
if (status)
throw Exception ("Folder::open", status);
}
void
Folder :: close ()
{
int status = mu_folder_close (folder);
if (status)
throw Exception ("Folder::close", status);
}
List&
Folder :: list (const std::string& dirname, void* pattern, size_t max_level)
{
mu_list_t c_list;
int status = mu_folder_list (folder, dirname.c_str (), pattern,
max_level, &c_list);
if (status)
throw Exception ("Folder::list", status);
return *new List (c_list);
}
List&
Folder :: enumerate (const std::string& name, void* pattern,
int flags, size_t max_level,
mu_folder_enumerate_fp enumfun, void* enumdata)
{
mu_list_t c_list;
int status = mu_folder_enumerate (folder, name.c_str (), pattern,
flags, max_level, &c_list,
enumfun, enumdata);
if (status)
throw Exception ("Folder::enumerate", status);
return *new List (c_list);
}
Stream&
Folder :: get_stream ()
{
mu_stream_t c_stream;
int status = mu_folder_get_stream (folder, &c_stream);
if (status)
throw Exception ("Folder::get_stream", status);
return *new Stream (c_stream);
}
void
Folder :: set_stream (const Stream& stream)
{
int status = mu_folder_set_stream (folder, stream.stm);
if (status)
throw Exception ("Folder::set_stream", status);
}
......@@ -48,6 +48,14 @@ Mailer :: ~Mailer ()
}
void
Mailer :: open ()
{
int status = mu_mailer_open (mailer, 0);
if (status)
throw Exception ("Mailer::open", status);
}
void
Mailer :: open (int flags)
{
int status = mu_mailer_open (mailer, flags);
......@@ -73,3 +81,15 @@ Mailer :: send_message (const Message& msg, const Address& from,
throw Exception ("Mailer::send_message", status);
}
Debug&
Mailer :: get_debug ()
{
mu_debug_t c_dbg;
int status = mu_mailer_get_debug (mailer, &c_dbg);
if (status)
throw Exception ("Mailer::get_debug", status);
return *new Debug (c_dbg);
}
......
......@@ -108,6 +108,15 @@ Message :: get_stream ()
return *new Stream (c_stream);
}
void
Message :: set_stream (const Stream& stream)
{
int status = mu_message_set_stream (msg, stream.stm, this);
if (status)
throw Exception ("Message::set_stream", status);
}
bool
Message :: is_multipart ()
{
......