Skip to content
This repository was archived by the owner on Jan 25, 2019. It is now read-only.

Commit 16df413

Browse files
committed
dbstuff.getLevel now uses join statements to the database to make sure,
that zdvuser is not visible somewhere at any time! Also the code of dbstuff was generally very much cleaned up and commented very much! Next big change will be converting this "library" to a class that will be instantiated in the ircbot for making its database queries.
1 parent 64622e1 commit 16df413

File tree

2 files changed

+72
-44
lines changed

2 files changed

+72
-44
lines changed

dbstuff.py

+70-43
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,49 @@
1-
"""
2-
Database abstraction layer for getting user modes in specified IRC channels
1+
"""Database connection for getting IRC user levels in IRC channels.
2+
3+
Uses sqlalchemy as database abstraction layer and the defined model here
34
4-
@version: beta 0.1
5+
@version: 0.1
56
@author: moschlar
67
"""
78

9+
DEBUG = 0
810
CONFIG_FILE = "testbot.cfg"
9-
path_to_db = "sqlite:///:memory:"
11+
12+
#-----------------------------------------------------------------------------------
13+
# Parsing Database Configuration
14+
#-----------------------------------------------------------------------------------
1015

1116
from ConfigParser import SafeConfigParser
1217
config = SafeConfigParser()
1318
config.read(CONFIG_FILE)
1419

15-
db_engine = config.get("database","engine")
16-
db_server = config.get("database", "server")
17-
user = config.get("database", "user")
18-
passwd = config.get("database", "passwd")
19-
db = config.get("database", "database")
20+
# Getting database information from CONFIG_FILE
21+
try:
22+
db_engine = config.get("database","engine")
23+
db_server = config.get("database", "server")
24+
user = config.get("database", "user")
25+
passwd = config.get("database", "password")
26+
db = config.get("database", "database")
27+
except:
28+
raise Exception("Could not read database path from %s" % CONFIG_FILE)
2029

21-
# Parsing usable information from CONFIG_FILE
22-
if db_engine == "mysql":
23-
path_to_db = "mysql://"
30+
# Parsing database information to path-string
31+
try:
32+
path_to_db = db_engine + "://"
2433
if user:
2534
path_to_db += user
2635
if passwd:
2736
path_to_db += ":" + passwd
2837
path_to_db += "@"
2938
path_to_db += db_server + "/"
3039
path_to_db += db
40+
except:
41+
raise Exception("Could not parse path to database!")
42+
43+
if DEBUG:
44+
print path_to_db
45+
46+
#-----------------------------------------------------------------------------------
3147

3248
import socket
3349

@@ -37,6 +53,10 @@
3753

3854
from sqlalchemy.ext.declarative import declarative_base
3955

56+
#-----------------------------------------------------------------------------------
57+
# Declaring the database model
58+
#-----------------------------------------------------------------------------------
59+
4060
Base = declarative_base()
4161
class irc(Base):
4262
__tablename__ = 'irc'
@@ -59,57 +79,64 @@ class host(Base):
5979
lastmod = Column(Integer)
6080
modby = Column(String)
6181

82+
#-----------------------------------------------------------------------------------
83+
6284
engine = create_engine(path_to_db, echo=False)
6385
Session = sessionmaker(bind=engine)
6486
session = Session()
6587

88+
#-----------------------------------------------------------------------------------
89+
# The only function that makes this class useful
90+
#-----------------------------------------------------------------------------------
91+
6692
def getLevel(channel,hostname):
93+
"""Returns IRC user level from database.
94+
95+
The database model is used through sqlalchemy as defined above.
96+
The first query tries to get an entry just by submitting the ip address,
97+
the second query joins the host.zdvuser which has the given ip address with
98+
irc.user.
99+
"""
67100
level = "n"
68-
#print channel
101+
102+
if DEBUG:
103+
print channel
69104
if channel.startswith("#"):
70105
channel = channel[1:]
71-
#print hostname
106+
if DEBUG:
107+
print hostname
108+
72109
ip = socket.gethostbyname(hostname)
73-
#print ip
74-
q = session.query(irc.stat).filter(irc.user == ip).filter(irc.chan == channel).first()
75-
#print "q: %s"%q
110+
if DEBUG:
111+
print ip
76112

113+
q = session.query(irc.stat).filter(irc.user == ip).filter(irc.chan == channel).first()
77114
if q:
115+
if DEBUG:
116+
print "User level by ip address: %s" % q
78117
level = q[0]
79118

80-
# USE JOINS
81-
82-
p = session.query(host.zdvuser).filter(host.ipv4 == ip).first()
83-
#print "p: %s"%p
119+
# This SQL query shall be performed:
120+
#
121+
# SELECT irc.`stat` FROM `irc`
122+
# LEFT JOIN `host` ON irc.`user` = host.`zdvuser`
123+
# WHERE host.`ipv4` = 'ip' AND irc.`chan` = 'channel'
84124

125+
p = session.query(irc.stat).join((host, irc.user == host.zdvuser)).filter(host.ipv4 == ip).filter(irc.chan == channel).first()
85126
if p:
86-
p = p[0]
87-
r = session.query(irc.stat).filter(irc.user == p).filter(irc.chan == channel).first()
88-
#print "r: %s"%r
89-
if r:
90-
level = r[0]
127+
if DEBUG:
128+
print "User level by ZDV-Username: %s" % p
129+
level = p[0]
91130

92131
return level
93132

94-
def getChannelLevels(channel):
95-
96-
levels = {}
97-
98-
#print channel
99-
if channel.startswith("#"):
100-
channel = channel[1:]
101-
102-
q = session.query(irc.user,irc.stat).filter(irc.chan == channel).all()
103-
#print q
104-
105-
# Lets make a nice dictionary out of the tuples
106-
for (user,mode) in q:
107-
levels[user] = mode
108-
109-
return levels
133+
#-----------------------------------------------------------------------------------
134+
# And finally some test cases
135+
#-----------------------------------------------------------------------------------
110136

111137
if __name__ == "__main__":
138+
print ("My level in #public: %c" % getLevel("#public","134.93.50.65"))
139+
print ("My level in #spielplatz: %c" % getLevel("#spielplatz","134.93.50.65"))
112140
#print getLevel("#public","stewie.k3.wohnheim.uni-mainz.de")
113141
#print getLevel("#public","134.93.136.6")
114142
#print getLevel("#k3","134.93.136.6")
115-
print getChannelLevels("#public")

ircbot.cfg.default

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ nickname: masterbot
77
realname: Master of the Universe
88

99
[database]
10+
engine: mysql
1011
server: 2.3.4.5
1112
user: user
12-
passwd: passwd
13+
password: passwd
1314
database: db
1415

1516
[responses]

0 commit comments

Comments
 (0)