File: V2_0.pm

package info (click to toggle)
libvuser-google-api-perl 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 348 kB
  • sloc: perl: 3,176; makefile: 4
file content (1642 lines) | stat: -rw-r--r-- 40,650 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
package VUser::Google::ProvisioningAPI::V2_0;
use 5.008005;
use warnings;
use strict;

#(C) 2007 Randy Smith, perlstalker at vuser dot org
#(C) 2006 Johan Reinalda, johan at reinalda dot net

use vars qw($VERSION);

our $VERSION = '0.25';

use Carp;
use LWP::UserAgent qw(:strict);
use HTTP::Request qw(:strict);
use Encode;
use XML::Simple;

use Data::Dumper;

use base qw(VUser::Google::ProvisioningAPI);

use VUser::Google::ProvisioningAPI::V2_0::EmailListEntry;
use VUser::Google::ProvisioningAPI::V2_0::EmailListRecipientEntry;
use VUser::Google::ProvisioningAPI::V2_0::NicknameEntry;
use VUser::Google::ProvisioningAPI::V2_0::UserEntry;

our $APIVersion = '2.0';

#some constants
#web agent identification
use constant GOOGLEAGENT => "Google_ProvisioningAPI-perl/0.20";

#url for Google API token login
use constant GOOGLEHOST => 'www.google.com';
use constant GOOGLETOKENURL => 'https://www.google.com/accounts/ClientLogin';
use constant MAXTOKENAGE => 24 * 60 * 60;	#24 hours, see API docs

#base url to the Google REST API
use constant GOOGLEBASEURL => 'https://www.google.com/a/feeds/';

use constant GOOGLEAPPSSCHEMA => 'http://schemas.google.com/apps/2006';

use constant SUCCESSCODE => 'Success(2000)';
use constant FAILURECODE => 'Failure(2001)';

#some size constants
use constant MAXNAMELEN => 40;
use constant MAXUSERNAMELEN => 30;

sub DESTROY { };

# Preloaded methods go here.

=pod

=head1 NAME

VUser::Google::ProvisioningAPI::V2_0 - Perl module that implements version 2.0 of the Google Apps for Your Domain Provisioning API

=head1 SYNOPSIS

 use VUser::Google::ProvisioningAPI;
 my $google = new VUser::Google::ProvisioningAPI($domain, $admin, $passwd, '2.0');
 
 $google->CreateUser($userName, $givenName, $familyName, $password, $quotaMB);
 my $user = $google->RetrieveUser($userName);

=head1 REQUIREMENTS

VUser::Google::ProvisioningAPI requires the following modules to be installed:

=over

=item

C<LWP::UserAgent>

=item

C<HTTP::Request>

=item

C<Encode>

=item

C<XML::Simple>

=back

=head1 DESCRIPTION

VUser::Google::ProvisioningAPI provides a simple interface to the Google Apps for Your Domain Provisioning API.
It uses the C<LWP::UserAgent> module for the HTTP transport, and the C<HTTP::Request> module for the HTTP request and response.

=head2 Examples

Adding a user:

 use VUser::Google::ProvisioningAPI;
 my $google = VUser::Google::ProvisioningAPI->new('yourdomain.com',
					  'admin',
					  'your password',
					  '2.0');

 my $entry = $google->CreateUser('joeb', 'Joe', 'Blow', 'joespassword');
 if (defined $entry) {
   print $entry->User, " created\n";
 } else {
   die "Add failed: ".$google->{result}{reason};
 }

Updating a user:

 my $new_entry = VUser::Google::ProvisioningAPI::V2_0::UserEntry->new();
 $new_entry->Password('heresmynewpassword');
 $new_entry->GivenName('Joseph');
 my $entry = $google->UpdateUser('joeb', $new_entry);

Delete a user:

 my $rc = $google->DeleteUser('joeb');
 if (not $rc) {
   die "Can't delete user: ".$google->{result}{reason};
 }

=head1 CONSTRUCTOR

new ($domain, $admin, $adminpasswd)

This is the constructor for a new VUser::Google::ProvisioningAPI object.
$domain is the domain name registered with Google Apps For Your Domain,
$admin is an account in the above domain that has the right to manage that domain, and
$adminpassword is the password for that account.

Note that the constructor will NOT attempt to perform the 'ClientLogin' call to the Google Provisioning API (see below).
Authentication happens automatically when the first API call is performed. The token will be remembered for the duration of the object, and will be automatically refreshed as needed.
If you want to verify that you can get a valid token before performing any operations, follow the constructor with a call to IsAuthenticated() as such:

	print "Authentication OK\n" unless not $google->IsAuthenticated();

=cut

sub new
{
	#parse parameters, if any
	(@_ == 4) || croak 'Constructor takes 3 arguments: domain, admin, adminpassword';

	my $object = shift();
	my $class = ref($object) || $object;

	my $self = {
		#Google related variables
		domain => shift(),		#the Google hosted domain we are accessing
		admin => shift(),		#the account to use when authenticating
		password => shift(),	#the password to use when authenticating
		refreshtoken => 0,		#if set, will force a re-authentication
		authtoken => '',		#the authentication token returned from google
		authtime => 0,			#time when authentication happened; only valid for 24 hours
		requestcontent => '',	#the last http content posted to Google
		replyheaders => '',		#the http headers of the last reply
		replycontent => '',		#the http content of the last reply
		result => {},			#the resulting hash from the last reply data as parsed by XML::Simple
	
		#some other variables
		debug => 0,			#when turned on, will spit out debug info to STDERR
		
		#some statistics that are 'read-only'
		stats => {
			ctime =>  time,			#object creation time
			rtime => 0,				#time of last request
			requests => 0,			#number of API requests made
			success => 0,			#number of successes
			logins => 0,			#number of authentications performed
		}
	};
	#return object
	bless( $self, 'VUser::Google::ProvisioningAPI::V2_0');
	return $self;
	
}

