emailengine-app 1.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (405) hide show
  1. package/.eslintrc +14 -0
  2. package/.github/CODE_OF_CONDUCT.md +76 -0
  3. package/.github/FUNDING.yml +4 -0
  4. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  5. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  6. package/.github/contributing.md +17 -0
  7. package/.ncurc.js +10 -0
  8. package/.prettierrc.js +8 -0
  9. package/Dockerfile +17 -0
  10. package/Gruntfile.js +16 -0
  11. package/LICENSE.txt +661 -0
  12. package/README.md +524 -0
  13. package/bin/emailengine.js +14 -0
  14. package/config/default.toml +39 -0
  15. package/docker-compose.yml +47 -0
  16. package/encrypt.js +179 -0
  17. package/examples/api.md +137 -0
  18. package/examples/auth-server.js +104 -0
  19. package/getswagger.sh +8 -0
  20. package/lib/account.js +562 -0
  21. package/lib/append-list.js +67 -0
  22. package/lib/bounce-detect.js +380 -0
  23. package/lib/connection.js +1753 -0
  24. package/lib/consts.js +22 -0
  25. package/lib/db.js +72 -0
  26. package/lib/encrypt.js +100 -0
  27. package/lib/enum-message-flags.js +6 -0
  28. package/lib/get-raw-email.js +292 -0
  29. package/lib/get-secret.js +83 -0
  30. package/lib/logger.js +35 -0
  31. package/lib/lua/s-list-accounts.lua +51 -0
  32. package/lib/lua/z-expunge.lua +20 -0
  33. package/lib/lua/z-get-by-uid.lua +16 -0
  34. package/lib/lua/z-get-mailbox-id.lua +15 -0
  35. package/lib/lua/z-get-mailbox-path.lua +4 -0
  36. package/lib/lua/z-get.lua +15 -0
  37. package/lib/lua/z-push.lua +14 -0
  38. package/lib/lua/z-set.lua +17 -0
  39. package/lib/mailbox.js +1545 -0
  40. package/lib/message-port-stream.js +79 -0
  41. package/lib/schemas.js +311 -0
  42. package/lib/settings.js +63 -0
  43. package/lib/tools.js +488 -0
  44. package/package.json +79 -0
  45. package/scan.js +111 -0
  46. package/server.js +672 -0
  47. package/static/bootstrap-4.6.0-dist/css/bootstrap-grid.css +3872 -0
  48. package/static/bootstrap-4.6.0-dist/css/bootstrap-grid.css.map +1 -0
  49. package/static/bootstrap-4.6.0-dist/css/bootstrap-grid.min.css +7 -0
  50. package/static/bootstrap-4.6.0-dist/css/bootstrap-grid.min.css.map +1 -0
  51. package/static/bootstrap-4.6.0-dist/css/bootstrap-reboot.css +325 -0
  52. package/static/bootstrap-4.6.0-dist/css/bootstrap-reboot.css.map +1 -0
  53. package/static/bootstrap-4.6.0-dist/css/bootstrap-reboot.min.css +8 -0
  54. package/static/bootstrap-4.6.0-dist/css/bootstrap-reboot.min.css.map +1 -0
  55. package/static/bootstrap-4.6.0-dist/css/bootstrap.css +10298 -0
  56. package/static/bootstrap-4.6.0-dist/css/bootstrap.css.map +1 -0
  57. package/static/bootstrap-4.6.0-dist/css/bootstrap.min.css +7 -0
  58. package/static/bootstrap-4.6.0-dist/css/bootstrap.min.css.map +1 -0
  59. package/static/bootstrap-4.6.0-dist/js/bootstrap.bundle.js +7045 -0
  60. package/static/bootstrap-4.6.0-dist/js/bootstrap.bundle.js.map +1 -0
  61. package/static/bootstrap-4.6.0-dist/js/bootstrap.bundle.min.js +7 -0
  62. package/static/bootstrap-4.6.0-dist/js/bootstrap.bundle.min.js.map +1 -0
  63. package/static/bootstrap-4.6.0-dist/js/bootstrap.js +4432 -0
  64. package/static/bootstrap-4.6.0-dist/js/bootstrap.js.map +1 -0
  65. package/static/bootstrap-4.6.0-dist/js/bootstrap.min.js +7 -0
  66. package/static/bootstrap-4.6.0-dist/js/bootstrap.min.js.map +1 -0
  67. package/static/css/callout.css +63 -0
  68. package/static/css/emailengine.css +33 -0
  69. package/static/favicon/android-chrome-192x192.png +0 -0
  70. package/static/favicon/android-chrome-512x512.png +0 -0
  71. package/static/favicon/apple-touch-icon.png +0 -0
  72. package/static/favicon/favicon-16x16.png +0 -0
  73. package/static/favicon/favicon-32x32.png +0 -0
  74. package/static/favicon/manifest.json +20 -0
  75. package/static/favicon.ico +0 -0
  76. package/static/icons/alarm-fill.svg +3 -0
  77. package/static/icons/alarm.svg +7 -0
  78. package/static/icons/alert-circle-fill.svg +3 -0
  79. package/static/icons/alert-circle.svg +4 -0
  80. package/static/icons/alert-octagon-fill.svg +3 -0
  81. package/static/icons/alert-octagon.svg +5 -0
  82. package/static/icons/alert-square-fill.svg +3 -0
  83. package/static/icons/alert-square.svg +5 -0
  84. package/static/icons/alert-triangle-fill.svg +3 -0
  85. package/static/icons/alert-triangle.svg +5 -0
  86. package/static/icons/archive-fill.svg +3 -0
  87. package/static/icons/archive.svg +4 -0
  88. package/static/icons/arrow-bar-bottom.svg +4 -0
  89. package/static/icons/arrow-bar-left.svg +4 -0
  90. package/static/icons/arrow-bar-right.svg +4 -0
  91. package/static/icons/arrow-bar-up.svg +4 -0
  92. package/static/icons/arrow-clockwise.svg +4 -0
  93. package/static/icons/arrow-counterclockwise.svg +4 -0
  94. package/static/icons/arrow-down-left.svg +4 -0
  95. package/static/icons/arrow-down-right.svg +4 -0
  96. package/static/icons/arrow-down-short.svg +4 -0
  97. package/static/icons/arrow-down.svg +4 -0
  98. package/static/icons/arrow-left-right.svg +5 -0
  99. package/static/icons/arrow-left-short.svg +4 -0
  100. package/static/icons/arrow-left.svg +4 -0
  101. package/static/icons/arrow-repeat.svg +5 -0
  102. package/static/icons/arrow-right-short.svg +4 -0
  103. package/static/icons/arrow-right.svg +4 -0
  104. package/static/icons/arrow-up-down.svg +5 -0
  105. package/static/icons/arrow-up-left.svg +4 -0
  106. package/static/icons/arrow-up-right.svg +4 -0
  107. package/static/icons/arrow-up-short.svg +4 -0
  108. package/static/icons/arrow-up.svg +4 -0
  109. package/static/icons/arrows-angle-contract.svg +5 -0
  110. package/static/icons/arrows-angle-expand.svg +5 -0
  111. package/static/icons/arrows-collapse.svg +5 -0
  112. package/static/icons/arrows-expand.svg +5 -0
  113. package/static/icons/arrows-fullscreen.svg +7 -0
  114. package/static/icons/at.svg +3 -0
  115. package/static/icons/award.svg +4 -0
  116. package/static/icons/backspace-fill.svg +3 -0
  117. package/static/icons/backspace-reverse-fill.svg +3 -0
  118. package/static/icons/backspace-reverse.svg +5 -0
  119. package/static/icons/backspace.svg +5 -0
  120. package/static/icons/bar-chart-fill.svg +5 -0
  121. package/static/icons/bar-chart.svg +3 -0
  122. package/static/icons/battery-charging.svg +5 -0
  123. package/static/icons/battery-full.svg +4 -0
  124. package/static/icons/battery.svg +4 -0
  125. package/static/icons/bell-fill.svg +3 -0
  126. package/static/icons/bell.svg +4 -0
  127. package/static/icons/blockquote-left.svg +4 -0
  128. package/static/icons/blockquote-right.svg +4 -0
  129. package/static/icons/book-half-fill.svg +4 -0
  130. package/static/icons/book.svg +4 -0
  131. package/static/icons/bookmark-fill.svg +3 -0
  132. package/static/icons/bookmark.svg +3 -0
  133. package/static/icons/bootstrap-fill.svg +3 -0
  134. package/static/icons/bootstrap-reboot.svg +3 -0
  135. package/static/icons/bootstrap.svg +4 -0
  136. package/static/icons/box-arrow-bottom-left.svg +4 -0
  137. package/static/icons/box-arrow-bottom-right.svg +4 -0
  138. package/static/icons/box-arrow-down.svg +5 -0
  139. package/static/icons/box-arrow-left.svg +5 -0
  140. package/static/icons/box-arrow-right.svg +5 -0
  141. package/static/icons/box-arrow-up-left.svg +4 -0
  142. package/static/icons/box-arrow-up-right.svg +4 -0
  143. package/static/icons/box-arrow-up.svg +5 -0
  144. package/static/icons/braces.svg +3 -0
  145. package/static/icons/brightness-fill-high.svg +4 -0
  146. package/static/icons/brightness-fill-low.svg +11 -0
  147. package/static/icons/brightness-high.svg +3 -0
  148. package/static/icons/brightness-low.svg +11 -0
  149. package/static/icons/brush.svg +4 -0
  150. package/static/icons/bucket-fill.svg +4 -0
  151. package/static/icons/bucket.svg +4 -0
  152. package/static/icons/building.svg +5 -0
  153. package/static/icons/bullseye.svg +6 -0
  154. package/static/icons/calendar-fill.svg +4 -0
  155. package/static/icons/calendar.svg +4 -0
  156. package/static/icons/camera-video-fill.svg +4 -0
  157. package/static/icons/camera-video.svg +4 -0
  158. package/static/icons/camera.svg +5 -0
  159. package/static/icons/capslock-fill.svg +3 -0
  160. package/static/icons/capslock.svg +3 -0
  161. package/static/icons/chat-fill.svg +3 -0
  162. package/static/icons/chat.svg +3 -0
  163. package/static/icons/check-box.svg +4 -0
  164. package/static/icons/check-circle.svg +4 -0
  165. package/static/icons/check.svg +3 -0
  166. package/static/icons/chevron-compact-down.svg +3 -0
  167. package/static/icons/chevron-compact-left.svg +3 -0
  168. package/static/icons/chevron-compact-right.svg +3 -0
  169. package/static/icons/chevron-compact-up.svg +3 -0
  170. package/static/icons/chevron-down.svg +3 -0
  171. package/static/icons/chevron-left.svg +3 -0
  172. package/static/icons/chevron-right.svg +3 -0
  173. package/static/icons/chevron-up.svg +3 -0
  174. package/static/icons/circle-fill.svg +3 -0
  175. package/static/icons/circle-half.svg +3 -0
  176. package/static/icons/circle-slash.svg +3 -0
  177. package/static/icons/circle.svg +3 -0
  178. package/static/icons/clock-fill.svg +3 -0
  179. package/static/icons/clock.svg +4 -0
  180. package/static/icons/cloud-download.svg +5 -0
  181. package/static/icons/cloud-fill.svg +3 -0
  182. package/static/icons/cloud-upload.svg +5 -0
  183. package/static/icons/cloud.svg +3 -0
  184. package/static/icons/code-slash.svg +3 -0
  185. package/static/icons/code.svg +3 -0
  186. package/static/icons/columns-gutters.svg +3 -0
  187. package/static/icons/columns.svg +4 -0
  188. package/static/icons/command.svg +4 -0
  189. package/static/icons/compass.svg +5 -0
  190. package/static/icons/cone-striped.svg +4 -0
  191. package/static/icons/cone.svg +4 -0
  192. package/static/icons/controller.svg +5 -0
  193. package/static/icons/credit-card.svg +5 -0
  194. package/static/icons/cursor-fill.svg +3 -0
  195. package/static/icons/cursor.svg +3 -0
  196. package/static/icons/dash.svg +3 -0
  197. package/static/icons/diamond-half.svg +3 -0
  198. package/static/icons/diamond.svg +3 -0
  199. package/static/icons/display-fill.svg +5 -0
  200. package/static/icons/display.svg +4 -0
  201. package/static/icons/document-code.svg +4 -0
  202. package/static/icons/document-diff.svg +5 -0
  203. package/static/icons/document-richtext.svg +4 -0
  204. package/static/icons/document-spreadsheet.svg +5 -0
  205. package/static/icons/document-text.svg +4 -0
  206. package/static/icons/document.svg +3 -0
  207. package/static/icons/documents-alt.svg +4 -0
  208. package/static/icons/documents.svg +4 -0
  209. package/static/icons/dot.svg +3 -0
  210. package/static/icons/download.svg +5 -0
  211. package/static/icons/egg-fried.svg +4 -0
  212. package/static/icons/eject-fill.svg +3 -0
  213. package/static/icons/eject.svg +3 -0
  214. package/static/icons/envelope-fill.svg +3 -0
  215. package/static/icons/envelope-open-fill.svg +3 -0
  216. package/static/icons/envelope-open.svg +5 -0
  217. package/static/icons/envelope.svg +4 -0
  218. package/static/icons/eye-fill.svg +4 -0
  219. package/static/icons/eye-slash-fill.svg +5 -0
  220. package/static/icons/eye-slash.svg +6 -0
  221. package/static/icons/eye.svg +4 -0
  222. package/static/icons/filter.svg +3 -0
  223. package/static/icons/flag-fill.svg +4 -0
  224. package/static/icons/flag.svg +4 -0
  225. package/static/icons/folder-fill.svg +3 -0
  226. package/static/icons/folder-symlink-fill.svg +3 -0
  227. package/static/icons/folder-symlink.svg +5 -0
  228. package/static/icons/folder.svg +4 -0
  229. package/static/icons/fonts.svg +3 -0
  230. package/static/icons/forward-fill.svg +3 -0
  231. package/static/icons/forward.svg +3 -0
  232. package/static/icons/gear-fill.svg +3 -0
  233. package/static/icons/gear-wide-connected.svg +4 -0
  234. package/static/icons/gear-wide.svg +3 -0
  235. package/static/icons/gear.svg +4 -0
  236. package/static/icons/geo.svg +5 -0
  237. package/static/icons/graph-down.svg +5 -0
  238. package/static/icons/graph-up.svg +5 -0
  239. package/static/icons/grid-fill.svg +6 -0
  240. package/static/icons/grid.svg +3 -0
  241. package/static/icons/hammer.svg +4 -0
  242. package/static/icons/hash.svg +3 -0
  243. package/static/icons/heart-fill.svg +3 -0
  244. package/static/icons/heart.svg +3 -0
  245. package/static/icons/house-fill.svg +4 -0
  246. package/static/icons/house.svg +4 -0
  247. package/static/icons/image-alt.svg +4 -0
  248. package/static/icons/image-fill.svg +3 -0
  249. package/static/icons/image.svg +5 -0
  250. package/static/icons/images.svg +5 -0
  251. package/static/icons/inbox-fill.svg +4 -0
  252. package/static/icons/inbox.svg +4 -0
  253. package/static/icons/inboxes-fill.svg +4 -0
  254. package/static/icons/inboxes.svg +4 -0
  255. package/static/icons/info-fill.svg +3 -0
  256. package/static/icons/info-square-fill.svg +3 -0
  257. package/static/icons/info-square.svg +5 -0
  258. package/static/icons/info.svg +5 -0
  259. package/static/icons/justify-left.svg +3 -0
  260. package/static/icons/justify-right.svg +3 -0
  261. package/static/icons/justify.svg +3 -0
  262. package/static/icons/kanban-fill.svg +3 -0
  263. package/static/icons/kanban.svg +6 -0
  264. package/static/icons/laptop.svg +4 -0
  265. package/static/icons/layout-sidebar-reverse.svg +4 -0
  266. package/static/icons/layout-sidebar.svg +4 -0
  267. package/static/icons/layout-split.svg +3 -0
  268. package/static/icons/list-check.svg +3 -0
  269. package/static/icons/list-ol.svg +4 -0
  270. package/static/icons/list-task.svg +5 -0
  271. package/static/icons/list-ul.svg +3 -0
  272. package/static/icons/list.svg +3 -0
  273. package/static/icons/lock-fill.svg +4 -0
  274. package/static/icons/lock.svg +3 -0
  275. package/static/icons/map.svg +3 -0
  276. package/static/icons/mic.svg +4 -0
  277. package/static/icons/moon.svg +3 -0
  278. package/static/icons/music-player-fill.svg +4 -0
  279. package/static/icons/music-player.svg +5 -0
  280. package/static/icons/option.svg +3 -0
  281. package/static/icons/outlet.svg +5 -0
  282. package/static/icons/pause-fill.svg +3 -0
  283. package/static/icons/pause.svg +3 -0
  284. package/static/icons/pen.svg +5 -0
  285. package/static/icons/pencil.svg +4 -0
  286. package/static/icons/people-fill.svg +3 -0
  287. package/static/icons/people.svg +3 -0
  288. package/static/icons/person-fill.svg +3 -0
  289. package/static/icons/person.svg +3 -0
  290. package/static/icons/phone-landscape.svg +4 -0
  291. package/static/icons/phone.svg +4 -0
  292. package/static/icons/pie-chart-fill.svg +3 -0
  293. package/static/icons/pie-chart.svg +4 -0
  294. package/static/icons/play-fill.svg +3 -0
  295. package/static/icons/play.svg +3 -0
  296. package/static/icons/plug.svg +4 -0
  297. package/static/icons/plus.svg +4 -0
  298. package/static/icons/power.svg +4 -0
  299. package/static/icons/question-fill.svg +3 -0
  300. package/static/icons/question-square-fill.svg +3 -0
  301. package/static/icons/question-square.svg +4 -0
  302. package/static/icons/question.svg +4 -0
  303. package/static/icons/reply-all-fill.svg +4 -0
  304. package/static/icons/reply-all.svg +4 -0
  305. package/static/icons/reply-fill.svg +3 -0
  306. package/static/icons/reply.svg +3 -0
  307. package/static/icons/screwdriver.svg +3 -0
  308. package/static/icons/search.svg +4 -0
  309. package/static/icons/shield-fill.svg +3 -0
  310. package/static/icons/shield-lock-fill.svg +3 -0
  311. package/static/icons/shield-lock.svg +5 -0
  312. package/static/icons/shield-shaded.svg +4 -0
  313. package/static/icons/shield.svg +3 -0
  314. package/static/icons/shift-fill.svg +3 -0
  315. package/static/icons/shift.svg +3 -0
  316. package/static/icons/skip-backward-fill.svg +4 -0
  317. package/static/icons/skip-backward.svg +3 -0
  318. package/static/icons/skip-end-fill.svg +5 -0
  319. package/static/icons/skip-end.svg +4 -0
  320. package/static/icons/skip-forward-fill.svg +5 -0
  321. package/static/icons/skip-forward.svg +3 -0
  322. package/static/icons/skip-start-fill.svg +4 -0
  323. package/static/icons/skip-start.svg +4 -0
  324. package/static/icons/speaker.svg +4 -0
  325. package/static/icons/square-fill.svg +3 -0
  326. package/static/icons/square-half.svg +3 -0
  327. package/static/icons/square.svg +3 -0
  328. package/static/icons/star-fill.svg +3 -0
  329. package/static/icons/star-half.svg +3 -0
  330. package/static/icons/star.svg +3 -0
  331. package/static/icons/stop-fill.svg +3 -0
  332. package/static/icons/stop.svg +3 -0
  333. package/static/icons/stopwatch-fill.svg +3 -0
  334. package/static/icons/stopwatch.svg +5 -0
  335. package/static/icons/sun.svg +4 -0
  336. package/static/icons/table.svg +7 -0
  337. package/static/icons/tablet-landscape.svg +4 -0
  338. package/static/icons/tablet.svg +4 -0
  339. package/static/icons/tag-fill.svg +3 -0
  340. package/static/icons/tag.svg +4 -0
  341. package/static/icons/terminal-fill.svg +3 -0
  342. package/static/icons/terminal.svg +4 -0
  343. package/static/icons/text-center.svg +3 -0
  344. package/static/icons/text-indent-left.svg +3 -0
  345. package/static/icons/text-indent-right.svg +3 -0
  346. package/static/icons/text-left.svg +3 -0
  347. package/static/icons/text-right.svg +3 -0
  348. package/static/icons/three-dots-vertical.svg +3 -0
  349. package/static/icons/three-dots.svg +3 -0
  350. package/static/icons/toggle-off.svg +3 -0
  351. package/static/icons/toggle-on.svg +3 -0
  352. package/static/icons/toggles.svg +4 -0
  353. package/static/icons/tools.svg +4 -0
  354. package/static/icons/trash-fill.svg +3 -0
  355. package/static/icons/trash.svg +4 -0
  356. package/static/icons/triangle-fill.svg +3 -0
  357. package/static/icons/triangle-half.svg +3 -0
  358. package/static/icons/triangle.svg +3 -0
  359. package/static/icons/trophy.svg +6 -0
  360. package/static/icons/tv-fill.svg +3 -0
  361. package/static/icons/tv.svg +3 -0
  362. package/static/icons/type-bold.svg +3 -0
  363. package/static/icons/type-h1.svg +3 -0
  364. package/static/icons/type-h2.svg +3 -0
  365. package/static/icons/type-h3.svg +3 -0
  366. package/static/icons/type-italic.svg +3 -0
  367. package/static/icons/type-strikethrough.svg +4 -0
  368. package/static/icons/type-underline.svg +4 -0
  369. package/static/icons/type.svg +3 -0
  370. package/static/icons/unlock-fill.svg +4 -0
  371. package/static/icons/unlock.svg +3 -0
  372. package/static/icons/upload.svg +4 -0
  373. package/static/icons/volume-down-fill.svg +4 -0
  374. package/static/icons/volume-down.svg +4 -0
  375. package/static/icons/volume-mute-fill.svg +4 -0
  376. package/static/icons/volume-mute.svg +4 -0
  377. package/static/icons/volume-up-fill.svg +6 -0
  378. package/static/icons/volume-up.svg +6 -0
  379. package/static/icons/wallet.svg +3 -0
  380. package/static/icons/watch.svg +5 -0
  381. package/static/icons/wifi.svg +5 -0
  382. package/static/icons/window.svg +5 -0
  383. package/static/icons/wrench.svg +3 -0
  384. package/static/icons/x-circle-fill.svg +3 -0
  385. package/static/icons/x-circle.svg +5 -0
  386. package/static/icons/x-octagon-fill.svg +3 -0
  387. package/static/icons/x-octagon.svg +4 -0
  388. package/static/icons/x-square-fill.svg +3 -0
  389. package/static/icons/x-square.svg +4 -0
  390. package/static/icons/x.svg +4 -0
  391. package/static/index.html +752 -0
  392. package/static/js/emailengine.js +581 -0
  393. package/static/js/jquery-3.4.1.slim.min.js +2 -0
  394. package/static/js/moment-with-locales-2.24.0.min.js +1 -0
  395. package/static/js/popper.min.js +5 -0
  396. package/static/logo.png +0 -0
  397. package/systemd/emailengine.service +89 -0
  398. package/systemd/nginx-proxy.conf +77 -0
  399. package/views/error.hbs +2 -0
  400. package/workers/api.js +2266 -0
  401. package/workers/arena.js +89 -0
  402. package/workers/imap.js +611 -0
  403. package/workers/smtp.js +278 -0
  404. package/workers/submit.js +214 -0
  405. package/workers/webhooks.js +134 -0
package/lib/tools.js ADDED
@@ -0,0 +1,488 @@
1
+ /* eslint no-bitwise: 0 */
2
+
3
+ 'use strict';
4
+
5
+ const msgpack = require('msgpack5')();
6
+ const enumMessageFlags = require('./enum-message-flags');
7
+ const Joi = require('joi');
8
+ const fetch = require('node-fetch');
9
+ const he = require('he');
10
+ const packageData = require('../package.json');
11
+ const settings = require('./settings');
12
+ const crypto = require('crypto');
13
+
14
+ module.exports = {
15
+ /**
16
+ * Helper function to set specific bit in a buffer
17
+ * @param {Buffer} buffer Buffer to edit
18
+ * @param {Number} bytePos Which byte in buffer to edit
19
+ * @param {Number} bit Which bit to update
20
+ * @param {Boolean} value If true, then sets bit, if false, then clears it
21
+ * @returns {Boolean} If true then bit was updated
22
+ */
23
+ setBit(buffer, bytePos, bit, value) {
24
+ bytePos = Number(bytePos) || 0;
25
+ if (bytePos < 0 || bytePos >= buffer.length) {
26
+ return false;
27
+ }
28
+
29
+ if (!value) {
30
+ buffer[bytePos] &= ~(1 << bit);
31
+ } else {
32
+ buffer[bytePos] |= 1 << bit;
33
+ }
34
+
35
+ return true;
36
+ },
37
+
38
+ /**
39
+ * Helper function to get specific bit from a buffer
40
+ * @param {Buffer} buffer Buffer to check for
41
+ * @param {Number} bytePos Which byte in buffer to check
42
+ * @param {Number} bit Which bit to check
43
+ * @returns {Boolean} If true then bit was set, otherwise bit was not set
44
+ */
45
+ readBit(buffer, bytePos, bit) {
46
+ return !!((buffer[bytePos] >> bit) % 2);
47
+ },
48
+
49
+ /**
50
+ * Parses stored message entry
51
+ * @param {Buffer} buffer Stored message entry
52
+ * @ @returns {Object} Message entry object
53
+ */
54
+ unserialize(buffer) {
55
+ // < [4B (UInt32LE) UID] [1B ENUM_FLAGS] [8B (BigUInt64LE) MODSEQ] [nB META (msgpack) [msgid, [flags], [labels]] ] >
56
+
57
+ if (buffer.length === 1) {
58
+ switch (buffer.toString()) {
59
+ case 'D': {
60
+ return { deleted: true };
61
+ }
62
+ case 'N': {
63
+ return { placeholder: true };
64
+ }
65
+ }
66
+ return {};
67
+ }
68
+
69
+ let uid = buffer.readUInt32LE(0);
70
+ let modseq = buffer.readBigUInt64LE(5);
71
+
72
+ let formatted = {
73
+ uid,
74
+ flags: new Set()
75
+ };
76
+
77
+ if (modseq) {
78
+ formatted.modseq = modseq;
79
+ }
80
+
81
+ enumMessageFlags.forEach((flag, i) => {
82
+ if (module.exports.readBit(buffer, 4, i)) {
83
+ formatted.flags.add(flag);
84
+ }
85
+ });
86
+
87
+ if (buffer.length > 4 + 1 + 8) {
88
+ let extra = msgpack.decode(buffer.slice(4 + 1 + 8));
89
+ if (Array.isArray(extra)) {
90
+ let emailId = extra[0];
91
+ if (emailId) {
92
+ formatted.emailId = emailId;
93
+ }
94
+
95
+ if (Array.isArray(extra[1])) {
96
+ extra[1].forEach(flag => {
97
+ formatted.flags.add(flag);
98
+ });
99
+ }
100
+
101
+ if (Array.isArray(extra[2])) {
102
+ formatted.labels = new Set(extra[2]);
103
+ }
104
+ }
105
+ }
106
+
107
+ return formatted;
108
+ },
109
+
110
+ /**
111
+ * Generates message entry for storage
112
+ * @param {Object} messageData Message entry object
113
+ * @ @returns {Buffer} Serialized message entry for storage
114
+ */
115
+ serialize(messageData) {
116
+ let buf = Buffer.alloc(4 + 1 + 8);
117
+ buf.writeUInt32LE(messageData.uid, 0);
118
+
119
+ let extra = [
120
+ messageData.emailId || null, //emailId (if exists)
121
+ null, // extra flags not in the default flag set
122
+ null // labels if Gmail All data
123
+ ];
124
+
125
+ for (let flag of messageData.flags) {
126
+ let enumFlag = enumMessageFlags.indexOf(flag);
127
+ if (enumFlag >= 0) {
128
+ module.exports.setBit(buf, 4, enumFlag, true);
129
+ } else {
130
+ if (!extra[1]) {
131
+ extra[1] = [];
132
+ }
133
+ extra[1].push(flag);
134
+ }
135
+ }
136
+
137
+ if (messageData.labels && messageData.labels.size) {
138
+ extra[2] = Array.from(messageData.labels);
139
+ }
140
+
141
+ if (messageData.modseq) {
142
+ buf.writeBigUInt64LE(messageData.modseq, 5);
143
+ }
144
+
145
+ return Buffer.concat([buf, msgpack.encode(extra)]);
146
+ },
147
+
148
+ /**
149
+ * Compares two message objects to see if there are any changes
150
+ * @param {*} storedMessageEntry
151
+ * @param {*} messageData
152
+ * @returns {Object | Boolean} Changes or false
153
+ */
154
+ compareExisting(storedMessageEntry, messageData, keys) {
155
+ const changes = {};
156
+ let hasChanges = false;
157
+
158
+ // detect deleted flags
159
+ if (!keys || keys.includes('flags')) {
160
+ for (let flag of storedMessageEntry.flags.values()) {
161
+ if (!messageData.flags.has(flag)) {
162
+ if (!changes.flags) {
163
+ changes.flags = {};
164
+ }
165
+ if (!changes.flags.deleted) {
166
+ changes.flags.deleted = [];
167
+ }
168
+ changes.flags.deleted.push(flag);
169
+ hasChanges = true;
170
+ }
171
+ }
172
+
173
+ // detect added flags
174
+ for (let flag of messageData.flags.values()) {
175
+ if (!storedMessageEntry.flags.has(flag)) {
176
+ if (!changes.flags) {
177
+ changes.flags = {};
178
+ }
179
+ if (!changes.flags.added) {
180
+ changes.flags.added = [];
181
+ }
182
+ changes.flags.added.push(flag);
183
+ hasChanges = true;
184
+ }
185
+ }
186
+ }
187
+
188
+ if (!keys || keys.includes('labels')) {
189
+ if (storedMessageEntry.labels || messageData.labels) {
190
+ if (storedMessageEntry.labels) {
191
+ // detect deleted labels
192
+ for (let flag of storedMessageEntry.labels.values()) {
193
+ if (!messageData.labels || !messageData.labels.has(flag)) {
194
+ if (!changes.labels) {
195
+ changes.labels = {};
196
+ }
197
+ if (!changes.labels.deleted) {
198
+ changes.labels.deleted = [];
199
+ }
200
+ changes.labels.deleted.push(flag);
201
+ hasChanges = true;
202
+ }
203
+ }
204
+ }
205
+ if (messageData.labels) {
206
+ // detect added labels
207
+ for (let flag of messageData.labels.values()) {
208
+ if (!storedMessageEntry.labels || !storedMessageEntry.labels.has(flag)) {
209
+ if (!changes.labels) {
210
+ changes.labels = {};
211
+ }
212
+ if (!changes.labels.added) {
213
+ changes.labels.added = [];
214
+ }
215
+ changes.labels.added.push(flag);
216
+ hasChanges = true;
217
+ }
218
+ }
219
+ }
220
+ }
221
+ }
222
+
223
+ return hasChanges ? changes : false;
224
+ },
225
+
226
+ normalizePath(path) {
227
+ if (/^INBOX$/i.test(path)) {
228
+ return 'INBOX';
229
+ }
230
+ return path;
231
+ },
232
+
233
+ async resolveCredentials(account, proto) {
234
+ let authServer = await settings.get('authServer');
235
+ if (!authServer) {
236
+ let err = new Error('Authentication server requested but not set');
237
+ throw err;
238
+ }
239
+
240
+ let headers = { 'User-Agent': `${packageData.name}/${packageData.version} (+https://emailengine.app)` };
241
+
242
+ let parsed = new URL(authServer);
243
+ let username, password;
244
+
245
+ if (parsed.username) {
246
+ username = he.decode(parsed.username);
247
+ parsed.username = '';
248
+ }
249
+
250
+ if (parsed.password) {
251
+ password = he.decode(parsed.password);
252
+ parsed.password = '';
253
+ }
254
+
255
+ if (username || password) {
256
+ headers.Authorization = `Basic ${Buffer.from(he.encode(username || '') + ':' + he.encode(password || '')).toString('base64')}`;
257
+ }
258
+
259
+ parsed.searchParams.set('account', account);
260
+ parsed.searchParams.set('proto', proto);
261
+
262
+ let authResponse = await fetch(parsed.toString(), { method: 'GET', headers });
263
+ if (!authResponse.ok) {
264
+ throw new Error(`Invalid response: ${authResponse.status} ${authResponse.statusText}`);
265
+ }
266
+
267
+ let authData = await authResponse.json();
268
+ if (!authData) {
269
+ throw new Error('Failed resolving credentials for ' + account);
270
+ }
271
+
272
+ const schema = Joi.object({
273
+ user: Joi.string().max(256).required(),
274
+ pass: Joi.string().allow('').max(256),
275
+ accessToken: Joi.string().max(4096)
276
+ }).xor('pass', 'accessToken');
277
+
278
+ const { error, value } = schema.validate(authData, {
279
+ abortEarly: false,
280
+ stripUnknown: true,
281
+ convert: true
282
+ });
283
+
284
+ if (error) {
285
+ throw error;
286
+ }
287
+
288
+ return value;
289
+ },
290
+
291
+ getDuration(val) {
292
+ val = (val || '').toString().replace(/^([\d.]+)\s*([smhdy][a-z]*)$/i, (o, num, m) => {
293
+ if (!num || isNaN(num)) {
294
+ return false;
295
+ }
296
+
297
+ num = Number(num);
298
+ if (!num) {
299
+ return num;
300
+ }
301
+
302
+ switch (m.charAt(0).toLowerCase()) {
303
+ case 's':
304
+ num = num * 1000;
305
+ break;
306
+ case 'm':
307
+ if (/^mo/i.test(m)) {
308
+ // month
309
+ num = num * (30 * 24 * 3600 * 1000);
310
+ } else {
311
+ // minute
312
+ num = num * (60 * 1000);
313
+ }
314
+ break;
315
+ case 'h':
316
+ num = num * (3600 * 1000);
317
+ break;
318
+ case 'd':
319
+ num = num * (24 * 3600 * 1000);
320
+ break;
321
+ case 'y':
322
+ num = num * (365 * 24 * 3600 * 1000);
323
+ break;
324
+ }
325
+
326
+ return Math.round(num);
327
+ });
328
+
329
+ if (isNaN(val)) {
330
+ return val;
331
+ }
332
+
333
+ return Number(val);
334
+ },
335
+
336
+ getByteSize(val) {
337
+ val = (val || '').toString().replace(/^([\d.]+)\s*([kMGTP])B?$/i, (o, num, m) => {
338
+ if (!num || isNaN(num)) {
339
+ return false;
340
+ }
341
+
342
+ num = Number(num);
343
+ if (!num) {
344
+ return num;
345
+ }
346
+
347
+ switch (m.toUpperCase()) {
348
+ case 'K':
349
+ num = num * 1024;
350
+ break;
351
+ case 'M':
352
+ num = num * 1024 * 1024;
353
+ break;
354
+ case 'G':
355
+ num = num * 1024 * 1024 * 1024;
356
+ break;
357
+ case 'T':
358
+ num = num * 1024 * 1024 * 1024 * 1024;
359
+ break;
360
+ case 'P':
361
+ num = num * 1024 * 1024 * 1024 * 1024 * 1024;
362
+ break;
363
+ }
364
+
365
+ return Math.round(num);
366
+ });
367
+
368
+ if (isNaN(val)) {
369
+ return val;
370
+ }
371
+
372
+ return Number(val);
373
+ },
374
+
375
+ formatAccountListingResponse(entry) {
376
+ if (Array.isArray(entry)) {
377
+ let obj = {};
378
+ for (let i = 0; i < entry.length; i += 2) {
379
+ obj[entry[i]] = entry[i + 1];
380
+ }
381
+ return obj;
382
+ }
383
+ // return default
384
+ return entry;
385
+ },
386
+
387
+ async getCounterValues(redis, seconds) {
388
+ seconds = Number(seconds) || 3600;
389
+
390
+ let now = new Date();
391
+ let startTime = new Date(now.getTime() - seconds * 1000);
392
+
393
+ let hashKeys = [];
394
+
395
+ // find out all the date buckets we need to check for
396
+ let endDateStr = `${now
397
+ .toISOString()
398
+ .substr(0, 10)
399
+ .replace(/[^0-9]+/g, '')}`;
400
+ let dateStr = '00000000';
401
+ let hashTime = startTime;
402
+
403
+ let startTimeStr = `${startTime
404
+ .toISOString()
405
+ // bucket includes 1 minute
406
+ .substr(0, 16)
407
+ .replace(/[^0-9]+/g, '')}`;
408
+
409
+ while (dateStr < endDateStr) {
410
+ dateStr = `${hashTime
411
+ .toISOString()
412
+ .substr(0, 10)
413
+ .replace(/[^0-9]+/g, '')}`;
414
+ hashKeys.push(dateStr);
415
+ hashTime = new Date(hashTime.getTime() + 24 * 3600 * 1000);
416
+ }
417
+
418
+ // list potential counter keys
419
+ let statUpdateKeys = await redis.smembers('stats:keys');
420
+
421
+ let req = redis.multi();
422
+ let rIndex = [];
423
+
424
+ for (let statUpdateKey of statUpdateKeys) {
425
+ // load stats for this key
426
+ for (let dateStr of hashKeys) {
427
+ req = req.hgetall(`stats:${statUpdateKey}:${dateStr}`);
428
+ rIndex.push(statUpdateKey);
429
+ }
430
+ }
431
+
432
+ let res = await req.exec();
433
+
434
+ let counters = {};
435
+
436
+ for (let i = 0; i < res.length; i++) {
437
+ let value = res[i];
438
+ let statUpdateKey = rIndex[i];
439
+
440
+ if (value[0]) {
441
+ // error found
442
+ } else {
443
+ Object.keys(value[1] || {}).forEach(key => {
444
+ if (key >= startTimeStr) {
445
+ if (!counters[statUpdateKey]) {
446
+ counters[statUpdateKey] = 0;
447
+ }
448
+ counters[statUpdateKey] += Number(value[1][key]) || 0;
449
+ }
450
+ });
451
+ }
452
+ }
453
+
454
+ return counters;
455
+ },
456
+
457
+ getAuthSettings(authStr) {
458
+ if (!authStr || typeof authStr !== 'string') {
459
+ return { enabled: false };
460
+ }
461
+ let sep = authStr.indexOf(':');
462
+ let user = authStr.substr(0, sep).trim();
463
+ let pass = authStr.substr(sep + 1);
464
+ if (!user || !pass) {
465
+ return { enabled: false };
466
+ }
467
+ return { enabled: true, user, pass };
468
+ },
469
+
470
+ escapeRegExp(string) {
471
+ return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
472
+ },
473
+
474
+ getRendezvousScore(key, shardId) {
475
+ const alg = 'md5';
476
+ return crypto
477
+ .createHash(alg)
478
+ .update((shardId || '').toString())
479
+ .update('\x00')
480
+ .update(key)
481
+ .digest()
482
+ .readUInt32LE(0, true);
483
+ },
484
+
485
+ selectRendezvousNode(key, workers) {
486
+ return workers.map(worker => ({ worker, score: module.exports.getRendezvousScore(key, worker.threadId) })).sort((a, b) => b - a)[0].worker;
487
+ }
488
+ };
package/package.json ADDED
@@ -0,0 +1,79 @@
1
+ {
2
+ "name": "emailengine-app",
3
+ "version": "1.14.0",
4
+ "description": "Email Sync Engine",
5
+ "main": "server.js",
6
+ "scripts": {
7
+ "start": "node server.js",
8
+ "test": "grunt",
9
+ "swagger": "./getswagger.sh"
10
+ },
11
+ "keywords": [
12
+ "IMAP"
13
+ ],
14
+ "author": "Postal Systems OU",
15
+ "license": "AGPL-3.0-or-later",
16
+ "bin": {
17
+ "emailengine": "bin/emailengine.js"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/postalsys/emailengine.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/postalsys/emailengine/issues"
25
+ },
26
+ "homepage": "https://emailengine.app/",
27
+ "dependencies": {
28
+ "@hapi/basic": "6.0.0",
29
+ "@hapi/boom": "9.1.4",
30
+ "@hapi/hapi": "20.1.5",
31
+ "@hapi/inert": "6.0.4",
32
+ "@hapi/vision": "6.1.0",
33
+ "bull": "3.29.2",
34
+ "bull-arena": "3.29.3",
35
+ "dotenv": "10.0.0",
36
+ "email-reply-parser": "1.2.6",
37
+ "exponential-backoff": "3.1.0",
38
+ "express": "4.17.1",
39
+ "google-auth-library": "7.9.2",
40
+ "hapi-pino": "8.3.0",
41
+ "hapi-swagger": "14.2.4",
42
+ "he": "1.2.0",
43
+ "html-to-text": "8.0.0",
44
+ "imapflow": "1.0.68",
45
+ "ioredis": "4.27.9",
46
+ "joi": "17.4.2",
47
+ "jquery": "3.6.0",
48
+ "libmime": "5.0.0",
49
+ "linkifyjs": "2.1.9",
50
+ "mailparser": "3.3.1",
51
+ "mailsplit": "5.2.0",
52
+ "msgpack5": "5.3.2",
53
+ "node-fetch": "2.6.1",
54
+ "node-vault": "0.9.22",
55
+ "nodemailer": "6.6.3",
56
+ "pino": "6.13.2",
57
+ "prom-client": "14.0.0",
58
+ "punycode": "2.1.1",
59
+ "smtp-server": "3.9.0",
60
+ "uuid": "8.3.2",
61
+ "wild-config": "1.5.1"
62
+ },
63
+ "devDependencies": {
64
+ "eslint": "7.32.0",
65
+ "eslint-config-nodemailer": "1.2.0",
66
+ "eslint-config-prettier": "8.3.0",
67
+ "grunt": "1.4.1",
68
+ "grunt-cli": "1.4.3",
69
+ "grunt-eslint": "23.0.0"
70
+ },
71
+ "pkg": {
72
+ "scripts": "workers/**/*.js",
73
+ "assets": [
74
+ "node_modules/@hapi/teamwork/lib/index.js",
75
+ "views/**/*",
76
+ "static/**/*"
77
+ ]
78
+ }
79
+ }
package/scan.js ADDED
@@ -0,0 +1,111 @@
1
+ /* eslint no-console: 0, id-length: 0, no-plusplus: 0 */
2
+
3
+ 'use strict';
4
+
5
+ const config = require('wild-config');
6
+ const Redis = require('ioredis');
7
+
8
+ config.dbs = config.dbs || {
9
+ redis: 'redis://127.0.0.1:6379/8'
10
+ };
11
+
12
+ const REDIS_CONF = process.env.EENGINE_REDIS || config.dbs.redis;
13
+
14
+ const redis = new Redis(REDIS_CONF);
15
+
16
+ const MAX_PAGE = 1000;
17
+
18
+ let keymap = new Map();
19
+ let countKeys = 0;
20
+
21
+ const scanKeys = () => {
22
+ console.log('KEY,COUNT');
23
+ return new Promise((resolve, reject) => {
24
+ const stream = redis.scanStream({
25
+ count: MAX_PAGE
26
+ });
27
+
28
+ let reading = false;
29
+ let finished = false;
30
+
31
+ const countKey = str => {
32
+ if (str.match(/^bull:/)) {
33
+ str = str.replace(/:[^:]+$/, ':*');
34
+ } else if (/^ia[mlhrq]:/.test(str)) {
35
+ str = str.replace(/:[^:]+$/, ':*');
36
+ } else {
37
+ str = str
38
+ .replace(/\b[a-f0-9]{16,}\b/g, m => `hash(${m.length})`)
39
+ .replace(/\b[0-9]{6,}\b/g, m => `num(${m.length})`)
40
+ .replace(/\b[a-f0-9]{8,}\b/g, m => `hash(${m.length})`)
41
+ .replace(/[a-z0-9._-]+@[a-z0-9.-]+/g, () => 'email()')
42
+ .replace(/\d{4}\/\d{2}\/\d{2}/g, () => `date(yyyy/mm/dd)`);
43
+ }
44
+ if (keymap.has(str)) {
45
+ keymap.set(str, keymap.get(str) + 1);
46
+ } else {
47
+ keymap.set(str, 1);
48
+ }
49
+ };
50
+
51
+ const readFromStream = async () => {
52
+ let resultKeys;
53
+
54
+ while ((resultKeys = stream.read()) !== null) {
55
+ for (let i = 0; i < resultKeys.length; i++) {
56
+ countKey(resultKeys[i]);
57
+ countKeys++;
58
+ }
59
+ }
60
+ };
61
+
62
+ const finish = async () => {
63
+ for (let [key, val] of keymap) {
64
+ console.log(`"${key.replace(/"/g, '""')}",${val}`);
65
+ }
66
+
67
+ console.error(`Checked ${countKeys} keys`);
68
+ resolve();
69
+ };
70
+
71
+ stream.on('readable', () => {
72
+ if (!reading) {
73
+ reading = true;
74
+ readFromStream().then(() => {
75
+ reading = false;
76
+
77
+ if (finished) {
78
+ finish();
79
+ }
80
+ });
81
+ }
82
+ });
83
+
84
+ stream.on('end', () => {
85
+ finished = true;
86
+ if (reading) {
87
+ return;
88
+ }
89
+ finish();
90
+ });
91
+
92
+ stream.on('error', err => {
93
+ reject(err);
94
+ });
95
+ });
96
+ };
97
+
98
+ let start = Date.now();
99
+
100
+ let interval = setInterval(() => {
101
+ console.error(`Key size: ${keymap.size}; Counted: ${countKeys}`);
102
+ }, 10 * 1000);
103
+
104
+ scanKeys()
105
+ .then(() => console.error('done'))
106
+ .catch(err => console.error(err))
107
+ .finally(() => {
108
+ clearInterval(interval);
109
+ console.error(`Time: ${Date.now() - start}ms`);
110
+ process.exit();
111
+ });