Skip to content

Instantly share code, notes, and snippets.

@pvmchau
Created September 4, 2012 07:36
Show Gist options
  • Save pvmchau/3618037 to your computer and use it in GitHub Desktop.
Save pvmchau/3618037 to your computer and use it in GitHub Desktop.
Bot IRC Mention Module
<?php
/**
* Listen for conversation directed at, or about, the bot.
*
* @param $data
* The regular $data object prepared by the IRC library.
* @param $from_query
* Boolean; whether this was a queried request.
*/
function bot_mention_irc_msg_channel($data, $from_query = FALSE) {
$to = $from_query ? $data->nick : $data->channel;
$addressed = bot_name_regexp();
$all_nicks = bot_seen_all_nicks_for_regexp();
// Kiểm tra khi người dùng sử dụng mention
if (preg_match("/^($addressed)mention\s+([a-zA-Z0-9\[\]\{\}\\\|\^\`\-\_\*]*)[:;,]?\s+(.*)$/i", $data->message, $matches)) {
$recipient = $matches[3];
if(in_array($recipient, $all_nicks)){
if(bot_mention_condition_nick_on_channel($recipient, $data->channel)){
}
}
//bot_message($to, t("!nick: I'll pass that on when !recipient is around.", array('!nick' => $data->nick, '!recipient' => $matches[3])));
}
}
/**
* The condition function for 'botrules_condition_bot_on_channel'.
*
* @param string $channel
* Channel name.
*/
function bot_mention_condition_bot_on_channel($channel) {
global $irc;
// Rule fired from a bot event, $irc is directly available.
if (is_object($irc)) {
if (isset($irc->_channels[$channel])) {
return TRUE;
}
}
// Rule fired from elsewhere, look up cached bot data.
else {
$bot = cache_get('botrules_user_data');
if (isset($bot->data[$channel])) {
return TRUE;
}
}
return FALSE;
}
/**
* The condition function for 'botrules_condition_nick_on_channel'.
*
* @param string $nick
* Nickname.
* @param string $channel
* Channel name.
*/
function bot_mention_condition_nick_on_channel($nick, $channel) {
global $irc;
$nick = drupal_strtolower($nick);
// Rule fired from a bot event, $irc is directly available.
if (is_object($irc)) {
if (isset($irc->_channels[$channel]->users[$nick])) {
return TRUE;
}
}
// Rule fired from elsewhere, look up cached bot data.
else {
$bot = cache_get('botrules_user_data');
if (in_array($nick, $bot->data[$channel])) {
return TRUE;
}
}
return FALSE;
}
/**
* Return an array of all current nicks.
*/
function bot_mention_all_nicks_for_regexp() {
global $irc;
$nicks = array();
foreach ($irc->channel as $channel_name => $data) {
foreach ($irc->channel[$channel_name]->users as $nick_name => $data) {
$nick_name = preg_quote($nick_name, '/');
$nick_name = preg_replace('/^(\\\\?.)(.*)/', '(\1)(\2)', $nick_name);
$nicks[] = '/\b' . $nick_name . '\b/i';
}
}
return array_unique($nicks);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment