1#!--PERL-- 2# -*- indent-tabs-mode: nil; -*- 3# vim:ft=perl:et:sw=4 4# $Id$ 5 6# Sympa - SYsteme de Multi-Postage Automatique 7# 8# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel 9# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 10# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites 11# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER 12# 13# This program is free software; you can redistribute it and/or modify 14# it under the terms of the GNU General Public License as published by 15# the Free Software Foundation; either version 2 of the License, or 16# (at your option) any later version. 17# 18# This program is distributed in the hope that it will be useful, 19# but WITHOUT ANY WARRANTY; without even the implied warranty of 20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21# GNU General Public License for more details. 22# 23# You should have received a copy of the GNU General Public License 24# along with this program. If not, see <http://www.gnu.org/licenses/>. 25 26use lib '--modulesdir--'; 27use strict; 28use warnings; 29use Getopt::Long; 30use HTTP::Cookies; 31#use SOAP::Lite +trace; 32use SOAP::Lite; 33 34use Sympa::Tools::Data; 35 36my ($reponse, @ret, $val, %fault); 37 38my $usage = 39 "$0 is a perl soap client for Sympa for TEST ONLY. Use it to illustrate how to 40code access to features of Sympa soap server. Authentication can be done via 41user/password or user cookie or as a trusted remote application 42 43Usage: $0 <with the following options:> 44--soap_url=<soap sympa server url> 45--service=<a sympa service> 46--trusted_application=<app name> 47--trusted_application_password=<password> 48--proxy_vars=<id=value,id2=value2> 49--service_parameters=<value1,value2,value3> 50 51 52OR usage: $0 <with the following options:> 53--soap_url=<soap sympa server url> 54--user_email=<email> 55--user_password=<password> 56--session_id=<sessionid> 57--service=<a sympa service> 58--service_parameters=<value1,value2,value3> 59 60 61OR usage: $0 <with the following options:> 62--soap_url=<soap sympa server url> 63--cookie=<sympauser cookie string> 64 65Example: 66$0 --soap_url=<soap sympa server url> --cookie=sympauser=someone\@cru.fr 67"; 68 69my %options; 70unless ( 71 GetOptions( 72 \%main::options, 'soap_url=s', 73 'service=s', 'trusted_application=s', 74 'trusted_application_password=s', 'user_email=s', 75 'user_password=s', 'cookie=s', 76 'proxy_vars=s', 'service_parameters=s', 77 'session_id=s' 78 ) 79) { 80 printf ""; 81} 82 83my $soap_url = $main::options{'soap_url'}; 84unless (defined $soap_url) { 85 printf "error : missing soap_url parameter\n"; 86 printf $usage; 87 exit 1; 88} 89 90my $user_email = $main::options{'user_email'}; 91my $user_password = $main::options{'user_password'}; 92my $session_id = $main::options{'session_id'}; 93my $trusted_application = $main::options{'trusted_application'}; 94my $trusted_application_password = 95 $main::options{'trusted_application_password'}; 96my $proxy_vars = $main::options{'proxy_vars'}; 97my $service = $main::options{'service'}; 98my $service_parameters = $main::options{'service_parameters'}; 99my $cookie = $main::options{'cookie'}; 100 101if (defined $trusted_application) { 102 unless (defined $trusted_application_password) { 103 printf "error : missing trusted_application_password parameter\n"; 104 printf $usage; 105 exit 1; 106 } 107 unless (defined $service) { 108 printf "error : missing service parameter\n"; 109 printf $usage; 110 exit 1; 111 } 112 unless (defined $proxy_vars) { 113 printf "error : missing proxy_vars parameter\n"; 114 printf $usage; 115 exit 1; 116 } 117 118 play_soap_as_trusted($soap_url, $trusted_application, 119 $trusted_application_password, $service, $proxy_vars, 120 $service_parameters); 121} elsif ($service eq 'getUserEmailByCookie') { 122 play_soap( 123 soap_url => $soap_url, 124 session_id => $session_id, 125 service => $service 126 ); 127 128} elsif (defined $cookie) { 129 printf "error : get_email_cookie\n"; 130 get_email($soap_url, $cookie); 131 exit 1; 132} else { 133 unless (defined $session_id 134 || (defined $user_email && defined $user_password)) { 135 printf 136 "error : missing session_id OR user_email+user_passwors parameters\n"; 137 printf $usage; 138 exit 1; 139 } 140 141 play_soap( 142 soap_url => $soap_url, 143 user_email => $user_email, 144 user_password => $user_password, 145 session_id => $session_id, 146 service => $service, 147 service_parameters => $service_parameters 148 ); 149} 150 151sub play_soap_as_trusted { 152 my $soap_url = shift; 153 my $trusted_application = shift; 154 my $trusted_application_password = shift; 155 my $service = shift; 156 my $proxy_vars = shift; 157 my $service_parameters = shift; 158 159 my $soap = SOAP::Lite->new(); 160 $soap->uri('urn:sympasoap'); 161 $soap->proxy($soap_url); 162 163 my @parameters; 164 if (defined $service_parameters) { 165 @parameters = split /,/, $service_parameters; 166 } else { 167 @parameters = (); 168 } 169 my $p = join(',', @parameters); 170 printf 171 "calling authenticateRemoteAppAndRun( $trusted_application, $trusted_application_password, $proxy_vars,$service,$p)\n"; 172 173 my $reponse = 174 $soap->authenticateRemoteAppAndRun($trusted_application, 175 $trusted_application_password, $proxy_vars, $service, \@parameters); 176 print_result($reponse); 177} 178 179sub get_email { 180 my $soap_url = shift; 181 my $cookie = shift; 182 183 my ($service, $reponse, @ret, $val, %fault); 184 185 ## Cookies management 186 # my $uri = URI->new($soap_url); 187 188# my $cookies = HTTP::Cookies->new(ignore_discard => 1, 189# file => '/tmp/my_cookies' ); 190# $cookies->load(); 191 printf "cookie : %s\n", $cookie; 192 193 my $soap = SOAP::Lite->new(); 194 #$soap->on_debug(sub{print@_}); 195 $soap->uri('urn:sympasoap'); 196 $soap->proxy($soap_url); 197#, cookie_jar =>$cookies); 198 199 print "\n\ngetEmailUserByCookie....\n"; 200 $reponse = $soap->getUserEmailByCookie($cookie); 201 print_result($reponse); 202 exit; 203 204} 205 206sub play_soap { 207 my %param = @_; 208 209 my $soap_url = $param{'soap_url'}; 210 my $user_email = $param{'user_email'}; 211 my $user_password = $param{'user_password'}; 212 my $session_id = $param{'session_id'}; 213 my $service = $param{'service'}; 214 my $service_parameters = $param{'service_parameters'}; 215 216 my ($reponse, @ret, $val, %fault); 217 218 ## Cookies management 219 # my $uri = URI->new($soap_url); 220 221 my $cookies = HTTP::Cookies->new( 222 ignore_discard => 1, 223 file => '/tmp/my_cookies' 224 ); 225 $cookies->load(); 226 printf "cookie : %s\n", $cookies->as_string(); 227 228 my @parameters; 229 @parameters = split(/,/, $service_parameters) 230 if (defined $service_parameters); 231 my $p = join(',', @parameters); 232 foreach my $tmpParam (@parameters) { 233 printf "param: %s\n", $tmpParam; 234 } 235 236 # Change to the path of Sympa.wsdl 237 #$service = SOAP::Lite->service($soap_url); 238 #$reponse = $service->login($user_email,$user_password); 239 #my $soap = SOAP::Lite->service($soap_url); 240 241 my $soap = SOAP::Lite->new() || die; 242 #$soap->on_debug(sub{print@_}); 243 $soap->uri('urn:sympasoap'); 244 $soap->proxy($soap_url, cookie_jar => $cookies); 245 246 ## Do the login unless a session_id is provided 247 if ($session_id) { 248 print "Using Session_id $session_id\n"; 249 250 } else { 251 print "LOGIN....\n"; 252 253 #$reponse = $soap->casLogin($soap_url); 254 $reponse = $soap->login($user_email, $user_password); 255 $cookies->save; 256 print_result($reponse); 257 $session_id = $reponse->result; 258 } 259 260 ## Don't use authenticateAndRun for lists command 261 262 ## Split parameters 263 if ($service_parameters && $service_parameters ne '') { 264 @parameters = split /,/, $service_parameters; 265 } 266 267 if ($service eq 'lists') { 268 printf "\n\nlists....\n"; 269 $reponse = $soap->lists(); 270 271 } elsif ($service eq 'subscribe') { 272 printf "\n\n$service....\n"; 273 $reponse = $soap->subscribe(@parameters); 274 275 } elsif ($service eq 'signoff') { 276 printf "\n\n$service....\n"; 277 $reponse = $soap->signoff(@parameters); 278 279 } elsif ($service eq 'add') { 280 printf "\n\n$service....\n"; 281 $reponse = $soap->add(@parameters); 282 283 } elsif ($service eq 'del') { 284 printf "\n\n$service....\n"; 285 $reponse = $soap->del(@parameters); 286 287 } elsif ($service eq 'getUserEmailByCookie') { 288 printf "\n\n$service....\n"; 289 $reponse = $soap->getUserEmailByCookie($session_id); 290 291 } else { 292 printf "\n\nAuthenticateAndRun service=%s;(session_id=%s)....\n", 293 $service, $session_id; 294 $reponse = 295 $soap->authenticateAndRun($user_email, $session_id, $service, 296 \@parameters); 297 } 298 299 print_result($reponse); 300 301} 302 303sub print_result { 304 my $r = shift; 305 306# If we get a fault 307 if (defined $r && $r->fault) { 308 print "Soap error :\n"; 309 my %fault = %{$r->fault}; 310 foreach $val (keys %fault) { 311 print "$val = $fault{$val}\n"; 312 } 313 } else { 314 if (ref($r->result) =~ /^ARRAY/) { 315 #printf "R: $r->result\n"; 316 @ret = @{$r->result}; 317 } elsif (ref $r->result) { 318 print "Pb " . ($r->result) . "\n"; 319 return undef; 320 } else { 321 @ret = $r->result; 322 } 323 Sympa::Tools::Data::dump_var(\@ret, 0, \*STDOUT); 324 } 325 326 return 1; 327} 328