This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainframe.cpp
164 lines (110 loc) · 2.83 KB
/
mainframe.cpp
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
#include "mainframe.h"
Mainframe *Mainframe::mInstance = NULL;
Mainframe* Mainframe::instance()
{
// If Mainframe has never been constructed, let's do it ONCE
if(!mInstance)
mInstance = new Mainframe();
return mInstance;
}
Mainframe::Mainframe()
{
}
Mainframe::~Mainframe()
{
// Properly remove all channels
removeAllChannels();
}
void Mainframe::start()
{
// Déclaration du service principal
boost::asio::io_service ios;
Server server(ios);
server.start();
ios.run();
}
/////////////////////////////////////////////////////////////////////////
// USERS //
/////////////////////////////////////////////////////////////////////////
bool Mainframe::doesNicknameExists(const std::string& nick)
{
return ((mUsers.find(nick)) != mUsers.end());
}
bool Mainframe::addUser(User* user)
{
if(doesNicknameExists(user->nick())) return false;
mUsers[user->nick()] = user;
return true;
}
bool Mainframe::changeNickname(const std::string& old, const std::string& recent)
{
if(doesNicknameExists(recent)) return false;
if(!doesNicknameExists(old)) return false;
User* tmp = mUsers[old];
mUsers.erase(old);
mUsers[recent] = tmp;
return true;
}
void Mainframe::removeUser(const std::string& nick)
{
mUsers.erase(nick);
}
User* Mainframe::getUserByName(const std::string& nick)
{
if(! doesNicknameExists(nick) ) return NULL;
return mUsers[nick];
}
/////////////////////////////////////////////////////////////////////////
// CHANNELS //
/////////////////////////////////////////////////////////////////////////
bool Mainframe::doesChannelExists(const std::string& name)
{
ChannelMap::iterator it = mChannels.begin();
return ((mChannels.find(name)) != mChannels.end());
}
void Mainframe::addChannel(Channel* chan)
{
std::string channame = chan->name();
if(!doesChannelExists(channame))
{
mChannels[channame] = chan;
}
}
void Mainframe::removeChannel(const std::string& name)
{
mChannels.erase(name);
}
Channel* Mainframe::getChannelByName(const std::string& name)
{
if(!doesChannelExists(name)) return NULL;
return mChannels[name];
}
void Mainframe::removeAllChannels()
{
ChannelMap::iterator it = mChannels.begin();
for(; it != mChannels.end(); ++it)
{
delete (it->second);
}
}
void Mainframe::updateChannels()
{
ChannelMap::iterator it = mChannels.begin();
while(it != mChannels.end())
{
if(it->second->empty())
{
delete (it->second);
mChannels.erase(it);
it++;
}
else
{
it++;
}
}
}
ChannelMap Mainframe::channels() const
{
return mChannels;
}