#!/usr/bin/env perl

use strict;
use warnings;
use diagnostics;

use YAML::Tiny;
use XML::LibXML;
use MIME::Base64;
use AnyEvent::XMLRPC;
use experimental 'smartmatch';

##################################################################################################
my ($YAMLoutput, $YAMLbase64);
my ($member, $memberstatus, $membercounter, $username);
my ($voting, $birthday, $address, $votingright, $votingrights);
my ($regularmember, $studentmember, $supportmember, $starvingmember);
my ($serv, $sharedsecret, $XMLparser, $XMLfile, $XMLquery, $XMLnode);
my ($chaosid, $lastname, $forename, $email, $status, $date, $paid, $userbox, $keyowner, $comment);
my (@members, @usernames, %usercontacts);
my @sharedsecrets = ('secret1', 'secret2');
##################################################################################################

$serv = AnyEvent::XMLRPC->new(
                port    => 9090,
                uri     => "/RPC2",
                ssl     => {
                         cert_file => "cert.pem",
                         key_file  => "key.pem"
                },
                methods => {
                        'CHAOSID'     => \&CHAOSID,
                        'COUNTER'     => \&COUNTER,
                        'MEMBERINFO'  => \&MEMBERINFO,
                        'VOTINGRIGHT' => \&VOTINGRIGHT
                },
        );


sub CHAOSID {
  ($sharedsecret, $chaosid) = ("none", 0);
  ($sharedsecret, $chaosid) = @_;
  return "FUCK OFF"    unless ($sharedsecret ~~ @sharedsecrets);
  return "NO CHAOS ID" unless (length $chaosid == 36);

  $XMLparser = XML::LibXML->new();
  $XMLfile   = $XMLparser->parse_file('datebank');
  @members   = $XMLfile->findnodes('/memberdatebank/memberen/member');

  $username = 0;

  $XMLquery  = "/memberdatebank/memberen/member[id/text() = '$chaosid']";
    foreach $XMLnode ($XMLfile->findnodes($XMLquery)) {
      $username = $XMLnode->findnodes('./nick');
    }

  return "NO SUCH USER" unless $username;

  $YAMLoutput = YAML::Tiny->new( { ChaosID => "$chaosid", Username => "$username" } );
  chomp ($YAMLbase64 = encode_base64 $YAMLoutput->write_string);
  return $YAMLbase64;
}


sub MEMBERINFO {
  ($sharedsecret, $username) = ("none", 0);
  ($sharedsecret, $username) = @_;
  return "FUCK OFF" unless ($sharedsecret ~~ @sharedsecrets);
  return "NO USER"  unless $username;

  $XMLparser = XML::LibXML->new();
  $XMLfile   = $XMLparser->parse_file('datebank');
  @members   = $XMLfile->findnodes('/memberdatebank/memberen/member');

  ($chaosid, $lastname, $forename, $email, $status, $date, $paid, $userbox, $keyowner, $comment) = 0;

  $XMLquery  = "/memberdatebank/memberen/member[nick/text() = '$username']";
    foreach $XMLnode ($XMLfile->findnodes($XMLquery)) {
        $chaosid  = $XMLnode->findnodes('./id');
        $lastname = $XMLnode->findnodes('./numm');
        $forename = $XMLnode->findnodes('./nonumm');
        $birthday = $XMLnode->findnodes('./gebuertsdag');
        $address  = $XMLnode->findnodes('./adress');
        $email    = $XMLnode->findnodes('./email');
        $status   = $XMLnode->findnodes('./status');
        $voting   = int $XMLnode->findnodes('./stemmrecht');
        $date     = $XMLnode->findnodes('./datum');
        $paid     = int $XMLnode->findnodes('./bezuelt');
        $userbox  = int $XMLnode->findnodes('./kescht');
        $keyowner = int $XMLnode->findnodes('./badge');
        $comment  = $XMLnode->findnodes('./kommentar');
    }

  return "NO SUCH USER" unless $chaosid;

  $YAMLoutput =  YAML::Tiny->new( {
  ChaosID => "$chaosid",
  Names   => {
    'Forename' => "$forename",
    'Lastname' => "$lastname",
    'Username' => "$username"
  },
  Email  => "$email",
  Status => "$status",
  Voters => "$voting",
  More   => {
    'Address'    => "$address",
    'Birthday'   => "$birthday",
    'Storagebox' => "$userbox",
    'Payment'    => "$date",
    'Paid'       => "$paid",
    'Keyowner'   => "$keyowner",
    'Comment'    => "$comment"
  }  
  } );

  chomp ($YAMLbase64 = encode_base64 $YAMLoutput->write_string);
  return $YAMLbase64;

}


sub COUNTER {
  $sharedsecret = shift||"none";
  return "FUCK OFF" unless ($sharedsecret ~~ @sharedsecrets);

  $XMLparser = XML::LibXML->new();
  $XMLfile   = $XMLparser->parse_file('datebank');
  @members   = $XMLfile->findnodes('/memberdatebank/memberen/member');

  ($regularmember, $supportmember, $starvingmember, $votingrights) = 0;

    foreach $member (@members) {
        $memberstatus = $member->findnodes('./status');
        $votingright  = $member->findnodes('./stemmrecht');   
        $votingrights++     if int $votingright; 
        $regularmember++    if ($memberstatus  =~ /^Regular$/i);
        $studentmember++    if ($memberstatus  =~ /^Student$/i);
        $supportmember++    if ($memberstatus  =~ /^Supporter/i);
        $starvingmember++   if ($memberstatus  =~ /^Starving$/i);
    }  
    

  $membercounter = scalar @members;

  $YAMLoutput = YAML::Tiny->new( { Counter => $membercounter, Voters => $votingrights, Regular => $regularmember, Student => $studentmember, Supporter => $supportmember, Starving => $starvingmember } );
  chomp ($YAMLbase64 = encode_base64 $YAMLoutput->write_string);
  return $YAMLbase64;
}


sub VOTINGRIGHT {
  $sharedsecret = shift||"none";
  return "FUCK OFF" unless ($sharedsecret ~~ @sharedsecrets);

  $XMLparser = XML::LibXML->new();
  $XMLfile   = $XMLparser->parse_file('datebank');
  @members   = $XMLfile->findnodes('/memberdatebank/memberen/member');
  $votingrights = 0;
  
  $XMLquery  = "/memberdatebank/memberen/member[stemmrecht/text() = '1']";
    foreach $XMLnode ($XMLfile->findnodes($XMLquery)) {
      ($username, $email) = ( $XMLnode->findnodes('./nick') . '', $XMLnode->findnodes('./email') . '');  # Don't know why but I have to add "nothing" to the string to make YAML::Tiny work with XML::LibXML
      push @usernames, $username;
      $usercontacts{$username} = $email;
      $votingrights++;
    }
  

  $YAMLoutput = YAML::Tiny->new( { Voters => "$votingrights", Usernames => [@usernames], Contact => { %usercontacts } } );
  
  chomp ($YAMLbase64 = encode_base64 $YAMLoutput->write_string);
  undef @usernames;
  return $YAMLbase64;
}


$serv->run;
