File: utils.pm

package info (click to toggle)
webrt 1.0.1-4
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 652 kB
  • ctags: 324
  • sloc: perl: 5,541; makefile: 265; ansic: 28
file content (414 lines) | stat: -rwxr-xr-x 8,801 bytes parent folder | download
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package rt;


sub untaint {
	my $data = shift;
   if ($data =~ /^([-\@\w.]+)$/) {
               $data = $1;                     # $data now untainted
           } else {
               die "Bad data in $data";        # log this somewhere
           }
	return ($data);
}

#
#can the named user modify the named queue
sub can_manipulate_request{
    my ($in_serial_num, $in_user) =@_;
    &req_in($in_serial_num,$in_user);
    if (&can_manipulate_queue($rt::req[$in_serial_num]{'queue_id'},$in_user)) {
	return(1);
    }
    else {
	return(0);
    }
}

#can the named user display the named queue
sub can_display_request{
    my ($in_serial_num, $in_user) =@_;
    &req_in($in_serial_num,$in_user);
    if (&can_display_queue($rt::req[$in_serial_num]{'queue_id'},$in_user)) {
	return(1);
    }
    else {
	return(0);
    }
}


sub can_create_request{
    my $in_queue = shift;
    my $in_user = shift;
  
    if ($queues{$in_queue}{'acls'}{$in_user}{'manipulate'}) {
	return(1);
}
    if ($queues{$in_queue}{'allow_user_create'}) {
	return(2);
    }

    else {
	return(0);
    }
}

sub can_manipulate_queue {
    my ($in_queue, $in_user) =@_;

    if ($queues{$in_queue}{acls}{$in_user}{manipulate}) {
	return(1);
    }
	elsif ($users{$in_user}{admin_rt}) {
	return (2);
    }

    else {
	return(0);
    }
}



sub can_display_queue {
    my ($in_queue, $in_user) =@_;
    if ($queues{$in_queue}{acls}{$in_user}{display}) {
	return(1);
    }
    elsif ($users{$in_user}{admin_rt}) {
	return (2);
    }

    else {
	return(0);
    }
}

sub can_admin_queue {
    my ($in_queue, $in_user) =@_;
    if ($queues{$in_queue}{acls}{$in_user}{admin}) {
	return(1);
    }
    elsif ($users{$in_user}{admin_rt}) {
	return (2);
    }

    else {
	return(0);
    }
}
sub is_not_a_requestor{
    my($address,$serial_num) =@_;

   if (($address !~ /\@/) and (exists $users{$address})) {
        $address=$users{$address}{email}
    }
    if ($req[$serial_num]{'requestors'} =~ /(^|\s|,)$address(,|\s|\b)/i) {
	return(0);
    }
    else {
	return(1);
    }
    
}
sub is_owner{
    my($serial_num,$user) =@_;
    if ($req[$serial_num]{'owner'} eq $user) {
	return(1);
    }
    else {
	return(0);
    }
    
}


#normalize_sn takes care of opersations on reqs which have been merged
sub normalize_sn{
    my ($in_serial_num)=@_;
    my ($effective_sn);
    $effective_sn=&get_effective_sn($in_serial_num);
    $effective_sn=int($effective_sn);
    return($effective_sn);
}

#
# return something's boolean value
#

sub booleanize {
my ($in_val)=@_;
	if ($in_val) {
	return(1);
	}
	else{
	return(0);
	}
}


#
# quote_content
# will generate the content of a transaction...prefixed.
#
sub quote_content {
    my $transaction = shift;
    my $current_user = shift;
    my ($trans, $quoted_content, $body, $headers);
    $trans=&rt::transaction_in($transaction,$current_user);
    ((($body,$headers) =  split (/--- Headers Follow ---\n\n/, $rt::req[$serial_num]{'trans'}[$trans]{'content'},2))) or 
	(($headers, $body) = split ('\n\n',$lines)) or
	    $body = $lines;
    $quoted_content = "$rt::req[$serial_num]{'trans'}[$trans]{'actor'} wrote ($rt::req[$serial_num]{'trans'}[$trans]{'text_time'}):\n\n";
    
    $quoted_content .= &rt::prefix_string("> ",$body);
    return ($quoted_content);
}

#
# prefix_string quotes a message
#
sub prefix_string {
    
    my $prefix = shift;
    my $text = shift;
    my ($line, $newtext);
    foreach $line (split (/\n/,$text)) {
	$newtext .= $prefix . $line . "\n";
    }
    
    return ($newtext)
    }



#
#
#Adapted from ctime.pl
#