=pod

=head1 METHODS

Below are all the methods available on the object. For the Google API specific methods, see the Google API documentation for more details.

When a request is properly handed by Google's API engine, the results of the
action are returned as the content of the request.

If the request fails (as determined by the C<HTTP::Request> method
is_success()), it could mean a couple of things. If it's a failure within
the Google API, the content will contain an XML encoded error message. All
other HTTP errors are still possible.

=head2 Checking Authentication

IsAuthenticated()

=over

will check if the object has been able to authenticate with Google's api engine, and get an authentication ticket.
Returns 1 if successful, 0 on failure. To see why it may fail, see the $@ variable, and the $google->{results}->{reason} hash, and parse the returned page (see the 'content' and 'header' variables.)

=back

=cut

#check if we are authenticated. If not, try to re-login
sub IsAuthenticated {

	#get object reference
	my $self = shift();

	if( $self->{refreshtoken} or ( (time - $self->{authtime}) > MAXTOKENAGE ) ) {
		return $self->Relogin();
	}
	#we are still okay!
	return 1;
}

=pod

Relogin()

=over

Performs a login if required. Relogin() will be called but the API methods
and IsAuthenticated(). You should not need to call this directly.

=back

=cut

#method used to (re)login to the API, either first time, or as token times out
sub Relogin
{
	
	#get object reference
	my $self = shift();

	$self->dprint("Relogin called\n");

	my $retval = 0;
	
	#adjust stats counter
	$self->{stats}->{logins}++;
	
	#clear last results
	$self->{replyheaders} = $self->{replycontent} = '';
	$self->{result} = {};
	
	# Create an LWP object to make the HTTP POST request
	my $lwp = LWP::UserAgent->new;

	if(defined($lwp)) {
		$lwp->agent(GOOGLEAGENT);
		$lwp->from($self->{admin}.'@'.$self->{domain});
		# Submit the request with values for
		# accountType, Email and Passwd variables.
		my $response = $lwp->post( GOOGLETOKENURL,
				[ 'accountType' => 'HOSTED',
				  'Email' => $self->{admin}.'@'.$self->{domain},
				  'Passwd' => $self->{password},
				  'service' => 'apps'
				]
			);
		#save reply page
		$self->{replyheaders} = $response->headers->as_string;
		$self->{replycontent} = $response->content;
	
		if ($response->is_success) {
			# Extract the authentication token from the response
			foreach my $line (split/\n/, $response->content) {
				#$self->dprint( "RECV'd: $line" );
				if ($line =~ m/^Auth=(.+)$/) {
					$self->{authtoken} = $1;
					$self->{authtime} = time;
					$self->dprint("Token found: $self->{authtoken}\n");
					#clear refresh
					$self->{refreshtoken} = 0;
					$retval = 1;
					last;
				}
			}
		}
		else {
			$self->dprint("Error in login: " . $response->status_line . "\n");
			$self->{result}->{reason} = "Error in login: " . $response->status_line;

		}
	} else {
		$self->dprint("Error getting lwp object: $!\n");
		$self->{result}->{reason} = "Error getting lwp object: $!";
	}
	return $retval;
}

