user.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. // SuspendedStatus is the account suspended status
  160. SuspendedStatus uint16
  161. // EmailAddress is the email address set by the AIM client.
  162. EmailAddress string
  163. // ICQAffiliations holds information about the user's affiliations,
  164. // including past and current affiliations.
  165. ICQAffiliations ICQAffiliations
  166. // ICQInterests holds information about the user's interests, categorized
  167. // by code and associated keywords.
  168. ICQInterests ICQInterests
  169. // ICQMoreInfo contains additional information about the user.
  170. ICQMoreInfo ICQMoreInfo
  171. // ICQPermissions specifies the user's privacy settings.
  172. ICQPermissions ICQPermissions
  173. // ICQBasicInfo contains the user's basic profile information, including
  174. // contact details and personal identifiers.
  175. ICQBasicInfo ICQBasicInfo
  176. // ICQNotes allows the user to store personal notes or additional
  177. // information within their profile.
  178. ICQNotes ICQUserNotes
  179. // ICQWorkInfo contains the user's professional information, including
  180. // their workplace address and job-related details.
  181. ICQWorkInfo ICQWorkInfo
  182. AIMDirectoryInfo AIMNameAndAddr
  183. // TOCConfig is the user's saved server-side info (buddy list, etc) for
  184. // on the TOC service.
  185. TOCConfig string
  186. }
  187. // AIMNameAndAddr holds name and address AIM directory information.
  188. type AIMNameAndAddr struct {
  189. // FirstName is the user's first name.
  190. FirstName string
  191. // LastName is the user's last name.
  192. LastName string
  193. // MiddleName is the user's middle name.
  194. MiddleName string
  195. // MaidenName is the user's maiden name.
  196. MaidenName string
  197. // Country is the user's country of residence.
  198. Country string
  199. // State is the user's state or region of residence.
  200. State string
  201. // City is the user's city of residence.
  202. City string
  203. // NickName is the user's chosen nickname.
  204. NickName string
  205. // ZIPCode is the user's postal or ZIP code.
  206. ZIPCode string
  207. // Address is the user's street address.
  208. Address string
  209. }
  210. // ICQBasicInfo holds basic information about an ICQ user, including their name, contact details, and location.
  211. type ICQBasicInfo struct {
  212. // Address is the user's residential address.
  213. Address string
  214. // CellPhone is the user's mobile phone number.
  215. CellPhone string
  216. // City is the city where the user resides.
  217. City string
  218. // CountryCode is the code representing the user's country of residence.
  219. CountryCode uint16
  220. // EmailAddress is the user's primary email address.
  221. EmailAddress string
  222. // Fax is the user's fax number.
  223. Fax string
  224. // FirstName is the user's first name.
  225. FirstName string
  226. // GMTOffset is the user's time zone offset from GMT.
  227. GMTOffset uint8
  228. // LastName is the user's last name.
  229. LastName string
  230. // Nickname is the user's nickname or preferred name.
  231. Nickname string
  232. // Phone is the user's landline phone number.
  233. Phone string
  234. // PublishEmail indicates whether the user's email address is public.
  235. PublishEmail bool
  236. // State is the state or region where the user resides.
  237. State string
  238. // ZIPCode is the user's postal code.
  239. ZIPCode string
  240. }
  241. // ICQAffiliations contains information about the user's affiliations, both past and present.
  242. type ICQAffiliations struct {
  243. // PastCode1 is the code representing the user's first past affiliation.
  244. PastCode1 uint16
  245. // PastKeyword1 is the keyword associated with the user's first past affiliation.
  246. PastKeyword1 string
  247. // PastCode2 is the code representing the user's second past affiliation.
  248. PastCode2 uint16
  249. // PastKeyword2 is the keyword associated with the user's second past affiliation.
  250. PastKeyword2 string
  251. // PastCode3 is the code representing the user's third past affiliation.
  252. PastCode3 uint16
  253. // PastKeyword3 is the keyword associated with the user's third past affiliation.
  254. PastKeyword3 string
  255. // CurrentCode1 is the code representing the user's current first affiliation.
  256. CurrentCode1 uint16
  257. // CurrentKeyword1 is the keyword associated with the user's current first affiliation.
  258. CurrentKeyword1 string
  259. // CurrentCode2 is the code representing the user's current second affiliation.
  260. CurrentCode2 uint16
  261. // CurrentKeyword2 is the keyword associated with the user's current second affiliation.
  262. CurrentKeyword2 string
  263. // CurrentCode3 is the code representing the user's current third affiliation.
  264. CurrentCode3 uint16
  265. // CurrentKeyword3 is the keyword associated with the user's current third affiliation.
  266. CurrentKeyword3 string
  267. }
  268. // ICQInterests holds information about the user's interests, categorized by
  269. // interest code and associated keyword.
  270. type ICQInterests struct {
  271. // Code1 is the code representing the user's first interest.
  272. Code1 uint16
  273. // Keyword1 is the keyword associated with the user's first interest.
  274. Keyword1 string
  275. // Code2 is the code representing the user's second interest.
  276. Code2 uint16
  277. // Keyword2 is the keyword associated with the user's second interest.
  278. Keyword2 string
  279. // Code3 is the code representing the user's third interest.
  280. Code3 uint16
  281. // Keyword3 is the keyword associated with the user's third interest.
  282. Keyword3 string
  283. // Code4 is the code representing the user's fourth interest.
  284. Code4 uint16
  285. // Keyword4 is the keyword associated with the user's fourth interest.
  286. Keyword4 string
  287. }
  288. // ICQUserNotes contains personal notes or additional information added by the user.
  289. type ICQUserNotes struct {
  290. // Notes are the personal notes or additional information the user has
  291. // entered in their profile.
  292. Notes string
  293. }
  294. // ICQMoreInfo contains additional information about the user, such as
  295. // demographic and language preferences.
  296. type ICQMoreInfo struct {
  297. // Gender is the user's gender, represented by a code.
  298. Gender uint16
  299. // HomePageAddr is the URL of the user's personal homepage.
  300. HomePageAddr string
  301. // BirthYear is the user's birth year.
  302. BirthYear uint16
  303. // BirthMonth is the user's birth month.
  304. BirthMonth uint8
  305. // BirthDay is the user's birth day.
  306. BirthDay uint8
  307. // Lang1 is the code for the user's primary language.
  308. Lang1 uint8
  309. // Lang2 is the code for the user's secondary language.
  310. Lang2 uint8
  311. // Lang3 is the code for the user's tertiary language.
  312. Lang3 uint8
  313. }
  314. // ICQWorkInfo contains information about the user's professional life,
  315. // including their workplace and job title.
  316. type ICQWorkInfo struct {
  317. // Address is the address of the user's workplace.
  318. Address string
  319. // City is the city where the user's workplace is located.
  320. City string
  321. // Company is the name of the user's employer or company.
  322. Company string
  323. // CountryCode is the code representing the country where the user's
  324. // workplace is located.
  325. CountryCode uint16
  326. // Department is the name of the department within the user's company.
  327. Department string
  328. // Fax is the fax number for the user's workplace.
  329. Fax string
  330. // OccupationCode is the code representing the user's occupation.
  331. OccupationCode uint16
  332. // Phone is the phone number for the user's workplace.
  333. Phone string
  334. // Position is the user's job title or position within the company.
  335. Position string
  336. // State is the state or region where the user's workplace is located.
  337. State string
  338. // WebPage is the URL of the user's company's website.
  339. WebPage string
  340. // ZIPCode is the postal code for the user's workplace.
  341. ZIPCode string
  342. }
  343. // ICQPermissions specifies the privacy settings of an ICQ user.
  344. type ICQPermissions struct {
  345. // AuthRequired indicates where users must ask this permission to add them
  346. // to their contact list.
  347. AuthRequired bool
  348. }
  349. // Age returns the user's age relative to their birthday and timeNow.
  350. func (u *User) Age(timeNow func() time.Time) uint16 {
  351. now := timeNow().UTC()
  352. switch {
  353. case u.ICQMoreInfo.BirthYear > 0 && u.ICQMoreInfo.BirthDay == 0 && u.ICQMoreInfo.BirthMonth == 0:
  354. bday := time.Date(int(u.ICQMoreInfo.BirthYear), time.January, 1, 0, 0, 0, 0, time.UTC)
  355. return uint16(now.Year() - bday.Year())
  356. case u.ICQMoreInfo.BirthYear > 0 && u.ICQMoreInfo.BirthDay > 0 && u.ICQMoreInfo.BirthMonth > 0:
  357. bday := time.Date(int(u.ICQMoreInfo.BirthYear), time.Month(u.ICQMoreInfo.BirthMonth), int(u.ICQMoreInfo.BirthDay), 0, 0, 0, 0, time.UTC)
  358. years := now.Year() - bday.Year()
  359. if now.YearDay() < bday.YearDay() {
  360. years--
  361. }
  362. return uint16(years)
  363. default: // invalid date
  364. return 0
  365. }
  366. }
  367. // ValidateHash checks if md5Hash is identical to one of the password hashes.
  368. func (u *User) ValidateHash(md5Hash []byte) bool {
  369. return bytes.Equal(u.StrongMD5Pass, md5Hash) || bytes.Equal(u.WeakMD5Pass, md5Hash)
  370. }
  371. // ValidateRoastedPass checks if the provided roasted password matches the MD5
  372. // hash of the user's actual password. A roasted password is a XOR-obfuscated
  373. // form of the real password, intended to add a simple layer of security.
  374. func (u *User) ValidateRoastedPass(roastedPass []byte) bool {
  375. clearPass := wire.RoastOSCARPassword(roastedPass)
  376. md5Hash := wire.WeakMD5PasswordHash(string(clearPass), u.AuthKey) // todo remove string conversion
  377. return bytes.Equal(u.WeakMD5Pass, md5Hash)
  378. }
  379. // ValidateRoastedJavaPass checks if the provided roasted password matches the MD5
  380. // hash of the user's actual password. A roasted password is a XOR-obfuscated
  381. // form of the real password, intended to add a simple layer of security. // todo toc description
  382. func (u *User) ValidateRoastedJavaPass(roastedPass []byte) bool {
  383. clearPass := wire.RoastOSCARJavaPassword(roastedPass)
  384. md5Hash := wire.WeakMD5PasswordHash(string(clearPass), u.AuthKey) // todo remove string conversion
  385. return bytes.Equal(u.WeakMD5Pass, md5Hash)
  386. }
  387. // ValidateRoastedTOCPass checks if the provided roasted password matches the MD5
  388. // hash of the user's actual password. A roasted password is a XOR-obfuscated
  389. // form of the real password, intended to add a simple layer of security. // todo toc description
  390. func (u *User) ValidateRoastedTOCPass(roastedPass []byte) bool {
  391. clearPass := wire.RoastTOCPassword(roastedPass)
  392. md5Hash := wire.WeakMD5PasswordHash(string(clearPass), u.AuthKey) // todo remove string conversion
  393. return bytes.Equal(u.WeakMD5Pass, md5Hash)
  394. }
  395. // HashPassword computes MD5 hashes of the user's password. It computes both
  396. // weak and strong variants and stores them in the struct.
  397. func (u *User) HashPassword(passwd string) error {
  398. if u.IsICQ {
  399. if err := validateICQPassword(passwd); err != nil {
  400. return err
  401. }
  402. } else {
  403. if err := validateAIMPassword(passwd); err != nil {
  404. return err
  405. }
  406. }
  407. u.WeakMD5Pass = wire.WeakMD5PasswordHash(passwd, u.AuthKey)
  408. u.StrongMD5Pass = wire.StrongMD5PasswordHash(passwd, u.AuthKey)
  409. return nil
  410. }
  411. // validateAIMPassword returns an error if the AIM password is invalid.
  412. // A valid password is 4-16 characters long. The min and max password length
  413. // values reflect AOL's password validation rules circa 2000.
  414. func validateAIMPassword(pass string) error {
  415. if len(pass) < 4 || len(pass) > 16 {
  416. return fmt.Errorf("%w: password length must be between 4-16 characters", ErrPasswordInvalid)
  417. }
  418. return nil
  419. }
  420. // validateICQPassword returns an error if the ICQ password is invalid.
  421. // A valid password is 6-8 characters long. It's unclear what min length the
  422. // ICQ service required, so a plausible minimum value is set. The max length
  423. // reflects the password limitation imposed by old ICQ clients.
  424. func validateICQPassword(pass string) error {
  425. if len(pass) < 6 || len(pass) > 8 {
  426. return fmt.Errorf("%w: password must be between 6-8 characters", ErrPasswordInvalid)
  427. }
  428. return nil
  429. }
  430. type OfflineMessage struct {
  431. Sender IdentScreenName
  432. Recipient IdentScreenName
  433. Message wire.SNAC_0x04_0x06_ICBMChannelMsgToHost
  434. Sent time.Time
  435. }
  436. // Category represents an AIM directory category.
  437. type Category struct {
  438. // ID is the category ID
  439. ID uint8
  440. // Name is the category name
  441. Name string `oscar:"len_prefix=uint16"`
  442. }
  443. // Keyword represents an AIM directory keyword.
  444. type Keyword struct {
  445. // ID is the keyword ID
  446. ID uint8
  447. // Name is the keyword name
  448. Name string `oscar:"len_prefix=uint16"`
  449. }