]> jfr.im git - irc/SurrealServices/srsv.git/blame - branches/0.5.0/modules/serviceslibs/hostserv.pm
Fixed some more stuff.. No more getuuid for normal users!
[irc/SurrealServices/srsv.git] / branches / 0.5.0 / modules / serviceslibs / hostserv.pm
CommitLineData
aecfa1fd 1# This file is part of SurrealServices.
2#
3# SurrealServices is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# SurrealServices is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with SurrealServices; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16package hostserv;
17
18use strict;
19
20use SrSv::Text::Format qw(columnar);
21use SrSv::Errors;
22
23use SrSv::HostMask qw(parse_mask);
24
25use SrSv::User qw(get_user_nick get_user_id :flood);
26use SrSv::User::Notice;
27use SrSv::Help qw( sendhelp );
28
29use SrSv::NickReg::Flags qw(NRF_NOHIGHLIGHT nr_chk_flag_user);
30use SrSv::NickReg::User qw(is_identified);
026939ee 31use SrSv::IRCd::State qw($ircline synced initial_synced %IRCd_capabilities);
aecfa1fd 32use SrSv::MySQL '$dbh';
33use SrSv::MySQL::Glob;
34require SrSv::DB::StubGen;
35
36
37our $hsnick_default = 'HostServ';
38our $hsnick = $hsnick_default;
7b3a5814 39our $hsuser = { NICK => $hsnick, ID => ircd::getAgentUuid($hsnick) };
aecfa1fd 40sub init() {
1eb006d9 41$hsuser = { NICK => $hsnick, ID => ircd::getAgentUuid($hsnick) };
aecfa1fd 42import SrSv::DB::StubGen (
43 dbh => $dbh,
44 generator => 'services_mysql_stubgen',
45);
46
47services_mysql_stubgen(
48 [set_vhost => 'INSERT', "REPLACE INTO vhost SELECT id, ?, ?, ?, UNIX_TIMESTAMP() FROM nickreg WHERE nick=?"],
49 [get_vhost => 'ROW', "SELECT vhost.ident, vhost.vhost
50 FROM vhost, nickalias
51 WHERE nickalias.nrid=vhost.nrid AND nickalias.alias=?"],
52 [del_vhost => 'NULL', "DELETE FROM vhost USING vhost, nickreg WHERE nickreg.nick=? AND vhost.nrid=nickreg.id"],
53 [get_matching_vhosts => 'ARRAY', "SELECT nickreg.nick, vhost.ident, vhost.vhost, vhost.adder, vhost.time
54 FROM vhost JOIN nickreg ON (vhost.nrid=nickreg.id)
55 WHERE nickreg.nick LIKE ? AND vhost.ident LIKE ? AND vhost.vhost LIKE ?
56 ORDER BY nickreg.nick"],
57);
58}
59
60sub dispatch($$$) {
5e682044 61 my ($user, $dstUser, $msg) = @_;
62 my $src = $user->{NICK};
aecfa1fd 63 $msg =~ s/^\s+//;
64 my @args = split(/\s+/, $msg);
65 my $cmd = shift @args;
7b3a5814 66 $user->{AGENT} = $hsuser;
5e682044 67 get_user_id ($user);
aecfa1fd 68 return if flood_check($user);
5e682044 69 return unless (lc $dstUser->{NICK} eq lc $hsnick);
aecfa1fd 70 if(lc $cmd eq 'on') {
71 hs_on($user, $src, 0);
72 }
73 elsif(lc $cmd eq 'off') {
74 hs_off($user);
75 }
76 elsif($cmd =~ /^(add|set(host))?$/i) {
77 if (@args == 2) {
78 hs_sethost($user, @args);
79 }
80 else {
81 notice($user, 'Syntax: SETHOST <nick> <[ident@]vhost>');
82 }
83 }
84 elsif($cmd =~ /^del(ete)?$/i) {
85 if (@args == 1) {
86 hs_delhost($user, @args);
87 }
88 else {
89 notice($user, 'Syntax: DELETE <nick>');
90 }
91 }
92 elsif($cmd =~ /^list$/i) {
93 if (@args == 1) {
94 hs_list($user, @args);
95 }
96 else {
97 notice($user, 'Syntax: LIST <nick!vident@vhost>');
98 }
99 }
100 elsif($cmd =~ /^help$/i) {
101 sendhelp($user, 'hostserv', @args)
102 }
103 else { notice($user, "Unknown command."); }
104}
105
106sub hs_on($$;$) {
107 my ($user, $nick, $identify) = @_;
108 my $src = get_user_nick($user);
109
110 unless(nickserv::is_registered($nick)) {
111 notice($user, "Your nick, \002$nick\002, is not registered.");
112 return;
113 }
114
115 if(!$identify and !is_identified($user, $nick)) {
116 notice($user, "You are not identified to \002$nick\002.");
117 return;
118 }
026939ee 119 if ($IRCd_capabilities{"CHGHOST"} eq "" || $IRCd_capabilities{"CHGIDENT"} eq "" || $IRCd_capabilities{"CLOAKHOST"} eq "" || $IRCd_capabilities{"CLOAK"} eq "") {
120 notice ($user, "The IRCd is not properly configured to support vhosts. Please contact your friendly network administrators.");
121 notice ($user, "CHGHOST, CHGIDENT, CLOAKHOST and CLOAK need to be enabled for proper vhost support.");
122 return;
123 }
aecfa1fd 124 my ($vident, $vhost) = get_vhost($nick);
125 unless ($vhost) {
126 notice($user, "You don't have a vHost.") unless $identify;
127 return;
128 }
5e682044 129
aecfa1fd 130 if ($vident) {
026939ee 131 ircd::chgident($hsuser, $user, $vident);
aecfa1fd 132 }
026939ee 133 ircd::chghost($hsuser, $user, $vhost);
aecfa1fd 134
135 notice($user, "Your vHost has been changed to \002".($vident?"$vident\@":'')."$vhost\002");
136}
137
138sub hs_off($) {
139 my ($user) = @_;
140 my $src = get_user_nick($user);
026939ee 141 if (!$IRCd_capabilities{"CHGHOST"} || !$IRCd_capabilities{"CHGIDENT"} || !$IRCd_capabilities{"CLOAKHOST"} || !$IRCd_capabilities{"CLOAK"}) {
142 notice ($user, "The IRCd is not properly configured to support vhosts. Please contact your friendly network administrators.");
143 notice ($user, "CHGHOST, CHGIDENT, CLOAKHOST and CLOAK need to be enabled for proper vhost support.");
144 return;
145 }
aecfa1fd 146 # This requires a hack that is only known to work in UnrealIRCd 3.2.6 and later.
026939ee 147 # And insp!
148 ircd::reset_cloakhost($hsuser, $user);
aecfa1fd 149
150 notice($user, "vHost reset to cloakhost.");
151}
152
153sub hs_sethost($$$) {
154 my ($user, $target, $vhost) = @_;
155 unless(adminserv::is_svsop($user, adminserv::S_OPER())) {
156 notice($user, $err_deny);
157 return;
158 }
026939ee 159 if (!$IRCd_capabilities{"CHGHOST"} || !$IRCd_capabilities{"CHGIDENT"} || !$IRCd_capabilities{"CLOAKHOST"} || !$IRCd_capabilities{"CLOAK"}) {
160 notice ($user, "The IRCd is not properly configured to support vhosts. Please contact your friendly network administrators.");
161 notice ($user, "CHGHOST, CHGIDENT, CLOAKHOST and CLOAK need to be enabled for proper vhost support.");
162 return;
163 }
aecfa1fd 164 my $rootnick = nickserv::get_root_nick($target);
165
166 unless ($rootnick) {
167 notice($user, "\002$target\002 is not registered.");
168 return;
169 }
170
171 my $vident = '';
172 if($vhost =~ /\@/) {
173 ($vident, $vhost) = split(/\@/, $vhost);
174 }
175 my $src = get_user_nick($user);
176 set_vhost($vident, $vhost, $src, $rootnick);
177
178 notice($user, "vHost for \002$target ($rootnick)\002 set to \002".($vident?"$vident\@":'')."$vhost\002");
179}
180
181sub hs_delhost($$) {
182 my ($user, $target) = @_;
183 unless(adminserv::is_svsop($user, adminserv::S_OPER())) {
184 notice($user, $err_deny);
185 return;
186 }
187 my $rootnick = nickserv::get_root_nick($target);
188
189 unless ($rootnick) {
190 notice($user, "\002$target\002 is not registered.");
191 return;
192 }
193
194 del_vhost($rootnick);
195
196 notice($user, "vHost for \002$target ($rootnick)\002 deleted.");
197}
198
199sub hs_list($$) {
200 my ($user, $mask) = @_;
201
202 unless(adminserv::is_svsop($user, adminserv::S_HELP())) {
203 notice($user, $err_deny);
204 return;
205 }
206
207 my ($mnick, $mident, $mhost) = glob2sql(parse_mask($mask));
208
209 $mnick = '%' if($mnick eq '');
210 $mident = '%' if($mident eq '');
211 $mhost = '%' if($mhost eq '');
212
213 my @data;
214 foreach my $vhostEnt (get_matching_vhosts($mnick, $mident, $mhost)) {
215 my ($rnick, $vident, $vhost) = @$vhostEnt;
216 push @data, [$rnick, ($vident?"$vident\@":'').$vhost];
217 }
218
219 notice($user, columnar({TITLE => "vHost list matching \002$mask\002:",
220 NOHIGHLIGHT => nr_chk_flag_user($user, NRF_NOHIGHLIGHT)}, @data));
221}
222
223
224### MISCELLANEA ###
225
226
227
228## IRC EVENTS ##
229
2301;