File: Freeze.pm

package info (click to toggle)
tiarra 20100212-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,732 kB
  • ctags: 1,712
  • sloc: perl: 32,032; lisp: 193; sh: 109; makefile: 10
file content (256 lines) | stat: -rw-r--r-- 6,752 bytes parent folder | download | duplicates (4)
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
# -----------------------------------------------------------------------------
# $Id: Freeze.pm 11365 2008-05-10 14:58:28Z topia $
# -----------------------------------------------------------------------------
# このモジュールは再起動しても凍結設定を失なわないようにする為、
# 設定をBulletinBoardのfrost-channelsに保存します。
# -----------------------------------------------------------------------------
package Channel::Freeze;
use strict;
use warnings;
use base qw(Module);
use Multicast;
use Timer;
use BulletinBoard;
use Mask;

sub new {
    my $class = shift;
    
    my $this = $class->SUPER::new(@_);
    $this->{reminder_timer} = undef; # Timer
    $this->set_timer_if_required;
    
    $this;
}

sub destruct {
    my $this = shift;
    if (defined $this->{reminder_timer}) {
	$this->{reminder_timer}->uninstall;
	$this->{reminder_timer} = undef;
    }
}

sub set_timer_if_required {
    my $this = shift;
    if (defined $this->{reminder_timer}) {
	# 既にタイマーが入っている。
	return;
    }

    if (!$this->config->reminder_interval) {
	# 報告しないやうに設定されている。
	return;
    }

    my $channels = BulletinBoard->shared->frost_channels;
    if (defined $channels && keys(%$channels) > 0) {
	# 掲示板に情報が有る。
	$this->{reminder_timer} = Timer->new(
	    Interval => 60 * $this->config->reminder_interval,
	    Repeat => 1,
	    Code => sub {
		$this->notify_list_of_frost_channels;
	    })->install;
	#::printmsg("Channel::Freeze - timer installed");
    }
}

sub notify_list_of_frost_channels {
    my ($this) = @_;
    my $channels = BulletinBoard->shared->frost_channels;
    if (defined $channels && keys(%$channels) > 0) {
	# 報告内容を作る
	my $msg = "These channels are frost: ".join(', ',keys %$channels);
	if (length($msg) > 400) {
	    # 400バイトを越えたら切り詰める。
	    $msg = substr($msg, 0, 400)."...";
	}
	
	# 報告
	RunLoop->shared->broadcast_to_clients(
	    $this->construct_irc_message(
		Prefix => RunLoop->shared_loop->sysmsg_prefix(qw(priv system)),
		Command => 'NOTICE',
		Params => [
		    RunLoop->shared->current_nick,
		    $msg]
	    )
	);
    }
}

sub message_arrived {
    my ($this, $msg, $sender) = @_;
    
    if ($sender->client_p) {
	# コマンドの入力か?
	my $notify = sub {
	    my $notice = shift;
	    RunLoop->shared->broadcast_to_clients(
		$this->construct_irc_message(
		    Prefix => RunLoop->shared_loop->sysmsg_prefix(qw(priv system)),
		    Command => 'NOTICE',
		    Params => [
			RunLoop->shared->current_nick,
			$notice]
		)
	    );
	};
	
	if ($msg->command eq uc($this->config->freeze_command || 'freeze')) {
	    # 凍結
	    if (my @frost = $this->freeze($msg->param(0))) {
		$notify->("Channel ".join(', ', @frost)." frost.");
	    }
	    $msg = undef; # 捨て
	}
	elsif ($msg->command eq uc($this->config->defrost_command || 'defrost')) {
	    # 解凍
	    if (my @defrost = $this->defrost($msg->param(0))) {
		$notify->("Channel ".join(', ', @defrost)." defrost.");
	    }
	    $msg = undef; # 捨て
	}
    }
    else {
	# PRIVMSGやNOTICEか?
	if ($msg->command eq 'PRIVMSG' || $msg->command eq 'NOTICE') {
	    # 凍結されてゐるチャンネルが存在するか?
	    my $board = BulletinBoard->shared;
	    my $channels = $board->frost_channels;
	    if (defined $channels) {
		# 凍結されてゐるチャンネルか?
		if ($channels->{$msg->param(0)}) {
		    # do-not-send-to-clientsを付ける。
		    $msg->remark('do-not-send-to-clients', 1);
		}
	    }
	}
    }

    $msg;
}

sub normalize {
    my ($ch_full) = @_;
    my ($ch_short, $network_name) = Multicast::detach($ch_full);
    if (Multicast::channel_p($ch_short)) {
	# チャンネル名として許される。
	Multicast::attach($ch_short, $network_name);
    }
    else {
	# 許されない。
	undef;
    }
}

sub freeze {
    # 今囘のfreezeの呼出しでフリーズされたチャンネル名の配列を返す。
    my ($this, $ch_mask) = @_;

    if (!defined $ch_mask) {
	# リスト表示
	$this->notify_list_of_frost_channels;
	return ();
    }

    if (defined $ch_mask) {
	my $board = BulletinBoard->shared;
	my $channels = $board->frost_channels;
	
	if (!defined $channels) {
	    # まだ掲示板に入つてゐない。
	    $channels = {}; # {フルチャンネル名 => 1}
	    $board->frost_channels($channels);
	}
	
	# 全てのサーバーの、全てのjoinしているチャンネルの中から、
	# このマスクに該当するチャンネル名を探し、全てfreezeする。
	my @ch_to_freeze;
	foreach my $network (RunLoop->shared->networks_list) {
	    foreach my $ch ($network->channels_list) {
		my $longname = Multicast::attach($ch, $network);
		if (Mask::match($ch_mask, $longname)) {
		    if (!$channels->{$longname}) {
			$channels->{$longname} = 1;
			push @ch_to_freeze, $longname;
		    }
		}
	    }
	}

	# 必要ならタイマー起動。
	$this->set_timer_if_required;

	return @ch_to_freeze;
    }
    else {
	return ();
    }
}

sub defrost {
    my ($this, $ch_mask) = @_;
    if (!defined $ch_mask) {
	return ();
    }

    my @result;

    if (defined $ch_mask) {
	my $board = BulletinBoard->shared;
	my $channels = $board->frost_channels;

	if (!defined $channels) {
	    return; # 何も凍結されていない。
	}

	%$channels = map {
	    $_ => 1;
	} grep {
	    my $ch_full = $_;
	    if (Mask::match($ch_mask, $ch_full)) {
		push @result, $ch_full;
		0;
	    }
	    else {
		1;
	    }
	} keys %$channels;

	if (keys(%$channels) == 0) {
	    # 凍結されたチャンネルはもう無い。
	    if (defined $this->{reminder_timer}) {
		$this->{reminder_timer}->uninstall;
		$this->{reminder_timer} = undef;
	    }
	    #::printmsg("Channel::Freeze - timer DELETED");
	}
    }

    @result;
}

1;

=pod
info: 特定のチャンネルの発言を、一時的に受信するのをやめる。
default: off

# ログを取っているなら、ログには記録される。

# チャンネルの凍結に用いるコマンド名。
# 省略時は freeze であり、/freeze #channel@network のように使う。
# チャンネル名を省略すると、現在フリーズされているチャンネルのリストを表示する。
freeze-command: freeze

# 凍結解除に用いるコマンド名。
# 省略時は defrost であり、/defrost #channel@network のように使う。
defrost-command: defrost

# 凍結しているチャンネルが存在する時、一定時間毎にその旨を報告する事も可能。
# この機能は凍結した事を忘れないようにする為にある。
# 単位は分、デフォルトはゼロ(報告しない)。
reminder-interval: 30
=cut