4
0

user.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. package state
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "unicode"
  10. "github.com/google/uuid"
  11. "github.com/mk6i/retro-aim-server/wire"
  12. )
  13. var (
  14. // ErrDupUser indicates that a user already exists.
  15. ErrDupUser = errors.New("user already exists")
  16. // ErrNoUser indicates that a user does not exist.
  17. ErrNoUser = errors.New("user does not exist")
  18. // ErrNoEmail indicates that a user has not set an email address.
  19. ErrNoEmailAddress = errors.New("user has no email address")
  20. )
  21. // IdentScreenName struct stores the normalized version of a user's screen name.
  22. // This format is used for uniformity in storage and comparison by removing spaces
  23. // and converting all characters to lowercase.
  24. type IdentScreenName struct {
  25. // screenName contains the identifier screen name value. Do not assign this
  26. // value directly. Rather, set it through NewIdentScreenName. This ensures
  27. // that when an instance of IdentScreenName is present, it's guaranteed to
  28. // have a normalized value.
  29. screenName string
  30. }
  31. // String returns the string representation of the IdentScreenName.
  32. func (i IdentScreenName) String() string {
  33. return i.screenName
  34. }
  35. // UIN returns a numeric UIN representation of the IdentScreenName.
  36. func (i IdentScreenName) UIN() uint32 {
  37. v, _ := strconv.Atoi(i.screenName)
  38. return uint32(v)
  39. }
  40. // NewIdentScreenName creates a new IdentScreenName.
  41. func NewIdentScreenName(screenName string) IdentScreenName {
  42. str := strings.ReplaceAll(screenName, " ", "")
  43. str = strings.ToLower(str)
  44. return IdentScreenName{screenName: str}
  45. }
  46. // DisplayScreenName type represents the screen name in the user-defined format.
  47. // This includes the original casing and spacing as defined by the user.
  48. type DisplayScreenName string
  49. var (
  50. ErrAIMHandleInvalidFormat = errors.New("screen name must start with a letter, cannot end with a space, and must contain only letters, numbers, and spaces")
  51. ErrAIMHandleLength = errors.New("screen name must be between 3 and 16 characters")
  52. ErrPasswordInvalid = errors.New("invalid password length")
  53. ErrICQUINInvalidFormat = errors.New("uin must be a number in the range 10000-2147483646")
  54. )
  55. // ValidateAIMHandle returns an error if the instance is not a valid AIM screen name.
  56. // Possible errors:
  57. // - ErrAIMHandleLength: if the screen name has less than 3 non-space
  58. // characters or more than 16 characters (including spaces).
  59. // - ErrAIMHandleInvalidFormat: if the screen name does not start with a
  60. // letter, ends with a space, or contains invalid characters
  61. func (s DisplayScreenName) ValidateAIMHandle() error {
  62. // Must contain min 3 letters, max 16 letters and spaces.
  63. c := 0
  64. for _, r := range s {
  65. if unicode.IsLetter(r) {
  66. c++
  67. }
  68. if c == 3 {
  69. break
  70. }
  71. }
  72. if c < 3 || len(s) > 16 {
  73. return ErrAIMHandleLength
  74. }
  75. // Must start with a letter, cannot end with a space, and must contain only
  76. // letters, numbers, and spaces.
  77. if !unicode.IsLetter(rune(s[0])) || s[len(s)-1] == ' ' {
  78. return ErrAIMHandleInvalidFormat
  79. }
  80. for _, ch := range s {
  81. if !unicode.IsLetter(ch) && !unicode.IsDigit(ch) && ch != ' ' {
  82. return ErrAIMHandleInvalidFormat
  83. }
  84. }
  85. return nil
  86. }
  87. // IsUIN indicates whether the screen name is an ICQ UIN.
  88. func (s DisplayScreenName) IsUIN() bool {
  89. if len(s) == 0 {
  90. return false
  91. }
  92. for _, r := range s {
  93. if !unicode.IsDigit(r) {
  94. return false
  95. }
  96. }
  97. return true
  98. }
  99. // ValidateUIN returns an error if the instance is not a valid ICQ UIN.
  100. // Possible errors:
  101. // - ErrICQUINInvalidFormat: if the UIN is not a number or is not in the valid
  102. // range
  103. func (s DisplayScreenName) ValidateUIN() error {
  104. uin, err := strconv.Atoi(string(s))
  105. if err != nil || uin < 10000 || uin > 2147483646 {
  106. return ErrICQUINInvalidFormat
  107. }
  108. return nil
  109. }
  110. // IdentScreenName converts the DisplayScreenName to an IdentScreenName by applying
  111. // the normalization process defined in NewIdentScreenName.
  112. func (s DisplayScreenName) IdentScreenName() IdentScreenName {
  113. return NewIdentScreenName(string(s))
  114. }
  115. // String returns the original display string of the screen name, preserving the user-defined
  116. // casing and spaces.
  117. func (s DisplayScreenName) String() string {
  118. return string(s)
  119. }
  120. // NewStubUser creates a new user with canned credentials. The default password
  121. // is "welcome1". This is typically used for development purposes.
  122. func NewStubUser(screenName DisplayScreenName) (User, error) {
  123. uid, err := uuid.NewRandom()
  124. if err != nil {
  125. return User{}, err
  126. }
  127. u := User{
  128. IdentScreenName: NewIdentScreenName(string(screenName)),
  129. DisplayScreenName: screenName,
  130. AuthKey: uid.String(),
  131. IsICQ: screenName.IsUIN(),
  132. }
  133. err = u.HashPassword("welcome1")
  134. return u, err
  135. }
  136. // User represents a user account.
  137. type User struct {
  138. // IdentScreenName is the AIM screen name.
  139. IdentScreenName IdentScreenName
  140. // DisplayScreenName is the formatted screen name.
  141. DisplayScreenName DisplayScreenName
  142. // AuthKey is the salt for the MD5 password hash.
  143. AuthKey string
  144. // StrongMD5Pass is the MD5 password hash format used by AIM v4.8-v5.9.
  145. StrongMD5Pass []byte
  146. // WeakMD5Pass is the MD5 password hash format used by AIM v3.5-v4.7. This
  147. // hash is used to authenticate roasted passwords for AIM v1.0-v3.0.
  148. WeakMD5Pass []byte
  149. // IsICQ indicates whether the user is an ICQ account (true) or an AIM
  150. // account (false).
  151. IsICQ bool
  152. // ConfirmStatus indicates whether the user has confirmed their AIM account.
  153. ConfirmStatus bool
  154. // RegStatus is the AIM registration status.
  155. // 1: no disclosure
  156. // 2: limit disclosure
  157. // 3: full disclosure
  158. RegStatus int
  159. // EmailAddress is the email address set by the AIM client.
  160. EmailAddress string
  161. // ICQAffiliations holds information about the user's affiliations,
  162. // including past and current affiliations.
  163. ICQAffiliations ICQAffiliations
  164. // ICQInterests holds information about the user's interests, categorized
  165. // by code and associated keywords.
  166. ICQInterests ICQInterests
  167. // ICQMoreInfo contains additional information about the user.
  168. ICQMoreInfo ICQMoreInfo
  169. // ICQPermissions specifies the user's privacy settings.
  170. ICQPermissions ICQPermissions
  171. // ICQBasicInfo contains the user's basic profile information, including
  172. // contact details and personal identifiers.
  173. ICQBasicInfo ICQBasicInfo
  174. // ICQNotes allows the user to store personal notes or additional
  175. // information within their profile.
  176. ICQNotes ICQUserNotes
  177. // ICQWorkInfo contains the user's professional information, including
  178. // their workplace address and job-related details.
  179. ICQWorkInfo ICQWorkInfo
  180. AIMDirectoryInfo AIMNameAndAddr
  181. }
  182. // AIMNameAndAddr holds name and address AIM directory information.
  183. type AIMNameAndAddr struct {
  184. // FirstName is the user's first name.
  185. FirstName string
  186. // LastName is the user's last name.
  187. LastName string
  188. // MiddleName is the user's middle name.
  189. MiddleName string
  190. // MaidenName is the user's maiden name.
  191. MaidenName string
  192. // Country is the user's country of residence.
  193. Country string
  194. // State is the user's state or region of residence.
  195. State string
  196. // City is the user's city of residence.
  197. City string
  198. // NickName is the user's chosen nickname.
  199. NickName string
  200. // ZIPCode is the user's postal or ZIP code.
  201. ZIPCode string
  202. // Address is the user's street address.
  203. Address string
  204. }
  205. // ICQBasicInfo holds basic information about an ICQ user, including their name, contact details, and location.
  206. type ICQBasicInfo struct {
  207. // Address is the user's residential address.
  208. Address string
  209. // CellPhone is the user's mobile phone number.
  210. CellPhone string
  211. // City is the city where the user resides.
  212. City string
  213. // CountryCode is the code representing the user's country of residence.
  214. CountryCode uint16
  215. // EmailAddress is the user's primary email address.
  216. EmailAddress string
  217. // Fax is the user's fax number.
  218. Fax string
  219. // FirstName is the user's first name.
  220. FirstName string
  221. // GMTOffset is the user's time zone offset from GMT.
  222. GMTOffset uint8
  223. // LastName is the user's last name.
  224. LastName string
  225. // Nickname is the user's nickname or preferred name.
  226. Nickname string
  227. // Phone is the user's landline phone number.
  228. Phone string
  229. // PublishEmail indicates whether the user's email address is public.
  230. PublishEmail bool
  231. // State is the state or region where the user resides.
  232. State string
  233. // ZIPCode is the user's postal code.
  234. ZIPCode string
  235. }
  236. // ICQAffiliations contains information about the user's affiliations, both past and present.
  237. type ICQAffiliations struct {
  238. // PastCode1 is the code representing the user's first past affiliation.
  239. PastCode1 uint16
  240. // PastKeyword1 is the keyword associated with the user's first past affiliation.
  241. PastKeyword1 string
  242. // PastCode2 is the code representing the user's second past affiliation.
  243. PastCode2 uint16
  244. // PastKeyword2 is the keyword associated with the user's second past affiliation.
  245. PastKeyword2 string
  246. // PastCode3 is the code representing the user's third past affiliation.
  247. PastCode3 uint16
  248. // PastKeyword3 is the keyword associated with the user's third past affiliation.
  249. PastKeyword3 string
  250. // CurrentCode1 is the code representing the user's current first affiliation.
  251. CurrentCode1 uint16
  252. // CurrentKeyword1 is the keyword associated with the user's current first affiliation.
  253. CurrentKeyword1 string
  254. // CurrentCode2 is the code representing the user's current second affiliation.
  255. CurrentCode2 uint16
  256. // CurrentKeyword2 is the keyword associated with the user's current second affiliation.
  257. CurrentKeyword2 string
  258. // CurrentCode3 is the code representing the user's current third affiliation.
  259. CurrentCode3 uint16
  260. // CurrentKeyword3 is the keyword associated with the user's current third affiliation.
  261. CurrentKeyword3 string
  262. }
  263. // ICQInterests holds information about the user's interests, categorized by
  264. // interest code and associated keyword.
  265. type ICQInterests struct {
  266. // Code1 is the code representing the user's first interest.
  267. Code1 uint16
  268. // Keyword1 is the keyword associated with the user's first interest.
  269. Keyword1 string
  270. // Code2 is the code representing the user's second interest.
  271. Code2 uint16
  272. // Keyword2 is the keyword associated with the user's second interest.
  273. Keyword2 string
  274. // Code3 is the code representing the user's third interest.
  275. Code3 uint16
  276. // Keyword3 is the keyword associated with the user's third interest.
  277. Keyword3 string
  278. // Code4 is the code representing the user's fourth interest.
  279. Code4 uint16
  280. // Keyword4 is the keyword associated with the user's fourth interest.
  281. Keyword4 string
  282. }
  283. // ICQUserNotes contains personal notes or additional information added by the user.
  284. type ICQUserNotes struct {
  285. // Notes are the personal notes or additional information the user has
  286. // entered in their profile.
  287. Notes string
  288. }
  289. // ICQMoreInfo contains additional information about the user, such as
  290. // demographic and language preferences.
  291. type ICQMoreInfo struct {
  292. // Gender is the user's gender, represented by a code.
  293. Gender uint16
  294. // HomePageAddr is the URL of the user's personal homepage.
  295. HomePageAddr string
  296. // BirthYear is the user's birth year.
  297. BirthYear uint16
  298. // BirthMonth is the user's birth month.
  299. BirthMonth uint8
  300. // BirthDay is the user's birth day.
  301. BirthDay uint8
  302. // Lang1 is the code for the user's primary language.
  303. Lang1 uint8
  304. // Lang2 is the code for the user's secondary language.
  305. Lang2 uint8
  306. // Lang3 is the code for the user's tertiary language.
  307. Lang3 uint8
  308. }
  309. // ICQWorkInfo contains information about the user's professional life,
  310. // including their workplace and job title.
  311. type ICQWorkInfo struct {
  312. // Address is the address of the user's workplace.
  313. Address string
  314. // City is the city where the user's workplace is located.
  315. City string
  316. // Company is the name of the user's employer or company.
  317. Company string
  318. // CountryCode is the code representing the country where the user's
  319. // workplace is located.
  320. CountryCode uint16
  321. // Department is the name of the department within the user's company.
  322. Department string
  323. // Fax is the fax number for the user's workplace.
  324. Fax string
  325. // OccupationCode is the code representing the user's occupation.
  326. OccupationCode uint16
  327. // Phone is the phone number for the user's workplace.
  328. Phone string
  329. // Position is the user's job title or position within the company.
  330. Position string
  331. // State is the state or region where the user's workplace is located.
  332. State string
  333. // WebPage is the URL of the user's company's website.
  334. WebPage string
  335. // ZIPCode is the postal code for the user's workplace.
  336. ZIPCode string
  337. }
  338. // ICQPermissions specifies the privacy settings of an ICQ user.
  339. type ICQPermissions struct {
  340. // AuthRequired indicates where users must ask this permission to add them
  341. // to their contact list.
  342. AuthRequired bool
  343. }
  344. // Age returns the user's age relative to their birthday and timeNow.
  345. func (u *User) Age(timeNow func() time.Time) uint16 {
  346. now := timeNow().UTC()
  347. switch {
  348. case u.ICQMoreInfo.BirthYear > 0 && u.ICQMoreInfo.BirthDay == 0 && u.ICQMoreInfo.BirthMonth == 0:
  349. bday := time.Date(int(u.ICQMoreInfo.BirthYear), time.January, 1, 0, 0, 0, 0, time.UTC)
  350. return uint16(now.Year() - bday.Year())
  351. case u.ICQMoreInfo.BirthYear > 0 && u.ICQMoreInfo.BirthDay > 0 && u.ICQMoreInfo.BirthMonth > 0:
  352. bday := time.Date(int(u.ICQMoreInfo.BirthYear), time.Month(u.ICQMoreInfo.BirthMonth), int(u.ICQMoreInfo.BirthDay), 0, 0, 0, 0, time.UTC)
  353. years := now.Year() - bday.Year()
  354. if now.YearDay() < bday.YearDay() {
  355. years--
  356. }
  357. return uint16(years)
  358. default: // invalid date
  359. return 0
  360. }
  361. }
  362. // ValidateHash checks if md5Hash is identical to one of the password hashes.
  363. func (u *User) ValidateHash(md5Hash []byte) bool {
  364. return bytes.Equal(u.StrongMD5Pass, md5Hash) || bytes.Equal(u.WeakMD5Pass, md5Hash)
  365. }
  366. // ValidateRoastedPass checks if the provided roasted password matches the MD5
  367. // hash of the user's actual password. A roasted password is a XOR-obfuscated
  368. // form of the real password, intended to add a simple layer of security.
  369. func (u *User) ValidateRoastedPass(roastedPass []byte) bool {
  370. clearPass := wire.RoastPassword(roastedPass)
  371. md5Hash := wire.WeakMD5PasswordHash(string(clearPass), u.AuthKey) // todo remove string conversion
  372. return bytes.Equal(u.WeakMD5Pass, md5Hash)
  373. }
  374. // HashPassword computes MD5 hashes of the user's password. It computes both
  375. // weak and strong variants and stores them in the struct.
  376. func (u *User) HashPassword(passwd string) error {
  377. if u.IsICQ {
  378. if err := validateICQPassword(passwd); err != nil {
  379. return err
  380. }
  381. } else {
  382. if err := validateAIMPassword(passwd); err != nil {
  383. return err
  384. }
  385. }
  386. u.WeakMD5Pass = wire.WeakMD5PasswordHash(passwd, u.AuthKey)
  387. u.StrongMD5Pass = wire.StrongMD5PasswordHash(passwd, u.AuthKey)
  388. return nil
  389. }
  390. // validateAIMPassword returns an error if the AIM password is invalid.
  391. // A valid password is 4-16 characters long. The min and max password length
  392. // values reflect AOL's password validation rules circa 2000.
  393. func validateAIMPassword(pass string) error {
  394. if len(pass) < 4 || len(pass) > 16 {
  395. return fmt.Errorf("%w: password length must be between 4-16 characters", ErrPasswordInvalid)
  396. }
  397. return nil
  398. }
  399. // validateICQPassword returns an error if the ICQ password is invalid.
  400. // A valid password is 6-8 characters long. It's unclear what min length the
  401. // ICQ service required, so a plausible minimum value is set. The max length
  402. // reflects the password limitation imposed by old ICQ clients.
  403. func validateICQPassword(pass string) error {
  404. if len(pass) < 6 || len(pass) > 8 {
  405. return fmt.Errorf("%w: password must be between 6-8 characters", ErrPasswordInvalid)
  406. }
  407. return nil
  408. }
  409. type OfflineMessage struct {
  410. Sender IdentScreenName
  411. Recipient IdentScreenName
  412. Message wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  413. Sent time.Time
  414. }
  415. // Category represents an AIM directory category.
  416. type Category struct {
  417. // ID is the category ID
  418. ID uint8
  419. // Name is the category name
  420. Name string `oscar:"len_prefix=uint16"`
  421. }
  422. // Keyword represents an AIM directory keyword.
  423. type Keyword struct {
  424. // ID is the keyword ID
  425. ID uint8
  426. // Name is the keyword name
  427. Name string `oscar:"len_prefix=uint16"`
  428. }