IRC Plugin

Other plugins for extending OpenKore's functionality. This forum is only for posting new plugins and commenting on existing plugins. For support, use the Support forum.

Moderator: Moderators

Message
Author
sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

IRC Plugin

#1 Post by sli »

Here. Fix and finish mine. It runs just fine under 2.0.6.1.

Code: Select all

#############################
# ircControl v.1 by sli (GPL)
#
# See the forum for an explanation of the config.
#
# Sample config:
#
#	ircControl 1
#	ircControl_server irc.openkore.com
#	ircControl_servPass something
#	ircControl_port 6667
#	ircControl_nick SomeKoreBot
#	ircControl_nsPass nickpassword
#	ircControl_owner sli
#	ircControl_password blahblah
#	ircControl_channel #openkore
#	ircControl_key blahblah
#
#	To be added: multiple bot owners
#	ircControl_owners sli sli|school
#
#############################

package ircControl;

use strict;
use threads;

use Plugins;
use Globals qw(%config);
use Net::IRC;

use Switch;
use Data::Dumper;

Plugins::register('ircControl', 'Allows control of Kore over IRC.', \&onUnload);

my $hooks = Plugins::addHooks(
	['start3', \&onLoad, undef]
);

my $irc = new Net::IRC;
my ($loggedIn, @owners);

sub onLoad {
	@owners = split(/ /,$config{ircControl_owners});
	my $ircthread = threads->create('threadWrapper');
}

sub threadWrapper {
	if ($config{ircControl}) {
		print "[ircControl] Connecting to IRC...\n";

		my $conn = $irc->newconn(Nick		=> $config{ircControl_nick},
					 Server		=> $config{ircControl_server},
					 Port		=> $config{ircControl_port},
					 Ircname	=> $config{ircControl_nick},
					 Password	=> $config{ircControl_servPass});

		$conn->add_global_handler('376', \&onConnect);
		$conn->add_global_handler('msg', \&onMsg);

		$irc->start;
	}
}

sub onUnload {
	Plugins::delHooks($hooks);
}

sub onConnect {
	my $self = shift;

	if ($config{ircControl_nspass}) {
		$self->privmsg("Nickserv", "identify " . $config{ircControl_nsPass});
	}

	if ($config{ircControl_channel}) {
		$self->join($config{ircControl_channel});
	}

	foreach (@owners) {
		$self->privmsg($_,"OpenKore ircControl online. Waiting for login from owner.");
	}
}

sub onMsg {
	my ($self, $event) = @_;
	my $nick = $event->nick;
	my $rawMsg = $event->{args}[0];
	my @msg = split(/ /, $rawMsg);

	if (!$loggedIn && $msg[0] ne "login") {
		$self->privmsg($nick, "You are not logged in.");
	} else {
		if (arrayContains(\@owners, $nick)) {
			switch ($msg[0]) {
				case "login" {
					if ($msg[1] eq $config{ircControl_password}) {
						$loggedIn = $nick;
						$self->privmsg($nick, "You are now logged in.");
					} else {
						$self->privmsg($nick, "Invalid password.");
					}
				}
				case "last"	{ $self->privmsg($nick, "Not implemented."); }
				case "s"	{ $self->privmsg($nick, "Not implemented."); }
				case "st"	{ $self->privmsg($nick, "Not implemented."); }

				case "logout" {
					if ($loggedIn eq $nick) {
						$loggedIn = undef;
						$self->privmsg($nick, "You are now logged out.");
					} else {
						# this will never run
						$self->privmsg($nick, "You are not logged in.");
					}
				}

				else { $self->privmsg($nick, "Invalid command."); }
			}

		} else {
			$self->privmsg($nick, "You are not this bot's owner.");
		}
	}
}

sub arrayContains {
    my ($arr,$search_for) = @_;
    foreach my $value (@$arr) {
        return 1 if $value eq $search_for;
    }
    return 0;
}


1;
cs : ee : realist

Technology
Super Moderators
Super Moderators
Posts: 801
Joined: 06 May 2008, 12:47
Noob?: No

Re: Any irc plugin?

#2 Post by Technology »

You might want to take a look at the POE-Component-IRC-5.88 Component.

Its more up to date.

