Adding start of new Mail Service.

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7723 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
drbob 2014-12-02 13:22:48 +00:00
parent 6d32524837
commit 6588eafb99
14 changed files with 1321 additions and 1 deletions

View File

@ -49,6 +49,7 @@
typedef SSLIdType RsPeerId ; typedef SSLIdType RsPeerId ;
typedef PGPIdType RsPgpId ; typedef PGPIdType RsPgpId ;
typedef Sha1CheckSum RsFileHash ; typedef Sha1CheckSum RsFileHash ;
typedef Sha1CheckSum RsMessageId ;
const uint32_t FT_STATE_FAILED = 0x0000 ; const uint32_t FT_STATE_FAILED = 0x0000 ;
const uint32_t FT_STATE_OKAY = 0x0001 ; const uint32_t FT_STATE_OKAY = 0x0001 ;

View File

@ -84,7 +84,8 @@ const uint8_t QOS_PRIORITY_RS_HEARTBEAT_PULSE = 8 ;
// //
const uint8_t QOS_PRIORITY_RS_CHAT_ITEM = 7 ; const uint8_t QOS_PRIORITY_RS_CHAT_ITEM = 7 ;
const uint8_t QOS_PRIORITY_RS_CHAT_AVATAR_ITEM = 2 ; const uint8_t QOS_PRIORITY_RS_CHAT_AVATAR_ITEM = 2 ;
const uint8_t QOS_PRIORITY_RS_MSG_ITEM = 2 ; const uint8_t QOS_PRIORITY_RS_MSG_ITEM = 2 ; // depreciated.
const uint8_t QOS_PRIORITY_RS_MAIL_ITEM = 2 ; // new mail service
const uint8_t QOS_PRIORITY_RS_STATUS_ITEM = 2 ; const uint8_t QOS_PRIORITY_RS_STATUS_ITEM = 2 ;
// RTT // RTT

View File

