Exemplo n.º 1
0
		/// <summary>
		/// Returns a user for the given values, either from the db,
		/// or by creating a new one.
		/// </summary>
		/// <param name="accountId"></param>
		/// <param name="characterEntityId"></param>
		/// <param name="characterName"></param>
		/// <param name="server"></param>
		/// <param name="channelName"></param>
		/// <returns></returns>
		public User GetOrCreateContact(string accountId, long characterEntityId, string characterName, string server, string channelName)
		{
			using (var conn = this.Connection)
			{
				var user = new User();
				user.AccountId = accountId;
				user.CharacterId = characterEntityId;
				user.Name = characterName;
				user.Server = server;
				user.ChannelName = channelName;
				user.Status = ContactStatus.Online;
				user.ChatOptions = ChatOptions.NotifyOnFriendLogIn;
				user.LastLogin = DateTime.Now;

				// Try to get contact from db
				using (var mc = new MySqlCommand("SELECT * FROM `contacts` WHERE `characterEntityId` = @characterEntityId", conn))
				{
					mc.Parameters.AddWithValue("@characterEntityId", characterEntityId);

					using (var reader = mc.ExecuteReader())
					{
						if (reader.Read())
						{
							user.Id = reader.GetInt32("contactId");
							user.Status = (ContactStatus)reader.GetByte("status");
							user.ChatOptions = (ChatOptions)reader.GetUInt32("chatOptions");
							user.Nickname = reader.GetStringSafe("nickname") ?? "";
							user.LastLogin = reader.GetDateTimeSafe("lastLogin");

							if (!Enum.IsDefined(typeof(ContactStatus), user.Status) || user.Status == ContactStatus.None)
								user.Status = ContactStatus.Online;

							this.UpdateLastLogin(user);

							return user;
						}
					}
				}

				// Create new contact
				using (var cmd = new InsertCommand("INSERT INTO `contacts` {0}", conn))
				{
					cmd.Set("accountId", accountId);
					cmd.Set("characterEntityId", characterEntityId);
					cmd.Set("characterName", characterName);
					cmd.Set("server", server);
					cmd.Set("status", (byte)user.Status);
					cmd.Set("chatOptions", (uint)user.ChatOptions);
					cmd.Set("nickname", "");
					cmd.Set("lastLogin", user.LastLogin);

					cmd.Execute();

					user.Id = (int)cmd.LastId;

					return user;
				}
			}
		}
