You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.3KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "time"
  9. )
  10. //a Procedure is a alphanumerical string lessthan 128 bytes.
  11. //when messages comes in we need to differentiate what Procedure is this in.
  12. //default Procedure is where command can happen
  13. //where the Procedure is stored,
  14. // in sessions/openID.json
  15. //if there is no such file, we are in default Procedure.
  16. //First Procedure is to record user's name, email, and phone number.
  17. // for individual Procedure
  18. // Procedure/Procedurename/openID.json
  19. var sessionDir = "sessions"
  20. //where to find the procedure state information
  21. var ProcedureDir = "Procedure"
  22. //openIDSession openID user's session
  23. type openIDSession struct {
  24. Procedure string `json:"Procedure"` //name of the current current process, alphanumerical
  25. CreateAt int32 `json:"CreateAt"` //when is this session created
  26. UpdateAt int32 `json:"UpdateAt"` //when is this session updated
  27. Expire int32 `json:"Expire"` //unix timestamp of when this Procedure expires
  28. }
  29. //read sessions/openID.json to check whether its a Procedure for ongoing dialog
  30. func getCurrentSesssion(openID string) (result openIDSession, err error) {
  31. path := getSessionPath(openID)
  32. log.Printf("read session from %s\r\n", path)
  33. body, err := ioutil.ReadFile(path)
  34. if err != nil { //read file error
  35. if isFileExist(path) {
  36. log.Println("Error session reading " + path)
  37. }
  38. return //empty and expired session
  39. }
  40. err = json.Unmarshal(body, &result)
  41. if err != nil {
  42. log.Printf("Session Content [path=%s] not correct: ", path)
  43. }
  44. //we don't check Expire, we give the caller full control on
  45. //how to deal wiht expired session
  46. return
  47. }
  48. func writeSession(openid string, ss openIDSession) (err error) {
  49. path := getSessionPath(openid)
  50. r, err := json.Marshal(ss)
  51. if err == nil {
  52. err = ioutil.WriteFile(path, r, 0600)
  53. if err != nil {
  54. log.Printf("Error writing session %s: \r\n %s \r\n", openid, r)
  55. log.Println(err)
  56. } else {
  57. log.Println("write session to " + path)
  58. }
  59. } else {
  60. log.Printf("Error encoding session %s ", openid)
  61. log.Println(err)
  62. }
  63. return
  64. }
  65. //create if not available
  66. func setSessionProcedure(openID, procedure string, expireAfter int32) (updatedSession openIDSession, err error) {
  67. s, err := getCurrentSesssion(openID)
  68. now := int32(time.Now().Unix())
  69. if s.CreateAt == 0 {
  70. s.CreateAt = now
  71. }
  72. s.UpdateAt = now
  73. s.Procedure = procedure
  74. s.Expire = now + expireAfter
  75. err = writeSession(openID, s)
  76. return s, err
  77. }
  78. func isExpired(timestamp int32) bool {
  79. now := int32(time.Now().Unix())
  80. return now > timestamp
  81. }
  82. func getSessionPath(openID string) (path string) {
  83. path = sessionDir + string(os.PathSeparator) + openID + ".json"
  84. ensurePathExist(path)
  85. return path
  86. }
  87. func getProcedurePath(openID, ProcedureName string) (path string) {
  88. path = ProcedureDir + string(os.PathSeparator) + ProcedureName + string(os.PathSeparator) + openID + ".json"
  89. ensurePathExist(path)
  90. return
  91. }
  92. func ensurePathExist(path string) {
  93. d := filepath.Dir(path)
  94. if !isFileExist(d) {
  95. log.Println("Creating path [" + d + "]")
  96. os.MkdirAll(d, 0700)
  97. }
  98. }
  99. func deleteSession(openID string) {
  100. path := getSessionPath(openID)
  101. if isFileExist(path) {
  102. err := os.Remove(path)
  103. if err != nil {
  104. log.Println(err)
  105. } else {
  106. log.Println("delete session :" + path)
  107. }
  108. }
  109. }