Attachment 'SaveMailTo0.2.1.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - SaveMailTo macro
   4 
   5     @copyright: 2007 Richard Flieger
   6     @contact: < e DASH mail AT rf DASH mmx DOT de >
   7     @license: GNU GPL, see COPYING for details.
   8     
   9     Description:
  10     
  11         Outputs from the given email an image to disable spam robots.
  12         Replaces AT, DOT and DASH, strips blanks and ALLUPPERCASE words.
  13         On every request a new image is generated.
  14     
  15     History:
  16     
  17         2007/04/17 0.2.1:  some refactoring
  18         2007/04/02 0.2b: Beta, new features
  19         2007/03/18 0.1b: Beta, dirty hack
  20         
  21         
  22     Features:
  23     
  24         * letters
  25             * rotate letters
  26             * random color
  27             * move letters
  28         * different spacing
  29         * lines
  30             * random line-color
  31             * fixed line-color
  32             * random line position
  33             * lines in front and/or background
  34         * background
  35             * random bg-color
  36             * fixed bg-color
  37         * split image
  38             * fixed pieces
  39             * random pieces
  40         * small java-script for clicking
  41         * link for refreshing the image
  42             * configurable
  43         * unique image name
  44         * easier configuration
  45    
  46 """
  47 
  48 import re, sha
  49 from MoinMoin.Page import Page
  50 from MoinMoin import user
  51 from MoinMoin.action import AttachFile
  52 import Image, ImageDraw, ImageFont, ImageOps
  53 from MoinMoin.util.mail import decodeSpamSafeEmail
  54 import random
  55 
  56 Dependencies = [""]
  57 
  58 class UserList:
  59     
  60     ############################################################################
  61     ##
  62     ##  Configuration
  63     ##
  64     ############################################################################
  65     
  66     ## E-Mail Link
  67     ## -------------------------------------------------------------------------
  68     ## Enable or disable javascript for email-link
  69     javascript_enable = True
  70     
  71     ## Link below the image, for refreshing the image
  72     refesh_text = u"refresh email-image"
  73     
  74     ## Text
  75     ## -------------------------------------------------------------------------
  76      ## Font
  77      ## You can also define your own font
  78     font = u"lubB24.pil"
  79         
  80      ## Range of random space between every letter
  81      ## ( from, to )
  82     space = 0,10
  83     
  84      ## Rotate a little bit, do some trick if you want
  85      ## Range of degrees for rotation of every letter
  86      ## ( from, to )
  87     letter_rotate_range = -20, 20
  88     
  89      ## rgb color range ( from, to )
  90     letter_color_red = 150, 150 ## red
  91     letter_color_green = 200, 256 ## green
  92     letter_color_blue = 150, 150 ## blue
  93 
  94     ## Background
  95     ## -------------------------------------------------------------------------
  96 
  97     ## Backgournd-Color
  98     
  99      ## fixed
 100     enable_fixed_bg_color = True
 101      ## rgb color (red,green,blue)
 102     bg_color_fixed = 100, 165, 79
 103     
 104      ## random
 105     enable_random_bg_color = False
 106      ## rgb color random range ( from, to )
 107     bg_color_red = 120, 120 ##red
 108     bg_color_green = 120, 120 ## green
 109     bg_color_blue = 80, 140 ## blue
 110     
 111     ## Lines
 112     enable_lines_in_bg = True
 113     enable_lines_in_font = False
 114      ## fixed
 115     enable_fixed_line_color = False
 116      ## rgb color (red,green,blue)
 117     line_color_fixed = 0, 0, 0
 118     
 119      ## random
 120     enable_random_line_color = True
 121      ## rgb color random range ( from, to )
 122     line_color_red = 100, 100 ##red
 123     line_color_green = 120, 250 ## green
 124     line_color_blue = 100, 100 ## blue
 125 
 126     enable_split_image = True
 127 
 128     entrys = 15
 129     
 130     ############################################################################
 131     ##
 132     ##  End of configuration
 133     ##
 134     ############################################################################
 135     
 136     
 137     Dependencies = Dependencies
 138     
 139     def __init__( self, macro, email ):
 140         self.request = macro.request
 141         self.formatter = macro.formatter
 142         self.pagename = macro.formatter.page.page_name
 143         self.email = email
 144 
 145     def getSpace(self):
 146         """
 147             return the random size of space between a letter
 148         """
 149         return random.randint(self.space[0], self.space[1])
 150 
 151 
 152     def getLetterImage(self, letter):
 153         """
 154             generate from a single letter a image
 155         """
 156         # Create a dummy image 1x1
 157         im = Image.new("RGBA", (1, 1))
 158         draw = ImageDraw.Draw(im)
 159         lettersize = draw.textsize(letter.encode("iso-8859-1"),font=self.font)
 160         # Create a small image to hold one character. Background is black
 161         charImg = Image.new('RGB', (lettersize[0], lettersize[1]))
 162         tmpDraw = ImageDraw.Draw(charImg)
 163         fill = self.getTextColor()
 164         tmpDraw.text((0, 0), letter.encode("iso-8859-1"),font=self.font, fill=fill)
 165         # Rotate a little bit, do some trick if you want
 166         rot = random.randint(self.letter_rotate_range[0], self.letter_rotate_range[1])
 167         charImg = charImg.rotate(rot, expand=1)
 168         return charImg
 169 
 170 
 171     def getLines(self, draw, image):
 172         """
 173             draw random lines
 174         """
 175         if self.enable_random_line_color or self.enable_fixed_line_color:
 176             r = random.Random()
 177             for a in range(len(self.email.encode("iso-8859-1"))):
 178                 fill = self.getLineColor()
 179                 x1 = r.randint(0, image.size[0])
 180                 y1 = r.randint(0, image.size[1])
 181                 x2 = r.randint(0, image.size[0])
 182                 y2 = r.randint(0, image.size[1])
 183                 width = r.randint(1, 7)
 184                 draw.line((x1, y1, x2, y2),fill=fill, width=width)
 185 
 186         return draw
 187 
 188 
 189     def getTextColor(self):
 190         """
 191             Return a random 3-tuple for text-color
 192         """
 193         fill = random.randint(self.letter_color_red[0],
 194                               self.letter_color_red[1]),\
 195                random.randint(self.letter_color_green[0],
 196                               self.letter_color_green[1]),\
 197                random.randint(self.letter_color_blue[0],
 198                               self.letter_color_blue[1])
 199         return fill
 200 
 201         
 202     def render(self):
 203         
 204         self.getFont()
 205         self.email = decodeSpamSafeEmail( self.email )
 206 
 207         self.getImagePath()
 208         html = []              
 209         try:
 210             im = Image.new( "RGBA", ( 1, 1 ), ( 0, 0, 0 ) )
 211             
 212             draw = ImageDraw.Draw( im )
 213             textsize = draw.textsize( self.email.encode( "iso-8859-1" ),
 214                                      font = self.font )
 215             
 216             heigh = int( textsize[1] + 20 )
 217             width = int( textsize[0] * 1.6 )
 218             
 219             fill = self.getBGColor()
 220             
 221             image = Image.new( "RGB", ( width, heigh ), fill )
 222             
 223             draw = ImageDraw.Draw( image )
 224 
 225             ## lines behind the text
 226             if (self.enable_random_line_color or self.enable_fixed_line_color)\
 227             and self.enable_lines_in_bg:
 228                 draw = self.getLines(draw, image)
 229 
 230             newtextsize = 0
 231             r = random.Random()
 232 
 233             
 234             imgH = self.getSpace()
 235             hpos = self.getSpace()
 236             
 237             for letter in self.email:
 238                 
 239                 charImg = self.getLetterImage( letter)
 240             
 241                 # Create a mask which is same size of the small image
 242                 mask = Image.new('L',(charImg.size[0],charImg.size[1]))
 243                 
 244                 mask.paste(charImg,(0,0))
 245             
 246                 # Generate Random Y
 247                 vpos = random.randint(0,20)
 248             
 249                 # Paste for the visibility
 250                 image.paste(charImg,(hpos,vpos),mask)
 251 
 252                 if charImg.size[1] > imgH:
 253                     imgH = charImg.size[1]
 254                 
 255                 # Generate Random X
 256                 hpos = hpos + charImg.size[0] + random.randint(self.space[0],
 257                                                                self.space[1])
 258             
 259             # Draw random lines in front of the image
 260             if (self.enable_random_line_color or self.enable_fixed_line_color)\
 261             and self.enable_lines_in_font:
 262                 draw = self.getLines(draw, image)
 263                           
 264             del draw
 265             
 266             html.append(u"""<table><tbody><tr><td style="border: 0pt none ;text-align: center;">""")
 267             if self.enable_split_image and type(self.entrys) == int and self.entrys:
 268                 hpos = 0
 269                 for entry in range(self.entrys):
 270                     width = int(image.size[0]/self.entrys)
 271                     im = Image.new( "RGB",(width,image.size[1] ) )
 272                     im.paste(image,(0-hpos,0))
 273                     hpos += width
 274                     imagename =  self.imgname + "_" + str(entry)  + ".jpg" 
 275                     im.save(self.imgpath  + "/" + imagename, "JPEG")
 276                     html.append( self.getHTML( imagename ) )
 277             else:
 278                 imagename =  self.imgname + "_0.jpg" 
 279                 image.save(self.imgpath  + "/" + imagename, "JPEG")
 280                 html.append( self.getHTML( imagename ) )
 281                 
 282         
 283             html.append( u"""</td></tr><tr><td style="border: 0pt none ;text-align: center;">""")
 284             html.append( u"""<a href="?action=show">%(refresh_text)s</a> """
 285                          % { u"refresh_text" : self.refesh_text })
 286             html.append( u"""</td></tr></tbody></table>""")
 287         except:
 288             pass
 289         
 290         return "\n".join(html)
 291     
 292     def getLineColor(self):
 293         """
 294             Return a random 3-tuple for line-color
 295         """
 296         r = random.Random()
 297         try:
 298             if self.enable_fixed_line_color:
 299                 # get defined color
 300                 return self.line_color_fixed
 301             elif self.enable_random_line_color:
 302                 # generate random color
 303                 return (r.randint(self.line_color_red[0],self.line_color_red[1]),r.randint(self.line_color_green[0],self.line_color_green[1]),r.randint(self.line_color_blue[0],self.line_color_blue[1]))
 304             else:
 305                 return ( 0, 0, 0 )
 306         except:
 307             return ( 0, 0, 0 )
 308     
 309     def getBGColor(self):
 310         """
 311             Return a random 3-tuple for background-color
 312         """
 313         r = random.Random()
 314         if self.enable_fixed_bg_color:
 315             return self.bg_color_fixed
 316         elif self.enable_random_bg_color:
 317             return (r.randint(self.bg_color_red[0],self.bg_color_red[1]),r.randint(self.bg_color_green[0],self.bg_color_green[1]),r.randint(self.bg_color_blue[0],self.bg_color_blue[1]))
 318         else:
 319             return 256,256,256
 320         
 321     def getHTML(self, imagename):
 322         """
 323             Create the HTML for the page
 324         """
 325         kw = {} 
 326         kw['src'] = AttachFile.getAttachUrl(self.pagename,
 327                                             imagename.encode("iso-8859-1"),
 328                                             self.request)
 329 
 330         html = []
 331  
 332         if self.javascript_enable:
 333             html.append( u"""\n<script language="JavaScript"><!--\n""")
 334             index = 0
 335             for char in self.email:
 336                 html.append( u"""var v%(index)d = "%(char)s";\n"""
 337                              % { u"index" : index, u"char" : char } )
 338                 index += 1
 339             html.append( u"""document.write('<a href=\"mailto:'""")
 340             
 341             for index in range(len(self.email)):
 342                 html.append( u""" + v%d""" % index )
 343              
 344             html.append( u""" + '\">');""")
 345             
 346             html.append( u"""document.write('""")
 347 #                     
 348         html.append( self.formatter.image( **kw ) )
 349         
 350         if self.javascript_enable:
 351             html.append(u"""</a>');""")
 352             
 353             html.append( u"""// --></script>\n""")
 354         
 355         return u"".join(html)
 356 
 357     def getImagePath(self):
 358         """
 359             return path to save the new image
 360         """
 361         self.getImageName()
 362         self.imgpath = AttachFile.getAttachDir(self.request,self.pagename,
 363                                                create=1)
 364 
 365     def getFontPath(self):
 366         self.getFontDir()
 367         self.font_file =  self.font_dir + '\\' + self.font
 368 
 369     def getFont(self):
 370         """
 371             create the font opject
 372         """
 373         self.getFontPath()
 374         try:
 375             self.font  = ImageFont.load(self.font_file)
 376         except:
 377             self.font = None
 378         
 379     def getImageName(self):
 380         """
 381             return the crypted imagename
 382         """
 383         import base64
 384         
 385         filename = self.email.encode("utf-8")
 386         filename = sha.new(filename).hexdigest()
 387         filename = base64.encodestring(filename).rstrip()
 388         self.imgname = "cryptedemail_" + filename
 389         
 390     def getFontDir(self):
 391         """
 392             return the directory of the font
 393         """
 394         try:
 395             self.font_dir = self.request.cfg.font_dir
 396         except:
 397             self.font_dir = self.request.cfg.plugin_dir + "\\font"
 398 
 399 def execute(macro, args):
 400     return UserList(macro, args).render()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-04-17 16:35:42, 13.2 KB) [[attachment:SaveMailTo0.2.1.py]]
  • [get | view] (2007-04-17 16:44:06, 9.7 KB) [[attachment:SaveMailTo_Example.html]]
  • [get | view] (2007-04-02 15:30:36, 5.1 KB) [[attachment:blue.jpg]]
  • [get | view] (2007-04-02 15:31:06, 5.5 KB) [[attachment:colorfull.jpg]]
  • [get | view] (2007-04-17 16:16:38, 0.8 KB) [[attachment:green0.jpg]]
  • [get | view] (2007-04-17 16:17:10, 0.9 KB) [[attachment:green1.jpg]]
  • [get | view] (2007-04-17 16:20:30, 1.1 KB) [[attachment:green10.jpg]]
  • [get | view] (2007-04-17 16:20:52, 1.0 KB) [[attachment:green11.jpg]]
  • [get | view] (2007-04-17 16:21:17, 1.0 KB) [[attachment:green12.jpg]]
  • [get | view] (2007-04-17 16:21:40, 0.9 KB) [[attachment:green13.jpg]]
  • [get | view] (2007-04-17 16:21:57, 0.9 KB) [[attachment:green14.jpg]]
  • [get | view] (2007-04-17 16:17:28, 1.0 KB) [[attachment:green2.jpg]]
  • [get | view] (2007-04-17 16:17:45, 1.0 KB) [[attachment:green3.jpg]]
  • [get | view] (2007-04-17 16:18:01, 0.9 KB) [[attachment:green4.jpg]]
  • [get | view] (2007-04-17 16:18:25, 0.9 KB) [[attachment:green5.jpg]]
  • [get | view] (2007-04-17 16:18:42, 1.0 KB) [[attachment:green6.jpg]]
  • [get | view] (2007-04-17 16:19:38, 1.0 KB) [[attachment:green7.jpg]]
  • [get | view] (2007-04-17 16:19:50, 1.0 KB) [[attachment:green8.jpg]]
  • [get | view] (2007-04-17 16:20:11, 1.1 KB) [[attachment:green9.jpg]]
  • [get | view] (2007-04-02 15:30:18, 2.2 KB) [[attachment:lightgrey.jpg]]
  • [get | view] (2007-04-02 15:30:54, 6.0 KB) [[attachment:orange.jpg]]
  • [get | view] (2007-04-02 15:31:27, 7.8 KB) [[attachment:red.jpg]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.