File Coverage

blib/lib/Slash/Client.pm
Criterion Covered Total %
statement 28 76 36.8
branch 5 30 16.6
condition 0 14 0.0
subroutine 6 10 60.0
pod 0 6 0.0
total 39 136 28.6


line stmt bran cond sub pod time code
1             # This code is a part of Slash, and is released under the GPL.
2             # Copyright 1997-2005 by Open Source Technology Group. See README
3             # and COPYING for more information, or see http://slashcode.com/.
4             # $Id: Client.pm,v 1.1 2005/11/21 17:31:46 pudge Exp $
5              
6             package Slash::Client;
7              
8 1     1   24502 use strict;
  1         2  
  1         30  
9 1     1   5 use warnings;
  1         2  
  1         33  
10              
11 1     1   5 use Digest::MD5 'md5_hex';
  1         5  
  1         68  
12 1     1   1051 use File::Spec::Functions;
  1         1009  
  1         959  
13              
14             our $VERSION = 0.01;
15              
16             sub new {
17 1     1 0 2 my($class, $opts) = @_;
18              
19 1         2 my $self = {};
20 1 50       5 $self->{host} = $opts->{host} or return;
21 1 50       4 $self->{http} = $opts->{ssl} ? 'https' : 'http';
22              
23 1         2 $self->{uid} = $opts->{uid};
24 1         3 $self->{pass} = $opts->{pass};
25 1         3 $self->{logtoken} = $opts->{logtoken};
26 1         3 $self->{cookie_file} = $opts->{cookie_file};
27              
28 1         2 bless $self, $class;
29 1         3 return $self;
30             }
31              
32             sub soap {
33 1     1 0 3 my($self) = @_;
34 1 50       7 my $uri = $self->{soap}{uri} or return;
35 1 50       6 my $proxy = $self->{soap}{proxy} or return;
36              
37 1 50       5 return $self->{soap}{cache} if $self->{soap}{cache};
38              
39 1         7687 require HTTP::Cookies;
40 1         20268 require SOAP::Lite;
41              
42 0           my $cookies = HTTP::Cookies->new;
43              
44 0 0 0       if ($self->{logtoken}) {
    0          
45 0           $cookies->set_cookie(0, user => $self->{logtoken}, '/', $self->{host});
46              
47             } elsif ($self->{uid} && $self->{pass}) {
48 0           my $cookie = bakeUserCookie($self->{uid}, $self->{pass});
49 0           $cookies->set_cookie(0, user => $cookie, '/', $self->{host});
50              
51             } else {
52 0   0       my $cookie_file = $self->{cookie_file} || find_cookie_file();
53 0 0         if ($cookie_file) {
54 0           $cookies = HTTP::Cookies::Netscape->new;
55 0           $cookies->load($cookie_file);
56             }
57             }
58              
59 0           my $soap = SOAP::Lite->uri($uri)->proxy($proxy, cookie_jar => $cookies);
60 0           $self->{soap}{cache} = $soap;
61 0           return $soap;
62             }
63              
64              
65             sub find_cookie_file {
66 0   0 0 0   my $app = shift || 'Firefox';
67 0           my $file;
68 0 0 0       if ($^O eq 'MacOS' || $^O eq 'darwin') {
69 0           require Mac::Files;
70 0           Mac::Files->import(':DEFAULT');
71              
72 0           my($dir, $vref, $type);
73 0 0         if ($^O eq 'darwin') {
    0          
74 0           $vref = &kUserDomain;
75 0 0 0       if ($app eq 'Chimera' || $app eq 'Firefox') {
    0          
76 0           $type = &kApplicationSupportFolderType;
77             } elsif ($app eq 'Mozilla') {
78 0           $type = &kDomainLibraryFolderType;
79             }
80             } elsif ($^O eq 'MacOS') {
81 0           $vref = &kOnSystemDisk;
82 0           $type = &kDocumentsFolderType;
83             }
84              
85 0           $dir = FindFolder($vref, $type, &kDontCreateFolder);
86 0           $dir = catdir($dir, $app, 'Profiles');
87 0           for (0, 1) {
88 0 0         last if -e catfile($dir, 'cookies.txt');
89 0 0         opendir(my $dh, $dir) or die "Can't open $dir: $!";
90 0           $dir = catdir($dir, (grep !/^\./, readdir($dh))[0]);
91 0           closedir($dh);
92             }
93 0           $file = catfile($dir, 'cookies.txt');
94             }
95 0           return $file;
96             }
97              
98             sub bakeUserCookie {
99 0     0 0   my($uid, $pass) = @_;
100 0           my $cookie = $uid . '::' . md5_hex($pass);
101 0           $cookie =~ s/(.)/sprintf("%%%02x", ord($1))/ge;
  0            
102 0           $cookie =~ s/%/%25/g;
103 0           return $cookie;
104             }
105              
106             sub literal {
107 0     0 0   my($str) = @_;
108 0           $str =~ s/&/&/g;
109 0           $str =~ s/
110 0           $str =~ s/>/>/og;
111 0           return $str;
112             }
113              
114             sub fixparam {
115 0     0 0   my($str) = @_;
116 0           $str =~ s/([^$URI::unreserved])/$URI::Escape::escapes{$1}/og;
117 0           return $str;
118             }
119              
120             1;
121              
122             __END__