-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo1
executable file
·118 lines (105 loc) · 3.16 KB
/
demo1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env perl
# $Id: demo1,v 1.4 2007/06/11 04:47:17 tans Exp $
# Copyright (c) 2006, 2007 Shufeng Tan. All rights reserved.
#
# This package is free software and is provided "as is" without express
# or implied warranty. It may be used, redistributed and/or modified
# under the terms of the Perl Artistic License (see
# http://www.perl.com/perl/misc/Artistic.html)
# This is a simple example of using Net::OICQ modules.
use strict;
use Net::OICQ;
use Net::OICQ::ServerEvent;
use Net::OICQ::ClientEvent;
use Term::ReadKey;
my $uid = shift;
die "Usage: $0 <QQ_id>\n" unless $uid =~ /^[1-9]\d{2,9}$/;
my $oicq = new Net::OICQ;
$oicq->{Debug} = 9;
my $pw = $ENV{OICQ_PW};
if ($pw) {
print "OICQ_PW found in env.\n";
} else {
print "Password: ";
local $SIG{__DIE__} = { ReadMode 0 };
ReadMode 2;
$pw = <STDIN>;
ReadMode 0;
$pw =~ s/[^ -~]*$//;
print "\n";
die "Password is empty.\n" unless $pw;
}
my $login = $oicq->login($uid, $pw, 'Invisible');
die "Login failed.\n" unless $login;
$oicq->get_online_friends;
$oicq->get_user_info($uid);
$oicq->get_friends_list;
$oicq->send_msg($uid, "Hello " . localtime);
my $socket = $oicq->{Socket};
my $timeout = 30;
my $timeout_msg = "timeout\n";
local $SIG{ALRM} = sub { die $timeout_msg };
alarm($timeout);
my $count = @{$oicq->{EventQueue}};
print "$count commands sent. Program will timeout in $timeout\"\n";
while($count) {
my $reply;
eval {$socket->recv($reply, 49152)};
last unless $reply;
foreach my $data ($oicq->get_data($reply)) {
my $event = new Net::OICQ::ServerEvent($data, $oicq);
unless(defined $event) {
print "No event created for:\n", unpack("H*", $data), "\n";
next;
}
next unless $event->{Data};
$event->parse;
my $cmd = $event->cmd;
if ($cmd eq 'get_online_friends') {
my $online_friends = $event->{OnlineFriends};
if (@$online_friends) {
print "Online friends:\n";
} else {
print "No friend online.\n";
}
foreach my $fid (@$online_friends) {
my $info = $oicq->{Info}->{$fid};
print "$fid $info->{Mode} $info->{Addr}\n";
}
$count--;
} elsif ($cmd eq 'get_user_info') {
my $info = $oicq->{MyInfo};
for(my $i = 0; $i < @Net::OICQ::InfoHeader; $i++) {
print $Net::OICQ::InfoHeader[$i], ": ", $info->[$i], "\n";
}
$count--;
} elsif ($cmd eq 'get_friends_list') {
print '-'x25, " Friends List ", '-'x25, "\n";
my $info = $oicq->{Info};
my $idx = 1;
foreach my $id (sort {$a <=> $b} keys %$info) {
my $hashref = $info->{$id};
next unless defined $hashref->{Friend};
printf "%2d. %9d %3s %3s %4s : %-16s %s\n",
$idx++, $id,
defined($hashref->{Sex}) ? $hashref->{Sex} : '',
defined($hashref->{Age}) ? $hashref->{Age} : '',
defined($hashref->{Face}) ? $hashref->{Face} : '',
defined($hashref->{Nickname}) ? $hashref->{Nickname} : '',
defined($hashref->{Unknown}) ? $hashref->{Unknown} : '';
}
print '='x65, "\n";
$count--;
} elsif ($cmd eq 'send_msg') {
$count--;
} elsif ($cmd eq 'recv_msg') {
print "Message from $event->{SrcId}:\n$event->{Mesg}\n";
}
}
}
print "=========== Event Queue =========\n";
foreach my $e (@{$oicq->{EventQueue}}) {
print $e->dump;
}
$oicq->logout;
__END__