-
Notifications
You must be signed in to change notification settings - Fork 1
/
sirc_channel.cpp
105 lines (92 loc) · 3.06 KB
/
sirc_channel.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
/*
*
* Copyright (C) 2009 Daniel J. Calandria Hernández &
* Antonio Cañas Vargas
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sirc.h"
using namespace std;
ssize_t IrcChannel::printf ( const char *fmt, ... )
{
va_list list;
users_map_t::iterator it;
for (it = users.begin(); it != users.end(); ++it)
{
va_start( list, fmt );
it->second->vprintf ( fmt, list );
va_end( list );
}
return S_OK;
}
bool IrcChannel::IsOperator ( IrcUser *usr )
{
users_map_t::iterator it;
for (it = operators.begin(); it != operators.end(); ++it )
if ( usr == it->second)
return true;
return false;
}
void IrcChannel::PartUser ( IrcUser *user, const std::string& part_msg )
{
printf(":%s!%s@%s PART %s :%s\r\n", user->GetNick().c_str(), user->GetNick().c_str(),
user->GetHostName().c_str(), name.c_str(), part_msg.c_str() );
GetUsers().erase ( user );
GetOperators().erase ( user );
}
ssize_t IrcChannel::SendNamesList ( IrcUser *user )
{
//IrcUser *src_user;
users_map_t::iterator it;
string nick_list = "";
for (it = users.begin(); it != users.end(); ++it)
{
string nick = "";
if ( IsOperator ( it->second ) )
nick = "@";
nick = nick + it->second->GetNick();
nick_list += nick + " ";
if ( nick_list.length() > 100 )
{
user->printf(":%s 353 %s = %s :%s\r\n", user->GetServerName().c_str(), user->GetNick().c_str(),
name.c_str(), nick_list.c_str() );
nick_list = "";
}
}
if (nick_list.length())
user->printf(":%s 353 %s = %s :%s\r\n", user->GetServerName().c_str(), user->GetNick().c_str(),
name.c_str(), nick_list.c_str() );
user->printf(":%s 366 %s %s :End of NAMES list\r\n", user->GetServerName().c_str(), user->GetNick().c_str(), name.c_str());
return S_OK;
}
ssize_t IrcChannel::SendMessage ( IrcUser *user, const string& message )
{
IrcUser *dst_user;
users_map_t::iterator it;
for (it = users.begin(); it != users.end(); ++it)
{
dst_user = it->second;
if (dst_user != user)
{
dst_user->printf (":%s!%s@%s PRIVMSG %s :%s\r\n", user->GetNick().c_str(), user->GetNick().c_str(),
user->GetHostName().c_str(), name.c_str(), message.c_str() );
}
}
return S_OK;
}
ssize_t IrcChannel::SendTopic ( IrcUser *user )
{
user->printf ( ":%s 332 %s %s :%s\r\n", user->GetServerName().c_str(), user->GetNick().c_str(), name.c_str (), topic.c_str() );
return S_OK;
}