Created
April 29, 2015 01:45
-
-
Save anonymous/a6ea4114efdc23802b21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'socket' | |
require 'thread' | |
class IRCClient | |
#@@keywords = ["PRIVMSG", "MODE", "KICK", "NOTICE"] | |
# | |
# | |
# constructor - initialize data | |
# | |
# | |
attr_accessor :server, :port, :nick, :channels | |
def initialize args, &block | |
args.each do |ivar, value| | |
instance_variable_set "@#{ivar}", value unless value.nil? | |
end | |
@socket = TCPSocket.new @server, @port | |
user(@nick) | |
nick(@nick) | |
@admins = ['gr33n7007h', 'al2o3-cr'] | |
@reg = Regexp.union(/\:(.*)\!~|(\#.*)\s\:|\@(.+?)\s/) | |
@filter_info = Proc.new {|d| d.scan(@reg).flatten.compact } | |
end | |
# | |
# | |
# check if nick is an admin | |
# | |
# | |
def admin?(nick) | |
if @admins.include? nick | |
true | |
else | |
false | |
end | |
end | |
# | |
# | |
# play ping pong with the server | |
# | |
# | |
def pong | |
Thread.new do | |
@socket.write "PONG 0\r\n" | |
end.join | |
end | |
# | |
# | |
# user method - username/hostname/servername/realname | |
# | |
# | |
def user(nick) | |
Thread.new do | |
@socket.write "USER %s %s %s :%s\r\n" % [*[@nick]*4] | |
end.join | |
end | |
# | |
# | |
# nick method | |
# | |
# | |
def nick(nick) | |
Thread.new do | |
@socket.write "NICK %s\r\n" % @nick | |
end.join | |
end | |
# | |
# | |
# join method | |
# | |
# | |
def join | |
Thread.new do | |
@channels.each {|channel| @socket.write "JOIN %s\r\n" % [channel] } | |
end.join | |
end | |
# | |
# | |
# quit method | |
# | |
# | |
def quit | |
Thread.new do | |
@socket.write "QUIT :goodbye ben-ja-min\r\n" | |
end.join | |
end | |
# | |
# | |
# kick method | |
# | |
# | |
def kick(channel, nick, reason) | |
Thread.new do | |
@socket.write "KICK %s %s :%s\r\n" % [channel, nick, reason] | |
end.join | |
end | |
# | |
# | |
# op method | |
# | |
# | |
def op(channel, nick) | |
Thread.new do | |
@socket.write "MODE %s +o %s\r\n" % [channel, nick] | |
end.join | |
end | |
# | |
# | |
# deop method | |
# | |
# | |
def deop(channel, nick) | |
Thread.new do | |
@socket.write "MODE %s -o %s\r\n" % [channel, nick] | |
end.join | |
end | |
# | |
# | |
# quiet method | |
# | |
# | |
def quiet(channel, nick) | |
Thread.new do | |
@socket.write "MODE %s +q %s\r\n" % [channel, nick] | |
end.join | |
end | |
# | |
# | |
# unquiet method | |
# | |
# | |
def unquiet(channel, nick) | |
Thread.new do | |
@socket.write "MODE %s -q %s\r\n" % [channel, nick] | |
end.join | |
end | |
# | |
# | |
# ban method | |
# | |
# | |
def ban(channel, nick) | |
Thread.new do | |
@socket.write "MODE " # todo not decided ban formatting yet | |
end.join | |
end | |
# | |
# | |
# auth error method | |
# | |
# | |
def auth_error(channel, nick) | |
Thread.new do | |
#@socket.write "PRIVMSG %s :%s\r\n" % [nick, error] | |
@socket.write "PRIVMSG %s :unpriviledged user (%s)\r\n" % [channel, nick] | |
end.join | |
end | |
# | |
# | |
# sends a message to the channel | |
# | |
# | |
def say(channel, msg) | |
Thread.new do | |
@socket.write "PRIVMSG %s :%s\r\n" % [channel, msg] | |
end.join | |
end | |
# | |
# | |
# start indefinite loop | |
# | |
# | |
def start | |
loop do | |
data = @socket.readline # get data line by line | |
if data.include? "376" # check for MOTD | |
join | |
end | |
if data.include?('PRIVMSG' || 'MODE' || 'KICK' || 'NOTICE') | |
_nick, _host, _channel = @filter_info[data] # filter nick/host/channel into variables | |
end | |
puts data # prints data to stdout | |
if data.include? 'PING' | |
pong | |
end | |
# | |
# just a helper method for now | |
# | |
if match = data.match(/!hello/) | |
say(_channel, "Hi %s" % _nick) | |
end | |
# | |
# add to admins | |
# | |
if match = data.match(/!admin\s(.*)/) | |
if admin?(_nick) | |
new_admin = match[1].strip | |
@admins << new_admin | |
else | |
auth_error(_channel, _nick) | |
end | |
end | |
# | |
# check for kick query | |
# | |
if match = data.match(/!kick\s(.+?)\s(.*)/) | |
if admin?(_nick) | |
k_nick = match[1].strip | |
reason = match[2].strip | |
kick(_channel, k_nick, reason) | |
else | |
auth_error(_channel, _nick) | |
end | |
end | |
# | |
# check for op query | |
# | |
if match = data.match(/!op\s(.*)/) | |
if admin?(_nick) | |
o_nick = match[1].strip | |
op(_channel, o_nick) | |
else | |
auth_error(_channel, _nick) | |
end | |
end | |
# | |
# check for deop query | |
# | |
if match = data.match(/!deop\s(.*)/) | |
if admin?(_nick) | |
d_nick = match[1].strip | |
deop(_channel, d_nick) | |
else | |
auth_error(_channel, _nick) | |
end | |
end | |
# | |
# check for quiet query | |
# | |
if match = data.match(/!quiet\s(.*)/) | |
if admin?(_nick) | |
m_nick = match[1].strip | |
quiet(_channel, m_nick) | |
else | |
auth_error(_channel, _nick) | |
end | |
end | |
# | |
# check for unquiet query | |
# | |
if match = data.match(/!unquiet\s(.*)/) | |
if admin?(_nick) | |
u_nick = match[1].strip | |
unquiet(_channel, u_nick) | |
else | |
auth_error(_channel, _nick) | |
end | |
end | |
end | |
end | |
end | |
config = { | |
server: "irc.freenode.net", | |
port: 6667, | |
nick: "alfab0t", | |
channels: ["##alfa", "#cymothoa"] | |
} | |
bot = IRCClient.new(config).start | |
p bot | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment