| 1 | #! /usr/bin/env python |
|---|
| 2 | # -*- coding: utf8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Itaka is free software; you can redistribute it and/or modify |
|---|
| 5 | # it under the terms of the GNU General Public License as published by |
|---|
| 6 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 7 | # any later version. |
|---|
| 8 | # |
|---|
| 9 | # Itaka is distributed in the hope that it will be useful, |
|---|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | # GNU General Public License for more details. |
|---|
| 13 | # |
|---|
| 14 | # You should have received a copy of the GNU General Public License |
|---|
| 15 | # along with Itaka; if not, write to the Free Software |
|---|
| 16 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 | # |
|---|
| 18 | # Copyright 2003-2007 Marc E. <santusmarc_at_gmail.com>. |
|---|
| 19 | # http://itaka.jardinpresente.com.ar |
|---|
| 20 | # |
|---|
| 21 | # $Id$ |
|---|
| 22 | |
|---|
| 23 | """ Itaka web server engine """ |
|---|
| 24 | |
|---|
| 25 | from twisted.web.resource import Resource |
|---|
| 26 | import datetime, os, traceback, sys |
|---|
| 27 | |
|---|
| 28 | try: |
|---|
| 29 | import screenshot as iscreenshot |
|---|
| 30 | except ImportError: |
|---|
| 31 | print "[*] ERROR: Failed to import Itaka screenshot module." |
|---|
| 32 | traceback.print_exc() |
|---|
| 33 | sys.exit(1) |
|---|
| 34 | |
|---|
| 35 | # Server hit iteration counter |
|---|
| 36 | lcounter = 0 |
|---|
| 37 | |
|---|
| 38 | class ImageResource(Resource): |
|---|
| 39 | """ Take the screenshot code and handle the requests. """ |
|---|
| 40 | |
|---|
| 41 | def __init__(self, guiinstance, consoleinstance): |
|---|
| 42 | """ Intialize inherited GUI, Console and global Configuration (through GUI instance) values """ |
|---|
| 43 | self.gui = guiinstance |
|---|
| 44 | self.console = consoleinstance |
|---|
| 45 | self.itakaglobals = self.gui.itakaglobals |
|---|
| 46 | |
|---|
| 47 | def render_GET(self, request): |
|---|
| 48 | """ Handle GET requests for screenshot. """ |
|---|
| 49 | |
|---|
| 50 | # Get up to date configuration values |
|---|
| 51 | self.configuration = self.gui.configuration |
|---|
| 52 | |
|---|
| 53 | if (request.uri == "/screenshot"): |
|---|
| 54 | request.setHeader("Content-type", "image/" + self.configuration['screenshot']['format']) |
|---|
| 55 | request.setHeader("Connection", "close") |
|---|
| 56 | |
|---|
| 57 | self.icip = request.getClientIP() |
|---|
| 58 | self.time = datetime.datetime.now() |
|---|
| 59 | # self.icbrowser = request.getClient() |
|---|
| 60 | |
|---|
| 61 | # This takes the screenshot |
|---|
| 62 | self.shotFile = iscreenshot.Screenshot(self.gui) |
|---|
| 63 | |
|---|
| 64 | if self.shotFile is not False: |
|---|
| 65 | global lcounter |
|---|
| 66 | lcounter += 1 |
|---|
| 67 | |
|---|
| 68 | # Call libnotify |
|---|
| 69 | print self.configuration['server']['notify'] |
|---|
| 70 | if (self.configuration['server']['notify'] == "True") and self.itakaglobals.notifyavailable != False: |
|---|
| 71 | import pynotify |
|---|
| 72 | |
|---|
| 73 | uri = "file://" + (os.path.join(self.itakaglobals.image_dir, "itaka-take.png")) |
|---|
| 74 | |
|---|
| 75 | n = pynotify.Notification("Itaka Screenshot taken", "%s took screenshot number %d" % (self.icip, lcounter), uri) |
|---|
| 76 | if not n.show(): |
|---|
| 77 | pass |
|---|
| 78 | |
|---|
| 79 | # Tell the GUI what changed |
|---|
| 80 | self.gui.talk('updateGuiStatus', str(lcounter), str(self.icip), self.time) |
|---|
| 81 | |
|---|
| 82 | return open(self.shotFile, 'rb').read() |
|---|