Exemplo n.º 2
0
        /// <summary>
        /// Changes group the friend is in in the database.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="friendContactId"></param>
        /// <param name="groupId"></param>
        public void ChangeGroup(User user, int friendContactId, int groupId)
        {
            using (var conn = this.Connection)
            using (var cmd = new UpdateCommand("UPDATE `friends` SET {0} WHERE `userId1` = @userId1 AND `userId2` = @userId2", conn))
            {
                cmd.Set("groupId", groupId);
                cmd.AddParameter("@userId1", user.Id);
                cmd.AddParameter("@userId2", friendContactId);

                cmd.Execute();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Adds group to database.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="groupId"></param>
        /// <param name="groupName"></param>
        public void AddGroup(User user, Group group)
        {
            using (var conn = this.Connection)
            using (var cmd = new InsertCommand("INSERT INTO `groups` {0}", conn))
            {
                cmd.Set("groupId", group.Id);
                cmd.Set("contactId", user.Id);
                cmd.Set("name", group.Name);

                cmd.Execute();
            }
        }
Exemplo n.º 4
0
		/// <summary>
		/// Adds user to chat session and notifies clients.
		/// </summary>
		/// <param name="user"></param>
		public void Join(User user)
		{
			lock (_sync)
			{
				_users[user.Id] = user;
				_implicitUsers.Add(user.Id);
			}

			// Update clients if this wasn't the initiating user.
			if (this.Count > 1)
			{
				// Notify users about new user
				Send.ChatInviteR(this, user);

				// Send chat information to new user
				Send.ChatJoin(user, this);
			}
		}
Exemplo n.º 5
0
		/// <summary>
		/// Returns all notes for user.
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public List<Note> GetNotes(User user)
		{
			var result = new List<Note>();

			using (var conn = this.Connection)
			using (var mc = new MySqlCommand("SELECT * FROM `notes` WHERE `receiver` = @receiver", conn))
			{
				mc.Parameters.AddWithValue("@receiver", user.FullName);

				using (var reader = mc.ExecuteReader())
				{
					while (reader.Read())
					{
						var note = this.ReadNote(reader);
						if (note == null)
							continue;

						result.Add(note);
					}
				}
			}

			return result;
		}
Exemplo n.º 6
0
		/// <summary>
		/// Sends initial chat session information to user.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="session"></param>
		public static void ChatJoin(User user, ChatSession session)
		{
			var users = session.GetUsers();

			var packet = new Packet(Op.Msgr.ChatJoin, 0);

			packet.PutLong(session.Id);
			packet.PutInt(users.Length);
			foreach (var sessionUser in users)
			{
				packet.PutInt(sessionUser.Id);
				packet.PutString(sessionUser.FullName);
			}

			user.Client.Send(packet);
		}
Exemplo n.º 7
0
		/// <summary>
		/// Adds user to implicit user list, to be notified once a msg gets in.
		/// </summary>
		/// <param name="user"></param>
		public void Add(User user)
		{
			lock (_sync)
				_implicitUsers.Add(user.Id);
		}
Exemplo n.º 8
0
		/// <summary>
		/// Deletes group from database and moves friends in that group to ETC.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="groupId"></param>
		public void DeleteGroup(User user, int groupId)
		{
			using (var conn = this.Connection)
			{
				// Move friends
				using (var cmd = new UpdateCommand("UPDATE `friends` SET {0} WHERE `userId1` = @userId1 AND `groupId` = @oldGroupId", conn))
				{
					cmd.Set("groupId", -1);
					cmd.AddParameter("@userId1", user.Id);
					cmd.AddParameter("@oldGroupId", groupId);

					cmd.Execute();
				}

				// Delete group
				using (var mc = new MySqlCommand("DELETE FROM `groups` WHERE `contactId` = @contactId AND `groupId` = @groupId", conn))
				{
					mc.Parameters.AddWithValue("@contactId", user.Id);
					mc.Parameters.AddWithValue("@groupId", groupId);
					mc.ExecuteNonQuery();
				}
			}
		}
Exemplo n.º 9
0
		/// <summary>
		/// Returns list of all groups in user's friend list.
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public ICollection<Group> GetGroups(User user)
		{
			var result = new Dictionary<int, Group>();

			using (var conn = this.Connection)
			using (var mc = new MySqlCommand("SELECT * FROM `groups` WHERE `contactId` = @contactId", conn))
			{
				mc.Parameters.AddWithValue("@contactId", user.Id);

				using (var reader = mc.ExecuteReader())
				{
					while (reader.Read())
					{
						var group = new Group();
						group.Id = reader.GetInt32("groupId");
						group.Name = reader.GetStringSafe("name");

						// Override duplicate ids
						result[group.Id] = group;
					}
				}
			}

			return result.Values;
		}
Exemplo n.º 10
0
		/// <summary>
		/// Removes user from manager.
		/// </summary>
		/// <param name="user"></param>
		public void Remove(User user)
		{
			lock (_users)
				_users.Remove(user.Id);
		}
Exemplo n.º 11
0
		/// <summary>
		/// Updates user's status and nickname for all friends.
		/// </summary>
		/// <param name="friends"></param>
		/// <param name="user"></param>
		public static void FriendOptionChanged(List<User> friends, User user)
		{
			var packet = new Packet(Op.Msgr.FriendOptionChanged, 0);
			packet.PutInt(user.Id);
			packet.PutString(user.Nickname);
			packet.PutByte((byte)user.Status);

			foreach (var friendUser in friends)
				friendUser.Client.Send(packet);
		}
Exemplo n.º 12
0
		/// <summary>
		/// Returns list of all groups in user's friend list.
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public List<Group> GetGroups(User user)
		{
			var result = new List<Group>();

			using (var conn = this.Connection)
			using (var mc = new MySqlCommand("SELECT * FROM `groups` WHERE `contactId` = @contactId", conn))
			{
				mc.Parameters.AddWithValue("@contactId", user.Id);

				using (var reader = mc.ExecuteReader())
				{
					while (reader.Read())
					{
						var group = new Group();
						group.Id = reader.GetInt32("groupId");
						group.Name = reader.GetStringSafe("name");

						result.Add(group);
					}
				}
			}

			return result;
		}
Exemplo n.º 13
0
		/// <summary>
		/// Sends GuildChatMsg from sender to user's client.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="sender"></param>
		/// <param name="msg"></param>
		/// <returns></returns>
		public static void GuildChatMsg(User user, string sender, string msg)
		{
			var packet = new Packet(Op.Msgr.GuildChatMsg, 0);
			packet.PutString(sender);
			packet.PutString(msg);

			user.Client.Send(packet);
		}
Exemplo n.º 14
0
		/// <summary>
		/// Sends GuildMemberState to client.
		/// </summary>
		/// <param name="client"></param>
		/// <param name="guild"></param>
		/// <param name="member"></param>
		/// <param name="user"></param>
		/// <param name="status"></param>
		public static void GuildMemberState(MsgrClient client, Guild guild, GuildMember member, User user, ContactStatus status)
		{
			var packet = new Packet(Op.Msgr.GuildMemberState, 0);
			packet.PutByte(true);
			packet.PutInt(user.Id);
			packet.PutByte((byte)(status == ContactStatus.Online ? ContactStatus.Online : ContactStatus.Offline));
			packet.PutString(user.ChannelName);
			packet.PutLong(user.LastLogin.Ticks);
			packet.PutInt((int)member.Rank);
			packet.PutString(user.FullName);

			client.Send(packet);
		}
Exemplo n.º 15
0
		/// <summary>
		/// Removes user from chat session.
		/// </summary>
		/// <param name="user"></param>
		public void Leave(User user)
		{
			lock (_sync)
			{
				_users.Remove(user.Id);

				// Remove user from implicit list if more than one user is left
				// in chat, so the user leaves group chats for good.
				if (_users.Count > 1)
				{
					_implicitUsers.Remove(user.Id);
				}
				// Clear user lists if no active user is left.
				else if (_users.Count == 0)
				{
					_users.Clear();
					_implicitUsers.Clear();
				}
			}

			Send.ChatLeave(this, user);
		}
Exemplo n.º 16
0
		/// <summary>
		/// Notifies session user about user closing chat window.
		/// </summary>
		/// <param name="session"></param>
		/// <param name="user"></param>
		public static void ChatLeave(ChatSession session, User user)
		{
			var packet = new Packet(Op.Msgr.ChatLeave, 0);

			packet.PutLong(session.Id);
			packet.PutInt(user.Id);

			session.Broadcast(packet);
		}
Exemplo n.º 17
0
		/// <summary>
		/// Response to FriendUnblock request.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="success"></param>
		/// <param name="friendId">Only required on success.</param>
		public static void FriendUnblockR(User user, bool success, int friendId)
		{
			var packet = new Packet(Op.Msgr.FriendUnblockR, 0);

			packet.PutByte(success);
			if (success)
				packet.PutInt(friendId);

			user.Client.Send(packet);
		}
Exemplo n.º 18
0
		/// <summary>
		/// Sends live invitation from friend to user.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="friend"></param>
		public static void FriendConfirm(User user, User friend)
		{
			var packet = new Packet(Op.Msgr.FriendConfirm, 0);

			packet.PutInt(friend.Id);
			packet.PutString(friend.Name);
			packet.PutString(friend.Server);
			packet.PutString(friend.FullName);

			user.Client.Send(packet);
		}
Exemplo n.º 19
0
		/// <summary>
		/// Adds user to manager.
		/// </summary>
		/// <param name="user"></param>
		public void Add(User user)
		{
			lock (_users)
				_users[user.Id] = user;
		}
Exemplo n.º 20
0
		/// <summary>
		/// Notifies users about friend being online.
		/// </summary>
		/// <param name="user"></param>
		public static void FriendOnline(List<User> users, User friend)
		{
			var packet = new Packet(Op.Msgr.FriendOnline, 0);

			packet.PutInt(friend.Id);
			packet.PutString(friend.Nickname);
			packet.PutByte((byte)friend.Status);
			packet.PutString(friend.ChannelName);

			foreach (var user in users)
				user.Client.Send(packet);
		}
Exemplo n.º 21
0
		/// <summary>
		/// Returns contact with the given character id from the database
		/// or null if it wasn't found.
		/// </summary>
		/// <param name="characterEntityId"></param>
		/// <returns></returns>
		public User GetUserByCharacterId(long characterEntityId)
		{
			using (var conn = this.Connection)
			using (var mc = new MySqlCommand("SELECT * FROM `contacts` WHERE `characterEntityId` = @characterEntityId", conn))
			{
				mc.Parameters.AddWithValue("@characterEntityId", characterEntityId);

				using (var reader = mc.ExecuteReader())
				{
					if (!reader.Read())
						return null;

					var user = new User();
					user.Id = reader.GetInt32("contactId");
					user.AccountId = reader.GetString("accountId");
					user.CharacterId = characterEntityId;
					user.Name = reader.GetString("characterName");
					user.Server = reader.GetString("server");
					user.Status = (ContactStatus)reader.GetByte("status");
					user.ChatOptions = (ChatOptions)reader.GetUInt32("chatOptions");
					user.Nickname = reader.GetStringSafe("nickname") ?? "";
					user.LastLogin = reader.GetDateTimeSafe("lastLogin");

					if (!Enum.IsDefined(typeof(ContactStatus), user.Status) || user.Status == ContactStatus.None)
						user.Status = ContactStatus.Online;

					return user;
				}
			}
		}
Exemplo n.º 22
0
		/// <summary>
		/// Notifies user about friend being offline.
		/// </summary>
		/// <param name="user"></param>
		public static void FriendOffline(User user, User friend)
		{
			var packet = new Packet(Op.Msgr.FriendOffline, 0);
			packet.PutInt(friend.Id);
			user.Client.Send(packet);
		}
Exemplo n.º 23
0
		/// <summary>
		/// Saves user's options to database.
		/// </summary>
		/// <param name="user"></param>
		public void SaveOptions(User user)
		{
			using (var conn = this.Connection)
			using (var cmd = new UpdateCommand("UPDATE `contacts` SET {0} WHERE `contactId` = @contactId", conn))
			{
				cmd.Set("status", (byte)user.Status);
				cmd.Set("chatOptions", (uint)user.ChatOptions);
				cmd.Set("nickname", user.Nickname ?? "");
				cmd.AddParameter("@contactId", user.Id);

				cmd.Execute();
			}
		}
Exemplo n.º 24
0
		/// <summary>
		/// Notifies users about friend being offline.
		/// </summary>
		/// <param name="user"></param>
		public static void FriendOffline(List<User> users, User friend)
		{
			var packet = new Packet(Op.Msgr.FriendOffline, 0);
			packet.PutInt(friend.Id);

			foreach (var user in users)
				user.Client.Send(packet);
		}
Exemplo n.º 25
0
		/// <summary>
		/// Renames group in database.
		/// </summary>
		/// <param name="user"></param>
		/// <param name="groupId"></param>
		/// <param name="groupName"></param>
		public void RenameGroup(User user, int groupId, string groupName)
		{
			using (var conn = this.Connection)
			using (var cmd = new UpdateCommand("UPDATE `groups` SET {0} WHERE `groupId` = @groupId AND `contactId` = @contactId", conn))
			{
				cmd.Set("name", groupName);
				cmd.AddParameter("@groupId", groupId);
				cmd.AddParameter("@contactId", user.Id);

				cmd.Execute();
			}
		}
Exemplo n.º 26
0
		/// <summary>
		/// Notifies users about friend being offline.
		/// </summary>
		/// <param name="user"></param>
		public static void ChatBeginR(User user, long sessionId, int friendId)
		{
			var packet = new Packet(Op.Msgr.ChatBeginR, 0);

			packet.PutLong(sessionId);
			packet.PutInt(friendId);

			user.Client.Send(packet);
		}
Exemplo n.º 27
0
		/// <summary>
		/// Returns list of friends for user.
		/// </summary>
		/// <param name="user"></param>
		/// <returns></returns>
		public List<Friend> GetFriends(User user)
		{
			var result = new List<Friend>();

			using (var conn = this.Connection)
			using (var mc = new MySqlCommand(
				"SELECT f.userId2 AS friendId, c.characterName AS friendName, c.server AS friendServer, f.groupId AS groupId, f.status AS status " +
				"FROM `friends` AS f " +
				"INNER JOIN `contacts` AS c ON `f`.`userId2` = `c`.`contactId` " +
				"WHERE `f`.`userId1` = @userId", conn))
			{
				mc.Parameters.AddWithValue("@userId", user.Id);

				using (var reader = mc.ExecuteReader())
				{
					while (reader.Read())
					{
						var friend = new Friend();
						friend.Id = reader.GetInt32("friendId");
						friend.Name = reader.GetStringSafe("friendName");
						friend.Server = reader.GetStringSafe("friendServer");
						friend.GroupId = reader.GetInt32("groupId");
						friend.FriendshipStatus = (FriendshipStatus)reader.GetByte("status");

						result.Add(friend);
					}
				}
			}

			return result;
		}
Exemplo n.º 28
0
		/// <summary>
		/// Notifies users about someone joining the chat.
		/// </summary>
		/// <param name="session"></param>
		/// <param name="user"></param>
		public static void ChatInviteR(ChatSession session, User user)
		{
			var packet = new Packet(Op.Msgr.ChatInviteR, 0);

			packet.PutLong(session.Id);
			packet.PutInt(user.Id);
			packet.PutString(user.FullName);
			packet.PutString(user.Nickname);

			session.Broadcast(packet);
		}