@ -0,0 +1,345 @@
/*
* libretroshare/src/serialiser: rsmailtransportitems.cc
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include <stdexcept>
#include <time.h>
#include "serialiser/rsbaseserial.h"
#include "serialiser/rsmailtransportitems.h"
#include "serialiser/rstlvbase.h"
/***
#define RSSERIAL_DEBUG 1
***/
#include <iostream>
/*************************************************************************/
// RsMailMimeItem
bool RsMailChunkItem::isPartial()
{
return (mPartCount != 1);
}
void RsMailChunkItem::clear()
{
mMailId.TlvClear();
mPartCount = 0;
mMailIndex = 0;
mWholeMailId.clear();
mMessage.clear();
}
std::ostream &RsMailChunkItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsMailChunkItem", indent);
uint16_t int_Indent = indent + 2;
mMailId.print(out, int_Indent);
printIndent(out, int_Indent);
out << "PartCount: " << mPartCount << std::endl;
printIndent(out, int_Indent);
out << "MailIndex: " << mMailIndex << std::endl;
printIndent(out, int_Indent);
out << "WholeMailId: " << mWholeMailId.toStdString() << std::endl;
printIndent(out, int_Indent);
out << "mMessage: " << mMessage << std::endl;
printRsItemEnd(out, "RsMailChunkItem", indent);
return out;
}
uint32_t RsMailChunkItem::serial_size()
{
uint32_t s = 8; /* header */
s += mMailId.TlvSize(); /* mMailId */
s += 2; /* mPartCount */
s += 2; /* mMailIndex */
s += mWholeMailId.serial_size(); /* mRecvTime */
s += GetTlvStringSize(mMessage);
return s;
}
/* serialise the data to the buffer */
bool RsMailChunkItem::serialise(void *data, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsMailChunkItem::serialise() Header: " << ok << std::endl;
std::cerr << "RsMailChunkItem::serialise() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= mMailId.SetTlv(data, tlvsize, &offset);
ok &= setRawUInt16(data, tlvsize, &offset, mPartCount);
ok &= setRawUInt16(data, tlvsize, &offset, mMailIndex);
ok &= mWholeMailId.serialise(data, tlvsize, offset);
ok &= SetTlvString(data, tlvsize, &offset, TLV_TYPE_STR_MSG, mMessage);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsMailMimeSerialiser::serialiseItem() Size Error! " << std::endl;
}
return ok;
}
RsMailChunkItem *RsMailTransportSerialiser::deserialiseChunkItem(void *data, uint32_t *pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(mServiceType != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_MAIL_TRANSPORT_CHUNK != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < rssize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = rssize;
bool ok = true;
/* ready to load */
RsMailChunkItem *item = new RsMailChunkItem(mServiceType);
item->clear();
/* skip the header */
offset += 8;
/* get mandatory parts first */
ok &= item->mMailId.GetTlv(data, rssize, &offset);
ok &= getRawUInt16(data, rssize, &offset, &(item->mPartCount));
ok &= getRawUInt16(data, rssize, &offset, &(item->mMailIndex));
ok &= item->mWholeMailId.deserialise(data, rssize, offset);
ok &= GetTlvString(data, rssize, &offset, TLV_TYPE_STR_MSG, item->mMessage);
if (offset != rssize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
void RsMailAckItem::clear()
{
mMailId.TlvClear();
}
std::ostream& RsMailAckItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsMailAckItem", indent);
uint16_t int_Indent = indent + 2;
mMailId.print(out, int_Indent);
printRsItemEnd(out, "RsMailAckItem", indent);
return out;
}
uint32_t RsMailAckItem::serial_size()
{
uint32_t s = 8; /* header */
s += mMailId.TlvSize();
return s;
}
bool RsMailAckItem::serialise(void *data, uint32_t& pktsize)
{
uint32_t tlvsize = serial_size() ;
uint32_t offset = 0;
if (pktsize < tlvsize)
return false; /* not enough space */
pktsize = tlvsize;
bool ok = true;
ok &= setRsItemHeader(data, tlvsize, PacketId(), tlvsize);
#ifdef RSSERIAL_DEBUG
std::cerr << "RsMailAckItem::serialise() Header: " << ok << std::endl;
std::cerr << "RsMailAckItem::serialise() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= mMailId.SetTlv(data, tlvsize, &offset);
if (offset != tlvsize)
{
ok = false;
std::cerr << "RsMailAckItem::serialise() Size Error! " << std::endl;
}
return ok;
}
RsMailAckItem* RsMailTransportSerialiser::deserialiseAckItem(void *data,uint32_t* pktsize)
{
/* get the type and size */
uint32_t rstype = getRsItemId(data);
uint32_t rssize = getRsItemSize(data);
uint32_t offset = 0;
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(mServiceType != getRsItemService(rstype)) ||
(RS_PKT_SUBTYPE_MAIL_TRANSPORT_ACK != getRsItemSubType(rstype)))
{
return NULL; /* wrong type */
}
if (*pktsize < rssize) /* check size */
return NULL; /* not enough data */
/* set the packet length */
*pktsize = rssize;
bool ok = true;
/* ready to load */
RsMailAckItem *item = new RsMailAckItem(mServiceType);
item->clear();
/* skip the header */
offset += 8;
/* get mandatory parts first */
ok &= item->mMailId.GetTlv(data, rssize, &offset);
if (offset != rssize)
{
/* error */
delete item;
return NULL;
}
if (!ok)
{
delete item;
return NULL;
}
return item;
}
/**************************/
RsItem* RsMailTransportSerialiser::deserialise(void *data, uint32_t *pktsize)
{
#ifdef RSSERIAL_DEBUG
std::cerr << "RsMailTransportSerialiser::deserialise()" << std::endl;
#endif
/* get the type and size */
uint32_t rstype = getRsItemId(data);
if ((RS_PKT_VERSION_SERVICE != getRsItemVersion(rstype)) ||
(mServiceType != getRsItemService(rstype)))
{
return NULL; /* wrong type */
}
switch(getRsItemSubType(rstype))
{
case RS_PKT_SUBTYPE_MAIL_TRANSPORT_CHUNK:
return deserialiseChunkItem(data, pktsize);
break;
case RS_PKT_SUBTYPE_MAIL_TRANSPORT_ACK:
return deserialiseAckItem(data, pktsize);
break;
default:
return NULL;
break;
}
return NULL;
}
bool RsMailTransportSerialiser::serialise(RsItem *item, void *data, uint32_t *size){
if (item->PacketService() != mServiceType)
{
return false;
}
return dynamic_cast<RsMailTransportItem*>(item)->serialise(data,*size);
}
/*************************************************************************/

View File

@ -0,0 +1,145 @@
#ifndef RS_MAIL_TRANSPORT_ITEMS_H
#define RS_MAIL_TRANSPORT_ITEMS_H
/*
* libretroshare/src/serialiser: rsmailtransportitems.h
*
* RetroShare Serialiser.
*
* Copyright 2014-2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include <map>
#include "retroshare/rstypes.h"
#include "serialiser/rsserviceids.h"
#include "serialiser/rsserial.h"
#include "serialiser/rstlvmail.h"
/**************************************************************************/
// for defining tags themselves and msg tags
const uint8_t RS_PKT_SUBTYPE_MAIL_TRANSPORT_CHUNK = 0x01;
const uint8_t RS_PKT_SUBTYPE_MAIL_TRANSPORT_ACK = 0x02;
/**************************************************************************/
// These Types are generic - and usable by all MailTransport services.
// However they must be tweaked to indicate the Service ID before sending.
class RsMailTransportItem: public RsItem
{
public:
RsMailTransportItem(uint16_t service_type, uint8_t msg_subtype) : RsItem(RS_PKT_VERSION_SERVICE,service_type,msg_subtype)
{
setPriorityLevel(QOS_PRIORITY_RS_MAIL_ITEM) ;
}
RsMailTransportItem(uint8_t msg_subtype) : RsItem(RS_PKT_VERSION_SERVICE,0,msg_subtype)
{
setPriorityLevel(QOS_PRIORITY_RS_MAIL_ITEM) ;
}
virtual ~RsMailTransportItem() {}
virtual void clear() {}
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0) = 0 ;
virtual bool serialise(void *data,uint32_t& size) = 0 ;
virtual uint32_t serial_size() = 0 ;
};
class RsMailChunkItem: public RsMailTransportItem
{
public:
RsMailChunkItem() :RsMailTransportItem(RS_PKT_SUBTYPE_MAIL_TRANSPORT_CHUNK) {}
RsMailChunkItem(uint16_t service_type) :RsMailTransportItem(service_type, RS_PKT_SUBTYPE_MAIL_TRANSPORT_CHUNK) {}
virtual ~RsMailChunkItem() {}
virtual void clear();
virtual bool serialise(void *data,uint32_t& size) ;
virtual uint32_t serial_size() ;
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
// extra functions.
bool isPartial();
// Serialised.
RsTlvMailId mMailId;
uint16_t mPartCount;
uint16_t mMailIndex;
RsMessageId mWholeMailId;
std::string mMessage;
};
class RsMailAckItem : public RsMailTransportItem
{
public:
RsMailAckItem() :RsMailTransportItem(RS_PKT_SUBTYPE_MAIL_TRANSPORT_ACK) {}
RsMailAckItem(uint16_t service_type) :RsMailTransportItem(service_type, RS_PKT_SUBTYPE_MAIL_TRANSPORT_ACK) {}
virtual std::ostream &print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ;
virtual uint32_t serial_size() ;
virtual ~RsMailAckItem() {}
virtual void clear();
// serialised
RsTlvMailId mMailId;
};
class RsMailTransportSerialiser: public RsSerialType
{
public:
RsMailTransportSerialiser(uint16_t service_type)
:RsSerialType(RS_PKT_VERSION_SERVICE, service_type),
mServiceType(service_type) {}
virtual ~RsMailTransportSerialiser() {}
virtual uint32_t size(RsItem *item)
{
return dynamic_cast<RsMailTransportItem*>(item)->serial_size() ;
}
virtual bool serialise(RsItem *i, void *d, uint32_t *s);
virtual RsItem * deserialise(void *data, uint32_t *size);
private:
virtual RsMailChunkItem *deserialiseChunkItem(void *data, uint32_t *size);
virtual RsMailAckItem *deserialiseAckItem(void *data, uint32_t *size);
uint16_t mServiceType;
};
/**************************************************************************/
#endif /* RS_MAIL_TRANSPORT_ITEMS_H */