#generic request routine that handles most functionality
#requires 3 arguments: Method, URL, Body
#Method is the HTTP method to use. ('GET', 'POST', etc)
#URL is the API URL to talk to.
#Body is the xml specific to the action.
# This is not used on 'GET' or 'DELETE' requests.
sub Request
{
	my $retval = 0;

	#get object reference
	my $self = shift();

	$self->dprint( "***REQUEST***\n");
	
	#clear last results
	$self->{replyheaders} = $self->{replycontent} = '';
	$self->{result} = {};
	
	if(@_ != 2 and @_ != 3) {
		$self->{result}->{reason} = 'Invalid number of arguments to request()';
		return 0;
	}
	
	#get parameters
	my($method,$url,$body) = @_;
	
	#$self->dprint( "Type: $type\nAction: $action\n$body\n");
	$self->dprint("Method: $method; URL: $url\n");
	$self->dprint("Body: $body\n") if $body;
	
	#keep some stats
	$self->{stats}->{requests}++;
	$self->{stats}->{rtime} = time;
	
	#check if we are authenticated to google
	if(!$self->IsAuthenticated()) {
		$self->dprint( "Error authenticating\n");
		return 0;
	}

	#standard XML pre and post segments
	# TODO: this changes in 2.0

	#properly encode it
	$body = encode('UTF-8',$body);

	#save the request content
	$self->{requestcontent} = $body;
	
	# Create an LWP object to make the HTTP POST request over
	my($ua) = LWP::UserAgent->new;
	if(!defined($ua)) {
		$self->dprint("Cannot create LWP::UserAgent object: $!\n");
		$self->{result}->{reason} = "Cannot create LWP::UserAgent object in request(): $!";
		return $retval;
	}
	
	#and create the request object where are we connecting to
	# v2.0 uses a diffent url based what's being done.
	# The API methods will construct the URL becuase action specific
	# information, such as domain and user, is embedded with it.
	# v2.0 use different methods depending on the action
	# It's up to the API methods to know which method to use
	my $req = HTTP::Request->new($method => $url);
	if(!defined($req)) {
		$self->dprint("Cannot create HTTP::Request object: $!\n");
		$self->{result}->{reason} = "Cannot create HTTP::Request object in request(): $!";
		return $retval;
	}
	
	#set some user agent variables
	$ua->agent( GOOGLEAGENT );
	$ua->from( '<' . $self->{admin}.'@'.$self->{domain} . '>');

	# Submit the request
	$req->header('Accept' => 'application/atom+xml');
	$req->header('Content-Type' => 'application/atom+xml');
	if ($body) {
	    $req->header('Content-Length' => length($body) );
	}
	$req->header('Connection' => 'Keep-Alive');
	$req->header('Host' => GOOGLEHOST);
	$req->header('Authorization' => 'GoogleLogin auth='.$self->{authtoken});
	#assign the data to the request
	# Perhaps if $method eq 'GET' or 'DELETE' would be better
	if ($body) {
	    $req->content($body);
	}
	
	#$self->dprint(Data::Dumper::Dumper($req));

	#execute the request
	my $response = $ua->request($req);
	$self->dprint(Data::Dumper::Dumper($response));
	#save reply page
	$self->{replyheaders} = $response->headers->as_string;
	$self->{replycontent} = $response->content;
	#check result
	if ($response->is_success) {
		$self->{stats}->{success}++;
		$self->dprint( "Success in post:\n");
		
		#delete all namespace elements to keep it simple (ie. remove "hs:")
		#this avoids the need to use XML::NameSpace
		# v2.0 uses a couple namespaces now, instead of just one.
		# I'm not sure that we can avoid using XML::NameSpace
		my $xml = decode('UTF-8', $response->content);
		#$xml =~ s/hs\://g;
		$self->dprint( $xml );
		
		if ($xml) {
		    #now go parse it using XML::Simple
		    my $simple = XML::Simple->new(ForceArray => 1);
		    #my $parser = XML::SAX::ParserFactory->new(Handler => $simple);
		    #$self->{result} = $parser->parse_string($xml);
		    $self->{result} = $simple->XMLin($xml);
		    # (OLD) $self->{result} = XMLin($xml,ForceArray => 0);
		    #include Data::Dumper above if you want to use this line:
		    $self->dprint( Dumper($self->{result}) );
		} else {
		    $self->{result} = {};
		}

		$self->dprint("Google API success!");
		$retval = 1;

	}
	else {
	    # OK. Funky issue. When trying to get a user that doesn't exist,
	    # Google throws a 400 error instead of returning a error document.

	    # Google has fun. If there is a problem with the request,
	    # google triggers a 400 error witch then fails on ->is_success.
	    # So, we need to check the content anyway to see if there is a
	    # reason for the failure.
	    $self->dprint("Google API failure!");
	    my $xml = decode('UTF-8', $response->content);
	    $self->dprint( $xml );
	    if ($xml) {
		my $simple = XML::Simple->new(ForceArray => 1);
		$self->{result} = $simple->XMLin($xml);
		$self->dprint( 'Error result: '.Dumper($self->{result}) );
	    }
	    if (defined ($self->{result}{error}[0]{reason})) {
		$@ = "Google API failure: "
		    .$self->{result}{error}[0]{errorCode}.' - '
		    .$self->{result}{error}[0]{reason};
		$self->dprint("$@\n");
		$self->{result}->{reason} = $@;
	    } else {
		$@ = "Google API failure: reason not found!";
		$self->dprint( "Error in post: " . $response->status_line . "\n");
		$self->{result}->{reason} = "Error in http post: " . $response->status_line;
	    }
	}
	#show full response for now
	#$self->dprint( "Headers:\n" . $response->headers->as_string);
	#foreach my $line (split/\n/, $response->content) {
	#	$self->dprint( "RECV'd:   $line\n");
	#}
	
	return $retval;
}

=pod

=head2 User Methods

These are the acutual API calls. These calls match up with the client
library methods described for the .Net and Java libraries.

=cut

### HOSTED ACCOUNT routines ###

=pod

CreateUser($userName, $givenName, $familyName, $password, $quota, $forceChange, $hashName)

=over

Creates a user in your Google Apps domain. The first four arguments are
required. The C<$quota> argument is optional and may not do anything unless
your agreement with Google allows you to change quotas.

If C<$forceChange> is true, the user will be required to change their
password after log in.

C<$hashName>, if set, must be I<sha-1> or I<md5>.

CreateUser() returns a C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> object if
the request was successful and C<undef> otherwise.

=back

=cut

