This file is indexed.

/usr/share/tiarra/main/Mask.pm is in tiarra 20100212+r39209-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
# -----------------------------------------------------------------------------
# $Id: Mask.pm 11365 2008-05-10 14:58:28Z topia $
# -----------------------------------------------------------------------------
package Mask;
use strict;
use warnings;
use Carp;
use Multicast;


# -----------------------------------------------------------------------------
# $bool = match($masks, $str).
# $bool = match($masks, $str, $match_type, $use_re, $use_flag).
# どれにもマッチしなかった際はundef, つまり偽がかえる.
# 明示的に拒否された場合は 0, つまりdefinedな偽が返る.
#
sub match {
  # matchはワイルドカードを使ったマッチングを行う関数です。
  # ワイルドカード以外にも、+や-を使った除外指定や、
  # re: を使った正規表現マッチングが行えます。

  # $masksには','(コンマ)で区切ったマッチリストを渡してください。
  # 条件中に','(コンマ)を使いたい場合は'\,'と書けます。

  # 引数名      : [既定値] - 説明 -
  # $masks      : [-] カンマ区切りのマッチリスト.
  # $str        : [-] マッチ対象の文字列.
  # $match_type : [0] 0: 最後にマッチした値を返します。 1: 最初にマッチした値を返します。
  # $use_re     : [1] 0: 正規表現マッチを使用しません。 1: 使用します。
  # $use_flag   : [1] 0: +や-を使用しません。           1: 使用します。

  # 返り値      : { 1 (true)  => + にマッチ,
  #                 0 (false) => - にマッチ,
  #                  (undef)  => まったくマッチしなかった
  my ($masks, $str, $match_type, $use_re, $use_flag) = @_;
  if (!defined $masks || !defined $str) {
    return undef;
  }

  return match_array([_split($masks)], $str, $match_type, $use_re, $use_flag);
}

# -----------------------------------------------------------------------------
# $bool = match_deep(\@masks_list, $str).
# $bool = match_deep(\@masks_list, $str, $g_match_type, $match_type, $use_re, $use_flag);
# @masks の各要素に対して match() を行う.
#
sub match_deep {
  # match_deepは次のようなマスクの解釈に使います。

  # mask: +*!*@*
  # mask: -example!*

  # 引数名             : [既定値] - 説明 -
  # $masks_array       : [無し] マスク配列の参照を渡します。
  #  Mask::match_deep([Mask::mask_array_or_all($this->config->mask('all'))], $msg->prefix)
  #                    : のように使います。
  # $global_match_type : [1] 0: 最後にマッチした行の値を返します。 1: 最初にマッチした行の値を返します。
  my ($masks_array, $str, $g_match_type, $match_type, $use_re, $use_flag) = @_;
  if (!defined $masks_array) {
    return undef;
  }

  $g_match_type = 1 unless defined $g_match_type;

  my $g_matched = undef;
  foreach my $masks (@$masks_array) {
    my $matched = match_array([_split($masks)], $str, $match_type, $use_re, $use_flag);
    if (defined $matched) {
      $g_matched = $matched;
      return $g_matched if $g_match_type == 1;
    }
  }

  return $g_matched;
}

