diff --git a/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-api/src/main/java/org/xwiki/ircbot/internal/DocumentModifiedEventListener.java b/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-api/src/main/java/org/xwiki/ircbot/internal/DocumentModifiedEventListener.java
index 9ede3b592c6522fc7234d6d7cc9e4440dcd81709..22421b25ad99cf71e4ee0d045f43f8afb9c9174f 100644
--- a/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-api/src/main/java/org/xwiki/ircbot/internal/DocumentModifiedEventListener.java
+++ b/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-api/src/main/java/org/xwiki/ircbot/internal/DocumentModifiedEventListener.java
@@ -20,6 +20,7 @@
 package org.xwiki.ircbot.internal;
 
 import java.util.Arrays;
+import java.util.Iterator;
 import java.util.List;
 import java.util.regex.Pattern;
 
@@ -127,7 +128,13 @@ public void onEvent(Event event, Object source, Object data)
                         getNotificationAuthor(event, document),
                         getNotificationComment(event, document),
                         getNotificationURL(event, document));
-                    this.bot.sendMessage(this.bot.getChannelsNames().iterator().next(), message);
+
+                    // Get the channel to which to send to. If there's no channel name it means the Bot hasn't joined
+                    // any channel yet so don't do anything!
+                    Iterator<String> channelNameItator = this.bot.getChannelsNames().iterator();
+                    if (channelNameItator.hasNext()) {
+                        this.bot.sendMessage(channelNameItator.next(), message);
+                    }
                 }
             } catch (IRCBotException e) {
                 // Failed to handle the event, log an error
diff --git a/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-ui/src/main/resources/IRC/IRCLogMacro.xml b/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-ui/src/main/resources/IRC/IRCLogMacro.xml
index cef7d01c12313cb816f0358aef0108830a279ae6..633f2360f5c2b0964b06c241b5633fd3276a3812 100644
--- a/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-ui/src/main/resources/IRC/IRCLogMacro.xml
+++ b/xwiki-platform-core/xwiki-platform-ircbot/xwiki-platform-ircbot-ui/src/main/resources/IRC/IRCLogMacro.xml
@@ -148,51 +148,55 @@
 #set ($data = $configDoc.getObject("IRC.LoggingBotListener"))
 #set ($pageScript = $data.getProperty("page").getValue())
 #set ($pageName = $logListenerDoc.getRenderedContent($pageScript, $logListenerDoc.syntax.toIdString(), "plain/1.0"))
-#set ($logDoc = $xwiki.getDocument($pageName))
-## ============================================
-## If the Doc doesn't exist, create it
-## ============================================
-#if ($logDoc.isNew())
-  ## ===========================================
-  ## Set the title (find the channel)
-  ## ===========================================
-  #set ($irccontext = $services.ircbot.context)
-  #if ("$!irccontext" != "")
-    #set ($channel = "$!irccontext.channel.name")
+## Protection against rogue script execution: only do something if the evaluated script returns a page name starting
+## with "IRC."
+#if ($pageName.startsWith("IRC."))
+  #set ($logDoc = $xwiki.getDocument($pageName))
+  ## ============================================
+  ## If the Doc doesn't exist, create it
+  ## ============================================
+  #if ($logDoc.isNew())
+    ## ===========================================
+    ## Set the title (find the channel)
+    ## ===========================================
+    #set ($irccontext = $services.ircbot.context)
+    #if ("$!irccontext" != "")
+      #set ($channel = "$!irccontext.channel.name")
+      #if ("$!channel" == "")
+        #set ($channel = "$!irccontext.bot.channelsNames.iterator().next()")
+      #end
+      #set ($channel = $stringtool.stripStart($channel, '#'))
+    #end
     #if ("$!channel" == "")
-      #set ($channel = "$!irccontext.bot.channelsNames.iterator().next()")
+      #set ($channel = "&lt;unknown&gt;")
     #end
-    #set ($channel = $stringtool.stripStart($channel, '#'))
+    #set ($titleDate = $datetool.get('dd MMMM yyyy'))
+    #set ($discard = $logDoc.setTitle("IRC Archive for channel #$channel on $titleDate"))
+    ## ===========================================
+    ## Set other metadata
+    ## ===========================================
+    #set ($discard = $logDoc.setParent('IRC.WebHome'))
+    #set ($discard = $logDoc.setCreator('IRCBot'))
+    #set ($archiveClassName = 'IRC.IRCBotArchiveClass')
+    #set ($archiveObject = $logDoc.getObject($archiveClassName, true))
+    #set ($discard = $archiveObject.set('channel', $channel))
   #end
-  #if ("$!channel" == "")
-    #set ($channel = "&lt;unknown&gt;")
+  ## ============================================
+  ## Add the new content to the logging doc
+  ## ============================================
+  #set ($currentContent = $logDoc.content)
+  #set ($newContent = $xcontext.macro.content)
+  #if ($currentContent &amp;&amp; $currentContent.length() &gt; 0)
+    #set ($logDate = $datetool.get('HH:mm'))
+    #set ($newContent = "$currentContent
+  $logDate $newContent")
   #end
-  #set ($titleDate = $datetool.get('dd MMMM yyyy'))
-  #set ($discard = $logDoc.setTitle("IRC Archive for channel #$channel on $titleDate"))
-  ## ===========================================
-  ## Set other metadata
-  ## ===========================================
-  #set ($discard = $logDoc.setParent('IRC.WebHome'))
-  #set ($discard = $logDoc.setCreator('IRCBot'))
-  #set ($archiveClassName = 'IRC.IRCBotArchiveClass')
-  #set ($archiveObject = $logDoc.getObject($archiveClassName, true))
-  #set ($discard = $archiveObject.set('channel', $channel))
-#end
-## ============================================
-## Add the new content to the logging doc
-## ============================================
-#set ($currentContent = $logDoc.content)
-#set ($newContent = $xcontext.macro.content)
-#if ($currentContent &amp;&amp; $currentContent.length() &gt; 0)
-  #set ($logDate = $datetool.get('HH:mm'))
-  #set ($newContent = "$currentContent
-$logDate $newContent")
+  #set ($discard = $logDoc.setContent($newContent))
+  ## ============================================
+  ## Save it!
+  ## ============================================
+  #set ($discard = $logDoc.save("Logging from IRC", true))
 #end
-#set ($discard = $logDoc.setContent($newContent))
-## ============================================
-## Save it!
-## ============================================
-#set ($discard = $logDoc.save("Logging from IRC", true))
 {{/velocity}}</code>
     </property>
     <property>