sub CreateUser {
    my $self = shift;

    if (@_ < 4 and @_ > 7) {
	$self->dprint("CreateUser method requires 4 to 7 arguments\n");
	$self->{result}->{reason} = "CreateUser method requires 4 to 7 arguments";
	return undef;
    }

    my ($username, $given_name, $family_name, $password, $quotaMB, $forceChange, $hash_name) = @_;
    $forceChange = $forceChange? 1 : 0;
    if(defined $hash_name) {
      if(lc($hash_name) eq "sha-1") {
        $hash_name = "SHA-1";
      } elsif (lc($hash_name) eq 'md5') {
        $hash_name = "MD5";
      }
      else {
	  # Unset $hash_name if it's not a valid hash type
	  $hash_name = undef;
      }
    }

    my $body = $self->XMLPrefix;
    #LP:changePasswordAtNextLogin (todo)
    $body .= '<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/apps/2006#user"/>';
    $body .= "<apps:login userName=\"$username\" password=\"$password\" suspended=\"false\"";
    if(defined $hash_name) {
      $body .= " hashFunctionName=\"$hash_name\"";
    }
    if ($forceChange) {
	$body .= ' changePasswordAtNextLogin="true"';
    }
    $body .= "/>";
    $body .= "<apps:quota limit=\"$quotaMB\"/>" if defined $quotaMB; 
    $body .= "<apps:name familyName=\"$family_name\" givenName=\"$given_name\"/>";
    $body .= $self->XMLPostfix;

    if ($self->Request('POST',
		       GOOGLEBASEURL.$self->{domain}."/user/$APIVersion",
		       $body)) {
	my $entry = $self->buildUserEntry();
	return $entry;
    } else {
	return undef;
    }

    # Return UserEntry
}

=pod

RetrieveUser($userName)

=over

Get the passed user from Google. Returns a
C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> object.

=back

=cut

sub RetrieveUser {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("RetrieveUser method requires 1 argument\n");
	$self->{result}->{reason} = "RetrieveUser method requires 1 argument";
	return undef;
    }

    my $username = shift;
    my $url = GOOGLEBASEURL.$self->{domain}."/user/$APIVersion/$username";

    if ($self->Request('GET',$url)) {
	return $self->buildUserEntry();
    } else {
	return undef;
    }

    # Return UserEntry
}

=pod

RetrieveAllUsers()

=over

Returns a list of all users in your domain. The entries are
C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> objects.

=back

=cut

sub RetrieveAllUsers {
    my $self = shift;

    # Need to deal with google's pagination thing.
    my $last_page = 0;
    my $url = GOOGLEBASEURL.$self->{domain}."/user/$APIVersion";
    my @entries = ();
    while (not $last_page) {
	# It might be better to adjust this to use RetrievePageOfUsers()
	if ($self->Request('GET', $url)) {			   
	    foreach my $entry (@{ $self->{result}{'entry'} }) {
		push @entries, $self->buildUserEntry($entry);
	    }
	} else {
	    # There was some sort of error which caused the lookup to fail.
	    # This also means that if pages beyond the first fail, the entire
	    # dataset is discarded.
	    return undef;
	}
	$last_page = 1; # gets reset to 0 if there are more pages
	# Look through the links to see if there's another page.
	# A link with rel=next means that we have another page to look at.
	#
	# TODO: May be more efficient with a last; in the else but
	# I had problems with infinite loops while trying to get it
	# sorted out.
	foreach my $link (@{ $self->{result}{'link'} }) {
	    if ($link->{'rel'} eq 'next') {
		$url = $link->{'href'};
		$last_page = 0;
#	    } else {
#		$last_page = 1;
	    }
	}
    }
    return @entries;

    # Return list of UserEntries
}

=pod

RetrievePageOfUsers($startUser)

=over

Google Provisioning API 2.0 supports returning lists of users 100 at a time.
C<$startUser> is optional. When used, it will be the list will start at
that user. Otherwise, it will return the first 100 users.

RetrievePageOfUsers() returns a list of
C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> objects.

=back

=cut

sub RetrievePageOfUsers {
    my $self = shift;

    if (@_ > 1) {
	$self->dprint("RetrievePageOfUser method requires 0 or 1 argument\n");
	$self->{result}->{reason} = "RetrievePageOfUser method requires 0 or 1 argument";
	return undef;
    }

    my $start_username = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/user/$APIVersion";
    $url .= "?startUsername=$start_username" if defined $start_username;

    my @entries = ();
    if ($self->Request('GET', $url)) {			   
	foreach my $entry (@{ $self->{result}{'entry'} }) {
	    push @entries, $self->buildUserEntry($entry);
	}
    } else {
	# There was some sort of error which caused the lookup to fail.
	# This also means that if pages beyond the first fail, the entire
	# dataset is discarded.
	return undef;
    }

    # Return list of UserEntries
    return @entries;
}

=pod

UpdateUser($userName, $newUserEntry)

=over

C<$userName> is the mandatory name of the user account. C<$newUserEntry> is a
C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> object with the changes to the
account. You only need to set the elements of C<$newUserEntry> that are being
changed. B<Note:> According to the Google API docs, you should not set the
password unless you are actually changing the password.

=back

=cut