# -----------------------------------------------------------------------------
# $bool = match_array(\@masks, $str).
# $bool = match_array(\@masks, $str, $match_type, $use_re, $use_flag).
#
sub match_array {
  # match_arrayは、matchから呼ばれる内部関数ですが、普通に呼び出して使うこともできます。
  # match との違いは、マスクをマスク配列の参照として渡す点です。

  # $match_type: 0: last matching rule, 1: first matching rule
  # $use_re    : use 're:' feature.
  # $use_flag  : use [+-] match flag.

  # <return value> : status { 1 (true)  => +, matched,
  #                           0 (false) => -, matched,
  #                            (undef)  => no-match }
  my ($mask_array, $str, $match_type, $use_re, $use_flag) = @_;

  if (!defined $mask_array || ref($mask_array) ne 'ARRAY' || !defined $str) {
    return undef;
  }

  $match_type = 0 unless defined $match_type;
  $use_re = 1 unless defined $use_re;
  $use_flag = 1 unless defined $use_flag;

  my $matched = undef;
  foreach my $part (@$mask_array) {
    my $work = $part;
    my $first = substr($work, 0, 1);
    my $include = 1;
    if (!$use_flag) {
      # noop
    } elsif ($first eq '+') {
      substr($work, 0, 1) = '';
    } elsif ($first eq '-') {
      $include = 0;
      substr($work, 0, 1) = '';
    }

    if ($use_re && substr($work, 0, 3) eq 're:') {
      # 正規表現
      $work = substr($work,3);
      # untaint
      $work =~ /\A(.*)\z/s;
      $work = eval {
	qr/$1/;
      }; if ($@) {
	$work = '';
	carp "error in regex: $@";
      }
    } else {
      $work = make_regex($work);
    }

    if ($str =~ m/$work/) {
      # マッチした
      $matched = $include;
      return $matched if  $match_type == 1;
    }
  }
  return $matched;
}


# channel version
# Mask::match_chan($mask, $user_long, $ch_long).
# $mask      = '#{example}@ircnet,-#{example2}@2ch   +*!*@*.example.com'
# $user_long = 'nick!user@remote'
# $ch_long   = '#chan@ircnet:*.jp'
# ユーザ名/チャンネル名のマッチング.
sub match_chan {
  my ($masks, $str, $chan, $match_type, $use_re, $use_flag) = @_;
  if (!defined $masks || !defined $str) {
    return undef;
  }

  return match_array_chan(_split_with_chan($masks), $str, $chan, $match_type, $use_re, $use_flag);
}

sub match_deep_chan {
  my ($masks_array, $str, $chan, $g_match_type, $match_type, $use_re, $use_flag) = @_;
  if (!defined $masks_array) {
    return undef;
  }

  $g_match_type = 1 unless defined $g_match_type;

  my $g_matched = undef;
  foreach my $masks (@$masks_array) {
    my $matched = match_array_chan(_split_with_chan($masks), $str, $chan, $match_type, $use_re, $use_flag);
    if (defined $matched) {
      $g_matched = $matched;
      return $g_matched if $g_match_type == 1;
    }
  }

  return $g_matched;
}

my $chanmask_mode = undef; # undefined,
my $CHANMASK_TIARRA = 1;
my $CHANMASK_PLUM = 2;

# tiarra Configuration check;
sub _check_chanmask_conf {
  # configuration を読み、chanmask_mode を決定する。
  use Configuration;

  my $maskmode = Configuration::shared_conf->general->chanmask_mode;
  if (defined $maskmode) {
    if ($maskmode =~ /plum/i) {
      $chanmask_mode = $CHANMASK_PLUM;
    } elsif ($maskmode =~ /tiarra/i) {
      $chanmask_mode = $CHANMASK_TIARRA;
    } else {
      ::printmsg('Configure_variable [maskmode] ' . $maskmode . ' is not known... use Tiarra mode.');
      $chanmask_mode = $CHANMASK_TIARRA;
    }
  } else {
    # fallback
    $chanmask_mode = $CHANMASK_TIARRA;
  }
}

