| 1 |
<?php
|
| 2 |
// $Id: bot_twitter.module,v 1.2 2008/06/25 21:47:06 robloach Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enables the ability for users to post Twitter updates from IRC.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_help().
|
| 11 |
*/
|
| 12 |
function bot_twitter_help($path, $arg) {
|
| 13 |
switch ($path) {
|
| 14 |
case 'irc:features':
|
| 15 |
return array(t('Twitter'));
|
| 16 |
case 'irc:features#twitter':
|
| 17 |
return t('Post updates directly to Twitter by using "!botname: twitter <message>".', array('!botname' => variable_get('bot_nickname', 'BOTNAME')));
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_perm().
|
| 23 |
*/
|
| 24 |
function bot_twitter_perm() {
|
| 25 |
return array('make twitter bot updates');
|
| 26 |
}
|
| 27 |
|
| 28 |
/**
|
| 29 |
* Listen for conversation directed at, or around, the bot.
|
| 30 |
*
|
| 31 |
* @param $data
|
| 32 |
* The regular $data object prepared by the IRC library.
|
| 33 |
* @param $from_query
|
| 34 |
* Boolean; whether this was a queried request.
|
| 35 |
*/
|
| 36 |
function bot_twitter_irc_msg_channel($data, $from_query = FALSE) {
|
| 37 |
$bot_name = variable_get('bot_nickname', 'bot_module');
|
| 38 |
$addressed = $from_query ? '' : "\s*${bot_name}[\:,-]\s*"; //"\s*${bot_name}[\:,-]\s*"; // bot mentioned?
|
| 39 |
$to = $from_query ? $data->nick : $data->channel;
|
| 40 |
|
| 41 |
// Check if they're posting a note to Twitter
|
| 42 |
if (preg_match("/^($addressed)twitter? (.*)$/i", $data->message, $matches)) {
|
| 43 |
$user = bot_authenticate($data);
|
| 44 |
if (user_access('make twitter bot updates', $user)) {
|
| 45 |
module_load_include('inc', 'twitter');
|
| 46 |
$twitter_accounts = twitter_get_user_accounts($user->uid);
|
| 47 |
if (!empty($twitter_accounts)) {
|
| 48 |
foreach ($twitter_accounts as $screen_name => $account) {
|
| 49 |
if (!empty($screen_name)) {
|
| 50 |
$result = twitter_set_status($screen_name, $account['password'], $matches[2]);
|
| 51 |
if ($result->code == 200) {
|
| 52 |
bot_message($to, t('!nick: Successfully posted to Twitter as !screen_name.', array('!nick' => $data->nick, '!screen_name' => $screen_name)));
|
| 53 |
}
|
| 54 |
else {
|
| 55 |
bot_message($to, t('!nick: Failed posting to Twitter as !screen_name.', array('!nick' => $data->nick, '!screen_name' => $screen_name)));
|
| 56 |
}
|
| 57 |
}
|
| 58 |
}
|
| 59 |
}
|
| 60 |
else {
|
| 61 |
bot_message($to, t('!nick: You do not have any Twitter accounts setup.', array('!nick' => $data->nick)));
|
| 62 |
}
|
| 63 |
}
|
| 64 |
else {
|
| 65 |
bot_message($to, t('!nick: You do not have access to post to Twitter.', array('!nick' => $data->nick)));
|
| 66 |
}
|
| 67 |
}
|
| 68 |
}
|
| 69 |
|
| 70 |
/**
|
| 71 |
* All responses are available via a query.
|
| 72 |
*/
|
| 73 |
function bot_twitter_irc_msg_query($data) {
|
| 74 |
bot_twitter_irc_msg_channel($data, TRUE);
|
| 75 |
}
|