askcompu

Untitled

Sep 10th, 2015
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.93 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. seen.py - Phenny seen module
  4. """
  5. import sqlite3 as lite
  6. import os
  7.  
  8. def db_connect(db):
  9.     return lite.connect(db, check_same_thread = False)
  10.  
  11. def setup(self):
  12.     self.seen_db = os.path.join(os.path.expanduser('~/.phenny'), 'seen.db')
  13.     self.seen_conn = db_connect(self.seen_db)
  14.     c = self.seen_conn.cursor()
  15.     c.execute('''create table if not exists seen(
  16.        nick    varchar(31) NOT NULL PRIMARY KEY,
  17.        channel varchar(31) NOT NULL,
  18.        message text,
  19.        event   varchar(10) NOT NULL,
  20.        time    time DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
  21.    );''')
  22.     c.close()
  23.     self.seen_conn.commit()
  24.     self.seen_conn.close()
  25. setup.thread = False
  26.  
  27. def smart_truncate(content):
  28.     suffix='...'
  29.     length=int(150)
  30.     if len(content) <= length:
  31.         return content
  32.     else:
  33.         return content[:length].rsplit(' ', 1)[0]+suffix
  34.  
  35. def seen(phenny, input):
  36.     """Gives when a user was last seen."""
  37.     inputnick = input.group(2)
  38.  
  39.     add.conn = db_connect(phenny.seen_db)
  40.     c = add.conn.cursor()
  41.     query = (inputnick,)
  42.     c.execute("SELECT * FROM seen WHERE nick LIKE ?;", query)
  43.     results = c.fetchall()
  44.    
  45.     try:
  46.         nick = results[0]
  47.         channel = results[1]
  48.         message = results[2]
  49.         event = results[3]
  50.         seentime = results[4]
  51.     except:
  52.         nick = None
  53.    
  54.     if not nick:
  55.         phenny.say("Sorry I haven't seen " + inputnick)
  56.     else:
  57.         message = smart_truncate(message)
  58.         if event == "PRIVMSG":
  59.             phenny.say(nick + " was last seen in " + channel + 'saying "' + message + '" at ' + seentime)
  60.         elif event == "JOIN":
  61.             phenny.say(nick + " was last seen joining " + channel + " at " + seentime)
  62.         elif event == "PART":
  63.             phenny.say(nick + " was last seen leaving " + channel + ' with message "' + message + '" at ' + seentime)
  64.         elif event == "QUIT":
  65.             phenny.say(nick + ' was last seen quitting with message "' + message + '" at ' + seentime)
  66.     c.close()
  67. seen.commands = ['seen']
  68. seen.example = ".seen somenick"
  69.  
  70. def seenstore(phenny, input, event):
  71.     nick = input.nick
  72.     channel = input.sender
  73.     if event == "JOIN":
  74.         message = None
  75.     else:
  76.         message = input.group()
  77.     add.conn = db_connect(phenny.seen_db)
  78.     c = add.conn.cursor()
  79.     entry = (nick, channel, message, event)
  80.     c.execute("INSERT INTO seen VALUES(?, ?, ?, ?)", entry)
  81.     add.conn.commit()
  82.     c.close()
  83.     add.conn.close()
  84.  
  85. def seenmsg(phenny, input):
  86.     event = "PRIVMSG"
  87.     seenstore(phenny, input, event)
  88. seenmsg.rule = r'.*'
  89.  
  90. def seenjoin(phenny, input):
  91.     event = "JOIN"
  92.     seenstore(phenny, input, event)
  93. seenjoin.event = 'JOIN'
  94. seenjoin.rule = r'.*'
  95.  
  96. def seenquit(phenny, input):
  97.     event = "QUIT"
  98.     seenstore(phenny, input, event)
  99. seenquit.event = 'QUIT'
  100. seenquit.rule = r'.*'
  101.  
  102. def seenpart(phenny, input):
  103.     event = "PART"
  104.     seenstore(phenny, input, event)
  105. seenquit.event = 'PART'
  106. seenquit.rule = r'.*'
Advertisement
Add Comment
Please, Sign In to add comment