summaryrefslogtreecommitdiff
path: root/ychat-0.1/chat.cpp
blob: 301739e7a8be42cde779af1ef847857df9e7c29f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// class chat implementation.

#ifndef CHAT_CXX
#define CHAT_CXX

#include "chat.h"
#include "CONF.h"
#include "MUTX.h"

using namespace std;

chat::chat( )
{
#ifdef VERBOSE
 cout << "chat::chat()" << endl;
#endif

}

chat::~chat( )
{
#ifdef VERBOSE
 cout << "chat::~chat()" << endl;
#endif
}

user*
chat::get_user( string &s_user )
{
 bool b_flag;
 return get_user( s_user, b_flag );
}

user*
chat::get_user( string &s_user, bool &b_found )
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "chat::get_user( " << s_user << ", bool& )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 container param;

 param.elem[0] = (void*) &s_user ;
 param.elem[1] = (void*) &b_found;

 b_found = false;

 run_func( get_user_, (void*)&param ); 

 if ( *( (bool*)param.elem[1] ) )
  return (user*)param.elem[2];
}

void
chat::get_user_( name *name_obj, void *v_arg )
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "chat::get_user_( name *name_obj, void *v_arg )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
 container* param = (container*) v_arg;
 room *room_obj = static_cast<room*>(name_obj); 
 param->elem[2] = (void*)room_obj->get_elem( *((string*)param->elem[0]), *((bool*)param->elem[1]) ); 
}

void
chat::login( map_string &map_params )
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "chat::login( map_params )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 string s_user = map_params["nick"];

 // prove if nick is empty 
 if ( s_user.empty() )
 {
  map_params["INFO"]    = E_NONICK; 
  map_params["request"] = CONF::get().get_val( "STARTMPL" ); // redirect to the startpage.
  return;
 }

 bool b_flag;

 // prove if nick is already online / logged in.
 get_user( s_user, b_flag );
 if ( b_flag ) 
 {
  map_params["INFO"]    = E_ONLINE;
  map_params["request"] = CONF::get().get_val( "STARTMPL" );
  return;
 } 

 string s_room = map_params["room"]; 
 room*  p_room = get_room( s_room , b_flag );

 // if room does not exist add room to list!
 if ( ! b_flag )
 {
  p_room = new room( s_room );

#ifdef _VERBOSE
  pthread_mutex_lock  ( &MUTX::get().mut_stdout );
  cout << NEWROOM << s_room << endl;
  pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
  
  add_elem( p_room );
 }

 user *p_user = new user( s_user );

 // add user to the room.
 p_room->add_user( p_user );

 // post "username enters the chat" into the room.
 p_room->msg_post( new string( s_user.append( USERENTR ) ) );  

#ifdef _VERBOSE
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << LOGINPR << s_user << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif
}

void 
chat::post( user* u_user, map_string &map_params )
{
#ifdef VERBOSE_
 pthread_mutex_lock  ( &MUTX::get().mut_stdout );
 cout << "chat::post( user* u_user, map_string &map_params )" << endl;
 pthread_mutex_unlock( &MUTX::get().mut_stdout );
#endif

 string s_msg( "<font color=\""   );
 s_msg.append( u_user->get_col1() )
      .append( "\">"              )
      .append( u_user->get_name() ) 
      .append( ": "               )
      .append( map_params["message"] )
      .append( "</font><br>\n"    );

 u_user->get_p_room()->msg_post( &s_msg ); 
}

#endif