View File

@ -175,6 +175,11 @@ uint16_t RsItem::PacketService() const
return (type >> 8) & 0xFFFF; return (type >> 8) & 0xFFFF;
} }
void RsItem::setPacketService(uint16_t service)
{
type &= 0xFF0000FF;
type |= (uint32_t) (service << 8);
}
RsSerialType::RsSerialType(uint32_t t) RsSerialType::RsSerialType(uint32_t t)

View File

@ -101,6 +101,7 @@ class RsItem: public RsMemoryManagement::SmallObject
/* For Service Packets */ /* For Service Packets */
RsItem(uint8_t ver, uint16_t service, uint8_t subtype); RsItem(uint8_t ver, uint16_t service, uint8_t subtype);
uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */ uint16_t PacketService() const; /* combined Packet class/type (mid 16bits) */
void setPacketService(uint16_t service);
inline uint8_t priority_level() const { return _priority_level ;} inline uint8_t priority_level() const { return _priority_level ;}
inline void setPriorityLevel(uint8_t l) { _priority_level = l ;} inline void setPriorityLevel(uint8_t l) { _priority_level = l ;}

View File

@ -53,6 +53,11 @@ const uint16_t RS_SERVICE_TYPE_GROUTER = 0x0018;
const uint16_t RS_SERVICE_TYPE_SERVICEINFO = 0x0020; const uint16_t RS_SERVICE_TYPE_SERVICEINFO = 0x0020;
/* Bandwidth Control */ /* Bandwidth Control */
const uint16_t RS_SERVICE_TYPE_BWCTRL = 0x0021; const uint16_t RS_SERVICE_TYPE_BWCTRL = 0x0021;
// New Mail Service (replace old Msg Service)
const uint16_t RS_SERVICE_TYPE_MAIL = 0x0022;
const uint16_t RS_SERVICE_TYPE_DIRECT_MAIL = 0x0023;
const uint16_t RS_SERVICE_TYPE_DISTANT_MAIL = 0x0024;
const uint16_t RS_SERVICE_TYPE_GWEMAIL_MAIL = 0x0025;
// Non essential services. // Non essential services.
const uint16_t RS_SERVICE_TYPE_BANLIST = 0x0101; const uint16_t RS_SERVICE_TYPE_BANLIST = 0x0101;

