ssid
[learning-git.git] / net-oscar / lib / oscar / types / tlv.rb
blob91620c25348172819bde775b520d7a1a6ae774e4
1 module OSCAR
2   module Types
3     module TLV
4       
5       ###################################################
6       # TLV Types
7       #
8       # used in authentication, login
9       Type_Screen_Name            = 0x0001;
10       Type_Roasted_Password       = 0x0002;
11       Type_Client_ID_String       = 0x0003;
12       Type_Error_URL              = 0x0004;
13       Type_Connection_Address     = 0x0005;
14       Type_Connection_Cookie      = 0x0006;
15       Type_Error_Code             = 0x0008;
16       Type_Message_of_the_Day     = 0x000B;
17       Type_Client_Country         = 0x000E;
18       Type_Client_Language        = 0x000F;
19       Type_Client_Distribution    = 0x0014;
20       Type_Client_ID              = 0x0016;
21       Type_Client_Major_Version   = 0x0017;
22       Type_Client_Minor_Version   = 0x0018;
23       Type_Client_Lesser_Version  = 0x0019;
24       Type_Client_Build_Version   = 0x001A;
25       
26       # used in client info, receiving messages
27       Type_User_Class             = 0x0001;
28       User_Class_Unconfirmed      = 0x0001;
29       User_Class_Administrator    = 0x0002;
30       User_Class_AOL_Staff        = 0x0004;
31       User_Class_Commercial       = 0x0008;
32       User_Class_Free             = 0x0010;
33       User_Class_Away             = 0x0020;
34       User_Class_ICQ              = 0x0040;
35       User_Class_Wireless         = 0x0080;
37       
38       Type_Signon_Time            = 0x0003;
39       Type_Automated_Response     = 0x0004;
40       Type_Member_Since           = 0x0005;
41       Type_User_Status            = 0x0006;
42       #top bytes - random flags
43       User_Status_Webaware        = 0x00010000;
44       User_Status_Show_IP         = 0x00020000;
45       User_Status_Birthday        = 0x00080000;
46       User_Status_Webfront        = 0x00200000;
47       User_Status_Direct_Connection_Disabled    = 0x01000000; #no DC
48       User_Status_Direct_Connection_Authorized  = 0x10000000; #DC after auth
49       User_Status_Direct_Connection_Contacts    = 0x20000000; #DC with buddies only
50       #bottom bytes - status
51       User_Status_Online          = 0x00000000;
52       User_Status_Away            = 0x00000001;
53       User_Status_Do_Not_Disturb  = 0x00000002;
54       User_Status_Not_Available   = 0x00000004;
55       User_Status_Occupied        = 0x00000010;
56       User_Status_Free_For_Chat   = 0x00000020;
57       User_Status_Invisible       = 0x00000100;
58       
59       Type_External_IP            = 0x000A;
60       
61       Type_Direct_Connection_Info = 0x000C;
62       Direct_Connection_Disabled      = 0x0000;
63       Direct_Connection_HTTPS_Proxy   = 0x0001;
64       Direct_Connection_SOCKS_Proxy   = 0x0002;
65       Direct_Connection_Normal        = 0x0004;
66       Direct_Connection_Web           = 0x0006; #= no direct connection
67       
68       Type_Client_Idle_Time         = 0x000F;
69       
70       # used in service parameters
71       Type_Profile_Max_Length       = 0x0001;
72       Type_Profile_Max_Capabilities = 0x0002;
73       Type_Client_Cabilities_List   = 0x0005;
74       
75       # used in buddy list parameters
76       Type_Buddylist_Max_Size       = 0x0001;
77       Type_Buddylist_Max_Watchers   = 0x0002;
78       
79       # used in privacy management parameters
80       Type_Visible_List_Max_Size    = 0x0001;
81       Type_Invisible_List_Max_Size  = 0x0002;
82       
83       # used for sending, receiving messages
84       Type_Message_Data             = 0x0002;
85       Type_Server_Ack_Request       = 0x0003;
86       Type_Store_If_Offline         = 0x0006;
87       
88       ###################################################
89       # TLV Error Codes
90       #
91       Error_Messages = {
92         0x01 => "Invalid SNAC header.",
93         0x02 => "Server rate limit exceeded.",
94         0x03 => "Client rate limit exceeded.",
95         0x04 => "Recipient is not logged in.",
96         0x05 => "Requested service unavailable.",
97         0x06 => "Requested service not defined.",
98         0x07 => "You sent obsolete SNAC.",
99         0x08 => "Not supported by server.",
100         0x09 => "Not supported by client.",
101         0x0A => "Refused by client.",
102         0x0B => "Reply too big.",
103         0x0C => "Responses lost.",
104         0x0D => "Request denied.",
105         0x0E => "Incorrect SNAC format.",
106         0x0F => "Insufficient rights.",
107         0x10 => "In local permit/deny (recipient blocked).",
108         0x11 => "Sender too evil.",
109         0x12 => "Receiver too evil.",
110         0x13 => "User temporarily unavailable.",
111         0x14 => "No match.",
112         0x15 => "List overflow.",
113         0x16 => "Request ambiguous.",
114         0x17 => "Server queue full.",
115         0x18 => "Not while on AOL."
116       }
117       
118       ###################################################
119       # TLV Constructors
120       #
121       def construct_tlv_raw(type, data)
122         tlv = 
123         [
124           type,
125           data.length,
126         ].pack("nn") + data;
127         
128         Client.logger.info("TLV::construct_tlv_raw"){ "Built TLV of length #{tlv.length}, as hex: " + tlv.unpack("H*").join() + "." };
129         
130         return tlv;
131       end
132       
133       def construct_tlv_empty(type)
134         return construct_tlv_raw(type, "");
135       end
137       def construct_tlv_short(type, data)
138         return construct_tlv_raw(type, [data].pack("n"));
139       end
141       def construct_tlv_int(type, data)
142         return construct_tlv_raw(type, [data].pack("N"));
143       end
145       def construct_tlv_string(type, data)
146         return construct_tlv_raw(type, data);
147       end
148       
149       ###################################################
150       # TLV Parsers
151       #
152       def parse_tlv_raw(data)
153         if(data.length < 4)
154           Client.logger.error("TLV::parse_tlv_raw"){ "Data isn't a proper TLV." };
155           raise OscarError, "Communication error."
156         end
157         type, length = data.unpack("nn");
158         
159         data.slice!(0..3);
160         if(!data || (data.length < length))
161           Client.logger.error("TLV::parse_tlv_raw"){ "Expected length #{length} is greater than data length #{data.length}." };
162           raise OscarError, "Communication error."
163         end
164         curdata = data.slice!(0..(length-1));
165         
166         Client.logger.info("TLV::parse_tlv_raw"){ "Parsing TLV: #{sprintf('0x%04x', type)}, #{sprintf('0x%04x', length)}." };
167         if(length <= 16)
168           Client.logger.info("TLV::parse_tlv_raw"){ "or as hex: " + curdata.unpack("H*").join() + "."};
169         end
170         
171         return type, curdata, data;
172       end
173       
174       def parse_tlv_short(data)
175         type, val, consumed = parse_tlv_raw(data);
176         return type, val.unpack("n").first(), consumed;
177       end
179       def parse_tlv_string(data)
180         type, str, consumed = parse_tlv_raw(data);
181         return type, str, consumed;
182       end
183     end
184   end