This file is indexed.

/usr/bin/rivescript is in librivescript-perl 1.30-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

# A front-end to RiveScript.
# See `rivescript --help` for help.

use 5.10.0;
use strict;
use warnings;
use RiveScript;
use Getopt::Long;
use Pod::Text;
use IO::Socket;
use IO::Select;
use JSON;

#------------------------------------------------------------------------------#
# Command Line Arguments                                                       #
#------------------------------------------------------------------------------#

my $opt = {
	debug   => 0,  # --debug, enables debug mode
	verbose => 1,  # Private, verbose mode for RS
	log     => "", # --log, debug logs to file instead of terminal
	json    => 0,  # --json, running in batch mode
	listen  => "", # --listen, listen on a TCP port
	utf8    => 0,  # --utf8, use UTF-8 mode in RiveScript
	depth   => 50, # depth variable
	strict  => 1,  # --strict, strict mode
	data    => "", # --data, provide JSON data via CLI instead of STDIN
	help    => 0,  # --help
};
GetOptions (
	'debug|d'    => \$opt->{debug},
	'log=s'      => \$opt->{log},
	'help|h|?'   => \$opt->{help},
	'json|j'     => \$opt->{json},
	'utf8|u'     => \$opt->{utf8},
	'listen|l=s' => \$opt->{listen},
	'depth=i'    => \$opt->{depth},
	'data=s'     => \$opt->{data},
	'strict!'    => \$opt->{strict},
);

# Asking for help?
if ($opt->{help}) {
	# Give them our POD instructions.
	my $pod = Pod::Text->new (sentence => 0, width => 78);
	$pod->parse_from_filehandle(*DATA);
	exit(0);
}

# Debug mode options.
if ($opt->{log}) {
	# Logging automatically enables debugging.
	$opt->{debug}   = 1;
	$opt->{verbose} = 0;
}

# UTF-8 support?
if ($opt->{utf8}) {
	binmode(STDIN, ":utf8");
	binmode(STDOUT, ":utf8");
	binmode(STDERR, ":utf8");
}

#------------------------------------------------------------------------------#
# Main Program Begins Here                                                     #
#------------------------------------------------------------------------------#

# A brain has been specified?
my $root = scalar(@ARGV) ? $ARGV[0] : $RiveScript::basedir . "/demo";

# Create the RiveScript interpreter.
my $rs = init();
my $json;   # JSON interpreter if we need it.
my $server; # Server socket if we need it.
my $select; # Selector object if we need it.

# Interactive mode?
if (!$opt->{json} && !$opt->{listen}) {
	# If called with no arguments, hint about the --help option.
	unless (scalar(@ARGV)) {
		print "Hint: use `rivescript --help` for documentation on this command.\n\n";
	}

	print "RiveScript Interpreter - Interactive Mode\n"
		. "-----------------------------------------\n"
		. "RiveScript Version: $RiveScript::VERSION\n"
		. "        Reply Root: $root\n\n"
		. "You are now chatting with the RiveScript bot. Type a message and press Return to send it.\n"
		. "When finished, type '/quit' to exit the program. Type '/help' for other options.\n\n";

	while (1) {
		print "You> ";
		chomp(my $input = <STDIN>);

		# Commands.
		if ($input =~ /^\/help/i) {
			print "> Supported Commands:\n"
				. "> /help   - Displays this message.\n"
				. "> /reload - Reload the RiveScript brain.\n"
				. "> /quit   - Exit the program.\n";
		}
		elsif ($input =~ /^\/reload/i) {
			# Reload the brain.
			undef $rs;
			$rs = init();
			print "> RiveScript has been reloaded.\n\n";
		}
		elsif ($input =~ /^\/(?:quit|exit)/i) {
			# Quit.
			exit(0);
		}
		else {
			# Get a response.
			my $reply = $rs->reply("localuser", $input);
			print "Bot> $reply\n";
		}
	}
}
else {
	# JSON mode.
	$json = JSON->new->pretty();

	# Are we listening from a TCP socket or standard I/O?
	if ($opt->{listen}) {
		tcp_mode();
	} else {
		json_mode();
	}
}

