-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-it.pl
executable file
·291 lines (264 loc) · 7.81 KB
/
post-it.pl
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
#!/usr/bin/env perl
# See LICENCE for licence details
# Or go to http://files.entropy.net.nz/LICENCE
use warnings;
use strict;
use POE;
use POE::Component::IRC;
use utils;
my ($irc, $server, $port, $ownernick, $nickname, %config, %todo);
my @help = ("post-it - To-do list/reminder IRC bot\n",
"Commands:\n",
"!todo list <n> Show your to-do list, or the nth item if given.\n",
"!todo add <reminder> Add <reminder> to your list\n",
"!todo remove <text> Remove all reminders containing <text>\n",
"!todo removepos <n> Remove remonder at position <n>\n");
sub command
{
my ($todo_ref, $irc, %info) = @_;
my $command = $info{'command'};
# !todo....
if ($command =~ /^!todo/)
{
if (not $todo_ref->{$info{'ident'}})
{
print "No to-do list in memory for $info{'ident'}, attempting to load\n";
if (-e "$info{'ident'}\.todo")
{
# load todo list
open(TODO, "<", "$info{'ident'}.todo") || warn "Couldn't open $info{'ident'}: $!\n";
return if (tell(TODO) == -1);
while(<TODO>)
{
chomp($_);
push(@{$todo_ref->{$info{'ident'}}}, $_);
}
close(TODO);
print "Loaded todo list for $info{'ident'}\n";
}
else
{
print "No list exists.\n";
}
}
# todo list
if ($command =~ /^!todo( list)* *(\d+)*$/)
{
if ($2 ne "")
{
list($todo_ref, $irc, $2 ,%info)
}
else
{
list($todo_ref, $irc, 0, %info);
}
}
# add to list
elsif ($command =~ /^!todo add (.+)$/)
{
add($todo_ref, $irc, %info);
}
#todo remove
elsif ($command =~ /^!todo remove (.+)$/)
{
remove($todo_ref, $irc, %info);
}
# remove at position
elsif ($command =~ /^!todo removepos (\d+)/)
{
removepos($todo_ref, $irc, %info);
}
# get help from the bot
elsif ($command =~ /^!todo help/)
{
foreach my $line (@help)
{
$$irc->yield('privmsg', $info{'nick'}, "$line");
}
}
}
}
# add to the todo list
sub add
{
my ($todo_ref, $irc, %info) = @_;
push(@{$todo_ref->{$info{'ident'}}}, $1) if ($todo_ref->{$info{'ident'}});
open(TODO, ">>", "$info{'ident'}.todo") || warn "Could not open to-do list file: $!\n";
return if (tell(TODO) == -1);
print "Writing $1 to $info{'ident'}'s list\n";
print TODO "$1\n";
close(TODO);
}
# list all items in todo list
sub list
{
my ($todo_ref, $irc, $index, %info) = @_;
# all lines
return if not $todo_ref->{$info{'ident'}};
return if ($#{$todo_ref->{$info{'ident'}}} < 0);
$index = 0 if $index < 0;
$index = $#{$todo_ref->{$info{'ident'}}} if $index > $#{$todo_ref->{$info{'ident'}}};
$$irc->yield('privmsg', $info{'nick'}, "$info{'nick'}'s to-do list: (" . ($#{$todo_ref->{$info{'ident'}}}+1) . " items)");
for (my $i = $index; $i < @{$todo_ref->{$info{'ident'}}}; $i++)
{
if ($i > ($index + $info{'limit'} - 1))
{
$$irc->yield('privmsg', $info{'nick'}, "---Limit reached.---");
return;
}
$$irc->yield('privmsg', $info{'nick'}, "$i: ${$todo_ref->{$info{'ident'}}}[$i]");
}
}
# list nth item in todo list
sub listone
{
my ($todo_ref, $irc, $index, %info) = @_;
if ($todo_ref->{$info{'ident'}})
{
$index = 0 if $index < 0;
$index = $#{$todo_ref->{$info{'ident'}}} if $index > $#{$todo_ref->{$info{'ident'}}};
return if $index < 0;
$$irc->yield('privmsg', $info{'nick'}, "Item no. $index: ${$todo_ref->{$info{'ident'}}}[$index]");
}
}
# remove all matvhing entries
sub remove
{
my ($todo_ref, $irc, %info) = @_;
my @removes;
my @removes_indices;
# find items to remove
for (my $i = 0; $i < @{$todo_ref->{$info{'ident'}}}; $i++)
{
if (${$todo_ref->{$info{'ident'}}}[$i] =~ /$1/)
{
my $remove = ${$todo_ref->{$info{'ident'}}}[$i];
chomp $remove;
print "Will remove $remove\n";
push(@removes, $remove);
splice(@{$todo_ref->{$info{'ident'}}}, $i, 1);
$i--;
}
}
if (@removes)
{
open (TODO, ">", "$info{'ident'}.todo") || warn "Could not open to-to list file: $!\n";
return if (tell(TODO) == -1);
foreach my $entry (@{$todo_ref->{$info{'ident'}}})
{
print TODO "$entry\n";
print "Writing $entry";
}
close(TODO);
$$irc->yield('privmsg', $info{'nick'}, "Removed: ".join(", ", @removes));
}
}
sub removepos
{
my ($todo_ref, $irc, %info) = @_;
return if not ($todo_ref->{$info{'ident'}});
if ($1 > $#{$todo_ref->{$info{'ident'}}})
{
$$irc->yield('privmsg', $info{'channel'}, "Index $1 is larger than the last index of your todo list (" . ($#{$todo_ref->{$info{'ident'}}} + 1) . ") items.");
return;
}
else
{
my $removed = ${$todo_ref->{$info{'ident'}}}[$1];
splice(@{$todo_ref->{$info{'ident'}}}, $1, 1);
open (TODO, ">", "$info{'ident'}.todo") || warn "Could not open to-to list file: $!\n";
return if (tell(TODO) == -1);
foreach my $entry (@{$todo_ref->{$info{'ident'}}})
{
print TODO "$entry\n";
print "Writing $entry\n";
}
close(TODO);
$$irc->yield('privmsg', $info{'nick'}, "Removed: $removed");
}
}
#############################
####### IRC functions #######
############################
# The bot session has started. Select a nickname. Connect to a server.
sub bot_start
{
%config = utils::parse_config("./settings.conf");
# validation
$config{'nick'} = "post-it" if not $config{'nick'};
$config{'username'} = "post-it" if not $config{'username'};
$config{'ircname'} = "post-it" if not $config{'ircname'};
$config{'server'} = "irc.segfault.net.nz" if not $config{'server'};
$config{'port'} = "6668" if not $config{'port'};
$config{'channel'} = "#bots" if not $config{'channel'};
$config{'limit'} = 5 if not $config{'limit'};
print "Connecting...\n";
$irc->yield(register => "all");
$irc->yield(
connect => {
Nick => $config{'nick'},
Username => $config{'username'},
Ircname => $config{'ircname'},
Server => $config{'server'},
Port => $config{'port'},
}
);
}
# The bot has successfully connected to a server. Join the configured channel
sub on_connect
{
print "Successfully connected.\n";
print "Joining $config{'channel'}\n";
$irc->yield(join => $config{'channel'});
}
# The bot has disconnected from the server, exit.
sub on_disconnect
{
print "Disconnected from server\n";
exit 0;
}
# Socket error, print the error and exit.
sub on_socket_error
{
my $error = $_[ARG0];
print "Error connecting to server: $error\n";
exit 0;
}
# actual communication stuff
# The bot has received a public message. Print and log it
sub on_public
{
my ($kernel, $who, $where, $msg) = @_[KERNEL, ARG0, ARG1, ARG2];
if ($msg =~ /^!/)
{
my @user = (split(/!/, $who));
my $channel = $where->[0];
my %info_to_pass = ('nick' => $user[0], 'ident' => $user[1], 'channel' => $channel, 'command' => $msg, 'limit' => $config{'limit'});
command(\%todo, \$irc, %info_to_pass);
}
}
# The bot has recieved a private message. Parse it for commands
sub on_msg
{
my ($kernel, $who, $where, $msg) = @_[KERNEL, ARG0, ARG1, ARG2];
my @user = (split(/!/, $who));
my $channel = $where->[0];
my %info_to_pass = ('nick' => $user[0], 'ident' => $user[1], 'channel' => $user[0], 'command' => $msg, 'limit' => $config{'limit'});
command(\%todo, \$irc, %info_to_pass);
}
# Create the component that will represent an IRC network.
$irc = POE::Component::IRC->spawn();
# Create the bot session. The new() call specifies the events the bot
# knows about and the functions that will handle those events.
POE::Session->create(
inline_states => {
_start => \&bot_start,
irc_001 => \&on_connect,
irc_disconnected => \&on_disconnect,
irc_socketerr => \&on_socket_error,
irc_public => \&on_public,
irc_msg => \&on_msg,
},
);
$poe_kernel->run();
exit 0;