Search code examples
cparsingirc

Parse IRC PRIVMSG in C


I am quite new to C (I am more used to C++) and I am trying to create an IRC Bot. I am currently struggling to find the correct string parsing functions to parse this line:

:nick!~username@server PRIVMSG #channel :message (could contain the word PRIVMSG)

So, I am asking if anyone could show me what functions I would use to split up this line into:

  • nick
  • username
  • server
  • channel
  • message

Thanks for any help!


Solution

  • I'd probably use sscanf. Something on this general order seems to be a reasonable starting point:

    char nick[32], user[32], server[32], channel[32], body[256];
    
    sscanf(buffer, ":%31[^!]!~%31[^@]@%31s PRIVMSG #%31s :%255[^\n]", 
                     nick,     user, server,       channel, body);