4
0

user.go 18 KB

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