Patch (modified) from Phenom to allow auto-subscribe to chat lobbies

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6466 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2013-06-29 16:15:33 +00:00
parent 9f88917ac1
commit 7414659bd9
12 changed files with 243 additions and 16 deletions

View file

@ -57,6 +57,7 @@ template<int n> class t_RsFlags32
#define FLAGS_TAG_FILE_STORAGE 0x184738
#define FLAGS_TAG_FILE_SEARCH 0xf29ba5
#define FLAGS_TAG_SERVICE_PERM 0x380912
#define FLAGS_TAG_SERVICE_CHAT 0x839042
// Flags for requesting transfers, ask for turtle, cache, speed, etc.
//
@ -74,3 +75,7 @@ typedef t_RsFlags32<FLAGS_TAG_FILE_SEARCH > FileSearchFlags ;
//
typedef t_RsFlags32<FLAGS_TAG_SERVICE_PERM > ServicePermissionFlags ;
// Flags for chat lobbies
//
typedef t_RsFlags32<FLAGS_TAG_SERVICE_CHAT > ChatLobbyFlags ;

View file

@ -77,6 +77,8 @@
#define RS_CHAT_LOBBY_PRIVACY_LEVEL_PUBLIC 1 /* lobby is visible by friends. Friends can connect.*/
#define RS_CHAT_LOBBY_PRIVACY_LEVEL_PRIVATE 2 /* lobby invisible by friends. Peers on invitation only .*/
const ChatLobbyFlags RS_CHAT_LOBBY_FLAGS_AUTO_SUBSCRIBE( 0x00000001 ) ;
typedef uint64_t ChatLobbyId ;
typedef uint64_t ChatLobbyMsgId ;
typedef std::string ChatLobbyNickName ;
@ -321,6 +323,8 @@ virtual bool setNickNameForChatLobby(const ChatLobbyId& lobby_id,const std::stri
virtual bool getNickNameForChatLobby(const ChatLobbyId& lobby_id,std::string& nick) = 0 ;
virtual bool setDefaultNickNameForChatLobby(const std::string& nick) = 0;
virtual bool getDefaultNickNameForChatLobby(std::string& nick) = 0 ;
virtual void setLobbyAutoSubscribe(const ChatLobbyId& lobby_id, const bool autoSubscribe) = 0 ;
virtual bool getLobbyAutoSubscribe(const ChatLobbyId& lobby_id) = 0 ;
virtual ChatLobbyId createChatLobby(const std::string& lobby_name,const std::string& lobby_topic,const std::list<std::string>& invited_friends,uint32_t lobby_privacy_type) = 0 ;
/****************************************/

View file

@ -332,6 +332,17 @@ ChatLobbyId p3Msgs::createChatLobby(const std::string& lobby_name,const std::str
return mChatSrv->createChatLobby(lobby_name,lobby_topic,invited_friends,privacy_type) ;
}
void p3Msgs::setLobbyAutoSubscribe(const ChatLobbyId& lobby_id, const bool autoSubscribe)
{
mChatSrv->setLobbyAutoSubscribe(lobby_id, autoSubscribe);
}
bool p3Msgs::getLobbyAutoSubscribe(const ChatLobbyId& lobby_id)
{
return mChatSrv->getLobbyAutoSubscribe(lobby_id);
}
bool p3Msgs::acceptLobbyInvite(const ChatLobbyId& id)
{
return mChatSrv->acceptLobbyInvite(id) ;

View file

@ -185,6 +185,8 @@ class p3Msgs: public RsMsgs
virtual bool getNickNameForChatLobby(const ChatLobbyId&,std::string& nick) ;
virtual bool setDefaultNickNameForChatLobby(const std::string&) ;
virtual bool getDefaultNickNameForChatLobby(std::string& nick) ;
virtual void setLobbyAutoSubscribe(const ChatLobbyId& lobby_id, const bool autoSubscribe);
virtual bool getLobbyAutoSubscribe(const ChatLobbyId& lobby_id);
virtual ChatLobbyId createChatLobby(const std::string& lobby_name,const std::string& lobby_topic,const std::list<std::string>& invited_friends,uint32_t privacy_type) ;
virtual bool createDistantChatInvite(const std::string& pgp_id,time_t time_of_validity,std::string& encrypted_string) ;

View file

@ -211,8 +211,18 @@ std::ostream& RsPrivateChatDistantInviteConfigItem::print(std::ostream &out, uin
printRsItemEnd(out, "RsPrivateChatDistantInviteConfigItem", indent);
return out;
}
std::ostream& RsChatLobbyConfigItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsChatLobbyConfigItem", indent);
uint16_t int_Indent = indent + 2;
printIndent(out, int_Indent);
out << "lobby_Id: " << lobby_Id << std::endl;
out << "flags : " << flags << std::endl;
printRsItemEnd(out, "RsChatLobbyConfigItem", indent);
return out;
}
std::ostream& RsChatStatusItem::print(std::ostream &out, uint16_t indent)
{
printRsItemBase(out, "RsChatStatusItem", indent);
@ -281,6 +291,7 @@ RsItem *RsChatSerialiser::deserialise(void *data, uint32_t *pktsize)
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST: return new RsChatLobbyListItem(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_deprecated: return new RsChatLobbyListItem_deprecated(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_deprecated2:return new RsChatLobbyListItem_deprecated2(data,*pktsize) ;
case RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG: return new RsChatLobbyConfigItem(data,*pktsize) ;
default:
std::cerr << "Unknown packet type in chat!" << std::endl ;
return NULL ;
@ -441,6 +452,16 @@ uint32_t RsChatAvatarItem::serial_size()
return s;
}
uint32_t RsChatLobbyConfigItem::serial_size()
{
uint32_t s = 8; /* header */
s += 8;/* lobby_Id */
s += 4;/* flags */
return s;
}
/*************************************************************************/
RsChatAvatarItem::~RsChatAvatarItem()
{
@ -942,6 +963,43 @@ bool RsChatAvatarItem::serialise(void *data, uint32_t& pktsize)
return ok;
}
bool RsChatLobbyConfigItem::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 CHAT_DEBUG
std::cerr << "RsChatLobbyConfigItem::serialiseItem() Header: " << ok << std::endl;
std::cerr << "RsChatLobbyConfigItem::serialiseItem() Size: " << tlvsize << std::endl;
#endif
/* skip the header */
offset += 8;
/* add mandatory parts first */
ok &= setRawUInt64(data, tlvsize, &offset, lobby_Id);
ok &= setRawUInt32(data, tlvsize, &offset, flags );
if (offset != tlvsize)
{
ok = false;
#ifdef CHAT_DEBUG
std::cerr << "RsChatLobbyConfigItem::serialise() Size Error! " << std::endl;
#endif
}
return ok;
}
RsChatMsgItem::RsChatMsgItem(void *data,uint32_t /*size*/,uint8_t subtype)
: RsChatItem(subtype)
{
@ -1234,6 +1292,25 @@ RsPrivateChatDistantInviteConfigItem::RsPrivateChatDistantInviteConfigItem(void
if (!ok)
std::cerr << "Unknown error while deserializing." << std::endl ;
}
RsChatLobbyConfigItem::RsChatLobbyConfigItem(void *data,uint32_t /*size*/)
: RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG)
{
uint32_t offset = 8; // skip the header
uint32_t rssize = getRsItemSize(data);
bool ok = true ;
/* get mandatory parts first */
ok &= getRawUInt64(data, rssize, &offset, &lobby_Id);
ok &= getRawUInt32(data, rssize, &offset, &flags);
#ifdef CHAT_DEBUG
std::cerr << "Building new chat msg config item." << std::endl ;
#endif
if (offset != rssize)
std::cerr << "Size error while deserializing." << std::endl ;
if (!ok)
std::cerr << "Unknown error while deserializing." << std::endl ;
}
/* set data from RsChatMsgItem to RsPrivateChatMsgConfigItem */
void RsPrivateChatMsgConfigItem::set(RsChatMsgItem *ci, const std::string &/*peerId*/, uint32_t confFlags)

View file

@ -66,6 +66,7 @@ const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_EVENT = 0x10 ;
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_LIST_deprecated2 = 0x11 ; // to be removed (deprecated since 02 Dec. 2012)
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_LIST = 0x12 ;
const uint8_t RS_PKT_SUBTYPE_DISTANT_INVITE_CONFIG = 0x13 ;
const uint8_t RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG = 0x15 ;
// for defining tags themselves and msg tags
const uint8_t RS_PKT_SUBTYPE_MSG_TAG_TYPE = 0x03;
@ -337,7 +338,23 @@ class RsPrivateChatDistantInviteConfigItem: public RsChatItem
uint32_t time_of_validity ;
uint32_t last_hit_time ;
};
class RsChatLobbyConfigItem: public RsChatItem
{
public:
RsChatLobbyConfigItem() :RsChatItem(RS_PKT_SUBTYPE_CHAT_LOBBY_CONFIG) { lobby_Id = 0; }
RsChatLobbyConfigItem(void *data,uint32_t size) ; // deserialization
virtual ~RsChatLobbyConfigItem() {}
virtual void clear() { lobby_Id = 0; }
virtual std::ostream& print(std::ostream &out, uint16_t indent = 0);
virtual bool serialise(void *data,uint32_t& size) ; // Isn't it better that items can serialize themselves ?
virtual uint32_t serial_size() ; // deserialise is handled using a constructor
uint64_t lobby_Id;
uint32_t flags ;
};
// This class contains activity info for the sending peer: active, idle, typing, etc.
//

View file

@ -399,6 +399,11 @@ void p3ChatService::locked_printDebugInfo() const
for(std::set<std::string>::const_iterator it2(it->second.participating_friends.begin());it2!=it->second.participating_friends.end();++it2)
std::cerr << " With friend: " << *it2 << std::endl;
}
std::cerr << "Chat lobby flags: " << std::endl;
for( std::map<ChatLobbyId,ChatLobbyFlags>::const_iterator it(_known_lobbies_flags.begin()) ;it!=_known_lobbies_flags.end();++it)
std::cerr << " \"" << std::hex << it->first << "\" flags = " << it->second << std::dec << std::endl;
}
bool p3ChatService::isLobbyId(const std::string& id,ChatLobbyId& lobby_id)
@ -840,6 +845,8 @@ void p3ChatService::handleRecvChatLobbyList(RsChatLobbyListItem_deprecated2 *ite
}
void p3ChatService::handleRecvChatLobbyList(RsChatLobbyListItem *item)
{
std::list<ChatLobbyId> chatLobbyToSubscribe;
{
time_t now = time(NULL) ;
@ -851,7 +858,7 @@ void p3ChatService::handleRecvChatLobbyList(RsChatLobbyListItem *item)
rec.lobby_id = item->lobby_ids[i] ;
rec.lobby_name = item->lobby_names[i] ;
rec.lobby_topic = item->lobby_topics[i] ;
rec.lobby_topic = item->lobby_topics[i] ;
rec.participating_friends.insert(item->PeerId()) ;
if(_should_reset_lobby_counts)
@ -861,9 +868,21 @@ void p3ChatService::handleRecvChatLobbyList(RsChatLobbyListItem *item)
rec.last_report_time = now ;
rec.lobby_privacy_level = item->lobby_privacy_levels[i] ;
std::map<ChatLobbyId,ChatLobbyFlags>::const_iterator it(_known_lobbies_flags.find(item->lobby_ids[i])) ;
if(it != _known_lobbies_flags.end() && (it->second & RS_CHAT_LOBBY_FLAGS_AUTO_SUBSCRIBE))
{
ChatLobbyId clid = item->lobby_ids[i];
chatLobbyToSubscribe.push_back(clid);
}
}
}
std::list<ChatLobbyId>::iterator it;
for (it = chatLobbyToSubscribe.begin(); it != chatLobbyToSubscribe.end(); it++)
joinVisibleChatLobby(*it);
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_CHAT_LOBBY_LIST, NOTIFY_TYPE_ADD) ;
_should_reset_lobby_counts = false ;
}
@ -1782,6 +1801,11 @@ bool p3ChatService::loadList(std::list<RsItem*>& load)
_default_nick_name = kit->value ;
}
RsChatLobbyConfigItem *cas = NULL ;
if(NULL != (cas = dynamic_cast<RsChatLobbyConfigItem*>(*it)))
_known_lobbies_flags[cas->lobby_Id] = ChatLobbyFlags(cas->flags) ;
// delete unknown items
delete *it;
}
@ -1855,6 +1879,17 @@ bool p3ChatService::saveList(bool& cleanup, std::list<RsItem*>& list)
list.push_back(vitem) ;
/* Save Lobby Auto Subscribe */
for(std::map<ChatLobbyId,ChatLobbyFlags>::const_iterator it=_known_lobbies_flags.begin();it!=_known_lobbies_flags.end();++it)
{
RsChatLobbyConfigItem *cas = new RsChatLobbyConfigItem ;
cas->lobby_Id=it->first;
cas->flags=it->second.toUInt32();
list.push_back(cas) ;
}
return true;
}
@ -2747,6 +2782,25 @@ bool p3ChatService::setNickNameForChatLobby(const ChatLobbyId& lobby_id,const st
return true ;
}
void p3ChatService::setLobbyAutoSubscribe(const ChatLobbyId& lobby_id, const bool autoSubscribe)
{
if(autoSubscribe)
_known_lobbies_flags[lobby_id] |= RS_CHAT_LOBBY_FLAGS_AUTO_SUBSCRIBE ;
else
_known_lobbies_flags[lobby_id] &= ~RS_CHAT_LOBBY_FLAGS_AUTO_SUBSCRIBE ;
rsicontrol->getNotify().notifyListChange(NOTIFY_LIST_CHAT_LOBBY_LIST, NOTIFY_TYPE_ADD) ;
IndicateConfigChanged();
}
bool p3ChatService::getLobbyAutoSubscribe(const ChatLobbyId& lobby_id)
{
if(_known_lobbies_flags.find(lobby_id) == _known_lobbies_flags.end()) // keep safe about default values form std::map
return false;
return _known_lobbies_flags[lobby_id] & RS_CHAT_LOBBY_FLAGS_AUTO_SUBSCRIBE ;
}
void p3ChatService::cleanLobbyCaches()
{
#ifdef CHAT_DEBUG
@ -3127,7 +3181,7 @@ void p3ChatService::sendTurtleData(RsChatItem *item)
gitem->data_bytes = malloc(gitem->data_size) ;
memcpy(gitem->data_bytes ,&IV,8) ;
memcpy(gitem->data_bytes+8,encrypted_data,encrypted_size) ;
memcpy(& (((uint8_t*)gitem->data_bytes)[8]),encrypted_data,encrypted_size) ;
delete[] encrypted_data ;
delete item ;

View file

@ -169,6 +169,8 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor, publi
bool getNickNameForChatLobby(const ChatLobbyId& lobby_id,std::string& nick) ;
bool setDefaultNickNameForChatLobby(const std::string& nick) ;
bool getDefaultNickNameForChatLobby(std::string& nick) ;
void setLobbyAutoSubscribe(const ChatLobbyId& lobby_id, const bool autoSubscribe);
bool getLobbyAutoSubscribe(const ChatLobbyId& lobby_id);
void sendLobbyStatusString(const ChatLobbyId& id,const std::string& status_string) ;
ChatLobbyId createChatLobby(const std::string& lobby_name,const std::string& lobby_topic, const std::list<std::string>& invited_friends,uint32_t privacy_type) ;
@ -286,12 +288,18 @@ class p3ChatService: public p3Service, public p3Config, public pqiMonitor, publi
time_t last_connexion_challenge_time ;
time_t last_keep_alive_packet_time ;
std::set<std::string> previously_known_peers ;
uint32_t flags ;
};
std::map<ChatLobbyId,ChatLobbyEntry> _chat_lobbys ;
std::map<ChatLobbyId,ChatLobbyInvite> _lobby_invites_queue ;
std::map<ChatLobbyId,VisibleChatLobbyRecord> _visible_lobbies ;
std::map<std::string,ChatLobbyId> _lobby_ids ;
std::map<ChatLobbyId,ChatLobbyFlags> _known_lobbies_flags ; // flags for all lobbies, including the ones that are not known. So we can't
// store them in _chat_lobbies (subscribed lobbies) nor _visible_lobbies.
// Known flags:
// RS_CHAT_LOBBY_FLAGS_AUTO_SUBSCRIBE
std::string _default_nick_name ;
float _time_shift_average ;
time_t last_lobby_challenge_time ; // prevents bruteforce attack