# Handle JSON mode: standard input and output
sub json_mode {
	my $buffer   = "";
	my $stateful = 0;

	# Did they provide us a complete message via --data?
	if ($opt->{data}) {
		$buffer = $opt->{data};
	} else {
		# Nope. Read from standard input. This loop breaks when we
		# receive the EOF (Ctrl+D) signal.
		while (my $line = <STDIN>) {
			chomp($line);
			$line =~ s/[\x0D\x0A]+//g;

			# Look for the __END__ line.
			if ($line =~ /^__END__$/i) {
				# Process it.
				$stateful = 1; # This is a stateful session.
				print json_in($buffer, 1);
				$buffer = "";
				next;
			}

			$buffer .= "$line\n";
		}
	}

	# If the session was stateful, just exit, otherwise
	# process what we just read.
	if ($stateful) {
		exit(0);
	}

	print json_in($buffer);
	exit(0);
}

# Handle TCP mode: using a TCP socket
sub tcp_mode {
	# Validate the listen parameter.
	my $hostname = "localhost";
	my $port     = 2001;
	if ($opt->{listen} =~ /^(.+?):(\d+)$/) {
		$hostname = $1;
		$port     = $2;
	} elsif ($opt->{listen} =~ /^\d+$/) {
		$port     = $opt->{listen};
	} else {
		print "The --listen option requires an address and/or port number. Examples:\n"
			. "--listen localhost:2001\n"
			. "--listen 2001\n";
		exit(1);
	}

	# Create a listening socket.
	$server = IO::Socket::INET->new (
		LocalAddr => $hostname,
		LocalPort => $port,
		Proto     => 'tcp',
		Listen    => 1,
		Reuse     => 1,
	);
	if (!$server) {
		say "Couldn't create socket on $hostname:$port: $@";
		exit 1;
	}

	# Create a socket selector.
	$select = IO::Select->new($server);

	say "Listening for connections at $hostname:$port";

	# Listen for events.
	while (do_one_loop()) {
		select(undef,undef,undef,0.001);
	}
}

# Main loop for TCP server.
my $buffer = {}; # Message buffers per connection.
sub do_one_loop {
	# Look for new events.
	my @ready = $select->can_read(.1);
	return 1 unless @ready;

	foreach my $socket (@ready) {
		my $id = $socket->fileno;
		if ($socket == $server) {
			# It's a new connection.
			$socket = $server->accept();
			$select->add($socket);
			$id = $socket->fileno; # Get the correct fileno for the new socket.
			say ts() ."Connection created: $id";

			# Initialize their buffers.
			$buffer->{$id} = {
				buffer => "", # Current line being read
				lines  => [], # Previous lines being read
			};
		} else {
			# Read what they have to say.
			my $buf;
			$socket->recv($buf, 1024);

			# Completely empty? They've disconnected.
			if (length $buf == 0) {
				# Note that even a "blank line" will still have \r\n characters, so there are
				# no false positives to be had here!
				disconnect($socket);
				next;
			}

			# Trim excess fat.
			$buf =~ s/\x0D\x0A/\x0A/g; # The \r characters

			# Any newlines here?
			if ($buf =~ /\n/) {
				my @pieces = split(/\n/, $buf);
				if (scalar(@pieces) > 1) {
					$buffer->{$id}->{buffer} = pop(@pieces);    # Keep the most recent piece

					# Is this the end?
					if ($buffer->{$id}->{buffer} =~ /^__END__/) {
						# We want this piece after all!
						push(@pieces, $buffer->{$id}->{buffer});
						$buffer->{$id}->{buffer} = "";
					}
				}
				push (@{$buffer->{$id}->{lines}}, @pieces); # Stash the rest
			}

			# Are they done?
			if (scalar @{$buffer->{$id}->{lines}} > 0 &&
			$buffer->{$id}->{lines}->[-1] eq "__END__") {
				# Get their response.
				my @lines = @{$buffer->{$id}->{lines}};
				pop(@lines); # Remove the __END__ line.

				# Get the reply and send it.
				my $response = json_in(join("\n",@lines), 1, $id);
				sock_send($socket, $response);

				# Reset their line buffer.
				$buffer->{$id}->{lines} = [];
			} elsif (scalar @{$buffer->{$id}->{lines}} > 20 || length($buffer->{$id}->{buffer}) > 1024) {
				# This is getting ridiculous.
				sock_send($socket, $json->encode({
					status => "error",
					reply => "Internal Error: Input stream too long. Giving up.",
				}), 1);
				next;
			}
		}
	}

	return 1;
}

