author | unc0rr |
Sun, 12 Apr 2009 15:35:22 +0000 | |
changeset 1970 | 130e7805d49c |
parent 1963 | 58c29439225d |
child 1979 | 912e450d4db2 |
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 |
|
1921 | 18 |
localAddressList = ["127.0.0.1", "0:0:0:0:0:0:0:1", "0:0:0:0:0:ffff:7f00:1"] |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
19 |
|
1857
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
20 |
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
|
21 |
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
|
22 |
case q of |
1921 | 23 |
CheckAccount client -> do |
24 |
writeChan (coreChan serverInfo) $ ClientAccountInfo (clientUID client) $ |
|
25 |
if host client `elem` localAddressList then Admin else Guest |
|
1857
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 |
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
|
28 |
|
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
29 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
30 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
31 |
-- borrowed from base 4.0.0 --------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
32 |
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
|
33 |
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
|
34 |
throw (e :: Exception) -- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
35 |
-- to be deleted -------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
36 |
------------------------------------------------------------------- |
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
37 |
|
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
38 |
dbQueryString = |
1963 | 39 |
"select users.pass, users_roles.rid from users left join users_roles on users.uid = users_roles.uid where users.name = ?" |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
40 |
|
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
41 |
dbInteractionLoop queries coreChan dbConn = do |
1833 | 42 |
q <- readChan queries |
1804 | 43 |
case q of |
1921 | 44 |
CheckAccount client -> do |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
45 |
statement <- prepare dbConn dbQueryString |
1921 | 46 |
execute statement [SqlString $ nick client] |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
47 |
passAndRole <- fetchRow statement |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
48 |
finish statement |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
49 |
if isJust passAndRole then |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
50 |
writeChan coreChan $ |
1921 | 51 |
ClientAccountInfo (clientUID client) $ |
1847
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
52 |
HasAccount |
2178c0fc838c
Set admin flag and send admin notification to users with rid equal to 3
unc0rr
parents:
1841
diff
changeset
|
53 |
(fromSql $ head $ fromJust $ passAndRole) |
1970
130e7805d49c
Prevent server from crashing when get SqlNull value
unc0rr
parents:
1963
diff
changeset
|
54 |
((fromSql $ last $ fromJust $ passAndRole) == (Just (3 :: Int))) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
55 |
else |
1921 | 56 |
writeChan coreChan $ ClientAccountInfo (clientUID client) Guest |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
57 |
`onException` |
1921 | 58 |
(unGetChan queries q) |
1804 | 59 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
60 |
dbInteractionLoop queries coreChan dbConn |
1804 | 61 |
|
1833 | 62 |
dbConnectionLoop serverInfo = do |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
63 |
Control.Exception.handle (\e -> infoM "Database" $ show e) $ handleSqlError $ |
1804 | 64 |
bracket |
1833 | 65 |
(connectMySQL defaultMySQLConnectInfo {mysqlHost = dbHost serverInfo, mysqlDatabase = "hedge_main", mysqlUser = dbLogin serverInfo, mysqlPassword = dbPassword serverInfo }) |
1804 | 66 |
(disconnect) |
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
67 |
(dbInteractionLoop (dbQueries serverInfo) (coreChan serverInfo)) |
1804 | 68 |
|
1839
5dd4cb7fd7e5
Server now send ASKPASSWORD command to frontend when user has web account
unc0rr
parents:
1834
diff
changeset
|
69 |
threadDelay (5 * 10^6) |
1833 | 70 |
dbConnectionLoop serverInfo |
1804 | 71 |
|
1833 | 72 |
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
|
73 |
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
|
74 |
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
|
75 |
else |
b835395659e2
Fake database connection with routine which marks all users as guests, when no database host was specified
unc0rr
parents:
1847
diff
changeset
|
76 |
forkIO $ fakeDbConnection serverInfo |