summaryrefslogtreecommitdiffstats
path: root/scripts/mood.pl
blob: 660dabaae6188d01f77b85c9941895b2dac3a63b (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
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
#!/usr/bin/perl
#
# This script tracks the general mood in a channel.
# 
#
# Changelog:
# 19.03.2002
# *first release
#
# 20.03.2002
# *some regexp tweaking
#
# 07.04.2002
# *own messages can be interpreted
#
# 05.01.2019
# *some regexp tweaking

use strict;

use vars qw($VERSION %IRSSI);

$VERSION = "20190105";
%IRSSI = (
    authors     => "Stefan 'tommie' Tomanek",
    contact     => "stefan\@pico.ruhr.de",
    name        => "Mood",
    description => "Keeps track of the channel mood",
    license     => "GPLv2",
    sbitems     => "moodbar",
    changed     => "$VERSION",
);

use Irssi;
use Irssi::TextUI;
use vars qw(%channels $eye $refresh $shouting $bored_mouth);

sub find_smiley {
    my ($msg) = @_;
    my $eyes = '[:=8;]';
    my $noses = '[\-o]?';
    my $sad = '[\(\<\[]';
    my $happy = '[\)\>\]D]';
    my %smiley = ($eyes.$noses.$happy         =>  10,
		  $sad.$noses.$eyes           =>  10,
		  $eyes.$noses.$sad           => -10,
		  $happy.$noses.$eyes         => -10,
		  $eyes.'\.+'.$noses.$sad     => -20,
		  $happy.$noses.'\.+'.$eyes   => -20,
		 );
    foreach (keys(%smiley)) {
	return($smiley{$_}) if ($msg =~ m/.*($_).*/);
    }
    return 0;
}

sub event_event_privmsg {
    my ($server, $data, $nick, $address) = @_;
    my ($target, $msg) = split(/ :/, $data,2);
    change_mood($target, find_smiley($msg));
}

sub event_message_own_public {
    my ($server, $msg, $target) = @_;
    change_mood($target, find_smiley($msg));
}

sub event_message_kick {
    my ($server, $channel, $nick, $kicker, $address, $reason) = @_;
    change_mood($channel, -20);
}

sub event_ban_new {
    my ($channel, $ban) = @_;
    my $name = $channel->{name};
    change_mood($name, -20);
}

sub event_ban_remove {
    my ($channel, $ban) = @_;
    my $name = $channel->{name};
    change_mood($name, 20);
}

sub event_netsplit_new {
    my ($netsplit) = @_;
    #FIXME Not Idea :)
    #Irssi::print $netsplit->{nick};
}

sub event_window_hilight {
    my ($window) = @_;
    open_mouth();
}

sub change_mood {
    my ($name, $points) = @_;
    if (not exists $channels{$name}) {
	$channels{lc $name} = 0;
    }
    $channels{lc $name} += $points;
    mood_refresh();
}

sub draw_smiley {
    my ($points) = @_;
    
    my $mouth = $bored_mouth;
    my $nose = Irssi::settings_get_str('mood_nose');
    
    if    ($points > 20) { $mouth = 'D'; }
    elsif ($points >  0) { $mouth = ')'; }
    elsif ($points <-20) { $mouth = '<'; }
    elsif ($points <  0) { $mouth = '('; }
    if ($shouting) { $mouth = 'O' };
    return $eye.$nose.$mouth;
}

sub mood_show {
    my ($item, $get_size_only) = @_;
    my $win = !Irssi::active_win() ? undef : Irssi::active_win()->{active};
    
    if (ref $win && ($win->{type}) and $win->{type} eq "CHANNEL") {
	my $target = lc $win->{name};
	my $face = draw_smiley($channels{$target});
	my $format = "{sb ".$face."}";
	$item->{min_size} = $item->{max_size} = length($face);
	$item->default_handler($get_size_only, $format, 0, 1);
    } else {
	$item->{min_size} = $item->{max_size} = 0;
    }
}

sub mood_decay {
    foreach (keys %channels) {
	if    ($channels{$_} < 0) {
	    $channels{$_}++;
	    mood_refresh() if (! draw_smiley($channels{$_}) eq draw_smiley($channels{$_}-1));
	} elsif ($channels{$_} > 0) {
	    $channels{$_}--;
	    mood_refresh() if (! draw_smiley($channels{$_}) eq draw_smiley($channels{$_}+1));
	}
    }
}

sub close_eyes {
    ($refresh) && Irssi::timeout_remove($refresh);
    $eye = '|';
    mood_refresh();
    $refresh=Irssi::timeout_add(200, 'open_eyes' , undef);
}

sub open_eyes {
    ($refresh) && Irssi::timeout_remove($refresh);
    $eye = ':';
    mood_refresh();
    my $min_delay = Irssi::settings_get_int('mood_blink');
    my $next_close = int( rand()*6000 + $min_delay );
    $refresh=Irssi::timeout_add($next_close, 'close_eyes', undef);
}

sub open_mouth {
    $shouting = 1;
    mood_refresh();
    Irssi::timeout_add(2000, 'close_mouth', undef);
}

sub close_mouth {
    Irssi::timeout_remove('close_mouth');
    $shouting = 0;
    mood_refresh();
}

sub mood_refresh {
    Irssi::statusbar_items_redraw('moodbar');
}

sub change_bored_mouth {
    $bored_mouth = ('\\\\\\\\', '|', '/')[int( rand(3) )];
}

#Irssi::signal_add('window item hilight', 'event_window_hilight');
Irssi::signal_add('event privmsg', 'event_event_privmsg');
Irssi::signal_add('message own_public', 'event_message_own_public');
Irssi::signal_add('message kick','event_message_kick');
Irssi::signal_add('ban new','event_ban_new');
Irssi::signal_add('ban remove','event_ban_remove');
Irssi::signal_add('netsplit new','event_netsplit_new');

Irssi::settings_add_int('misc', 'mood_blink', 6000);
Irssi::settings_add_str('misc', 'mood_nose', '-');

Irssi::statusbar_item_register('moodbar', 0, 'mood_show');

Irssi::timeout_add(5000, 'mood_decay', undef);
Irssi::timeout_add(10000, 'change_bored_mouth', undef);

close_mouth;
change_bored_mouth();
open_eyes();

# vim:set ts=8 sw=4: