I thikn you're going to have a hard time with this one. If I'm not mistaken the IP address will not always be there and the exact format can vary. You'll have to tweak the regex to fit:
You'll have to work out looping though your messages/folders bit but that is the easy part
You may also want to look at the Mail::Box[1] suite of modules. That will give you a nice set of objects to manipulate the folders/messages/headers.
~Charlie
[1]
http://search.cpan.org/~markov/Mail-...b/Mail/Box.pod