MessageModel CreatePresenceUpdateMessage(Jid jid, PersonModel person, Presence pres)
{
var builder = CreateMessageBuilder();
builder.AppendEventPrefix();
string idstring = GenerateIdString(person);
// print the type (and in case of available detailed type)
switch (pres.Type) {
case PresenceType.available:
switch(pres.Show) {
case ShowType.NONE:
builder.AppendFormat(_("{0}{1} is available"), person, idstring);
builder.AppendPresenceState(person, MessageType.PresenceStateOnline);
break;
case ShowType.away:
builder.AppendFormat(_("{0}{1} is away"), person, idstring);
builder.AppendPresenceState(person, MessageType.PresenceStateAway);
break;
case ShowType.xa:
builder.AppendFormat(_("{0}{1} is extended away"), person, idstring);
builder.AppendPresenceState(person, MessageType.PresenceStateAway);
break;
case ShowType.dnd:
builder.AppendFormat(_("{0}{1} wishes not to be disturbed"), person, idstring);
builder.AppendPresenceState(person, MessageType.PresenceStateAway);
break;
case ShowType.chat:
builder.AppendFormat(_("{0}{1} wants to chat"), person, idstring);
builder.AppendPresenceState(person, MessageType.PresenceStateOnline);
break;
}
break;
case PresenceType.unavailable:
builder.AppendPresenceState(person, MessageType.PresenceStateOffline);
builder.AppendFormat(_("{0}{1} is offline"), person, idstring);
break;
case PresenceType.subscribe:
if ((person as XmppPersonModel).Ask == AskType.subscribe) {
builder = CreateMessageBuilder();
builder.AppendActionPrefix();
builder.AppendFormat(_("Automatically allowed {0} to subscribe to you, since you are already asking to subscribe"),
person
);
} else {
builder.AppendFormat(_("{0}{1} wishes to subscribe to you"),
person, idstring);
// you have to respond
builder.MarkAsHighlight();
}
break;
case PresenceType.subscribed:
// you can now see their presences
builder.AppendFormat(_("{0}{1} allowed you to subscribe"), person, idstring);
break;
case PresenceType.unsubscribed:
if ((person as XmppPersonModel).Subscription == SubscriptionType.from) {
builder = CreateMessageBuilder();
builder.AppendActionPrefix();
builder.AppendFormat(
_("Automatically removed {0}'s subscription to " +
"your presences after losing the subscription " +
"to theirs"),
person
);
} else {
// you cannot (anymore?) see their presences
builder.AppendFormat(_("{0}{1} denied/removed your subscription"), person, idstring);
}
break;
case PresenceType.unsubscribe:
// you might still be able to see their presences
builder.AppendFormat(_("{0}{1} unsubscribed from you"), person, idstring);
break;
case PresenceType.error:
if (pres.Error == null) {
builder.AppendErrorText(_("received a malformed error message: {0}"), pres);
break;
}
switch (pres.Error.Type) {
case ErrorType.cancel:
switch (pres.Error.Condition) {
case ErrorCondition.RemoteServerNotFound:
builder.AppendErrorText(_("{0}{1}'s server could not be found"), person.IdentityName, idstring);
break;
case ErrorCondition.Conflict:
builder.AppendErrorText(_("{0}{1} is already using your requested resource"), person.IdentityName, idstring);
break;
default:
if (!String.IsNullOrEmpty(pres.Error.ErrorText)) {
builder.AppendErrorText(pres.Error.ErrorText);
} else {
builder.AppendErrorText(
_("There is currently no useful error message for {0}, {1}, {2}{3}"),
pres.Error.Type,
pres.Error.Condition,
person.IdentityName,
idstring);
}
break;
}
break;
case ErrorType.auth:
switch (pres.Error.Condition) {
case ErrorCondition.Forbidden:
builder.AppendErrorText(
_("You do not have permission to access {0}{1}")
, person.IdentityName,
idstring);
break;
default:
if (!String.IsNullOrEmpty(pres.Error.ErrorText)) {
builder.AppendErrorText(pres.Error.ErrorText);
} else {
builder.AppendErrorText(
_("There is currently no useful error message for {0}, {1}, {2}{3}"),
pres.Error.Type,
pres.Error.Condition,
person.IdentityName,
idstring);
}
break;
}
break;
default:
if (!String.IsNullOrEmpty(pres.Error.ErrorText)) {
builder.AppendErrorText(pres.Error.ErrorText);
} else {
builder.AppendErrorText(
_("There is currently no useful error message for {0}, {1}, {2}{3}"),
pres.Error.Type,
pres.Error.Condition,
person.IdentityName,
idstring);
}
break;
}
break;
}
// print timestamp of presence
if (pres.XDelay != null || pres.Last != null) {
DateTime stamp = DateTime.MinValue;
TimeSpan span = TimeSpan.MinValue;
if (pres.XDelay != null) {
stamp = pres.XDelay.Stamp;
span = DateTime.Now.Subtract(stamp);
} else if (pres.Last != null) {
span = TimeSpan.FromSeconds(pres.Last.Seconds);
stamp = DateTime.Now.Subtract(span);
}
string spanstr;
if (span > TimeSpan.FromDays(1)) {
spanstr = String.Format(
"{0:00}:{1:00}:{2:00}:{3:00}",
span.TotalDays, span.Hours, span.Minutes, span.Seconds
);
spanstr = String.Format(_("{0} days"), spanstr);
} else if (span > TimeSpan.FromHours(1)) {
spanstr = String.Format(
"{0:00}:{1:00}:{2:00}",
span.Hours, span.Minutes, span.Seconds
);
spanstr = String.Format(_("{0} hours"), spanstr);
} else if (span > TimeSpan.FromMinutes(1)) {
spanstr = String.Format("{0:00}:{1:00}",
span.Minutes, span.Seconds);
spanstr = String.Format(_("{0} minutes"), spanstr);
} else {
spanstr = String.Format("{0:00}", span.Seconds);
spanstr = String.Format(_("{0} seconds"), spanstr);
}
string timestamp = null;
try {
string format = Session.UserConfig["Interface/Notebook/TimestampFormat"] as string;
if (!String.IsNullOrEmpty(format)) {
timestamp = stamp.ToString(format);
}
} catch (FormatException e) {
timestamp = "Timestamp Format ERROR: " + e.Message;
}
builder.AppendText(_(" since {0} ({1})"), timestamp, spanstr);
}
// print user defined message
if (pres.Status != null && pres.Status.Trim().Length > 0) {
builder.AppendText(": {0}", pres.Status);
}
return builder.ToMessage();
}