View File

@ -229,6 +229,8 @@ const uint16_t TLV_TYPE_DSDV_ENTRY_SET= 0x1082;
const uint16_t TLV_TYPE_BAN_ENTRY = 0x1090; const uint16_t TLV_TYPE_BAN_ENTRY = 0x1090;
const uint16_t TLV_TYPE_BAN_LIST = 0x1091; const uint16_t TLV_TYPE_BAN_LIST = 0x1091;
const uint16_t TLV_TYPE_MSG_ADDRESS = 0x10A0;
const uint16_t TLV_TYPE_MSG_ID = 0x10A1;
const uint32_t RSTLV_IMAGE_TYPE_PNG = 0x0001; const uint32_t RSTLV_IMAGE_TYPE_PNG = 0x0001;

View File

@ -0,0 +1,265 @@
/*
* libretroshare/src/serialiser: rstlvmsgs.cc
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rstlvbase.h"
#include "serialiser/rstlvmail.h"
#include "serialiser/rsbaseserial.h"
/************************************* RsTlvIpAddress ************************************/
RsTlvMailAddress::RsTlvMailAddress()
:RsTlvItem()
{
return;
}
void RsTlvMailAddress::TlvClear()
{
mAddressType = 0;
mAddress.clear();
}
uint32_t RsTlvMailAddress::TlvSize() const
{
uint32_t s = TLV_HEADER_SIZE;
s += 4; // TYPE.
s += GetTlvStringSize(mAddress);
return s;
}
bool RsTlvMailAddress::SetTlv(void *data, uint32_t size, uint32_t *offset) const
{
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
return false; /* not enough space */
bool ok = true;
/* start at data[offset] */
/* add mandatory parts first */
ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_MSG_ADDRESS, tlvsize);
ok &= setRawUInt32(data, tlvend, offset, mAddressType);
ok &= SetTlvString(data, tlvend, offset, TLV_TYPE_STR_MSG, mAddress);
return ok;
}
bool RsTlvMailAddress::GetTlv(void *data, uint32_t size, uint32_t *offset)
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
return false; /* not enough space */
if (tlvtype != TLV_TYPE_MSG_ADDRESS) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
ok &= getRawUInt32(data, tlvend, offset, &(mAddressType));
ok &= GetTlvString(data, tlvend, offset, TLV_TYPE_STR_MSG, mAddress);
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvIpAddress::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
std::ostream &RsTlvMailAddress::print(std::ostream &out, uint16_t indent) const
{
printBase(out, "RsTlvMailAddress", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "AddressType:" << mAddressType << std::endl;
printIndent(out, int_Indent);
out << "Address:" << mAddress << std::endl;
printEnd(out, "RsTlvMailAddress", indent);
return out;
}
/************************************* RsTlvIpAddressInfo ************************************/
RsTlvMailId::RsTlvMailId()
:RsTlvItem(), mSentTime(0)
{
mMailFrom.TlvClear();
mMailDest.TlvClear();
mMailId.clear();
return;
}
void RsTlvMailId::TlvClear()
{
mMailFrom.TlvClear();
mMailDest.TlvClear();
mSentTime = 0;
mMailId.clear();
}
uint32_t RsTlvMailId::TlvSize() const
{
uint32_t s = TLV_HEADER_SIZE;
s += mMailFrom.TlvSize();
s += mMailDest.TlvSize();
s += 4; // SentTime
s += mMailId.serial_size();
return s;
}
bool RsTlvMailId::SetTlv(void *data, uint32_t size, uint32_t *offset) const
{
/* must check sizes */
uint32_t tlvsize = TlvSize();
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend)
return false; /* not enough space */
bool ok = true;
/* start at data[offset] */
/* add mandatory parts first */
ok &= SetTlvBase(data, tlvend, offset, TLV_TYPE_MSG_ID, tlvsize);
ok &= mMailFrom.SetTlv(data, tlvend, offset);
ok &= mMailDest.SetTlv(data, tlvend, offset);
ok &= setRawUInt32(data, tlvend, offset, mSentTime);
ok &= mMailId.serialise(data, tlvend, *offset);
return ok;
}
bool RsTlvMailId::GetTlv(void *data, uint32_t size, uint32_t *offset)
{
if (size < *offset + TLV_HEADER_SIZE)
return false;
uint16_t tlvtype = GetTlvType( &(((uint8_t *) data)[*offset]) );
uint32_t tlvsize = GetTlvSize( &(((uint8_t *) data)[*offset]) );
uint32_t tlvend = *offset + tlvsize;
if (size < tlvend) /* check size */
return false; /* not enough space */
if (tlvtype != TLV_TYPE_MSG_ID) /* check type */
return false;
bool ok = true;
/* ready to load */
TlvClear();
/* skip the header */
(*offset) += TLV_HEADER_SIZE;
ok &= mMailFrom.GetTlv(data, tlvend, offset);
ok &= mMailDest.GetTlv(data, tlvend, offset);
ok &= getRawUInt32(data, tlvend, offset, &(mSentTime));
ok &= mMailId.deserialise(data, tlvend, *offset);
/***************************************************************************
* NB: extra components could be added (for future expansion of the type).
* or be present (if this code is reading an extended version).
*
* We must chew up the extra characters to conform with TLV specifications
***************************************************************************/
if (*offset != tlvend)
{
#ifdef TLV_DEBUG
std::cerr << "RsTlvMailId::GetTlv() Warning extra bytes at end of item";
std::cerr << std::endl;
#endif
*offset = tlvend;
}
return ok;
}
std::ostream &RsTlvMailId::print(std::ostream &out, uint16_t indent) const
{
printBase(out, "RsTlvMailId", indent);
uint16_t int_Indent = indent + 2;
mMailFrom.print(out, int_Indent);
mMailDest.print(out, int_Indent);
printIndent(out, int_Indent);
out << "SentTime:" << mSentTime;
out << std::endl;
printIndent(out, int_Indent);
out << "mMailId:" << mMailId.toStdString();
out << std::endl;
printEnd(out, "RsTlvMailId", indent);
return out;
}

