2007-12-11 20:43:17 -05:00
|
|
|
/*
|
|
|
|
* "$Id: p3ChatService.cc,v 1.24 2007-05-05 16:10:06 rmf24 Exp $"
|
|
|
|
*
|
|
|
|
* Other Bits for RetroShare.
|
|
|
|
*
|
|
|
|
* 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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-03-29 09:58:28 -04:00
|
|
|
#include "util/rsdir.h"
|
2009-05-05 09:18:53 -04:00
|
|
|
#include "rsiface/rsiface.h"
|
2009-03-20 16:41:31 -04:00
|
|
|
#include "pqi/pqibin.h"
|
2009-03-29 09:58:28 -04:00
|
|
|
#include "pqi/pqinotify.h"
|
2009-05-08 20:19:54 -04:00
|
|
|
#include "pqi/pqistore.h"
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
#include "services/p3chatservice.h"
|
|
|
|
|
2008-03-02 09:25:59 -05:00
|
|
|
/****
|
|
|
|
* #define CHAT_DEBUG 1
|
|
|
|
****/
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
/************ NOTE *********************************
|
|
|
|
* This Service is so simple that there is no
|
|
|
|
* mutex protection required!
|
|
|
|
*
|
|
|
|
*/
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-01-25 02:49:28 -05:00
|
|
|
p3ChatService::p3ChatService(p3ConnectMgr *cm)
|
2009-03-20 16:41:31 -04:00
|
|
|
:p3Service(RS_SERVICE_TYPE_CHAT), pqiConfig(CONFIG_TYPE_CHAT), mConnMgr(cm)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
addSerialType(new RsChatSerialiser());
|
2009-01-30 14:52:47 -05:00
|
|
|
|
|
|
|
_own_avatar = NULL ;
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int p3ChatService::tick()
|
|
|
|
{
|
2008-02-04 16:40:34 -05:00
|
|
|
|
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "p3ChatService::tick()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3ChatService::status()
|
|
|
|
{
|
2008-02-04 16:40:34 -05:00
|
|
|
|
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "p3ChatService::status()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************** Chat Stuff **********************/
|
|
|
|
|
2008-01-26 08:00:57 -05:00
|
|
|
int p3ChatService::sendChat(std::wstring msg)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
/* go through all the peers */
|
|
|
|
|
2008-01-25 02:49:28 -05:00
|
|
|
std::list<std::string> ids;
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
mConnMgr->getOnlineList(ids);
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-01-25 02:49:28 -05:00
|
|
|
/* add in own id -> so get reflection */
|
|
|
|
ids.push_back(mConnMgr->getOwnId());
|
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "p3ChatService::sendChat()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-01-25 02:49:28 -05:00
|
|
|
for(it = ids.begin(); it != ids.end(); it++)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = new RsChatMsgItem();
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2008-01-25 02:49:28 -05:00
|
|
|
ci->PeerId(*it);
|
2007-12-11 20:43:17 -05:00
|
|
|
ci->chatFlags = 0;
|
|
|
|
ci->sendTime = time(NULL);
|
|
|
|
ci->message = msg;
|
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "p3ChatService::sendChat() Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
ci->print(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
sendItem(ci);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-01-30 14:52:47 -05:00
|
|
|
class p3ChatService::AvatarInfo
|
|
|
|
{
|
|
|
|
public:
|
2009-02-22 12:36:39 -05:00
|
|
|
AvatarInfo()
|
|
|
|
{
|
|
|
|
_peer_is_new = false ; // true when the peer has a new avatar
|
|
|
|
_own_is_new = false ; // true when I myself a new avatar to send to this peer.
|
|
|
|
}
|
|
|
|
|
2009-01-30 14:52:47 -05:00
|
|
|
AvatarInfo(const unsigned char *jpeg_data,int size)
|
|
|
|
{
|
|
|
|
int n_c = size ;
|
2009-02-22 12:36:39 -05:00
|
|
|
int p = 2 ;// minimum value for sizeof(wchar_t) over win/mac/linux ;
|
2009-01-30 14:52:47 -05:00
|
|
|
int n = n_c/p + 1 ;
|
|
|
|
|
|
|
|
_jpeg_wstring = std::wstring(n,0) ;
|
|
|
|
|
|
|
|
for(int i=0;i<n;++i)
|
|
|
|
{
|
2009-02-22 12:36:39 -05:00
|
|
|
wchar_t h = jpeg_data[p*i+p-1] ;
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
for(int j=p-2;j>=0;--j)
|
2009-01-30 14:52:47 -05:00
|
|
|
{
|
|
|
|
h = h << 8 ;
|
|
|
|
h += jpeg_data[p*i+j] ;
|
|
|
|
}
|
|
|
|
_jpeg_wstring[i] = h ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AvatarInfo(const std::wstring& s) : _jpeg_wstring(s) {}
|
|
|
|
|
|
|
|
const std::wstring& toStdWString() const { return _jpeg_wstring; }
|
|
|
|
|
|
|
|
void toUnsignedChar(unsigned char *& data,int& size) const
|
|
|
|
{
|
2009-02-22 12:36:39 -05:00
|
|
|
int p = 2 ;// minimum value for sizeof(wchar_t) over win/mac/linux ;
|
2009-01-30 14:52:47 -05:00
|
|
|
int n = _jpeg_wstring.size() ;
|
|
|
|
int n_c = p*n ;
|
|
|
|
|
|
|
|
data = new unsigned char[n_c] ;
|
|
|
|
size = n_c ;
|
|
|
|
|
|
|
|
for(int i=0;i<n;++i)
|
|
|
|
{
|
|
|
|
wchar_t h = _jpeg_wstring[i] ;
|
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
for(int j=0;j<p;++j)
|
2009-01-30 14:52:47 -05:00
|
|
|
{
|
|
|
|
data[p*i+j] = (unsigned char)(h & 0xff) ;
|
|
|
|
h = h >> 8 ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::wstring _jpeg_wstring;
|
|
|
|
int _peer_is_new ; // true when the peer has a new avatar
|
|
|
|
int _own_is_new ; // true when I myself a new avatar to send to this peer.
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
void p3ChatService::sendStatusString( const std::string& id , const std::string& status_string)
|
|
|
|
{
|
|
|
|
RsChatStatusItem *cs = new RsChatStatusItem ;
|
|
|
|
|
|
|
|
cs->status_string = status_string ;
|
|
|
|
cs->PeerId(id);
|
|
|
|
|
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "sending chat status packet:" << std::endl ;
|
|
|
|
cs->print(std::cerr) ;
|
|
|
|
#endif
|
|
|
|
sendItem(cs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int p3ChatService::sendPrivateChat( std::wstring msg, std::string id)
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
// make chat item....
|
2008-02-04 16:40:34 -05:00
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "p3ChatService::sendPrivateChat()";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = new RsChatMsgItem();
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
ci->PeerId(id);
|
|
|
|
ci->chatFlags = RS_CHAT_FLAG_PRIVATE;
|
|
|
|
ci->sendTime = time(NULL);
|
|
|
|
ci->message = msg;
|
|
|
|
|
2009-01-30 14:52:47 -05:00
|
|
|
std::map<std::string,AvatarInfo*>::iterator it = _avatars.find(id) ;
|
|
|
|
|
2009-02-22 12:36:39 -05:00
|
|
|
if(it == _avatars.end())
|
|
|
|
{
|
|
|
|
_avatars[id] = new AvatarInfo ;
|
|
|
|
it = _avatars.find(id) ;
|
|
|
|
}
|
2009-01-30 14:52:47 -05:00
|
|
|
if(it != _avatars.end() && it->second->_own_is_new)
|
|
|
|
{
|
2009-02-22 12:36:39 -05:00
|
|
|
#ifdef CHAT_DEBUG
|
2009-01-30 14:52:47 -05:00
|
|
|
std::cerr << "p3ChatService::sendPrivateChat: new avatar never sent to peer " << id << ". Setting <new> flag to packet." << std::endl;
|
2009-02-22 12:36:39 -05:00
|
|
|
#endif
|
2009-01-30 14:52:47 -05:00
|
|
|
|
|
|
|
ci->chatFlags |= RS_CHAT_FLAG_AVATAR_AVAILABLE ;
|
|
|
|
it->second->_own_is_new = false ;
|
|
|
|
}
|
|
|
|
|
2008-02-04 16:40:34 -05:00
|
|
|
#ifdef CHAT_DEBUG
|
2009-02-22 12:36:39 -05:00
|
|
|
std::cerr << "Sending msg to peer " << id << ", flags = " << ci->chatFlags << std::endl ;
|
2008-02-04 16:40:34 -05:00
|
|
|
std::cerr << "p3ChatService::sendPrivateChat() Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
ci->print(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
#endif
|
2007-12-11 20:43:17 -05:00
|
|
|
|
|
|
|
sendItem(ci);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
std::list<RsChatMsgItem *> p3ChatService::getChatQueue()
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
|
|
|
time_t now = time(NULL);
|
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
RsItem *item ;
|
|
|
|
std::list<RsChatMsgItem *> ilist;
|
2007-12-11 20:43:17 -05:00
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
while(NULL != (item=recvItem()))
|
2007-12-11 20:43:17 -05:00
|
|
|
{
|
2008-04-04 08:19:50 -04:00
|
|
|
#ifdef CHAT_DEBUG
|
2009-05-05 09:18:53 -04:00
|
|
|
std::cerr << "p3ChatService::getChatQueue() Item:" << (void*)item << std::endl ;
|
2009-02-22 12:36:39 -05:00
|
|
|
#endif
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = dynamic_cast<RsChatMsgItem*>(item) ;
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
if(ci != NULL)
|
2009-01-30 14:52:47 -05:00
|
|
|
{
|
2009-05-05 09:18:53 -04:00
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "p3ChatService::getChatQueue() Item:";
|
|
|
|
std::cerr << std::endl;
|
|
|
|
ci->print(std::cerr);
|
|
|
|
std::cerr << std::endl;
|
|
|
|
std::cerr << "Got msg. Flags = " << ci->chatFlags << std::endl ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(ci->chatFlags & RS_CHAT_FLAG_CONTAINS_AVATAR) // no msg here. Just an avatar.
|
|
|
|
receiveAvatarJpegData(ci) ;
|
|
|
|
else if(ci->chatFlags & RS_CHAT_FLAG_REQUESTS_AVATAR) // no msg here. Just an avatar request.
|
|
|
|
sendAvatarJpegData(ci->PeerId()) ;
|
|
|
|
else // normal msg. Return it normally.
|
2009-01-30 14:52:47 -05:00
|
|
|
{
|
2009-05-05 09:18:53 -04:00
|
|
|
// Check if new avatar is available at peer's. If so, send a request to get the avatar.
|
|
|
|
if(ci->chatFlags & RS_CHAT_FLAG_AVATAR_AVAILABLE)
|
|
|
|
{
|
|
|
|
std::cerr << "New avatar is available for peer " << ci->PeerId() << ", sending request" << std::endl ;
|
|
|
|
sendAvatarRequest(ci->PeerId()) ;
|
|
|
|
ci->chatFlags &= ~RS_CHAT_FLAG_AVATAR_AVAILABLE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<std::string,AvatarInfo *>::const_iterator it = _avatars.find(ci->PeerId()) ;
|
|
|
|
|
|
|
|
std::cerr << "p3chatservice:: avatar requested from above. " << std::endl ;
|
|
|
|
// has avatar. Return it strait away.
|
|
|
|
//
|
|
|
|
if(it!=_avatars.end() && it->second->_peer_is_new)
|
|
|
|
{
|
|
|
|
std::cerr << "Adatar is new for peer. ending info above" << std::endl ;
|
|
|
|
ci->chatFlags |= RS_CHAT_FLAG_AVATAR_AVAILABLE ;
|
|
|
|
}
|
|
|
|
|
|
|
|
ci->recvTime = now;
|
|
|
|
ilist.push_back(ci);
|
2009-01-30 14:52:47 -05:00
|
|
|
}
|
2009-05-05 09:18:53 -04:00
|
|
|
}
|
|
|
|
RsChatStatusItem *cs = dynamic_cast<RsChatStatusItem*>(item) ;
|
|
|
|
|
|
|
|
if(cs != NULL)
|
|
|
|
{
|
|
|
|
// we should notify for a status string for the current peer.
|
|
|
|
#ifdef CHAT_DEBUG
|
|
|
|
std::cerr << "Received status string \"" << cs->status_string << "\"" << std::endl ;
|
|
|
|
#endif
|
|
|
|
rsicontrol->getNotify().notifyChatStatus(cs->PeerId(),cs->status_string) ;
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
delete item ;
|
2009-01-30 14:52:47 -05:00
|
|
|
}
|
2007-12-11 20:43:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return ilist;
|
|
|
|
}
|
|
|
|
|
2009-01-30 14:52:47 -05:00
|
|
|
void p3ChatService::setOwnAvatarJpegData(const unsigned char *data,int size)
|
|
|
|
{
|
2009-03-20 16:41:31 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
std::cerr << "p3chatservice: Setting own avatar to new image." << std::endl ;
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-03-20 16:41:31 -04:00
|
|
|
if(_own_avatar != NULL)
|
|
|
|
delete _own_avatar ;
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-03-20 16:41:31 -04:00
|
|
|
_own_avatar = new AvatarInfo(data,size) ;
|
|
|
|
|
|
|
|
// set the info that our avatar is new, for all peers
|
|
|
|
for(std::map<std::string,AvatarInfo *>::iterator it(_avatars.begin());it!=_avatars.end();++it)
|
|
|
|
it->second->_own_is_new = true ;
|
|
|
|
}
|
|
|
|
IndicateConfigChanged();
|
2009-05-08 19:07:08 -04:00
|
|
|
std::cerr << "p3chatservice:setOwnAvatarJpegData() done." << std::endl ;
|
2009-01-30 14:52:47 -05:00
|
|
|
}
|
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
void p3ChatService::receiveAvatarJpegData(RsChatMsgItem *ci)
|
2009-01-30 14:52:47 -05:00
|
|
|
{
|
2009-03-20 16:41:31 -04:00
|
|
|
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
|
2009-01-30 14:52:47 -05:00
|
|
|
std::cerr << "p3chatservice: received avatar jpeg data for peer " << ci->PeerId() << ". Storing it." << std::endl ;
|
|
|
|
|
|
|
|
bool new_peer = (_avatars.find(ci->PeerId()) == _avatars.end()) ;
|
|
|
|
|
|
|
|
_avatars[ci->PeerId()] = new AvatarInfo(ci->message) ;
|
|
|
|
_avatars[ci->PeerId()]->_peer_is_new = true ;
|
|
|
|
_avatars[ci->PeerId()]->_own_is_new = new_peer ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3ChatService::getOwnAvatarJpegData(unsigned char *& data,int& size)
|
|
|
|
{
|
|
|
|
// should be a Mutex here.
|
2009-03-20 16:41:31 -04:00
|
|
|
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
|
2009-01-30 14:52:47 -05:00
|
|
|
|
|
|
|
std::cerr << "p3chatservice:: own avatar requested from above. " << std::endl ;
|
|
|
|
// has avatar. Return it strait away.
|
|
|
|
//
|
|
|
|
if(_own_avatar != NULL)
|
|
|
|
_own_avatar->toUnsignedChar(data,size) ;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data=NULL ;
|
|
|
|
size=0 ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void p3ChatService::getAvatarJpegData(const std::string& peer_id,unsigned char *& data,int& size)
|
|
|
|
{
|
|
|
|
// should be a Mutex here.
|
2009-03-20 16:41:31 -04:00
|
|
|
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
2009-01-30 14:52:47 -05:00
|
|
|
std::map<std::string,AvatarInfo *>::const_iterator it = _avatars.find(peer_id) ;
|
|
|
|
|
|
|
|
std::cerr << "p3chatservice:: avatar requested from above. " << std::endl ;
|
|
|
|
// has avatar. Return it strait away.
|
|
|
|
//
|
|
|
|
if(it!=_avatars.end())
|
|
|
|
{
|
|
|
|
it->second->toUnsignedChar(data,size) ;
|
|
|
|
it->second->_peer_is_new = false ;
|
|
|
|
std::cerr << "Already has avatar. Returning it" << std::endl ;
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
std::cerr << "No avatar for this peer. Requesting it by sending request packet." << std::endl ;
|
|
|
|
|
|
|
|
sendAvatarRequest(peer_id) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void p3ChatService::sendAvatarRequest(const std::string& peer_id)
|
|
|
|
{
|
|
|
|
// Doesn't have avatar. Request it.
|
|
|
|
//
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = new RsChatMsgItem();
|
2009-01-30 14:52:47 -05:00
|
|
|
|
|
|
|
ci->PeerId(peer_id);
|
|
|
|
ci->chatFlags = RS_CHAT_FLAG_PRIVATE | RS_CHAT_FLAG_REQUESTS_AVATAR ;
|
|
|
|
ci->sendTime = time(NULL);
|
|
|
|
ci->message = std::wstring() ;
|
|
|
|
|
|
|
|
std::cerr << "p3ChatService::sending request for avatar, to peer " << peer_id << std::endl ;
|
|
|
|
std::cerr << std::endl;
|
|
|
|
|
|
|
|
sendItem(ci);
|
|
|
|
}
|
|
|
|
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *p3ChatService::makeOwnAvatarItem()
|
2009-03-20 16:41:31 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = new RsChatMsgItem();
|
2009-03-20 16:41:31 -04:00
|
|
|
|
|
|
|
ci->chatFlags = RS_CHAT_FLAG_PRIVATE | RS_CHAT_FLAG_CONTAINS_AVATAR ;
|
|
|
|
ci->sendTime = time(NULL);
|
|
|
|
ci->message = _own_avatar->toStdWString() ;
|
|
|
|
|
|
|
|
return ci ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-30 14:52:47 -05:00
|
|
|
void p3ChatService::sendAvatarJpegData(const std::string& peer_id)
|
|
|
|
{
|
|
|
|
std::cerr << "p3chatservice: sending requested for peer " << peer_id << ", data=" << (void*)_own_avatar << std::endl ;
|
|
|
|
|
|
|
|
if(_own_avatar != NULL)
|
2009-03-20 16:41:31 -04:00
|
|
|
{
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = makeOwnAvatarItem();
|
2009-03-20 16:41:31 -04:00
|
|
|
ci->PeerId(peer_id);
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-03-20 16:41:31 -04:00
|
|
|
// take avatar, and embed it into a std::wstring.
|
|
|
|
//
|
|
|
|
std::cerr << "p3ChatService::sending avatar image to peer" << peer_id << ", string size = " << ci->message.size() << std::endl ;
|
|
|
|
std::cerr << std::endl;
|
2009-01-30 14:52:47 -05:00
|
|
|
|
2009-03-20 16:41:31 -04:00
|
|
|
sendItem(ci) ;
|
|
|
|
}
|
2009-01-30 14:52:47 -05:00
|
|
|
else
|
|
|
|
std::cerr << "Doing nothing" << std::endl ;
|
|
|
|
}
|
|
|
|
|
2009-03-20 16:41:31 -04:00
|
|
|
bool p3ChatService::loadConfiguration(std::string &loadHash)
|
|
|
|
{
|
|
|
|
std::list<std::string>::iterator it;
|
|
|
|
std::string msgfile = Filename();
|
|
|
|
|
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsChatSerialiser());
|
|
|
|
|
|
|
|
BinFileInterface *in = new BinFileInterface(msgfile.c_str(), BIN_FLAGS_READABLE | BIN_FLAGS_HASH_DATA);
|
2009-05-12 17:55:50 -04:00
|
|
|
pqistore *pa_in = new pqistore(rss, "CHATCONFIG", in, BIN_FLAGS_READABLE);
|
2009-03-20 16:41:31 -04:00
|
|
|
RsItem *item;
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *mitem;
|
2009-03-20 16:41:31 -04:00
|
|
|
|
|
|
|
while((item = pa_in -> GetItem()))
|
|
|
|
{
|
2009-05-05 09:18:53 -04:00
|
|
|
if(NULL != (mitem = dynamic_cast<RsChatMsgItem *>(item)))
|
2009-03-20 16:41:31 -04:00
|
|
|
{
|
|
|
|
RsStackMutex stack(mChatMtx); /********** STACK LOCKED MTX ******/
|
|
|
|
|
|
|
|
_own_avatar = new AvatarInfo(mitem->message) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string hashin = in->gethash();
|
|
|
|
delete pa_in;
|
|
|
|
|
|
|
|
if (hashin != loadHash)
|
|
|
|
{
|
|
|
|
/* big error message! */
|
|
|
|
std::cerr << "p3ChatService::loadConfiguration() FAILED! avatar Tampered" << std::endl;
|
|
|
|
std::string msgfileold = msgfile + ".failed";
|
|
|
|
|
|
|
|
rename(msgfile.c_str(), msgfileold.c_str());
|
|
|
|
|
|
|
|
std::cerr << "Moving Old file to: " << msgfileold << std::endl;
|
|
|
|
std::cerr << "removing dodgey msgs" << std::endl;
|
|
|
|
|
|
|
|
_own_avatar = NULL ;
|
|
|
|
|
|
|
|
setHash("");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHash(hashin);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool p3ChatService::saveConfiguration()
|
|
|
|
{
|
2009-05-08 20:19:54 -04:00
|
|
|
/* now we create a pqistore, and stream all the msgs into it */
|
2009-03-20 16:41:31 -04:00
|
|
|
|
|
|
|
std::string msgfile = Filename();
|
|
|
|
std::string msgfiletmp = Filename()+".tmp";
|
|
|
|
|
|
|
|
RsSerialiser *rss = new RsSerialiser();
|
|
|
|
rss->addSerialType(new RsChatSerialiser());
|
|
|
|
|
|
|
|
BinFileInterface *out = new BinFileInterface(msgfiletmp.c_str(), BIN_FLAGS_WRITEABLE | BIN_FLAGS_HASH_DATA);
|
2009-05-12 17:55:50 -04:00
|
|
|
pqistore *pa_out = new pqistore(rss, "CHATCONFIG", out, BIN_FLAGS_WRITEABLE);
|
2009-03-20 16:41:31 -04:00
|
|
|
|
|
|
|
if(_own_avatar != NULL)
|
|
|
|
{
|
|
|
|
std::cerr << "Saving avatar config to file " << msgfile << std::endl ;
|
2009-05-05 09:18:53 -04:00
|
|
|
RsChatMsgItem *ci = makeOwnAvatarItem() ;
|
2009-03-20 16:41:31 -04:00
|
|
|
ci->PeerId(mConnMgr->getOwnId());
|
|
|
|
|
|
|
|
if(!pa_out -> SendItem(ci))
|
|
|
|
return false ;
|
|
|
|
}
|
|
|
|
|
|
|
|
setHash(out->gethash());
|
|
|
|
delete pa_out;
|
|
|
|
|
2009-03-29 09:58:28 -04:00
|
|
|
if(!RsDirUtil::renameFile(msgfiletmp,msgfile))
|
|
|
|
{
|
|
|
|
getPqiNotify()->AddSysMessage(0, RS_SYS_WARNING, "File rename error", "Error while renaming file " + msgfile) ;
|
2009-03-20 16:41:31 -04:00
|
|
|
return false ;
|
2009-03-29 09:58:28 -04:00
|
|
|
}
|
2009-03-20 16:41:31 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-11 20:43:17 -05:00
|
|
|
|