]> jfr.im git - irc/SurrealServices/srsv.git/blob - branches/erry-devel/SrSv/IRCd/Event.pm
initial commit of erry's Insp work.
[irc/SurrealServices/srsv.git] / branches / erry-devel / SrSv / IRCd / Event.pm
1 # This file is part of SurrealServices.
2 #
3 # SurrealServices is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # SurrealServices is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with SurrealServices; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
17 package SrSv::IRCd::Event;
18
19 use strict;
20
21 use Exporter 'import';
22 BEGIN { our @EXPORT_OK = qw(addhandler callfuncs) }
23
24 use SrSv::Debug;
25
26 use SrSv::IRCd::Queue qw(ircd_enqueue);
27 use SrSv::IRCd::State qw($ircline $ircline_real synced initial_synced);
28
29 use SrSv::Message qw(add_callback message);
30
31 # FIXME
32 use constant {
33 # Wait For
34 WF_NONE => 0,
35 WF_NICK => 1,
36 WF_CHAN => 2,
37 WF_ALL => 3,
38 };
39
40 sub addhandler($$$$;$) {
41 my ($type, $src, $dst, $cb, $po) = @_;
42
43 if($cb !~ /::/) {
44 $cb = caller() . "::$cb";
45 }
46
47 print "Adding callback: $cb\n" if DEBUG;
48
49 my @cond = ( CLASS => 'IRCD', TYPE => $type );
50 push @cond, ( SRC => $src ) if($src);
51 push @cond, ( DST => $dst ) if($dst);
52
53 add_callback({
54 NAME => $cb,
55 TRIGGER_COND => { @cond },
56 CALL => 'SrSv::IRCd::Event::_realcall',
57 REALCALL => $cb,
58 PARENTONLY => $po,
59 });
60 }
61
62 sub callfuncs {
63 my ($args, $sync, $wf, $message);
64
65 if(@_ == 4) {
66 $args = $_[3];
67 $sync = 1;
68 $wf = WF_NONE;
69 } else {
70 $args = $_[4];
71 $sync = 0;
72 $wf = $_[3];
73 }
74
75 $message = {
76 CLASS => 'IRCD',
77 TYPE => $_[0],
78 SYNC => $sync,
79 SRC => (defined($_[1]) ? $args->[$_[1]] : undef),
80 DST => (defined($_[2]) ? $args->[$_[2]] : undef),
81 WF => $wf,
82 IRCLINE => ($sync ? $ircline : $ircline_real),
83 ARGS => $args,
84 ON_FINISH => ($sync ? undef : 'SrSv::IRCd::Queue::finished'), # FIXME
85 SYNCED => [synced, initial_synced],
86 };
87
88 if($sync) {
89 message($message);
90 } else {
91 ircd_enqueue($message);
92 }
93 }
94
95 sub _realcall($$) {
96 no strict 'refs';
97
98 my ($message, $callback) = @_;
99
100 print "Calling ", $callback->{REALCALL}, " ", join(',', @{$message->{ARGS}}), "\n" if DEBUG();
101 $ircline = $message->{IRCLINE};
102
103 local $SrSv::IRCd::State::synced = $message->{SYNCED}[0]; # XXX This is questionable.
104 local $SrSv::IRCd::State::initial_synced = $message->{SYNCED}[1];
105
106 print "IRCLINE is $ircline synced is $SrSv::IRCd::State::synced initial_synced is $SrSv::IRCd::State::initial_synced\n" if DEBUG();
107
108 &{$callback->{REALCALL}}(@{$message->{ARGS}});
109 ircd::flushmodes() unless $message->{SYNC}; # FIXME
110 print "Finished with $ircline\n" if DEBUG();
111 }
112
113 1;