The sunroof drains are probably blocked. When this happens the tray surrounding the sunroof fills up with water and overflows, and the water just finds it way downwards, ending up wherever gravity and chaos theory takes it. You will end up with wet carpet as ultimately that's the bottom of the car.
Get a length of line trimmer plastic cord, open your sunroof, put a towel or something over your seats, and start cleaning out the gunk. Eventually you should find the holes in the front corners of the drainage tray, which lead to the drain channels. Feed the line trimmer cord door the holes, working it backwards and forwards. Hopefully you'll here a gurgling sound and water will drain out behind the front wheels when the blockage is cleared. You can also use a household water spray to help clean out the gunk around the sunroof.
Do you know how fast your E39 is?