askcompu

Untitled

Jul 10th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.49 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.     if not remove.conn:
  112.         remove.conn = t_connect(phenny.img_db)
  113.    
  114.     c = remove.conn.cursor()
  115.     c.execute('''
  116.        delete from character_images where character = :character
  117.            and nick = :nick
  118.            and channel = :channel;
  119.    ''', sqlitedata)
  120.     c.close()
  121.     remove.conn.commit()
  122.     return True
  123. remove.conn = None
  124. remove.thread = False
  125.  
  126. def verify_nick(phenny, channel, nick, character):
  127.     try:
  128.      sqlitedata = {
  129.         'channel': channel,
  130.         'nick': nick,
  131.         'character': character,
  132.      }
  133.    
  134.      verify = False
  135.    
  136.      if not verify_nick.conn:
  137.       verify_nick.conn = t_connect(phenny.img_db)
  138.    
  139.      c = verify_nick.conn.cursor()
  140.      c.execute('''
  141.        select * from character_images where channel = :channel
  142.            and nick = :nick and character = :character;
  143.     ''', sqlitedata)
  144.     # we should only fetch one record since the characters are unique.
  145.     # if no record was retrieved, we'll default to no verification, and
  146.     # the delete can't proceed.
  147.      if c.fetchone():
  148.       verify = True
  149.      c.close()
  150.      verify_nick.conn.close()
  151.      return verify
  152.     except SyntaxError:
  153.      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 + "\".")
  154. verify_nick.conn = None
  155. verify_nick.thread = False
  156.  
  157. def delete(phenny, input):
  158.     '''.del <character> - Removes the character's image from the database.
  159.    You must be an admin or own the character to delete the image.'''
  160.     channel = input.sender
  161.     nick = input.nick
  162.     character = input.group(2)
  163.     # admins can delete any image they want
  164.     if input.admin:
  165.         if remove(phenny, channel, nick, character):
  166.             phenny.say('!!ADMIN!! deleted the image for ' + character + ' owned by ' + nick + ' from the database.')
  167.             return
  168.         else:
  169.             phenny.say('Oh SHIT! Something went wrong!')
  170.     else:
  171.         if not verify_nick(phenny, channel, nick, character):
  172.             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 + ".")
  173.             return
  174.         else:
  175.             if remove(phenny, channel, nick, character):
  176.                 phenny.say('Successfully deleted the image for ' + character + ' owned by ' + nick + ' from the database.')
  177.             else:
  178.                 phenny.say('Oh SHIT! Something went wrong!')
  179. delete.commands = ['del','delete','remove']
  180. delete.thread = False
  181.  
  182. def get(phenny, input):
  183.     '''.get <character> - Retrieves the image URL for the given character.'''
  184.     channel = input.sender
  185.     character = input.group(2)
  186.    
  187.     sqlitedata = {
  188.         'channel': channel,
  189.         'character': character,
  190.     }
  191.     if not get.conn:
  192.         get.conn = t_connect(phenny.img_db)
  193.    
  194.     c = get.conn.cursor()
  195.     c.execute('''
  196.        select url from character_images where channel = :channel
  197.            and character = :character;
  198.    ''', sqlitedata) # we don't want to check the nick;
  199.     # if someone else looks up a character, no match will be found.
  200.     try:
  201.      url = c.fetchone()[0] # we just fetched a tuple with only one value
  202.      if url:
  203.       phenny.say('The url for ' + character + ' is ' + url)
  204.       return
  205.      else:
  206.       phenny.say('No url found for ' + character + '. Someone might\'ve done fucked up.')
  207.       return
  208.     except TypeError: #A NoneType will raise an error.
  209.      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! :(")
  210.     c.close()
  211. get.conn = None
  212. get.commands = ['get']
  213. get.thread = False
  214.  
  215. def getall(phenny, input):
  216.     '''admin only get command'''
  217.    
  218.     if not input.admin:
  219.         phenny.msg(input.nick, getall.__doc__.strip())
  220.     else:
  221.         if not getall.conn:
  222.             getall.conn = t_connect(phenny.img_db)
  223.    
  224.     c = getall.conn.cursor()
  225.     c.execute('''select channel, nick, character, url from character_images;''')
  226.     # we need to process all of these results.
  227.     for x in range(0, c.rowcount):
  228.         row = c.fetchone()
  229.         phenny.msg(input.nick, 'from ' + row[0] + ' nick ' + row[1] + ' posted ' + row[2] + '\'s image at ' + row[3])
  230.     c.close()
  231. getall.conn = None
  232. getall.commands = ['getall']
  233. getall.thread = False
Advertisement
Add Comment
Please, Sign In to add comment