| 1 | ## |
|---|
| 2 | ## Copyright (C) 2005-2006 Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 3 | ## Copyright (C) 2005-2006 Nikos Kouremenos <kourem@gmail.com> |
|---|
| 4 | ## Copyright (C) 2005-2006 Travis Shirk <travis@pobox.com> |
|---|
| 5 | ## |
|---|
| 6 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 7 | ## it under the terms of the GNU General Public License as published |
|---|
| 8 | ## by the Free Software Foundation; version 2 only. |
|---|
| 9 | ## |
|---|
| 10 | ## This program is distributed in the hope that it will be useful, |
|---|
| 11 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | ## GNU General Public License for more details. |
|---|
| 14 | ## |
|---|
| 15 | |
|---|
| 16 | import os |
|---|
| 17 | import sys |
|---|
| 18 | import stat |
|---|
| 19 | |
|---|
| 20 | from common import gajim |
|---|
| 21 | import logger |
|---|
| 22 | |
|---|
| 23 | # DO NOT MOVE ABOVE OF import gajim |
|---|
| 24 | try: |
|---|
| 25 | import sqlite3 as sqlite # python 2.5 |
|---|
| 26 | except ImportError: |
|---|
| 27 | try: |
|---|
| 28 | from pysqlite2 import dbapi2 as sqlite |
|---|
| 29 | except ImportError: |
|---|
| 30 | raise exceptions.PysqliteNotAvailable |
|---|
| 31 | |
|---|
| 32 | def create_log_db(): |
|---|
| 33 | print _('creating logs database') |
|---|
| 34 | con = sqlite.connect(logger.LOG_DB_PATH) |
|---|
| 35 | os.chmod(logger.LOG_DB_PATH, 0600) # rw only for us |
|---|
| 36 | cur = con.cursor() |
|---|
| 37 | # create the tables |
|---|
| 38 | # kind can be |
|---|
| 39 | # status, gcstatus, gc_msg, (we only recv for those 3), |
|---|
| 40 | # single_msg_recv, chat_msg_recv, chat_msg_sent, single_msg_sent |
|---|
| 41 | # to meet all our needs |
|---|
| 42 | # logs.jid_id --> jids.jid_id but Sqlite doesn't do FK etc so it's done in python code |
|---|
| 43 | # jids.jid text column will be JID if TC-related, room_jid if GC-related, |
|---|
| 44 | # ROOM_JID/nick if pm-related. |
|---|
| 45 | cur.executescript( |
|---|
| 46 | ''' |
|---|
| 47 | CREATE TABLE jids( |
|---|
| 48 | jid_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, |
|---|
| 49 | jid TEXT UNIQUE, |
|---|
| 50 | type INTEGER |
|---|
| 51 | ); |
|---|
| 52 | |
|---|
| 53 | CREATE TABLE unread_messages( |
|---|
| 54 | message_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, |
|---|
| 55 | jid_id INTEGER |
|---|
| 56 | ); |
|---|
| 57 | |
|---|
| 58 | CREATE INDEX idx_unread_messages_jid_id ON unread_messages (jid_id); |
|---|
| 59 | |
|---|
| 60 | CREATE TABLE transports_cache ( |
|---|
| 61 | transport TEXT UNIQUE, |
|---|
| 62 | type INTEGER |
|---|
| 63 | ); |
|---|
| 64 | |
|---|
| 65 | CREATE TABLE logs( |
|---|
| 66 | log_line_id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE, |
|---|
| 67 | jid_id INTEGER, |
|---|
| 68 | contact_name TEXT, |
|---|
| 69 | time INTEGER, |
|---|
| 70 | kind INTEGER, |
|---|
| 71 | show INTEGER, |
|---|
| 72 | message TEXT, |
|---|
| 73 | subject TEXT |
|---|
| 74 | ); |
|---|
| 75 | |
|---|
| 76 | CREATE INDEX idx_logs_jid_id_kind ON logs (jid_id, kind); |
|---|
| 77 | ''' |
|---|
| 78 | ) |
|---|
| 79 | |
|---|
| 80 | con.commit() |
|---|
| 81 | con.close() |
|---|
| 82 | |
|---|
| 83 | def check_and_possibly_create_paths(): |
|---|
| 84 | LOG_DB_PATH = logger.LOG_DB_PATH |
|---|
| 85 | VCARD_PATH = gajim.VCARD_PATH |
|---|
| 86 | AVATAR_PATH = gajim.AVATAR_PATH |
|---|
| 87 | dot_gajim = os.path.dirname(VCARD_PATH) |
|---|
| 88 | if os.path.isfile(dot_gajim): |
|---|
| 89 | print _('%s is a file but it should be a directory') % dot_gajim |
|---|
| 90 | print _('Gajim will now exit') |
|---|
| 91 | sys.exit() |
|---|
| 92 | elif os.path.isdir(dot_gajim): |
|---|
| 93 | s = os.stat(dot_gajim) |
|---|
| 94 | if s.st_mode & stat.S_IROTH: # others have read permission! |
|---|
| 95 | os.chmod(dot_gajim, 0700) # rwx------ |
|---|
| 96 | |
|---|
| 97 | if not os.path.exists(VCARD_PATH): |
|---|
| 98 | create_path(VCARD_PATH) |
|---|
| 99 | elif os.path.isfile(VCARD_PATH): |
|---|
| 100 | print _('%s is a file but it should be a directory') % VCARD_PATH |
|---|
| 101 | print _('Gajim will now exit') |
|---|
| 102 | sys.exit() |
|---|
| 103 | |
|---|
| 104 | if not os.path.exists(AVATAR_PATH): |
|---|
| 105 | create_path(AVATAR_PATH) |
|---|
| 106 | elif os.path.isfile(AVATAR_PATH): |
|---|
| 107 | print _('%s is a file but it should be a directory') % AVATAR_PATH |
|---|
| 108 | print _('Gajim will now exit') |
|---|
| 109 | sys.exit() |
|---|
| 110 | |
|---|
| 111 | if not os.path.exists(LOG_DB_PATH): |
|---|
| 112 | create_log_db() |
|---|
| 113 | gajim.logger.init_vars() |
|---|
| 114 | elif os.path.isdir(LOG_DB_PATH): |
|---|
| 115 | print _('%s is a directory but should be a file') % LOG_DB_PATH |
|---|
| 116 | print _('Gajim will now exit') |
|---|
| 117 | sys.exit() |
|---|
| 118 | |
|---|
| 119 | else: # dot_gajim doesn't exist |
|---|
| 120 | if dot_gajim: # is '' on win9x so avoid that |
|---|
| 121 | create_path(dot_gajim) |
|---|
| 122 | if not os.path.isdir(VCARD_PATH): |
|---|
| 123 | create_path(VCARD_PATH) |
|---|
| 124 | if not os.path.exists(AVATAR_PATH): |
|---|
| 125 | create_path(AVATAR_PATH) |
|---|
| 126 | if not os.path.isfile(LOG_DB_PATH): |
|---|
| 127 | create_log_db() |
|---|
| 128 | gajim.logger.init_vars() |
|---|
| 129 | |
|---|
| 130 | def create_path(directory): |
|---|
| 131 | print _('creating %s directory') % directory |
|---|
| 132 | os.mkdir(directory, 0700) |
|---|