Sleepaholic88 has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I asked this question a few days ago, but did not get an answer that worked when i tried it. I am looking for a way for my AIM-Bot (AIM Screenname: TestAIMBot) to open a file (screenname.emo) and if it doesnt exist, it makes it, and prints 0. Then, when someone is talking to the bot, and says "sorry", "sry" or "srry", it adds 1 to the file, thus making it 1, instead of 0, and add 1 for every time someone says "sorry", etc. I guess it would go something like this, but not sure:
if (! -e "emotions/$victim.emo") {
open (DATA, ">emotions/$victim.emo");
print DATA "0";
close (DATA);
}
if ($msg =~ /^sorry/ || $msg =~ /^srry/ || $msg =~ /^sry/)
{open (FILE, ">emotions/$victim.emo");
$emo = <FILE>;
print DATA "$emo ++ 1";
close(FILE);
return "it's OK.";
}
Any ideas? Feel free to talk to it, or talk to me on my AIM Screenname, Sleepaholic88. Thanks.
Re: Need more help with aimbot =)
by bbfu (Curate) on Mar 10, 2003 at 23:03 UTC
|
The following code should do pretty much what you want. Of course, it uses Net::AIM (you don't specify how you're connecting to AIM), so you can remove the AIM-related code if you like and just use the readcount / writecount / on_im subs.
#!/usr/bin/winperl
use warnings;
use strict;
use Net::AIM;
my $aim = Net::AIM->new();
$aim->newconn(
Screenname => "TestAIMBot",
Password => "TestAIMBot's Password",
);
my $conn = $aim->getconn() or die "Can't connect to AIM server.\n";
$conn->set_handler('im_in', \&on_im);
$aim->start;
sub on_im {
my ($self, $evt, $from, $to) = @_;
my ($nick, $auto, $msg) = @{$evt->args()};
# Normalize removes non-alpha-numeric characters.
# Note that if you don't remove non-word characters,
# opening a file based on the username becomes a
# security risk. Be careful.
my $filename = $self->normalize($from) . '.emo';
my $count = readcount($filename);
# will match 'sorry', 'srry', 'sry', and 'sory'
# Note that it's not anchored to the start of
# the string, as the word 'sorry' may appear
# anywhere in the message. Change if desired.
if($msg =~ /so?r?ry/) {
# Increase and save the count.
writecount($filename, ++$count);
}
$self->send_im($from, "You were sorry $count times.");
}
sub readcount {
my $filename = shift;
# If the file doesn't already exist, we will
# simply return a count of 0 and let writecount()
# create the file later on.
open my $fh, "< $filename" or return 0;
my $count = <$fh>;
close $fh;
return $count;
}
sub writecount {
my $filename = shift;
my $count = shift;
# If the file doesn't exist, this will create it.
open my $fh, "> $filename" or die "Can't open $filename for write: $
+!\n";
print $fh $count;
close $fh;
}
bbfu
Black flowers blossum
Fearless on my breath | [reply] [d/l] |
|
Thanks, but actually, I just needed this part:
if($msg =~ /so?r?ry/) {
# Increase and save the count.
writecount($filename, ++$count);
}
but i replace $filename with $screenname.emo, or whatever variable the for the screenname is, right? Thanks. | [reply] [d/l] |
|
Do you have to write this to a file? It would make more sense to me to store it in a hash with the screen name, i.e.
my %sorry;
if ($msg=~/so?r?ry/){
++$sorry{"$screenname"};
}
but of course, you may need the file for something else, in which case I'd just store it in a hash anyway and replace the number in the file when you got a 'sry' in a message. | [reply] [d/l] |
|
but i replace $filename with $screenname.emo, or whatever variable the for the screenname is, right?
Assuming you are certain $screenname doesn't contain anything except alpha-numeric characters (for instance, the character '/' could cause some havoc, though don't assume that's the only character to watch out for), then yes. You can replace $filename with something like "$screenname.emo"
DarknessX has a good point, though. If you don't need the count to be persistent across invocations of your bot (and you don't need the file for some other process), you would be better off just storing the count in a hash, as this would save a lot of (unnecessary) file I/O.
bbfu
Black flowers blossum
Fearless on my breath
| [reply] [d/l] [select] |
|
|
|
Wait, actually, i needed more than that. I also needed
sub readcount {
my $filename = shift;
# If the file doesn't already exist, we will
# simply return a count of 0 and let writecount()
# create the file later on.
open my $fh, "< $filename" or return 0;
my $count = <$fh>;
close $fh;
return $count;
}
sub writecount {
my $filename = shift;
my $count = shift;
# If the file doesn't exist, this will create it.
open my $fh, "> $filename" or die "Can't open $filename for write: $
+!\n";
print $fh $count;
close $fh;
}
Thank you very much! | [reply] [d/l] |
Re: Need more help with aimbot =)
by Nkuvu (Priest) on Mar 10, 2003 at 22:58 UTC
|
Two observations. One, your regex can be simplified. Try if ($msg =~ /so?r?ry) to replace that whole || business.
Two, why write the string of "some value ++ 1"? Your file contents will be something like "1 ++ 1", which I don't think is what you're looking for... but I could be misinterpreting your designs...
And one question. What's the problem?
Edit: Note that I'm not sure if this is a continuing question -- from the other replies it seems like it is. And the regex I posted here may have unintended side effects, like matching "sory" as well as the first three options (so my regex could probably use some work, too. ;) )
| [reply] [d/l] |
Re: Need more help with aimbot =)
by mowgli (Friar) on Mar 10, 2003 at 22:48 UTC
|
I'm not sure what exactly your question is now. In any case, I think that using a tied hash would be a better way to go; if you need a separate plain text file for each screen name, for example, Tie::Textdir might be helpful (or at least it looks helpful at first glance).
--
mowgli
| [reply] |
|
I looked at the Tie::TextDir info, but i do not think that that is needed. I am just wondering on how i can make the bot add 1 to the $screenname.emo file when someone says Sorry, srry, sry, and as Nkuvu and bbfu pointed out, sory, but, as I said before, I am not sure how to add a number to what the file allready has in it (maybe it doesnt recognize it as only numerals, and can't add anything?) Thanks.
| [reply] |
|
Read the existing number from the file, seek to the file's beginning and write back the new number. However, I really think those are gory details that would much better be hidden in a tied hash; Tie::TextDir may not be what you want, but you can always roll your own.
--
mowgli
| [reply] |
|
|