Skip to content

Commit

Permalink
Fixes for Linux. Cleaned up the code a lot. And users passwords are n…
Browse files Browse the repository at this point in the history
…ow encrypted.
  • Loading branch information
dominik committed May 3, 2010
1 parent 606ede1 commit 4edf861
Show file tree
Hide file tree
Showing 10 changed files with 394 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
*~
27 changes: 16 additions & 11 deletions IRCLibrary/IRC.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""
Nyx - A powerful IRC Client
MDS IRC Lib
Copyright (C) 2009 Mad Dog Software
http://maddogsoftware.co.uk - morfeusz8@yahoo.co.uk
Expand All @@ -24,7 +24,7 @@
########################
version = "0.1"

class server:
class connection:
def __init__(self, addresses, nicks, realname, username):
self.addresses = addresses # Addresses of which to connect to.
self.nicks = nicks # Nicks to use
Expand All @@ -36,7 +36,7 @@ def __init__(self, addresses, nicks, realname, username):
self.port = None #This is none if not connected
self.socket = None #This is none if not connected

self.autojoinchans = [] #These will be joined when the End of MOTD(376) command is received.
self.autojoinchans = [] #These will be joined when the 001 command is received.

self.nick = nicks[0]

Expand All @@ -52,7 +52,7 @@ def __init__(self, addresses, nicks, realname, username):
self.gen_eol = gen_eol

def connect(self, addr=0, pingServ=True, threaded=True):
"""Connects this server(Asynchronosouly), you can pass a optional integer of the address(in addresses)"""
"""Connects this server(Asynchronously), you can pass a optional integer of the address(in addresses)"""
try:
#If connect is called, spawn it in a new thread
if threaded == True:
Expand Down Expand Up @@ -82,28 +82,33 @@ def connect(self, addr=0, pingServ=True, threaded=True):

logger.log_instance.log("Couldn't connect to server: " + str(err), "IRC.server.connect", "error")

#########################
#RESPONSE FUNCTION #
#########################
thread.start_new(lambda x: self.response(), (None,))

#Register the connection
if self.addresses[addr][2] != '':
self.socket.send("PASS %s \r\n" % (self.addresses[addr][2]))

self.socket.send("NICK %s\r\n" % (self.nick))

self.socket.send("USER %s %s %s :%s\r\n" % (self.username, self.username, self.addresses[addr][0], self.realname))
self.socket.send("USER %s %s %s :%s\r\n" % (self.username, \
self.username, self.addresses[addr][0], self.realname))

#Hook the RPL_WELCOME(001) command, to the ping_server function.
if pingServ:
self.events.hook_event("001", lambda s, w, w_eol, args: thread.start_new(lambda x: self.pinger.ping_server(), (None,)))
self.events.hook_event("001", lambda s, w, w_eol, \
args: thread.start_new(lambda x: self.pinger.ping_server(), (None,)))
#And also hook the 001 to the autojoin_chans function
if len(self.autojoinchans) != 0:
self.events.hook_event("001", lambda s, w, w_eol, args: thread.start_new(lambda x: self.autojoin_chans(), (None,)))
self.events.hook_event("001", lambda s, w, w_eol, \
args: thread.start_new(lambda x: self.autojoin_chans(), (None,)))
#This will reply to the PING command
self.events.hook_event("PING", lambda serv, w, w_eol, args: serv.send("PONG %s" % (w_eol[2])), 5)

#########################
#RESPONSE FUNCTION #
#########################
#thread.start_new(lambda x: self.response(), (None,))
self.response() # Use this thread for the response function.

except Exception as err:
logger.log_instance.log(err, "IRC.connect", "critical")

Expand Down
4 changes: 2 additions & 2 deletions IRCLibrary/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""
Nyx - A powerful IRC Client
MDS IRC Lib
Copyright (C) 2009 Mad Dog Software
http://maddogsoftware.co.uk - morfeusz8@yahoo.co.uk
Expand Down Expand Up @@ -74,4 +74,4 @@ def __init__(self, command, callback, priority, id, args):
self.priority = priority
self.id = id
self.args = args