sub UpdateUser {
    my $self = shift;

    if (@_ != 2) {
	$self->dprint("UpdateUser method requires 2 arguments\n");
	$self->{result}->{reason} = "UpdateUser method requires 2 arguments";
	return undef;
    }

    my $username = shift;
    my $new_entry = shift; # G::P::V2_0::UserEntry

    my $body = $self->XMLPrefix;
    $body .= '<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/apps/2006#user"/>';
    if (defined ($new_entry->User)
	or defined ($new_entry->Password)
	or defined ($new_entry->isSuspended)
	or defined ($new_entry->changePasswordAtNextLogin)
	) {
	$body .= '<apps:login';
	if(defined $new_entry->{hashFunctionName}) {
	    $body .= ' hashFunctionName="'.$new_entry->{hashFunctionName}.'"';
	}
	$body .= ' userName="'.$new_entry->User.'"' if defined $new_entry->User;

	if (defined $new_entry->Password) {
	    my $passwd = $new_entry->Password;
	    # escape quotes
	    # See section 2.4 of http://www.w3.org/TR/xml/
	    #$passwd =~ s/\"/\\"/;
	    $passwd =~ s/\"/&quot;/;
	    $body .= ' password="'.$passwd.'"';
	}

	$body .= ' suspended="'.($new_entry->isSuspended? 'true' : 'false').'"';
	#LP:changePasswordAtNextLogin
	#print "too(".$new_entry->changePasswordAtNextLogin.")";
	$body .= ' changePasswordAtNextLogin="'.($new_entry->changePasswordAtNextLogin? 'true' : 'false').'"';
	$body .= '/>';
    }

    if (defined ($new_entry->FamilyName)
	or defined ($new_entry->GivenName)) {
	$body .= '<apps:name';
	$body .= ' familyName="'.$new_entry->FamilyName.'"' if defined $new_entry->FamilyName;
	$body .= ' givenName="'.$new_entry->GivenName.'"' if defined $new_entry->GivenName;
	$body .= '/>';
    }

    if (defined ($new_entry->Quota)) {
	$body .= '<apps:quota limit="'.$new_entry->Quota.'"/>';
    }

    $body .= $self->XMLPostfix;

    # The body has been contructed. We are 'Go' to make the request.
    if ($self->Request('PUT',
		       GOOGLEBASEURL.$self->{domain}."/user/$APIVersion/$username",
		       $body)) {
	my $entry = $self->buildUserEntry();
	return $entry;
    } else {
	return undef;
    }

    # Return UserEntry
}

=pod

SuspendUser($userName)

=over

C<$userName> is the name of the user that you want to suspend.

Returns a C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> object if successful.

=back

=cut

sub SuspendUser {
    my $self = shift;
    my $username = shift;

    my $entry = VUser::Google::ProvisioningAPI::V2_0::UserEntry->new();
    $entry->isSuspended(1);

    return $self->UpdateUser($username, $entry);
    
    # Return UserEntry
}

=pod

RestoreUser($userName)

=over

Unsuspend the user's account. C<$userName> is required.

Returns a C<VUser::Google::ProvisioningAPI::V2_0::UserEntry> object if successful.

=back

=cut

sub RestoreUser {
    my $self = shift;
    my $username = shift;

    my $entry = VUser::Google::ProvisioningAPI::V2_0::UserEntry->new();
    $entry->isSuspended(0);

    return $self->UpdateUser($username, $entry);

    # Return UserEntry
}

=pod

DeleteUser($userName)

=over

C<$userName> is the required user name to delete.

Returns '1' on success.

=back

=cut

sub DeleteUser {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("DeleteUser method requires 1 argument\n");
	$self->{result}->{reason} = "DeleteUser method requires 1 argument";
	return undef;
    }

    my $username = shift;

    if ($self->Request('DELETE',
		       GOOGLEBASEURL.$self->{domain}."/user/$APIVersion/$username")) {
	return 1;
    } else {
	return undef;
    }

    # Return undef
}

### NICKNAME routines ###

=pod

=head3 Nickname methods

CreateNickname($userName, $nickName)

=over

Creates a nickname (or alias) for a user. C<$userName> is the existing user
and C<$nickName> is the user's new nickname.

Returns a C<VUser::Google::ProvisioningAPI::V2_0::NicknameEntry> object on success.

=back

=cut

sub CreateNickname {
    my $self = shift;

    if (@_ != 2) {
	$self->dprint("CreateNickname method requires 2 arguments\n");
	$self->{result}->{reason} = "CreateNickname method requires 2 arguments";
	return undef;
    }

    my $username = shift;
    my $nickname = shift;

    my $body = $self->XMLPrefix;
    $body .= '<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/apps/2006#nickname"/>';
    $body .= "<apps:nickname name=\"$nickname\"/>";
    $body .= "<apps:login userName=\"$username\"/>";
    $body .= $self->XMLPostfix;

    if ($self->Request('POST',
		       GOOGLEBASEURL.$self->{domain}."/nickname/$APIVersion",
		       $body)) {
	return $self->buildNicknameEntry();
    } else {
	return undef;
    }

    # Return NicknameEntry
}

=pod

RetrieveNickname($nickName)

=over

Returns a C<VUser::Google::ProvisioningAPI::V2_0::NicknameEntry> if the C<$nickName>
exists.

=back

=cut

sub RetrieveNickname {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("RetrieveNickname method requires 1 argument\n");
	$self->{result}->{reason} = "RetrieveNickname method requires 1 argument";
	return undef;
    }

    my $nickname = shift;

    if ($self->Request('GET',
		       GOOGLEBASEURL.$self->{domain}."/nickname/$APIVersion/$nickname")) {
	return $self->buildNicknameEntry();
    } else {
	return undef;
    }
	
    # Return NicknameEntry
}

=pod

RetrieveNicknames($userName)

=over

Get all nicknames for C<$userName>.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::NicknameEntry> objects.

=back

=cut

