ValidationLogger.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2013-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "ValidationLogger.h"
  21. #import "BoxedMessage.h"
  22. #import "AbstractMessage.h"
  23. #import "NSString+Hex.h"
  24. #import "ContactStore.h"
  25. #import "Contact.h"
  26. #import "DocumentManager.h"
  27. #import "BoxVoIPCallOfferMessage.h"
  28. #import "BoxVoIPCallAnswerMessage.h"
  29. #import "BoxVoIPCallIceCandidatesMessage.h"
  30. #import "DeliveryReceiptMessage.h"
  31. #import "UserSettings.h"
  32. #import "ThreemaFramework/ThreemaFramework-swift.h"
  33. #ifdef DEBUG
  34. static const DDLogLevel ddLogLevel = DDLogLevelAll;
  35. #else
  36. static const DDLogLevel ddLogLevel = DDLogLevelNotice;
  37. #endif
  38. @implementation ValidationLogger
  39. + (ValidationLogger*)sharedValidationLogger {
  40. static ValidationLogger *instance;
  41. @synchronized (self) {
  42. if (!instance)
  43. instance = [ValidationLogger alloc];
  44. }
  45. return instance;
  46. }
  47. - (void)logBoxedMessage:(BoxedMessage*)boxedMessage isIncoming:(BOOL)incoming description:(NSString *)description {
  48. if (![UserSettings sharedUserSettings].validationLogging)
  49. return;
  50. /* obtain public key for convenience */
  51. NSString *publicKeyIdentity;
  52. if (incoming) {
  53. publicKeyIdentity = boxedMessage.fromIdentity;
  54. } else {
  55. publicKeyIdentity = boxedMessage.toIdentity;
  56. }
  57. [[ContactStore sharedContactStore] fetchPublicKeyForIdentity:publicKeyIdentity onCompletion:^(NSData *publicKey) {
  58. DDLogNotice(@"%@ message ID %@ from %@ to %@:\nDate: %@\nNonce: %@\nData: %@\nPublic key (%@): %@\nDescription: %@",
  59. incoming ? @"Incoming" : @"Outgoing",
  60. [NSString stringWithHexData:boxedMessage.messageId],
  61. boxedMessage.fromIdentity,
  62. boxedMessage.toIdentity,
  63. [DateFormatter shortStyleDateTimeSeconds:boxedMessage.date],
  64. [NSString stringWithHexData:boxedMessage.nonce],
  65. [NSString stringWithHexData:boxedMessage.box],
  66. publicKeyIdentity,
  67. [NSString stringWithHexData:publicKey],
  68. description ? description : @"");
  69. } onError:^(NSError *error) {
  70. ;//ignore
  71. }];
  72. }
  73. - (void)logSimpleMessage:(AbstractMessage*)message isIncoming:(BOOL)incoming description:(NSString *)description {
  74. if (![UserSettings sharedUserSettings].validationLogging)
  75. return;
  76. if (message.type == MSGTYPE_TYPING_INDICATOR)
  77. return;
  78. NSString *logmsg = @"";
  79. NSError *error;
  80. NSDictionary *json;
  81. NSArray *receiptMessageIds;
  82. __block NSMutableString *messageIds = [NSMutableString new];
  83. NSString *typeText = @"Unknown";
  84. switch (message.type) {
  85. case MSGTYPE_TEXT:
  86. typeText = @"TEXT";
  87. break;
  88. case MSGTYPE_IMAGE:
  89. typeText = @"IMAGE";
  90. break;
  91. case MSGTYPE_LOCATION:
  92. typeText = @"LOCATION";
  93. break;
  94. case MSGTYPE_VIDEO:
  95. typeText = @"VIDEO";
  96. break;
  97. case MSGTYPE_AUDIO:
  98. typeText = @"AUDIO";
  99. break;
  100. case MSGTYPE_BALLOT_CREATE:
  101. typeText = @"BALLOT_CREATE";
  102. break;
  103. case MSGTYPE_BALLOT_VOTE:
  104. typeText = @"BALLOT_VOTE";
  105. break;
  106. case MSGTYPE_CONTACT_SET_PHOTO:
  107. typeText = @"SET_PHOTO";
  108. break;
  109. case MSGTYPE_CONTACT_DELETE_PHOTO:
  110. typeText = @"DELETE_PHOTO";
  111. break;
  112. case MSGTYPE_CONTACT_REQUEST_PHOTO:
  113. typeText = @"REQUEST_PHOTO";
  114. break;
  115. case MSGTYPE_GROUP_TEXT:
  116. typeText = @"GROUP_TEXT";
  117. break;
  118. case MSGTYPE_GROUP_LOCATION:
  119. typeText = @"GROUP_LOCATION";
  120. break;
  121. case MSGTYPE_GROUP_IMAGE:
  122. typeText = @"GROUP_IMAGE";
  123. break;
  124. case MSGTYPE_GROUP_VIDEO:
  125. typeText = @"GROUP_VIDEO";
  126. break;
  127. case MSGTYPE_GROUP_AUDIO:
  128. typeText = @"GROUP_AUDIO";
  129. break;
  130. case MSGTYPE_GROUP_FILE:
  131. typeText = @"GROUP_FILE";
  132. break;
  133. case MSGTYPE_GROUP_CREATE:
  134. typeText = @"GROUP_CREATE";
  135. break;
  136. case MSGTYPE_GROUP_RENAME:
  137. typeText = @"GROUP_RENAME";
  138. break;
  139. case MSGTYPE_GROUP_LEAVE:
  140. typeText = @"GROUP_LEAVE";
  141. break;
  142. case MSGTYPE_GROUP_SET_PHOTO:
  143. typeText = @"GROUP_SET_PHOTO";
  144. break;
  145. case MSGTYPE_GROUP_REQUEST_SYNC:
  146. typeText = @"GROUP_REQUEST_SYNC";
  147. break;
  148. case MSGTYPE_GROUP_BALLOT_CREATE:
  149. typeText = @"GROUP_BALLOT_CREATE";
  150. break;
  151. case MSGTYPE_GROUP_BALLOT_VOTE:
  152. typeText = @"GROUP_BALLOT_VOTE";
  153. break;
  154. case MSGTYPE_VOIP_CALL_OFFER:
  155. typeText = @"CALL_OFFER";
  156. json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:((BoxVoIPCallOfferMessage *)message).jsonData options:0 error:&error];
  157. if (json == nil) {
  158. return;
  159. }
  160. logmsg = [NSString stringWithFormat:@"Offer:\n%@\nDescription: %@",
  161. json,
  162. description];
  163. break;
  164. case MSGTYPE_VOIP_CALL_ANSWER:
  165. typeText = @"CALL_ANSWER";
  166. json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:((BoxVoIPCallAnswerMessage *)message).jsonData options:0 error:&error];
  167. if (json == nil) {
  168. return;
  169. }
  170. logmsg = [NSString stringWithFormat:@"Answer:\n%@\nDescription: %@",
  171. json,
  172. description];
  173. break;
  174. case MSGTYPE_VOIP_CALL_ICECANDIDATE:
  175. typeText = @"CALL_ICECANDIDATE";
  176. json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:((BoxVoIPCallIceCandidatesMessage *)message).jsonData options:0 error:&error];
  177. if (json == nil) {
  178. return;
  179. }
  180. logmsg = [NSString stringWithFormat:@"Candidates:\n%@\nDescription: %@",
  181. json,
  182. description];
  183. break;
  184. case MSGTYPE_VOIP_CALL_HANGUP:
  185. typeText = @"CALL_HANGUP";
  186. break;
  187. case MSGTYPE_VOIP_CALL_RINGING:
  188. typeText = @"CALL_RINGING";
  189. break;
  190. case MSGTYPE_DELIVERY_RECEIPT:
  191. typeText = @"DELIVERY_RECEIPT";
  192. receiptMessageIds = ((DeliveryReceiptMessage *)message).receiptMessageIds;
  193. [receiptMessageIds enumerateObjectsUsingBlock:^(NSData *messageId, NSUInteger idx, BOOL * _Nonnull stop) {
  194. if (messageIds.length > 0) {
  195. [messageIds appendString:@", "];
  196. }
  197. [messageIds appendString:[NSString stringWithHexData:messageId]];
  198. }];
  199. logmsg = [NSString stringWithFormat:@"ReceiptMessageIds: %@",
  200. messageIds
  201. ];
  202. break;
  203. case MSGTYPE_TYPING_INDICATOR:
  204. typeText = @"TYPING_INDICATOR";
  205. break;
  206. default:
  207. break;
  208. }
  209. // Prepend generic message header information
  210. DDLogNotice(@"%@ %@ message ID %@ from %@ to %@\nDate: %@%@%@",
  211. incoming ? @"Incoming" : @"Outgoing",
  212. typeText,
  213. [NSString stringWithHexData:message.messageId],
  214. message.fromIdentity,
  215. message.toIdentity,
  216. [DateFormatter shortStyleDateTimeSeconds:message.date],
  217. logmsg.length > 0 ? @"\n" : @"",
  218. logmsg);
  219. }
  220. - (void)logString:(NSString *)logMessage {
  221. if ([UserSettings sharedUserSettings].validationLogging) {
  222. DDLogNotice(@"%@", logMessage);
  223. }
  224. }
  225. @end