source: trollforge/dsp-trollforge/Library/Libtroll.py @ 249

Revision 41, 5.4 KB checked in by sam, 8 years ago (diff)
  • DSP's trollforge snapshot.
  • Property svn:executable set to *
  • Property svn:keywords set to Id
Line 
1#!/usr/bin/env python
2
3
4
5#     _     ___  _       _____ ____   ___  _     _
6
7#    | |   / _ \| |     |_   _|  _ \ / _ \| |   | |
8
9#    | |  | | | | |       | | | |_) | | | | |   | |
10
11#    | |__| |_| | |___    | | |  _ <| |_| | |___| |___
12
13#    |_____\___/|_____|   |_| |_| \_\\___/|_____|_____|
14
15#
16
17
18
19# TODO: webmail stuff
20
21#  http://www.dodgeit.com/
22
23#  http://spam.la/
24
25
26
27# Domains that accept email for @mailinator.com
28
29mailinator = [ 'mailinator.com',
30
31               'fastacura.com',
32
33               'fastchevy.com',
34
35               'fastchrysler.com',
36
37               'fastkawasaki.com',
38
39               'fastmazda.com',
40
41               'fastmitsubishi.com',
42
43               'fastnissan.com',
44
45               'fastsubaru.com',
46
47               'fastsuzuki.com',
48
49               'fasttoyota.com',
50
51               'fastyamaha.com' ]
52
53
54
55from urllib import FancyURLopener, urlencode
56
57from re import compile
58
59
60
61def sanitize_cookie(cookie):
62
63    str = ''
64
65    regex = compile(' *([^;]*);[^;]*;[^;]*;[^;,]*,*(.*)')
66
67    while True:
68
69        m = regex.match(cookie)
70
71        if not m:
72
73            return str
74
75        str += m.group(1) + '; '
76
77        cookie = m.group(2)
78
79
80
81# SO LITTLE SO TINY SO PRETTY SO ZOMFGLOLPWND
82
83class make_troll:
84
85    # HACK CLASS BECAUSE PYTHON FAILS IT AT USING COOKIES
86
87    class urlopener(FancyURLopener):
88
89        _cookie = ''
90
91        def http_error_302(self, *args):
92
93            headers = args[4]
94
95            cookie = headers.get('set-cookie')
96
97            if cookie:
98
99                self._cookie = sanitize_cookie(cookie)
100
101                self.addheaders.append(('Cookie', self._cookie))
102
103            return FancyURLopener.http_error_302(self, *args)
104
105
106
107    # Private vars
108
109    _proxy = {}
110
111    _referer = ''
112
113    _uagent = 'Mozilla/4.0'
114
115    _cookie = ''
116
117    _data = {}
118
119
120
121    def set_proxy(self, proxy):
122
123        if proxy:
124
125            self._proxy = {'http': proxy}
126
127        else:
128
129            self._proxy = {}
130
131
132
133    def set_uagent(self, uagent):
134
135        self._uagent = uagent
136
137
138
139    def set_postdata(self, data):
140
141        self._data = data
142
143
144
145    def set_cookie(self, cookie):
146
147        self._cookie = cookie
148
149
150
151    def get_cookie(self):
152
153        return self._cookie
154
155
156
157    def get_url(self, url):
158
159        u = self.urlopener(self._proxy)
160
161        if self._uagent:
162
163            # This needs to be our first addheader call
164
165            u.addheaders = []
166
167            u.addheader('User-agent', self._uagent)
168
169        if self._cookie:
170
171            u.addheader('Cookie', self._cookie)
172
173        if self._referer:
174
175            u.addheader('Referer', self._referer)
176
177        try:
178
179            if self._data:
180
181                f = u.open(url, urlencode(self._data))
182
183            else:
184
185                f = u.open(url)
186
187        except KeyboardInterrupt:
188
189            raise
190
191        except:
192
193            # LOL @ FAILURE
194
195            return False
196
197        gotcookie = []
198
199        if u._cookie:
200
201            self._cookie = u._cookie
202
203            u._cookie = ''
204
205        info = f.info()
206
207        if info.has_key('set-cookie'):
208
209            self._cookie = sanitize_cookie(info['set-cookie'])
210
211        try:
212
213            from operator import add
214
215            text = reduce(add, f.readlines())
216
217        except KeyboardInterrupt:
218
219            raise
220
221        except:
222
223            # LOL @ FAILURE
224
225            return False
226
227        f.close()
228
229        # Update our status
230
231        self._data = {}
232
233        self._referer = url
234
235        return text
236
237
238
239    def get_mail(self, address, subject):
240
241        m = compile("(.*)@(.*)").match(address)
242
243        login = m.group(1)
244
245        domain = m.group(2)
246
247        if domain in mailinator:
248
249            return -1 # Disabled for now, they refuse email from IMDb
250
251            url = "http://www.mailinater.com/mailinator/CheckMail.do?email=" + login
252
253            banned = "nefarious, mysterious"
254
255            link = '(http://[^>]*ShowMail.do[^>]*)>[^<]*' + subject + '[^<]*<'
256
257            prefix = ''
258
259        elif domain == "spam.la":
260
261            url = "http://spam.la/?f=" + login
262
263            banned = False
264
265            link = 'href="(.id=[0-9]+)">[^<]*' + subject + '[^<]*<'
266
267            prefix = "http://spam.la/"
268
269        elif domain == "tanya.com":
270
271            url = "http://www.tanya.com/cgi-bin/checkmail.pl?username=" + login + "&button=Check+Mail&terms=on"
272
273            banned = False
274
275            link = 'href="/([^"]*id=[0-9]+)">[^<]*' + subject + '[^<]*<'
276
277            prefix = "http://www.tanya.com/"
278
279        elif domain == "dodgeit.com":
280
281            url = "http://dodgeit.com/run/checkmail?mailbox=" + login
282
283            banned = False
284
285            link = "href='([^']*id=[0-9]+)'>[^<]*" + subject + "[^<]*<"
286
287            prefix = ''
288
289        else:
290
291            return -1
292
293        # Now really get this email
294
295        output = self.get_url(url)
296
297        if not output:
298
299            return -2 # Site unavailable
300
301        if banned and output.find(banned) != -1:
302
303            return -4 # Banned...
304
305        mbox = False
306
307        for m in compile(link).finditer(output):
308
309            mbox = m.group(1)
310
311        if not mbox:
312
313            return -3 # No mail for us
314
315        output = self.get_url(prefix + mbox)
316
317        if not output:
318
319            return -2 # Site unavailable
320
321        if banned and output.find(banned) != -1:
322
323            return -4 # Banned...
324
325        return output
326
327
328
Note: See TracBrowser for help on using the repository browser.