This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbstuff.py
executable file
·151 lines (116 loc) · 4.41 KB
/
dbstuff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Database connection for getting IRC user levels in IRC channels.
Uses sqlalchemy as database abstraction layer and the defined model here
@version: 0.2
@author: moschlar
"""
DEBUG = 0
VERSION = "0.2"
import sys
from os.path import exists, splitext
from ConfigParser import SafeConfigParser
#-----------------------------------------------------------------------------------
# Parsing Database Configuration
#-----------------------------------------------------------------------------------
config_file = "testbot.cfg"
config = SafeConfigParser()
config.read(config_file)
# Getting database information from config_file
try:
db_engine = config.get("database","engine")
db_server = config.get("database", "server")
user = config.get("database", "user")
passwd = config.get("database", "password")
db = config.get("database", "database")
except:
raise Exception("Could not read database path from %s" % config_file)
# Parsing database information to path-string
try:
path_to_db = db_engine + "://"
if user:
path_to_db += user
if passwd:
path_to_db += ":" + passwd
path_to_db += "@"
path_to_db += db_server + "/"
path_to_db += db
except:
raise Exception("Could not parse path to database!")
if DEBUG: print path_to_db
#-----------------------------------------------------------------------------------
import socket
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
#-----------------------------------------------------------------------------------
# Declaring the database model
#-----------------------------------------------------------------------------------
Base = declarative_base()
class irc(Base):
__tablename__ = 'irc'
id = Column(Integer, primary_key=True)
chan = Column(String)
user = Column(String, ForeignKey('host.zdvuser'))
stat = Column(String)
class host(Base):
__tablename__ = "host"
id = Column(Integer, primary_key=True)
zdvuser = Column(String)
wohnheim = Column(Integer)
hostname = Column(String)
mac = Column(String)
ipv4 = Column(String)
ipv6 = Column(String)
lastmod = Column(Integer)
modby = Column(String)
#-----------------------------------------------------------------------------------
engine = create_engine(path_to_db, echo=False)
Session = sessionmaker(bind=engine)
session = Session()
#-----------------------------------------------------------------------------------
# The only function that makes this class useful
#-----------------------------------------------------------------------------------
def getLevel(channel,hostname):
"""Returns IRC user level from database.
The database model is used through sqlalchemy as defined above.
The first query tries to get an entry just by submitting the ip address,
the second query joins the host.zdvuser which has the given ip address with
irc.user.
"""
level = "n"
if DEBUG: print channel
if channel.startswith("#"):
channel = channel[1:]
if DEBUG: print hostname
try:
ip = socket.gethostbyname(hostname)
if DEBUG: print ip
except:
return level
q = session.query(irc.stat).filter(irc.user == ip).filter(irc.chan == channel).first()
if q:
if DEBUG: print "User level by ip address: %s" % q
level = q[0]
# This SQL query shall be performed:
#
# SELECT irc.`stat` FROM `irc`
# LEFT JOIN `host` ON irc.`user` = host.`zdvuser`
# WHERE host.`ipv4` = 'ip' AND irc.`chan` = 'channel'
p = session.query(irc.stat).join(host).filter(host.ipv4 == ip).filter(irc.chan == channel).first()
if p:
if DEBUG: print "User level by ZDV-Username: %s" % p
level = p[0]
return level
#-----------------------------------------------------------------------------------
# And finally some test cases
#-----------------------------------------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) != 3:
print ("Usage: dbstuff.py #channel hostname")
else:
hostname = sys.argv[2]
channel = sys.argv[1]
level = getLevel(channel,hostname)
print ("%s has mode +%c in %s" % (hostname,level,channel))