Skip to content
Snippets Groups Projects
Commit 51003f47 authored by Yann Leboulanger's avatar Yann Leboulanger
Browse files

remove the old plugin system

new connection and GnuPG class
parent 317f0e0d
No related branches found
No related tags found
No related merge requests found
## Core/GnuPG.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org>
## - Nikos Kouremenos <nkour@jabber.org>
##
## Copyright (C) 2003-2005 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
from tempfile import *
USE_GPG = 1
try:
import GnuPGInterface
except:
USE_GPG = 0
class GnuPG(GnuPGInterface.GnuPG):
def __init__(self):
GnuPGInterface.GnuPG.__init__(self)
self._setup_my_options()
def _setup_my_options(self):
self.options.armor = 1
self.options.meta_interactive = 0
self.options.extra_args.append('--no-secmem-warning')
# Nolith's patch - prevent crashs on non fully-trusted keys
self.options.extra_args.append('--always-trust')
def _read_response(self, child_stdout):
# Internal method: reads all the output from GPG, taking notice
# only of lines that begin with the magic [GNUPG:] prefix.
# (See doc/DETAILS in the GPG distribution for info on GPG's
# output when --status-fd is specified.)
#
# Returns a dictionary, mapping GPG's keywords to the arguments
# for that keyword.
resp = {}
while 1:
line = child_stdout.readline()
if line == "": break
line = line.rstrip()
if line[0:9] == '[GNUPG:] ':
# Chop off the prefix
line = line[9:]
L = line.split(None, 1)
keyword = L[0]
if len(L) > 1:
resp[ keyword ] = L[1]
else:
resp[ keyword ] = ""
return resp
def encrypt(self, str, recipients):
if not USE_GPG:
return str
self.options.recipients = recipients # a list!
proc = self.run(['--encrypt'], create_fhs=['stdin', 'stdout'])
proc.handles['stdin'].write(str)
proc.handles['stdin'].close()
output = proc.handles['stdout'].read()
proc.handles['stdout'].close()
try: proc.wait()
except IOError: pass
return self._stripHeaderFooter(output)
def decrypt(self, str, keyID):
if not USE_GPG:
return str
proc = self.run(['--decrypt', '-q', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status'])
enc = self._addHeaderFooter(str, 'MESSAGE')
proc.handles['stdin'].write(enc)
proc.handles['stdin'].close()
output = proc.handles['stdout'].read()
proc.handles['stdout'].close()
resp = proc.handles['status'].read()
proc.handles['status'].close()
try: proc.wait()
except IOError: pass
return output
def sign(self, str, keyID):
if not USE_GPG:
return str
proc = self.run(['-b', '-u %s'%keyID], create_fhs=['stdin', 'stdout', 'status', 'stderr'])
proc.handles['stdin'].write(str)
proc.handles['stdin'].close()
output = proc.handles['stdout'].read()
proc.handles['stdout'].close()
proc.handles['stderr'].close()
stat = proc.handles['status']
resp = self._read_response(stat)
proc.handles['status'].close()
try: proc.wait()
except IOError: pass
if resp.has_key('BAD_PASSPHRASE'):
return 'BAD_PASSPHRASE'
elif resp.has_key('GOOD_PASSPHRASE'):
return self._stripHeaderFooter(output)
def verify(self, str, sign):
if not USE_GPG:
return str
if not str:
return ''
file = TemporaryFile(prefix='gajim')
fd = file.fileno()
file.write(str)
file.seek(0)
proc = self.run(['--verify', '--enable-special-filenames', '-', '-&%s'%fd], create_fhs=['stdin', 'status', 'stderr'])
file.close()
sign = self._addHeaderFooter(sign, 'SIGNATURE')
proc.handles['stdin'].write(sign)
proc.handles['stdin'].close()
proc.handles['stderr'].close()
stat = proc.handles['status']
resp = self._read_response(stat)
proc.handles['status'].close()
try: proc.wait()
except IOError: pass
keyid = ''
if resp.has_key('GOODSIG'):
keyid = resp['GOODSIG'].split()[0]
elif resp.has_key('BADSIG'):
keyid = resp['BADSIG'].split()[0]
return keyid
def get_secret_keys(self):
if not USE_GPG:
return
proc = self.run(['--with-colons', '--list-secret-keys'], \
create_fhs=['stdout'])
output = proc.handles['stdout'].read()
proc.handles['stdout'].close()
keys = {}
lines = output.split('\n')
for line in lines:
sline = line.split(':')
if sline[0] == 'sec':
keys[sline[4][8:]] = sline[9]
return keys
try: proc.wait()
except IOError: pass
def _stripHeaderFooter(self, data):
"""Remove header and footer from data"""
lines = data.split('\n')
while lines[0] != '':
lines.remove(lines[0])
while lines[0] == '':
lines.remove(lines[0])
i = 0
for line in lines:
if line:
if line[0] == '-': break
i = i+1
line = '\n'.join(lines[0:i])
return line
def _addHeaderFooter(self, data, type):
"""Add header and footer from data"""
out = "-----BEGIN PGP %s-----\n" % type
out = out + "Version: PGP\n"
out = out + "\n"
out = out + data + "\n"
out = out + "-----END PGP %s-----\n" % type
return out
This diff is collapsed.
This diff is collapsed.
## common/hub.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org>
##
## Copyright (C) 2003-2005 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
import Queue
import common.plugin
import common.thread
""" Hub definitions """
class GajimHub:
def __init__(self):
self.queues = {}
# {event1:[queue1, queue2]}
self.events = {}
self.queueIn = self.newQueue('in', 100)
self.saveQueue = Queue.Queue(100)
self.events_to_store = ['WARNING', 'MSG', 'MSGERROR', 'SUBSCRIBED', 'UNSUBSCRIBED', 'SUBSCRIBE']
self.queue_to_send = None
# END __init__
def newQueue(self, name, size):
""" Creates a new queue """
qu = Queue.Queue(size)
self.queues[name] = qu
return qu
# END newQueue
def newPlugin(self, name):
"""Creates a new Plugin """
if name in self.queues.keys():
return 0
qu = self.newQueue(name, 100)
pl = common.plugin.GajimPlugin(name, qu, self.queueIn)
return pl
# END newPlugin
def register(self, name, event):
""" Records a plugin from an event """
if not self.queues.has_key(name):
return
qu = self.queues[name]
if event == 'VISUAL' and not self.saveQueue.empty():
# we save the queue in whitch we must send saved events
# after the roster is sent
self.queue_to_send = qu
if self.events.has_key(event):
if not qu in self.events[event]:
self.events[event].append(qu)
else :
self.events[event] = [qu]
# END register
def unregisterEvents(self, name, event):
""" Records a plugin from an event """
if not self.queues.has_key(name):
return
qu = self.queues[name]
if self.events.has_key(event) :
if qu in self.events[event]:
self.events[event].remove(qu)
# END register
def unregister(self, name):
if not self.queues.has_key(name):
return
qu = self.queues[name]
for event in self.events:
if qu in self.events[event]:
self.events[event].remove(qu)
del self.queues[name]
# END unregister
def sendPlugin(self, event, con, data, qu=None):
""" Sends an event to registered plugins"""
if self.events.has_key(event):
if event in self.events_to_store and len(self.events['VISUAL']) == 0:
# Save event if no visual plugin is registered
self.saveQueue.put((event, con, data))
for queue in self.events[event]:
if qu == None or qu == queue:
queue.put((event, con, data))
if event == 'ROSTER' and self.queue_to_send in self.events[event]:
# send saved events
while not self.saveQueue.empty():
ev = self.saveQueue.get()
self.queue_to_send.put(ev)
self.queue_to_send = None
# END sendPlugin
# END GajimHub
## common/plugin.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org>
##
## Copyright (C) 2003-2005 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
import common.thread
""" Plugin definitions """
class GajimPlugin:
def __init__(self, name, queueIn, queueOut):
""" queueIn is a queue to interact from the hub to the plugin """
self.name = name
self.queueIn = queueIn
self.queueOut= queueOut
# END __init__
def load(self):
thr = common.thread.GajimThread(self.name, self.queueIn, self.queueOut)
# thr.setDaemon(1)
thr.start()
# END load
# END GajimPlugin
## common/thread.py
##
## Gajim Team:
## - Yann Le Boulanger <asterix@lagaule.org>
## - Vincent Hanquez <tab@snarc.org>
##
## Copyright (C) 2003-2005 Gajim Team
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published
## by the Free Software Foundation; version 2 only.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
import threading
import socket
import time
import sys
from common import i18n
_ = i18n._
class GajimThread(threading.Thread):
def __init__(self, name = None, queueIn = None, queueOut = None):
self.queueIn = queueIn
self.queueOut = queueOut
threading.Thread.__init__(self, target = self.run, name = name)
# END __init__
def run(self):
mod = compile('import plugins.%s' % self.getName(), self.getName(), 'exec')
res = eval(mod)
mod = compile('plugins.%s.%s.plugin(self.queueIn, self.queueOut)'
% (self.getName(), self.getName()), self.getName(), 'exec')
res = eval(mod)
# END run
# END GajimThread
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment