From 6fde6b0fe90abde84011202edd40fe46eb06af44 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 21 Nov 2010 16:20:55 +0000 Subject: --- src/chat/user.cpp | 307 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 182 insertions(+), 125 deletions(-) (limited to 'src/chat/user.cpp') diff --git a/src/chat/user.cpp b/src/chat/user.cpp index 80f4c65..93dd672 100644 --- a/src/chat/user.cpp +++ b/src/chat/user.cpp @@ -1,12 +1,11 @@ /*:* *: File: ./src/chat/user.cpp *: - *: yChat; Homepage: ychat.buetow.org; Version 0.9.0-CURRENT + *: yChat; Homepage: www.yChat.org; Version 0.8.3-CURRENT *: *: Copyright (C) 2003 Paul C. Buetow, Volker Richter *: Copyright (C) 2004 Paul C. Buetow *: Copyright (C) 2005 EXA Digital Solutions GbR - *: Copyright (C) 2006, 2007 Paul C. Buetow *: *: This program is free software; you can redistribute it and/or *: modify it under the terms of the GNU General Public License @@ -31,8 +30,6 @@ using namespace std; -#include - user::user() : name( "" ) { initialize(); @@ -44,7 +41,27 @@ user::user( string s_name ) : name( s_name ) } user::~user() -{} +{ + // pthread_mutex_destroy( & mut_map_mods ); + pthread_mutex_destroy( & mut_away ); + pthread_mutex_destroy( & mut_b_online ); + pthread_mutex_destroy( & mut_b_fake ); + pthread_mutex_destroy( & mut_b_invisible ); + pthread_mutex_destroy( & mut_b_has_sess ); + pthread_mutex_destroy( & mut_b_is_reg ); + pthread_mutex_destroy( & mut_b_is_gag ); + pthread_mutex_destroy( & mut_s_mess ); + pthread_mutex_destroy( & mut_s_pass ); + pthread_mutex_destroy( & mut_p_room ); + pthread_mutex_destroy( & mut_s_col1 ); + pthread_mutex_destroy( & mut_s_col2 ); + pthread_mutex_destroy( & mut_s_email ); + pthread_mutex_destroy( & mut_s_tmpid ); + pthread_mutex_destroy( & mut_i_status ); + pthread_mutex_destroy( & mut_map_changed_data ); + pthread_cond_destroy ( &cond_message ); + pthread_mutex_destroy( &mut_message ); +} void user::initialize() @@ -54,10 +71,7 @@ user::initialize() init_ints(wrap::CONF->get_vector("chat.fields.userints")); init_bools(wrap::CONF->get_vector("chat.fields.userbools")); - this -> l_messages_recv = 0; - this -> p_sock = NULL; this -> p_room = NULL; - this -> s_msg = ""; this -> b_is_reg = false; this -> b_is_gag = false; this -> b_set_changed_data = false; @@ -67,6 +81,25 @@ user::initialize() this -> s_col1 = wrap::CONF->get_elem( "chat.html.user.color1" ); this -> s_col2 = wrap::CONF->get_elem( "chat.html.user.color2" ); + // pthread_mutex_init( & mut_map_mods , NULL ); + pthread_mutex_init( & mut_away , NULL ); + pthread_mutex_init( & mut_b_online , NULL ); + pthread_mutex_init( & mut_b_fake , NULL ); + pthread_mutex_init( & mut_b_invisible , NULL ); + pthread_mutex_init( & mut_b_has_sess , NULL ); + pthread_mutex_init( & mut_b_is_reg , NULL ); + pthread_mutex_init( & mut_b_is_gag , NULL ); + pthread_mutex_init( & mut_s_mess , NULL ); + pthread_mutex_init( & mut_s_pass , NULL ); + pthread_mutex_init( & mut_p_room , NULL ); + pthread_mutex_init( & mut_s_col1 , NULL ); + pthread_mutex_init( & mut_s_col2 , NULL ); + pthread_mutex_init( & mut_s_email , NULL ); + pthread_mutex_init( & mut_s_tmpid , NULL ); + pthread_mutex_init( & mut_i_status , NULL ); + pthread_mutex_init( & mut_map_changed_data , NULL ); + pthread_cond_init ( &cond_message, NULL); + pthread_mutex_init( &mut_message , NULL); renew_timeout(); } @@ -74,10 +107,9 @@ void user::clean() { destroy_session(); - set_fake(false); - set_invisible(false); - set_away(false, ""); - set_sock(NULL); + set_fake( false ); + set_invisible( false ); + set_away( false, "" ); } void @@ -89,8 +121,8 @@ user::destroy_session() #ifdef DATABASE // Store all changed data into the mysql table if this user is registered: if ( b_is_reg ) - wrap::DATA->update_user_data( get_name(), "savechangednick", - map_changed_data ); + wrap::DATA->update_user_data( get_name(), "savechangednick", + map_changed_data ); #endif set_has_sess(false); @@ -115,46 +147,78 @@ user::get_data( map *p_map_data ) (*p_map_data)[get_name()] = get_col1(); } +string +user::get_mess( ) +{ + pthread_mutex_lock ( &mut_s_mess ); + string s_ret = s_mess; + s_mess = ""; + pthread_mutex_unlock( &mut_s_mess ); + return s_ret; +} + bool user::get_online( ) { - return b_online; + bool b_ret; + pthread_mutex_lock ( &mut_b_online ); + b_ret = b_online; + pthread_mutex_unlock( &mut_b_online ); + return b_ret; } - bool user::get_fake( ) { - return b_fake; + bool b_ret; + pthread_mutex_lock ( &mut_b_fake ); + b_ret = b_fake; + pthread_mutex_unlock( &mut_b_fake ); + return b_ret; } - bool user::get_invisible( ) { - return b_invisible; + bool b_ret; + pthread_mutex_lock ( &mut_b_invisible ); + b_ret = b_invisible; + pthread_mutex_unlock( &mut_b_invisible ); + return b_ret; } bool user::get_has_sess( ) { - return b_has_sess; + bool b_ret; + pthread_mutex_lock ( &mut_b_has_sess ); + b_ret = b_has_sess; + pthread_mutex_unlock( &mut_b_has_sess ); + return b_ret; } void -user::set_has_sess(bool b_has_sess) +user::set_has_sess( bool b_has_sess ) { + pthread_mutex_lock ( &mut_b_has_sess ); this->b_has_sess = b_has_sess; + pthread_mutex_unlock( &mut_b_has_sess ); } bool user::get_is_reg( ) { - return b_is_reg; + bool b_ret; + b_ret = b_is_reg; + return b_ret; } bool user::get_is_gag( ) { - return b_is_gag; + bool b_ret; + pthread_mutex_lock ( &mut_b_is_gag ); + b_ret = b_is_gag; + pthread_mutex_unlock( &mut_b_is_gag ); + return b_ret; } void @@ -166,60 +230,42 @@ user::set_is_reg( bool b_is_reg ) void user::set_is_gag( bool b_is_gag ) { + pthread_mutex_lock ( &mut_b_is_gag ); this -> b_is_gag = b_is_gag; + pthread_mutex_unlock( &mut_b_is_gag ); } void user::set_online( bool b_online ) { - if (this->b_online == b_online) - return; - + pthread_mutex_lock ( &mut_b_online ); this -> b_online = b_online; - if (!b_online) - { - cout << "SETTING OFFLINE" << endl; - // remove the user from its room. - string s_user(get_name()); - string s_user_lowercase(get_lowercase_name()); - - room* p_room = get_room(); - p_room->del_elem(s_user_lowercase); - - // post the room that the user has left the chat. - string s_msg = wrap::TIMR->get_time() + " " - + get_colored_bold_name() - + wrap::CONF->get_elem( "chat.msgs.userleaveschat" ) - + "
\n"; - - p_room->msg_post( &s_msg ); - p_room->reload_onlineframe(); - -#ifdef VERBOSE - cout << REMUSER << s_user << endl; -#endif - - wrap::GCOL->add_user_to_garbage(this); - } + pthread_mutex_unlock( &mut_b_online ); } void user::set_fake( bool b_fake ) { + pthread_mutex_lock ( &mut_b_fake ); this -> b_fake = b_fake; + pthread_mutex_unlock( &mut_b_fake ); } void user::set_invisible( bool b_invisible ) { + pthread_mutex_lock ( &mut_b_invisible ); this -> b_invisible = b_invisible; + pthread_mutex_unlock( &mut_b_invisible ); } bool user::get_away() { bool b_ret; + pthread_mutex_lock ( &mut_away ); b_ret = b_away; + pthread_mutex_unlock( &mut_away ); return b_ret; } @@ -227,35 +273,45 @@ string user::get_away_msg() { string s_ret; + pthread_mutex_lock ( &mut_away ); s_ret = s_away; + pthread_mutex_unlock( &mut_away ); return s_ret; } void user::set_away( bool b_away, string s_away ) { + pthread_mutex_lock ( &mut_away ); this -> b_away = b_away; this -> s_away = s_away; + pthread_mutex_unlock( &mut_away ); } void user::set_away( bool b_away ) { + pthread_mutex_lock ( &mut_away ); this -> b_away = b_away; + pthread_mutex_unlock( &mut_away ); } room* user::get_room( ) { room* p_return; + pthread_mutex_lock ( &mut_p_room ); p_return = p_room; + pthread_mutex_unlock( &mut_p_room ); return p_return; } void user::set_p_room( room* p_room ) { + pthread_mutex_lock ( &mut_p_room ); this -> p_room = p_room; + pthread_mutex_unlock( &mut_p_room ); } /* @@ -270,7 +326,9 @@ string user::get_pass() { string s_ret; + pthread_mutex_lock ( &mut_s_pass ); s_ret = s_pass; + pthread_mutex_unlock( &mut_s_pass ); return s_ret; } @@ -278,7 +336,9 @@ string user::get_col1() { string s_ret; + pthread_mutex_lock ( &mut_s_col1 ); s_ret = s_col1; + pthread_mutex_unlock( &mut_s_col1 ); return s_ret; } @@ -286,7 +346,9 @@ string user::get_col2() { string s_ret; + pthread_mutex_lock ( &mut_s_col2 ); s_ret = s_col2; + pthread_mutex_unlock( &mut_s_col2 ); return s_ret; } @@ -294,7 +356,9 @@ string user::get_email() { string s_ret; + pthread_mutex_lock ( &mut_s_email ); s_ret = s_email; + pthread_mutex_unlock( &mut_s_email ); return s_ret; } @@ -302,49 +366,63 @@ string user::get_tmpid() { string s_ret; + pthread_mutex_lock ( &mut_s_tmpid ); s_ret = s_tmpid; + pthread_mutex_unlock( &mut_s_tmpid ); return s_ret; } void user::set_tmpid ( string s_tmpid ) { + pthread_mutex_lock ( &mut_s_tmpid ); this -> s_tmpid = s_tmpid; + pthread_mutex_unlock( &mut_s_tmpid ); } void user::set_pass ( string s_pass ) { set_changed_data( "password", s_pass ); + pthread_mutex_lock ( &mut_s_pass ); this -> s_pass = s_pass; + pthread_mutex_unlock( &mut_s_pass ); } void user::set_col1 ( string s_col1 ) { set_changed_data( "color1", s_col1 ); + pthread_mutex_lock ( &mut_s_col1 ); this -> s_col1 = s_col1; + pthread_mutex_unlock( &mut_s_col1 ); } void user::set_col2 ( string s_col2 ) { set_changed_data( "color2", s_col2 ); + pthread_mutex_lock ( &mut_s_col2 ); this -> s_col2 = s_col2; + pthread_mutex_unlock( &mut_s_col2 ); } void user::set_email ( string s_email ) { set_changed_data( "email", s_email ); + pthread_mutex_lock ( &mut_s_email ); this -> s_email = s_email; + pthread_mutex_unlock( &mut_s_email ); } int user::get_status ( ) { int r_ret; + pthread_mutex_lock ( &mut_i_status ); r_ret = i_status; + pthread_mutex_unlock(&mut_i_status ); return r_ret; } @@ -352,8 +430,10 @@ void user::set_status ( int i_status ) { set_changed_data( "status", tool::int2string(i_status)); + pthread_mutex_lock ( &mut_i_status ); i_old_status = this -> i_status; this -> i_status = i_status; + pthread_mutex_unlock( &mut_i_status ); } void @@ -361,18 +441,28 @@ user::set_changed_data( string s_varname, string s_value ) { if ( b_set_changed_data ) { + pthread_mutex_lock ( &mut_map_changed_data ); map_changed_data[s_varname] = s_value; + pthread_mutex_unlock( &mut_map_changed_data ); } } +bool +user::new_msgs ( ) +{ + pthread_mutex_lock ( &mut_s_mess ); + return s_mess.empty(); + pthread_mutex_unlock( &mut_s_mess ); +} + void user::command( string &s_command ) { check_restore_away(); - unsigned long pos = s_command.find( "/" ); - unsigned long pos2 = s_command.find( " " ); - if ( pos != (unsigned long) string::npos ) + unsigned pos = s_command.find( "/" ); + unsigned pos2 = s_command.find( " " ); + if( pos != string::npos ) { s_command.replace( pos, 1, "" ); } @@ -434,88 +524,57 @@ user::command( string &s_command ) delete c; } + void -user::set_sock(_socket* p_sock) +user::s_mess_delete( ) { - this->p_sock = p_sock; + pthread_mutex_lock ( &mut_s_mess ); + s_mess = ""; + pthread_mutex_unlock( &mut_s_mess ); } + void user::msg_post( string *p_msg ) { - /* - ++l_messages_recv; - if (p_sock == NULL) { - s_msg.append(*p_msg); - return; - - } - else if (!s_msg.empty()) - { - wrap::SOCK->_send(p_sock, s_msg.c_str(), s_msg.size() ); - s_msg.clear(); - } + pthread_mutex_lock ( &mut_s_mess ); + s_mess.append( *p_msg ); + pthread_mutex_unlock( &mut_s_mess ); - if ( 0 > wrap::SOCK->_send(p_sock, p_msg->c_str(), p_msg->size() ) ) { - cout << "psock: " << (int) p_sock << endl; - set_online( false ); - */ + pthread_cond_signal( &cond_message ); } -/* - void - sock::handle_client_write(int i_fd, short event, void *p_arg) { - static int i_char_size = sizeof(char); - - context *p_context = static_cast(p_arg); - string *p_response = p_context->p_response; - - if (-1 == write(i_fd, p_response->c_str(), p_response->length()*i_char_size)) - { - switch (errno) { - case EAGAIN: - case EINTR: - event_add(p_context->p_event, NULL); - return; - } - } - - delete p_context; - } -*/ void user::post_action_msg(string s_msgkey) { - get_room()->msg_post(wrap::TIMR->get_time()+" "+get_colored_bold_name()+wrap::CONF->get_elem(s_msgkey)+"
\n"); + get_room()->msg_post(wrap::TIMR->get_time()+" "+get_colored_bold_name()+wrap::CONF->get_elem(s_msgkey)+"
\n"); } void -user::renew_timeout() +user::renew_timeout() { timo::renew_timeout(); double d_time_diff = wrap::TIMR->get_time_diff(t_flood_time); - if (d_time_diff < static_cast(wrap::CONF->get_int("chat.floodprotection.seconds")) - ) + if (d_time_diff < static_cast(wrap::CONF->get_int("chat.floodprotection.seconds"))) { - if (++i_flood_messages > static_cast(wrap::CONF->get_int("chat.floodprotection.messages")) - ) - { - room* p_room = get_room(); - if (p_room == 0) - { - i_flood_messages = 0; - return; - } - - wrap::system_message(CHATFLO+get_name()+","+p_room->get_name()+","+tool::int2string(i_flood_messages)+")"); - msg_post(wrap::CONF->colored_error_msg("chat.msgs.err.flooding")); - if (!get_is_gag()) - { - set_is_gag(true); - post_action_msg("chat.msgs.floodgag"); - } - } + if (++i_flood_messages > static_cast(wrap::CONF->get_int("chat.floodprotection.messages"))) + { + room* p_room = get_room(); + if (p_room == 0) + { + i_flood_messages = 0; + return; + } + + wrap::system_message(CHATFLO+get_name()+","+p_room->get_name()+","+tool::int2string(i_flood_messages)+")"); + msg_post(wrap::CONF->colored_error_msg("chat.msgs.err.flooding")); + if (!get_is_gag()) + { + set_is_gag(true); + post_action_msg("chat.msgs.floodgag"); + } + } } else @@ -523,7 +582,7 @@ user::renew_timeout() time(&t_flood_time); i_flood_messages = 0; } -} +} void user::check_timeout( int* i_idle_timeout ) @@ -538,6 +597,7 @@ user::check_timeout( int* i_idle_timeout ) msg_post( &s_quit ); set_online( false ); + pthread_cond_signal( &cond_message ); } else if ( ! get_away() && i_idle_timeout[2] <= d_user_timeout ) { @@ -624,21 +684,18 @@ user::reconf() void user::dumpit() { - dumpable::add - ("[user]"); - dumpable::add - ("Name: " + get_name() + - "; Room: " + get_room()->get_name() + - "; Status: " + tool::int2string(get_status())); - dumpable::add - ("TempID: " + get_tmpid()); + dumpable::add("[user]"); + dumpable::add("Name: " + get_name() + + "; Room: " + get_room()->get_name() + + "; Status: " + tool::int2string(get_status())); + dumpable::add("TempID: " + get_tmpid()); } bool user::same_rooms(user *p_user) { return p_user->get_room()->get_lowercase_name() - .compare(this->get_room()->get_lowercase_name()) == 0; + .compare(this->get_room()->get_lowercase_name()) == 0; } string -- cgit v1.2.3