[EDIT]
more up to date than Net::IRC
Last edited by Technology on 24 Sep 2008, 00:54, edited 1 time in total.
One ST0 to rule them all? One PE viewer to find them!
One ST_kRO to bring them all and in the darkness bind them...

Mount Doom awaits us, fellowship of OpenKore!

sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: Any irc plugin?

#3 Post by sli »

This plugin in its current state has just been sitting in my disabled_plugins folder for the past 5 months or so.
cs : ee : realist

kingkevz
Human
Human
Posts: 25
Joined: 04 Apr 2008, 22:55

Re: IRC Plugin

#4 Post by kingkevz »

how to use it?

sli
Perl Monk
Perl Monk
Posts: 810
Joined: 04 Apr 2008, 17:26
Noob?: No

Re: IRC Plugin

#5 Post by sli »

You can't. And I mean you specifically.
cs : ee : realist

Malboro
Noob
Noob
Posts: 12
Joined: 09 Jul 2012, 16:29
Noob?: No

Re: IRC Plugin

#6 Post by Malboro »

hello,

could you update this plugin to the newest openkore version?
when i try to use your plugin with openkore 2.1 i get the following error.

Image

i hope you can help me.

User avatar
ChrstphrR
Testers Team
Testers Team
Posts: 42
Joined: 09 May 2010, 17:30
Noob?: No
Location: Northern Alberta, Canada

Re: IRC Plugin

#7 Post by ChrstphrR »

This plugin looks to be abandoned, as Technology mentioned YEARS ago in this thread, the cpan component that sli had used was outdated -- in fact, that IRC component used was abandoned by it's author:

http://search.cpan.org/~apeiron/Net-IRC-0.79/IRC.pm

From that page:
NAME

Net::IRC - DEAD SINCE 2004 Perl interface to the Internet Relay Chat protocol

USE THESE INSTEAD

This module has been abandoned and is no longer developed. This release serves only to warn current and future users about this and to direct them to supported and actively-developed libraries for connecting Perl to IRC. Most new users will want to use Bot::BasicBot, whereas more advanced users will appreciate the flexibility offered by POE::Component::IRC. We understand that porting code to a new framework can be difficult. Please stop by #perl on irc.freenode.net and we'll be happy to help you out with bringing your bots into the modern era.
The IRC module used was abandoned *4* years prior to the IRC control plugin for openkore being made.

IMO, I'm not sure why this plugin was ever committed to the SVN repository, let alone, kept there; as it was written, it was incomplete. Simply, it never, EVER provided means to actually provide a command and control framework for openkore bots over an IRC network.

Malboro, you might want to look at the busCommands.pl plugin post as a more workable alternative: http://forums.openkore.com/viewtopic.php?f=34&t=16865

Technology
Super Moderators
Super Moderators
Posts: 801
Joined: 06 May 2008, 12:47
Noob?: No

Re: IRC Plugin

#8 Post by Technology »

Here's the plugin I used, its kinda a hack in some places, but it has no external deps and it used to work.
Maybe someone should put it up on svn?

Code: Select all

package koreIRC;

use strict;
use Plugins;
use Globals;
use Log qw(message debug error);
use Commands;
use Utils qw(dataWaiting);
use IO::Socket::INET;
use Time::HiRes qw(time usleep);
#added
use Utils qw(timeOut);
use Utils::DataStructures qw(existsInList);

### USER CONFIGURATION ###
my $irc_server = 'irc.openkore.com';
my $irc_port = 6667;
my $irc_nick = 'test';
my $irc_password = '';
my $irc_username = 'KoreIRC';
my $irc_desc = 'Just a Bot';
my $irc_channel = '#test';
my $irc_webmaster = 0;
my $Commander_Password = 'justatest';
my $irc_session_timeout = 120;
my $irc_channel_pwd = "";
my $irc_identify_pwd = "";
### USER CONFIGURATION ###

#my $irc_debug = 1;
my $irc_sent_login = 0;
my $irc_socket;
#my $irc_connect_step = 1;
my $irc_session_time;
my $irc_session;

#UNUSED
#my %timeout;
$timeout{loginSent}{timeout} = 0;
#$timeout{ircConnectCheck}{time} = time;

