Commit 69cc4041 authored by Benoît Minisini's avatar Benoît Minisini

[GB.MIME]

* NEW: Mime.Encode() and Mime.Decode() are two new methods to encode to and
  decode from base64, quoted-printable or uuencode encoding.
* NEW: Support for message parts. The MimePart.Message property will return
  the MimeMessage embedded inside the part.


git-svn-id: svn://localhost/gambas/trunk@5119 867c0c6c-44f3-4631-809d-bfa615b0a4ec
parent 81cb7240
......@@ -27,6 +27,68 @@
#include "c_mime.h"
//-------------------------------------------------------------------------
BEGIN_METHOD(Mime_Encode, GB_STRING data; GB_INTEGER encoding)
GMimeEncoding menc;
int encoding = VARG(encoding);
size_t outlen;
char *outbuf;
switch(encoding)
{
case GMIME_CONTENT_ENCODING_BASE64:
case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
case GMIME_CONTENT_ENCODING_UUENCODE:
break;
default:
GB.Error("Bad encoding");
return;
}
g_mime_encoding_init_encode(&menc, encoding);
outlen = g_mime_encoding_outlen(&menc, LENGTH(data));
outbuf = GB.NewString(NULL, outlen);
outlen = g_mime_encoding_step(&menc, STRING(data), LENGTH(data), outbuf);
outbuf = GB.ExtendString(outbuf, outlen);
GB.ReturnString(GB.FreeStringLater(outbuf));
END_METHOD
BEGIN_METHOD(Mime_Decode, GB_STRING data; GB_INTEGER encoding)
GMimeEncoding menc;
int encoding = VARG(encoding);
size_t outlen;
char *outbuf;
switch(encoding)
{
case GMIME_CONTENT_ENCODING_BASE64:
case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
case GMIME_CONTENT_ENCODING_UUENCODE:
break;
default:
GB.Error("Bad encoding");
return;
}
g_mime_encoding_init_decode(&menc, encoding);
outlen = g_mime_encoding_outlen(&menc, LENGTH(data));
outbuf = GB.NewString(NULL, outlen);
outlen = g_mime_encoding_step(&menc, STRING(data), LENGTH(data), outbuf);
outbuf = GB.ExtendString(outbuf, outlen);
GB.ReturnString(GB.FreeStringLater(outbuf));
END_METHOD
//-------------------------------------------------------------------------
GB_DESC MimeDesc[] =
{
GB_DECLARE_VIRTUAL("Mime"),
......@@ -39,5 +101,8 @@ GB_DESC MimeDesc[] =
GB_CONSTANT("QuotedPrintable", "i", GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE),
GB_CONSTANT("UUEncode", "i", GMIME_CONTENT_ENCODING_UUENCODE),
GB_STATIC_METHOD("Encode", "s", Mime_Encode, "(Data)s(Encoding)i"),
GB_STATIC_METHOD("Decode", "s", Mime_Decode, "(Data)s(Encoding)i"),
GB_END_DECLARE
};
\ No newline at end of file
......@@ -32,43 +32,71 @@
//-------------------------------------------------------------------------
BEGIN_METHOD(MimeMessage_new, GB_STRING contents)
static GMimeMessage *_message = NULL;
GMimeMessage *message;
CMIMEMESSAGE *CMIMEMESSAGE_create(GMimeMessage *message)
{
CMIMEMESSAGE *mmsg;
if (!message)
return NULL;
if (MISSING(contents))
mmsg = (CMIMEMESSAGE *)g_object_get_data(G_OBJECT(message), "gambas-object");
if (!mmsg)
{
message = g_mime_message_new(FALSE);
// Add a default part?
_message = message;
g_object_ref(message);
mmsg = (CMIMEMESSAGE *)GB.New(GB.FindClass("MimeMessage"), NULL, NULL);
_message = NULL;
}
else
{
GMimeParser *parser;
GMimeStream *stream;
/* create a stream to read from memory */
stream = g_mime_stream_mem_new_with_buffer(STRING(contents), LENGTH(contents));
/* create a new parser object to parse the stream */
parser = g_mime_parser_new_with_stream (stream);
/* unref the stream (parser owns a ref, so this object does not actually get free'd until we destroy the parser) */
g_object_unref (stream);
/* parse the message from the stream */
message = g_mime_parser_construct_message (parser);
return mmsg;
}
//-------------------------------------------------------------------------
BEGIN_METHOD(MimeMessage_new, GB_STRING contents)
GMimeMessage *message = _message;
/* free the parser (and the stream) */
g_object_unref (parser);
if (!message)
{
if (MISSING(contents))
{
message = g_mime_message_new(FALSE);
// Add a default part?
}
else
{
GMimeParser *parser;
GMimeStream *stream;
/* create a stream to read from memory */
stream = g_mime_stream_mem_new_with_buffer(STRING(contents), LENGTH(contents));
/* create a new parser object to parse the stream */
parser = g_mime_parser_new_with_stream (stream);
/* unref the stream (parser owns a ref, so this object does not actually get free'd until we destroy the parser) */
g_object_unref(stream);
/* parse the message from the stream */
message = g_mime_parser_construct_message (parser);
/* free the parser (and the stream) */
g_object_unref(parser);
}
}
THIS->message = message;
g_object_set_data(G_OBJECT(message), "gambas-object", THIS);
END_METHOD
BEGIN_METHOD_VOID(MimeMessage_free)
g_object_set_data(G_OBJECT(MESSAGE), "gambas-object", NULL);
g_object_unref(MESSAGE);
END_METHOD
......@@ -144,6 +172,28 @@ BEGIN_METHOD_VOID(MimeMessage_ToString)
END_METHOD
/*BEGIN_PROPERTY(MimeMessage_Date)
if (READ_PROPERTY)
{
time_t time;
GB_DATE date;
g_mime_message_get_date(MESSAGE, &time, NULL);
GB.MakeDateFromTime(time, 0, &date);
GB.ReturnDate(&date);
}
else
{
GB_DATE_SERIAL *date;
struct tm time;
date = GB.SplitDate(PROP(GB_DATE));
time.tm_sec = date.
}
END_PROPERTY*/
//-------------------------------------------------------------------------
BEGIN_METHOD(MimeMessage_Headers_get, GB_STRING name)
......
......@@ -43,4 +43,6 @@ typedef
}
CMIMEMESSAGE;
CMIMEMESSAGE *CMIMEMESSAGE_create(GMimeMessage *message);
#endif /* __MAIN_H */
......@@ -27,12 +27,14 @@
#include <string.h>
#include "c_mimemessage.h"
#include "c_mimepart.h"
#define THIS ((CMIMEPART *)_object)
#define PART THIS->part
#define MPART ((GMimePart *)THIS->part)
#define MMPART ((GMimeMultipart *)THIS->part)
#define MSPART ((GMimeMessagePart *)THIS->part)
//-------------------------------------------------------------------------
......@@ -61,8 +63,6 @@ CMIMEPART *CMIMEPART_create(GMimeObject *part)
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
BEGIN_METHOD(MimePart_new, GB_STRING ctype)
GMimeObject *part;
......@@ -92,7 +92,7 @@ BEGIN_METHOD(MimePart_new, GB_STRING ctype)
}
PART = part;
g_object_ref(part);
//g_object_ref(part);
g_object_set_data(G_OBJECT(part), "gambas-object", (gpointer)THIS);
END_METHOD
......@@ -190,10 +190,14 @@ BEGIN_PROPERTY(MimePart_Data)
GMimeStream *stream;
GByteArray *array;
CHECK_PART();
if (READ_PROPERTY)
{
if (!GMIME_IS_PART(PART))
{
GB.ReturnNull();
return;
}
content = g_mime_part_get_content_object(MPART);
array = g_byte_array_new();
......@@ -213,6 +217,8 @@ BEGIN_PROPERTY(MimePart_Data)
}
else
{
CHECK_PART();
/* create the parts' content stream */
stream = g_mime_stream_mem_new_with_buffer(PSTRING(), PLENGTH());
......@@ -236,12 +242,18 @@ END_PROPERTY
BEGIN_PROPERTY(MimePart_FileName)
CHECK_PART();
if (READ_PROPERTY)
GB.ReturnNewZeroString(g_mime_part_get_filename(MPART));
{
if (GMIME_IS_PART(PART))
GB.ReturnNewZeroString(g_mime_part_get_filename(MPART));
else
GB.ReturnNull();
}
else
{
CHECK_PART();
g_mime_part_set_filename(MPART, GB.ToZeroString(PROP(GB_STRING)));
}
END_PROPERTY
......@@ -326,6 +338,33 @@ END_METHOD
//-------------------------------------------------------------------------
BEGIN_PROPERTY(MimePart_Message)
if (READ_PROPERTY)
{
if (!GMIME_IS_MESSAGE_PART(PART))
GB.ReturnNull();
else
GB.ReturnObject(CMIMEMESSAGE_create(g_mime_message_part_get_message(MSPART)));
}
else
{
CMIMEMESSAGE *mmsg;
if (!GMIME_IS_MESSAGE_PART(PART))
{
GB.Error("Not a message part");
return;
}
mmsg = VPROP(GB_OBJECT);
g_mime_message_part_set_message(MSPART, mmsg ? mmsg->message : NULL);
}
END_PROPERTY
//-------------------------------------------------------------------------
BEGIN_METHOD(MimePart_Headers_get, GB_STRING name)
GB.ReturnNewZeroString(g_mime_object_get_header(PART, GB.ToZeroString(ARG(name))));
......@@ -377,6 +416,7 @@ GB_DESC MimePartDesc[] =
GB_PROPERTY("ContentEncoding", "i", MimePart_ContentEncoding),
GB_PROPERTY("FileName", "s", MimePart_FileName),
GB_PROPERTY("Data", "s", MimePart_Data),
GB_PROPERTY("Message", "MimeMessage", MimePart_Message),
GB_PROPERTY_READ("Count", "i", MimePart_Count),
//GB_METHOD("Clear", NULL, MimePart_Clear, NULL),
......
[Component]
Author=Benoît Minisini
Alpha=1
State=NotFinished
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment