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