?? rest.pm
字號:
package PunJab::REST;use strict;use warnings;use POE;use POE::Filter::XML;use POE::Filter::XML::Node;use POE::Filter::XML::NS qw/ :JABBER :IQ /;use POE::Filter::XML::Utils;use PunJab;use PunJab::Util;use PunJab::Session;use PunJab::Server::REST;use POSIX qw(strftime);use Data::Uniqid qw ( suniqid uniqid luniqid );my %help;sub new { shift; my $config = shift; $config->{'type'} = "rest_shell"; PunJab::Server::REST->new($config,'rest_server'); POE::Session->create ( inline_states => { _start => sub { my $kernel = $_[KERNEL]; if ($config->{debug}>0) { &debug("REST Interface Session Started\n"); } $kernel->alias_set("rest_shell"); $kernel->post( rest_server => publish => rest_shell => "ls" ); $kernel->post( rest_server => publish => rest_shell => "help" ); $kernel->post( rest_server => publish => rest_shell => "date" ); $kernel->post( rest_server => publish => rest_shell => "gmtime" ); $kernel->post( rest_server => publish => rest_shell => "login" ); $kernel->post( rest_server => publish => rest_shell => "logout" ); $kernel->post( rest_server => publish => rest_shell => "presences" ); $kernel->post( rest_server => publish => rest_shell => "messages" ); $kernel->post( rest_server => publish => rest_shell => "iqs" ); $kernel->post( rest_server => publish => rest_shell => "roster" ); $kernel->post( rest_server => publish => rest_shell => "register" ); $kernel->post( rest_server => publish => rest_shell => "vcard" ); $kernel->post( rest_server => publish => rest_shell => "browse" ); $kernel->post( rest_server => publish => rest_shell => "pubsub" ); # give a stream option to all methods. #$kernel->post( rest_server => publish => rest_shell => "stream" ); #$kernel->post( rest_server => publish => rest_shell => "transport" ); $kernel->yield('get_help'); }, session_register => sub { my $sid = $_[ARG0]; $punjab_session->register($sid); $punjab_session->logged_in($sid,1); $punjab_session->activity($sid,time()); if ($_[HEAP]->{'CONFIG'}->{debug} > 1) { &debug("REST Session $sid registered"); } }, session_unregister => sub { my $sid = $_[ARG0]; $punjab_session->unregister($sid); if ($_[HEAP]->{'CONFIG'}->{debug} > 1) { &debug("REST Session $sid unregistered"); } }, # -------------------------- # # rest calls # -------------------------- # date => sub { my $rest_transaction = $_[ARG0]; if ($rest_transaction->method() eq "GET") { my $date = localtime(); $rest_transaction->return(&str_to_node("<resources><date>".$date."</date></resources>")); } else { $rest_transaction->error($rest_transaction->method(). " is not supported."); } }, gmtime => sub { my $rest_transaction = $_[ARG0]; if ($rest_transaction->method() eq "GET") { my $date = gmtime(); $rest_transaction->return(&str_to_node("<resources><gmtime>" . $date."</gmtime></resources>")); } else { $rest_transaction->error($rest_transaction->method(). " is not supported."); } }, login => \&login, logout => \&logout, ls => \&ls, help => \&help, presences => \&presences, messages => \&messages, iqs => \&iqs, roster => \&roster, vcard => \&vcard, register => \®ister, browse => \&browse, disco => \&disco, pubsub => \&pubsub, send_rest => \&send_rest, get_help => \&get_help, get_presence => \&get_presence, get_roster => \&get_roster, send_roster => \&send_roster, return_browse => \&return_browse, return_pubsub => \&return_pubsub, check_timeout => \&check_timeout, check_timeout_continue => \&check_timeout_continue, }, heap => { CONFIG => $config }, options => { debug => $config->{'debug'}, trace => $config->{'debug'} } ); return undef;}sub login { # this will need to be overwritten or changed into a redifinable tool my ($kernel,$heap,$session,$rest_transaction) = @_[KERNEL, HEAP, SESSION, ARG0]; my ($options,$content,$session_id,$error); my $params = $rest_transaction->params(); my $port = $params->{'port'}; my $ch = $params->{'challenge'}; my $jid = $params->{'jid'}; my $pass = $params->{'pass'}; if ($rest_transaction->method() eq "GET") { if (not defined $rest_transaction->request()->authorization()) { $rest_transaction->auth(&str_to_node("<error><code>401</code><fault>Bad Authentication</fault><description>auth failed</description></error>")); return; } ($jid,$pass) = $rest_transaction->request()->authorization_basic(); # need to grab port! if (not defined($jid) or not defined($pass)) { $rest_transaction->error("you need a username and password"); return FALSE; } } if ($rest_transaction->method() eq "POST") { my $input = $rest_transaction->stdin(); $input =~ s/\x00//gi; &debug($input); my ($stdin) = &stdin_to_node($input); my $pc = $stdin->get_children_hash(); $port = $pc->{'port'}->data() if defined $pc->{'port'}; $pass = $pc->{'password'}->data() if defined $pc->{'password'}; $jid = $pc->{'jid'}->data() if defined $pc->{'jid'}; } ### Grab hostname #my $hostname = &get_host($jid); my ($username,$hostname,$resource) = &parse_jid($jid); if (not defined($username) or not defined($hostname)) { if ($rest_transaction->method() eq "GET") { $rest_transaction->auth(&str_to_node("<error><code>401</code><fault>Bad Authentication</fault><description>auth failed</description></error>")); } else { $rest_transaction->error(&str_to_node("<error><code>401</code><fault>Bad Authentication</fault><description>auth failed</description></error>")); } return FALSE; } #my $port_num = '5222' if not defined $port; if (not defined($port) or $port eq "") { $port = 5222; } $resource = "PunJab" if not defined $resource or $resource eq ""; ### Create the session # this needs some work my $ip = $rest_transaction->request()->{remote_ip}; $ip = luniqid if not defined $ip; my $key = $jid.$pass.$resource.$ip; $session_id = $punjab_session->key($key); ### Start the jabber client. if ($heap->{'CONFIG'}->{debug} > 1) { &debug('Connecting : ' .$jid." : ". $session_id . ":" .$port."\n"); } if ($punjab_session->check_session($session_id)) { $rest_transaction->return(&str_to_node("<login><sid>".$session_id."</sid></login>")); return; } PunJab::JabberConnection->new($session_id, $jid, $pass, $username, $resource, $hostname, $port, $heap->{'CONFIG'}, $rest_transaction, 'session_register', 'session_unregister'); $kernel->delay('check_timeout', $heap->{'CONFIG'}->{'idle_check'}); }sub logout { my ($kernel,$heap,$session,$rest_transaction) = @_[KERNEL, HEAP, SESSION, ARG0]; my $params = $rest_transaction->params(); my $args = $rest_transaction->args(); # maybe support this too? &debug($rest_transaction->stdin()); my ($stdin)= &stdin_to_node($rest_transaction->stdin()); my $sid = $params->{'sid'}; return if not defined $sid; if ($punjab_session->check_session($sid)) { $kernel->post($sid,'disconnect'); } else { $rest_transaction->error("Session does not exist"); return; } $rest_transaction->return(&str_to_node("<logout><sid>".$sid."</sid></logout>"));}sub messages { my ($kernel,$heap,$session,$rest_transaction) = @_[KERNEL, HEAP, SESSION, ARG0]; my $params = $rest_transaction->params(); my $args = $rest_transaction->args(); my $sid = $params->{'sid'}; my $to = $params->{'to'}; my $from = $params->{'from'}; my $id = $params->{'id'}; my $type = $params->{'type'}; my $c = $params->{'check'}; my $s = $params->{'stream'}; my $p = shift @$args if defined $args; return if not defined $sid; my $messages = POE::Filter::XML::Node->new('messages'); if ($punjab_session->check_session($sid)) { $punjab_session->activity($sid,time()); if ($rest_transaction->method() eq "GET") { # grab the message $rest_transaction->request()->{'rest_type'} = 'messages'; $kernel->post('Manager'.$sid,'start_message_event','send_rest',$rest_transaction) if $s; if (defined $c and $c==1) { $kernel->post('Manager'.$sid,'check_managed_message',$to,$from,'send_rest',$rest_transaction); } else { $kernel->post('Manager'.$sid,'get_managed_message',$to,$from,$id,$type,'send_rest',$rest_transaction); } return; } elsif ($rest_transaction->method() eq "POST") { my ($stdin)= &stdin_to_node($rest_transaction->stdin()); my $pc = $stdin->get_children(); if (defined($pc)) { # need to do more checks foreach my $p (@$pc) { if ($p->name() eq "message") { $kernel->post($sid,'send_xml',$p); $messages->insert_tag($p); } } } else { $rest_transaction->error("Error in xml"); return; } } $rest_transaction->return($messages); } else { $rest_transaction->error("Session does not exist"); return; } }sub presences{ my ($kernel,$heap,$session,$rest_transaction) = @_[KERNEL, HEAP, SESSION, ARG0]; my $params = $rest_transaction->params(); my $args = $rest_transaction->args(); my ($stdin)= &stdin_to_node($rest_transaction->stdin()); my ($pc,$status,$show); my $sid = $params->{'sid'}; my $to = $params->{'to'}; my $from = $params->{'from'}; my $s = $params->{'stream'}; return if not defined $sid; my $presences = POE::Filter::XML::Node->new('presences'); if ($punjab_session->check_session($sid)) { $punjab_session->activity($sid,time()); if ($rest_transaction->method() eq "GET") { # grab the presence $rest_transaction->request()->{'rest_type'} = 'presences'; $kernel->post('Manager'.$sid,'start_presence_event','send_rest',$rest_transaction) if $s; $kernel->post('Manager'.$sid,'get_managed_presence',$to,$from,'send_rest',$rest_transaction); return; } elsif ($rest_transaction->method() eq "POST") { $pc = $stdin->get_children(); if (defined($pc)) { # need to do more checks foreach my $p (@$pc) { if ($p->name() eq "presence") { $kernel->post($sid,'send_xml',$p); $presences->insert_tag($p); } else { my $tp = POE::Filter::XML::Node->new('presence'); $tp->insert_tag($p->name)->data($p->data()); $kernel->post($sid,'send_xml',$tp); $presences->insert_tag($tp); } } } else { } } $rest_transaction->return($presences); } else { $rest_transaction->error("Session does not exist"); }}sub roster{ my ($kernel,$heap,$session,$rest_transaction) = @_[KERNEL, HEAP, SESSION, ARG0]; my $params = $rest_transaction->params(); my $args = $rest_transaction->args(); # maybe support this too? my ($stdin)= &stdin_to_node($rest_transaction->stdin()); my $s = $params->{'stream'}; &debug($s); my $sid = $params->{'sid'}; return if not defined $sid; my $item; my $id = luniqid; if ($punjab_session->check_session($sid)) { $punjab_session->activity($sid,time()); $rest_transaction->request()->{'rest_type'} = 'roster'; if ($rest_transaction->method() eq "GET") { # should this go on the heap? It may be a bit to crazy $rest_transaction->request()->{'start_roster'} = $sid; $kernel->post('Manager'.$sid,'start_roster', 'get_roster', $rest_transaction,$id,$sid,'JABBER'.$sid); return; } elsif ($rest_transaction->method() eq "POST") { my $iq = POE::Filter::XML::Node->new('iq'); $iq->attr('type',+IQ_SET); $iq->attr('id',$id); my $query = $iq->insert_tag('query',['xmlns',+NS_JABBER_ROSTER]); if (ref($stdin) eq "POE::Filter::XML::Node") { my $items = $stdin->get_children(); foreach my $i (@$items) { my $p = POE::Filter::XML::Node->new('presence'); $p->attr('type','subscribe'); $p->attr('to',$i->attr('jid')); $kernel->post($sid,'send_xml',$p); $query->insert_tag($i); } } else { $item = $query->insert_tag('item'); $item->attr('name',$params->{'name'}) if defined $params->{'name'}; $item->attr('jid',$params->{'jid'}) if defined $params->{'jid'}; $item->insert_tag('group')->data($params->{'group'}) if defined $params->{'group'}; } $kernel->post($sid,'send_xml',$iq); $kernel->post('Manager'.$sid,'manage_events', 'send_rest', $rest_transaction,$iq,$sid); return; } } else { $rest_transaction->error("Session does not exist"); return; }}sub get_roster() { my ($kernel,$session,$heap,$ret_val, $rest_transaction,$node,$sid) = @_[KERNEL, SESSION, HEAP, ARG0 .. ARG3]; if ($punjab_session->check_session($sid)) { if (defined($rest_transaction->request()->{'start_roster'})) { # we want to combine presence with our roster $kernel->post('Manager'.$sid,'get_managed_roster','get_presence',$rest_transaction,$sid); } else { $kernel->post('Manager'.$sid,'get_managed_roster','send_rest',$rest_transaction); } } else { $rest_transaction->error("Session does not exist"); }}sub get_presence() { my ($kernel,$session,$heap,$ret_val, $rest_transaction,$node,$sid) = @_[KERNEL, SESSION, HEAP, ARG0 .. ARG3]; $sid = $rest_transaction->request()->{'start_roster'} if defined $rest_transaction->request()->{'start_roster'}; if ($punjab_session->check_session($sid)) { if (defined($rest_transaction->request()->{'start_roster'})) { # should this go on the heap? It may be a bit to crazy $rest_transaction->request()->{'roster'} = $ret_val; $kernel->post('Manager'.$sid,'get_managed_presence',undef,undef,'send_roster',$rest_transaction); } else { $kernel->post('Manager'.$sid,'get_managed_presence',undef,undef,'send_rest',$rest_transaction); } } else { $rest_transaction->error("Session does not exist"); }}sub send_roster { my ($kernel,$session,$heap,$ret_val,$rest_trans) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1]; my $ret = POE::Filter::XML::Node->new('roster'); if(ref($rest_trans->request()->{'roster'}) eq 'ARRAY') { foreach my $r (@{$rest_trans->request()->{'roster'}}) { my $rvn = &str_to_node($r->{'xml'}); foreach my $tr (@$ret_val) { my $tmp_jid = $r->{'jid'}; $tmp_jid =~ s/\+/\\+/gi; if ($tr->{'from_attr'} =~ /$tmp_jid/) { my $trvn = &str_to_node($tr->{'xml'}); $rvn->insert_tag($trvn); next; } } $ret->insert_tag($rvn); } } $rest_trans->return($ret);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -