-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDCBUser.pm
executable file
·214 lines (184 loc) · 6.02 KB
/
DCBUser.pm
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package DCBUser;
use strict;
use warnings;
use DCBSettings;
use DCBDatabase;
use Data::Dumper;
use Exporter;
our @ISA= qw(Exporter);
our @EXPORT = qw(user_init user_load user_load_by_name user_load_by_mail user_update user_check_errors user_access user_permissions user_permissions_inherit user_connect user_disconnect user_is_admin PERMISSIONS);
use constant PERMISSIONS => {
OFFLINE => 0,
KEY_NOT_SENT => 1,
KEY_SENT => 2,
ANONYMOUS => 4,
AUTHENTICATED => 8,
OPERATOR => 16,
ADMINISTRATOR => 32,
TELNET => 64,
};
sub new { bless {}, shift }
sub user_init() {
my @user_list = @_;
my @online = ();
foreach (split(/\s+/, $user_list[1])) {
push(@online, $_);
}
user_sanitize_disconnects(\@online);
our $userlist = ();
my $userh = DCBDatabase::db_select('users');
while (my $user = $userh->fetchrow_hashref()) {
$userlist->{lc($user->{name})} = $user;
}
}
# Whenever we're dealing with logins or logouts we need the user data from the database.
sub user_load_by_mail($) {
my $mail = shift;
my %where = ('mail' => { -like => [$mail] });
my @fields = ('*');
my $userh = DCBDatabase::db_select('users', \@fields, \%where);
# Send back the row as a ref hash array.
# ie username is $user->{'name'}
my $user = $userh->fetchrow_hashref();
# If the user is new we must add their name to the returned hash
$user->{'mail'} = $user->{'mail'} ? $user->{'mail'} : $mail;
return $user;
}
# Whenever we're dealing with logins or logouts we need the user data from the database.
sub user_load_by_name($) {
my $name = shift;
my %where = ('name' => { -like => [$name] });
my @fields = ('*');
my $userh = DCBDatabase::db_select('users', \@fields, \%where);
# Send back the row as a ref hash array.
# ie username is $user->{'name'}
my $user = $userh->fetchrow_hashref();
# If the user is new we must add their name to the returned hash
$user->{'name'} = $user->{'name'} ? $user->{'name'} : $name;
return $user;
}
sub user_load($) {
my $uid = shift;
my %where = ('uid' => $uid);
my @fields = ('*');
my $userh = DCBDatabase::db_select('users', \@fields, \%where);
# Send back the row as a ref hash array.
# ie username is $user->{'name'}
my $user = $userh->fetchrow_hashref();
return $user;
}
sub user_connect($) {
my $user = shift;
my %fields = (
'ip' => $user->{ip},
'permission' => $user->{permission},
'connect_share' => $user->{connect_share},
'connect_time' => $user->{connect_time},
'client' => $user->{client},
);
if ($user->{new}) {
$fields{'name'} = $user->{name},
$fields{'mail'} = $user->{mail},
$fields{'join_time'} = $user->{join_time};
$fields{'join_share'} = $user->{join_share};
$fields{'disconnect_time'} = 0;
DCBDatabase::db_insert('users', \%fields);
# We need to get uid of new user for commands
my @fields = ( 'uid' );
my %where = ( 'name' => $fields{'name'} );
my $uidh = DCBDatabase::db_select('users', \@fields, \%where, \(), 1);
$user->{uid} = $uidh->fetchrow_array();
}
else {
my %where = ('uid' => $user->{uid});
DCBDatabase::db_update('users', \%fields, \%where);
}
}
sub user_update($ $) {
my $user = shift;
my $fields = shift;
my %where = ( 'uid' => $user->{'uid'} );
DCBDatabase::db_update('users', $fields, \%where);
}
sub user_disconnect($) {
my $user = shift;
my %fields = (
'disconnect_time' => $user->{disconnect_time},
);
my %where = ('uid' => $user->{uid});
DCBDatabase::db_update('users', \%fields, \%where);
}
sub user_sanitize_disconnects {
my $online = shift;
my %fields = ('disconnect_time' => time());
my %where = ( name => { -not_in => $online } );
DCBDatabase::db_update('users', \%fields, \%where);
}
sub user_permissions( @ ) {
my (@permissions) = @_;
my $return = 0;
# Convert strings of permission names to their integer
my @bits = map { PERMISSIONS->{$_} } @permissions;
for (my $i = 0; $i < scalar(@bits); $i++) {
$return |= $bits[$i];
}
return $return;
}
sub user_permissions_inherit() {
my $permission = shift;
# Takes a permission and makes it so any underneath inherit permissions
return ~($permission << 1);
}
sub user_access {
my $user = shift;
my $permission = shift;
return ($user->{permission} & $permission) ? 1 : 0;
}
sub user_check_errors($) {
my $user = shift;
my @errors = ();
if ($user->{'permission'} <= 4 && !$DCBSettings::config->{allow_anon}) {
push(@errors, "Registered users only");
}
my @name_errors = user_invalid_name($user->{'name'});
if (@name_errors && $user->{'new'}) {
push(@errors, "Your username is invalid, please change it.");
push(@errors, @name_errors);
}
if ($DCBSettings::config->{minshare} > $user->{'connect_share'}) {
push(@errors, "Your share is currently under the minimum share. The minimum share is currently " . DCBCommon::common_format_size($DCBSettings::config->{minshare}));
}
if (!$DCBSettings::config->{allow_external} && $user->{'ip'} !~ 127.0.0.1) {
push(@errors, "External users are not currently accepted.");
}
if (!$DCBSettings::config->{allow_passive} && $user->{'client'} =~ /M:P,H/) {
push(@errors, "Passive users are not currently accepted.");
}
# If we're dealing with an op/op-admin then disregard all errors
if (user_is_admin($user)) {
@errors = ();
}
return @errors;
}
sub user_invalid_name($) {
my $name = shift;
my @errors = ();
if (length($name) > $DCBSettings::config->{username_max_length}) {
push(@errors, "Name length exceeds maximum of $DCBSettings::config->{username_max_length}");
}
if (($name !~ /[\w-]+/) || ($name =~ /[\\\/]/)) {
push(@errors, "Name contains illegal characters. Letters, numbers, underscores and hyphens only.");
}
if (lc($name) eq lc($DCBSettings::config->{botname}) || lc($name) eq lc($DCBSettings::config->{username_anonymous})) {
push(@errors, "An illegal name has been chosen please use another.");
}
return @errors;
}
sub user_is_admin {
my $user = shift;
if ($user->{'permission'} >= 16) {
return 1;
}
return 0;
}
1;