Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added features #14

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
botnick = 'batul' # The nick of the bot.
channel_admin = ['kushal','sayan','mbuf','rtnpro','chandankumar','praveenkumar'] # List of IRC nicks as masters.
botnick = 'batul' # The nick of the bot.
# List of IRC nicks as masters.
channel_admin = ['kushal', 'sayan', 'mbuf',
'rtnpro', 'chandankumar', 'praveenkumar']
93 changes: 77 additions & 16 deletions ekan0ra.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
from twisted.internet import defer

# system imports
import time, sys, os
import time
import sys
import os
import datetime
import config as conf
import json

from collections import deque
import fpaste

commands = [
Expand All @@ -23,6 +26,8 @@
('startclass', 'start logging the class'),
('endclass', 'ends logging the class'),
('pingall:[message]', 'pings the message to all'),
('lastwords:[nick]','show last 10 lines of the user'),
('lastseen:[nick]','shows last seen datetime'),
('help', 'list all the commands'),
('.link [portal]', 'Returns the link of the portal')
]
Expand All @@ -31,11 +36,13 @@
{command} - {help_text}
"""


class MessageLogger(object):
"""
An independent logger class (because separation of application
and protocol logic is a good thing).
"""

def __init__(self, file):
self.file = file

Expand All @@ -54,12 +61,14 @@ class LogBot(irc.IRCClient):

nickname = conf.botnick

def __init__(self, channel):
self.chn = '#'+channel
def __init__(self, channel):
self.chn = '#' + channel
self.channel_admin = conf.channel_admin
self.qs_queue = []
self.links_reload()
self.logger = None
self.lastseen = {}
self.lastspoken = {}

def clearqueue(self):
self.qs_queue = []
Expand All @@ -71,22 +80,21 @@ def connectionMade(self):

def startlogging(self, user, msg):
now = datetime.datetime.now()
self.filename = "Logs-%s.txt"%now.strftime("%Y-%m-%d-%H-%M")
self.filename = "Logs-%s.txt" % now.strftime("%Y-%m-%d-%H-%M")
self.logger = MessageLogger(open(self.filename, "a"))

self.logger.log("[## Class Started at %s ##]" %
time.asctime(time.localtime(time.time())))
time.asctime(time.localtime(time.time())))
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))
self.islogging = True
self.islogging = False

def stoplogging(self, channel):
if not self.logger:
return
self.logger.log("[## Class Ended at %s ##]" %
time.asctime(time.localtime(time.time())))
self.logger.log("[## Class Ended at %s ##]" % time.asctime(time.localtime(time.time())))
self.logger.close()
#self.upload_logs(channel)
self.upload_logs(channel)
self.islogging = False

def connectionLost(self, reason):
Expand All @@ -99,7 +107,8 @@ def signedOn(self):

def pingall(self, nicklist):
"""Called to ping all with a message"""
msg = ', '.join([nick for nick in nicklist if nick != self.nickname and nick not in self.channel_admin])
msg = ', '.join([nick for nick in nicklist if nick !=
self.nickname and nick not in self.channel_admin])
self.msg(self.chn, msg)
self.msg(self.chn, self.pingmsg.lstrip())

Expand All @@ -112,16 +121,18 @@ def links_reload(self):
def privmsg(self, user, channel, msg):
"""This will get called when the bot receives a message."""
user = user.split('!', 1)[0]
self.updateLastSeen(user)
if self.islogging:
user = user.split('!', 1)[0]
self.logger.log("<%s> %s" % (user, msg))

# Check to see if they're sending me a private message
user_cond = user in self.channel_admin
if msg == '!' and self.islogging:
if msg == '!' and self.islogging:
self.qs_queue.append(user)
if msg == '!' and not self.islogging:
self.msg(self.chn, '%s no session is going on, feel free to ask a question. You do not have to type !' % user)
self.msg(
self.chn, '%s no session is going on, feel free to ask a question. You do not have to type !' % user)
return
if msg == 'givemelogs':
import sys
Expand All @@ -139,31 +150,58 @@ def privmsg(self, user, channel, msg):
name = self.qs_queue.pop(0)
msg = "%s please ask your question." % name
if len(self.qs_queue) > 0:
msg = "%s. %s you are next. Get ready with your question." % (msg, self.qs_queue[0])
msg = "%s. %s you are next. Get ready with your question." % (
msg, self.qs_queue[0])
self.msg(self.chn, msg)
else:
self.msg(self.chn, "No one is in queue.")
if msg == 'masters' and user_cond:
self.msg(self.chn, "My current masters are: %s" % ",".join(self.channel_admin))
self.msg(self.chn, "My current masters are: %s" %
",".join(self.channel_admin))
if msg.startswith('add:') and user_cond:
try:
name = msg.split()[1]
print name
self.channel_admin.append(name)
self.msg(self.chn,'%s is a master now.' % name)
self.msg(self.chn, '%s is a master now.' % name)
except Exception, err:
print err
if msg.startswith('rm:') and user_cond:
try:
name = msg.split()[1]
self.channel_admin = filter(lambda x: x != name, self.channel_admin)
self.channel_admin = filter(
lambda x: x != name, self.channel_admin)
except Exception, err:
print err

if msg.startswith('s\\'):
wordlist = msg.split('\\')[1::]
line = self.lastspoken[user][-1]
for target,replace in zip(wordlist[0::2],wordlist[1::2]):
line = line.replace(target,replace)
statement = "what {user} meant is , {line}".format(user=user,line=line)
self.msg(channel,statement)

if msg == 'help':
for command, help_txt in commands:
self.msg(user, help_template.format(command=command,
help_text=help_txt))
if msg.startswith('lastwords'):
nick = msg.split(':')[-1]
if nick in self.lastspoken:
for line in self.lastspoken[nick]:
self.msg(channel,line)

if msg.startswith('lastseen'):
nick = msg.split(':')[-1]
self.names(channel).addCallback(self.activityTracker,nick=nick,channel=channel)


if user in self.lastspoken:
self.lastspoken[user].append(msg)
else:
self.lastspoken[user] = deque(maxlen=10)
self.lastspoken[user].append(msg)

if channel == self.nickname:

Expand Down Expand Up @@ -199,6 +237,29 @@ def irc_NICK(self, prefix, params):
if self.islogging:
self.logger.log("%s is now known as %s" % (old_nick, new_nick))

def userLeft(self,user,channel):
self.updateLastSeen(user)

def userQuit(self,user,quitMessage):
self.updateLastSeen(user)

def uesrJoined(self,nick,channel):
self.updateLastSeen(user)

def updateLastSeen(self,user):
self.lastseen[user]=datetime.datetime.now().strftime('%c')

def activityTracker(self,nicklist,nick,channel):
if nick in nicklist:
if self.lastseen.get(nick):
self.msg(channel, "%s is online now, last activity at %s" % (nick,self.lastseen[nick]))
else:
self.msg(channel, "%s is online now, last activity not known" % (nick))
else:
if nick in self.lastseen:
self.msg(channel,"last seen activity was on %s"%self.lastseen[nick])
else:
self.msg(channel,"no data found")

# For fun, override the method that determines how a nickname is changed on
# collisions. The default method appends an underscore.
Expand Down
Loading