View File

@ -0,0 +1,71 @@
#pragma once
/*
* libretroshare/src/serialiser: rstlvmail.h
*
* RetroShare Serialiser.
*
* Copyright 2014 by Robert Fernie
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
/*******************************************************************
* These are the Compound TLV structures that must be (un)packed.
******************************************************************/
#include <retroshare/rstypes.h>
#include "serialiser/rstlvitem.h"
#include "util/rsnet.h"
#include <list>
class RsTlvMailAddress: public RsTlvItem
{
public:
RsTlvMailAddress();
virtual ~RsTlvMailAddress() { return; }
virtual uint32_t TlvSize() const;
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const;
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset);
virtual std::ostream &print(std::ostream &out, uint16_t indent) const;
uint32_t mAddressType;
std::string mAddress;
};
class RsTlvMailId: public RsTlvItem
{
public:
RsTlvMailId();
virtual ~RsTlvMailId() { return; }
virtual uint32_t TlvSize() const;
virtual void TlvClear();
virtual bool SetTlv(void *data, uint32_t size, uint32_t *offset) const;
virtual bool GetTlv(void *data, uint32_t size, uint32_t *offset);
virtual std::ostream &print(std::ostream &out, uint16_t indent) const;
RsTlvMailAddress mMailFrom;
RsTlvMailAddress mMailDest;
uint32_t mSentTime;
RsMessageId mMailId;
};

