Delete Users » History » Version 4
  Zaffre, 05/31/2023 06:34 AM 
  
| 1 | 1 | ragna | h1. Delete Users (SQLite) | 
|---|---|---|---|
| 2 | 1 | ragna | |
| 3 | 4 | Zaffre | Script to remove Quassel users from a SQLite database backend. Code below copied to "pastebin.com/MxAujj47":https://pastebin.com/MxAujj47 for your wget needs. | 
| 4 | 1 | ragna | |
| 5 | 1 | ragna | <pre><code class="text"> | 
| 6 | 1 | ragna | #!/bin/sh | 
| 7 | 1 | ragna | # | 
| 8 | 1 | ragna | # Delete Quasselcore users from your SQLite database | 
| 9 | 1 | ragna | # | 
| 10 | 1 | ragna | # File: deleteuser-sqlite.sh | 
| 11 | 1 | ragna | # Author: Robbe Van der Gucht | 
| 12 | 1 | ragna | # License: BSD-3-Clause, GPLv2, GPLv3 | 
| 13 | 1 | ragna | # License statements can be found at the bottom. | 
| 14 | 1 | ragna | # Any of the indicated licenses can be chosen for redistribution | 
| 15 | 1 | ragna | # and only requires one of the license statements to be preserved. | 
| 16 | 1 | ragna | |
| 17 | 1 | ragna | exeq() | 
| 18 | 1 | ragna | { | 
| 19 | 1 | ragna | # Execute SQL Query | 
| 20 | 1 | ragna |     result=$(sqlite3 "${QUASSELDB}" "${1}") | 
| 21 | 1 | ragna |     echo "${result}" | 
| 22 | 1 | ragna | } | 
| 23 | 1 | ragna | |
| 24 | 1 | ragna | usage() | 
| 25 | 1 | ragna | { | 
| 26 | 1 | ragna |     echo "Usage: ${SCRIPT} username [database]" | 
| 27 | 1 | ragna | } | 
| 28 | 1 | ragna | |
| 29 | 1 | ragna | print_users() | 
| 30 | 1 | ragna | { | 
| 31 | 1 | ragna |     sqlite3 "${QUASSELDB}" "SELECT quasseluser.userid, quasseluser.username FROM quasseluser ORDER BY quasseluser.userid;" | 
| 32 | 1 | ragna | } | 
| 33 | 1 | ragna | |
| 34 | 1 | ragna | # Main body | 
| 35 | 1 | ragna | |
| 36 | 1 | ragna | SCRIPT="${0}" | 
| 37 | 1 | ragna | QUASSELDB="" | 
| 38 | 1 | ragna | USER="" | 
| 39 | 1 | ragna | |
| 40 | 1 | ragna | if [ -z "${2}" ] ; then | 
| 41 | 1 | ragna | # No file supplied. | 
| 42 | 1 | ragna | QUASSELDB="quassel-storage.sqlite" | 
| 43 | 1 | ragna | else | 
| 44 | 1 | ragna |     QUASSELDB="${2}" | 
| 45 | 1 | ragna | fi | 
| 46 | 1 | ragna | |
| 47 | 1 | ragna | if [ -z "${1}" ] ; then | 
| 48 | 1 | ragna | echo "No user supplied." | 
| 49 | 1 | ragna | echo "Pick one: " | 
| 50 | 1 | ragna | print_users | 
| 51 | 1 | ragna | usage | 
| 52 | 1 | ragna | exit 1 | 
| 53 | 1 | ragna | else | 
| 54 | 1 | ragna |     USER="${1}" | 
| 55 | 1 | ragna | fi | 
| 56 | 1 | ragna | |
| 57 | 1 | ragna | if [ -e "${QUASSELDB}" ] ; then | 
| 58 | 1 | ragna |     echo "SELECTED DB: ${QUASSELDB}" | 
| 59 | 1 | ragna | else | 
| 60 | 1 | ragna |     echo "SELECTED DB '${QUASSELDB}' does not exist." | 
| 61 | 1 | ragna | usage | 
| 62 | 1 | ragna | exit 2 | 
| 63 | 1 | ragna | fi | 
| 64 | 1 | ragna | |
| 65 | 1 | ragna | if [ -z $(exeq "SELECT quasseluser.username FROM quasseluser WHERE username = '${USER}';") ] ; then | 
| 66 | 1 | ragna |     echo "SELECTED USER '${USER}' does not exist." | 
| 67 | 1 | ragna | print_users | 
| 68 | 1 | ragna | usage | 
| 69 | 1 | ragna | exit 3 | 
| 70 | 1 | ragna | else | 
| 71 | 1 | ragna |     echo "SELECTED USER: ${USER}" | 
| 72 | 1 | ragna | fi | 
| 73 | 1 | ragna | |
| 74 | 1 | ragna | # Sadly SQLITE does not allow DELETE statements that JOIN tables. | 
| 75 | 1 | ragna | # All queries are written with a subquery. | 
| 76 | 1 | ragna | # Contact me if you know a better way. | 
| 77 | 1 | ragna | |
| 78 | 1 | ragna | backlogq="DELETE | 
| 79 | 1 | ragna | FROM backlog | 
| 80 | 1 | ragna | WHERE backlog.bufferid in ( | 
| 81 | 1 | ragna | SELECT bufferid | 
| 82 | 1 | ragna | FROM buffer, quasseluser | 
| 83 | 1 | ragna | WHERE buffer.userid = quasseluser.userid | 
| 84 | 1 | ragna |     AND quasseluser.username = '${USER}' | 
| 85 | 1 | ragna | );" | 
| 86 | 1 | ragna | |
| 87 | 1 | ragna | bufferq="DELETE | 
| 88 | 1 | ragna | FROM buffer | 
| 89 | 1 | ragna | WHERE buffer.userid in ( | 
| 90 | 1 | ragna | SELECT userid | 
| 91 | 1 | ragna | FROM quasseluser | 
| 92 | 1 | ragna |     WHERE quasseluser.username = '${USER}' | 
| 93 | 1 | ragna | );" | 
| 94 | 1 | ragna | |
| 95 | 1 | ragna | ircserverq="DELETE | 
| 96 | 1 | ragna | FROM ircserver | 
| 97 | 1 | ragna | WHERE ircserver.userid in ( | 
| 98 | 1 | ragna | SELECT userid | 
| 99 | 1 | ragna | FROM quasseluser | 
| 100 | 1 | ragna |     WHERE quasseluser.username = '${USER}' | 
| 101 | 1 | ragna | );" | 
| 102 | 1 | ragna | |
| 103 | 1 | ragna | identity_nickq="DELETE | 
| 104 | 1 | ragna | FROM identity_nick | 
| 105 | 1 | ragna | WHERE identity_nick.identityid in ( | 
| 106 | 1 | ragna | SELECT identityid | 
| 107 | 1 | ragna | FROM quasseluser, identity | 
| 108 | 1 | ragna | WHERE quasseluser.userid = identity.userid | 
| 109 | 1 | ragna |     AND quasseluser.username = '${USER}' | 
| 110 | 1 | ragna | );" | 
| 111 | 1 | ragna | |
| 112 | 1 | ragna | identityq="DELETE | 
| 113 | 1 | ragna | FROM identity | 
| 114 | 1 | ragna | WHERE identity.userid in ( | 
| 115 | 1 | ragna | SELECT userid | 
| 116 | 1 | ragna | FROM quasseluser | 
| 117 | 1 | ragna |     WHERE quasseluser.username = '${USER}' | 
| 118 | 1 | ragna | );" | 
| 119 | 1 | ragna | |
| 120 | 1 | ragna | networkq="DELETE | 
| 121 | 1 | ragna | FROM network | 
| 122 | 1 | ragna | WHERE network.userid in ( | 
| 123 | 1 | ragna | SELECT userid | 
| 124 | 1 | ragna | FROM quasseluser | 
| 125 | 1 | ragna |     WHERE quasseluser.username = '${USER}' | 
| 126 | 1 | ragna | );" | 
| 127 | 1 | ragna | |
| 128 | 1 | ragna | usersettingq="DELETE | 
| 129 | 1 | ragna | FROM user_setting | 
| 130 | 1 | ragna | WHERE user_setting.userid in ( | 
| 131 | 1 | ragna | SELECT userid | 
| 132 | 1 | ragna | FROM quasseluser | 
| 133 | 1 | ragna |     WHERE quasseluser.username = '${USER}' | 
| 134 | 1 | ragna | );" | 
| 135 | 1 | ragna | |
| 136 | 1 | ragna | quasseluserq="DELETE | 
| 137 | 1 | ragna | FROM quasseluser | 
| 138 | 1 | ragna | WHERE quasseluser.username = '${USER}' | 
| 139 | 1 | ragna | ;" | 
| 140 | 1 | ragna | |
| 141 | 1 | ragna | |
| 142 | 1 | ragna | exeq "${backlogq}" | 
| 143 | 1 | ragna | exeq "${bufferq}" | 
| 144 | 1 | ragna | exeq "${ircserverq}" | 
| 145 | 1 | ragna | exeq "${identity_nickq}" | 
| 146 | 1 | ragna | exeq "${identityq}" | 
| 147 | 1 | ragna | exeq "${networkq}" | 
| 148 | 1 | ragna | exeq "${usersettingq}" | 
| 149 | 1 | ragna | exeq "${quasseluserq}" | 
| 150 | 1 | ragna | |
| 151 | 1 | ragna | |
| 152 | 1 | ragna | #-----------------------------------------------------------------------------# | 
| 153 | 1 | ragna | # BSD-3-Clause | 
| 154 | 1 | ragna | # Copyright (c) 2018, Robbe Van der Gucht | 
| 155 | 1 | ragna | # All rights reserved. | 
| 156 | 1 | ragna | |
| 157 | 1 | ragna | # Redistribution and use in source and binary forms, with or without | 
| 158 | 1 | ragna | # modification, are permitted provided that the following conditions are met: | 
| 159 | 1 | ragna | # 1. Redistributions of source code must retain the above copyright | 
| 160 | 1 | ragna | # notice, this list of conditions and the following disclaimer. | 
| 161 | 1 | ragna | # 2. Redistributions in binary form must reproduce the above copyright | 
| 162 | 1 | ragna | # notice, this list of conditions and the following disclaimer in the | 
| 163 | 1 | ragna | # documentation and/or other materials provided with the distribution. | 
| 164 | 1 | ragna | # 3. All advertising materials mentioning features or use of this software | 
| 165 | 1 | ragna | # must display the following acknowledgement: | 
| 166 | 1 | ragna | # This product includes software developed by the <organization>. | 
| 167 | 1 | ragna | # 4. Neither the name of the <organization> nor the | 
| 168 | 1 | ragna | # names of its contributors may be used to endorse or promote products | 
| 169 | 1 | ragna | # derived from this software without specific prior written permission. | 
| 170 | 1 | ragna | |
| 171 | 1 | ragna | # THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY | 
| 172 | 1 | ragna | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | 
| 173 | 1 | ragna | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| 174 | 1 | ragna | # DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY | 
| 175 | 1 | ragna | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | 
| 176 | 1 | ragna | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | 
| 177 | 1 | ragna | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | 
| 178 | 1 | ragna | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
| 179 | 1 | ragna | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | 
| 180 | 1 | ragna | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| 181 | 1 | ragna | |
| 182 | 1 | ragna | #-----------------------------------------------------------------------------# | 
| 183 | 1 | ragna | # GPL Clauses | 
| 184 | 1 | ragna | # Copyright (c) 2018, Robbe Van der Gucht | 
| 185 | 1 | ragna | # All rights reserved. | 
| 186 | 1 | ragna | |
| 187 | 1 | ragna | # This program is free software: you can redistribute it and/or modify | 
| 188 | 1 | ragna | # it under the terms of the GNU General Public License as published by | 
| 189 | 1 | ragna | # the Free Software Foundation, either version 2 of the License, or | 
| 190 | 1 | ragna | # (at your option) any later version. | 
| 191 | 1 | ragna | |
| 192 | 1 | ragna | # This program is distributed in the hope that it will be useful, | 
| 193 | 1 | ragna | # but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 194 | 1 | ragna | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
| 195 | 1 | ragna | # GNU General Public License for more details. | 
| 196 | 1 | ragna | |
| 197 | 1 | ragna | # You should have received a copy of the GNU General Public License | 
| 198 | 1 | ragna | # along with this program. If not, see <http://www.gnu.org/licenses/>. | 
| 199 | 1 | ragna | </code></pre> | 
| 200 | 3 | ragna | |
| 201 | 3 | ragna | It was decided in "PR 294":https://github.com/quassel/quassel/pull/294 to put this on the wiki instead of the main repo. |