diff --git a/libretroshare/src/rsiface/rsiface.h b/libretroshare/src/rsiface/rsiface.h index 04f677cff..79a583f4d 100644 --- a/libretroshare/src/rsiface/rsiface.h +++ b/libretroshare/src/rsiface/rsiface.h @@ -98,24 +98,20 @@ virtual void unlockData() = 0; const PersonInfo *getPerson(std::string id); const DirInfo *getDirectory(std::string id, std::string path); - const std::list &getMessages() - { return mMessageList; } - const std::map &getChannels() { return mChannelMap; } const std::map &getOurChannels() { return mChannelOwnMap; } - const MessageInfo *getMessage(std::string cId, std::string mId); - const MessageInfo *getChannelMsg(std::string chId, std::string mId); + //const MessageInfo *getChannelMsg(std::string chId, std::string mId); - std::list getChatNew() - { - std::list newList = mChatList; - mChatList.clear(); - return newList; - } + //std::list getChatNew() + // { + // std::list newList = mChatList; + // mChatList.clear(); + // return newList; + // } const std::list &getRecommendList() { return mRecommendList; } @@ -162,10 +158,10 @@ void fillLists(); /* create some dummy data to display */ std::list mRemoteDirList; std::list mLocalDirList; std::list mTransferList; - std::list mMessageList; + //std::list mMessageList; std::map mChannelMap; std::map mChannelOwnMap; - std::list mChatList; + //std::list mChatList; std::list mRecommendList; bool mChanged[NumOfFlags]; @@ -218,9 +214,9 @@ virtual int FileSetBandwidthTotals(float outkB, float inkB) = 0; /****************************************/ /* Message Items */ -virtual int MessageSend(MessageInfo &info) = 0; -virtual int MessageDelete(std::string mid) = 0; -virtual int MessageRead(std::string mid) = 0; +//virtual int MessageSend(MessageInfo &info) = 0; +//virtual int MessageDelete(std::string mid) = 0; +//virtual int MessageRead(std::string mid) = 0; /* Channel Items */ virtual int ChannelCreateNew(ChannelInfo &info) = 0; @@ -228,7 +224,7 @@ virtual int ChannelSendMsg(ChannelInfo &info) = 0; /****************************************/ /* Chat */ -virtual int ChatSend(ChatInfo &ci) = 0; +//virtual int ChatSend(ChatInfo &ci) = 0; /****************************************/ @@ -244,6 +240,9 @@ virtual int ClearInBroadcast() = 0; virtual int ClearInSubscribe() = 0; virtual int ClearInRecommend() = 0; +virtual bool IsInChat(std::string id) = 0; /* friend : chat msgs */ +virtual bool IsInMsg(std::string id) = 0; /* friend : msg recpts*/ + /****************************************/ /* RsIface Networking */ //virtual int NetworkDHTActive(bool active) = 0; diff --git a/libretroshare/src/rsiface/rsmsgs.h b/libretroshare/src/rsiface/rsmsgs.h new file mode 100644 index 000000000..79af976fd --- /dev/null +++ b/libretroshare/src/rsiface/rsmsgs.h @@ -0,0 +1,138 @@ +#ifndef RS_MSG_GUI_INTERFACE_H +#define RS_MSG_GUI_INTERFACE_H + +/* + * libretroshare/src/rsiface: rsmsgs.h + * + * RetroShare C++ Interface. + * + * Copyright 2007-2008 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 +#include +#include + +#include "rsiface/rstypes.h" + +/********************** For Messages and Channels *****************/ + +#define RS_MSG_BOXMASK 0x000f /* Mask for determining Box */ + +#define RS_MSG_OUTGOING 0x0001 /* !Inbox */ +#define RS_MSG_PENDING 0x0002 /* OutBox */ +#define RS_MSG_DRAFT 0x0004 /* Draft */ + +/* ORs of above */ +#define RS_MSG_INBOX 0x00 /* Inbox */ +#define RS_MSG_SENTBOX 0x01 /* Sentbox */ +#define RS_MSG_OUTBOX 0x03 /* Outbox */ +#define RS_MSG_DRAFTBOX 0x05 /* Draftbox */ + +#define RS_MSG_NEW 0x0010 + +class MessageInfo +{ + public: + MessageInfo() {} + std::string msgId; + std::string srcId; + + unsigned int msgflags; + + std::list msgto; + std::list msgcc; + std::list msgbcc; + + std::wstring title; + std::wstring msg; + + std::wstring attach_title; + std::wstring attach_comment; + std::list files; + int size; /* total of files */ + int count; /* file count */ + + int ts; +}; + +class MsgInfoSummary +{ + public: + MsgInfoSummary() {} + + std::string msgId; + std::string srcId; + + uint32_t msgflags; + + std::wstring title; + int count; /* file count */ + time_t ts; + +}; + +#define RS_CHAT_PUBLIC 0x0001 +#define RS_CHAT_PRIVATE 0x0002 + +class ChatInfo +{ + public: + std::string rsid; + unsigned int chatflags; + std::string name; + std::wstring msg; +}; + +std::ostream &operator<<(std::ostream &out, const MessageInfo &info); +std::ostream &operator<<(std::ostream &out, const ChatInfo &info); + +class RsMsgs; +extern RsMsgs *rsMsgs; + +class RsMsgs +{ + public: + + RsMsgs() { return; } +virtual ~RsMsgs() { return; } + +/****************************************/ + /* Message Items */ + +virtual bool getMessageSummaries(std::list &msgList) = 0; +virtual bool getMessage(std::string mId, MessageInfo &msg) = 0; + +virtual bool MessageSend(MessageInfo &info) = 0; +virtual bool MessageDelete(std::string mid) = 0; +virtual bool MessageRead(std::string mid) = 0; + +/****************************************/ + /* Chat */ +virtual bool ChatSend(ChatInfo &ci) = 0; +virtual bool getNewChat(std::list &chats) = 0; + +/****************************************/ + +}; + + +#endif diff --git a/libretroshare/src/rsiface/rstypes.h b/libretroshare/src/rsiface/rstypes.h index ac7fdbcde..48fc670c7 100644 --- a/libretroshare/src/rsiface/rstypes.h +++ b/libretroshare/src/rsiface/rstypes.h @@ -32,25 +32,6 @@ #include -#if 0 -#define RSCERTIDLEN 16 - -class RsCertId -{ - public: - RsCertId(); - RsCertId(std::string idstr); - - bool operator<(const RsCertId &ref) const; - bool operator==(const RsCertId &ref) const; - bool operator!=(const RsCertId &ref) const; - char data[RSCERTIDLEN]; -}; - -std::ostream &operator<<(std::ostream &out, const RsCertId &id); - -#endif - typedef std::string RsCertId; typedef std::string RsChanId; typedef std::string RsMsgId; @@ -157,48 +138,6 @@ class FileTransferInfo: public FileInfo int downloadStatus; /* 0 = Err, 1 = Ok, 2 = Done */ }; - -/********************** For Messages and Channels *****************/ - -#define RS_MSG_BOXMASK 0x000f /* Mask for determining Box */ - -#define RS_MSG_OUTGOING 0x0001 /* !Inbox */ -#define RS_MSG_PENDING 0x0002 /* OutBox */ -#define RS_MSG_DRAFT 0x0004 /* Draft */ - -/* ORs of above */ -#define RS_MSG_INBOX 0x00 /* Inbox */ -#define RS_MSG_SENTBOX 0x01 /* Sentbox */ -#define RS_MSG_OUTBOX 0x03 /* Outbox */ -#define RS_MSG_DRAFTBOX 0x05 /* Draftbox */ - -#define RS_MSG_NEW 0x0010 - -class MessageInfo: public BaseInfo -{ - public: - MessageInfo() {} - RsMsgId msgId; - - unsigned int msgflags; - std::string srcname; - - std::list msgto; - std::list msgcc; - std::list msgbcc; - - std::wstring title; - std::wstring msg; - - std::wstring attach_title; - std::wstring attach_comment; - std::list files; - int size; /* total of files */ - int count; /* file count */ - - int ts; -}; - class ChannelInfo: public BaseInfo { public: @@ -206,7 +145,7 @@ class ChannelInfo: public BaseInfo RsChanId chanId; bool publisher; std::string chanName; - std::list msglist; + //std::list msglist; /* details */ int mode; @@ -219,18 +158,6 @@ class ChannelInfo: public BaseInfo int count; /* msg count */ }; -#define RS_CHAT_PUBLIC 0x0001 -#define RS_CHAT_PRIVATE 0x0002 - -class ChatInfo: public BaseInfo -{ - public: - std::string rsid; - unsigned int chatflags; - std::string name; - std::wstring msg; -}; - /* matched to the uPnP states */ #define UPNP_STATE_UNINITIALISED 0 #define UPNP_STATE_UNAVAILABILE 1 @@ -303,9 +230,7 @@ class SearchRequest }; -std::ostream &operator<<(std::ostream &out, const MessageInfo &info); std::ostream &operator<<(std::ostream &out, const ChannelInfo &info); -std::ostream &operator<<(std::ostream &out, const ChatInfo &info); std::ostream &operator<<(std::ostream &out, const PersonInfo &info); std::ostream &print(std::ostream &out, const DirInfo &info, int indentLvl); diff --git a/libretroshare/src/rsserver/Makefile b/libretroshare/src/rsserver/Makefile index 0e23b8ba5..780105e3b 100644 --- a/libretroshare/src/rsserver/Makefile +++ b/libretroshare/src/rsserver/Makefile @@ -7,15 +7,16 @@ RS_TOP_DIR = .. include $(RS_TOP_DIR)/scripts/config.mk ############################################################### -RSOBJ = p3face-file.o \ - p3face-msgs.o \ +RSOBJ = p3peers.o \ + p3rank.o \ + p3msgs.o \ + p3face-file.o \ p3face-server.o \ p3face-config.o \ p3face-startup.o \ rstypes.o \ rsiface.o \ - p3peers.o \ - p3rank.o + p3face-msgs.o # pqistrings.o \ # p3face-people.o diff --git a/libretroshare/src/rsserver/p3face-msgs.cc b/libretroshare/src/rsserver/p3face-msgs.cc index e6e78f0d6..ce9aafc03 100644 --- a/libretroshare/src/rsserver/p3face-msgs.cc +++ b/libretroshare/src/rsserver/p3face-msgs.cc @@ -37,130 +37,6 @@ const int p3facemsgzone = 11453; #include #include -#if 0 - -unsigned long getMsgId(RsMsgId &id) -{ - unsigned long mid = 0; - mid = (id.data[0] << 24); - mid |= (id.data[1] << 16); - mid |= (id.data[2] << 8); - mid |= id.data[3]; - - return mid; -} - -void setMsgId(RsMsgId &rsmid, unsigned int mid) -{ - /* version that uses the uniqueMsgId stored in sid */ - /* 16 Bytes XXX Must be equal! */ - for(int i = 0; i < CHAN_SIGN_SIZE; i++) - rsmid.data[i] = 0; - - rsmid.data[0] = (0xff & (mid >> 24)); - rsmid.data[1] = (0xff & (mid >> 16)); - rsmid.data[2] = (0xff & (mid >> 8)); - rsmid.data[3] = (0xff & (mid >> 0)); - - return; -} - -#endif - - -/****************************************/ -/****************************************/ - /* Message Items */ -int RsServer::MessageSend(MessageInfo &info) -{ - /* so we send this.... */ - lockRsCore(); /* LOCK */ - - RsMsgItem *msg = new RsMsgItem(); - - /* id who it is to ???? handled lower */ - msg -> PeerId(""); - - msg -> msgFlags = 0; - msg -> msgId = 0; - msg -> sendTime = time(NULL); - msg -> recvTime = 0; - - msg -> subject = info.title; - msg -> message = info.msg; - - std::list::iterator pit; - for(pit = info.msgto.begin(); pit != info.msgto.end(); pit++) - { - msg -> msgto.ids.push_back(pit->id); - } - - for(pit = info.msgcc.begin(); pit != info.msgcc.end(); pit++) - { - msg -> msgcc.ids.push_back(pit->id); - } - - for(pit = info.msgbcc.begin(); pit != info.msgbcc.end(); pit++) - { - msg -> msgbcc.ids.push_back(pit->id); - } - - msg -> attachment.title = info.attach_title; - msg -> attachment.comment = info.attach_comment; - - std::list::iterator it; - for(it = info.files.begin(); it != info.files.end(); it++) - { - RsTlvFileItem mfi; - mfi.hash = it -> hash; - mfi.name = it -> fname; - mfi.filesize = it -> size; - msg -> attachment.items.push_back(mfi); - } - - std::cerr << "RsServer::MessageSend()" << std::endl; - msg->print(std::cerr); - - msgSrv -> sendMessage(msg); - - unlockRsCore(); /* UNLOCK */ - - UpdateAllMsgs(); - return 1; -} - -/****************************************/ -/****************************************/ -int RsServer::MessageDelete(std::string mid) -{ - lockRsCore(); /* LOCK */ - - std::cerr << "RsServer::MessageDelete() "; - std::cerr << "mid: " << mid << std::endl; - - msgSrv -> removeMsgId(atoi(mid.c_str())); - - unlockRsCore(); /* UNLOCK */ - - UpdateAllMsgs(); - return 1; -} - -int RsServer::MessageRead(std::string mid) -{ - lockRsCore(); /* LOCK */ - - std::cerr << "RsServer::MessageRead() "; - std::cerr << "mid: " << mid << std::endl; - - msgSrv -> markMsgIdRead(atoi(mid.c_str())); - - unlockRsCore(); /* UNLOCK */ - - // (needed?) UpdateAllMsgs(); - return 1; -} - /****************************************/ /****************************************/ int RsServer::ChannelCreateNew(ChannelInfo &info) @@ -177,105 +53,6 @@ int RsServer::ChannelSendMsg(ChannelInfo &info) return 1; } -/****************************************/ -/****************************************/ -int RsServer::ChatSend(ChatInfo &ci) -{ - lockRsCore(); /* LOCK */ - /* send a message to all for now */ - if (ci.chatflags & RS_CHAT_PRIVATE) - { - chatSrv -> sendPrivateChat(ci.msg, ci.rsid); - } - else - { - /* global */ - chatSrv -> sendChat(ci.msg); - } - unlockRsCore(); /* UNLOCK */ - - UpdateAllChat(); - return 1; -} - -int RsServer::UpdateAllChat() -{ - RsIface &iface = getIface(); - - /* lock Mutexes */ - lockRsCore(); /* LOCK */ - iface.lockData(); /* LOCK */ - - /* get any messages and push them to iface */ - - // get the items from the list. - std::list clist = chatSrv -> getChatQueue(); - std::list::iterator it; - for(it = clist.begin(); it != clist.end(); it++) - { - ChatInfo ci; - initRsChatInfo((*it), ci); - iface.mChatList.push_back(ci); - delete (*it); - } - - iface.setChanged(RsIface::Chat); - - /* unlock Mutexes */ - iface.unlockData(); /* UNLOCK */ - unlockRsCore(); /* UNLOCK */ - - return 1; -} - -/****************************************/ -/****************************************/ -int RsServer::UpdateAllMsgs() -{ - NotifyBase &cb = getNotify(); - cb.notifyListPreChange(NOTIFY_LIST_MESSAGELIST, NOTIFY_TYPE_MOD); - - RsIface &iface = getIface(); - - /* lock Mutexes */ - lockRsCore(); /* LOCK */ - iface.lockData(); /* LOCK */ - - /* do stuff */ - std::list &msglist = msgSrv -> getMsgList(); - std::list &msgOutlist = msgSrv -> getMsgOutList(); - std::list::iterator mit; - - std::list &msgs = iface.mMessageList; - - msgs.clear(); - - for(mit = msglist.begin(); mit != msglist.end(); mit++) - { - MessageInfo mi; - initRsMI(*mit, mi); - msgs.push_back(mi); - } - - for(mit = msgOutlist.begin(); mit != msgOutlist.end(); mit++) - { - MessageInfo mi; - initRsMI(*mit, mi); - msgs.push_back(mi); - } - - iface.setChanged(RsIface::Message); - - /* unlock Mutexes */ - iface.unlockData(); /* UNLOCK */ - unlockRsCore(); /* UNLOCK */ - - cb.notifyListChange(NOTIFY_LIST_MESSAGELIST, NOTIFY_TYPE_MOD); - - return 1; -} - - /****************************************/ /****************************************/ int RsServer::UpdateAllChannels() @@ -374,25 +151,6 @@ int RsServer::UpdateAllChannels() * for intAddChannel / intAddChannelMsg. */ -void RsServer::initRsChatInfo(RsChatItem *c, ChatInfo &i) -{ - i.rsid = c -> PeerId(); - i.name = mAuthMgr->getName(i.rsid); - - i.msg = c -> message; - if (c -> chatFlags & RS_CHAT_FLAG_PRIVATE) - { - std::cerr << "RsServer::initRsChatInfo() Chat Private!!!"; - i.chatflags = RS_CHAT_PRIVATE; - } - else - { - i.chatflags = RS_CHAT_PUBLIC; - std::cerr << "RsServer::initRsChatInfo() Chat Public!!!"; - } - std::cerr << std::endl; -} - #ifdef PQI_USE_CHANNELS int RsServer::intAddChannel(ChannelInfo &info) @@ -503,101 +261,13 @@ void RsServer::intCheckFileStatus(FileInfo &file) } -void RsServer::initRsMI(RsMsgItem *msg, MessageInfo &mi) -{ - - mi.msgflags = 0; - - /* translate flags, if we sent it... outgoing */ - if ((msg->msgFlags & RS_MSG_FLAGS_OUTGOING) - || (msg->PeerId() == mAuthMgr->OwnId())) - { - mi.msgflags |= RS_MSG_OUTGOING; - } - /* if it has a pending flag, then its in the outbox */ - if (msg->msgFlags & RS_MSG_FLAGS_PENDING) - { - mi.msgflags |= RS_MSG_PENDING; - } - if (msg->msgFlags & RS_MSG_FLAGS_NEW) - { - mi.msgflags |= RS_MSG_NEW; - } - - mi.id = msg->PeerId(); - mi.srcname = mAuthMgr->getName(mi.id); - - std::list::iterator pit; - - for(pit = msg->msgto.ids.begin(); - pit != msg->msgto.ids.end(); pit++) - { - PersonInfo pi; - pi.id = (*pit); - pi.name = mAuthMgr->getName(pi.id); - mi.msgto.push_back(pi); - } - - for(pit = msg->msgcc.ids.begin(); - pit != msg->msgcc.ids.end(); pit++) - { - PersonInfo pi; - pi.id = (*pit); - pi.name = mAuthMgr->getName(pi.id); - mi.msgcc.push_back(pi); - } - - for(pit = msg->msgbcc.ids.begin(); - pit != msg->msgbcc.ids.end(); pit++) - { - PersonInfo pi; - pi.id = (*pit); - pi.name = mAuthMgr->getName(pi.id); - mi.msgbcc.push_back(pi); - } - - mi.title = msg->subject; - mi.msg = msg->message; - - mi.attach_title = msg->attachment.title; - mi.attach_comment = msg->attachment.comment; - - mi.count = 0; - mi.size = 0; - - std::list::iterator it; - for(it = msg->attachment.items.begin(); - it != msg->attachment.items.end(); it++) - { - FileInfo fi; - fi.fname = RsDirUtil::getTopDir(it->name); - fi.size = it->filesize; - fi.hash = it->hash; - fi.path = it->path; - mi.files.push_back(fi); - mi.count++; - mi.size += fi.size; - } - - mi.ts = msg->sendTime; - mi.msgId = msg->msgId; -} - /* Flagging Persons / Channels / Files in or out of a set (CheckLists) */ int RsServer::ClearInChat() { lockRsCore(); /* LOCK */ -#if 0 - std::list::iterator it; - std::list &certs = sslr -> getCertList(); - - for(it = certs.begin(); it != certs.end(); it++) - { - (*it)->InChat(false); - } - sslr->IndicateCertsChanged(); -#endif + mInChatList.clear(); + unlockRsCore(); /* UNLOCK */ return 1; @@ -610,25 +280,26 @@ int RsServer::SetInChat(std::string id, bool in) /* friend : cha /* so we send this.... */ lockRsCore(); /* LOCK */ -#if 0 - RsCertId rsid(id); - cert *c = intFindCert(rsid); - - if (c) + std::cerr << "Set InChat(" << id << ") to " << (in ? "True" : "False") << std::endl; + std::list::iterator it; + it = std::find(mInChatList.begin(), mInChatList.end(), id); + if (it == mInChatList.end()) { - c->InChat(in); - sslr->IndicateCertsChanged(); - std::cerr << "Set InChat(" << id << ") to " << (in ? "True" : "False") << std::endl; + if (in) + { + mInChatList.push_back(id); + } } else { - std::cerr << "FAILED TO Set InChat(" << id << ") to " << (in ? "True" : "False") << std::endl; + if (!in) + { + mInChatList.erase(it); + } } -#endif unlockRsCore(); /* UNLOCK */ - //UpdateAllCerts(); return 1; } @@ -637,16 +308,7 @@ int RsServer::ClearInMsg() { lockRsCore(); /* LOCK */ -#if 0 - std::list::iterator it; - std::list &certs = sslr -> getCertList(); - - for(it = certs.begin(); it != certs.end(); it++) - { - (*it)->InMessage(false); - } - sslr->IndicateCertsChanged(); -#endif + mInMsgList.clear(); unlockRsCore(); /* UNLOCK */ @@ -659,30 +321,59 @@ int RsServer::SetInMsg(std::string id, bool in) /* friend : msgs /* so we send this.... */ lockRsCore(); /* LOCK */ -#if 0 - RsCertId rsid(id); - cert *c = intFindCert(rsid); - - if (c) + std::cerr << "Set InMsg(" << id << ") to " << (in ? "True" : "False") << std::endl; + std::list::iterator it; + it = std::find(mInMsgList.begin(), mInMsgList.end(), id); + if (it == mInMsgList.end()) { - c->InMessage(in); - sslr->IndicateCertsChanged(); - std::cerr << "Set InMsg(" << id << ") to " << (in ? "True" : "False") << std::endl; + if (in) + { + mInMsgList.push_back(id); + } } else { - std::cerr << "FAILED to Set InMsg(" << id << ") to " << (in ? "True" : "False") << std::endl; - + if (!in) + { + mInMsgList.erase(it); + } } -#endif + unlockRsCore(); /* UNLOCK */ + return 1; +} + +bool RsServer::IsInChat(std::string id) /* friend : chat msgs */ +{ + /* so we send this.... */ + lockRsCore(); /* LOCK */ + + std::list::iterator it; + it = std::find(mInChatList.begin(), mInChatList.end(), id); + bool inChat = (it != mInChatList.end()); unlockRsCore(); /* UNLOCK */ - //UpdateAllCerts(); - return 1; + return inChat; } + +bool RsServer::IsInMsg(std::string id) /* friend : msg recpts*/ +{ + /* so we send this.... */ + lockRsCore(); /* LOCK */ + + std::list::iterator it; + it = std::find(mInMsgList.begin(), mInMsgList.end(), id); + bool inMsg = (it != mInMsgList.end()); + + unlockRsCore(); /* UNLOCK */ + + return inMsg; +} + + + int RsServer::ClearInBroadcast() { diff --git a/libretroshare/src/rsserver/p3face-server.cc b/libretroshare/src/rsserver/p3face-server.cc index 390b95d3a..68c87b7d7 100644 --- a/libretroshare/src/rsserver/p3face-server.cc +++ b/libretroshare/src/rsserver/p3face-server.cc @@ -175,11 +175,6 @@ void RsServer::run() { lastSec = (int) ts; - /* Chat needs to be quick too! */ - //std::cerr << "RsServer::run() UpdateAllChat()" << std::endl; - UpdateAllChat(); - - // every five loops (> 5 secs) if (loop % 5 == 0) { @@ -199,8 +194,6 @@ void RsServer::run() //std::cerr << "RsServer::run() UpdateAllTransfers()" << std::endl; UpdateAllTransfers(); - //std::cerr << "RsServer::run() UpdateAllMsgs()" << std::endl; - UpdateAllMsgs(); //std::cerr << "RsServer::run() UpdateAllChannels()" << std::endl; UpdateAllChannels(); diff --git a/libretroshare/src/rsserver/p3face-startup.cc b/libretroshare/src/rsserver/p3face-startup.cc index e92311749..1b4910fc8 100644 --- a/libretroshare/src/rsserver/p3face-startup.cc +++ b/libretroshare/src/rsserver/p3face-startup.cc @@ -38,7 +38,6 @@ #include "upnp/upnphandler.h" #include "dht/opendhtmgr.h" -// Removed temporarily... #include "services/p3disc.h" #include "services/p3msgservice.h" #include "services/p3chatservice.h" @@ -56,6 +55,7 @@ #include "rsserver/p3face.h" #include "rsserver/p3peers.h" #include "rsserver/p3rank.h" +#include "rsserver/p3msgs.h" #include "rsiface/rsgame.h" /**************** PQI_USE_XPGP ******************/ @@ -621,6 +621,7 @@ int RsServer::StartupRetroShare(RsInit *config) /* setup the gui */ rsGameLauncher = gameLauncher; rsRanks = new p3Rank(ranking); + rsMsgs = new p3Msgs(mAuthMgr, msgSrv, chatSrv); /* put a welcome message in! */ if (config->firsttime_run) diff --git a/libretroshare/src/rsserver/p3face.h b/libretroshare/src/rsserver/p3face.h index f95dab249..e816cc76d 100644 --- a/libretroshare/src/rsserver/p3face.h +++ b/libretroshare/src/rsserver/p3face.h @@ -36,12 +36,9 @@ #include "rsiface/rstypes.h" #include "util/rsthreads.h" -class p3disc; -class p3MsgService; -class p3ChatService; - -#include "services/p3chatservice.h" +#include "services/p3disc.h" #include "services/p3msgservice.h" +#include "services/p3chatservice.h" /* The Main Interface Class - for controlling the server */ @@ -159,18 +156,10 @@ int UpdateRemotePeople(); /* p3face-msg Operations */ public: - /* Message Items */ -virtual int MessageSend(MessageInfo &info); -virtual int MessageDelete(std::string mid); -virtual int MessageRead(std::string mid); - /* Channel Items */ virtual int ChannelCreateNew(ChannelInfo &info); virtual int ChannelSendMsg(ChannelInfo &info); - /* Chat */ -virtual int ChatSend(ChatInfo &ci); - /* Flagging Persons / Channels / Files in or out of a set (CheckLists) */ virtual int SetInChat(std::string id, bool in); /* friend : chat msgs */ virtual int SetInMsg(std::string id, bool in); /* friend : msg receipients */ @@ -183,16 +172,17 @@ virtual int ClearInBroadcast(); virtual int ClearInSubscribe(); virtual int ClearInRecommend(); +virtual bool IsInChat(std::string id); /* friend : chat msgs */ +virtual bool IsInMsg(std::string id); /* friend : msg recpts*/ + private: + std::list mInChatList, mInMsgList; + /* Internal Update Iface Fns */ -int UpdateAllChat(); -int UpdateAllMsgs(); int UpdateAllChannels(); -void initRsChatInfo(RsChatItem *c, ChatInfo &i); - #ifdef PQI_USE_CHANNELS /* Internal Helper Fns */ diff --git a/libretroshare/src/rsserver/p3msgs.cc b/libretroshare/src/rsserver/p3msgs.cc new file mode 100644 index 000000000..0ad751def --- /dev/null +++ b/libretroshare/src/rsserver/p3msgs.cc @@ -0,0 +1,155 @@ + +/* + * "$Id: p3face-msgs.cc,v 1.7 2007-05-05 16:10:06 rmf24 Exp $" + * + * RetroShare C++ Interface. + * + * Copyright 2004-2006 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 "rsserver/p3face.h" +#include "util/rsdir.h" + +#include +#include + +#include "pqi/pqidebug.h" +const int p3facemsgzone = 11453; + +#include +#include + +#include "rsserver/p3msgs.h" + +/* external reference point */ +RsMsgs *rsMsgs = NULL; + +/****************************************/ +/****************************************/ + +bool p3Msgs::getMessageSummaries(std::list &msgList) +{ + return mMsgSrv->getMessageSummaries(msgList); +} + + + +bool p3Msgs::getMessage(std::string mid, MessageInfo &msg) +{ + return mMsgSrv->getMessage(mid, msg); +} + + + +/****************************************/ +/****************************************/ + /* Message Items */ +bool p3Msgs::MessageSend(MessageInfo &info) +{ + return mMsgSrv->MessageSend(info); +} + +/****************************************/ +/****************************************/ +bool p3Msgs::MessageDelete(std::string mid) +{ + std::cerr << "p3Msgs::MessageDelete() "; + std::cerr << "mid: " << mid << std::endl; + + mMsgSrv -> removeMsgId(mid); + + return 1; +} + +bool p3Msgs::MessageRead(std::string mid) +{ + std::cerr << "p3Msgs::MessageRead() "; + std::cerr << "mid: " << mid << std::endl; + + mMsgSrv -> markMsgIdRead(mid); + + return 1; +} + +/****************************************/ +/****************************************/ +bool p3Msgs::ChatSend(ChatInfo &ci) +{ + /* send a message to all for now */ + if (ci.chatflags & RS_CHAT_PRIVATE) + { + mChatSrv -> sendPrivateChat(ci.msg, ci.rsid); + } + else + { + /* global */ + mChatSrv -> sendChat(ci.msg); + } + return true; +} + +bool p3Msgs::getNewChat(std::list &chats) +{ + /* get any messages and push them to iface */ + + // get the items from the list. + std::list clist = mChatSrv -> getChatQueue(); + if (clist.size() < 1) + { + return false; + } + + std::list::iterator it; + for(it = clist.begin(); it != clist.end(); it++) + { + ChatInfo ci; + initRsChatInfo((*it), ci); + chats.push_back(ci); + delete (*it); + } + return true; +} + +/**** HELPER FNS For Chat/Msg/Channel Lists ************ + * + * The iface->Mutex is required to be locked + * for intAddChannel / intAddChannelMsg. + */ + +void p3Msgs::initRsChatInfo(RsChatItem *c, ChatInfo &i) +{ + i.rsid = c -> PeerId(); + i.name = mAuthMgr->getName(i.rsid); + + i.msg = c -> message; + if (c -> chatFlags & RS_CHAT_FLAG_PRIVATE) + { + std::cerr << "RsServer::initRsChatInfo() Chat Private!!!"; + i.chatflags = RS_CHAT_PRIVATE; + } + else + { + i.chatflags = RS_CHAT_PUBLIC; + std::cerr << "RsServer::initRsChatInfo() Chat Public!!!"; + } + std::cerr << std::endl; +} + diff --git a/libretroshare/src/rsserver/p3msgs.h b/libretroshare/src/rsserver/p3msgs.h new file mode 100644 index 000000000..a4c833890 --- /dev/null +++ b/libretroshare/src/rsserver/p3msgs.h @@ -0,0 +1,71 @@ +#ifndef RS_P3MSG_INTERFACE_H +#define RS_P3MSG_INTERFACE_H + +/* + * libretroshare/src/rsserver: p3msgs.h + * + * RetroShare C++ Interface. + * + * Copyright 2007-2008 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 "rsiface/rsmsgs.h" + +class p3AuthMgr; +class p3MsgService; +class p3ChatService; + +class p3Msgs: public RsMsgs +{ + public: + + p3Msgs(p3AuthMgr *p3a, p3MsgService *p3m, p3ChatService *p3c) + :mAuthMgr(p3a), mMsgSrv(p3m), mChatSrv(p3c) { return; } +virtual ~p3Msgs() { return; } + +/****************************************/ + /* Message Items */ + +virtual bool getMessageSummaries(std::list &msgList); +virtual bool getMessage(std::string mId, MessageInfo &msg); + +virtual bool MessageSend(MessageInfo &info); +virtual bool MessageDelete(std::string mid); +virtual bool MessageRead(std::string mid); + +/****************************************/ + /* Chat */ +virtual bool ChatSend(ChatInfo &ci); +virtual bool getNewChat(std::list &chats); + +/****************************************/ + + + private: + +void initRsChatInfo(RsChatItem *c, ChatInfo &i); + + p3AuthMgr *mAuthMgr; + p3MsgService *mMsgSrv; + p3ChatService *mChatSrv; +}; + + +#endif diff --git a/libretroshare/src/rsserver/rsiface.cc b/libretroshare/src/rsserver/rsiface.cc index b7960bbdb..fd0fe1d86 100644 --- a/libretroshare/src/rsserver/rsiface.cc +++ b/libretroshare/src/rsserver/rsiface.cc @@ -111,6 +111,7 @@ DirInfo *RsIface::getDirectoryMod(std::string uid, std::string path) +#if 0 const MessageInfo *RsIface::getMessage(std::string cid_in, std::string mid_in) { @@ -172,6 +173,8 @@ const MessageInfo *RsIface::getChannelMsg(std::string chid_in, std::string mid_i return NULL; } +#endif + /* set to true */ bool RsIface::setChanged(DataFlags set) { diff --git a/libretroshare/src/rsserver/rstypes.cc b/libretroshare/src/rsserver/rstypes.cc index c1a651d92..1df616fdf 100644 --- a/libretroshare/src/rsserver/rstypes.cc +++ b/libretroshare/src/rsserver/rstypes.cc @@ -89,6 +89,8 @@ std::ostream &print(std::ostream &out, const DirInfo &info, int indentLvl) } +#if 0 + std::ostream &operator<<(std::ostream &out, const MessageInfo &info) { out << "MessageInfo(TODO)"; @@ -96,16 +98,18 @@ std::ostream &operator<<(std::ostream &out, const MessageInfo &info) return out; } -std::ostream &operator<<(std::ostream &out, const ChannelInfo &info) +std::ostream &operator<<(std::ostream &out, const ChatInfo &info) { - out << "ChannelInfo(TODO)"; + out << "ChatInfo(TODO)"; out << std::endl; return out; } -std::ostream &operator<<(std::ostream &out, const ChatInfo &info) +#endif + +std::ostream &operator<<(std::ostream &out, const ChannelInfo &info) { - out << "ChatInfo(TODO)"; + out << "ChannelInfo(TODO)"; out << std::endl; return out; } diff --git a/libretroshare/src/serialiser/rsserial.cc b/libretroshare/src/serialiser/rsserial.cc index 3691fb810..ab9c2d86c 100644 --- a/libretroshare/src/serialiser/rsserial.cc +++ b/libretroshare/src/serialiser/rsserial.cc @@ -212,6 +212,12 @@ uint32_t RsSerialiser::size(RsItem *item) #ifdef RSSERIAL_DEBUG std::cerr << "RsSerialiser::size() serialiser missing!"; + + std::ostringstream out; + out << std::hex << item->PacketId(); + + std::cerr << "RsSerialiser::size() PacketId: "; + std::cerr << out.str(); std::cerr << std::endl; #endif return 0; @@ -248,6 +254,11 @@ bool RsSerialiser::serialise (RsItem *item, void *data, uint32_t *size) #ifdef RSSERIAL_DEBUG std::cerr << "RsSerialiser::serialise() serialiser missing!"; + std::ostringstream out; + out << std::hex << item->PacketId(); + + std::cerr << "RsSerialiser::serialise() PacketId: "; + std::cerr << out.str(); std::cerr << std::endl; #endif return false; @@ -308,6 +319,11 @@ RsItem * RsSerialiser::deserialise(void *data, uint32_t *size) #ifdef RSSERIAL_DEBUG std::cerr << "RsSerialiser::deserialise() deserialiser missing!"; + std::ostringstream out; + out << std::hex << getRsItemId(data); + + std::cerr << "RsSerialiser::deserialise() PacketId: "; + std::cerr << out.str(); std::cerr << std::endl; #endif return NULL; diff --git a/libretroshare/src/services/p3msgservice.cc b/libretroshare/src/services/p3msgservice.cc index 7cc188dae..78941456d 100644 --- a/libretroshare/src/services/p3msgservice.cc +++ b/libretroshare/src/services/p3msgservice.cc @@ -31,6 +31,8 @@ #include "services/p3msgservice.h" +#include "util/rsdir.h" + #include #include @@ -57,8 +59,16 @@ p3MsgService::p3MsgService(p3ConnectMgr *cm) :p3Service(RS_SERVICE_TYPE_MSG), mConnMgr(cm), msgChanged(1), msgMajorChanged(1) { + addSerialType(new RsMsgSerialiser()); } +bool p3MsgService::ModifiedMsgs() +{ + bool m1 = msgChanged.Changed(); + bool m2 = msgMajorChanged.Changed(); + + return (m1 || m2); +} int p3MsgService::tick() { @@ -111,91 +121,12 @@ int p3MsgService::incomingMsgs() /* STORE MsgID */ mi -> msgId = getNewUniqueMsgId(); - imsg.push_back(mi); + imsg[mi->msgId] = mi; msgChanged.IndicateChanged(); } return 1; } - -std::list &p3MsgService::getMsgList() -{ - return imsg; -} - -std::list &p3MsgService::getMsgOutList() -{ - return msgOutgoing; -} - -/* remove based on the unique mid (stored in sid) */ -int p3MsgService::removeMsgId(uint32_t mid) -{ - std::list::iterator it; - - for(it = imsg.begin(); it != imsg.end(); it++) - { - if ((*it)->msgId == mid) - { - RsMsgItem *mi = (*it); - imsg.erase(it); - delete mi; - msgChanged.IndicateChanged(); - msgMajorChanged.IndicateChanged(); - return 1; - } - } - - /* try with outgoing messages otherwise */ - for(it = msgOutgoing.begin(); it != msgOutgoing.end(); it++) - { - if ((*it)->msgId == mid) - { - RsMsgItem *mi = (*it); - msgOutgoing.erase(it); - delete mi; - msgChanged.IndicateChanged(); - msgMajorChanged.IndicateChanged(); - return 1; - } - } - - return 0; -} - -int p3MsgService::markMsgIdRead(uint32_t mid) -{ - std::list::iterator it; - - for(it = imsg.begin(); it != imsg.end(); it++) - { - if ((*it)->msgId == mid) - { - RsMsgItem *mi = (*it); - mi -> msgFlags &= ~(RS_MSG_FLAGS_NEW); - msgChanged.IndicateChanged(); - return 1; - } - } - return 0; -} - -int p3MsgService::sendMessage(RsMsgItem *item) -{ - pqioutput(PQL_DEBUG_BASIC, msgservicezone, - "p3MsgService::sendMessage()"); - - /* add pending flag */ - item->msgFlags |= - (RS_MSG_FLAGS_OUTGOING | - RS_MSG_FLAGS_PENDING); - /* STORE MsgID */ - item -> msgId = getNewUniqueMsgId(); - msgOutgoing.push_back(item); - - return 1; -} - int p3MsgService::checkOutgoingMessages() { /* iterate through the outgoing queue @@ -205,38 +136,59 @@ int p3MsgService::checkOutgoingMessages() const std::string ownId = mConnMgr->getOwnId(); - std::list::iterator it; - for(it = msgOutgoing.begin(); it != msgOutgoing.end();) + std::list::iterator it; + std::list toErase; + + std::map::iterator mit; + + for(mit = msgOutgoing.begin(); mit != msgOutgoing.end(); mit++) { /* find the certificate */ - std::string pid = (*it)->PeerId(); + std::string pid = mit->second->PeerId(); peerConnectState pstate; - if (!mConnMgr->getFriendNetStatus(pid, pstate)) + bool toSend = false; + + if (mConnMgr->getFriendNetStatus(pid, pstate)) { - delete(*it); - it = msgOutgoing.erase(it); + if (pstate.state & RS_PEER_S_ONLINE) + { + toSend = true; + } } - /* if online, send it */ - else if ((pstate.state & RS_PEER_S_ONLINE) - || (pid == ownId)) + else if (pid == ownId) /* FEEDBACK Msg to Ourselves */ + { + toSend = true; + } + + if (toSend) { /* send msg */ pqioutput(PQL_ALERT, msgservicezone, "p3MsgService::checkOutGoingMessages() Sending out message"); /* remove the pending flag */ - (*it)->msgFlags &= ~RS_MSG_FLAGS_PENDING; + (mit->second)->msgFlags &= ~RS_MSG_FLAGS_PENDING; - sendItem(*it); - it = msgOutgoing.erase(it); + sendItem(mit->second); + toErase.push_back(mit->first); } else { pqioutput(PQL_ALERT, msgservicezone, "p3MsgService::checkOutGoingMessages() Delaying until available..."); - it++; } } + + /* clean up */ + for(it = toErase.begin(); it != toErase.end(); it++) + { + mit = msgOutgoing.find(*it); + if (mit != msgOutgoing.end()) + { + msgOutgoing.erase(mit); + } + } + return 0; } @@ -260,11 +212,10 @@ int p3MsgService::save_config() pqiarchive *pa_out = new pqiarchive(rss, out, BIN_FLAGS_WRITEABLE | BIN_FLAGS_NO_DELETE); bool written = false; - std::list::iterator mit; + std::map::iterator mit; for(mit = imsg.begin(); mit != imsg.end(); mit++) { - //RsMsgItem *mi = (*mit)->clone(); - if (pa_out -> SendItem(*mit)) + if (pa_out -> SendItem(mit->second)) { written = true; } @@ -275,18 +226,13 @@ int p3MsgService::save_config() { //RsMsgItem *mi = (*mit)->clone(); //mi -> msgFlags |= RS_MSG_FLAGS_PENDING; - if (pa_out -> SendItem(*mit)) + if (pa_out -> SendItem(mit->second)) { written = true; } } - if (!written) - { - /* need to push something out to overwrite old data! (For WINDOWS ONLY) */ - } - delete pa_out; return 1; } @@ -324,11 +270,11 @@ int p3MsgService::load_config() std::cerr << "MSG_PENDING"; std::cerr << std::endl; mitem->print(std::cerr); - msgOutgoing.push_back(mitem); + msgOutgoing[mitem->msgId] = mitem; } else { - imsg.push_back(mitem); + imsg[mitem->msgId] = mitem; } } else @@ -350,7 +296,7 @@ void p3MsgService::loadWelcomeMsg() msg -> PeerId(mConnMgr->getOwnId()); - msg -> sendTime = 0; + msg -> sendTime = time(NULL); msg -> subject = L"Welcome to Retroshare"; @@ -365,8 +311,344 @@ void p3MsgService::loadWelcomeMsg() msg -> message += L"Enjoy.\n"; - imsg.push_back(msg); + msg -> msgId = getNewUniqueMsgId(); + + imsg[msg->msgId] = msg; +} + + +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ +/***********************************************************************/ + + +/****************************************/ +/****************************************/ + +bool p3MsgService::getMessageSummaries(std::list &msgList) +{ + /* do stuff */ + msgList.clear(); + + std::map::iterator mit; + for(mit = imsg.begin(); mit != imsg.end(); mit++) + { + MsgInfoSummary mis; + initRsMIS(mit->second, mis); + msgList.push_back(mis); + } + + for(mit = msgOutgoing.begin(); mit != msgOutgoing.end(); mit++) + { + MsgInfoSummary mis; + initRsMIS(mit->second, mis); + msgList.push_back(mis); + } + return 1; +} + + +bool p3MsgService::getMessage(std::string mId, MessageInfo &msg) +{ + std::map::iterator mit; + uint32_t msgId = atoi(mId.c_str()); + + mit = imsg.find(msgId); + if (mit == imsg.end()) + { + mit = msgOutgoing.find(msgId); + if (mit == msgOutgoing.end()) + { + return false; + } + } + + /* mit valid */ + initRsMI(mit->second, msg); + return true; +} + + +/* remove based on the unique mid (stored in sid) */ +bool p3MsgService::removeMsgId(std::string mid) +{ + std::map::iterator mit; + uint32_t msgId = atoi(mid.c_str()); + + mit = imsg.find(msgId); + if (mit != imsg.end()) + { + RsMsgItem *mi = mit->second; + imsg.erase(mit); + delete mi; + msgChanged.IndicateChanged(); + msgMajorChanged.IndicateChanged(); + + return true; + } + + mit = msgOutgoing.find(msgId); + if (mit != msgOutgoing.end()) + { + RsMsgItem *mi = mit->second; + msgOutgoing.erase(mit); + delete mi; + msgChanged.IndicateChanged(); + msgMajorChanged.IndicateChanged(); + + return true; + } + + return false; +} + +bool p3MsgService::markMsgIdRead(std::string mid) +{ + std::map::iterator mit; + uint32_t msgId = atoi(mid.c_str()); + + mit = imsg.find(msgId); + if (mit != imsg.end()) + { + RsMsgItem *mi = mit->second; + mi -> msgFlags &= ~(RS_MSG_FLAGS_NEW); + msgChanged.IndicateChanged(); + return true; + } + return false; +} + +/****************************************/ +/****************************************/ + /* Message Items */ +int p3MsgService::sendMessage(RsMsgItem *item) +{ + pqioutput(PQL_DEBUG_BASIC, msgservicezone, + "p3MsgService::sendMessage()"); + + /* add pending flag */ + item->msgFlags |= + (RS_MSG_FLAGS_OUTGOING | + RS_MSG_FLAGS_PENDING); + /* STORE MsgID */ + item -> msgId = getNewUniqueMsgId(); + msgOutgoing[item->msgId] = item; + + return 1; +} + +bool p3MsgService::MessageSend(MessageInfo &info) +{ + std::list::const_iterator pit; + for(pit = info.msgto.begin(); pit != info.msgto.end(); pit++) + { + RsMsgItem *msg = initMIRsMsg(info, *pit); + if (msg) + { + sendMessage(msg); + } + } + + for(pit = info.msgcc.begin(); pit != info.msgcc.end(); pit++) + { + RsMsgItem *msg = initMIRsMsg(info, *pit); + if (msg) + { + sendMessage(msg); + } + } + + for(pit = info.msgbcc.begin(); pit != info.msgbcc.end(); pit++) + { + RsMsgItem *msg = initMIRsMsg(info, *pit); + if (msg) + { + sendMessage(msg); + } + } + + /* send to ourselves as well */ + RsMsgItem *msg = initMIRsMsg(info, mConnMgr->getOwnId()); + if (msg) + { + sendMessage(msg); + } + + return true; +} + +/****************************************/ +/****************************************/ + + +/****************************************/ + +/**** HELPER FNS For Chat/Msg/Channel Lists ************ + * These aren't required to be locked, unless + * the data used is from internal stores -> then they should be. + */ + +void p3MsgService::initRsMI(RsMsgItem *msg, MessageInfo &mi) +{ + + mi.msgflags = 0; + + /* translate flags, if we sent it... outgoing */ + if ((msg->msgFlags & RS_MSG_FLAGS_OUTGOING) + || (msg->PeerId() == mConnMgr->getOwnId())) + { + mi.msgflags |= RS_MSG_OUTGOING; + } + /* if it has a pending flag, then its in the outbox */ + if (msg->msgFlags & RS_MSG_FLAGS_PENDING) + { + mi.msgflags |= RS_MSG_PENDING; + } + if (msg->msgFlags & RS_MSG_FLAGS_NEW) + { + mi.msgflags |= RS_MSG_NEW; + } + + mi.ts = msg->sendTime; + mi.srcId = msg->PeerId(); + { + //msg->msgId; + std::ostringstream out; + out << msg->msgId; + mi.msgId = out.str(); + } + + std::list::iterator pit; + + for(pit = msg->msgto.ids.begin(); + pit != msg->msgto.ids.end(); pit++) + { + mi.msgto.push_back(*pit); + } + + for(pit = msg->msgcc.ids.begin(); + pit != msg->msgcc.ids.end(); pit++) + { + mi.msgcc.push_back(*pit); + } + + for(pit = msg->msgbcc.ids.begin(); + pit != msg->msgbcc.ids.end(); pit++) + { + mi.msgbcc.push_back(*pit); + } + + mi.title = msg->subject; + mi.msg = msg->message; + + mi.attach_title = msg->attachment.title; + mi.attach_comment = msg->attachment.comment; + + mi.count = 0; + mi.size = 0; + + std::list::iterator it; + for(it = msg->attachment.items.begin(); + it != msg->attachment.items.end(); it++) + { + FileInfo fi; + fi.fname = RsDirUtil::getTopDir(it->name); + fi.size = it->filesize; + fi.hash = it->hash; + fi.path = it->path; + mi.files.push_back(fi); + mi.count++; + mi.size += fi.size; + } + +} + + +void p3MsgService::initRsMIS(RsMsgItem *msg, MsgInfoSummary &mis) +{ + mis.msgflags = 0; + + /* translate flags, if we sent it... outgoing */ + if ((msg->msgFlags & RS_MSG_FLAGS_OUTGOING) + || (msg->PeerId() == mConnMgr->getOwnId())) + { + mis.msgflags |= RS_MSG_OUTGOING; + } + /* if it has a pending flag, then its in the outbox */ + if (msg->msgFlags & RS_MSG_FLAGS_PENDING) + { + mis.msgflags |= RS_MSG_PENDING; + } + if (msg->msgFlags & RS_MSG_FLAGS_NEW) + { + mis.msgflags |= RS_MSG_NEW; + } + + mis.srcId = msg->PeerId(); + { + //msg->msgId; + std::ostringstream out; + out << msg->msgId; + mis.msgId = out.str(); + } + + mis.title = msg->subject; + mis.count = msg->attachment.items.size(); + mis.ts = msg->sendTime; } +RsMsgItem *p3MsgService::initMIRsMsg(MessageInfo &info, std::string to) +{ + RsMsgItem *msg = new RsMsgItem(); + + msg -> PeerId(to); + + msg -> msgFlags = 0; + msg -> msgId = 0; + msg -> sendTime = time(NULL); + msg -> recvTime = 0; + + msg -> subject = info.title; + msg -> message = info.msg; + + std::list::iterator pit; + for(pit = info.msgto.begin(); pit != info.msgto.end(); pit++) + { + msg -> msgto.ids.push_back(*pit); + } + + for(pit = info.msgcc.begin(); pit != info.msgcc.end(); pit++) + { + msg -> msgcc.ids.push_back(*pit); + } + + /* We don't fill in bcc (unless to ourselves) */ + if (to == mConnMgr->getOwnId()) + { + for(pit = info.msgbcc.begin(); pit != info.msgbcc.end(); pit++) + { + msg -> msgbcc.ids.push_back(*pit); + } + } + + msg -> attachment.title = info.attach_title; + msg -> attachment.comment = info.attach_comment; + + std::list::iterator it; + for(it = info.files.begin(); it != info.files.end(); it++) + { + RsTlvFileItem mfi; + mfi.hash = it -> hash; + mfi.name = it -> fname; + mfi.filesize = it -> size; + msg -> attachment.items.push_back(mfi); + } + + std::cerr << "p3MsgService::initMIRsMsg()" << std::endl; + msg->print(std::cerr); + return msg; +} + diff --git a/libretroshare/src/services/p3msgservice.h b/libretroshare/src/services/p3msgservice.h index 722d7e362..868f1afc0 100644 --- a/libretroshare/src/services/p3msgservice.h +++ b/libretroshare/src/services/p3msgservice.h @@ -31,12 +31,14 @@ #include #include +#include "rsiface/rsmsgs.h" + #include "pqi/pqi.h" #include "pqi/pqiindic.h" #include "services/p3service.h" #include "serialiser/rsmsgitems.h" +#include "util/rsthreads.h" -#include "rsiface/rsiface.h" class p3ConnectMgr; class p3MsgService: public p3Service @@ -44,17 +46,25 @@ class p3MsgService: public p3Service public: p3MsgService(p3ConnectMgr *cm); + /* External Interface */ +bool ModifiedMsgs(); +bool getMessageSummaries(std::list &msgList); +bool getMessage(std::string mid, MessageInfo &msg); + +bool removeMsgId(std::string mid); +bool markMsgIdRead(std::string mid); + +bool MessageSend(MessageInfo &info); + + + void loadWelcomeMsg(); /* startup message */ -int sendMessage(RsMsgItem *item); int checkOutgoingMessages(); std::list &getMsgList(); std::list &getMsgOutList(); - // cleaning up.... -int removeMsgId(uint32_t mid); /* id stored in sid */ -int markMsgIdRead(uint32_t mid); int load_config(); int save_config(); @@ -64,20 +74,27 @@ int status(); private: +int sendMessage(RsMsgItem *item); int incomingMsgs(); +void initRsMI(RsMsgItem *msg, MessageInfo &mi); +void initRsMIS(RsMsgItem *msg, MsgInfoSummary &mis); +RsMsgItem *initMIRsMsg(MessageInfo &info, std::string to); -std::list imsg; -std::list msgOutgoing; /* ones that haven't made it out yet! */ + + /* Mutex Required for stuff below */ + + RsMutex msgMtx; + +std::map imsg; +std::map msgOutgoing; /* ones that haven't made it out yet! */ p3ConnectMgr *mConnMgr; -// bool state flags. - public: Indicator msgChanged; Indicator msgMajorChanged; -std::string config_dir; + std::string config_dir; }; #endif // MESSAGE_SERVICE_HEADER diff --git a/libretroshare/src/services/p3service.cc b/libretroshare/src/services/p3service.cc index 7d2a0fc00..1d1bfd094 100644 --- a/libretroshare/src/services/p3service.cc +++ b/libretroshare/src/services/p3service.cc @@ -26,6 +26,8 @@ #include "pqi/pqi.h" #include "services/p3service.h" +#define SERV_DEBUG 1 + void p3Service::addSerialType(RsSerialType *st) { rsSerialiser->addSerialType(st); @@ -78,7 +80,10 @@ int p3Service::receive(RsRawItem *raw) { srvMtx.lock(); /***** LOCK MUTEX *****/ - std::cerr << "p3Service::receive()" << std::endl; +#ifdef SERV_DEBUG + std::cerr << "p3Service::receive()"; + std::cerr << std::endl; +#endif /* convert to RsServiceItem */ uint32_t size = raw->getRawLength(); @@ -86,11 +91,20 @@ int p3Service::receive(RsRawItem *raw) if ((!item) || (size != raw->getRawLength())) { /* error in conversion */ +#ifdef SERV_DEBUG std::cerr << "p3Service::receive() Error" << std::endl; std::cerr << "p3Service::receive() Size: " << size << std::endl; std::cerr << "p3Service::receive() RawLength: " << raw->getRawLength() << std::endl; +#endif + if (item) { +#ifdef SERV_DEBUG + std::cerr << "p3Service::receive() Bad Item:"; + std::cerr << std::endl; + item->print(std::cerr, 0); + std::cerr << std::endl; +#endif delete item; } } @@ -99,6 +113,13 @@ int p3Service::receive(RsRawItem *raw) /* if we have something - pass it on */ if (item) { +#ifdef SERV_DEBUG + std::cerr << "p3Service::receive() item:"; + std::cerr << std::endl; + item->print(std::cerr, 0); + std::cerr << std::endl; +#endif + /* ensure PeerId is transferred */ item->PeerId(raw->PeerId()); recv_queue.push_back(item); @@ -126,10 +147,22 @@ RsRawItem *p3Service::send() RsItem *si = send_queue.front(); send_queue.pop_front(); +#ifdef SERV_DEBUG + std::cerr << "p3Service::send() Sending item:"; + std::cerr << std::endl; + si->print(std::cerr, 0); + std::cerr << std::endl; +#endif + /* try to convert */ uint32_t size = rsSerialiser->size(si); if (!size) { +#ifdef SERV_DEBUG + std::cerr << "p3Service::send() ERROR size == 0"; + std::cerr << std::endl; +#endif + /* can't convert! */ delete si; srvMtx.unlock(); /***** UNLOCK MUTEX *****/ @@ -139,12 +172,20 @@ RsRawItem *p3Service::send() RsRawItem *raw = new RsRawItem(si->PacketId(), size); if (!rsSerialiser->serialise(si, raw->getRawData(), &size)) { +#ifdef SERV_DEBUG + std::cerr << "p3Service::send() ERROR serialise failed"; + std::cerr << std::endl; +#endif delete raw; raw = NULL; } if ((raw) && (size != raw->getRawLength())) { +#ifdef SERV_DEBUG + std::cerr << "p3Service::send() ERROR serialise size mismatch"; + std::cerr << std::endl; +#endif delete raw; raw = NULL; } diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index 84de3a0c5..3b7d3c078 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -45,6 +45,17 @@ bool trylock() { return (0 == pthread_mutex_trylock(&realMutex)); } pthread_mutex_t realMutex; }; +class RsStackMutex +{ + public: + + RsStackMutex(RsMutex &mtx): mMtx(mtx) { mMtx.lock(); } + ~RsStackMutex() { mMtx.unlock(); } + + private: + RsMutex &mMtx; +}; + class RsThread; /* to create a thread! */