-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIRC_Service.cpp
More file actions
168 lines (126 loc) · 4.01 KB
/
IRC_Service.cpp
File metadata and controls
168 lines (126 loc) · 4.01 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "IRC_Service.h"
IRC_Service::IRC_Service(std::string path, IRC_Server* link)
: IRC_Plugin(path, link, true)
{
if (!is_valid())
return;
this->link = link;
void* handle = get_plugin_handle();
void* mhf_handle = dlsym(handle, "parse_message");
if (mhf_handle == NULL)
{
valid = false;
return;
}
mhf = (message_handler_function)mhf_handle;
user_link = new IRC_Server::User;
user_link->nick = "Service";
user_link->username = "services";
user_link->realname = "Service";
user_link->hostname = link->get_hostname();
user_link->client = -1;
user_link->ping_timer = -1;
user_link->ping_contents = "";
user_link->isService = true;
link->lock_message_mutex();
std::vector<IRC_Server::User*>* services = link->get_services();
services->push_back(user_link);
link->unlock_message_mutex();
((service_init)init)(link, this);
}
IRC_Service::~IRC_Service()
{}
std::vector<IRC_Plugin::Call_Type> IRC_Service::get_supported_calls()
{
if (!valid)
return std::vector<Call_Type>();
std::vector<IRC_Plugin::Call_Type> calls;
calls.push_back(IRC_Plugin::ON_RECIEVE_MESSAGE);
return calls;
}
IRC_Service::Result_Of_Call IRC_Service::plugin_call(Call_Type type, IRC_Server::User* user, std::vector<std::string> &parts)
{
using namespace std;
if (!valid)
return IRC_Plugin::FAILURE;
if (type != IRC_Plugin::ON_RECIEVE_MESSAGE)
return IRC_Plugin::NOT_HANDLED;
Service_Result result = mhf(user, parts, link, this);
std::string final_result = ":" + get_nick() + "!" + user_link->username + "@" + link->get_hostname() + " ";
if (result.type == IRC_Service::NOT_HANDLED)
return IRC_Plugin::NOT_HANDLED;
else if (result.type == IRC_Service::NO_RETURN)
return IRC_Plugin::HANDLED;
else if (result.type == IRC_Service::NOTICE)
final_result += "NOTICE ";
else if (result.type == IRC_Service::PRIVMSG)
final_result += "PRIVMSG ";
else
{
cerr << "Service Failure (" << get_name_of_plugin() << ") (Nick: " << get_nick() << "):" << "Invalid Message Type: " << type << "." << endl;
return IRC_Plugin::FAILURE;
}
if (result.user_or_channel.size() == 0)
{
cerr << "Service Failure (" << get_name_of_plugin() << ") (Nick: " << get_nick() << "):" << "User or Channel Size is 0 (zero)." << endl;
return IRC_Plugin::FAILURE;
}
final_result += result.user_or_channel;
if (result.message.size() == 0)
{
cerr << "Service Failure (" << get_name_of_plugin() << ") (Nick: " << get_nick() << "):" << "Message returned is 0 (zero)." << endl;
return FAILURE;
}
final_result += " :";
final_result += result.message;
final_result += IRC_Server::irc_ending;
if (result.user_or_channel.find_first_of("#&", 0) == 0)
{
IRC_Server::Channel* channel_to_send_to = link->get_channel_from_string(result.user_or_channel);
if (channel_to_send_to != NULL)
link->broadcast_message(final_result, channel_to_send_to);
else
{
cerr << "Service Failure (" << get_name_of_plugin() << ") (Nick: " << get_nick() << "):" << "No such channel called: '" << result.user_or_channel << endl;
return FAILURE;
}
}
else
{
IRC_Server::User* user_to_send_to = link->get_user_from_string(result.user_or_channel);
if (user_to_send_to != NULL)
link->send_message(final_result, user_to_send_to);
else
{
cerr << "Service Failure (" << get_name_of_plugin() << ") (Nick: " << get_nick() << "):" << "No such user called: '" << result.user_or_channel << endl;
return FAILURE;
}
}
return HANDLED;
}
std::string IRC_Service::get_name_of_plugin(bool absolute)
{
if (!valid)
return "FAILURE";
return "Service (" + this->IRC_Plugin::get_name_of_plugin(true) + ")";
}
std::string IRC_Service::get_nick()
{
return user_link->nick;
}
void IRC_Service::set_nick(std::string nick)
{
link->lock_message_mutex();
user_link->nick = nick;
link->unlock_message_mutex();
}
std::string IRC_Service::get_realname()
{
return user_link->realname;
}
void IRC_Service::set_real_name(std::string realname)
{
link->lock_message_mutex();
user_link->realname = realname;
link->unlock_message_mutex();
}