summaryrefslogtreecommitdiffstats
path: root/Spline/Data.pm
blob: beae3fa44594b3359191cda2eb7e7f4ac9cad6be (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/perl
=pod

=head1 NAME

Spline::Data - Per Message data

=head1 SYNOPSIS

    use Spline::Data;

    my $data = Spline::Data->new($ctx);
    my $value = $data->get($key);
    $data->set($key, $value);

    my $data = Spline::Data->load($ctx); 

=head1 DESCRIPTION

=cut

package Spline::Data;

use strict;
use warnings;

use Spline::Log qw(set_log_context);
use Data::Dumper;

use base 'Exporter';
our @EXPORT = qw();
our @EXPORT_OK = qw(); 

=head2 _generate_id

    my $id = _gernerate_id();

Generate a random string to tag interleaving log lines belonging to
the same message.

=cut

sub _generate_id() {
    my @chars = ('A'..'Z', 'a'..'z', '0'..'9');
    my $id = '';
    $id .= $chars[rand @chars] for 1..10;

    return $id;    
};

=head2 new

    my $data = Spline::Data::new($ctx);

=cut

sub new {
    my $class = shift;
    my $ctx = shift;

    my $id = _generate_id();
    my $self = {
        ctx => $ctx,
        data => {
            log_ctx => $id,
        }
    };
    set_log_context($id);

    return bless $self, $class;
}

=head2 get

    my $value = $data->get($key);

Get the matching value for the supplied key as scalar. If there is no
such key in the data, return undef.

=cut

sub get($$) {
    my $self = shift;
    my $key = shift;

    return $self->{data}->{$key} if defined $self->{data}->{$key};
    return undef;
}

=head2 set

    $data->set($key, $value);

Set the supplied value for the key and save the data in the Milter
context. This method will silently create new keys and overwrite
possible existent values.

=cut

sub set($$$) {
    my $self = shift;
    my ($key, $value) = @_;

    $self->{data}->{$key} = $value;
    $self->_save();
}

=head2 _save

    $data->_save();

Save the data in the Milter context.

=cut

sub _save($) {
    my $self = shift;

    $self->{ctx}->setpriv($self->{data}); 
}

=head2 load

    my $data = Spline::Data->load($ctx);

Get the data from the Milter context and return a Spline::Data object.
The data is saved again after receiving it, so that it will persist
even if no value is changed.

=cut

sub load {
    my $class = shift;
    my $ctx = shift;

    my $self = {
        ctx => $ctx,
        data => {},
    };
    $self->{data} = $ctx->getpriv();
    _save($self);

    if (defined $self->{data}->{log_ctx}) { 
        set_log_context($self->{data}->{log_ctx});
    }

    return bless $self, $class;
} 

=head1 AUTHOR

Alexander Sulfrian <alex@spline.inf.fu-berlin.de>

=cut

1;
# vim: set et tabstop=4 tw=70: