summaryrefslogtreecommitdiff
path: root/android/VulpIRC/src/main/java/net/brokenfox/vulpirc/ChannelData.java
blob: d98b261040d5a7e80d8a4dd967328350424056e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package net.brokenfox.vulpirc;

import android.support.v4.app.Fragment;

import java.nio.ByteBuffer;
import java.util.HashMap;

/**
 * Created by ninji on 2/6/14.
 */
public class ChannelData extends WindowData {
	public static class UserData {
		public int id;
		public int modes;
		public char prefix;
	}
	public HashMap<String, UserData> users = new HashMap<String, UserData>();
	public String topic;

	public interface ChannelListener extends WindowListener {
		void handleUsersChanged();
	}

	private int mNextUserID = 1;


	@Override
	protected Fragment instantiateFragmentClass() {
		return new ChannelFragment();
	}

	@Override
	public void processInitialSync(ByteBuffer p) {
		super.processInitialSync(p);

		processAddUsers(p);
		processChangeTopic(p);
	}

	public void processChannelPacket(int type, ByteBuffer p) {
		switch (type) {
			case 0x120: processAddUsers(p); break;
			case 0x121: processRemoveUsers(p); break;
			case 0x122: processRenameUser(p); break;
			case 0x123: processChangeUserMode(p); break;
			case 0x124: processChangeTopic(p); break;
		}
	}

	private void processAddUsers(ByteBuffer p) {
		int count = p.getInt();

		for (int i = 0; i < count; i++) {
			String nick = Util.readStringFromBuffer(p);

			UserData d = new UserData();
			d.id = ++mNextUserID;
			d.modes = p.getInt();
			d.prefix = (char)p.get();

			users.put(nick, d);
		}

		if (count > 0)
			for (WindowListener l : mListeners)
				((ChannelListener)l).handleUsersChanged();
	}

	private void processRemoveUsers(ByteBuffer p) {
		int count = p.getInt();

		if (count == 0) {
			users.clear();
		} else {
			for (int i = 0; i < count; i++) {
				String nick = Util.readStringFromBuffer(p);
				users.remove(nick);
			}
		}

		for (WindowListener l : mListeners)
			((ChannelListener)l).handleUsersChanged();
	}

	private void processRenameUser(ByteBuffer p) {
		String fromNick = Util.readStringFromBuffer(p);
		String toNick = Util.readStringFromBuffer(p);

		if (users.containsKey(fromNick)) {
			users.put(toNick, users.get(fromNick));
			users.remove(fromNick);

			for (WindowListener l : mListeners)
				((ChannelListener)l).handleUsersChanged();
		}
	}

	private void processChangeUserMode(ByteBuffer p) {
		String nick = Util.readStringFromBuffer(p);
		int modes = p.getInt();
		char prefix = (char)p.get();

		if (users.containsKey(nick)) {
			UserData d = users.get(nick);
			d.modes = modes;
			d.prefix = prefix;

			for (WindowListener l : mListeners)
				((ChannelListener)l).handleUsersChanged();
		}
	}

	private void processChangeTopic(ByteBuffer p) {
		topic = Util.readStringFromBuffer(p);
	}
}