17 changes: 7 additions & 10 deletions logger.py → IRCLibrary/logger.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
"""
Nyx - A powerful IRC Client
MDS IRC Lib
Copyright (C) 2009 Mad Dog Software
http://maddogsoftware.co.uk - morfeusz8@yahoo.co.uk
Expand All @@ -27,17 +27,14 @@

class logger:

def __init__(self, text="Nyx logging initialized"):
def __init__(self, text):
"""
Starts the log, opens the file, writes text to the file, and a div tag
"""
#FILENAME
LOG_FILENAME = os.path.dirname(sys.argv[0]) + "\\log.html"

if LOG_FILENAME.replace("/", "\\").startswith("\\"):
LOG_FILENAME = "C:\\log.html"
#!-TODO-! Make the path different in linux...
print LOG_FILENAME
LOG_FILENAME = os.path.join(os.path.dirname(sys.argv[0]), "log.html")


#Open the log file...
self.fLog = open(LOG_FILENAME, "a")
self.write("<div style=\"font-family:Calibri;\">\n" + text + "<br/>\n")
Expand Down Expand Up @@ -109,5 +106,5 @@ def close(self, text):

#If this modules is imported, initialize the logger class
if __name__ != "__main__":
log_instance = logger()

log_instance = logger("MDSBot 0.1 initialized")

16 changes: 9 additions & 7 deletions XmlHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import os, sys

#USERS
def load_users(path=os.path.dirname(sys.argv[0]) + "\\users.xml"):
def load_users(path=os.path.join(os.path.dirname(sys.argv[0]), "users.xml")):
"""Loads the users"""
xmlDoc = xml.dom.minidom.parse(path)
import users
usrManager = users.user_manager()
for userElement in xmlDoc.getElementsByTagName("user"):
nick = getAttribute(userElement.attributes, "nick")
password = getAttribute(userElement.attributes, "password")
import base64
password = base64.b64decode(getAttribute(userElement.attributes, "password"))
email = getAttribute(userElement.attributes, "email")
privileges = getAttribute(userElement.attributes, "privileges")

Expand All @@ -24,14 +25,15 @@ def load_users(path=os.path.dirname(sys.argv[0]) + "\\users.xml"):
return usrManager


def save_users(user_manager, path=os.path.dirname(sys.argv[0]) + "\\users.xml"):
def save_users(user_manager, path=os.path.join(os.path.dirname(sys.argv[0]), "users.xml")):
"""Saves the users"""
xmlDoc = xml.dom.minidom.parseString("<users></users>")

for i in user_manager.users:
userElement = xmlDoc.createElement("user")
userElement.setAttribute("nick", i.nick)
userElement.setAttribute("password", i.password)
import base64
userElement.setAttribute("password", base64.b64encode(i.password))
userElement.setAttribute("privileges", ",".join(i.privileges))
userElement.setAttribute("email", i.email)
xmlDoc.documentElement.appendChild(userElement)
Expand All @@ -41,7 +43,7 @@ def save_users(user_manager, path=os.path.dirname(sys.argv[0]) + "\\users.xml"):
return True

#FACTOIDS
def load_factoids(path=os.path.dirname(sys.argv[0]) + "\\factoids.xml"):
def load_factoids(path=os.path.join(os.path.dirname(sys.argv[0]), "factoids.xml")):
"""Loads the users"""
xmlDoc = xml.dom.minidom.parse(path)
import factoids
Expand All @@ -54,7 +56,7 @@ def load_factoids(path=os.path.dirname(sys.argv[0]) + "\\factoids.xml"):

return factoidManager

def save_factoids(factoid_manager, path=os.path.dirname(sys.argv[0]) + "\\factoids.xml"):
def save_factoids(factoid_manager, path=os.path.join(os.path.dirname(sys.argv[0]), "factoids.xml")):
"""Saves the users"""
xmlDoc = xml.dom.minidom.parseString("<factoids></factoids>")

Expand Down Expand Up @@ -94,4 +96,4 @@ def getAttribute(attrlist, attrName):
if attrlist.item(i).nodeName == attrName:
txt=attrlist.item(i).nodeValue
#Then return the attributes value
return txt
return txt
16 changes: 3 additions & 13 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
@author: Dominik
'''
import XmlHelper
def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loadedModules):
def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loadedModules):
# if this is not a PRIVMSG to a channel make word[2] the nick of the
# person that is sending this.
if word[2] == server.nick:
word[2] = word[0].split("!")[0]

Expand Down Expand Up @@ -34,7 +36,6 @@ def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loaded
relayManager.rem(i, quitMsg=quitMsg)

server.send("QUIT :%s" % (quitMsg))
import sys; sys.exit(1)
else:
usrManager.print_insufficient_privils(word, server, "quit")
return True
Expand Down Expand Up @@ -67,7 +68,6 @@ def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loaded
XmlHelper.save_users(usrManager)
#Send a message confirming.
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "You have registered successfully"))
server.send("PRIVMSG %s :%s" % ("#opers", "\x0307" + nick + "\x03 registered"))
else:
reply = "An account for " + nick + " already exists."
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], reply))
Expand All @@ -91,10 +91,8 @@ def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loaded
usrManager.change_user_status(nick, True)
#Send a message confirming.
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "You are now identified as \x02" + nick))
server.send("PRIVMSG %s :%s" % ("#opers", "\x0307" + nick + "\x03logged in."))
else:
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "\x0305Incorrect password"))
server.send("PRIVMSG %s :%s" % ("#opers", "\x0307" + nick + "\x03failed to login"))
else:
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "\x0305No user by this nickname found"))

Expand All @@ -113,10 +111,8 @@ def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loaded
usrManager.change_user_status(nick, False)
#Send a message confirming.
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "\x02" + nick + "\x02 is now logged out"))
server.send("PRIVMSG %s :%s" % ("#opers", "\x0307" + nick + "\x03logged out."))
else:
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "\x0305Incorrect password"))
server.send("PRIVMSG %s :%s" % ("#opers", "\x0307" + nick + "\x03failed to logout"))

else:
server.send("PRIVMSG %s :%s" % (word[0].split("!")[0], "\x0305User doesn't exist"))
Expand Down Expand Up @@ -158,9 +154,6 @@ def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loaded
server.send("PRIVMSG %s :%s" % (word[2], \
"\x0303Privileges for \x0307" + word[3].split()[1] + " \x0303added successfully"))

opersMsg = "\x0307" + word[0].split("!")[0] + "\x03"
opersMsg += "gave " + word[3].split()[4] + " to\x0307" + word[3].split()[1]
server.send("PRIVMSG %s :%s" % ("#opers", opersMsg))
else:
usrManager.print_insufficient_privils(word, server, "user_privils_add")

Expand All @@ -178,9 +171,6 @@ def cmd(server, word, word_eol, usrManager, relayManager, factoidManager, loaded
server.send("PRIVMSG %s :%s" % (word[2], \
"\x0303Privileges for \x0307" + word[3].split()[1] + " \x0303removed successfully"))

opersMsg = "\x0307" + word[0].split("!")[0] + "\x03"
opersMsg += "removed " + word[3].split()[4] + " from\x0307" + word[3].split()[1]
server.send("PRIVMSG %s :%s" % ("#opers", opersMsg))
else:
usrManager.print_insufficient_privils(word, server, "user_privils_rem")

Expand Down
1 change: 1 addition & 0 deletions factoids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<factoid name="dom96">
<content content="cool"/>
<content content="an irc mentor"/>
<content content="awesome"/>
</factoid>
<factoid name="test">
<content content="test"/>
Expand Down
Loading

0 comments on commit 4edf861

Please sign in to comment.