sub parse_time {
    local($time) = @_;
    local($[) = 0;
    local($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
    @DoW = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
    @MoY = ('Jan','Feb','Mar','Apr','May','Jun',
	    'Jul','Aug','Sep','Oct','Nov','Dec');
    # Determine what time zone is in effect.
    # Use GMT if TZ is defined as null, local time if TZ undefined.
    # There's no portable way to find the system default timezone.

    $TZ = defined($ENV{'TZ'}) ? ( $ENV{'TZ'} ? $ENV{'TZ'} : 'GMT' ) : '';
    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
        ($TZ eq 'GMT') ? gmtime($time) : localtime($time);
    
    # Hack to deal with 'PST8PDT' format of TZ
    # Note that this can't deal with all the esoteric forms, but it
    # does recognize the most common: [:]STDoff[DST[off][,rule]]
    
    if($TZ=~/^([^:\d+\-,]{3,})([+-]?\d{1,2}(:\d{1,2}){0,2})([^\d+\-,]{3,})?/){
        $TZ = $isdst ? $4 : $1;
    }
    $TZ .= ' ' unless $TZ eq '';
    if (!$time) {
	$TZ = "NULLTIME";
    }
    $year += 1900;
    return ($DoW[$wday], $MoY[$mon], $mday, $hour, $min, $sec, $TZ, $year);
}
	
#
# Date_diff from req-1.2.7 by Remy Evard (remy@ccs.neu.edu)
# Hacked by jesse vincent to deal with negative time
#	
sub date_diff {

    local($old, $new) = @_;
    local($diff, $minute, $hour, $day, $week, $month, $year, $s, $string, $negative="");


    $diff = $new - $old;
    if ($diff < 0) {
	$negative = "-";
	$diff = $old - $new;
    }

    $minute = 60;
    $hour   = 60 * $minute;
    $day    = 24 * $hour;
    $week   = 7 * $day;
    $month  = 4 * $week;
    $year   = 365 * $day;
    
    if($diff < $minute) {
	$s=$diff;
	$string="sec";
    } elsif($diff < (2 * $hour)) {
	$s = int($diff/$minute);
	$string="min";
    } elsif($diff < (2 * $day)) {
	$s = int($diff/$hour);
	$string="hr";
    } elsif($diff < (2 * $week)) {
	$s = int($diff/$day);
	$string="day";
    } elsif($diff < (2 * $month)) {
	$s = int($diff/$week);
	$string="wk";
    } elsif($diff < $year) {
	$s = int($diff/$month);
	$string="mth";
    } else {
	$s = int($diff/$year);
	$string="yr";
    }
    return "$negative$s $string";
}


sub template_read {
    
    local ($in_template, $in_queue) =@_;
    local ($template_content="");
    
    
    if (! (-f "$rt::template_dir/queues/$in_queue/$in_template")) 
    {
       	return ("The specified template is missing or inaccessable.\n ($rt::template_dir/queues/$in_queue/$in_template)\n However, the custom content which was supposed to fill the template was:\n %content%");
    }
    open(CONTENT, "$rt::template_dir/queues/$in_queue/$in_template"); 
    while (<CONTENT>)
    {
	$template_content .= $_;
    }
    close (CONTENT);
    return ($template_content);
}



sub date_parse {
    my ($date_string) = shift;

    my ($now_wday, $now_mon, $now_mday, $now_hour, $now_min, $now_sec, $now_TZ, $now_year, $time);   
    use Time::Local;
    
    
    ($now_wday, $now_mon, $now_mday, $now_hour, $now_min, $now_sec, $now_TZ, $now_year)=&rt::parse_time($rt::time);  

    #print "$date_string is the date string\n";
    
    if ($date_string =~ /(\d*)\/(\d*)\/(\d*)/) {
	$month = $1;
	$day = $2;
	$year = $3;
    }

    elsif ($date_string =~ /^(.?)\/(.?)$/) {
	$month = $1;
	$day = $2;
	#FIXME THIS IS NOT THE RIGHT WAY TO DO THIS. IT ONLY WORKS FOR DATES IN
	# THE CURRENT YEAR
	$year = $now_year;
    }
    
    elsif ($date_string =~ /^(...), (\d?) (...) (\d?) (\d?):(\d?):(\d?) (\W*)/) {
	$monthword = $3;
	$month = &getmonth($monthword);

	$day = $2;
	$year = $4;
	$hour = $5;
	$min = $6;
	$sec = $7;
	$tz = $8;
    }
    if ($year == -1) {
	$year = $now_year;
    }
    if (($year > 70) and ($year < 100))  {
	$year += 1900;
    }
    elsif ($year < 100) {
	$year += 2000;
    }
    
    #print "$time: $hours:$sec:$min $month\/$day\/$year\n";

    $time = timelocal($sec, $min, $hours, $day, $month, $year);

    #print "$time: $hours:$sec:$min $month\/$day\/$year\n";
    
    return ($time);

}

sub getmonth {
    my ($monthword) = shift;
    if ($monthword =~ /jan/i) {
	return(1);
    }
    elsif ($monthword =~ /feb/i) {
	return(2);
    }   
    elsif ($monthword =~ /mar/i) {
	return(3);
    }    
    elsif ($monthword =~ /apr/i) {
	return(4);
    }    
    elsif ($monthword =~ /may/i) {
	return(5);
    }    
    elsif ($monthword =~ /jun/i) {
	return(6);
    }    
    elsif ($monthword =~ /jul/i) {
	return(7);
    }     
    elsif ($monthword =~ /aug/i) {
	return(8);
    }    
    elsif ($monthword =~ /sep/i) {
	return(9);
    }    
    elsif ($monthword =~ /oct/i) {
	return(10);
    }    
    elsif ($monthword =~ /nov/i) {
	return(11);
    }   
    elsif ($monthword =~ /dec/i) {
	return(12);
    }
    else {
	return (-1);
    }
    
    
}

# make sure we're using full e-mail addresses rather than username w/o
# the domain part
sub norm_requestors
{
my $reqs = shift;
my %nreqs;
my $err = "ERROR: Incorrect requestor(s): '$reqs'";

   $reqs =~ s/\s//g;
#i think we should allow empty requestors
#   return ('',$err) if ! $reqs;
   @l = split(/,/,$reqs);
   for( @l )
   {
	$_ .= "\@$rt::domain" if ! /\@/;
	$nreqs{$_} = 1;
   }
   $res = join(",",sort keys %nreqs);
#   return ('',$err) if ! $res;
   return ($res,'');
}

1;