View File

@ -0,0 +1,254 @@
/*
* libretroshare/src/services directmailservice.cc
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "services/mail/directmailservice.h"
#include "util/rsdebug.h"
//#define MSG_DEBUG 1
const int directmailzone = 54319;
using namespace Rs::Mail;
DirectMailService::DirectMailService(p3ServiceControl *sc)
:MailTransport(DEFAULT_MAX_MESSAGE_SIZE, RS_SERVICE_TYPE_DIRECT_MAIL),
p3Service(), mServiceCtrl(sc), mMsgMtx("DirectMailService")
{
mSerialiser = new RsMailTransportSerialiser(RS_SERVICE_TYPE_DIRECT_MAIL);
addSerialType(mSerialiser);
}
const std::string MSGDIRECT_APP_NAME = "msgdirect";
const uint16_t MSGDIRECT_APP_MAJOR_VERSION = 1;
const uint16_t MSGDIRECT_APP_MINOR_VERSION = 0;
const uint16_t MSGDIRECT_MIN_MAJOR_VERSION = 1;
const uint16_t MSGDIRECT_MIN_MINOR_VERSION = 0;
RsServiceInfo DirectMailService::getServiceInfo()
{
return RsServiceInfo(RS_SERVICE_TYPE_DIRECT_MAIL,
MSGDIRECT_APP_NAME,
MSGDIRECT_APP_MAJOR_VERSION,
MSGDIRECT_APP_MINOR_VERSION,
MSGDIRECT_MIN_MAJOR_VERSION,
MSGDIRECT_MIN_MINOR_VERSION);
}
bool DirectMailService::CanSend(const RsTlvMailAddress &addr)
{
/* we can send if MessageAddress is a peerID,
* and the peer ID is a friend.
*/
if (addr.mAddressType == ADDRESS_TYPE_PEER_ID)
{
return true;
}
return false;
}
bool DirectMailService::sendMail(RsMailChunkItem *item)
{
// set the right serviceId.
item->setPacketService(RS_SERVICE_TYPE_DIRECT_MAIL);
if (!attemptToSend(item))
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
mOutgoingMsgs.push_back(item);
}
return true;
}
RsMailChunkItem *DirectMailService::recvMail()
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
if (!mIncomingMsgs.empty())
{
RsMailChunkItem *item = mIncomingMsgs.front();
mIncomingMsgs.pop_front();
return item;
}
return NULL;
}
RsPeerId convertAddressToPeerId(RsMailAckItem *item)
{
RsPeerId id;
return id;
}
bool DirectMailService::sendMailAck(RsMailAckItem *item)
{
// set the right serviceId.
item->setPacketService(RS_SERVICE_TYPE_DIRECT_MAIL);
item->PeerId(convertAddressToPeerId(item));
if (!attemptToSend(item))
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
mOutgoingMsgs.push_back(item);
}
return true;
}
RsMailAckItem *DirectMailService::recvMailAck()
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
if (!mIncomingAckMsgs.empty())
{
RsMailAckItem *item = mIncomingAckMsgs.front();
mIncomingMsgs.pop_front();
return item;
}
return NULL;
}
int DirectMailService::tick()
{
rslog(RSL_DEBUG_BASIC, directmailzone,
"DirectMailService::tick()");
incomingMsgs();
return 0;
}
int DirectMailService::status()
{
rslog(RSL_DEBUG_BASIC, directmailzone,
"DirectMailService::status()");
return 1;
}
void DirectMailService::statusChange(const std::list<pqiServicePeer> &plist)
{
/* should do it properly! */
/* only do this when a new peer is connected */
bool newPeers = false;
std::list<pqiServicePeer>::const_iterator it;
for(it = plist.begin(); it != plist.end(); it++)
{
if (it->actions & RS_SERVICE_PEER_CONNECTED)
{
newPeers = true;
}
}
if (newPeers)
{
checkOutgoingMessages();
}
}
int DirectMailService::incomingMsgs()
{
RsItem *item;
int i = 0;
while((item = recvItem()) != NULL)
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
RsMailChunkItem *mi = dynamic_cast<RsMailChunkItem *>(item);
RsMailAckItem *mai = dynamic_cast<RsMailAckItem *>(item);
if (mi)
{
mIncomingMsgs.push_back(mi);
}
else if (mai)
{
mIncomingAckMsgs.push_back(mai);
}
else
{
std::cerr << "DirectMailService::incomingMsgs() Unknown incoming Msg";
std::cerr << std::endl;
delete item;
}
++i;
}
return i;
}
// This is a Local copy of an Outgoing message - either SentBox or Draft.
bool DirectMailService::attemptToSend(RsItem *mi)
{
RsPeerId pid = mi->PeerId();
if (mServiceCtrl->isPeerConnected(getServiceInfo().mServiceType, pid))
{
sendItem(mi);
return true;
}
return false;
}
void DirectMailService::checkOutgoingMessages()
{
/* iterate through the outgoing queue and attempt to send.
*/
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
std::list<RsItem *>::iterator mit;
for(mit = mOutgoingMsgs.begin(); mit != mOutgoingMsgs.end(); mit++)
{
if (attemptToSend(*mit))
{
/* actually sent the message */
mit = mOutgoingMsgs.erase(mit);
}
else
{
mit++;
}
}
}
bool DirectMailService::haveIncomingMail()
{
RsStackMutex stack(mMsgMtx); /********** STACK LOCKED MTX ******/
if (!mIncomingMsgs.empty())
{
return true;
}
if (!mIncomingAckMsgs.empty())
{
return true;
}
return false;
}

