CodeSOD: Reloaded Commands
William Heimbinger joins the ranks of developers who come to us to confess their sins. This particular sin was committed when he was but a young child of fifteen years old, which raises more questions than it answers.
Young William was writing Perl and building an anti-spam bot for IRC channels. As he wrote code, he wanted to quickly reload the module to test it, but actually learning how to reload modules looked like too much work. William decided to reinvent that wheel, using a hammer he already knew.
With a trivial block of Perl code:
foreach my $command ( @{$::commands->{command}} ) { if ($cmd=~/$command->{cmd}/) { eval $command->{content}; warn $@ if $@; }}
William could reload all of the commands his bot needed to handle. And what, exactly, was he loading them from? Why, an XML file, of course.
<commands> <command cmd="^;source$"> <![CDATA[ $conn->privmsg($event->replyto, "source is at https://gitlab.devlabs.linuxassist.net/asm/antispammeta/"); ]]> </command> <command cmd="^;monitor (?<chan>\S+) ?(?<switch>yes|no)$" flag="a"> <![CDATA[ my $chan = lc $+{chan}; my $switch = lc $+{switch}; $::channels->{channel}->{$chan}->{monitor} = $switch; ASM::XML->writeChannels(); $conn->privmsg($event->replyto, "Monitor flag for $chan set to $switch"); ]]> </command> <command cmd="^;monitor (?<chan>\S+) *$" flag="s"> <![CDATA[ my $chan = lc $+{chan}; my $switch = $::channels->{channel}->{$chan}->{monitor} // 'yes'; $conn->privmsg($event->replyto, "Monitor flag for $chan is currently set to $switch"); ]]> </command> <command cmd="^;sl (.*)" flag="d"> <![CDATA[ $conn->sl($1); ]]> </command> <command cmd="^;quit ?(.*)" flag="a"> <![CDATA[ $conn->quit($1); ]]> </command> <command cmd="^;ev (.*)" flag="d"> <![CDATA[ eval $1; warn $@ if $@; ]]> </command> <command cmd="^;rehash$" flag="a"> <![CDATA[ ASM::XML->readXML(); $conn->privmsg($event->replyto, 'config files were re-read'); ]]> </command> <command cmd="^;ping\s+(\S.*)$" flag="s"> <![CDATA[ $conn->privmsg($event->replyto, "pong $1"); ]]> </command> <command cmd="^;ping\s*$"> <![CDATA[ $conn->privmsg($event->replyto, "pong"); ]]> </command></commands>
This combination of XML and Perl may have had some serious side effects on William's mental health, or perhaps they're just a sign of a disturbed mind. Either way, this design has been removed from the current version of his bot.
[Advertisement] Use NuGet or npm? Check out ProGet, the easy-to-use package repository that lets you host and manage your own personal or enterprise-wide NuGet feeds and npm repositories. It's got an impressively-featured free edition, too!