askcompu

Untitled

Jul 10th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.66 KB | None | 0 0
  1. #!/usr/bin/env python
  2. '''
  3. img.py - PinkiePyBot character image storage module
  4. (C) 2012 Jordan T Kinsley ([email protected])
  5. GPLv2 or later
  6.  
  7. This module will store an image for a roleplay character.
  8. By image I of course mean link, because screw DCC. 6_9
  9. '''
  10.  
  11. import os, sqlite3, re
  12.  
  13. # The regular expression that matches any of http: , https: , ftp: , and ftps:
  14. # as well as .jpg, .png, .gif, and .jpeg.
  15. url_re = re.compile('(ht|f)tp(s)?://.*')
  16.  
  17. def t_connect(db):
  18.     return sqlite3.connect(db, check_same_thread = False)
  19.  
  20. def setup(self):
  21.     self.img_db = os.path.join(os.path.expanduser('~/.phenny'), 'img.db')
  22.     self.img_conn = t_connect(self.img_db)
  23.  
  24.     c = self.img_conn.cursor()
  25.     c.execute('''create table if not exists character_images(
  26.         channel     varchar(255),
  27.         nick        varchar(255),
  28.         character   varchar(255),
  29.         url         text,
  30.         unique(character) on conflict replace
  31.    );''')
  32.     c.close()
  33.     self.img_conn.commit()
  34. setup.thread = False
  35.  
  36. def verify_url(url):
  37.     m = url_re.match(url)
  38.     if m:
  39.         return True
  40.     return False
  41.  
  42. def store(phenny, channel, nick, url, character=None):
  43.     # If no character is supplied, the nick is the character.
  44.     # e.g. from #RPMeetup : Aurora, Hush, Necropony, Thorinair
  45.     if not character:
  46.         character = nick
  47.    
  48.     if not verify_url(url):
  49.         raise GrumbleError("THAT'S NOT A FUCKING CORRECT URL!")
  50.    
  51.     sqlitedata = {
  52.         'channel': channel,
  53.         'nick': nick,
  54.         'character': character,
  55.         'url': url,
  56.     }
  57.    
  58.     if not store.conn:
  59.         store.conn = t_connect(phenny.img_db)
  60.    
  61.     c = store.conn.cursor()
  62.     c.execute('''insert or replace into character_images
  63.                (channel, nick, character, url)
  64.                    values(
  65.                    :channel,
  66.                    :nick,
  67.                    :character,
  68.                    :url);''', sqlitedata)
  69.     c.close()
  70.     store.conn.commit()
  71.     return True
  72. store.conn = None # these variables MUST be named with the function, other they won't register. :S
  73. store.thread = False
  74.  
  75. def add(phenny, input):
  76.     '''.add [<character>] <image url> - Adds an image URL for a given character.
  77.    The URL must have http in it.'''
  78.     if not input.sender.startswith('#'):
  79.         phenny.reply('This is a channel-only command. Please try again in a channel.')
  80.     channel = input.sender
  81.     nick = input.nick
  82.     # get the url from the input
  83.     try:
  84.         character, url = input.group(2).split()
  85.     except ValueError:
  86.         url = input.group(2)
  87.         character = nick
  88.     if not url:
  89.         if not verify_url(character):
  90.             phenny.say(add.__doc__.strip() + ' You may have entered an invalid URL.')
  91.             return
  92.         else:
  93.             url = character
  94.             character = nick
  95.     else:
  96.         if not verify_url(url):
  97.             phenny.say(add.__doc__.strip() + ' You may have entered an invalid URL.')
  98.             return
  99.         if store(phenny, channel, nick, url, character):
  100.             phenny.say('Image for ' + character + ' at ' + url + ' added successfully.')
  101. add.commands = ['add']
  102. add.thread = False
  103. add.priority = 'low'
  104.  
  105. def remove(phenny, channel, nick, character):
  106.     sqlitedata = {
  107.         'channel': channel,
  108.         'nick': nick,
  109.         'character': character,
  110.     }
  111.     remove.conn = t_connect(phenny.img_db)
  112.    
  113.     c = remove.conn.cursor()
  114.     c.execute('''
  115.        delete from character_images where character = :character
  116.            and nick = :nick
  117.            and channel = :channel;
  118.    ''', sqlitedata)
  119.     c.close()
  120.     remove.conn.commit()
  121.     return True
  122. remove.conn = None
  123. remove.thread = False
  124.  
  125. def verify_nick(phenny, channel, nick, character):
  126.     try:
  127.      sqlitedata = {
  128.         'channel': channel,
  129.         'nick': nick,
  130.         'character': character,
  131.      }
  132.    
  133.      verify = False
  134.    
  135.      verify_nick.conn = t_connect(phenny.img_db)
  136.    
  137.      c = verify_nick.conn.cursor()
  138.      c.execute('''
  139.        select * from character_images where channel = :channel
  140.            and nick = :nick and character = :character;
  141.     ''', sqlitedata)
  142.     # we should only fetch one record since the characters are unique.
  143.     # if no record was retrieved, we'll default to no verification, and
  144.     # the delete can't proceed.
  145.      if c.fetchone():
  146.       verify = True
  147.      c.close()
  148.      verify_nick.conn.close()
  149.      return verify
  150.     except SyntaxError:
  151.      phenny.say("I'm sorry but you're not authorized to delete " + character + ", if you own the link then make sure you're using the nick \"" + nick + "\".")
  152. verify_nick.conn = None
  153. verify_nick.thread = False
  154.  
  155. def delete(phenny, input):
  156.     '''.del <character> - Removes the character's image from the database.
  157.    You must be an admin or own the character to delete the image.'''
  158.     channel = input.sender
  159.     nick = input.nick
  160.     character = input.group(2)
  161.     # admins can delete any image they want
  162.     if input.admin:
  163.         if remove(phenny, channel, nick, character):
  164.             phenny.say('!!ADMIN!! deleted the image for ' + character + ' owned by ' + nick + ' from the database.')
  165.             return
  166.         else:
  167.             phenny.say('Oh SHIT! Something went wrong!')
  168.     else:
  169.         if not verify_nick(phenny, channel, nick, character):
  170.             phenny.reply("I'm sorry but you're not authorized to delete " + character + ", if you own the link then make sure you're using the nick you used when you created " + character + ".")
  171.             return
  172.         else:
  173.             if remove(phenny, channel, nick, character):
  174.                 phenny.say('Successfully deleted the image for ' + character + ' owned by ' + nick + ' from the database.')
  175.             else:
  176.                 phenny.say('Oh SHIT! Something went wrong!')
  177. delete.commands = ['del','delete','remove']
  178. delete.thread = False
  179.  
  180. def get(phenny, input):
  181.     '''.get <character> - Retrieves the image URL for the given character.'''
  182.     channel = input.sender
  183.     character = input.group(2)
  184.    
  185.     sqlitedata = {
  186.         'channel': channel,
  187.         'character': character,
  188.     }
  189.     if not get.conn:
  190.         get.conn = t_connect(phenny.img_db)
  191.    
  192.     c = get.conn.cursor()
  193.     c.execute('''
  194.        select url from character_images where channel = :channel
  195.            and character = :character;
  196.    ''', sqlitedata) # we don't want to check the nick;
  197.     # if someone else looks up a character, no match will be found.
  198.     try:
  199.      url = c.fetchone()[0] # we just fetched a tuple with only one value
  200.      if url:
  201.       phenny.say('The url for ' + character + ' is ' + url)
  202.       return
  203.      else:
  204.       phenny.say('No url found for ' + character + '. Someone might\'ve done fucked up.')
  205.       return
  206.     except TypeError: #A NoneType will raise an error.
  207.      phenny.say("Sowwy, but I don't remember anything by that name! Are you sure that you stored it? Pwease don't be mad at me! :(")
  208.     c.close()
  209. get.conn = None
  210. get.commands = ['get']
  211. get.thread = False
  212.  
  213. def getall(phenny, input):
  214.     '''admin only get command'''
  215.    
  216.     if not input.admin:
  217.         phenny.msg(input.nick, getall.__doc__.strip())
  218.     else:
  219.         if not getall.conn:
  220.             getall.conn = t_connect(phenny.img_db)
  221.    
  222.     c = getall.conn.cursor()
  223.     c.execute('''select channel, nick, character, url from character_images;''')
  224.     # we need to process all of these results.
  225.     for x in range(0, c.rowcount):
  226.         row = c.fetchone()
  227.         phenny.msg(input.nick, 'from ' + row[0] + ' nick ' + row[1] + ' posted ' + row[2] + '\'s image at ' + row[3])
  228.     c.close()
  229. getall.conn = None
  230. getall.commands = ['getall']
  231. getall.thread = False
Advertisement
Add Comment
Please, Sign In to add comment