sub RetrieveNicknames {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("RetrieveNicknames method requires 1 argument\n");
	$self->{result}->{reason} = "RetrieveNicknames method requires 1 argument";
	return undef;
    }

    my $username = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/nickname/$APIVersion?username=$username";
    my $last_page = 0;
    my @entries = ();
    # And we get to deal with funky pagination here, too.
    while (not $last_page) {
	if ($self->Request('GET', $url)) {
	    foreach my $entry (@{ $self->{result}{'entry'} }) {
		push @entries, $self->buildNicknameEntry($entry);
	    }
	} else {
	    return undef;
	}

	# Look through the links to see if there's another page.
	# A link with rel=next means that we have another page to look at.
	foreach my $link (@{ $self->{result}{'link'} }) {
	    if ($link->{'rel'} eq 'next') {
		$url = $link->{'href'};
		$last_page = 0;
	    } else {
		$last_page = 1;
	    }
	}
    }

    return @entries;

    # Return list of NicknameEntries
}

=pod

RetrieveAllNicknames()

=over

Get all of the nick names for your domain.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::NicknameEntry> objects.

=back

=cut

sub RetrieveAllNicknames {
    my $self = shift;


    my $url = GOOGLEBASEURL.$self->{domain}."/nickname/$APIVersion";
    my $last_page = 0;
    my @entries = ();
    # And we get to deal with funky pagination here, too.
    while (not $last_page) {
	if ($self->Request('GET', $url)) {
	    foreach my $entry (@{ $self->{result}{'entry'} }) {
		push @entries, $self->buildNicknameEntry($entry);
	    }
	} else {
	    return undef;
	}

	# Look through the links to see if there's another page.
	# A link with rel=next means that we have another page to look at.
	foreach my $link (@{ $self->{result}{'link'} }) {
	    if ($link->{'rel'} eq 'next') {
		$url = $link->{'href'};
		$last_page = 0;
	    } else {
		$last_page = 1;
	    }
	}
    }

    return @entries;

    # Return list of NicknameEntries
}

=pod

RetrievePageOfNicknames($startNick)

=over

Get 100 of the nick names for your domain. If C<$startNick> is defined,
the list will start with that nick name, otherwise, the first 100 nicks
will be returned.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::NicknameEntry> objects.

=back

=cut

sub RetrievePageOfNicknames {
    my $self = shift;
    my $start_nick = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/nickname/$APIVersion";
    $url .= "?startNickname=$start_nick" if defined $start_nick;
    my @entries = ();
    if ($self->Request('GET', $url)) {
	foreach my $entry (@{ $self->{result}{'entry'} }) {
	    push @entries, $self->buildNicknameEntry($entry);
	}
    } else {
	return undef;
    }

    return @entries;
    # Return list of NicknameEntries
}

=pod

DeleteNickname($nickName)

=over

Delete C<$nickName> from your domain. Returns 1 if the request succeeds.

=back

=cut

sub DeleteNickname {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("DeleteNickname method requires 1 argument\n");
	$self->{result}->{reason} = "DeleteNickname method requires 1 argument";
	return undef;
    }

    my $nickname = shift;

    if ($self->Request('DELETE',
		       GOOGLEBASEURL.$self->{domain}."/nickname/$APIVersion/$nickname")) {
	return 1;
    } else {
	return undef;
    }

    # Return undef
}

### EMAIL LIST routines ###

=pod

=head3 Email list methods

CreateEmailList($listName)

=over

Create an email list named C<$listName>.

Returns a C<VUser::Google::ProvisioningAPI::V2_0::EmailListEntry> on success.

=back

=cut

sub CreateEmailList {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("CreateEmailList method requires 1 argument\n");
	$self->{result}->{reason} = "CreateEmailList method requires 1 argument";
	return undef;
    }

    my $emaillist = shift;

    my $body = $self->XMLPrefix;
    $body .= '<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/apps/2006#emailList"/>';
    $body .= "<apps:emailList name=\"$emaillist\"/>";
    $body .= $self->XMLPostfix;

    if ($self->Request('POST',
		       GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion",
		       $body)) {
	my $entry = $self->buildEmailListEntry();
	return $entry;
    } else {
	return undef;
    }

    # Return EmailListEntry
}

=pod

RetrieveEmailLists($recipient)

=over

Get a list of all local email lists that C<$recipient> is subscribed to.
C<$recipient> is limited to users at your domain.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::EmailListEntry> objects.

=back

=cut

sub RetrieveEmailLists {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("RetrieveEmailLists method requires 1 argument\n");
	$self->{result}->{reason} = "RetrieveEmailLists method required 1 argument\n";
    }

    my $recipient = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion?recipient=$recipient";
    my $last_page = 0;
    my @entries = ();
    # Work with Google's pagination
    while (not $last_page) {
	if ($self->Request('GET', $url)) {
	    foreach my $entry (@{ $self->{result}{'entry'} }) {
		push @entries, $self->buildEmailListEntry($entry);
	    }
	} else {
	    return undef;
	}

	# Look for next page link
	foreach my $link (@{ $self->{result}{'link'} }) {
	    if ($link->{'rel'} eq 'next') {
		$url = $link->{'href'};
		$last_page = 0;
	    } else {
		$last_page = 1;
	    }
	}
    }

    # Return list of EmailListEntries
    return @entries;
}

=pod

RetrieveAllEmailLists()

=over

