2005-04-15 19:02:23 +02:00
|
|
|
## common/gajim.py
|
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-06-06 01:42:30 +02:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2005-04-18 16:05:30 +02:00
|
|
|
## - Nikos Kouremenos <kourem@gmail.com>
|
2005-04-15 19:02:23 +02:00
|
|
|
##
|
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
|
2005-06-25 03:23:21 +02:00
|
|
|
import os
|
2005-10-30 12:20:24 +01:00
|
|
|
import sys
|
2005-04-15 19:02:23 +02:00
|
|
|
import logging
|
2005-08-09 20:45:16 +02:00
|
|
|
import mutex
|
|
|
|
|
2005-04-15 19:02:23 +02:00
|
|
|
import common.config
|
2005-11-23 20:12:52 +01:00
|
|
|
|
2005-04-15 19:02:23 +02:00
|
|
|
|
2005-10-20 13:17:17 +02:00
|
|
|
interface = None # The actual interface (the gtk one for the moment)
|
2005-09-02 18:11:52 +02:00
|
|
|
version = '0.9'
|
2005-04-15 19:02:23 +02:00
|
|
|
config = common.config.Config()
|
|
|
|
connections = {}
|
2005-05-29 21:04:01 +02:00
|
|
|
verbose = False
|
2005-05-17 22:31:43 +02:00
|
|
|
|
|
|
|
h = logging.StreamHandler()
|
|
|
|
f = logging.Formatter('%(asctime)s %(name)s: %(message)s', '%d %b %Y %H:%M:%S')
|
|
|
|
h.setFormatter(f)
|
2005-04-15 19:02:23 +02:00
|
|
|
log = logging.getLogger('Gajim')
|
2005-05-17 22:31:43 +02:00
|
|
|
log.addHandler(h)
|
|
|
|
|
2005-11-23 20:12:52 +01:00
|
|
|
import common.logger
|
2005-11-22 15:46:37 +01:00
|
|
|
logger = common.logger.Logger() # init the logger
|
|
|
|
|
2005-10-30 12:20:24 +01:00
|
|
|
if os.name == 'nt':
|
2005-11-30 19:48:58 +01:00
|
|
|
if '.svn' in os.listdir('.') or '_svn' in os.listdir('.'):
|
2005-11-22 15:46:37 +01:00
|
|
|
DATA_DIR = os.path.join('..', 'data')
|
2005-11-30 19:48:58 +01:00
|
|
|
else: # we are normal users (not svn users)
|
|
|
|
DATA_DIR = 'data'
|
2005-10-30 12:20:24 +01:00
|
|
|
try:
|
2005-11-23 20:12:52 +01:00
|
|
|
# Documents and Settings\[User Name]\Application Data\Gajim
|
2005-11-22 12:54:33 +01:00
|
|
|
LOGPATH = os.path.join(os.environ['appdata'], 'Gajim', 'Logs') # deprecated
|
2005-11-22 12:39:32 +01:00
|
|
|
VCARDPATH = os.path.join(os.environ['appdata'], 'Gajim', 'Vcards')
|
2005-10-30 12:20:24 +01:00
|
|
|
except KeyError:
|
2005-11-23 20:12:52 +01:00
|
|
|
# win9x, in cwd
|
2005-11-22 12:54:33 +01:00
|
|
|
LOGPATH = 'Logs' # deprecated
|
2005-11-22 12:56:16 +01:00
|
|
|
VCARDPATH = 'Vcards'
|
2005-11-22 12:54:33 +01:00
|
|
|
else: # Unices
|
|
|
|
DATA_DIR = '../data'
|
|
|
|
LOGPATH = os.path.expanduser('~/.gajim/logs') # deprecated
|
|
|
|
VCARDPATH = os.path.expanduser('~/.gajim/vcards')
|
|
|
|
|
2005-10-30 12:20:24 +01:00
|
|
|
try:
|
|
|
|
LOGPATH = LOGPATH.decode(sys.getfilesystemencoding())
|
|
|
|
VCARDPATH = VCARDPATH.decode(sys.getfilesystemencoding())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2005-06-25 11:35:31 +02:00
|
|
|
LANG = os.getenv('LANG') # en_US, fr_FR, el_GR etc..
|
2005-06-25 09:37:14 +02:00
|
|
|
if LANG:
|
2005-06-25 11:35:31 +02:00
|
|
|
LANG = LANG[:2] # en, fr, el etc..
|
2005-06-25 09:37:14 +02:00
|
|
|
else:
|
|
|
|
LANG = 'en'
|
2005-07-02 13:06:02 +02:00
|
|
|
|
2005-07-03 17:27:41 +02:00
|
|
|
last_message_time = {} # list of time of the latest incomming message
|
|
|
|
# {acct1: {jid1: time1, jid2: time2}, }
|
2005-07-02 13:06:02 +02:00
|
|
|
encrypted_chats = {} # list of encrypted chats {acct1: [jid1, jid2], ..}
|
2005-07-18 23:08:31 +02:00
|
|
|
|
|
|
|
contacts = {} # list of contacts {acct: {jid1: [C1, C2]}, } one Contact per resource
|
2005-07-25 22:04:24 +02:00
|
|
|
gc_contacts = {} # list of contacts that are in gc {acct: {room_jid: {nick: C}}}
|
2005-11-14 11:51:32 +01:00
|
|
|
gc_connected = {} # tell if we are connected to the room or not {acct: {room_jid: True}}
|
2005-11-13 18:24:08 +01:00
|
|
|
gc_passwords = {} # list of the pass required to enter a room {room_jid: password}
|
2005-07-25 22:04:24 +02:00
|
|
|
|
2005-07-18 23:08:31 +02:00
|
|
|
groups = {} # list of groups
|
|
|
|
newly_added = {} # list of contacts that has just signed in
|
|
|
|
to_be_removed = {} # list of contacts that has just signed out
|
2005-11-23 19:15:24 +01:00
|
|
|
|
2005-10-15 22:49:08 +02:00
|
|
|
awaiting_events = {} # list of messages/FT reveived but not printed
|
|
|
|
# awaiting_events[jid] = (type, (data1, data2, ...))
|
2005-10-16 15:18:34 +02:00
|
|
|
# if type in ('chat', 'normal'): data = (message, subject, kind, time,
|
2005-11-23 19:15:24 +01:00
|
|
|
# encrypted, resource)
|
2005-10-15 22:49:08 +02:00
|
|
|
# kind can be (incoming, error)
|
2005-10-19 23:14:51 +02:00
|
|
|
# if type in file-request, file-request-error, file-send-error, file-error,
|
|
|
|
# file-completed, file-stopped:
|
2005-10-19 22:16:22 +02:00
|
|
|
# data = file_props
|
2005-07-18 23:08:31 +02:00
|
|
|
nicks = {} # list of our nick names in each account
|
|
|
|
allow_notifications = {} # do we allow notifications for each account ?
|
|
|
|
con_types = {} # type of each connection (ssl, tls, tcp, ...)
|
2005-07-22 01:47:27 +02:00
|
|
|
|
|
|
|
sleeper_state = {} # whether we pass auto away / xa or not
|
|
|
|
#'off': don't use sleeper for this account
|
|
|
|
#'online': online and use sleeper
|
|
|
|
#'autoaway': autoaway and use sleeper
|
|
|
|
#'autoxa': autoxa and use sleeper
|
2005-07-22 23:27:04 +02:00
|
|
|
status_before_autoaway = {}
|
2005-08-09 20:45:16 +02:00
|
|
|
#queues of events from connections...
|
2005-08-09 19:21:35 +02:00
|
|
|
events_for_ui = {}
|
2005-08-09 20:45:16 +02:00
|
|
|
#... and its mutex
|
|
|
|
mutex_events_for_ui = mutex.mutex()
|
2005-07-22 02:01:05 +02:00
|
|
|
|
2005-08-16 21:26:11 +02:00
|
|
|
SHOW_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
|
|
|
|
'invisible']
|
2005-07-22 02:01:05 +02:00
|
|
|
|
2005-07-22 16:30:35 +02:00
|
|
|
def get_nick_from_jid(jid):
|
|
|
|
pos = jid.find('@')
|
|
|
|
return jid[:pos]
|
|
|
|
|
2005-09-11 17:02:22 +02:00
|
|
|
def get_server_from_jid(jid):
|
2005-09-11 19:06:37 +02:00
|
|
|
pos = jid.find('@') + 1 # after @
|
2005-09-11 17:02:22 +02:00
|
|
|
return jid[pos:]
|
|
|
|
|
2005-07-22 16:30:35 +02:00
|
|
|
def get_nick_from_fjid(jid):
|
|
|
|
# fake jid is the jid for a contact in a room
|
2005-08-30 00:24:13 +02:00
|
|
|
# gaim@conference.jabber.no/nick/nick-continued
|
2005-07-22 16:30:35 +02:00
|
|
|
return jid.split('/', 1)[1]
|
2005-07-26 09:22:08 +02:00
|
|
|
|
2005-09-11 17:02:22 +02:00
|
|
|
def get_room_name_and_server_from_room_jid(jid):
|
|
|
|
room_name = get_nick_from_jid(jid)
|
|
|
|
server = get_server_from_jid(jid)
|
|
|
|
return room_name, server
|
|
|
|
|
2005-07-26 09:22:08 +02:00
|
|
|
def get_room_and_nick_from_fjid(jid):
|
|
|
|
# fake jid is the jid for a contact in a room
|
2005-08-30 00:24:13 +02:00
|
|
|
# gaim@conference.jabber.no/nick/nick-continued
|
|
|
|
# return ('gaim@conference.jabber.no', 'nick/nick-continued')
|
|
|
|
l = jid.split('/', 1)
|
2005-09-27 00:29:52 +02:00
|
|
|
if len(l) == 1: # No nick
|
2005-08-30 00:24:13 +02:00
|
|
|
l.append('')
|
2005-08-14 23:02:51 +02:00
|
|
|
return l
|
2005-11-02 09:12:54 +01:00
|
|
|
|
|
|
|
def get_real_jid_from_fjid(account, fjid):
|
2005-11-03 21:48:27 +01:00
|
|
|
'''returns real jid or returns None
|
2005-11-02 12:06:51 +01:00
|
|
|
if we don't know the real jid'''
|
2005-11-02 09:12:54 +01:00
|
|
|
room_jid, nick = get_room_and_nick_from_fjid(fjid)
|
2005-11-02 12:06:51 +01:00
|
|
|
if not nick: # It's not a fake_jid, it is a real jid
|
2005-11-02 12:13:05 +01:00
|
|
|
return fjid # we return the real jid
|
2005-11-02 09:12:54 +01:00
|
|
|
real_jid = fjid
|
2005-11-13 21:31:44 +01:00
|
|
|
gcs = interface.instances[account]['gc']
|
2005-11-02 09:12:54 +01:00
|
|
|
if gcs.has_key(room_jid):
|
|
|
|
# It's a pm, so if we have real jid it's in contact.jid
|
2005-11-03 21:48:27 +01:00
|
|
|
if not gc_contacts[account][room_jid].has_key(nick):
|
|
|
|
return
|
2005-11-02 09:12:54 +01:00
|
|
|
contact = gc_contacts[account][room_jid][nick]
|
2005-11-02 12:06:51 +01:00
|
|
|
# contact.jid is None when it's not a real jid (we don't know real jid)
|
2005-11-02 09:12:54 +01:00
|
|
|
real_jid = contact.jid
|
|
|
|
return real_jid
|
|
|
|
|
2005-09-27 00:29:52 +02:00
|
|
|
def get_room_from_fjid(jid):
|
|
|
|
return get_room_and_nick_from_fjid(jid)[0]
|
2005-07-22 16:30:35 +02:00
|
|
|
|
2005-07-22 02:01:05 +02:00
|
|
|
def get_contact_instances_from_jid(account, jid):
|
|
|
|
''' we may have two or more resources on that jid '''
|
2005-07-22 16:30:35 +02:00
|
|
|
if jid in contacts[account]:
|
|
|
|
contacts_instances = contacts[account][jid]
|
|
|
|
return contacts_instances
|
2005-07-22 02:01:05 +02:00
|
|
|
|
|
|
|
def get_first_contact_instance_from_jid(account, jid):
|
2005-08-07 22:58:44 +02:00
|
|
|
contact = None
|
2005-07-22 16:30:35 +02:00
|
|
|
if jid in contacts[account]:
|
|
|
|
contact = contacts[account][jid][0]
|
|
|
|
else: # it's fake jid
|
2005-07-22 20:10:55 +02:00
|
|
|
#FIXME: problem see comment in next line
|
2005-07-26 09:22:08 +02:00
|
|
|
room, nick = \
|
|
|
|
get_room_and_nick_from_fjid(jid) # if we ban/kick we now real jid
|
2005-08-04 19:05:39 +02:00
|
|
|
if gc_contacts[account].has_key(room) and \
|
|
|
|
nick in gc_contacts[account][room]:
|
2005-07-26 09:22:08 +02:00
|
|
|
contact = gc_contacts[account][room][nick]
|
2005-07-22 16:30:35 +02:00
|
|
|
return contact
|
2005-07-22 02:01:05 +02:00
|
|
|
|
2005-08-06 12:20:04 +02:00
|
|
|
def get_contact_instance_with_highest_priority(account, jid):
|
|
|
|
contact_instances = contacts[account][jid]
|
2005-08-09 21:15:59 +02:00
|
|
|
return get_highest_prio_contact_from_contacts(contact_instances)
|
2005-08-06 12:20:04 +02:00
|
|
|
|
2005-07-22 02:01:05 +02:00
|
|
|
def get_contact_name_from_jid(account, jid):
|
2005-07-22 02:34:08 +02:00
|
|
|
return contacts[account][jid][0].name
|
2005-08-09 21:15:59 +02:00
|
|
|
|
|
|
|
def get_highest_prio_contact_from_contacts(contacts):
|
|
|
|
prim_contact = None # primary contact
|
|
|
|
for contact in contacts:
|
|
|
|
if prim_contact == None or int(contact.priority) > \
|
|
|
|
int(prim_contact.priority):
|
|
|
|
prim_contact = contact
|
|
|
|
return prim_contact
|
2005-07-22 02:34:08 +02:00
|
|
|
|
|
|
|
def get_jid_without_resource(jid):
|
|
|
|
return jid.split('/')[0]
|
2005-07-22 16:30:35 +02:00
|
|
|
|
|
|
|
def construct_fjid(room_jid, nick):
|
|
|
|
''' nick is in utf8 (taken from treeview); room_jid is in unicode'''
|
2005-08-04 16:30:41 +02:00
|
|
|
# fake jid is the jid for a contact in a room
|
|
|
|
# gaim@conference.jabber.org/nick
|
2005-09-11 17:02:22 +02:00
|
|
|
if isinstance(nick, str):
|
2005-08-04 16:30:41 +02:00
|
|
|
nick = unicode(nick, 'utf-8')
|
|
|
|
return room_jid + '/' + nick
|
2005-07-22 16:30:35 +02:00
|
|
|
|
|
|
|
def get_resource_from_jid(jid):
|
2005-10-07 15:45:58 +02:00
|
|
|
jids = jid.split('/', 1)
|
|
|
|
if len(jids) > 1:
|
|
|
|
return jids[1] # abc@doremi.org/res/res-continued
|
|
|
|
else:
|
|
|
|
return ''
|
2005-07-22 16:30:35 +02:00
|
|
|
'''\
|
|
|
|
[15:34:28] <asterix> we should add contact.fake_jid I think
|
|
|
|
[15:34:46] <asterix> so if we know real jid, it wil be in contact.jid, or we look in contact.fake_jid
|
|
|
|
[15:32:54] <asterix> they can have resource if we know the real jid
|
|
|
|
[15:33:07] <asterix> and that resource is in contact.resource
|
|
|
|
'''
|
2005-08-03 15:46:48 +02:00
|
|
|
|
|
|
|
def get_number_of_accounts():
|
|
|
|
return len(connections.keys())
|
2005-08-26 15:11:20 +02:00
|
|
|
|
2005-08-26 15:15:16 +02:00
|
|
|
def get_transport_name_from_jid(jid, use_config_setting = True):
|
2005-08-26 15:11:20 +02:00
|
|
|
'''returns 'aim', 'gg', 'irc' etc'''
|
2005-08-26 16:44:46 +02:00
|
|
|
#FIXME: jid can be None! one TB I saw had this problem:
|
|
|
|
# in the code block # it is a groupchat presence in handle_event_notify
|
|
|
|
# jid was None. Yann why?
|
|
|
|
if not jid or (use_config_setting and not config.get('use_transports_iconsets')):
|
2005-08-26 15:11:20 +02:00
|
|
|
return
|
|
|
|
host = jid.split('@')[-1]
|
|
|
|
if host.startswith('aim'):
|
|
|
|
return 'aim'
|
|
|
|
elif host.startswith('gadugadu'):
|
|
|
|
return 'gadugadu'
|
|
|
|
elif host.startswith('gg'):
|
|
|
|
return 'gadugadu'
|
|
|
|
elif host.startswith('irc'):
|
|
|
|
return 'irc'
|
|
|
|
# abc@icqsucks.org will match as ICQ, but what to do..
|
|
|
|
elif host.startswith('icq'):
|
|
|
|
return 'icq'
|
|
|
|
elif host.startswith('msn'):
|
|
|
|
return 'msn'
|
|
|
|
elif host.startswith('sms'):
|
|
|
|
return 'sms'
|
|
|
|
elif host.startswith('tlen'):
|
|
|
|
return 'tlen'
|
2005-08-30 00:24:13 +02:00
|
|
|
elif host.startswith('weather'):
|
|
|
|
return 'weather'
|
2005-08-26 15:11:20 +02:00
|
|
|
elif host.startswith('yahoo'):
|
|
|
|
return 'yahoo'
|
2005-10-24 16:32:13 +02:00
|
|
|
else:
|
|
|
|
return None
|
2005-08-26 15:11:20 +02:00
|
|
|
|
|
|
|
def jid_is_transport(jid):
|
2005-12-02 14:08:58 +01:00
|
|
|
aim = jid.startswith('aim.')
|
|
|
|
gg = jid.startswith('gg.') # gadugadu
|
|
|
|
irc = jid.startswith('irc.')
|
|
|
|
icq = jid.startswith('icq.')
|
|
|
|
msn = jid.startswith('msn.')
|
|
|
|
sms = jid.startswith('sms.')
|
|
|
|
tlen = jid.startswith('tlen.')
|
|
|
|
yahoo = jid.startswith('yahoo.')
|
2005-10-24 16:32:13 +02:00
|
|
|
|
2005-12-02 14:08:58 +01:00
|
|
|
if aim or gg or irc or icq or msn or sms or yahoo or tlen:
|
2005-10-24 16:32:13 +02:00
|
|
|
is_transport = True
|
|
|
|
else:
|
|
|
|
is_transport = False
|
|
|
|
|
|
|
|
return is_transport
|
2005-09-09 22:52:29 +02:00
|
|
|
|
|
|
|
def get_jid_from_account(account_name):
|
|
|
|
name = config.get_per('accounts', account_name, 'name')
|
2005-09-09 23:03:53 +02:00
|
|
|
hostname = config.get_per('accounts', account_name, 'hostname')
|
2005-09-09 22:52:29 +02:00
|
|
|
jid = name + '@' + hostname
|
|
|
|
return jid
|
2005-09-11 02:32:58 +02:00
|
|
|
|
2005-11-20 14:27:37 +01:00
|
|
|
def get_hostname_from_account(account_name, use_srv = False):
|
2005-09-11 02:32:58 +02:00
|
|
|
'''returns hostname (if custom hostname is used, that is returned)'''
|
2005-11-20 14:27:37 +01:00
|
|
|
if use_srv and connections[account_name].connected_hostname:
|
|
|
|
return connections[account_name].connected_hostname
|
2005-09-12 08:27:20 +02:00
|
|
|
if config.get_per('accounts', account_name, 'use_custom_host'):
|
|
|
|
return config.get_per('accounts', account_name, 'custom_host')
|
|
|
|
return config.get_per('accounts', account_name, 'hostname')
|
2005-10-18 11:07:52 +02:00
|
|
|
|
|
|
|
def get_first_event(account, jid, typ = None):
|
|
|
|
'''returns the first event of the given type from the awaiting_events queue'''
|
|
|
|
if not awaiting_events[account].has_key(jid):
|
|
|
|
return None
|
|
|
|
q = awaiting_events[account][jid]
|
|
|
|
if not typ:
|
|
|
|
return q[0]
|
|
|
|
for ev in q:
|
|
|
|
if ev[0] == typ:
|
|
|
|
return ev
|
|
|
|
return None
|
2005-10-18 22:30:26 +02:00
|
|
|
|
|
|
|
def popup_window(account):
|
|
|
|
autopopup = config.get('autopopup')
|
|
|
|
autopopupaway = config.get('autopopupaway')
|
|
|
|
if autopopup and (autopopupaway or connections[account].connected > 3):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def show_notification(account):
|
|
|
|
if config.get('notify_on_new_message'):
|
|
|
|
# check OUR status and if we allow notifications for that status
|
|
|
|
if config.get('autopopupaway'): # always show notification
|
|
|
|
return True
|
|
|
|
if connections[account].connected in (2, 3): # we're online or chat
|
|
|
|
return True
|
|
|
|
return False
|