Skip to content
Snippets Groups Projects
Commit 00db1177 authored by Vincent Massol's avatar Vincent Massol
Browse files

XWIKI-8340: Exception in console when the IRC Bot quits the channel

+ Protect against errors when the bot is not yet fully connected to the channel
parent 8ee73cb2
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
package org.xwiki.ircbot.internal; package org.xwiki.ircbot.internal;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -127,7 +128,13 @@ public void onEvent(Event event, Object source, Object data) ...@@ -127,7 +128,13 @@ public void onEvent(Event event, Object source, Object data)
getNotificationAuthor(event, document), getNotificationAuthor(event, document),
getNotificationComment(event, document), getNotificationComment(event, document),
getNotificationURL(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) { } catch (IRCBotException e) {
// Failed to handle the event, log an error // Failed to handle the event, log an error
......
...@@ -148,51 +148,55 @@ ...@@ -148,51 +148,55 @@
#set ($data = $configDoc.getObject("IRC.LoggingBotListener")) #set ($data = $configDoc.getObject("IRC.LoggingBotListener"))
#set ($pageScript = $data.getProperty("page").getValue()) #set ($pageScript = $data.getProperty("page").getValue())
#set ($pageName = $logListenerDoc.getRenderedContent($pageScript, $logListenerDoc.syntax.toIdString(), "plain/1.0")) #set ($pageName = $logListenerDoc.getRenderedContent($pageScript, $logListenerDoc.syntax.toIdString(), "plain/1.0"))
#set ($logDoc = $xwiki.getDocument($pageName)) ## Protection against rogue script execution: only do something if the evaluated script returns a page name starting
## ============================================ ## with "IRC."
## If the Doc doesn't exist, create it #if ($pageName.startsWith("IRC."))
## ============================================ #set ($logDoc = $xwiki.getDocument($pageName))
#if ($logDoc.isNew()) ## ============================================
## =========================================== ## If the Doc doesn't exist, create it
## Set the title (find the channel) ## ============================================
## =========================================== #if ($logDoc.isNew())
#set ($irccontext = $services.ircbot.context) ## ===========================================
#if ("$!irccontext" != "") ## Set the title (find the channel)
#set ($channel = "$!irccontext.channel.name") ## ===========================================
#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" == "") #if ("$!channel" == "")
#set ($channel = "$!irccontext.bot.channelsNames.iterator().next()") #set ($channel = "&lt;unknown&gt;")
#end #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 #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 #end
#set ($titleDate = $datetool.get('dd MMMM yyyy')) #set ($discard = $logDoc.setContent($newContent))
#set ($discard = $logDoc.setTitle("IRC Archive for channel #$channel on $titleDate")) ## ============================================
## =========================================== ## Save it!
## Set other metadata ## ============================================
## =========================================== #set ($discard = $logDoc.save("Logging from IRC", true))
#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")
#end #end
#set ($discard = $logDoc.setContent($newContent))
## ============================================
## Save it!
## ============================================
#set ($discard = $logDoc.save("Logging from IRC", true))
{{/velocity}}</code> {{/velocity}}</code>
</property> </property>
<property> <property>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment