File Coverage

blib/lib/Etherpad/API.pm
Criterion Covered Total %
statement 38 848 4.4
branch 5 326 1.5
condition 5 9 55.5
subroutine 11 54 20.3
pod 46 46 100.0
total 105 1283 8.1


line stmt bran cond sub pod time code
1             package Etherpad::API;
2 1     1   15067 use strict;
  1         1  
  1         34  
3 1     1   583 use LWP::UserAgent;
  1         34465  
  1         28  
4 1     1   7 use URI::Escape;
  1         5  
  1         53  
5 1     1   621 use JSON::XS;
  1         4209  
  1         55  
6 1     1   5 use Carp qw(carp);
  1         1  
  1         40  
7              
8              
9             BEGIN {
10 1     1   3 use Exporter ();
  1         1  
  1         15  
11 1     1   3 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         1  
  1         77  
12 1     1   2 $VERSION = '1.2.10.2';
13 1         7 @ISA = qw(Exporter);
14             #Give a hoot don't pollute, do not export more than needed by default
15 1         1 @EXPORT = qw();
16 1         1 @EXPORT_OK = qw();
17 1         5684 %EXPORT_TAGS = ();
18             }
19              
20             #################### main pod documentation begin ###################
21              
22              
23             =head1 NAME
24              
25             Etherpad::API - Access Etherpad Lite API easily
26              
27             =head1 SYNOPSIS
28              
29             use Etherpad::API;
30             my $ec = Etherpad::API->new({
31             url => "http://pad.example.com",
32             apikey => "teirnausetsraunieti",
33             user => "apiuser",
34             password => "apipassword"
35             });
36              
37             $ec->create_pad('new_pad_name');
38              
39             =head1 DESCRIPTION
40              
41             This is a client for the Etherpad Lite HTTP API.
42              
43             The Etherpad API currently supported is the 1.2.10 (Etherpad version: 1.5.1)
44              
45             Please note that this module now uses the Etherpad API version number for versioning with a revision number (for bugfixes).
46              
47             =head1 USAGE
48              
49             =cut
50              
51             #################### main pod documentation end ###################
52              
53              
54              
55              
56             #################### subroutine header begin ####################
57              
58             =head2 new
59              
60             Usage : my $ec = Etherpad::API->new({ url => "http://pad.example.com", apikey => "secretapikey" });
61             Purpose : Constructor
62             Returns : An Etherpad::API object
63             Argument : An mandatory hash reference, containing at least two keys:
64             url : mandatory, the epl URL (trailing slashes will be removed)
65             apikey : mandatory, the epl API key
66             user : optional, the user for epl authentication
67             password : optional, the password for epl authentication
68             proxy : optional, hash reference which can contain two keys: http and https.
69             These are the settings if you are behind a http(s) proxy
70              
71             =cut
72              
73             #################### subroutine header end ####################
74              
75             sub new {
76 4     4 1 393 my ($class, $parameters) = @_;
77 4 100 100     21 return undef unless (defined($parameters->{url}) && defined($parameters->{apikey}));
78              
79 1 50       7 $parameters->{url} =~ s#/+$## if (defined($parameters->{url}));
80 1 50 33     4 if (defined($parameters->{user}) && defined($parameters->{password})) {
81 0         0 $parameters->{url} =~ s#^(https?://)#$1$parameters->{user}:$parameters->{password}@#;
82             }
83              
84 1         9 $parameters->{ua} = LWP::UserAgent->new;
85              
86 1 50       2374 if (defined($parameters->{proxy})) {
87 0 0       0 $parameters->{ua}->proxy('http', $parameters->{proxy}->{http}) if (defined($parameters->{proxy}->{http}));
88 0 0       0 $parameters->{ua}->proxy('https', $parameters->{proxy}->{https}) if (defined($parameters->{proxy}->{https}));
89             }
90              
91 1   33     6 my $self = bless ($parameters, ref ($class) || $class);
92 1         2 return $self;
93             }
94              
95              
96             #################### subroutine header begin ####################
97              
98             =head2 apikey
99              
100             Usage : $ec->apikey();
101             Purpose : Get the apikey
102             Returns : The apikey
103              
104             =cut
105              
106             #################### subroutine header end ####################
107              
108             sub apikey {
109 1     1 1 2 my $self = shift;
110 1         4 return $self->{apikey};
111             }
112              
113              
114             #################### subroutine header begin ####################
115              
116             =head2 url
117              
118             Usage : $ec->url();
119             Purpose : Get the epl URL
120             Returns : The epl URL
121              
122             =cut
123              
124             #################### subroutine header end ####################
125              
126             sub url {
127 1     1 1 493 my $self = shift;
128 1         9 return $self->{url};
129             }
130              
131              
132             #################################################################
133             =head2 Groups
134              
135             Pads can belong to a group. The padID of grouppads is starting with a groupID like g.asdfasdfasdfasdf$test
136              
137             See http://etherpad.org/doc/v1.5.1/#index_groups
138              
139             =cut
140             #################################################################
141              
142             #################### subroutine header begin ####################
143              
144             =head3 create_group
145              
146             Usage : $ec->create_group();
147             Purpose : Creates a new group
148             Returns : The new group ID
149             Argument : None
150             See : http://etherpad.org/doc/v1.5.1/#index_creategroup
151              
152             =cut
153              
154             #################### subroutine header end ####################
155              
156             sub create_group {
157 0     0 1   my $self = shift;
158              
159 0           my $api = '1';
160 0           my $method = 'createGroup';
161              
162 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
163 0           my $response = $self->{ua}->get($request);
164 0 0         if ($response->is_success) {
165 0           my $hash = decode_json $response->decoded_content;
166 0 0         if ($hash->{code} == 0) {
167 0           return $hash->{data}->{groupID};
168             } else {
169 0           carp $hash->{message};
170 0           return undef;
171             }
172             } else {
173 0           carp $response->status_line;
174 0           return undef;
175             }
176             }
177              
178              
179             #################### subroutine header begin ####################
180              
181             =head3 create_group_if_not_exists_for
182              
183             Usage : $ec->create_group_if_not_exists_for('groupMapper');
184             Purpose : This functions helps you to map your application group ids to epl group ids
185             Returns : The epl group id
186             Argument : Your application group id
187             See : http://etherpad.org/doc/v1.5.1/#index_creategroupifnotexistsfor_groupmapper
188              
189             =cut
190              
191             #################### subroutine header end ####################
192              
193              
194             sub create_group_if_not_exists_for {
195 0     0 1   my $self = shift;
196 0           my $group_mapper = shift;
197              
198 0 0         unless (defined($group_mapper)) {
199 0           carp 'Please provide a group id';
200 0           return undef;
201             }
202              
203 0           my $api = '1';
204 0           my $method = 'createGroupIfNotExistsFor';
205              
206 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
207 0           $request .= '&groupMapper=' . $group_mapper;
208 0           my $response = $self->{ua}->get($request);
209 0 0         if ($response->is_success) {
210 0           my $hash = decode_json $response->decoded_content;
211 0 0         if ($hash->{code} == 0) {
212 0           return $hash->{data}->{groupID};
213             } else {
214 0           carp $hash->{message};
215 0           return undef;
216             }
217             }
218             else {
219 0           carp $response->status_line;
220 0           return undef;
221             }
222             }
223              
224              
225             #################### subroutine header begin ####################
226              
227             =head3 delete_group
228              
229             Usage : $ec->delete_group('groupId');
230             Purpose : Deletes a group
231             Returns : 1 if it succeeds
232             Argument : The id of the group you want to delete
233             See : http://etherpad.org/doc/v1.5.1/#index_deletegroup_groupid
234              
235             =cut
236              
237             #################### subroutine header end ####################
238              
239              
240             sub delete_group {
241 0     0 1   my $self = shift;
242 0           my $group_id = shift;
243              
244 0 0         unless (defined($group_id)) {
245 0           carp 'Please provide a group id';
246 0           return undef;
247             }
248              
249 0           my $api = '1';
250 0           my $method = 'deleteGroup';
251              
252 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
253 0           $request .= '&groupID=' . $group_id;
254 0           my $response = $self->{ua}->get($request);
255 0 0         if ($response->is_success) {
256 0           my $hash = decode_json $response->decoded_content;
257 0 0         if ($hash->{code} == 0) {
258 0           return 1;
259             } else {
260 0           carp $hash->{message};
261 0           return undef;
262             }
263             }
264             else {
265 0           carp $response->status_line;
266 0           return undef;
267             }
268             }
269              
270              
271             #################### subroutine header begin ####################
272              
273             =head3 list_pads
274              
275             Usage : $ec->list_pads('groupId');
276             Purpose : Returns all pads of this group
277             Returns : An array or an array reference (depending on the context) which contains the pad ids
278             Argument : The id of the group from which you want the pads
279             See : http://etherpad.org/doc/v1.5.1/#index_listpads_groupid
280              
281             =cut
282              
283             #################### subroutine header end ####################
284              
285              
286             sub list_pads {
287 0     0 1   my $self = shift;
288 0           my $group_id = shift;
289              
290 0 0         unless (defined($group_id)) {
291 0           carp 'Please provide a group id';
292 0           return undef;
293             }
294              
295 0           my $api = '1';
296 0           my $method = 'listPads';
297              
298 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
299 0           $request .= '&groupID=' . $group_id;
300 0           my $response = $self->{ua}->get($request);
301 0 0         if ($response->is_success) {
302 0           my $hash = decode_json $response->decoded_content;
303 0 0         if ($hash->{code} == 0) {
304 0 0         return (wantarray) ? @{$hash->{data}->{padIDs}}: $hash->{data}->{padIDs};
  0            
305             } else {
306 0           carp $hash->{message};
307 0           return undef;
308             }
309             }
310             else {
311 0           carp $response->status_line;
312 0           return undef;
313             }
314             }
315              
316              
317             #################### subroutine header begin ####################
318              
319             =head3 create_group_pad
320              
321             Usage : $ec->create_group_pad('groupID', 'padName' [, 'text'])
322             Purpose : Creates a new pad in this group
323             Returns : 1 if it succeeds
324             Argument : The group id, the pad name, optionally takes the pad's initial text
325             See : http://etherpad.org/doc/v1.5.1/#index_creategrouppad_groupid_padname_text
326              
327             =cut
328              
329             #################### subroutine header end ####################
330              
331              
332             sub create_group_pad {
333 0     0 1   my $self = shift;
334 0           my $group_id = shift;
335 0           my $pad_name = shift;
336 0           my $text = shift;
337              
338 0 0         unless (defined($pad_name)) {
339 0           carp 'Please provide at least 2 arguments : the group id and the pad name';
340             return undef
341 0           }
342              
343 0           my $api = '1';
344 0           my $method = 'createGroupPad';
345              
346 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
347 0           $request .= '&groupID=' . $group_id . '&padName=' . $pad_name;
348 0 0         $request .= '&text=' . uri_escape($text) if (defined($text));
349 0           my $response = $self->{ua}->get($request);
350 0 0         if ($response->is_success) {
351 0           my $hash = decode_json $response->decoded_content;
352 0 0         if ($hash->{code} == 0) {
353 0           return 1;
354             } else {
355 0           carp $hash->{message};
356 0           return undef;
357             }
358             }
359             else {
360 0           carp $response->status_line;
361 0           return undef;
362             }
363             }
364              
365              
366             #################### subroutine header begin ####################
367              
368             =head3 list_all_groups
369              
370             Usage : $ec->list_all_groups()
371             Purpose : Lists all existing groups
372             Returns : An array or an array reference (depending on the context) which contains the groups ids
373             Argument : None
374             See : http://etherpad.org/doc/v1.5.1/#index_listallgroups
375              
376             =cut
377              
378             #################### subroutine header end ####################
379              
380              
381             sub list_all_groups {
382 0     0 1   my $self = shift;
383              
384 0           my $api = '1.1';
385 0           my $method = 'listAllGroups';
386              
387 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
388 0           my $response = $self->{ua}->get($request);
389 0 0         if ($response->is_success) {
390 0           my $hash = decode_json $response->decoded_content;
391 0 0         if ($hash->{code} == 0) {
392 0 0         return (wantarray) ? @{$hash->{data}->{groupIDs}}: $hash->{data}->{groupIDs};
  0            
393             } else {
394 0           carp $hash->{message};
395 0           return undef;
396             }
397             }
398             else {
399 0           carp $response->status_line;
400 0           return undef;
401             }
402             }
403              
404              
405             #################################################################
406             =head2 Author
407              
408             These authors are bound to the attributes the users choose (color and name).
409              
410             See http://etherpad.org/doc/v1.5.1/#index_author
411              
412             =cut
413             #################################################################
414              
415             #################### subroutine header begin ####################
416              
417             =head3 create_author
418              
419             Usage : $ec->create_author(['name'])
420             Purpose : Creates a new author
421             Returns : The new author ID
422             Argument : Optionally takes a string as argument : the new author's name
423             See : http://etherpad.org/doc/v1.5.1/#index_createauthor_name
424              
425             =cut
426              
427             #################### subroutine header end ####################
428              
429              
430             sub create_author {
431 0     0 1   my $self = shift;
432 0           my $name = shift;
433              
434 0           my $api = '1';
435 0           my $method = 'createAuthor';
436              
437 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
438 0 0         $request .= '&name=' . $name if (defined($name));
439 0           my $response = $self->{ua}->get($request);
440 0 0         if ($response->is_success) {
441 0           my $hash = decode_json $response->decoded_content;
442 0 0         if ($hash->{code} == 0) {
443 0           return $hash->{data}->{authorID};
444             } else {
445 0           carp $hash->{message};
446 0           return undef;
447             }
448             }
449             else {
450 0           carp $response->status_line;
451 0           return undef;
452             }
453             }
454              
455              
456             #################### subroutine header begin ####################
457              
458             =head3 create_author_if_not_exists_for
459              
460             Usage : $ec->create_author_if_not_exists_for(authorMapper [, name])
461             Purpose : This functions helps you to map your application author ids to epl author ids
462             Returns : The epl author ID
463             Argument : Your application author ID (mandatory) and optionally the epl author name
464             See : http://etherpad.org/doc/v1.5.1/#index_createauthorifnotexistsfor_authormapper_name
465              
466             =cut
467              
468             #################### subroutine header end ####################
469              
470              
471             sub create_author_if_not_exists_for {
472 0     0 1   my $self = shift;
473 0           my $author_mapper = shift;
474 0           my $name = shift;
475              
476 0 0         unless (defined($author_mapper)) {
477 0           carp 'Please provide your application author id';
478 0           return undef;
479             }
480              
481 0           my $api = '1';
482 0           my $method = 'createAuthorIfNotExistsFor';
483              
484 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
485 0           $request .= '&authorMapper=' . $author_mapper;
486 0 0         $request .= '&name=' . $name if (defined($name));
487 0           my $response = $self->{ua}->get($request);
488 0 0         if ($response->is_success) {
489 0           my $hash = decode_json $response->decoded_content;
490 0 0         if ($hash->{code} == 0) {
491 0           return $hash->{data}->{authorID};
492             } else {
493 0           carp $hash->{message};
494 0           return undef;
495             }
496             }
497             else {
498 0           carp $response->status_line;
499 0           return undef;
500             }
501             }
502              
503              
504             #################### subroutine header begin ####################
505              
506             =head3 list_pads_of_author
507              
508             Usage : $ec->list_pads_of_author('authorID')
509             Purpose : Returns an array of all pads this author contributed to
510             Returns : An array or an array reference depending on the context, containing the pads names
511             Argument : An epl author ID
512             See : http://etherpad.org/doc/v1.5.1/#index_listpadsofauthor_authorid
513              
514             =cut
515              
516             #################### subroutine header end ####################
517              
518              
519             sub list_pads_of_author {
520 0     0 1   my $self = shift;
521 0           my $author_id = shift;
522              
523 0 0         unless (defined($author_id)) {
524 0           carp 'Please provide an author id';
525 0           return undef;
526             }
527              
528 0           my $api = '1';
529 0           my $method = 'listPadsOfAuthor';
530              
531 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
532 0           $request .= '&authorID=' . $author_id;
533 0           my $response = $self->{ua}->get($request);
534 0 0         if ($response->is_success) {
535 0           my $hash = decode_json $response->decoded_content;
536 0 0         if ($hash->{code} == 0) {
537 0 0         return (wantarray) ? @{$hash->{data}->{padIDs}}: $hash->{data}->{padIDs};
  0            
538             } else {
539 0           carp $hash->{message};
540 0           return undef;
541             }
542             }
543             else {
544 0           carp $response->status_line;
545 0           return undef;
546             }
547             }
548              
549              
550             #################### subroutine header begin ####################
551              
552             =head3 get_author_name
553              
554             Usage : $ec->get_author_name('authorID')
555             Purpose : Returns the Author Name of the author
556             Returns : The author name
557             Argument : The epl author ID
558             See : http://etherpad.org/doc/v1.5.1/#index_getauthorname_authorid
559              
560             =cut
561              
562             #################### subroutine header end ####################
563              
564              
565             sub get_author_name {
566 0     0 1   my $self = shift;
567 0           my $author_id = shift;
568              
569 0 0         unless (defined($author_id)) {
570 0           carp 'Please provide an author id';
571 0           return undef;
572             }
573              
574 0           my $api = '1.1';
575 0           my $method = 'getAuthorName';
576              
577 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
578 0           $request .= '&authorID=' . $author_id;
579 0           my $response = $self->{ua}->get($request);
580 0 0         if ($response->is_success) {
581 0           my $hash = decode_json $response->decoded_content;
582 0 0         if ($hash->{code} == 0) {
583             # Change in the API, without documentation
584 0 0         my $data = (ref($hash->{data}) eq 'HASH') ? $hash->{data}->{authorName} : $hash->{data};
585 0           return $data;
586             } else {
587 0           carp $hash->{message};
588 0           return undef;
589             }
590             } else {
591 0           carp $response->status_line;
592 0           return undef;
593             }
594             }
595              
596              
597             #################################################################
598             =head2 Session
599              
600             Sessions can be created between a group and an author. This allows an author to access more than one group. The sessionID will be set as a cookie to the client and is valid until a certain date. The session cookie can also contain multiple comma-seperated sessionIDs, allowing a user to edit pads in different groups at the same time. Only users with a valid session for this group, can access group pads. You can create a session after you authenticated the user at your web application, to give them access to the pads. You should save the sessionID of this session and delete it after the user logged out.
601              
602             See http://etherpad.org/doc/v1.5.1/#index_session
603              
604             =cut
605             #################################################################
606              
607             #################### subroutine header begin ####################
608              
609             =head3 create_session
610              
611             Usage : $ec->create_session('groupID', 'authorID', 'validUntil')
612             Purpose : Creates a new session. validUntil is an unix timestamp in seconds
613             Returns : The epl session ID
614             Argument : An epl group ID, an epl author ID and an valid unix timestamp (the session validity end date)
615             See : http://etherpad.org/doc/v1.5.1/#index_createsession_groupid_authorid_validuntil
616              
617             =cut
618              
619             #################### subroutine header end ####################
620              
621              
622             sub create_session {
623 0     0 1   my $self = shift;
624 0           my $group_id = shift;
625 0           my $author_id = shift;
626 0           my $valid_until = shift;
627              
628 0 0         unless (defined($valid_until)) {
629 0           carp 'Please provide 3 arguments : the group id, the author id and a valid unix timestamp';
630 0           return undef;
631             }
632 0 0         unless ($valid_until =~ m/\d+/) {
633 0           carp 'Please provide a *valid* unix timestamp as third argument';
634 0           return undef;
635             }
636              
637 0           my $api = '1';
638 0           my $method = 'createSession';
639              
640 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
641 0           $request .= '&groupID=' . $group_id . '&authorID=' . $author_id . '&validUntil=' . $valid_until;
642 0           my $response = $self->{ua}->get($request);
643 0 0         if ($response->is_success) {
644 0           my $hash = decode_json $response->decoded_content;
645 0 0         if ($hash->{code} == 0) {
646 0           return $hash->{data}->{sessionID};
647             } else {
648 0           carp $hash->{message};
649 0           return undef;
650             }
651             }
652             else {
653 0           carp $response->status_line;
654 0           return undef;
655             }
656             }
657              
658              
659             #################### subroutine header begin ####################
660              
661             =head3 delete_session
662              
663             Usage : $ec->delete_session('sessionID')
664             Purpose : Deletes a session
665             Returns : 1 if it succeeds
666             Argument : An epl session ID
667             See : http://etherpad.org/doc/v1.5.1/#index_deletesession_sessionid
668              
669             =cut
670              
671             #################### subroutine header end ####################
672              
673              
674             sub delete_session {
675 0     0 1   my $self = shift;
676 0           my $session_id = shift;
677              
678 0 0         unless (defined($session_id)) {
679 0           carp 'Please provide a session id';
680 0           return undef;
681             }
682              
683 0           my $api = '1';
684 0           my $method = 'deleteSession';
685              
686 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
687 0           $request .= '&sessionID=' . $session_id;
688 0           my $response = $self->{ua}->get($request);
689 0 0         if ($response->is_success) {
690 0           my $hash = decode_json $response->decoded_content;
691 0 0         if ($hash->{code} == 0) {
692 0           return 1;
693             } else {
694 0           carp $hash->{message};
695 0           return undef;
696             }
697             }
698             else {
699 0           carp $response->status_line;
700 0           return undef;
701             }
702             }
703              
704              
705             #################### subroutine header begin ####################
706              
707             =head3 get_session_info
708              
709             Usage : $ec->get_session_info('sessionID')
710             Purpose : Returns informations about a session
711             Returns : A hash reference, containing 3 keys : authorID, groupID and validUntil
712             Argument : An epl session ID
713             See : http://etherpad.org/doc/v1.5.1/#index_getsessioninfo_sessionid
714              
715             =cut
716              
717             #################### subroutine header end ####################
718              
719              
720             sub get_session_info {
721 0     0 1   my $self = shift;
722 0           my $session_id = shift;
723              
724 0 0         unless (defined($session_id)) {
725 0           carp 'Please provide a session id';
726 0           return undef;
727             }
728              
729 0           my $api = '1';
730 0           my $method = 'getSessionInfo';
731              
732 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
733 0           $request .= '&sessionID=' . $session_id;
734 0           my $response = $self->{ua}->get($request);
735 0 0         if ($response->is_success) {
736 0           my $hash = decode_json $response->decoded_content;
737 0 0         if ($hash->{code} == 0) {
738 0           return $hash->{data};
739             } else {
740 0           carp $hash->{message};
741 0           return undef;
742             }
743             }
744             else {
745 0           carp $response->status_line;
746 0           return undef;
747             }
748             }
749              
750              
751             #################### subroutine header begin ####################
752              
753             =head3 list_sessions_of_group
754              
755             Usage : $ec->list_sessions_of_group('groupID')
756             Purpose : Returns all sessions of a group
757             Returns : Returns a hash reference, which keys are sessions ID and values are sessions infos (see get_session_info)
758             Argument : An epl group ID
759             See : http://etherpad.org/doc/v1.5.1/#index_listsessionsofgroup_groupid
760              
761             =cut
762              
763             #################### subroutine header end ####################
764              
765              
766             sub list_sessions_of_group {
767 0     0 1   my $self = shift;
768 0           my $group_id = shift;
769              
770 0 0         unless (defined($group_id)) {
771 0           carp 'Please provide a group id';
772 0           return undef;
773             }
774              
775 0           my $api = '1';
776 0           my $method = 'listSessionsOfGroup';
777              
778 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
779 0           $request .= '&groupID=' . $group_id;
780 0           my $response = $self->{ua}->get($request);
781 0 0         if ($response->is_success) {
782 0           my $hash = decode_json $response->decoded_content;
783 0 0         if ($hash->{code} == 0) {
784 0           return $hash->{data};
785             } else {
786 0           carp $hash->{message};
787 0           return undef;
788             }
789             }
790             else {
791 0           carp $response->status_line;
792 0           return undef;
793             }
794             }
795              
796              
797             #################### subroutine header begin ####################
798              
799             =head3 list_sessions_of_author
800              
801             Usage : $ec->list_sessions_of_author('authorID')
802             Purpose : Returns all sessions of an author
803             Returns : Returns a hash reference, which keys are sessions ID and values are sessions infos (see get_session_info)
804             Argument : An epl group ID
805             See : http://etherpad.org/doc/v1.5.1/#index_listsessionsofauthor_authorid
806              
807             =cut
808              
809             #################### subroutine header end ####################
810              
811              
812             sub list_sessions_of_author {
813 0     0 1   my $self = shift;
814 0           my $author_id = shift;
815              
816 0 0         unless (defined($author_id)) {
817 0           carp 'Please provide an author id';
818 0           return undef;
819             }
820              
821 0           my $api = '1';
822 0           my $method = 'listSessionsOfAuthor';
823              
824 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
825 0           $request .= '&authorID=' . $author_id;
826 0           my $response = $self->{ua}->get($request);
827 0 0         if ($response->is_success) {
828 0           my $hash = decode_json $response->decoded_content;
829 0 0         if ($hash->{code} == 0) {
830 0           return $hash->{data};
831             } else {
832 0           carp $hash->{message};
833 0           return undef;
834             }
835             }
836             else {
837 0           carp $response->status_line;
838 0           return undef;
839             }
840             }
841              
842              
843             #################################################################
844             =head2 Pad Content
845              
846             Pad content can be updated and retrieved through the API.
847              
848             See http://etherpad.org/doc/v1.5.1/#index_pad_content
849              
850             =cut
851             #################################################################
852              
853             #################### subroutine header begin ####################
854              
855             =head3 get_text
856              
857             Usage : $ec->get_text('padID', ['rev'])
858             Purpose : Returns the text of a pad
859             Returns : A string, containing the text of the pad
860             Argument : Takes a pad ID (mandatory) and optionally a revision number
861             See : http://etherpad.org/doc/v1.5.1/#index_gettext_padid_rev
862              
863             =cut
864              
865             #################### subroutine header end ####################
866              
867              
868             sub get_text {
869 0     0 1   my $self = shift;
870 0           my $pad_id = shift;
871 0           my $rev = shift;
872              
873 0 0         unless (defined($pad_id)) {
874 0           carp 'Please provide at least a pad id';
875 0           return undef;
876             }
877              
878 0           my $api = '1';
879 0           my $method = 'getText';
880              
881 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
882 0           $request .= '&padID=' . $pad_id;
883 0 0         $request .= '&rev=' . $rev if (defined($rev));
884 0           my $response = $self->{ua}->get($request);
885 0 0         if ($response->is_success) {
886 0           my $hash = decode_json $response->decoded_content;
887 0 0         if ($hash->{code} == 0) {
888 0           return $hash->{data}->{text};
889             } else {
890 0           carp $hash->{message};
891 0           return undef;
892             }
893             }
894             else {
895 0           carp $response->status_line;
896 0           return undef;
897             }
898             }
899              
900              
901             #################### subroutine header begin ####################
902              
903             =head3 set_text
904              
905             Usage : $ec->set_text('padID', 'text')
906             Purpose : Sets the text of a pad
907             Returns : 1 if it succeeds
908             Argument : Takes a pad ID and the text you want to set (both mandatory)
909             See : http://etherpad.org/doc/v1.5.1/#index_settext_padid_text
910              
911             =cut
912              
913             #################### subroutine header end ####################
914              
915              
916             sub set_text {
917 0     0 1   my $self = shift;
918 0           my $pad_id = shift;
919 0           my $text = shift;
920              
921 0 0         unless (defined($text)) {
922 0           carp 'Please provide 2 arguments : a pad id and a text';
923 0           return undef;
924             }
925              
926 0           my $api = '1';
927 0           my $method = 'setText';
928              
929 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
930 0           $request .= '&padID=' . $pad_id . '&text=' . uri_escape($text);
931 0           my $response = $self->{ua}->get($request);
932 0 0         if ($response->is_success) {
933 0           my $hash = decode_json $response->decoded_content;
934 0 0         if ($hash->{code} == 0) {
935 0           return 1;
936             } else {
937 0           carp $hash->{message};
938 0           return undef;
939             }
940             }
941             else {
942 0           carp $response->status_line;
943 0           return undef;
944             }
945             }
946              
947              
948             #################### subroutine header begin ####################
949              
950             =head3 get_html
951              
952             Usage : $ec->get_html('padID', ['rev'])
953             Purpose : Returns the text of a pad formatted as html
954             Returns : A string, containing the text of the pad formatted as html
955             Argument : Takes a pad ID (mandatory) and optionally a revision number
956             See : http://etherpad.org/doc/v1.5.1/#index_gethtml_padid_rev
957              
958             =cut
959              
960             #################### subroutine header end ####################
961              
962              
963             sub get_html {
964 0     0 1   my $self = shift;
965 0           my $pad_id = shift;
966 0           my $rev = shift;
967              
968 0 0         unless (defined($pad_id)) {
969 0           carp 'Please provide at least a pad id';
970 0           return undef;
971             }
972              
973 0           my $api = '1';
974 0           my $method = 'getHTML';
975              
976 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
977 0           $request .= '&padID=' . $pad_id;
978 0 0         $request .= '&rev=' . $rev if (defined($rev));
979 0           my $response = $self->{ua}->get($request);
980 0 0         if ($response->is_success) {
981 0           my $hash = decode_json $response->decoded_content;
982 0 0         if ($hash->{code} == 0) {
983 0           return $hash->{data}->{html};
984             } else {
985 0           carp $hash->{message};
986 0           return undef;
987             }
988             }
989             else {
990 0           carp $response->status_line;
991 0           return undef;
992             }
993             }
994              
995              
996             #################### subroutine header begin ####################
997              
998             =head3 set_html
999              
1000             Usage : $ec->set_html('padID', 'html')
1001             Purpose : Sets the text of a pad based on HTML, HTML must be well formed.
1002             Returns : 1 if it succeeds
1003             Argument : Takes a pad ID and the HTML code you want to set (both mandatory)
1004             See : http://etherpad.org/doc/v1.5.1/#index_sethtml_padid_html
1005              
1006             =cut
1007              
1008             #################### subroutine header end ####################
1009              
1010              
1011             sub set_html {
1012 0     0 1   my $self = shift;
1013 0           my $pad_id = shift;
1014 0           my $html = shift;
1015              
1016 0 0         unless (defined($html)) {
1017 0           carp 'Please provide 2 arguments : a pad id and a HTML code';
1018 0           return undef;
1019             }
1020              
1021 0           my $api = '1';
1022 0           my $method = 'setHTML';
1023              
1024 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1025 0           $request .= '&padID=' . $pad_id . '&html=' . uri_escape($html);
1026 0           my $response = $self->{ua}->get($request);
1027 0 0         if ($response->is_success) {
1028 0           my $hash = decode_json $response->decoded_content;
1029 0 0         if ($hash->{code} == 0) {
1030 0           return 1;
1031             } else {
1032 0           carp $hash->{message};
1033 0           return undef;
1034             }
1035             }
1036             else {
1037 0           carp $response->status_line;
1038 0           return undef;
1039             }
1040             }
1041              
1042              
1043             #################### subroutine header begin ####################
1044              
1045             =head3 get_attribute_pool
1046              
1047             Usage : $ec->get_attribute_pool('padID')
1048             Purpose : Returns the attribute pool of a pad
1049             Returns : A hash reference, containing 3 keys
1050             * numToAttrib, containing a hash reference, which keys are integers and contents are array references
1051             * attribToNum, containing a hash reference, which keys are string and contents are integers
1052             * nextNum, which content is an integer
1053             Argument : Takes a pad ID (mandatory)
1054             See : http://etherpad.org/doc/v1.5.1/#index_getattributepool_padid
1055              
1056             =cut
1057              
1058             #################### subroutine header end ####################
1059              
1060              
1061             sub get_attribute_pool {
1062 0     0 1   my $self = shift;
1063 0           my $pad_id = shift;
1064              
1065 0 0         unless (defined($pad_id)) {
1066 0           carp 'Please provide a pad id';
1067 0           return undef;
1068             }
1069              
1070 0           my $api = '1.2.8';
1071 0           my $method = 'getAttributePool';
1072              
1073 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1074 0           $request .= '&padID=' . $pad_id;
1075 0           my $response = $self->{ua}->get($request);
1076 0 0         if ($response->is_success) {
1077 0           my $hash = decode_json $response->decoded_content;
1078 0 0         if ($hash->{code} == 0) {
1079 0           return $hash->{data}->{pool};
1080             } else {
1081 0           carp $hash->{message};
1082 0           return undef;
1083             }
1084             }
1085             else {
1086 0           carp $response->status_line;
1087 0           return undef;
1088             }
1089             }
1090              
1091              
1092             #################### subroutine header begin ####################
1093              
1094             =head3 get_revision_changeset
1095              
1096             Usage : $ec->get_revision_changeset
1097             Purpose : Get the changeset at a given revision, or last revision if 'rev' is not defined
1098             Returns : A string, representing an etherpad changeset
1099             Argument : Takes a pad ID (mandatory) and optionally a revision number
1100             See : http://etherpad.org/doc/v1.5.1/#index_getrevisionchangeset_padid_rev
1101              
1102             =cut
1103              
1104             #################### subroutine header end ####################
1105              
1106              
1107             sub get_revision_changeset {
1108 0     0 1   my $self = shift;
1109 0           my $pad_id = shift;
1110 0           my $rev = shift;
1111              
1112 0 0         unless (defined($pad_id)) {
1113 0           carp 'Please provide at least a pad id';
1114 0           return undef;
1115             }
1116              
1117 0           my $api = '1.2.8';
1118 0           my $method = 'getRevisionChangeset';
1119              
1120 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1121 0           $request .= '&padID=' . $pad_id;
1122 0 0         $request .= '&rev=' . $rev if (defined($rev));
1123 0           my $response = $self->{ua}->get($request);
1124 0 0         if ($response->is_success) {
1125 0           my $hash = decode_json $response->decoded_content;
1126 0 0         if ($hash->{code} == 0) {
1127 0           return $hash->{data};
1128             } else {
1129 0           carp $hash->{message};
1130 0           return undef;
1131             }
1132             }
1133             else {
1134 0           carp $response->status_line;
1135 0           return undef;
1136             }
1137             }
1138              
1139              
1140             #################### subroutine header begin ####################
1141              
1142             =head3 create_diff_html
1143              
1144             Usage : $ec->create_diff_html
1145             Purpose : Returns an object of diffs from 2 points in a pad
1146             Returns : A hash reference which keys are
1147             * html, which content is a string representing the diff between the two revisions
1148             * authors, which content is an array reference of authors
1149             Argument : Takes a pad ID, a revision number to start and a revision number to end. All arguments are mandatory
1150             See : http://etherpad.org/doc/v1.5.1/#index_creatediffhtml_padid_startrev_endrev
1151              
1152             =cut
1153              
1154             #################### subroutine header end ####################
1155              
1156              
1157             sub create_diff_html {
1158 0     0 1   my $self = shift;
1159 0           my $pad_id = shift;
1160 0           my $start_rev = shift;
1161 0           my $end_rev = shift;
1162              
1163 0 0         unless (defined($pad_id)) {
1164 0           carp 'Please provide a pad id, a start_rev and an end_rev';
1165 0           return undef;
1166             }
1167              
1168 0 0         unless (defined($start_rev)) {
1169 0           carp 'Please provide a start_rev and an end_rev';
1170 0           return undef;
1171             }
1172              
1173 0 0         unless (defined($end_rev)) {
1174 0           carp 'Please provide an end_rev';
1175 0           return undef;
1176             }
1177              
1178 0           my $api = '1.2.7';
1179 0           my $method = 'createDiffHTML';
1180              
1181 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1182 0           $request .= '&padID=' . $pad_id . '&startRev=' . $start_rev . '&endRev=' . $end_rev;
1183 0           my $response = $self->{ua}->get($request);
1184 0 0         if ($response->is_success) {
1185 0           my $hash = decode_json $response->decoded_content;
1186 0 0         if ($hash->{code} == 0) {
1187 0           return $hash->{data};
1188             } else {
1189 0           carp $hash->{message};
1190 0           return undef;
1191             }
1192             }
1193             else {
1194 0           carp $response->status_line;
1195 0           return undef;
1196             }
1197             }
1198              
1199              
1200             #################################################################
1201             =head2 Chat
1202              
1203             See http://etherpad.org/doc/v1.5.1/#index_chat
1204              
1205             =cut
1206             #################################################################
1207              
1208             #################### subroutine header begin ####################
1209              
1210             =head3 get_chat_history
1211              
1212             Usage : $ec->get_chat_history('padID' [, start, end])
1213             Purpose : Returns
1214             - a part of the chat history, when start and end are given
1215             - the whole chat history, when no extra parameters are given
1216             Returns : An array or an array reference, depending of the context, containing hash references with 4 keys :
1217             - text => text of the message
1218             - userId => epl user id
1219             - time => unix timestamp of the message
1220             - userName => epl user name
1221             Argument : Takes a pad ID (mandatory) and optionally the start and the end numbers of the messages you want.
1222             The start number can't be higher than or equal to the current chatHead. The first chat message is number 0.
1223             If you specify a start but not an end, all messages will be returned.
1224             See : http://etherpad.org/doc/v1.5.1/#index_getchathistory_padid_start_end
1225              
1226             =cut
1227              
1228             #################### subroutine header end ####################
1229              
1230              
1231             sub get_chat_history {
1232 0     0 1   my $self = shift;
1233 0           my $pad_id = shift;
1234 0           my $start = shift;
1235 0           my $end = shift;
1236              
1237 0 0         unless (defined($pad_id)) {
1238 0           carp 'Please provide at least a pad id';
1239 0           return undef;
1240             }
1241              
1242 0           my $api = '1.2.7';
1243 0           my $method = 'getChatHistory';
1244              
1245 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1246 0           $request .= '&padID=' . $pad_id;
1247 0 0         $request .= '&start=' . $start if (defined($start));
1248 0 0         $request .= '&end=' . $end if (defined($end));
1249 0           my $response = $self->{ua}->get($request);
1250 0 0         if ($response->is_success) {
1251 0           my $hash = decode_json $response->decoded_content;
1252 0 0         if ($hash->{code} == 0) {
1253 0 0         return (wantarray) ? @{$hash->{data}->{messages}}: $hash->{data}->{messages};
  0            
1254             } else {
1255 0           carp $hash->{message};
1256 0           return undef;
1257             }
1258             }
1259             else {
1260 0           carp $response->status_line;
1261 0           return undef;
1262             }
1263             }
1264              
1265              
1266             #################### subroutine header begin ####################
1267              
1268             =head3 get_chat_head
1269              
1270             Usage : $ec->get_chat_head('padID')
1271             Purpose : Returns the chatHead (last number of the last chat-message) of the pad
1272             Returns : The last chat-message number. -1 if there is no chat message
1273             Argument : A pad ID
1274             See : http://etherpad.org/doc/v1.5.1/#index_getchathead_padid
1275              
1276             =cut
1277              
1278             #################### subroutine header end ####################
1279              
1280              
1281             sub get_chat_head {
1282 0     0 1   my $self = shift;
1283 0           my $pad_id = shift;
1284              
1285 0 0         unless (defined($pad_id)) {
1286 0           carp 'Please provide a pad id';
1287 0           return undef;
1288             }
1289              
1290 0           my $api = '1.2.7';
1291 0           my $method = 'getChatHead';
1292              
1293 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1294 0           $request .= '&padID=' . $pad_id;
1295 0           my $response = $self->{ua}->get($request);
1296 0 0         if ($response->is_success) {
1297 0           my $hash = decode_json $response->decoded_content;
1298 0 0         if ($hash->{code} == 0) {
1299 0           return $hash->{data}->{chatHead};
1300             } else {
1301 0           carp $hash->{message};
1302 0           return undef;
1303             }
1304             }
1305             else {
1306 0           carp $response->status_line;
1307 0           return undef;
1308             }
1309             }
1310              
1311              
1312             #################################################################
1313             =head2 Pad
1314              
1315             Group pads are normal pads, but with the name schema GROUPID$PADNAME. A security manager controls access of them and its forbidden for normal pads to include a $ in the name.
1316              
1317             See http://etherpad.org/doc/v1.5.1/#index_pad
1318              
1319             =cut
1320             #################################################################
1321              
1322             #################### subroutine header begin ####################
1323              
1324             =head3 create_pad
1325              
1326             Usage : $ec->create_pad('padID' [, 'text'])
1327             Purpose : Creates a new (non-group) pad. Note that if you need to create a group Pad, you should call create_group_pad.
1328             Returns : 1 if it succeeds
1329             Argument : Takes a pad ID (mandatory) and optionally a text to fill the pad
1330             See : http://etherpad.org/doc/v1.5.1/#index_createpad_padid_text
1331              
1332             =cut
1333              
1334             #################### subroutine header end ####################
1335              
1336              
1337             sub create_pad {
1338 0     0 1   my $self = shift;
1339 0           my $pad_id = shift;
1340 0           my $text = shift;
1341              
1342 0 0         unless (defined($pad_id)) {
1343 0           carp 'Please provide at least a pad id';
1344 0           return undef;
1345             }
1346              
1347 0           my $api = '1';
1348 0           my $method = 'createPad';
1349              
1350 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1351 0           $request .= '&padID=' . $pad_id;
1352 0 0         $request .= '&text=' . $text if (defined($text));
1353 0           my $response = $self->{ua}->get($request);
1354 0 0         if ($response->is_success) {
1355 0           my $hash = decode_json $response->decoded_content;
1356 0 0         if ($hash->{code} == 0) {
1357 0           return 1;
1358             } else {
1359 0           carp $hash->{message};
1360 0           return undef;
1361             }
1362             }
1363             else {
1364 0           carp $response->status_line;
1365 0           return undef;
1366             }
1367             }
1368              
1369              
1370             #################### subroutine header begin ####################
1371              
1372             =head3 get_revisions_count
1373              
1374             Usage : $ec->get_revisions_count('padID')
1375             Purpose : Returns the number of revisions of this pad
1376             Returns : The number of revisions
1377             Argument : A pad ID
1378             See : http://etherpad.org/doc/v1.5.1/#index_getrevisionscount_padid
1379              
1380             =cut
1381              
1382             #################### subroutine header end ####################
1383              
1384              
1385             sub get_revisions_count {
1386 0     0 1   my $self = shift;
1387 0           my $pad_id = shift;
1388              
1389 0 0         unless (defined($pad_id)) {
1390 0           carp 'Please provide a pad id';
1391 0           return undef;
1392             }
1393              
1394 0           my $api = '1';
1395 0           my $method = 'getRevisionsCount';
1396              
1397 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1398 0           $request .= '&padID=' . $pad_id;
1399 0           my $response = $self->{ua}->get($request);
1400 0 0         if ($response->is_success) {
1401 0           my $hash = decode_json $response->decoded_content;
1402 0 0         if ($hash->{code} == 0) {
1403 0           return $hash->{data}->{revisions};
1404             } else {
1405 0           carp $hash->{message};
1406 0           return undef;
1407             }
1408             }
1409             else {
1410 0           carp $response->status_line;
1411 0           return undef;
1412             }
1413             }
1414              
1415              
1416             #################### subroutine header begin ####################
1417              
1418             =head3 get_users_count
1419              
1420             Usage : $ec->get_users_count('padID')
1421             Purpose : Returns the number of user that are currently editing this pad
1422             Returns : The number of users
1423             Argument : A pad ID
1424             See : http://etherpad.org/doc/v1.5.1/#index_paduserscount_padid
1425              
1426             =cut
1427              
1428             #################### subroutine header end ####################
1429              
1430              
1431             sub get_users_count {
1432 0     0 1   my $self = shift;
1433 0           my $pad_id = shift;
1434              
1435 0 0         unless (defined($pad_id)) {
1436 0           carp 'Please provide a pad id';
1437 0           return undef;
1438             }
1439              
1440 0           my $api = '1';
1441 0           my $method = 'padUsersCount';
1442 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1443 0           $request .= '&padID=' . $pad_id;
1444 0           my $response = $self->{ua}->get($request);
1445 0 0         if ($response->is_success) {
1446 0           my $hash = decode_json $response->decoded_content;
1447 0 0         if ($hash->{code} == 0) {
1448 0           return $hash->{data}->{padUsersCount};
1449             } else {
1450 0           carp $hash->{message};
1451 0           return undef;
1452             }
1453             }
1454             else {
1455 0           carp $response->status_line;
1456 0           return undef;
1457             }
1458             }
1459              
1460              
1461             #################### subroutine header begin ####################
1462              
1463             =head3 pad_users
1464              
1465             Usage : $ec->pad_users('padID')
1466             Purpose : Returns the list of users that are currently editing this pad
1467             Returns : An array or an array reference, depending of the context, containing hash references with 3 keys : colorId, name and timestamp
1468             Argument : A pad ID
1469             See : http://etherpad.org/doc/v1.5.1/#index_padusers_padid
1470              
1471             =cut
1472              
1473             #################### subroutine header end ####################
1474              
1475              
1476             sub pad_users {
1477 0     0 1   my $self = shift;
1478 0           my $pad_id = shift;
1479              
1480 0 0         unless (defined($pad_id)) {
1481 0           carp 'Please provide a pad id';
1482 0           return undef;
1483             }
1484              
1485 0           my $api = '1.1';
1486 0           my $method = 'padUsers';
1487              
1488 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1489 0           $request .= '&padID=' . $pad_id;
1490 0           my $response = $self->{ua}->get($request);
1491 0 0         if ($response->is_success) {
1492 0           my $hash = decode_json $response->decoded_content;
1493 0 0         if ($hash->{code} == 0) {
1494 0 0         return (wantarray) ? @{$hash->{data}->{padUsers}} : $hash->{data}->{padUsers};
  0            
1495             } else {
1496 0           carp $hash->{message};
1497 0           return undef;
1498             }
1499             }
1500             else {
1501 0           carp $response->status_line;
1502 0           return undef;
1503             }
1504             }
1505              
1506              
1507             #################### subroutine header begin ####################
1508              
1509             =head3 delete_pad
1510              
1511             Usage : $ec->delete_pad('padID')
1512             Purpose : Deletes a pad
1513             Returns : 1 if it succeeds
1514             Argument : A pad ID
1515             See : http://etherpad.org/doc/v1.5.1/#index_deletepad_padid
1516              
1517             =cut
1518              
1519             #################### subroutine header end ####################
1520              
1521              
1522             sub delete_pad {
1523 0     0 1   my $self = shift;
1524 0           my $pad_id = shift;
1525              
1526 0 0         unless (defined($pad_id)) {
1527 0           carp 'Please provide a pad id';
1528 0           return undef;
1529             }
1530              
1531 0           my $api = '1';
1532 0           my $method = 'deletePad';
1533              
1534 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1535 0           $request .= '&padID=' . $pad_id;
1536 0           my $response = $self->{ua}->get($request);
1537 0 0         if ($response->is_success) {
1538 0           my $hash = decode_json $response->decoded_content;
1539 0 0         if ($hash->{code} == 0) {
1540 0           return 1;
1541             } else {
1542 0           carp $hash->{message};
1543 0           return undef;
1544             }
1545             }
1546             else {
1547 0           carp $response->status_line;
1548 0           return undef;
1549             }
1550             }
1551              
1552              
1553             #################### subroutine header begin ####################
1554              
1555             =head3 copy_pad
1556              
1557             Usage : $ec->copy_pad('sourceID', 'destinationID' [, 1])
1558             Purpose : Copies a pad with full history and chat. If force is true and the destination pad exists, it will be overwritten
1559             Returns : 1 if it succeeds
1560             Argument : A source pad ID
1561             A destination pad ID
1562             A force flag : a value which is true or false, in perl interpretation (for example; 0 and '' are false, 1, 2 and 'foo' are true)
1563             See : http://etherpad.org/doc/v1.5.1/#index_copypad_sourceid_destinationid_force_false
1564              
1565             =cut
1566              
1567             #################### subroutine header end ####################
1568              
1569              
1570             sub copy_pad {
1571 0     0 1   my $self = shift;
1572 0           my $source_id = shift;
1573 0           my $destination_id = shift;
1574 0           my $force = shift;
1575              
1576 0 0         unless (defined($source_id)) {
1577 0           carp 'Please provide a source pad id and a destination pad id';
1578 0           return undef;
1579             }
1580              
1581 0 0         unless (defined($destination_id)) {
1582 0           carp 'Please provide a destination pad id';
1583 0           return undef;
1584             }
1585              
1586 0 0         $force = ($force) ? 'true' : 'false' if (defined($force));
    0          
1587              
1588 0           my $api = '1.2.8';
1589 0           my $method = 'copyPad';
1590              
1591 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1592 0           $request .= '&sourceID=' . $source_id . '&destinationID=' . $destination_id;
1593 0 0         $request .= '&force=' . $force if (defined($force));
1594 0           my $response = $self->{ua}->get($request);
1595 0 0         if ($response->is_success) {
1596 0           my $hash = decode_json $response->decoded_content;
1597 0 0         if ($hash->{code} == 0) {
1598 0           return 1;
1599             } else {
1600 0           carp $hash->{message};
1601 0           return undef;
1602             }
1603             }
1604             else {
1605 0           carp $response->status_line;
1606 0           return undef;
1607             }
1608             }
1609              
1610              
1611             #################### subroutine header begin ####################
1612              
1613             =head3 move_pad
1614              
1615             Usage : $ec->move_pad('sourceID', 'destinationID' [, 1])
1616             Purpose : Moves a pad. If force is true and the destination pad exists, it will be overwritten
1617             Returns : 1 if it succeeds
1618             Argument : A source pad ID
1619             A destination pad ID
1620             A force flag : a value which is true or false, in perl interpretation (for example; 0 and '' are false, 1, 2 and 'foo' are true)
1621             See : http://etherpad.org/doc/v1.5.1/#index_movepad_sourceid_destinationid_force_false
1622              
1623             =cut
1624              
1625             #################### subroutine header end ####################
1626              
1627              
1628             sub move_pad {
1629 0     0 1   my $self = shift;
1630 0           my $source_id = shift;
1631 0           my $destination_id = shift;
1632 0           my $force = shift;
1633              
1634 0 0         unless (defined($source_id)) {
1635 0           carp 'Please provide a source pad id and a destination pad id';
1636 0           return undef;
1637             }
1638              
1639 0 0         unless (defined($destination_id)) {
1640 0           carp 'Please provide a destination pad id';
1641 0           return undef;
1642             }
1643              
1644 0 0         $force = ($force) ? 'true' : 'false' if (defined($force));
    0          
1645              
1646 0           my $api = '1.2.8';
1647 0           my $method = 'movePad';
1648              
1649 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1650 0           $request .= '&sourceID=' . $source_id . '&destinationID=' . $destination_id;
1651 0 0         $request .= '&force=' . $force if (defined($force));
1652 0           my $response = $self->{ua}->get($request);
1653 0 0         if ($response->is_success) {
1654 0           my $hash = decode_json $response->decoded_content;
1655 0 0         if ($hash->{code} == 0) {
1656 0           return 1;
1657             } else {
1658 0           carp $hash->{message};
1659 0           return undef;
1660             }
1661             }
1662             else {
1663 0           carp $response->status_line;
1664 0           return undef;
1665             }
1666             }
1667              
1668              
1669             #################### subroutine header begin ####################
1670              
1671             =head3 get_read_only_id
1672              
1673             Usage : $ec->get_read_only_id('padID')
1674             Purpose : Returns the read only link of a pad
1675             Returns : A string
1676             Argument : A pad ID
1677             See : http://etherpad.org/doc/v1.5.1/#index_getreadonlyid_padid
1678              
1679             =cut
1680              
1681             #################### subroutine header end ####################
1682              
1683              
1684             sub get_read_only_id {
1685 0     0 1   my $self = shift;
1686 0           my $pad_id = shift;
1687              
1688 0 0         unless (defined($pad_id)) {
1689 0           carp 'Please provide a pad id';
1690 0           return undef;
1691             }
1692              
1693 0           my $api = '1';
1694 0           my $method = 'getReadOnlyID';
1695              
1696 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1697 0           $request .= '&padID=' . $pad_id;
1698 0           my $response = $self->{ua}->get($request);
1699 0 0         if ($response->is_success) {
1700 0           my $hash = decode_json $response->decoded_content;
1701 0 0         if ($hash->{code} == 0) {
1702 0           return $hash->{data}->{readOnlyID};
1703             } else {
1704 0           carp $hash->{message};
1705 0           return undef;
1706             }
1707             }
1708             else {
1709 0           carp $response->status_line;
1710 0           return undef;
1711             }
1712             }
1713              
1714              
1715             #################### subroutine header begin ####################
1716              
1717             =head3 get_pad_id
1718              
1719             Usage : $ec->get_pad_id('readOnlyID')
1720             Purpose : Returns the id of a pad which is assigned to the readOnlyID
1721             Returns : A string
1722             Argument : A read only ID
1723             See : http://etherpad.org/doc/v1.5.1/#index_getpadid_readonlyid
1724              
1725             =cut
1726              
1727             #################### subroutine header end ####################
1728              
1729              
1730             sub get_pad_id {
1731 0     0 1   my $self = shift;
1732 0           my $read_only_id = shift;
1733              
1734 0 0         unless (defined($read_only_id)) {
1735 0           carp 'Please provide a read only id';
1736 0           return undef;
1737             }
1738              
1739 0           my $api = '1.2.10';
1740 0           my $method = 'getPadID';
1741              
1742 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1743 0           $request .= '&padID=' . $read_only_id;
1744 0           my $response = $self->{ua}->get($request);
1745 0 0         if ($response->is_success) {
1746 0           my $hash = decode_json $response->decoded_content;
1747 0 0         if ($hash->{code} == 0) {
1748 0           return $hash->{data}->{padID};
1749             } else {
1750 0           carp $hash->{message};
1751 0           return undef;
1752             }
1753             }
1754             else {
1755 0           carp $response->status_line;
1756 0           return undef;
1757             }
1758             }
1759              
1760              
1761             #################### subroutine header begin ####################
1762              
1763             =head3 set_public_status
1764              
1765             Usage : $ec->set_public_status('padID', 'publicStatus')
1766             Purpose : Sets a boolean for the public status of a pad
1767             Returns : 1 if it succeeds
1768             Argument : A pad ID and the public status you want to set : 1 or 0
1769             See : http://etherpad.org/doc/v1.5.1/#index_setpublicstatus_padid_publicstatus
1770              
1771             =cut
1772              
1773             #################### subroutine header end ####################
1774              
1775              
1776             sub set_public_status {
1777 0     0 1   my $self = shift;
1778 0           my $pad_id = shift;
1779 0           my $public_status = shift;
1780              
1781 0 0         unless (defined($public_status)) {
1782 0           carp 'Please provide 2 arguments : a pad id and a public status (1 or 0)';
1783 0           return undef;
1784             }
1785              
1786 0 0         $public_status = ($public_status) ? JSON::XS::true : JSON::XS::false;
1787              
1788 0           my $api = '1';
1789 0           my $method = 'setPublicStatus';
1790              
1791 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1792 0           $request .= '&padID=' . $pad_id . '&publicStatus=' . $public_status;
1793 0           my $response = $self->{ua}->get($request);
1794 0 0         if ($response->is_success) {
1795 0           my $hash = decode_json $response->decoded_content;
1796 0 0         if ($hash->{code} == 0) {
1797 0           return 1;
1798             } else {
1799 0           carp $hash->{message};
1800 0           return undef;
1801             }
1802             }
1803             else {
1804 0           carp $response->status_line;
1805 0           return undef;
1806             }
1807             }
1808              
1809              
1810             #################### subroutine header begin ####################
1811              
1812             =head3 get_public_status
1813              
1814             Usage : $ec->get_public_status('padID')
1815             Purpose : Return true of false
1816             Returns : 1 or 0
1817             Argument : A pad ID
1818             See : http://etherpad.org/doc/v1.5.1/#index_getpublicstatus_padid
1819              
1820             =cut
1821              
1822             #################### subroutine header end ####################
1823              
1824              
1825             sub get_public_status {
1826 0     0 1   my $self = shift;
1827 0           my $pad_id = shift;
1828              
1829 0 0         unless (defined($pad_id)) {
1830 0           carp 'Please provide a pad id';
1831 0           return undef;
1832             }
1833              
1834 0           my $api = '1';
1835 0           my $method = 'getPublicStatus';
1836              
1837 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1838 0           $request .= '&padID=' . $pad_id;
1839 0           my $response = $self->{ua}->get($request);
1840 0 0         if ($response->is_success) {
1841 0           my $hash = decode_json $response->decoded_content;
1842 0 0         if ($hash->{code} == 0) {
1843 0 0         return ($hash->{data}->{publicStatus}) ? 1 : 0;
1844             } else {
1845 0           carp $hash->{message};
1846 0           return undef;
1847             }
1848             }
1849             else {
1850 0           carp $response->status_line;
1851 0           return undef;
1852             }
1853             }
1854              
1855              
1856             #################### subroutine header begin ####################
1857              
1858             =head3 set_password
1859              
1860             Usage : $ec->set_password('padID', 'password')
1861             Purpose : Returns ok or a error message
1862             Returns : 1 if it succeeds
1863             Argument : A pad ID and a password
1864             See : http://etherpad.org/doc/v1.5.1/#index_setpassword_padid_password
1865              
1866             =cut
1867              
1868             #################### subroutine header end ####################
1869              
1870              
1871             sub set_password {
1872 0     0 1   my $self = shift;
1873 0           my $pad_id = shift;
1874 0           my $password = shift;
1875              
1876 0 0         unless (defined($password)) {
1877 0           carp 'Please provide 2 arguments : a pad id and a password';
1878 0           return undef;
1879             }
1880              
1881 0           my $api = '1';
1882 0           my $method = 'setPassword';
1883              
1884 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1885 0           $request .= '&padID=' . $pad_id . '&password=' . uri_escape($password);
1886 0           my $response = $self->{ua}->get($request);
1887 0 0         if ($response->is_success) {
1888 0           my $hash = decode_json $response->decoded_content;
1889 0 0         if ($hash->{code} == 0) {
1890 0           return 1;
1891             } else {
1892 0           carp $hash->{message};
1893 0           return undef;
1894             }
1895             }
1896             else {
1897 0           carp $response->status_line;
1898 0           return undef;
1899             }
1900             }
1901              
1902              
1903             #################### subroutine header begin ####################
1904              
1905             =head3 is_password_protected
1906              
1907             Usage : $ec->is_password_protected('padID')
1908             Purpose : Returns true or false
1909             Returns : 1 or 0
1910             Argument : A pad ID
1911             See : http://etherpad.org/doc/v1.5.1/#index_ispasswordprotected_padid
1912              
1913             =cut
1914              
1915             #################### subroutine header end ####################
1916              
1917              
1918             sub is_password_protected {
1919 0     0 1   my $self = shift;
1920 0           my $pad_id = shift;
1921              
1922 0 0         unless (defined($pad_id)) {
1923 0           carp 'Please provide a pad id';
1924 0           return undef;
1925             }
1926              
1927 0           my $api = '1';
1928 0           my $method = 'isPasswordProtected';
1929              
1930 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1931 0           $request .= '&padID=' . $pad_id;
1932 0           my $response = $self->{ua}->get($request);
1933 0 0         if ($response->is_success) {
1934 0           my $hash = decode_json $response->decoded_content;
1935 0 0         if ($hash->{code} == 0) {
1936 0 0         return ($hash->{data}->{passwordProtection}) ? 1 : 0;
1937             } else {
1938 0           carp $hash->{message};
1939 0           return undef;
1940             }
1941             }
1942             else {
1943 0           carp $response->status_line;
1944 0           return undef;
1945             }
1946             }
1947              
1948              
1949             #################### subroutine header begin ####################
1950              
1951             =head3 list_authors_of_pad
1952              
1953             Usage : $ec->list_authors_of_pad('padID')
1954             Purpose : Returns an array of authors who contributed to this pad
1955             Returns : An array or an array reference depending on the context, containing the epl authors IDs
1956             Argument : A pad ID
1957             See : http://etherpad.org/doc/v1.5.1/#index_listauthorsofpad_padid
1958              
1959             =cut
1960              
1961             #################### subroutine header end ####################
1962              
1963              
1964             sub list_authors_of_pad {
1965 0     0 1   my $self = shift;
1966 0           my $pad_id = shift;
1967              
1968 0 0         unless (defined($pad_id)) {
1969 0           carp 'Please provide a pad id';
1970 0           return undef;
1971             }
1972              
1973 0           my $api = '1';
1974 0           my $method = 'listAuthorsOfPad';
1975              
1976 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
1977 0           $request .= '&padID=' . $pad_id;
1978 0           my $response = $self->{ua}->get($request);
1979 0 0         if ($response->is_success) {
1980 0           my $hash = decode_json $response->decoded_content;
1981 0 0         if ($hash->{code} == 0) {
1982 0 0         return (wantarray) ? @{$hash->{data}->{authorIDs}} : $hash->{data}->{authorIDs};
  0            
1983             } else {
1984 0           carp $hash->{message};
1985 0           return undef;
1986             }
1987             }
1988             else {
1989 0           carp $response->status_line;
1990 0           return undef;
1991             }
1992             }
1993              
1994              
1995             #################### subroutine header begin ####################
1996              
1997             =head3 list_names_of_authors_of_pad
1998              
1999             Usage : $ec->list_names_authors_of_pad('padID')
2000             Returns : Returns an array of the names of the authors who contributed to this pad in list context
2001             Returns an array reference in scalar context
2002             Argument : The pad ID
2003             See : This is not part of the Etherpad API but a facility offered by this module
2004              
2005             =cut
2006              
2007             #################### subroutine header end ####################
2008              
2009              
2010             sub list_names_of_authors_of_pad {
2011 0     0 1   my $self = shift;
2012 0           my $pad_id = shift;
2013              
2014 0 0         unless (defined($pad_id)) {
2015 0           carp 'Please provide a pad id';
2016 0           return undef;
2017             }
2018              
2019 0           my @names;
2020 0           my $anonymous = 0;
2021              
2022 0           my @authors = $self->list_authors_of_pad($pad_id);
2023 0           for my $author (@authors) {
2024 0           my $name = $self->get_author_name($author);
2025 0 0         if (defined($name)) {
2026 0           push @names, $name;
2027             } else {
2028 0           $anonymous++;
2029             }
2030             }
2031 0           @names = sort(@names);
2032 0 0         push @names, $anonymous . ' anonymous' if ($anonymous);
2033              
2034 0 0         return (wantarray) ? @names : \@names;
2035             }
2036              
2037              
2038             #################### subroutine header begin ####################
2039              
2040             =head3 get_last_edited
2041              
2042             Usage : $ec->get_last_edited('padID')
2043             Purpose : Returns the timestamp of the last revision of the pad
2044             Returns : A unix timestamp
2045             Argument : A pad ID
2046             See : http://etherpad.org/doc/v1.5.1/#index_getlastedited_padid
2047              
2048             =cut
2049              
2050             #################### subroutine header end ####################
2051              
2052              
2053             sub get_last_edited {
2054 0     0 1   my $self = shift;
2055 0           my $pad_id = shift;
2056              
2057 0 0         unless (defined($pad_id)) {
2058 0           carp 'Please provide at least a pad id';
2059 0           return undef;
2060             }
2061              
2062 0           my $api = '1';
2063 0           my $method = 'getLastEdited';
2064              
2065 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2066 0           $request .= '&padID=' . $pad_id;
2067 0           my $response = $self->{ua}->get($request);
2068 0 0         if ($response->is_success) {
2069 0           my $hash = decode_json $response->decoded_content;
2070 0 0         if ($hash->{code} == 0) {
2071 0           return $hash->{data}->{lastEdited};
2072             } else {
2073 0           carp $hash->{message};
2074 0           return undef;
2075             }
2076             }
2077             else {
2078 0           carp $response->status_line;
2079 0           return undef;
2080             }
2081             }
2082              
2083              
2084             #################### subroutine header begin ####################
2085              
2086             =head3 send_clients_message
2087              
2088             Usage : $ec->send_clients_message('padID', 'msg')
2089             Purpose : Sends a custom message of type msg to the pad
2090             Returns : 1 if it succeeds
2091             Argument : A pad ID and the message you want to send
2092             See : http://etherpad.org/doc/v1.5.1/#index_sendclientsmessage_padid_msg
2093              
2094             =cut
2095              
2096             #################### subroutine header end ####################
2097              
2098              
2099             sub send_clients_message {
2100 0     0 1   my $self = shift;
2101 0           my $pad_id = shift;
2102 0           my $msg = shift;
2103              
2104 0 0         unless (defined($msg)) {
2105 0           carp "Please provide 2 arguments : the pad id and a message";
2106 0           return undef;
2107             }
2108              
2109 0           my $api = '1.1';
2110 0           my $method = 'sendAPIsMessage';
2111              
2112 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2113 0           $request .= '&padID=' . $pad_id . 'msg=' . $msg;
2114 0           my $response = $self->{ua}->get($request);
2115 0 0         if ($response->is_success) {
2116 0           my $hash = decode_json $response->decoded_content;
2117 0 0         if ($hash->{code} == 0) {
2118 0           return 1;
2119             } else {
2120 0           carp $hash->{message};
2121 0           return undef;
2122             }
2123             }
2124             else {
2125 0           carp $response->status_line;
2126 0           return undef;
2127             }
2128             }
2129              
2130              
2131             #################### subroutine header begin ####################
2132              
2133             =head2 check_token
2134              
2135             Usage : $ec->check_token()
2136             Purpose : Returns ok when the current api token is valid
2137             Returns : 1 if the token is valid, 0 otherwise
2138             Argument : None
2139             See : http://etherpad.org/doc/v1.5.1/#index_checktoken
2140              
2141             =cut
2142              
2143             #################### subroutine header end ####################
2144              
2145              
2146             sub check_token {
2147 0     0 1   my $self = shift;
2148              
2149 0           my $api = '1.2';
2150 0           my $method = 'checkToken';
2151              
2152 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2153 0           my $response = $self->{ua}->get($request);
2154 0 0         if ($response->is_success) {
2155 0           my $hash = decode_json $response->decoded_content;
2156 0 0         if ($hash->{code} == 0) {
2157 0           return 1;
2158             } else {
2159 0           return 0;
2160             }
2161             }
2162             else {
2163 0           carp $response->status_line;
2164 0           return undef;
2165             }
2166             }
2167              
2168              
2169             #################################################################
2170             =head2 Pads
2171              
2172             See http://etherpad.org/doc/v1.5.1/#index_pads
2173              
2174             =cut
2175             #################################################################
2176              
2177             #################### subroutine header begin ####################
2178              
2179             =head3 list_all_pads
2180              
2181             Usage : $ec->list_all_pads()
2182             Purpose : Lists all pads on this epl instance
2183             Returns : An array or an array reference depending on the context, containing the pads names
2184             See : http://etherpad.org/doc/v1.5.1/#index_listallpads
2185              
2186             =cut
2187              
2188             #################### subroutine header end ####################
2189              
2190              
2191             sub list_all_pads {
2192 0     0 1   my $self = shift;
2193              
2194 0           my $api = '1.2.1';
2195 0           my $method = 'listAllPads';
2196              
2197 0           my $request = $self->{url} . '/api/' . $api . '/' . $method . '?apikey=' . $self->{apikey};
2198 0           my $response = $self->{ua}->get($request);
2199 0 0         if ($response->is_success) {
2200 0           my $hash = decode_json $response->decoded_content;
2201 0 0         if ($hash->{code} == 0) {
2202             # Change in the API, without documentation
2203 0 0         my $data = (ref($hash->{data}) eq 'HASH') ? $hash->{data}->{padIDs} : $hash->{data};
2204 0 0         return (wantarray) ? @{$data} : $data;
  0            
2205             } else {
2206 0           carp $hash->{message};
2207 0           return undef;
2208             }
2209             }
2210             else {
2211 0           carp $response->status_line;
2212 0           return undef;
2213             }
2214             }
2215              
2216              
2217             #################### footer pod documentation begin ###################
2218             =head1 INSTALL
2219              
2220             perl Makefile.PL
2221             make
2222             make test
2223             make install
2224              
2225             If you are on a windows box you should use 'nmake' rather than 'make'.
2226              
2227             =head1 BUGS and SUPPORT
2228              
2229             You can find documentation for this module with the perldoc command.
2230              
2231             perldoc Etherpad::API
2232              
2233             Bugs and feature requests will be tracked at RT:
2234              
2235             http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Etherpad-API
2236             bug-etherpad-api at rt.cpan.org
2237              
2238             The latest source code can be browsed and fetched at:
2239              
2240             https://github.com/ldidry/etherpad-api
2241             git clone git://github.com/ldidry/etherpad-api.git
2242              
2243             You can also look for information at:
2244              
2245             RT: CPAN's request tracker
2246              
2247             http://rt.cpan.org/NoAuth/Bugs.html?Dist=Etherpad-API
2248             AnnoCPAN: Annotated CPAN documentation
2249              
2250             http://annocpan.org/dist/Etherpad-API
2251             CPAN Ratings
2252              
2253             http://cpanratings.perl.org/d/Etherpad-API
2254             Search CPAN
2255              
2256             http://search.cpan.org/dist/Etherpad-API
2257              
2258              
2259             =head1 AUTHOR
2260              
2261             Luc DIDRY
2262             CPAN ID: LDIDRY
2263             ldidry@cpan.org
2264             https://fiat-tux.fr/
2265              
2266             =head1 COPYRIGHT
2267              
2268             This program is free software; you can redistribute
2269             it and/or modify it under the same terms as Perl itself.
2270              
2271             The full text of the license can be found in the
2272             LICENSE file included with this module.
2273              
2274              
2275             =head1 SEE ALSO
2276              
2277             perl(1).
2278              
2279             =cut
2280              
2281             #################### footer pod documentation end ###################
2282              
2283              
2284             1;
2285