Menu

[r40]: / trunk / WhatsappClient / WhatsAppProtocol / Response / MessageRecvResponse.cs  Maximize  Restore  History

Download this file

283 lines (264 with data), 12.0 kB

  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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
class MessageRecvResponse
{
private WhatsSendHandler sendHandler;
public MessageRecvResponse(WhatsSendHandler sendHandler)
{
this.sendHandler = sendHandler;
}
public void ParseMessageRecv(ProtocolTreeNode messageNode)
{
FMessage.Builder builder = new FMessage.Builder();
string tmpAttrbId = messageNode.GetAttribute("id");
string tmpAttrFrom = messageNode.GetAttribute("from");
string tmpAttrFromName = messageNode.GetAttribute("");
string tmpAttrFromJid = messageNode.GetAttribute("author") ?? "";
string tmpAttrType = messageNode.GetAttribute("type");
string tmpTAttribT = messageNode.GetAttribute("t");
long result = 0L;
if (!string.IsNullOrEmpty(tmpTAttribT) && long.TryParse(tmpTAttribT, out result))
{
builder.Timestamp(new DateTime?(WhatsConstants.UnixEpoch.AddSeconds((double)result)));
}
if ("error".Equals(tmpAttrType))
{
TypeError(messageNode, tmpAttrbId, tmpAttrFrom);
}
else if ("subject".Equals(tmpAttrType))
{
TypeSubject(messageNode, tmpAttrFrom, tmpAttrFromJid, tmpAttrbId, tmpTAttribT);
}
else if ("chat".Equals(tmpAttrType))
{
TypeChat(messageNode, tmpAttrFrom, tmpAttrbId, builder, tmpAttrFromJid);
}
else if ("notification".Equals(tmpAttrType))
{
TypeNotification(messageNode, tmpAttrFrom, tmpAttrbId);
}
}
private void TypeNotification(ProtocolTreeNode messageNode, string tmpAttrFrom, string tmpAttrbId)
{
foreach (ProtocolTreeNode tmpChild in (messageNode.GetAllChildren() ?? new ProtocolTreeNode[0]))
{
if (ProtocolTreeNode.TagEquals(tmpChild, "notification"))
{
string tmpChildType = tmpChild.GetAttribute("type");
if (StringComparer.Ordinal.Equals(tmpChildType, "picture"))
{
TypeNotificationPicture(tmpChild, tmpAttrFrom);
}
}
else if (ProtocolTreeNode.TagEquals(tmpChild, "request"))
{
this.sendHandler.SendNotificationReceived(tmpAttrFrom, tmpAttrbId);
}
}
}
private static void TypeNotificationPicture(ProtocolTreeNode tmpChild, string tmpFrom)
{
foreach (ProtocolTreeNode item in (tmpChild.GetAllChildren() ?? new ProtocolTreeNode[0]))
{
if (ProtocolTreeNode.TagEquals(item, "set"))
{
string photoId = item.GetAttribute("id");
if (photoId != null)
{
WhatsEventHandler.OnPhotoChangedEventHandler(tmpFrom, item.GetAttribute("author"), photoId);
}
}
else if (ProtocolTreeNode.TagEquals(item, "delete"))
{
WhatsEventHandler.OnPhotoChangedEventHandler(tmpFrom, item.GetAttribute("author"), null);
}
}
}
private void TypeChat(ProtocolTreeNode messageNode, string tmpAttrFrom, string tmpAttrbId, FMessage.Builder builder,
string tmpAttrFromJid)
{
foreach (ProtocolTreeNode itemNode in (messageNode.GetAllChildren() ?? new ProtocolTreeNode[0]))
{
if (ProtocolTreeNode.TagEquals(itemNode, "composing"))
{
WhatsEventHandler.OnIsTypingEventHandler(tmpAttrFrom, true);
}
else if (ProtocolTreeNode.TagEquals(itemNode, "paused"))
{
WhatsEventHandler.OnIsTypingEventHandler(tmpAttrFrom, false);
}
else if (ProtocolTreeNode.TagEquals(itemNode, "body") && (tmpAttrbId != null))
{
string dataString = Encoding.UTF8.GetString(itemNode.GetData());
var tmpMessKey = new FMessage.Key(tmpAttrFrom, false, tmpAttrbId);
builder.Key(tmpMessKey).Remote_resource(tmpAttrFromJid).NewIncomingInstance().Data(dataString);
}
else if (ProtocolTreeNode.TagEquals(itemNode, "media") && (tmpAttrbId != null))
{
long tmpMediaSize;
int tmpMediaDuration;
builder.Media_wa_type(FMessage.GetMessage_WA_Type(itemNode.GetAttribute("type"))).Media_url(
itemNode.GetAttribute("url")).Media_name(itemNode.GetAttribute("file"));
if (long.TryParse(itemNode.GetAttribute("size"), WhatsConstants.WhatsAppNumberStyle,
CultureInfo.InvariantCulture, out tmpMediaSize))
{
builder.Media_size(tmpMediaSize);
}
string tmpAttrSeconds = itemNode.GetAttribute("seconds");
if ((tmpAttrSeconds != null) &&
int.TryParse(tmpAttrSeconds, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpMediaDuration))
{
builder.Media_duration_seconds(tmpMediaDuration);
}
if (builder.Media_wa_type().HasValue && (builder.Media_wa_type().Value == FMessage.Type.Location))
{
double tmpLatitude = 0;
double tmpLongitude = 0;
string tmpAttrLatitude = itemNode.GetAttribute("latitude");
string tmpAttrLongitude = itemNode.GetAttribute("longitude");
if ((tmpAttrLatitude == null) || (tmpAttrLongitude == null))
{
tmpAttrLatitude = "0";
tmpAttrLongitude = "0";
}
else if (!double.TryParse(tmpAttrLatitude, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpLatitude) ||
!double.TryParse(tmpAttrLongitude, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpLongitude))
{
throw new CorruptStreamException("location message exception parsing lat or long attribute: " + tmpAttrLatitude + " " + tmpAttrLongitude);
}
builder.Latitude(tmpLatitude).Longitude(tmpLongitude);
string tmpAttrName = itemNode.GetAttribute("name");
string tmpAttrUrl = itemNode.GetAttribute("url");
if (tmpAttrName != null)
{
builder.Location_details(tmpAttrName);
}
if (tmpAttrUrl != null)
{
builder.Location_url(tmpAttrUrl);
}
}
if (builder.Media_wa_type().HasValue && (builder.Media_wa_type().Value) == FMessage.Type.Contact)
{
ProtocolTreeNode tmpChildMedia = itemNode.GetChild("media");
if (tmpChildMedia != null)
{
builder.Media_name(tmpChildMedia.GetAttribute("name")).Data(Encoding.UTF8.GetString(tmpChildMedia.GetData()));
}
}
else
{
string tmpAttrEncoding = itemNode.GetAttribute("encoding") ?? "text";
if (tmpAttrEncoding == "text")
{
builder.Data(Encoding.UTF8.GetString(itemNode.GetData()));
}
}
var tmpMessageKey = new FMessage.Key(tmpAttrFrom, false, tmpAttrbId);
builder.Key(tmpMessageKey).Remote_resource(tmpAttrFromJid).NewIncomingInstance();
}
else if (ProtocolTreeNode.TagEquals(itemNode, "request"))
{
builder.Wants_receipt(true);
}
else if (ProtocolTreeNode.TagEquals(itemNode, "x"))
{
string str16 = itemNode.GetAttribute("xmlns");
if ("jabber:x:event".Equals(str16) && (tmpAttrbId != null))
{
var tmpMessageKey = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId);
}
}
else if (ProtocolTreeNode.TagEquals(itemNode, "received"))
{
if (tmpAttrbId != null)
{
var tmpMessageKey = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId);
if (true)
{
string tmpAttrType = itemNode.GetAttribute("type");
if ((tmpAttrType != null) && !tmpAttrType.Equals("delivered"))
{
if (tmpAttrType.Equals("visible"))
{
this.sendHandler.SendVisibleReceiptAck(tmpAttrFrom, tmpAttrbId);
}
}
else
{
this.sendHandler.SendDeliveredReceiptAck(tmpAttrFrom, tmpAttrbId);
}
}
}
}
else if (ProtocolTreeNode.TagEquals(itemNode, "offline"))
{
builder.Offline(true);
}
else if (ProtocolTreeNode.TagEquals(itemNode, "notify"))
{
var tmpAttrName = itemNode.GetAttribute("name");
if (tmpAttrName != null)
{
builder.from_me = false;
builder.id = tmpAttrbId;
builder.remote_jid = tmpAttrFromJid;
builder.Key().serverNickname = tmpAttrName;
}
}
}
if (!builder.Timestamp().HasValue)
{
builder.Timestamp(new DateTime?(DateTime.Now));
}
FMessage message = builder.Build();
if (message != null)
{
WhatsEventHandler.OnMessageRecievedEventHandler(message);
}
}
private void TypeSubject(ProtocolTreeNode messageNode, string tmpFrom, string uJid, string tmpId, string tmpT)
{
bool flag = false;
foreach (ProtocolTreeNode item in messageNode.GetAllChildren("request"))
{
if (item.GetAttribute("xmlns").Equals("urn:xmpp:receipts"))
{
flag = true;
}
}
ProtocolTreeNode child = messageNode.GetChild("body");
//string subject = (child == null) ? null : child.GetDataString();
string subject = (child == null) ? null : WhatsAppProtocol.SYSEncoding.GetString(child.GetData());
if (subject != null)
{
WhatsEventHandler.OnGroupNewSubjectEventHandler(tmpFrom, uJid, subject, int.Parse(tmpT, CultureInfo.InvariantCulture));
}
if (flag)
{
this.sendHandler.SendSubjectReceived(tmpFrom, tmpId);
}
}
private void TypeError(ProtocolTreeNode messageNode, string tmpAttrbId, string tmpAttrFrom)
{
int num2 = 0;
foreach (ProtocolTreeNode node in messageNode.GetAllChildren("error"))
{
string tmpCode = node.GetAttribute("code");
try
{
num2 = int.Parse(tmpCode, CultureInfo.InvariantCulture);
}
catch (Exception)
{
}
}
if ((tmpAttrFrom != null) && (tmpAttrbId != null))
{
FMessage.Key key = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId);
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.