summaryrefslogtreecommitdiff
path: root/util/globsterctl
blob: 22d967da97a45172e5ee0b5b221420f029e64df8 (plain)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/perl

use strict;
use warnings;

eval { require Net::DBus; 1; }
	or die "The Net::DBus perl module does not seem to be installed on your machine.\nPlease install it if you wish to use this program.\n";

my($g, $hubm); # /net/blicky/Globster and _/HubManager objects


sub gethub {
	my $id = shift;
	my $h = $g->get_child_object("/Hub/$id");
	die "No hub object with id=$id.\n" if !eval { $h->Ping; 1 };
	$h;
}


sub formatsize {
	my $r = shift;
	for (qw|B KiB MiB GiB TiB PiB EiB ZiB YiB|) {
		return sprintf "%.2f %s", $r, $_ if $r < 999;
		$r /= 1024;
	}
}


my @userfields = ('', qw|nick description client mail slots sharedfiles sharesize conn hubs ip4 ip6 as cid flags|);
my %userfields = map +($userfields[$_], $_), 1..$#userfields;


sub userstruct2hash {
	my($fields, $struct) = @_;
	+{ map +($userfields[$fields->[$_]], $struct->[$_]), 0..$#{$struct} };
}


sub usertag { # Doesn't cover all fields and doesn't handle empty info very nicely.
	my $i = shift;
	$i->{hubs}||=[0,0,0];
	sprintf '<%s,M:%s,H:%d/%d/%d,S:%d>', $i->{client}, $i->{flags}&16?'A':'P', map($i->{hubs}[$_], 0..2), $i->{slots};
}


sub propset {
	my($obj, $prop, $val) = @_;

	# Net::DBus doesn't do type inference for properties, so let's do it ourself.
	my $ins = Net::DBus::Binding::Introspector->new(object_path => $obj->get_object_path(), xml => $obj->Introspect());
	my $itf = [$ins->has_property($prop)]->[0];
	die "No property by the name '$prop'.\n" if !$itf;

	# XXX: %simple_type_map is undocumented. No idea on which Net::DBus versions this works.
	no warnings 'once';
	$obj->Set('', $prop, Net::DBus::Binding::Value->new($Net::DBus::Binding::Introspector::simple_type_map{ $ins->get_property_type($itf, $prop) }, $val));
}


my %cmds;
my @cmds;
my $busarg = ($ARGV[0]||'') eq '--system' ? ' '.shift : ($ARGV[0]||'') eq '--session' ? shift&&'' : '';
my $cmd = shift||'';
sub cmd {
	my @c = @_;
	$cmds{$_[0]} = \@c;
	push @cmds, \@c;
}

sub usage { die "Usage: $0$busarg $cmd $cmds{$cmd}[1]\n"; }


sub help {
	print "$0 [--session|--system] <command> <args>\n\n";
	print "Available commands:\n";
	print "  $_->[0] $_->[1]\n" for (@cmds);
};


cmd version => '' => sub {
	printf "Globster-daemon %s (%s)\n", map $g->Get('', $_), qw|VersionString VersionNumber|;
};

cmd get => '<property>' => sub {
	my $var = shift;
	usage if !$var;
	printf "Global.$var = ".$g->Get('', $var)."\n";
};

cmd set => '<property> <newval>' => sub {
	my($var, $val) = @_;
	usage if !$var || !defined $val;
	propset $g, $var, $val;
	printf "Global.$var changed to $val\n";
};

cmd hubs => '' => sub {
	my @l = @{$hubm->List};
	printf "Hub counts: %s/%s/%s\n", map $hubm->Get('', $_), qw|NormalHubs RegHubs OpHubs|;
	printf "%3d: %s\n", $_, gethub($_)->Get('', 'ConnectAddress') for @l;
};

cmd hubinfo => '<hubid>' => sub {
	my $id = shift;
	usage if !$id;
	my %p = %{gethub($id)->GetAll('')};
	print "Hub #$id: ".($p{ConnectState} != 2 ? '(not connected)' : '')."\n";
	print "  Address:      $p{ConnectAddress} ($p{ResolvedAddress})\n";
	print "  Users:        $p{UserCount} online, sharing ".formatsize($p{UserShareSize})." (".formatsize($p{UserShareSize}/($p{UserCount}||1))."/user)\n";
	print "  Name:         $p{HubName}\n";
	print "  Description:  $p{HubDescription}\n";
	print "  Hub software: $p{HubVersion}\n";
	print "\n";
	print "User info:\n";
	print "  Nick:         $p{MyNick}\n";
	print "  Description:  $p{MyDescription}\n";
	print "  E-Mail:       $p{MyEmail}\n";
	print "  Connection:   ".formatsize($p{MyConnection})."/s\n";
};

cmd hubget => '<hubid> <property>' => sub {
	my($id, $var) = @_;
	usage if !$id || !$var;
	printf "hub($id).$var = ".gethub($id)->Get('', $var)."\n";
};

cmd hubset => '<hubid> <property> <newval>' => sub {
	my($id, $var, $val) = @_;
	usage if !$id || !$var || !defined $val;
	my $h = gethub($id);
	propset $h, $var, $val;
	printf "hub($id).$var changed to $val\n";
};

cmd hubcreate => '[<address>]' => sub {
	my $addr = shift;
	my $id = $hubm->Create();
	printf "Hub created with id=$id\n";
	if($addr) {
		my $h = gethub($id);
		$h->Connect($addr, 58);
		printf "Connected to ".$h->Get('', 'ResolvedAddress')."\n";
	}
};

cmd hubdelete => '<hubid>' => sub {
	my $id = shift;
	usage if !$id;
	$hubm->Delete($id);
	print "Hub $id deleted.\n";
};

cmd connect => '<hubid> [<address>]' => sub {
	my($id, $addr) = @_;
	usage if !$id;
	my $h = gethub($id);
	$h->Connect($addr||'', 58);
	printf "Connected to ".$h->Get('', 'ResolvedAddress')."\n";
};

cmd disconnect => '[--force] <hubid>' => sub {
	my $force = grep /^--force$/, @_;
	my $id = (grep !/^--force$/, @_)[0];
	usage if !$id;
	my $h = gethub($id);
	$force ? $h->ForceDisconnect : $h->Disconnect();
	printf "Disconnecting hub $id\n";
};

cmd userlist => '<hubid>' => sub {
	my $id = shift;
	usage if !$id;
	my $h = gethub($id);
	my($fields, $list) = $h->UserInfo(-1, [ map $userfields{$_}, qw|nick client sharesize hubs slots flags| ]);
	my @u = sort { $a->{nick} cmp $b->{nick} } map +{ id => $_, %{ userstruct2hash($fields, $list->{$_}) } }, keys %$list;
	print "User list for hub #$id:\n";
	printf "%20s %-15s %-40s\n", $_->{id}, $_->{nick}, usertag($_) for (grep $_->{flags} & 1, @u);
	my $cnt = $h->Get('', 'UserCount');
	printf "%d users online (%d hidden).\n", $cnt, @u-$cnt;
};

cmd userinfo => '<hubid> <userid>' => sub {
	my($id, $user) = @_;
	usage if !$id || !defined $user;
	my $h = gethub($id);
	my($fields, $list) = $h->UserInfo($user, [ values %userfields ]);
	die "No user with that id=$user online at the moment.\n" if !$list->{$user};
	my $u = userstruct2hash $fields, $list->{$user};
	print "User $user on hub $id:\n";
	print "  Nick:        $u->{nick}\n";
	print "  Description: $u->{description}\n" if $u->{description};
	print "  E-Mail:      $u->{mail}\n" if $u->{mail};
	print "  Connection:  ".formatsize($u->{conn})."/s\n";
	print "  Flags:       $u->{flags}\n"; # TODO: Visualize/expand
	print "  Tag:         ".usertag($u)."\n";
	print "  IP address:  ".join(', ', $u->{ip4}||(), $u->{ip6}||())."\n" if $u->{ip4} || $u->{ip6};
	# TODO: display CID.
};

cmd chat => '<hubid> <message>' => sub {
	my $id = shift;
	my $msg = join ' ', @_;
	usage if !$id || !$msg || $msg eq '';
	my $h = gethub($id);
	my $me = $msg =~ s/\/me +//;
	$h->SendChat(-1, $msg, $me);
};

cmd pm => '<hubid> <userid> <message>' => sub {
	my $id = shift;
	my $user = shift;
	my $msg = join ' ', @_;
	usage if !$id || !defined $user || !$msg || $msg eq '';
	my $h = gethub($id);
	my $me = $msg =~ s/\/me +//;
	$h->SendChat($user, $msg, $me);
};

cmd shutdown => '', sub {
	$g->Shutdown();
	print "Shutting down the Globster daemon...\n";
};


exit(help && 0) if !$cmds{$cmd};

$g = ($busarg ? Net::DBus->system : Net::DBus->session)->get_service("net.blicky.Globster")->get_object("/net/blicky/Globster");
$hubm = $g->get_child_object("/HubManager");
$cmds{$cmd}[2]->(@ARGV);

# vim: set noet sw=4 ts=4: