html/extaway.pl
1 # Away Manager
2 #
3 # This script allows you to have different automated
4 # actions on away
5 #
6 # - sets your nick, using a base and a keyword
7 # - in example: Crazy_Away
8 # - insert an away reason
9 # - say on all channels that your away
10 #
11 # This is my first IRSSI script, but I'll try to make it
12 # better soon.
13 # If you have ideas to improve it, or make it more efficient,
14 # contact me at crazycat@c-p-f.org
15
16 use Irssi;
17 use Irssi::Irc;
18 use strict;
19
20 use vars qw($VERSION %IRSSI);
21
22 $VERSION = '1.0';
23 %IRSSI = (
24 authors => 'CrazyCat',
25 contact => 'crazycat@c-p-f.org',
26 name => 'ExtAway',
27 description => 'Extended Away & Back programm',
28 license => 'GNU GPLv2 or later',
29 changed => '$Date: 2005/01/12 03:04:01$'
30 );
31
32 my $xa_confile = Irssi::get_irssi_dir()."/xa.config";
33 my %infos = ();
34 my ($oldnick, $t_away);
35
36 sub init {
37 # verifying if settings file exists
38 if (!(open xa_settings, "<$xa_confile")) {
39 putlog("No config file: /xahelp for help");
40 return 0;
41 };
42 # reading the config file
43 while (my $line = <xa_settings>) {
44 $line =~ s/\n//;
45 my ($xa_key,$xa_val) = split(/:/, $line);
46 $xa_key =~ tr/[A-Z]/[a-z]/;
47 if ($xa_val ne "") {
48 $infos{$xa_key} = $xa_val;
49 }
50 }
51 close xa_settings;
52 }
53
54 sub xa_go {
55 # Main procedure to go away
56 my($data,$server,$channel) = @_;
57 my $xa_nick = "";
58 my $xa_reason = "";
59 if ($server->{usermode_away}) {
60 # oooops! already marged as away
61 &putlog("You're allready marqued as away ($server->{away_reason})");
62 return 0;
63 }
64 $oldnick = $server->{nick};
65 $t_away = time();
66 my $t_data = split(/ /, $data);
67 if ($t_data < 2) {
68 # away called with just a keyword
69 $xa_nick = $data;
70 $xa_nick =~ tr/[A-Z]/[a-z]/;
71 $xa_reason = $infos{$xa_nick};
72 } else {
73 # this is a new reason
74 ($xa_nick, $xa_reason) = $data =~ /^(\S+) (.*)/;
75 &xa_add($xa_nick, $xa_reason);
76 }
77 if ($xa_reason eq "") {
78 putlog("Sorry, <$xa_nick> is not defined");
79 } else {
80 my $nick = "$infos{'bnick'}$xa_nick";
81 foreach my $server (Irssi::servers) {
82 $server->command("AWAY $xa_reason");
83 $server->command("NICK $nick");
84 foreach my $chan ($server->channels) {
85 $server->command("DESCRIBE $chan->{name} is away [Reason: $xa_reason]");
86 }
87 }
88 }
89 }
90
91 sub xa_back {
92 # the way to be back
93 my($data,$server,$channel) = @_;
94 if (!$server->{usermode_away}) {
95 &putlog("You're not marqued as away");
96 return 0;
97 }
98 my $delay = time() - $t_away;
99 my $f_delay = f_delay($delay);
100 foreach my $server (Irssi::servers) {
101 foreach my $chan ($server->channels) {
102 $server->command("DESCRIBE $chan->{name} is back from [$server->{away_reason}] - $f_delay away");
103 }
104 $server->command("AWAY");
105 $server->command("NICK $oldnick");
106 }
107 }
108
109 sub putlog {
110 # procedure to write in status window
111 my ($window) = Irssi::active_win();
112 Irssi::print("[$IRSSI{'name'}] @_", MSGLEVEL_CLIENTNOTICE);
113
114 }
115
116 sub f_delay {
117 # formatting the away time
118 my $seconds = shift;
119 my ($hours, $minutes, $formated);
120 if ($seconds > 3600) {
121 $hours = int($seconds / 3600);
122 $formated .= $hours."h:";
123 $seconds = $seconds - ($hours * 3600);
124 }
125 if ($seconds > 60) {
126 $minutes = int($seconds / 60);
127 $formated .= $minutes."m:";
128 $seconds = $seconds - ($minutes * 60);
129 }
130 $formated .= $seconds."s";
131 return $formated;
132 }
133
134 sub xa_add {
135 # Adding the keyword and the reason in the config file
136 # may create double entries...
137 my($kw, $reason) = @_;
138 if(!(open xa_settings, ">>$xa_confile")) {
139 &putlog("Unable to open file $xa_confile");
140 }
141 print xa_settings "$kw:$reason\n";
142 close xa_settings;
143 $infos{$kw} = $reason;
144 }
145
146 sub xa_save {
147 # save the temp infos (might correct the double entries)
148 my ($data,$server,$channel) = @_;
149 if(!(open xa_settings, ">$xa_confile")) {
150 &putlog("Unable to create file $xa_confile");
151 }
152 print xa_settings "bnick:$infos{'bnick'}\n";
153 while (my ($kw, $line) = each %infos) {
154 if ($kw ne "bnick") {
155 print xa_settings "$kw:$infos{$kw}\n";
156 }
157 }
158 close xa_settings;
159 }
160
161 sub xa_nick {
162 # The way to add the base nick
163 my ($data,$server,$channel) = @_;
164 if ($data eq "") {
165 putlog("You must define your base_nick")
166 }
167 $infos{'bnick'} = $data;
168 &xa_save;
169 }
170
171 sub xa_help {
172 &putlog("Help for $IRSSI{name} : $IRSSI{description}");
173 &putlog(" Setting your base nick: /xanick <base_nick>");
174 &putlog(" Going away: /aw <keyword> [reason]");
175 &putlog(" if keyword exists in the base, reason is automatically displayed");
176 &putlog(" if keyword is a new one, you MUST give a reason");
177 &putlog(" -- Nota: your away nick will be <base_nick><keyword> --");
178 &putlog(" Coming back from away: /back");
179 &putlog(" Saving all datas: /xasave (take care, setting your base_nick will save all your datas)");
180 }
181 #
182 # main
183 #
184 &init();
185 if ($infos{'bnick'} eq "") {
186 &putlog("Please, give a base nick with /xanick <base_nick>");
187 &putlog("Use /xahelp to get some help");
188 }
189 Irssi::command_bind("aw", "xa_go");
190 Irssi::command_bind("back", "xa_back");
191 Irssi::command_bind("xahelp", "xa_help");
192 Irssi::command_bind("xanick", "xa_nick");
193 Irssi::command_bind("xasave", "xa_save");
194