# register the plugin with openkore
Plugins::register('koreIRC', 'Communication between kore and IRC.', \&unload, \&reload);
my $hooks = Plugins::addHooks(
	['mainLoop_post',\&ircLoop,undef],
);
my $log_hook = Log::addHook(\&redirectToIRC,"");

sub unload {
	Plugins::delHooks($hooks);
	Log::delHook($log_hook);
	close($irc_socket) if (ircSocketOpen());
	message("koreIRC plugin unloaded.\n","koreIRC");
}

sub reload {
	&unload;
}

my $dcc_socket;
my $dccSockMsg;
my $dccConnected = 0;
my $IRC_Connection = 0;
my $ircsockmsg;

# redirects the allowed kore messages to IRC
sub redirectToIRC {
	my ($type, $domain, $level, $globalVerbosity, $message, $user_data) = @_;
	# ignoredomains is for if you hook on msgToIRC and do something in the console there
	return unless (existsInList($config{'irc_redirectDomains'}, $domain) || $user_data && !existsInList($config{'irc_ignoreDomains'}, $domain));
	#hardcoded this ignore
	return if ($domain =~ /^(koreIRC)/);
	# (e.x.) printing out a playerlist to IRC in separate lines
	my (@message) = split(/\n/,$message);
	if (ircSocketOpen()) {
		# see $Log_Hook_ID
		my $receiver = $user_data || $irc_channel;
		foreach $message (@message) {
			$irc_socket->send("PRIVMSG $receiver :$message\n");
			#debug("(To $receiver): $message\n","koreIRC");
			#kore to IRC
			my $type = ($user_data) ? 'Query' : 'Channel';
			Plugins::callHook('msgToIRC', {receiver => $receiver, msg => $message, type => $type});
		}
	}
}

