Commit | Line | Data |
---|---|---|
21c4e167 H |
1 | #!/usr/bin/env perl |
2 | #Kyberia client... (Harvie 2oo9) | |
3 | #THX2 || | |
4 | ||
5 | use strict; | |
6 | use warnings; | |
7 | ||
8 | package Kyberia; | |
9 | use LWP; | |
10 | use HTML::Entities; | |
11 | ||
12 | our $url; | |
13 | our $browser; | |
14 | ||
15 | sub new { #constructor | |
16 | my ($self, $kurl) = @_; | |
17 | $url = $kurl; | |
18 | $browser = LWP::UserAgent->new( ); | |
19 | $browser->cookie_jar( {} ); | |
20 | $browser->timeout(15); | |
21 | return $self; | |
22 | } | |
23 | ||
24 | sub Login { | |
25 | my ($self, $user, $pass) = @_; | |
26 | my $response = $browser->post( $url, | |
27 | [ 'event' => 'login', | |
28 | 'login' => $user, | |
29 | 'password' => $pass, | |
30 | 'login_type' => 'name', | |
31 | ] | |
32 | ); | |
33 | return $response->is_success; | |
34 | } | |
35 | ||
36 | sub Logout { | |
37 | my $response = $browser->post( $url, | |
38 | [ 'event' => 'logout' | |
39 | ] | |
40 | ); | |
41 | return $response->is_success; | |
42 | } | |
43 | ||
44 | sub SendMail { | |
45 | my ($self, $user, $message) = @_; | |
46 | my $response = $browser->post( $url, | |
47 | [ 'event' => 'send', | |
48 | 'mail_to' => $user, | |
49 | 'mail_to_type' => 'name', | |
50 | 'mail_text' => $message, | |
51 | ] | |
52 | ); | |
53 | return $response->is_success; | |
54 | } | |
55 | ||
56 | sub GetNode { | |
57 | my ($self, $id) = @_; | |
58 | my $response = $browser->get("$url/id/$id/"); | |
59 | return $response->content; | |
60 | } | |
61 | ||
62 | sub ReadMail { | |
63 | my $response = $browser->get("$url/id/2176597/"); | |
64 | my @mails = split(/\n/, $response->content); $response = ''; | |
65 | shift(@mails); #remove CSV header | |
66 | foreach (@mails) { | |
67 | my ($MSG_ID, $FROM, $FROM_ID, $TO, $TO_ID, $TIME, $NEW, $TEXT) = split(/;/, $_, 8); #parse CSV | |
68 | $TEXT =~ s/\\n/\n/g; decode_entities($TEXT); #unescape message | |
69 | print("#$MSG_ID $FROM --> $TO ($TIME)\n\n$TEXT\n---------------------------------\n"); | |
70 | } | |
71 | return $response->is_success; | |
72 | } | |
73 | ||
74 | sub CheckMail { | |
75 | my $response = $browser->get("$url/ajax/check_new_mail.php"); | |
76 | my @mails = split(/;/, $response->content); | |
77 | if($response->is_success) { | |
78 | my $text = "You have $mails[0] unread mails, last from $mails[1]."; | |
79 | if($mails[0] < 1) { $text = "No unread mail, last mail from $mails[1]." } | |
80 | return ($mails[0], $mails[1], $text); | |
81 | } | |
82 | return 0; | |
83 | } | |
84 | ||
85 | return 1; |