Skip to content

Instantly share code, notes, and snippets.

@zpao
Created October 28, 2011 05:37
Show Gist options
  • Save zpao/1321692 to your computer and use it in GitHub Desktop.
Save zpao/1321692 to your computer and use it in GitHub Desktop.
a hubot plugin using coffeescript vs a firebot plugin using perl
# Generate a uuid or cid.
#
# uuid - Generate a UUID.
# cid - Generate a UUID but outputs format suitable for components (CID).
exec = require("child_process").exec
module.exports = (robot) ->
robot.respond /(uuid|cid)/i, (msg) ->
exec "uuidgen", (error, stdout, stderr) ->
uuid = stdout.trim().toLowerCase()
if msg.match[1] == "uuid"
msg.send "#{uuid}"
# firebot does that extra message but meh
# msg.send "#{uuid} (/msg #{robot.name} cid for CID form)"
else
split = uuid.split "-"
parts = [split[0], split[1], split[2]].map (p) -> "0x#{p}"
parts2 = (split[3] + split[4]).match(/../g).map (p)-> "0x#{p}"
parts.push "{#{parts2.join(", ")}}"
msg.send "{#{parts.join(", ")}}"
################################
# UUIDGen Module #
################################
# "uuidgen" should be installed on the path somewhere.
# you can get the source of uuidgen from CVS, see:
# http://lxr.mozilla.org/mozilla/source/webtools/mozbot/uuidgen/
package BotModules::UUIDGen;
use vars qw(@ISA);
@ISA = qw(BotModules);
1;
sub Help {
my $self = shift;
my ($event) = @_;
return {
'' => 'This module is an interface to the uuidgen application.',
'uuid' => 'Generates a UUID.',
'cid' => 'Generates a UUID but outputs format suitable for components (CID).',
};
}
sub Told {
my $self = shift;
my ($event, $message) = @_;
if ($message =~ /^\s*uuid(?:[\s,!?]+please)?[\s,!?]*\s*$/osi) {
$self->spawnChild($event, 'uuidgen', [], 'UUID', []);
} elsif ($message =~ /^\s*cid(?:[\s,!?]+please)?[\s,!?]*\s*$/osi) {
$self->spawnChild($event, 'uuidgen', [], 'CID', []);
} else {
return $self->SUPER::Told(@_);
}
return 0; # we've dealt with it, no need to do anything else.
}
# ChildCompleted - Called when a child process has quit
sub ChildCompleted {
my $self = shift;
my ($event, $type, $output, @data) = @_;
if ($type eq 'UUID') {
chop($output);
$output .= " (/msg $nicks[$nick] cid for CID form)";
$self->say($event, $output);
} elsif ($type eq 'CID') {
# remove newline
chop($output);
my @split = split(/-/, $output);
$output = "{0x$split[0], 0x$split[1], 0x$split[2], {";
my @rest = $split[3] =~ m/(..)(..)/;
push(@rest, $split[4] =~ m/(..)(..)(..)(..)(..)(..)/);
foreach (@rest) {
$output .= "0x$_, ";
}
# remove the space and comma
chop($output);
chop($output);
$output .= "}}\n";
$self->say($event, $output);
} else {
return $self->SUPER::ChildCompleted(@_);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment