View Full Version : perl parsing
Awkwardsaw
06-25-2010, 02:47 AM
i'm making a perl irc bot, and im having trouble parsing this D:
i need to get 'akwardsaw!~myip PRIVMSG #ethan :s/lol//;' parsed into 2 different variables.. i need 'akwardsaw' in one, and every thing after 'PRIVMSG #ethan :' into an other, and im having lots of troubles with it D:
what i have to get the message:
$messge = $in =~ /PRIVMSG $chan \s+(.*)/;
where $in is the variable that holds the string, and $chan is the channel the bot is in. i suck at regex, and im sure im missing something :p
noidea
06-25-2010, 03:50 AM
Alright, heres a tip, irc servers do not allow the characters : ! ~ @ and space in the nick, name(I dont think) and ip.
idk if perl can be/is oop, but make a class/type/struct called Message. Every time a new message is sent, have a method parse it and create a new Message.
The line sent sent back from an irc server to the clients looks like
:noidea`!~noidea`@let.mee.frost.your.flakes PRIVMSG #SRL :testing
Have it go through the string and have it store the info.
:noidea`!~noidea`@let.mee.frost.your.flakes PRIVMSG #SRL :testing
name^....nick^...............ip^.................m sg type^..chan^..msg^
I think you know a bit of java, so here a crappy example I made
class Message { //prevent null showing up in the message
String original = "", sender = "", name = "", message="", ip = "", channel = "", type = "", originalmessage = "";
Tokenizer tokens = null;
String[] words = null, originalwords = null;
Core utils = new Core();
ArrayList<String> info = null;
public int MESSAGE_START_INDEX = 3;
//x x x x x x PRIVMSG channel
public void parsemessage(String line) {
//StringTokenizer newtokens = new StringTokenizer();
info = utils.multisplit(line, ":|!|~|@| ");
if (info.size() < 5) return;
original = line;
sender = info.get(utils.SENDER_IND);
name = info.get(utils.NAME_IND);
ip = info.get(utils.IP_IND);
type = info.get(utils.MSG_TYPE_IND);
channel = info.get(utils.CHANNEL_IND);
for (int i = utils.MESSAGE_IND; info.size() > i; i++) message += info.get(i)+ " ";
if (message.length() > 0) {
tokens = new Tokenizer(message);
words = message.split(" ");
}
originalwords = line.split(" ");
if (originalwords.length > 3) {
originalmessage += originalwords[MESSAGE_START_INDEX].substring(1);//get rid of the :
for (int i = MESSAGE_START_INDEX+1; originalwords.length > i; i++) {
originalmessage += " "+originalwords[i];
}
}
}
Message(String line) {
parsemessage(line);
}
}
Awkwardsaw
06-25-2010, 03:55 AM
thanks :) i'll look into it, try to port over to perl and stuff, but i was hoping for it to be in regex :( it makes it so much cleaner
noidea
06-25-2010, 03:58 AM
thanks :) i'll look into it, try to port over to perl and stuff, but i was hoping for it to be in regex :( it makes it so much cleaner
in java
String[] info = line.split(":|!|~|@| ");
but unfortunately that ruins whats after #channel :stuff to say
i luffs yeww
06-25-2010, 04:26 AM
I feel like this could be much easier..
Awkwardsaw
06-25-2010, 04:31 AM
I feel like this could be much easier..
idk, split helps a lot, but im to lazy atm to add it to the bot ;)
im torn between that and pos(m//)
Powered by vBulletin® Version 4.2.1 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.