View File

@ -0,0 +1,92 @@
/*
* libretroshare/src/services/mail directmailservice.h
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#ifndef DIRECT_MAIL_SERVICE_HEADER
#define DIRECT_MAIL_SERVICE_HEADER
#include "services/mail/mailtransport.h"
#include "services/p3service.h"
#include "pqi/pqiservicemonitor.h"
#include "util/rsthreads.h"
namespace Rs
{
namespace Mail
{
class DirectMailService: public MailTransport, public p3Service, public pqiServiceMonitor
{
public:
DirectMailService(p3ServiceControl *sc);
virtual RsServiceInfo getServiceInfo();
/******* MailTransport Interface ********************************************************/
virtual bool CanSend(const RsTlvMailAddress &addr);
virtual bool haveIncomingMail();
virtual bool sendMail(RsMailChunkItem *msg);
virtual RsMailChunkItem *recvMail();
virtual bool sendMailAck(RsMailAckItem *ack);
virtual RsMailAckItem *recvMailAck();
/******* MsgTransport Interface ********************************************************/
int tick();
int status();
/*** Overloaded from pqiMonitor ***/
virtual void statusChange(const std::list<pqiServicePeer> &plist);
/*** Overloaded from pqiMonitor ***/
private:
int incomingMsgs();
void checkOutgoingMessages();
bool attemptToSend(RsItem *item);
p3ServiceControl *mServiceCtrl;
/* Mutex Required for stuff below */
RsMutex mMsgMtx;
RsMailTransportSerialiser *mSerialiser ;
/* outgoing messages */
std::list<RsItem *> mOutgoingMsgs;
/* incoming msgs */
std::list<RsMailChunkItem *> mIncomingMsgs;
std::list<RsMailAckItem *> mIncomingAckMsgs;
};
} // namspace Msg
} // namspace Rs
#endif // DIRECT_MESSAGE_SERVICE_HEADER