sub ircLoop {
	ircConnectionCheck();
	return if (!ircSocketOpen());
	if (dataWaiting(\$irc_socket)) {
		#$irc_sent_login = 1;
		$irc_socket->recv($ircsockmsg, 512);
		#next => return?
		return if (length($ircsockmsg) <= 1);
		my (@ircsockmsg) = split(/\n/,$ircsockmsg);
		foreach $ircsockmsg (@ircsockmsg) {
			# check for PING
			if ($ircsockmsg =~ m/^PING/) {
				(undef, $ircsockmsg) = split(/\s/,$ircsockmsg);
				$ircsockmsg =~ s/://g;
				$irc_socket->send("PONG $ircsockmsg\n");
				return;
			}
			my ($ircname, $irccmd, $irctarget) = split(/\s/,$ircsockmsg);
			if ($irccmd eq "PRIVMSG") {
				# we DO care about channel messages for now => example channel: #openkore, so make different for that, way to separate public from pm
				#commented   return if ($irctarget =~ m/^#/);
				my (undef,undef,$ircchat) = split(/:/,$ircsockmsg);
				my ($ircmsgnick) = split(/!/,$ircsockmsg);
				$ircmsgnick =~ s/://g;
				
				debug("(From $ircmsgnick): $ircchat\n","koreIRC");
				#IRC to kore
				my $type = (!($irctarget =~ m/^#/)) ? 'Query' : 'Channel';
				Plugins::callHook('msgFromIRC', {sender => $ircmsgnick, msg => $ircchat, type => $type});
				
				# handle sub protocol
				if ( $ircchat =~ m/\001(.*?) (.*)\001*/ ) {
					ircHandleSubProtocol($1, $2);
					return; # continue on next main loop
				}
				if ($ircchat =~ /^auth/i) {
					my (undef, $tmppassword) = split(/\s/,$ircchat);
					if ($tmppassword eq $Commander_Password) {
						message("[IRC] -> Authorized $ircmsgnick for $irc_session_timeout seconds\n", "koreIRC");
						$irc_socket->send("PRIVMSG $ircmsgnick :Your command has been authorized.\n");
						$irc_session_time = time();
						$irc_session = $ircmsgnick;
					}
					return;
				}
				# session timed out
				if ($irc_session_time && time() > ($irc_session_time + $irc_session_timeout)) {
					$irc_session_time = 0;
					$irc_session = '';
					message("[IRC] -> $ircmsgnick is no longer authorized.\n");
					$irc_socket->send("PRIVMSG $ircmsgnick :You are no longer authorized.\n");
				}
				next if ($irc_session ne $ircmsgnick);
				message("[IRC] -> <Admin commander> $ircchat\n");
				my($ircadmincmd) = split(/\s/,$ircchat);
				if ($ircadmincmd eq "logout") {
					$irc_session_time = 0;
					$irc_session = '';
					message("[IRC] -> $ircmsgnick has been logout.\n", "koreIRC");
					#4,1 is some sort of color in IRC
					$irc_socket->send("PRIVMSG $ircmsgnick :" . chr(3) . "4,1Done.\n");
					next;
				} else {
					my $Log_Hook_ID = Log::addHook(\&redirectToIRC,$ircmsgnick);
					$ircchat =~ s/[\r\n].*$//;
					Commands::run($ircchat);
					Log::delHook($Log_Hook_ID);
				}
				next;
			} elsif ($irccmd eq "001" || $irccmd eq "007") {
				$irc_socket->send("PRIVMSG nickserv :identify $irc_identify_pwd\n");
				$irc_socket->send("JOIN $irc_channel $irc_channel_pwd\n");
				$irc_socket->send("UMODE -mM\n") if ($irc_webmaster);
				message("[IRC] -> Logged on irc server ($irc_server)\n");
				message("[IRC] -> Joining channel $irc_channel\n");
				next;
			} elsif ($irccmd eq "433") {
				my $newnick = substr($irc_nick,0,5) . int(rand(9999));
				$irc_socket->send("NICK $newnick\n");
				message("[IRC] -> Nickname in use, Using random nick instead ($newnick)\n");
				next;
			} elsif ($irccmd eq "372") {
				#message("[IRC] -> MOTD\n", "koreIRC");
			} elsif ($irccmd eq "376") {
				#message("[IRC] -> END MOTD\n", "koreIRC");
			}
		}
	}
}

### IRC CONNECTION HANDLING ###

# ircSocketCheck
sub ircSocketOpen {
	($irc_socket && $irc_socket->connected()) ? return 1 : return 0;
}
# ircConnectionCheck
sub ircConnectionCheck {
	if (!ircSocketOpen()) {
		message("[IRC] -> Opening socket\n", "koreIRC");
		$irc_socket = new IO::Socket::INET(PeerAddr => $irc_server, PeerPort => $irc_port, Proto => 'tcp');

		#$irc_socket = new IO::Socket::INET(
		#	PeerHost => $irc_server,
		#	PeerPort => $irc_port,
		#	Proto => 'tcp'
		#);

		$irc_sent_login = 0;
	}
	ircSendLogin() if (!$irc_sent_login && ircSocketOpen());
}

# ircSendLogin
sub ircSendLogin {
	$irc_socket->send("PASS $irc_password\n") if ($irc_password);
	$irc_socket->send("NICK $irc_nick\n");
	$irc_socket->send("USER $irc_username $irc_username $irc_username :$irc_desc\n");
	message("[IRC] -> Sending login request\n", "koreIRC");
	$irc_sent_login = 1;
}

return 1;
One ST0 to rule them all? One PE viewer to find them!
One ST_kRO to bring them all and in the darkness bind them...

Mount Doom awaits us, fellowship of OpenKore!

User avatar
kLabMouse
Administrator
Administrator
Posts: 1301
Joined: 24 Apr 2008, 12:02

Re: IRC Plugin

#9 Post by kLabMouse »

Technology wrote:Here's the plugin I used, its kinda a hack in some places, but it has no external deps and it used to work.
Maybe someone should put it up on svn?
You still have SVN Write Access.

Technology
Super Moderators
Super Moderators
Posts: 801
Joined: 06 May 2008, 12:47
Noob?: No

Re: IRC Plugin

#10 Post by Technology »

kLabMouse wrote:
Technology wrote:Here's the plugin I used, its kinda a hack in some places, but it has no external deps and it used to work.
Maybe someone should put it up on svn?
You still have SVN Write Access.
I know, but the pc that had the repo crashed.
And I figured, I'd let other people test if this plugin still works and let them fix it if it doesn't and then they can put it up on svn. ;)
One ST0 to rule them all? One PE viewer to find them!
One ST_kRO to bring them all and in the darkness bind them...

Mount Doom awaits us, fellowship of OpenKore!

Post Reply