In the Fatāwā of Shaykh al-Islām Ibn Taymiyyah the following is recorded:
“Question: Is it permissible to sleep in the mosque, to speak there, and to walk with footwear in places set aside for prayer, or not?
Answer: Sleeping there occasionally for someone in need—such as a stranger or a poor man who has no lodging—is permissible. But making the mosque one’s regular place to spend the night or take a midday nap is disapproved. Speech that Allah and His Messenger love is commendable in the mosque, whereas anything unlawful is even more strictly forbidden inside it, and what is disliked remains disliked; superfluous permissible talk is also discouraged. As for walking in the mosque with sandals, it is allowed, just as the Companions used to walk in the Prophet’s Mosque wearing theirs. Nevertheless, when a man comes to the mosque he should do as the Messenger of Allah ﷺ commanded: he should inspect his sandals, and if there is any filth on them he should rub them against the ground, for earth serves to cleanse them. And Allah knows best.”
I add: Regarding the oft-quoted saying that is even posted on the walls of many mosques—“Talking in the mosque consumes good deeds just as fire consumes firewood, or as cattle eat grass”—al-Ḥāfiẓ al-ʿIrāqī said in his Takhrīj al-Iḥyāʾ: “I have found no trace of it.” Al-Ṣaghānī, in his work on fabricated reports (al-Mawḍūʿāt), wrote: “Among the forged ḥadiths is the statement, ‘Whoever speaks of worldly matters in the mosques, Allah will erase his deeds for forty years.’ Likewise, the reports that claim special merit for lamps, lanterns, or mats in the mosque are all spurious; none of them is sound. In reality, the Companions would at times converse in the mosque and would also sleep there, but …”