Get a list of all email lists for your domain.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::EmailListEntry> objects.

=back

=cut

sub RetrieveAllEmailLists {
    my $self = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion";
    my $last_page = 0;
    my @entries = ();
    # Work with Google's pagination
    while (not $last_page) {
	if ($self->Request('GET', $url)) {
	    foreach my $entry (@{ $self->{result}{'entry'} }) {
		push @entries, $self->buildEmailListEntry($entry);
	    }
	} else {
	    return undef;
	}

	# Look for next page link
	foreach my $link (@{ $self->{result}{'link'} }) {
	    if ($link->{'rel'} eq 'next') {
		$url = $link->{'href'};
		$last_page = 0;
	    } else {
		$last_page = 1;
	    }
	}
    }

    # Return list of EmailListEntries
    return @entries;
}

=pod

RetrievePageOfEmailLists($startList)

=over

Get a single page (100 lists) of email lists.

=back

=cut

sub RetrievePageOfEmailLists {
    my $self = shift;

    my $start_emaillist = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion";
    if ($start_emaillist) {
	$url .= "?startEmailListName=$start_emaillist";
    }
    my @entries = ();

    if ($self->Request('GET', $url)) {
	foreach my $entry (@{ $self->{result}{'entry'} }) {
	    push @entries, $self->buildEmailListEntry($entry);
	}
    } else {
	return undef;
    }

    # Return list of EmailListEntries
    return @entries;
}

=pod

DeleteEmailList($emailList)

=over

Delete C<$emailList> from your domain.

Returns 1 on success.

=back

=cut

sub DeleteEmailList {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("DeleteUser method requires 1 argument\n");
	$self->{result}->{reason} = "DeleteUser method requires 1 argument";
	return undef;
    }

    my $emaillist = shift;

    if ($self->Request('DELETE',
		       GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion/$emaillist")) {
	return 1;
    } else {
	return undef;
    }

    # Return undef
}

=pod

AddRecipientToEmailList($recipient, $emailList)

=over

Adds a recipient to a mail list. C<$recipient> is the address you want to
add and C<$emailList> is the list to add to.

Returns a C<VUser::Google::ProvisioningAPI::V2_0::EmailListRecipientEntry> object on
success.

=back

=cut

sub AddRecipientToEmailList {
    my $self = shift;

    if (@_ != 2) {
	$self->dprint("AddRecipientToEmailList method requires 2 argument\n");
	$self->{result}->{reason} = "AddRecipientToEmailList method requires 2 argument";
	return undef;
    }

    my $recipient = shift;
    my $emaillist = shift;

    my $body = $self->XMLPrefix;
    $body =~ s!>$! xmlns:gd="http://schemas.google.com/g/2005">!;
    $body .= '<atom:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/apps/2006#emailList.recipient"/>';
    $body .= "<gd:who xmlns=\"http://schemas.google.com/g/2005\" email=\"$recipient\"/>";
    $body .= $self->XMLPostfix;

    if ($self->Request('POST',
		       GOOGLEBASEURL.$self->{domain}
		       ."/emailList/$APIVersion/$emaillist/recipient",
		       $body)) {
	my $entry = $self->buildEmailListRecipientEntry();
	return $entry;
    } else {
	return undef;
    }

    # Return EmailListRecipientEntry
}

=pod

RetrieveAllRecipients($emailList)

=over

Get a list of the recipients of the specified email list.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::EmailListRecipientEntry> objects.

=back

=cut

sub RetrieveAllRecipients {
    my $self = shift;

    if (@_ != 1) {
	$self->dprint("RetrieceAllRecipients method requires 1 argument\n");
	$self->{result}->{reason} = "RetrieveAllRecipients method requires 1 argument";
	return undef;
    }

    my $emaillist = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion/$emaillist/recipient";
    my $last_page = 0;
    my @entries = ();
    # Google Pagination again
    while (not $last_page) {
	if ($self->Request('GET', $url)) {
	    foreach my $entry (@{ $self->{result}{'entry'} }) {
		my $entry = $self->buildEmailListRecipientEntry($entry);
		push @entries, $entry if $entry;
	    }
	} else {
	    return undef;
	}

	foreach my $link (@{ $self->{result}{'link'} }) {
	    if ($link->{'rel'} eq 'next') {
		$url = $link->{'href'};
		$last_page = 0;
	    } else {
		$last_page = 1;
	    }
	}
    }

    # Return list of EmailListRecipientEntries
    return @entries;
}

=pod

RetrievePageOfRecipients($emailList, $startRecpt)

=over

Get a page of recipients for that given list (C<$emailList)> starting with
C<$startRecpt> or the beginning if C<$startRecpt> is not defined.

Returns a list of C<VUser::Google::ProvisioningAPI::V2_0::EmailListRecipientEntry> objects.

=back

=cut

sub RetrievePageOfRecipients {
    my $self = shift;

    if (@_ != 2) {
	$self->dprint("RetrievePageOfRecipients method requires 2 arguments\n");
	$self->{result}->{reason} = "RetrievePageOfRecipients method requires 2 arguments";
	return undef;
    }

    my $emaillist = shift;
    my $start_rcpt = shift;

    my $url = GOOGLEBASEURL.$self->{domain}."/emailList/$APIVersion/$emaillist/recipient";
    if ($start_rcpt) {
	$url .= "?startRecipient=$start_rcpt";
    }
    my @entries = ();
    
    if ($self->Request('GET', $url)) {
	foreach my $entry (@{ $self->{result}{'entry'} }) {
	    push @entries, $self->buildEmailListRecipientEntry();
	}
    } else {
	return undef;
    }

    # Return list of EmailListRecipientEntries
    return @entries;
}

