archive

- Random tools & helpful resources for IRC
git clone git://git.acid.vegas/archive.git
Log | Files | Refs | Archive

functions.py (4000B)

      1 #!/usr/bin/env python
      2 # IRC Services (IRCS) - Developed by acidvegas in Python (https://acid.vegas/ircs)
      3 # functions.py
      4 
      5 import inspect
      6 import os
      7 import sqlite3
      8 import string
      9 
     10 # Database
     11 database_dir  = os.path.join(os.path.dirname(os.path.realpath(inspect.stack()[-1][1])), 'data')
     12 database_file = os.path.join(database_dir, 'ircs.db')
     13 
     14 # Globals
     15 db  = sqlite3.connect(database_file)
     16 sql = db.cursor()
     17 
     18 class Database:
     19     def check():
     20         tables = sql.execute('SELECT name FROM sqlite_master WHERE type=\'table\'').fetchall()
     21         if len(tables):
     22             return True
     23         else:
     24             return False
     25 
     26     def create():
     27         sql.execute('CREATE TABLE CHANSERV (CHANNEL TEXT NOT NULL, IDENT TEXT NOT NULL, MODE TEXT NOT NULL);')
     28         sql.execute('CREATE TABLE HOSTSERV (IDENT TEXT NOT NULL, VHOST TEXT NOT NULL, STATUS TEXT NOT NULL);')
     29         db.commit()
     30 
     31 
     32 
     33 class ChanServ:
     34     def add_mode(chan, ident, mode):
     35         sql.execute('INSERT INTO CHANSERV (CHANNEL,IDENT,MODE) VALUES (?, ?, ?)', (chan, ident, mode))
     36         db.commit()
     37 
     38     def channels():
     39         return set(list(item[0] for item in sql.execute('SELECT CHANNEL FROM CHANSERV ORDER BY CHANNEL ASC').fetchall()))
     40 
     41     def del_mode(chan, ident):
     42         sql.execute('DELETE FROM CHANSERV WHERE CHANNEL=? AND IDENT=?', (chan, ident))
     43         db.commit()
     44 
     45     def drop(chan):
     46         sql.execute('DELETE FROM CHANSERV WHERE CHANNEL=?', (chan,))
     47         db.commit()
     48 
     49     def get_mode(chan, ident):
     50         data = sql.execute('SELECT MODE FROM CHANSERV WHERE CHANNEL=? AND IDENT=?', (chan, ident)).fetchone()
     51         if data:
     52             return data[0]
     53         else:
     54             return None
     55 
     56     def hosts():
     57         return set(list(item[0].split('@')[1] for item in sql.execute('SELECT IDENT FROM CHANSERV', (channel,)).fetchall()))
     58 
     59     def idents(chan, mode=None):
     60         if mode:
     61             return list(item[0] for item in sql.execute('SELECT IDENT FROM CHANSERV WHERE CHANNEL=? AND MODE=?', (channel, mode)).fetchall())
     62         else:
     63             return list(item[0] for item in sql.execute('SELECT IDENT FROM CHANSERV WHERE CHANNEL=?', (channel,)).fetchall())
     64 
     65     def read(channel, mode=None):
     66         if mode:
     67             return sql.execute('SELECT IDENT FROM CHANSERV WHERE CHANNEL=? AND MODE=? ORDER BY CHANNEL ASC, MODE ASC, IDENT ASC', (channel, mode)).fetchall()
     68         else:
     69             return sql.execute('SELECT IDENT,MODE FROM CHANSERV WHERE CHANNEL=? ORDER BY CHANNEL ASC, MODE ASC, IDENT ASC', (channel,)).fetchall()
     70 
     71 
     72 
     73 class HostServ:
     74     def add(ident, vhost):
     75         sql.execute('INSERT INTO HOSTSERV (IDENT,VHOST,STATUS) VALUES (?, ?, \'pending\')', (ident, vhost))
     76         db.commit()
     77 
     78     def delete(ident):
     79         sql.execute('DELETE FROM HOSTSERV WHERE IDENT=?', (ident,))
     80         db.commit()
     81 
     82     def get_vhost(ident, active=False):
     83         data = sql.execute('SELECT VHOST FROM HOSTSERV WHERE IDENT=? AND STATUS=\'on\'', (ident,)).fetchone()
     84         if data:
     85             return data[0]
     86         else:
     87             return None
     88 
     89     def get_status(ident):
     90         data =  sql.execute('SELECT STATUS FROM HOSTSERV WHERE IDENT=?', (ident,)).fetchone()
     91         if data:
     92             return data[0]
     93         else:
     94             return None
     95 
     96     def hosts():
     97         return set(list(item[0].split('@')[1] for item in sql.execute('SELECT IDENT FROM CHANSERV', (channel,)).fetchall()))
     98 
     99     def idents():
    100         return list(item[0] for item in sql.execute('SELECT IDENT FROM HOSTSERV').fetchall())
    101 
    102     def pending():
    103         return sql.execute('SELECT IDENT,VHOST FROM HOSTSERV WHERE STATUS=\'pending\' ORDER BY IDENT ASC').fetchall()
    104 
    105     def read():
    106         return sql.execute('SELECT IDENT,VHOST FROM HOSTSERV ORDER BY IDENT ASC').fetchall()
    107 
    108     def set_status(ident, status):
    109         sql.execute('UPDATE HOSTSERV SET STATUS=? WHERE IDENT=?', (status, ident))
    110         db.commit()
    111 
    112     def vhosts():
    113         return list(item[0] for item in sql.execute('SELECT VHOST FROM HOSTSERV').fetchall())