/doc/users/joshtriplett/

t' src='/cgit.js'>
summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/external.pm
blob: 204442c1e0b731db6d985cef61181e0c7b0fc424 (plain)
  1. #!/usr/bin/perl
  2. # Support for external plugins written in other languages.
  3. # Communication via XML RPC a pipe.
  4. # See externaldemo for an example of a plugin that uses this.
  5. package IkiWiki::Plugin::external;
  6. use warnings;
  7. use strict;
  8. use IkiWiki 2.00;
  9. use RPC::XML;
  10. use RPC::XML::Parser;
  11. use IPC::Open2;
  12. use IO::Handle;
  13. my %plugins;
  14. sub import { #{{{
  15. my $self=shift;
  16. my $plugin=shift;
  17. return unless defined $plugin;
  18. my ($plugin_read, $plugin_write);
  19. my $pid = open2($plugin_read, $plugin_write,
  20. IkiWiki::possibly_foolish_untaint($plugin));
  21. # open2 doesn't respect "use open ':utf8'"
  22. binmode($plugin_read, ':utf8');
  23. binmode($plugin_write, ':utf8');
  24. $plugins{$plugin}={in => $plugin_read, out => $plugin_write, pid => $pid,
  25. accum => ""};
  26. $RPC::XML::ENCODING="utf-8";
  27. rpc_call($plugins{$plugin}, "import");
  28. } #}}}
  29. sub rpc_write ($$) { #{{{
  30. my $fh=shift;
  31. my $string=shift;
  32. $fh->print($string."\n");
  33. $fh->flush;
  34. } #}}}
  35. sub rpc_call ($$;@) { #{{{
  36. my $plugin=shift;
  37. my $command=shift;
  38. # send the command
  39. my $req=RPC::XML::request->new($command, @_);
  40. rpc_write($plugin->{out}, $req->as_string);
  41. # process incoming rpc until a result is available
  42. while ($_ = $plugin->{in}->getline) {
  43. $plugin->{accum}.=$_;
  44. while ($plugin->{accum} =~ /^\s*(<\?xml\s.*?<\/(?:methodCall|methodResponse)>)\n(.*)/s) {
  45. $plugin->{accum}=$2;
  46. my $r = RPC::XML::Parser->new->parse($1);
  47. error("XML RPC parser failure: $r") unless ref $r;
  48. if ($r->isa('RPC::XML::response')) {
  49. my $value=$r->value;
  50. if ($r->is_fault($value)) {
  51. # throw the error as best we can
  52. print STDERR $value->string."\n";
  53. return "";
  54. }
  55. elsif ($value->isa('RPC::XML::array')) {
  56. return @{$value->value};
  57. }
  58. elsif ($value->isa('RPC::XML::struct')) {
  59. my %hash=%{$value->value};
  60. # XML-RPC v1 does not allow for
  61. # nil/null/None/undef values to be
  62. # transmitted, so until
  63. # XML::RPC::Parser honours v2
  64. # (<nil/>), external plugins send
  65. # a hash with one key "null" pointing
  66. # to an empty string.
  67. if (exists $hash{null} &&
  68. $hash{null} eq "" &&
  69. int(keys(%hash)) == 1) {
  70. return undef;
  71. }
  72. return %hash;
  73. }
  74. else {
  75. return $value->value;
  76. }
  77. }
  78. my $name=$r->name;
  79. my @args=map { $_->value } @{$r->args};
  80. # When dispatching a function, first look in
  81. # IkiWiki::RPC::XML. This allows overriding
  82. # IkiWiki functions with RPC friendly versions.
  83. my $ret;
  84. if (exists $IkiWiki::RPC::XML::{$name}) {
  85. $ret=$IkiWiki::RPC::XML::{$name}($plugin, @args);