=pod

RemoveRecipientFromEmailList($recipient, $emailList)

=over

Remove C<$recipient> from the given email list (C<$emailList>).

Returns 1 in success.

=back

=cut

sub RemoveRecipientFromEmailList {
    my $self = shift;

    if (@_ != 2) {
	$self->dprint("RemoveRecipientFromEmailList method requires 2 arguments\n");
	$self->{result}->{reason} = "RemoveRecipientFromEmailList method requires 2 arguments";
	return undef;
    }

    my $recipient = shift;
    my $emaillist = shift;

    if ($self->Request('DELETE',
		       GOOGLEBASEURL.$self->{domain}
		       ."/emailList/$APIVersion/$emaillist/recipient/$recipient")) {
	return 1;
    } else {
	return undef;
    }

    # Return undef
}

### Private methods

sub XMLPrefix {
    my $pre = '<?xml version="1.0" encoding="UTF-8"?>';
    $pre .= '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"';
    $pre .= ' xmlns:apps="'.GOOGLEAPPSSCHEMA.'">';

    return $pre;
}

sub XMLPostfix {
    return '</atom:entry>';
}

sub buildUserEntry {
    my $self = shift;
    my $xml = shift || $self->{result};

    my $entry = VUser::Google::ProvisioningAPI::V2_0::UserEntry->new();

    $entry->User($xml->{'apps:login'}[0]{'userName'});

    if ($xml->{'apps:login'}[0]{'suspended'}) {
	if ($xml->{'apps:login'}[0]{'suspended'} eq 'true') {
	    $entry->isSuspended(1);
	} else {
	    $entry->isSuspended(0);
	}
    }

    #LP: changePasswordAtNextLogin
    if ($xml->{'apps:login'}[0]{'changePasswordAtNextLogin'}) {
	if ($xml->{'apps:login'}[0]{'changePasswordAtNextLogin'} eq 'true') {
	    $entry->changePasswordAtNextLogin(1);
	} else {
	    $entry->changePasswordAtNextLogin(0);
	}
    }    

    $entry->FamilyName($xml->{'apps:name'}[0]{'familyName'});
    $entry->GivenName($xml->{'apps:name'}[0]{'givenName'});
    $entry->Quota($xml->{'apps:quota'}[0]{'limit'});

    return $entry;
}

sub buildNicknameEntry {
    my $self = shift;
    my $xml = shift || $self->{result};

    my $entry = VUser::Google::ProvisioningAPI::V2_0::NicknameEntry->new();

    $entry->User($xml->{'apps:login'}[0]{'userName'});
 
    # Odd parser problem:
    #  <apps:nickname name='test1'/>
    # yeilds:
    #  'apps:nickname' => { 'test1' => {} },
    #$entry->Nickname($xml->{'apps:nickname'}[0]{'name'});
    # This is an exceptionally ugly hack to work around the parser issue
    # above.
    $entry->Nickname((keys %{$xml->{'apps:nickname'}})[0]);

    return $entry;
}

sub buildEmailListEntry {
    my $self = shift;
    my $xml = shift || $self->{'result'};

    my $entry = VUser::Google::ProvisioningAPI::V2_0::EmailListEntry->new();

    # This seems to have the same problem as nicknames.
    #$entry->EmailList($xml->{'apps:emailList'}[0]{'name'});
    $entry->EmailList((keys %{$xml->{'apps:emailList'}})[0]);

    return $entry;
}

sub buildEmailListRecipientEntry {
    my $self = shift;
    my $xml = shift || $self->{'result'};

    my $entry = VUser::Google::ProvisioningAPI::V2_0::EmailListRecipientEntry->new();

    $entry->Who($xml->{'gd:who'}[0]{'email'});

    return $entry;
}

=pod

=head1 ACCESSING RESULTING DATA

Most API calls return an object so that you don't have to screw around with the
XML data. The parsed XML (by XML::Simple) is available in C<$google->{result}>.

=head1 EXPORT

None by default.


=head1 SEE ALSO

The perldocs for VUser::Google::ProvisioningAPI::V2_0::UserEntry;
VUser::Google::ProvisioningAPI::V2_0::NicknameEntry;
VUser::Google::ProvisioningAPI::V2_0::EmailListEntry;
and VUser::Google::ProvisioningAPI::V2_0::EmailListRecipientEntry.

The official Google documentation can be found at
http://code.google.com/apis/apps-for-your-domain/google_apps_provisioning_api_v2.0_reference.html

http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html

For support, see the Google Group at
http://groups.google.com/group/apps-for-your-domain-apis

For additional support specific to this modules, email me at johan at reinalda dot net.

=head1 AUTHOR

Johan Reinalda, johan at reinalda dot net
Randy Smith, perlstalker at vuser dot org

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006 by Johan Reinalda, johan at reinalda dot net
Copyright (C) 2007 by Randy Smith, perlstalker at vuser dot org

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.5 or,
at your option, any later version of Perl 5 you may have available.

If you make useful modification, kindly consider emailing then to me for inclusion in a future version of this module.

=cut

1;