File: remote.pl

package info (click to toggle)
snap 0.08-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 440 kB
  • ctags: 473
  • sloc: perl: 7,033; makefile: 117; sh: 70
file content (281 lines) | stat: -rw-r--r-- 5,915 bytes parent folder | download
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#
# This is a very simple remote control system for Snap. This is meant 
# for those times when you want to set up a stand-alone Snap server.  
# It allows you to set up the client as a background process and 
# communicate with it via messages on the Napster server itself.  Kinda 
# handy for weird situations, and demonstrates the power of scripting 
# in Snap.
#
# This new version, which replaces the original remote.pl script,
# is MUCH more stable, since it uses a much simpler system for trapping
# screen printing (Snap's standard tie() system).  Thus, the stability
# is greatly increased.  As well, it has some newer features, like
# the ability to disable specific commands, for enhanced security.
#
# Make sure you define these variables before you load this script:
#
# $remote_password - Password used to gain control over the client.
# @allowed         - List of nicks allowed to control this client.
# @disabled        - List of disabled commands.  ie, commands which 
#                    aren't allowed to run remotely (ie, /eval, /exec).
#
# To gain control of the client, send a private message to the client
# with the text: verify password
#
# The client will then return an ACK or a NAK.  If it's an ACK, you
# then send commands by sending a message to the client with the
# desired command.  Easy! :)
#

package RemoteWindow;

#
# This part of the module comprises the replacement screen routines,
# used to transparently send the text output through the socket to the
# remote user.  This part of the code provides an excellent template for
# modules which wish to intercept the screen printing and deal with the
# text themselves.
#

use SnapLib::MessageTypes;

# Print function... emulates buffered output to ensure proper line
# breaking, etc.

sub print
{
  my ($self, $text) = @_;
  my $handle = $self->{"handle"};

  if (defined $self->{"window"})
    { $self->{"window"}->print("$text") }

  return if ($main::user eq undef);

  $text =~ s/~.*?;//g;

  while ($text ne "")
    {
      # Now the guts...

      my $line;
      my $pos = index($text, "\n") + 1;

      if ($pos > 0)
        { $line = substr($text, 0, $pos); }
      else
        { $line = $text; }

      $text =~ s/^\Q$line\E//;

      $self->{"buffer"} .= $line;

      if ($line =~ /\n/)
        {
	  my $buf = $self->{"buffer"};

	  $buf =~ s/(\s+?)$//g;

	  $napster_sock->send(MSG_PRIVATE, "$::user |$buf");
	  $self->{"buffer"} = "";
	};
    }
}

sub TIEHANDLE
{
  my $class = shift;
  my $self = {};
  my $obj = tied(*STDOUT);

  bless $self, $class;
  $self->{"window"} = $obj;
  $self->{"total"} = 0;
  $self->{"handle"} = shift;

  return $self;
}

sub WRITE
{
  my $self = shift;

  $self->print("WRITE not supported...\n");
}

sub PRINT
{
  my $self = shift;
  my $var;

  foreach $var (@_)
    { $self->print($var); }
}

sub PRINTF
{
  my $self = shift;

  $self->print("PRINTF not supported...\n");
}

sub READ
{
  my $self = shift;

  $self->print("On STDIN?  Nuhuh...\n");
}

sub READLINE
{
  my $self = shift;

  $self->print("On STDIN?  Nuhuh...\n");
}

sub GETC
{
  my $self = shift;

  $self->print("On STDOUT?  Nuhuh...\n");
}

sub CLOSE
{
  my $self = shift;
  
  $self->print("CLOSE not supported...\n");
}

sub DESTROY
{
}

###############################################################################

package main;

my $user = undef;
my @verified;
my $bottom = 0;
my $active = 1;

$REMOTE_VERSION = "0.02";

print "~13;Remote Controller~c; $REMOTE_VERSION loaded...\n";

local *H = *STDOUT;
tie *STDOUT, 'RemoteWindow', *H;  # Tie our remote windowing code to STDOUT

sub send_message
{
  $_[0]->send(MSG_PRIVATE, $_[1]);
}

sub msg_handler
{
  my ($sock, $text) = @_; 
  my $cmd;
  $$text =~ /(.+?) (.*)/;  
  my $u = $1; $cmd = $2;

  if (($cmd =~ /^verify\s+(.*)/i) && grep (/\Q$u\E/, @allowed))
    {
      if ($1 eq $remote_password)
        {
          push @verified, $u;
          send_message($napster_sock, 
			      "$u Verification succeeded.  Welcome $u!");

          return;
         }
      else
        {
          send_message($napster_sock,
			      "$u Error, verification failed.");
          return;
        }
    }
  elsif (! grep (/\Q$u\E/, @verified) && ($cmd =~ /^verify\s+(.*)/i))
    {
      send_message($napster_sock,
                          "$u Error, you aren't allowed access to this client.");
      return;
    }
  elsif (! grep (/\Q$u\E/, @verified) && ($cmd !~ /^verify\s+(.*)/i))
    { return; }

  if ($cmd =~ /^disconnect$/i)
    {
      send_message($napster_sock, "$u Disconnecting view.");
      $::user = undef;

      return;
    }

  $::user = $u;

  debug_print("Remote", "Executing $cmd...\n");

  my $entry;

  foreach $entry (@disabled)
    {     
      if ($cmd =~ /^\Q$entry\E/i)
        {
	  send_message($napster_sock, 
			      "$u Sorry, that command is disabled.");
	  return;
        }
    }  

  return if (! $active);

  do_command($sock, $cmd);
}

sub do_remote_setup
{
  my ($sock, $param_str, @params) = @_;
  my %opts = ("disable" => 0,
              "enable" => 0,
              "version" => 0);

  my $results = getopts(\%opts, \@params);

  return if (! defined $results);

  if (defined $results->{"disable"})
    {
      $active = 0;
      print "Disabling Remote Controller...\n";
    }
  
  if (defined $results->{"enable"})
    {
      $active = 1;
      print "Enabling Remote Controller...\n";
    }

  if (defined $results->{"version"})
    {
      print "Remote Controller version $VERSION\n";
    }
}

sub do_remote_help
{
  return "Usage: /remote [options]

  Sets options for the Remote Controller module.  Options include:

    -ENABLE  - Enables Remote Controller
    -DISABLE - Disables Remote Controller
    -VERSION - Display Remote Controller version.
";
}

push @{ $code_hash{&MSG_PRIVATE} }, \&msg_handler;

$command_hash{"/remote"} = [\&do_remote_setup];
$help_hash{"/remote"} = \&do_remote_help;