View File

@ -0,0 +1,56 @@
/*
* libretroshare/src/services/mail mailtransport.cc
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "services/mail/mailtransport.h"
using namespace Rs::Mail;
MailTransport::MailTransport(uint32_t max_size, uint16_t transport_type)
:mEnabled(true), mMaxSize(max_size), mTransportType(transport_type)
{
return;
}
bool MailTransport::isEnabled()
{
return mEnabled;
}
void MailTransport::setEnabled(bool enabled)
{
mEnabled = enabled;
}
uint32_t MailTransport::getMaxSize()
{
return mMaxSize;
}
uint16_t MailTransport::getTransportType()
{
return mTransportType;
}

View File

@ -0,0 +1,77 @@
#pragma once
#ifndef RS_MAIL_TRANSPORT_HEADER
#define RS_MAIL_TRANSPORT_HEADER
/*
* libretroshare/src/services/mail mailtransport.h
*
* Services for RetroShare.
*
* Copyright 2014 by Robert Fernie.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
#include "serialiser/rsmailtransportitems.h"
namespace Rs
{
namespace Mail
{
const uint32_t DEFAULT_MAX_MESSAGE_SIZE = (64 * 1024);
// To decide how this will work!
const uint32_t ADDRESS_TYPE_PEER_ID = 0x001;
const uint32_t ADDRESS_TYPE_GXS_ID = 0x002;
const uint32_t ADDRESS_TYPE_EMAIL_ID = 0x003;
class MailTransport
{
public:
MailTransport(uint32_t max_size, uint16_t transport_type);
virtual ~MailTransport();
virtual bool CanSend(const RsTlvMailAddress &addr) = 0;
virtual bool haveIncomingMail() = 0;
virtual bool sendMail(RsMailChunkItem *msg) = 0;
virtual RsMailChunkItem *recvMail() = 0;
virtual bool sendMailAck(RsMailAckItem *ack) = 0;
virtual RsMailAckItem *recvMailAck() = 0;
virtual bool isEnabled();
virtual void setEnabled(bool enabled);
uint32_t getMaxSize();
uint16_t getTransportType();
private:
bool mEnabled;
uint32_t mMaxSize;
uint16_t mTransportType;
};
} // namespace Mail
} // namespace Rs
#endif