Set the svn:eol-style property on all text files to "native", so that
[obnox/wireshark/wip.git] / packet-aim-chat.c
1 /* packet-aim-chat.c
2  * Routines for AIM Instant Messenger (OSCAR) dissection, SNAC Chat
3  * Copyright 2004, Jelmer Vernooij <jelmer@samba.org>
4  * Copyright 2000, Ralf Hoelzer <ralf@well.com>
5  *
6  * $Id$
7  *
8  * Ethereal - Network traffic analyzer
9  * By Gerald Combs <gerald@ethereal.com>
10  * Copyright 1998 Gerald Combs
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include <glib.h>
37
38 #include <epan/packet.h>
39 #include <epan/strutil.h>
40
41 #include "packet-tcp.h"
42 #include "packet-aim.h"
43 #include "prefs.h"
44
45 /* SNAC families */
46 #define FAMILY_CHAT       0x000E
47
48 /* Family Chat */
49 #define FAMILY_CHAT_ERROR             0x0001
50 #define FAMILY_CHAT_ROOMINFOUPDATE    0x0002
51 #define FAMILY_CHAT_USERJOIN          0x0003
52 #define FAMILY_CHAT_USERLEAVE         0x0004
53 #define FAMILY_CHAT_OUTGOINGMSG       0x0005
54 #define FAMILY_CHAT_INCOMINGMSG       0x0006
55 #define FAMILY_CHAT_EVIL_REQ          0x0007
56 #define FAMILY_CHAT_EVIL_REPLY        0x0008
57 #define FAMILY_CHAT_DEFAULT           0xffff
58
59 static const value_string aim_fnac_family_chat[] = {
60   { FAMILY_CHAT_ERROR, "Error" },
61   { FAMILY_CHAT_ROOMINFOUPDATE, "Room Info Update" },
62   { FAMILY_CHAT_USERJOIN, "User Join" },
63   { FAMILY_CHAT_USERLEAVE, "User Leave" },
64   { FAMILY_CHAT_OUTGOINGMSG, "Outgoing Message" },
65   { FAMILY_CHAT_INCOMINGMSG, "Incoming Message" },
66   { FAMILY_CHAT_EVIL_REQ, "Evil Request" },
67   { FAMILY_CHAT_EVIL_REPLY, "Evil Reply" },
68   { FAMILY_CHAT_DEFAULT, "Chat Default" },
69   { 0, NULL }
70 };
71
72 #define AIM_CHAT_TLV_BROWSABLE_TREE             0x001
73 #define AIM_CHAT_TLV_CLASS_EXCLUSIVE            0x002
74 #define AIM_CHAT_TLV_MAX_CONCURRENT_ROOMS       0x003
75 #define AIM_CHAT_TLV_MAX_ROOM_NAME_LEN          0x004
76 #define AIM_CHAT_TLV_ROOT_ROOMS                         0x005
77 #define AIM_CHAT_TLV_SEARCH_TAGS                        0x006
78 #define AIM_CHAT_TLV_CHILD_ROOMS                        0x065
79 #define AIM_CHAT_TLV_CONTAINS_USER_CLASS        0x066
80 #define AIM_CHAT_TLV_CONTAINS_USER_ARRAY        0x067
81
82 static const aim_tlv chat_tlvs[] = {
83         { AIM_CHAT_TLV_BROWSABLE_TREE, "Browsable tree", dissect_aim_tlv_value_bytes },
84         { AIM_CHAT_TLV_CLASS_EXCLUSIVE, "Exclusively for class", dissect_aim_tlv_value_userclass },
85         { AIM_CHAT_TLV_MAX_CONCURRENT_ROOMS, "Max. number of concurrent rooms", dissect_aim_tlv_value_uint8 },
86         { AIM_CHAT_TLV_MAX_ROOM_NAME_LEN, "Max. length of room name", dissect_aim_tlv_value_uint8 },
87         { AIM_CHAT_TLV_ROOT_ROOMS, "Root Rooms", dissect_aim_tlv_value_bytes },
88         { AIM_CHAT_TLV_SEARCH_TAGS, "Search Tags", dissect_aim_tlv_value_bytes },
89         { AIM_CHAT_TLV_CHILD_ROOMS, "Child Rooms", dissect_aim_tlv_value_bytes },
90         { AIM_CHAT_TLV_CONTAINS_USER_CLASS, "Contains User Class", dissect_aim_tlv_value_bytes },
91         { AIM_CHAT_TLV_CONTAINS_USER_ARRAY, "Contains User Array", dissect_aim_tlv_value_bytes },
92         { 0, NULL, NULL }
93 };
94
95 /* Initialize the protocol and registered fields */
96 static int proto_aim_chat = -1;
97
98 /* Initialize the subtree pointers */
99 static gint ett_aim_chat          = -1;
100
101 static int dissect_aim_snac_chat(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
102 {
103   guint8 buddyname_length = 0;
104   int offset = 0;
105   struct aiminfo *aiminfo = pinfo->private_data;
106   char buddyname[MAX_BUDDYNAME_LENGTH + 1];
107   guchar msg[1000];
108   proto_item *ti;
109   proto_tree *chat_tree = NULL;
110                                                                                                                               
111   if(tree) {
112       ti = proto_tree_add_text(tree, tvb, 0, -1, "Chat Service");
113       chat_tree = proto_item_add_subtree(ti, ett_aim_chat);
114   }
115
116   switch(aiminfo->subtype)
117     {
118     case FAMILY_CHAT_ERROR:
119       return dissect_aim_snac_error(tvb, pinfo, offset, chat_tree);
120     case FAMILY_CHAT_USERLEAVE:
121     case FAMILY_CHAT_USERJOIN:
122       while(tvb_length_remaining(tvb, offset) > 0) {
123         offset = dissect_aim_userinfo(tvb, pinfo, offset, chat_tree);
124       }
125       return offset;
126     case FAMILY_CHAT_EVIL_REQ:
127     case FAMILY_CHAT_EVIL_REPLY:
128     case FAMILY_CHAT_ROOMINFOUPDATE:
129       /* FIXME */
130       return 0;
131     case FAMILY_CHAT_OUTGOINGMSG:
132       /* channel message from client */
133       aim_get_message( msg, tvb, 40 + buddyname_length, tvb_length(tvb) 
134            - 40 - buddyname_length );
135       
136       if (check_col(pinfo->cinfo, COL_INFO)) 
137         col_append_fstr(pinfo->cinfo, COL_INFO, " -> %s", msg);
138       return tvb_length(tvb);
139       
140     case FAMILY_CHAT_INCOMINGMSG:
141       /* channel message to client */
142       buddyname_length = aim_get_buddyname( buddyname, tvb, 30, 31 );
143       aim_get_message( msg, tvb, 36 + buddyname_length, tvb_length(tvb) 
144            - 36 - buddyname_length );
145       
146       if (check_col(pinfo->cinfo, COL_INFO)) {
147         col_append_fstr(pinfo->cinfo, COL_INFO, "from: %s", buddyname);
148         col_append_fstr(pinfo->cinfo, COL_INFO, " -> %s", msg);
149       }
150       
151       if(chat_tree) {
152         proto_tree_add_text(chat_tree, tvb, 31, buddyname_length, 
153                             "Screen Name: %s",
154                             format_text(buddyname, buddyname_length));
155       }
156       return tvb_length(tvb);
157     default:
158       return 0;
159     }
160 }
161
162 /* Register the protocol with Ethereal */
163 void
164 proto_register_aim_chat(void)
165 {
166
167 /* Setup list of header fields */
168 /*FIXME
169   static hf_register_info hf[] = {
170   };*/
171
172 /* Setup protocol subtree array */
173   static gint *ett[] = {
174     &ett_aim_chat,
175   };
176
177 /* Register the protocol name and description */
178   proto_aim_chat = proto_register_protocol("AIM Chat Service", "AIM Chat", "aim_chat");
179
180 /* Required function calls to register the header fields and subtrees used */
181 /*FIXME
182   proto_register_field_array(proto_aim_chat, hf, array_length(hf));*/
183   proto_register_subtree_array(ett, array_length(ett));
184 }
185
186 void
187 proto_reg_handoff_aim_chat(void)
188 {
189   dissector_handle_t aim_handle;
190   aim_handle = new_create_dissector_handle(dissect_aim_snac_chat, proto_aim_chat);
191   dissector_add("aim.family", FAMILY_CHAT, aim_handle);
192   aim_init_family(FAMILY_CHAT, "Chat", aim_fnac_family_chat);
193 }