# Send a message to a socket.
sub sock_send {
	my ($socket,$msg,$disconnect_after) = @_;

	$socket->send($msg) or do {
		# They've been disconnected.
		disconnect($socket);
		return;
	};

	# Disconnect after?
	if ($disconnect_after) {
		disconnect($socket);
	}
}

# Disconnect a socket.
sub disconnect {
	my $socket = shift;
	my $id     = $socket->fileno;
	say ts() . "Disconnected: $id";

	# Forget we ever saw them.
	delete $buffer->{$id};
	$select->remove($socket);
	$socket->close();
}

# Initializes the RiveScript interpreter.
sub init {
	my $rs = RiveScript->new (
		debug     => $opt->{debug},
		verbose   => $opt->{verbose},
		debugfile => $opt->{log},
		depth     => $opt->{depth},
		strict    => $opt->{strict},
		utf8      => $opt->{utf8},
	);

	$rs->loadDirectory($root);
	$rs->sortReplies();
	return $rs;
}

sub json_in {
	my $buffer = shift;
	my $end    = shift;
	my $tcp    = shift;

	my $data  = {};
	my $reply = {
		status => "ok",
	};

	# Try to decode their input.
	eval {
		$data = $json->decode($buffer);
	};

	# Error?
	if ($@) {
		$reply->{status} = "error";
		$reply->{reply}  = "Failed to decode your input: $@";
		if ($tcp) {
			say ts() . "$tcp: Failed to decode JSON input: $@";
		}
	}
	else {
		# Decode their variables.
		my $username = exists $data->{username} ? $data->{username} : "localuser";
		if (ref($data->{vars}) eq "HASH") {
			foreach my $key (keys %{$data->{vars}}) {
				next if ref($data->{vars}->{$key});
				$rs->setUservar($username, $key, $data->{vars}->{$key});
			}
		}

		# Get their answer.
		$reply->{reply} = $rs->reply($username, $data->{message});
		if ($tcp) {
			say ts() . "$tcp: [$username] $data->{message}";
			say ts() . "$tcp: [Response] $reply->{reply}";
		}

		# Retrieve vars.
		$reply->{vars} = {};
		my $vars = $rs->getUservars($username);
		foreach my $key (keys %{$vars}) {
			next if ref($vars->{$key});
			$reply->{vars}->{$key} = $vars->{$key};
		}
	}

	# Encode and print.
	my $return = $json->encode($reply);
	$return .= "__END__\n" if $end;
	return $return;
}

# Simple time stamp.
sub ts {
	my @now = localtime();
	return sprintf("[%02d:%02d:%02d] ", $now[2], $now[1], $now[0]);
}

__DATA__

=head1 NAME

rivescript - A command line frontend to the Perl RiveScript interpreter.

=head1 SYNOPSIS

  $ rivescript [options] [path to RiveScript documents]

=head1 DESCRIPTION

This is a command line front-end to the RiveScript interpreter. This script
obsoletes the old C<rsdemo>, and can also be used non-interactively by third
party programs. To that end, it supports a variety of input/output and session
handling methods.

If no RiveScript document path is given, it will default to the example brain
that ships with the RiveScript module, which is based on the Eliza bot.

=head1 OPTIONS

=over 4

=item --debug, -d

Enables debug mode. This will print all debug data from RiveScript to your
terminal. If you'd like it to log to a file instead, use the C<--log> option
instead of C<--debug>.

=item --log FILE

Enables debug mode and prints the debug output to C<FILE> instead of to your
terminal.

=item --json, -j

Runs C<rivescript> in JSON mode, for running the script in a non-interactive
way (for example, to use RiveScript in a programming language that doesn't have
a native RiveScript library). See L<"JSON Mode"> for details.

=item --data JSON_DATA

When using the C<--json> option, you can provide the JSON input message as a
command line argument with the C<--data> option. If not provided, then the
JSON data will be read from standard input instead. This option is helpful,
therefore, if you don't want to open a two-way pipe, but rather pass the message
as a command line argument and just read the response from standard output.
See L<"JSON Mode"> for more details.

=item --listen, -l [ADDRESS:]PORT

