author | unc0rr |
Tue, 10 Mar 2009 21:31:29 +0000 | |
changeset 1882 | fda9407a0070 |
parent 1857 | b835395659e2 |
child 1921 | 2a09f7f786a0 |
permissions | -rw-r--r-- |
1804 | 1 |
module OfficialServer.DBInteraction |
2 |
( |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
3 |
startDBConnection |
1804 | 4 |
) where |
5 |
||
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
6 |
import Prelude hiding (catch); |
1804 | 7 |
import Database.HDBC |
8 |
import Database.HDBC.MySQL |
|
9 |
import System.IO |
|
10 |
import Control.Concurrent |
|
11 |
import Control.Exception |
|
1833 | 12 |
import Monad |
1834 | 13 |
import Maybe |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
14 |
import System.Log.Logger |
1833 | 15 |
------------------------ |
16 |
import CoreTypes |
|
1804 | 17 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
18 |
|
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
19 |
fakeDbConnection serverInfo = do |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
20 |
q <- readChan $ dbQueries serverInfo |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
21 |
case q of |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
22 |
CheckAccount clID name -> do |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
23 |
writeChan (coreChan serverInfo) $ ClientAccountInfo clID Guest |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
24 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
25 |
fakeDbConnection serverInfo |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
26 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
27 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
28 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
29 |
-- borrowed from base 4.0.0 --------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
30 |
onException :: IO a -> IO b -> IO a -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
31 |
onException io what = io `catch` \e -> do what -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
32 |
throw (e :: Exception) -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
33 |
-- to be deleted -------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
34 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
35 |
|
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
36 |
dbQueryString = |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
37 |
"SELECT users.pass, users_roles.rid FROM `users`, users_roles " |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
38 |
++ "WHERE users.name = ? AND users_roles.uid = users.uid" |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
39 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
40 |
dbInteractionLoop queries coreChan dbConn = do |
1833 | 41 |
q <- readChan queries |
1804 | 42 |
case q of |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
43 |
CheckAccount clID name -> do |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
44 |
statement <- prepare dbConn dbQueryString |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
45 |
execute statement [SqlString name] |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
46 |
passAndRole <- fetchRow statement |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
47 |
finish statement |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
48 |
if isJust passAndRole then |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
49 |
writeChan coreChan $ |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
50 |
ClientAccountInfo clID $ |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
51 |
HasAccount |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
52 |
(fromSql $ head $ fromJust $ passAndRole) |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
53 |
((fromSql $ last $ fromJust $ passAndRole) == (3 :: Int)) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
54 |
else |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
55 |
writeChan coreChan $ ClientAccountInfo clID Guest |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
56 |
`onException` |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
57 |
(unGetChan queries $ CheckAccount clID name) |
1804 | 58 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
59 |
dbInteractionLoop queries coreChan dbConn |
1804 | 60 |
|
1833 | 61 |
dbConnectionLoop serverInfo = do |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
62 |
Control.Exception.handle (\e -> infoM "Database" $ show e) $ handleSqlError $ |
1804 | 63 |
bracket |
1833 | 64 |
(connectMySQL defaultMySQLConnectInfo {mysqlHost = dbHost serverInfo, mysqlDatabase = "hedge_main", mysqlUser = dbLogin serverInfo, mysqlPassword = dbPassword serverInfo }) |
1804 | 65 |
(disconnect) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
66 |
(dbInteractionLoop (dbQueries serverInfo) (coreChan serverInfo)) |
1804 | 67 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
68 |
threadDelay (5 * 10^6) |
1833 | 69 |
dbConnectionLoop serverInfo |
1804 | 70 |
|
1833 | 71 |
startDBConnection serverInfo = |
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
72 |
if (not . null $ dbHost serverInfo) then |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
73 |
forkIO $ dbConnectionLoop serverInfo |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
74 |
else |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
75 |
forkIO $ fakeDbConnection serverInfo |