View difference between Paste ID: U60QWWBp and 2TMEuLm0
SHOW: | | - or go back to the newest paste.
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
30+
		unique(character, channel) 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
    
83
    # check if character already exists, and verify user if it does
84
    
85
    
86
    # get the url from the input
87
    try: 
88
        character, url = input.group(2).split()
89
    except ValueError: 
90
        url = input.group(2)
91
        character = nick
92
    if not url:
93
        if not verify_url(character):
94
            phenny.say(add.__doc__.strip() + ' You may have entered an invalid URL.') 
95
            return
96
        else:
97
            url = character
98
            character = nick
99
    else:
100
        if not verify_url(url):
101
            phenny.say(add.__doc__.strip() + ' You may have entered an invalid URL.')
102
            return
103
        if store(phenny, channel, nick, url, character):
104
            phenny.say('Image for ' + character + ' at ' + url + ' added successfully.')
105
add.commands = ['add']
106
add.thread = False
107
add.priority = 'low'
108
109
def remove(phenny, channel, nick, character):
110
    sqlitedata = {
111
        'channel': channel,
112
        'character': character,
113
    }
114
    remove.conn = t_connect(phenny.img_db)
115-
        delete from character_images where character = :character 
115+
116-
            and nick = :nick 
116+
117
    c.execute('''
118
        delete from character_images where character = :character
119
            and channel = :channel;
120
    ''', sqlitedata)
121
    c.close()
122
    remove.conn.commit()
123
    return True
124
remove.conn = None
125
remove.thread = False
126
127-
     sqlitedata = {
127+
128
    try:
129
        sqlitedata = {
130
            'channel': channel,
131-
     }
131+
            'nick': nick,
132
            'character': character,
133-
     verify = False
133+
        }
134
    
135-
     verify_nick.conn = t_connect(phenny.img_db)
135+
        verify = False
136
    
137-
     c = verify_nick.conn.cursor()
137+
        verify_nick.conn = t_connect(phenny.img_db)
138-
     c.execute('''
138+
139
        c = verify_nick.conn.cursor()
140
        c.execute('''
141-
     ''', sqlitedata)
141+
142
            and nick = :nick and character = :character;
143
        ''', sqlitedata)
144
    # we should only fetch one record since the characters are unique. 
145-
     if c.fetchone():
145+
146-
      verify = True
146+
147-
     c.close()
147+
        if c.fetchone():
148-
     verify_nick.conn.close()
148+
            verify = True
149-
     return verify
149+
        c.close()
150
        verify_nick.conn.close()
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 + "\".")
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
        return verify
155
verify_nick.conn = None
156
verify_nick.thread = False
157
158
def delete(phenny, input):
159
    '''.del <character> - Removes the character's image from the database. 
160
    You must be an admin or own the character to delete the image.'''
161
    channel = input.sender
162
    nick = input.nick
163
    character = input.group(2)
164
    # admins can delete any image they want
165
    if input.admin:
166
        if remove(phenny, channel, nick, character):
167
            phenny.say('!!ADMIN!! deleted the image for ' + character + ' owned by ' + nick + ' from the database.')
168
            return
169
        else: 
170
            phenny.say('Oh SHIT! Something went wrong!')
171
    else: 
172
        if not verify_nick(phenny, channel, nick, character):
173
            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 + ".")
174
            return
175
        else:
176
            if remove(phenny, channel, nick, character):
177
                phenny.say('Successfully deleted the image for ' + character + ' owned by ' + nick + ' from the database.')
178
            else:
179
                phenny.say('Oh SHIT! Something went wrong!')
180
delete.commands = ['del','delete','remove']
181
delete.thread = False
182
183
def get(phenny, input):
184
    '''.get <character> - Retrieves the image URL for the given character.'''
185
    channel = input.sender
186
    character = input.group(2)
187
    
188
    sqlitedata = {
189
        'channel': channel,
190
        'character': character,
191
    }
192
    if not get.conn:
193
        get.conn = t_connect(phenny.img_db)
194
    
195
    c = get.conn.cursor()
196
    c.execute('''
197
        select url from character_images where channel = :channel 
198
            and character = :character;
199-
     url = c.fetchone()[0] # we just fetched a tuple with only one value
199+
200-
     if url:
200+
201-
      phenny.say('The url for ' + character + ' is ' + url)
201+
202-
      return
202+
        url = c.fetchone()[0] # we just fetched a tuple with only one value
203-
     else:
203+
        if url:
204-
      phenny.say('No url found for ' + character + '. Someone might\'ve done fucked up.')
204+
            phenny.say('The url for ' + character + ' is ' + url)
205-
      return
205+
206
        else:
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! :(")
207+
            phenny.say('No url found for ' + character + '. Someone might\'ve done fucked up.')
208
            return
209
    except TypeError: #A NoneType will raise an error.
210
        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! :(")
211
    c.close()
212
get.conn = None
213
get.commands = ['get']
214
get.thread = False
215
216
def getall(phenny, input):
217
    '''admin only get command'''
218
    
219
    if not input.admin:
220
        phenny.msg(input.nick, getall.__doc__.strip())
221
    else: 
222
        if not getall.conn:
223
            getall.conn = t_connect(phenny.img_db)
224
    
225
    c = getall.conn.cursor()
226
    c.execute('''select channel, nick, character, url from character_images;''')
227
    # we need to process all of these results. 
228
    for x in range(0, c.rowcount):
229
        row = c.fetchone()
230
        phenny.msg(input.nick, 'from ' + row[0] + ' nick ' + row[1] + ' posted ' + row[2] + '\'s image at ' + row[3])
231
    c.close()
232
getall.conn = None
233
getall.commands = ['getall']
234
getall.thread = False