Runs C<rivescript> in TCP mode, for running the script as a server daemon.
If an address isn't specified, it will bind to C<localhost>. See
L<"TCP Mode"> for details.

=item --strict, --nostrict

Enables strict mode for the RiveScript parser. It's enabled by default, use
C<--nostrict> to disable it. Strict mode prevents the parser from continuing
when it finds a syntax error in the RiveScript documents.

=item --depth=50

Override the default recursion depth limit. This controls how many times
RiveScript will recursively follow redirects to other replies. The default is
C<50>.

=item --utf8, -u

Use the UTF-8 option in RiveScript. This allows triggers to contain foreign
characters and relaxes the filtering of user messages. This is not enabled
by default!

=item --help

Displays this documentation in your terminal.

=back

=head1 USAGE

=head2 Interactive Mode

This is the default mode used when you run C<rivescript> without specifying
another mode. This mode behaves similarly to the old C<rsdemo> script and lets
you chat one-on-one with your RiveScript bot.

This mode can be used to test your RiveScript bot. Example:

  $ rivescript /path/to/rs/files

=head2 JSON Mode

This mode should be used when calling from a third party program. In this mode,
data that enters and leaves the script are encoded in JSON.

Example:

  $ rivescript --json /path/to/rs/files

The format for incoming JSON data is as follows:

  {
    "username": "localuser",
    "message":  "Hello bot!",
    "vars": {
      "name": "Aiden"
    }
  }

Here, C<username> is a unique name for the user, C<message> is their message to
the bot, and C<vars> is a hash of any user variables your program might be
keeping track of (such as the user's name and age).

The response from C<rivescript> will look like the following:

  {
    "status": "ok",
    "reply":  "Hello, human!",
    "vars": {
      "name": "Aiden"
    }
  }

Here, C<status> will be C<"ok"> or C<"error">, C<reply> is the bot's response to
your message, and C<vars> is a hash of the current variables for the user (so
that your program can save them somewhere).

=head3 Standard Input or Data

By default, JSON mode will read from standard input to receive your JSON
message. As an alternative to this, you can provide the C<--data> option to
C<rivescript> to present the incoming JSON data as a command line argument.

This may be helpful if you don't want to open a two-way pipe to C<rivescript>,
and would rather pass your input as a command line argument and simply read
the response from standard output.

Example:

  $ rivescript --json --data '{"username": "localuser", "message": "hello" }' \
    /path/to/rs/files

This will cause C<rivescript> to print its JSON response to standard output
and exit. You can't have a stateful session using this method.

=head3 End of Message

There are two ways you can use the JSON mode: "fire and forget," or keep a
stateful session open.

In "fire and forget," you open the program, print your JSON input and send the
EOF signal, and then C<rivescript> sends you the JSON response and exits.

In a stateful session mode, you must send the text C<__END__> on a line by
itself after you finish sending your JSON data. Then C<rivescript> will
process it, return its JSON response and then also say C<__END__> at the end.

Example:

  {
    "username": "localuser",
    "message": "Hello bot!",
    "vars": {}
  }
  __END__

And the response:

  {
    "status": "ok",
    "reply": "Hello, human!",
    "vars": {}
  }
  __END__

This way you can reuse the same pipe to send and receive multiple messages.

=head2 TCP Mode

TCP Mode will make C<rivescript> listen on a TCP socket for incoming
connections. This way you can connect to it from a different program (for
example, a CGI script or a program written in a different language).

Example:

  $ rivescript --listen localhost:2001

TCP Mode behaves similarly to L<"JSON Mode">; the biggest difference is that
it will read and write using a TCP socket instead of standard input and
output. Unlike JSON Mode, however, TCP Mode I<always> runs in a stateful
way (the JSON messages must end with the text "C<__END__>" on a line by
itself). See L<"End of Message">.

If the C<__END__> line isn't found after 20 lines of text are read from
the client, it will give up and send the client an error message (encoded
in JSON) and disconnect it.

=head1 SEE ALSO

L<RiveScript>, the Perl RiveScript interpreter.

=head1 AUTHOR

Noah Petherbridge, http://www.kirsle.net

=head1 LICENSE

  RiveScript - Rendering Intelligence Very Easily
  Copyright (C) 2012 Noah Petherbridge
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

=cut