sub match_array_chan {
  # $match_type: 0: last matching rule, 1: first matching rule
  # $use_re    : use 're:' feature.
  # $use_flag  : use [+-] match flag.

  # <return value> : status { 1 (true)  => +, matched,
  #                           0 (false) => -, matched,
  #                            (undef)  => no-match }
  my ($usermask_array, $chanmask_array, $str, $chan, $match_type, $use_re, $use_flag) = @_;

  return undef if (!defined $str);
  foreach my $var ($usermask_array, $chanmask_array) {
    return undef if (!defined $var || ref($var) ne 'ARRAY');
  }

  _check_chanmask_conf() if (!defined($chanmask_mode));

  my ($chanmask_use_flag);
  if ($chanmask_mode == $CHANMASK_TIARRA) {
    $chanmask_use_flag = $use_flag;
  } elsif ($chanmask_mode == $CHANMASK_PLUM) {
    $chanmask_use_flag = 0;
  } else {
    croak 'chanmask_mode is unsupported value!';
  }

  # channelマッチを行ってからuserマッチを行う。
  # channelマッチではflagは使わない。
  my $matched = undef;
  if (Multicast::channel_p($chan)) {
    # $chanがchannelの時は普通にマッチ。
    $matched = match_array($chanmask_array, $chan, $match_type, $use_re, $chanmask_use_flag);
  } else {
    # $chanがchannelでないときはpriv等なので * にマッチさせる。
    $matched = match_array($chanmask_array, '*', $match_type, $use_re, $chanmask_use_flag);
  }

  $matched = undef unless $matched; # matchしなかったらundefを代入する
  # channelでマッチしなかったらこの行は無視する。
  if (defined $matched) {
    $matched = undef;
    $matched = match_array($usermask_array, $str, $match_type, $use_re, $use_flag);
  }

  return $matched;
}

# support functions
my $cache_limit = 150;
my @cache_keys;
my %cache_table;
sub make_regex {
    my $str = $_[0];

    if (my $cached = $cache_table{$str}) {
	$cached;
    }
    else {
	# キャッシュされていない。
	if (@cache_keys >= $cache_limit) {
	    # キャッシュされている値をランダムに一つ消す。
	    my $to_delete = scalar(splice @cache_keys, int(rand @cache_keys), 1);
	    delete $cache_table{$to_delete};
	}

	my $compiled = compile($str);
	push @cache_keys, $str;
	$cache_table{$str} = $compiled;
	
	$compiled;
    }
}

sub compile {
    # $mask: マスク文字列
    # $consider_case: 真なら、大文字小文字を区別する。
    my ($mask, $consider_case) = @_;

    if (!defined $mask) {
	return qr/(?!)/; # マッチしない正規表現
    }

    my $regex = quotemeta($mask);
    $regex =~ s/\\\?/./g;
    $regex =~ s/\\\*/.*/g;
    #$regex =~ s/\\\#/\\d*/g;
    $regex = "^$regex\$";
    if ($consider_case) {
	qr/$regex/;
    }
    else {
	qr/$regex/i;
    }
}

sub _split {
    # ',' でわけられたマスクを配列にする。
    my $mask = shift;
    return () if !defined $mask;

    return map {
	s/\\,/,/g;
	$_;
    } split /(?<!\\),/,$mask;
}

sub _split_with_chan {
    # チャンネル付きマスクを配列にする。
    # パラメータ: mask プロパティの配列
    # output (user-array-ref, channel-array-ref)
    _check_chanmask_conf() if (!defined($chanmask_mode));

    if ($chanmask_mode == $CHANMASK_TIARRA) {
	my ($chan, $user) = split(/\s+/, shift, 2);

	return [_split($user)], [_split($chan)];
    } elsif ($chanmask_mode == $CHANMASK_PLUM) {
	my ($user, @chanarray) = split(/\s+/, shift);

	@chanarray = '*' unless @chanarray;

	@chanarray = map {
	    s/\\,/,/g;
	    $_;
	} map {
	    split /(?<!\\),/;
	} @chanarray;

	return [_split($user)], [@chanarray];
    } else {
	croak 'chanmask_mode is unsupported value!';
    }
}

# not related but often use
sub array_or_default {
  my ($default, @array) = @_;

  unless (@array) {
    return $default;
  } else {
    return @array;
  }
}

sub array_or_all {
  return array_or_default(all_mask(), @_);
}

sub array_or_all_chan {
  return array_or_default(all_chan_mask(), @_);
}

sub all_mask {
  return '*';
}

sub all_chan_mask {
  _check_chanmask_conf() if (!defined($chanmask_mode));
  if ($chanmask_mode == $CHANMASK_TIARRA) {
    return '* *!*@*';
  } elsif ($chanmask_mode == $CHANMASK_PLUM) {
    return '*!*@*';
  } else {
    croak 'chanmask_mode is unsupported value!';
  }
}


1;