gui.lua 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. -- refactor these to some proper management thing
  2. selected_idxs = {
  3. messages = {},
  4. contacts = {},
  5. to = {},
  6. cc = {},
  7. bcc = {},
  8. }
  9. message_drafts = {}
  10. local theme
  11. if minetest.get_modpath("default") then
  12. theme = default.gui_bg .. default.gui_bg_img
  13. else
  14. theme = ""
  15. end
  16. mail.inbox_formspec = "size[8,9;]" .. theme .. [[
  17. button[6,0.10;2,0.5;new;New]
  18. button[6,0.95;2,0.5;read;Read]
  19. button[6,1.70;2,0.5;reply;Reply]
  20. button[6,2.45;2,0.5;replyall;Reply All]
  21. button[6,3.20;2,0.5;forward;Forward]
  22. button[6,3.95;2,0.5;delete;Delete]
  23. button[6,4.82;2,0.5;markread;Mark Read]
  24. button[6,5.55;2,0.5;markunread;Mark Unread]
  25. button[6,6.55;2,0.5;contacts;Contacts]
  26. button[6,7.40;2,0.5;about;About]
  27. button_exit[6,8.45;2,0.5;quit;Close]
  28. tablecolumns[color;text;text]
  29. table[0,0;5.75,9;messages;#999,From,Subject]]
  30. mail.contacts_formspec = "size[8,9;]" .. theme .. [[
  31. button[6,0.10;2,0.5;new;New]
  32. button[6,0.85;2,0.5;edit;Edit]
  33. button[6,1.60;2,0.5;delete;Delete]
  34. button[6,8.25;2,0.5;back;Back]
  35. tablecolumns[color;text;text]
  36. table[0,0;5.75,9;contacts;#999,Name,Note]]
  37. mail.select_contact_formspec = "size[8,9;]" .. theme .. [[
  38. tablecolumns[color;text;text]
  39. table[0,0;3.5,9;contacts;#999,Name,Note%s]
  40. button[3.55,2.00;1.75,0.5;toadd;→ Add]
  41. button[3.55,2.75;1.75,0.5;toremove;← Remove]
  42. button[3.55,6.00;1.75,0.5;ccadd;→ Add]
  43. button[3.55,6.75;1.75,0.5;ccremove;← Remove]
  44. tablecolumns[color;text;text]
  45. table[5.15,0.0;2.75,4.5;to;#999,TO:,Note%s]
  46. tablecolumns[color;text;text]
  47. table[5.15,4.6;2.75,4.5;cc;#999,CC:,Note%s]
  48. button[3.55,8.25;1.75,0.5;back;Back]
  49. ]]
  50. function mail.show_about(name)
  51. local formspec = [[
  52. size[8,5;]
  53. button[7.25,0;0.75,0.5;back;X]
  54. label[0,0;Mail]
  55. label[0,0.5;By cheapie]
  56. label[0,1;http://github.com/cheapie/mail]
  57. label[0,1.5;See LICENSE file for license information]
  58. label[0,2.5;NOTE: Communication using this system]
  59. label[0,3;is NOT guaranteed to be private!]
  60. label[0,3.5;Admins are able to view the messages]
  61. label[0,4;of any player.]
  62. ]] .. theme
  63. minetest.show_formspec(name, "mail:about", formspec)
  64. end
  65. function mail.show_inbox(name)
  66. local formspec = { mail.inbox_formspec }
  67. local messages = mail.getMessages(name)
  68. message_drafts[name] = nil
  69. if messages[1] then
  70. for _, message in ipairs(messages) do
  71. mail.ensure_new_format(message, name)
  72. if message.unread then
  73. if not mail.player_in_list(name, message.to) then
  74. formspec[#formspec + 1] = ",#FFD788"
  75. else
  76. formspec[#formspec + 1] = ",#FFD700"
  77. end
  78. else
  79. if not mail.player_in_list(name, message.to) then
  80. formspec[#formspec + 1] = ",#CCCCDD"
  81. else
  82. formspec[#formspec + 1] = ","
  83. end
  84. end
  85. formspec[#formspec + 1] = ","
  86. formspec[#formspec + 1] = minetest.formspec_escape(message.sender)
  87. formspec[#formspec + 1] = ","
  88. if message.subject ~= "" then
  89. if string.len(message.subject) > 30 then
  90. formspec[#formspec + 1] =
  91. minetest.formspec_escape(string.sub(message.subject, 1, 27))
  92. formspec[#formspec + 1] = "..."
  93. else
  94. formspec[#formspec + 1] = minetest.formspec_escape(message.subject)
  95. end
  96. else
  97. formspec[#formspec + 1] = "(No subject)"
  98. end
  99. end
  100. if selected_idxs.messages[name] then
  101. formspec[#formspec + 1] = ";"
  102. formspec[#formspec + 1] = tostring(selected_idxs.messages[name] + 1)
  103. end
  104. formspec[#formspec + 1] = "]"
  105. else
  106. formspec[#formspec + 1] = "]label[2.25,4.5;No mail]"
  107. end
  108. minetest.show_formspec(name, "mail:inbox", table.concat(formspec, ""))
  109. end
  110. function mail.show_contacts(name)
  111. local formspec = mail.contacts_formspec .. mail.compile_contact_list(name, selected_idxs.contacts[name])
  112. minetest.show_formspec(name, "mail:contacts", formspec)
  113. end
  114. function mail.show_edit_contact(name, contact_name, note, illegal_name_hint)
  115. local formspec = [[
  116. size[6,7]
  117. button[4,6.25;2,0.5;back;Back]
  118. field[0.25,0.5;4,1;name;Player name:;%s]
  119. textarea[0.25,1.6;4,6.25;note;Note:;%s]
  120. button[4,0.10;2,1;save;Save]
  121. ]]
  122. if illegal_name_hint == "collision" then
  123. formspec = formspec .. [[
  124. label[4,1;That name]
  125. label[4,1.5;is already in]
  126. label[4,2;your contacts.]
  127. ]]
  128. elseif illegal_name_hint == "empty" then
  129. formspec = formspec .. [[
  130. label[4,1;The contact]
  131. label[4,1.5;name cannot]
  132. label[4,2;be empty.]
  133. ]]
  134. end
  135. formspec = formspec .. theme
  136. formspec = string.format(formspec,
  137. minetest.formspec_escape(contact_name or ""),
  138. minetest.formspec_escape(note or ""))
  139. minetest.show_formspec(name, "mail:editcontact", formspec)
  140. end
  141. function mail.show_select_contact(name, to, cc)
  142. local formspec = mail.select_contact_formspec
  143. local contacts = mail.compile_contact_list(name, selected_idxs.contacts[name])
  144. -- compile lists
  145. if to then
  146. to = mail.compile_contact_list(name, selected_idxs.to[name], to)
  147. else
  148. to = ""
  149. end
  150. if cc then
  151. cc = mail.compile_contact_list(name, selected_idxs.cc[name], cc)
  152. else
  153. cc = ""
  154. end
  155. --[[if bcc then
  156. bcc = table.concat(mail.compile_contact_list(name, selected_idxs.bcc[name], bcc)
  157. else
  158. bcc = ""
  159. end]]--
  160. formspec = string.format(formspec, contacts, to, cc)--, bcc()
  161. minetest.show_formspec(name, "mail:selectcontact", formspec)
  162. end
  163. function mail.compile_contact_list(name, selected, playernames)
  164. -- TODO: refactor this - not just compiles *a* list, but *the* list for the contacts screen (too inflexible)
  165. local formspec = {}
  166. local contacts = mail.getContacts(name)
  167. if playernames == nil then
  168. local length = 0
  169. for k, contact, i, l in mail.pairsByKeys(contacts) do
  170. if i == 1 then length = l end
  171. formspec[#formspec + 1] = ","
  172. formspec[#formspec + 1] = ","
  173. formspec[#formspec + 1] = minetest.formspec_escape(contact.name)
  174. formspec[#formspec + 1] = ","
  175. local note = contact.note
  176. -- display an ellipsis if the note spans multiple lines
  177. local idx = string.find(note, '\n')
  178. if idx ~= nil then
  179. note = string.sub(note, 1, idx-1) .. ' ...'
  180. end
  181. formspec[#formspec + 1] = minetest.formspec_escape(note)
  182. if type(selected) == "string" then
  183. if string.lower(selected) == k then
  184. selected = i
  185. end
  186. end
  187. end
  188. if length > 0 then
  189. if selected and type(selected) == "number" then
  190. formspec[#formspec + 1] = ";"
  191. formspec[#formspec + 1] = tostring(selected + 1)
  192. end
  193. formspec[#formspec + 1] = "]"
  194. else
  195. formspec[#formspec + 1] = "]label[2,4.5;No contacts]"
  196. end
  197. else
  198. if type(playernames) == "string" then
  199. playernames = mail.parse_player_list(playernames)
  200. end
  201. for i,c in ipairs(playernames) do
  202. formspec[#formspec + 1] = ","
  203. formspec[#formspec + 1] = ","
  204. formspec[#formspec + 1] = minetest.formspec_escape(c)
  205. formspec[#formspec + 1] = ","
  206. if contacts[string.lower(c)] == nil then
  207. formspec[#formspec + 1] = ""
  208. else
  209. local note = contacts[string.lower(c)].note
  210. -- display an ellipsis if the note spans multiple lines
  211. local idx = string.find(note, '\n')
  212. if idx ~= nil then
  213. note = string.sub(note, 1, idx-1) .. ' ...'
  214. end
  215. formspec[#formspec + 1] = minetest.formspec_escape(note)
  216. end
  217. if not selected then
  218. if type(selected) == "string" then
  219. if string.lower(selected) == string.lower(c) then
  220. selected = i
  221. end
  222. end
  223. end
  224. end
  225. if #playernames > 0 and selected and type(selected) == "number" then
  226. formspec[#formspec + 1] = ";"
  227. formspec[#formspec + 1] = tostring(selected + 1)
  228. end
  229. formspec[#formspec + 1] = "]"
  230. end
  231. return table.concat(formspec, "")
  232. end
  233. function mail.show_message(name, msgnumber)
  234. local messages = mail.getMessages(name)
  235. local message = messages[msgnumber]
  236. local formspec = [[
  237. size[8,9]
  238. button[7.25,0;0.75,0.5;back;X]
  239. label[0,0;From: %s]
  240. label[0,0.4;To: %s]
  241. label[0,0.8;CC: %s]
  242. label[0,1.3;Subject: %s]
  243. textarea[0.25,1.8;8,7.8;body;;%s]
  244. button[0,8.5;2,1;reply;Reply]
  245. button[2,8.5;2,1;replyall;Reply All]
  246. button[4,8.5;2,1;forward;Forward]
  247. button[6,8.5;2,1;delete;Delete]
  248. ]] .. theme
  249. local from = minetest.formspec_escape(message.sender)
  250. local to = minetest.formspec_escape(message.to)
  251. local cc = minetest.formspec_escape(message.cc)
  252. local subject = minetest.formspec_escape(message.subject)
  253. local body = minetest.formspec_escape(message.body)
  254. formspec = string.format(formspec, from, to, cc, subject, body)
  255. if message.unread then
  256. message.unread = false
  257. mail.setMessages(name, messages)
  258. end
  259. minetest.show_formspec(name,"mail:message",formspec)
  260. end
  261. function mail.show_compose(name, defaultto, defaultsubj, defaultbody, defaultcc, defaultbcc)
  262. local formspec = [[
  263. size[8,9]
  264. button[0,0;1,1;tocontacts;To:]
  265. field[1.1,0.3;3.2,1;to;;%s]
  266. button[4,0;1,1;cccontacts;CC:]
  267. field[5.1,0.3;3.1,1;cc;;%s]
  268. button[4,0.75;1,1;bcccontacts;BCC:]
  269. field[5.1,1.05;3.1,1;bcc;;%s]
  270. field[0.25,2;8,1;subject;Subject:;%s]
  271. textarea[0.25,2.5;8,6;body;;%s]
  272. button[0.5,8.5;3,1;cancel;Cancel]
  273. button[4.5,8.5;3,1;send;Send]
  274. ]] .. theme
  275. defaultto = defaultto or ""
  276. defaultsubj = defaultsubj or ""
  277. defaultbody = defaultbody or ""
  278. defaultcc = defaultcc or ""
  279. defaultbcc = defaultbcc or ""
  280. formspec = string.format(formspec,
  281. minetest.formspec_escape(defaultto),
  282. minetest.formspec_escape(defaultcc),
  283. minetest.formspec_escape(defaultbcc),
  284. minetest.formspec_escape(defaultsubj),
  285. minetest.formspec_escape(defaultbody))
  286. minetest.show_formspec(name, "mail:compose", formspec)
  287. end
  288. function mail.reply(name, message)
  289. mail.ensure_new_format(message)
  290. local replyfooter = "Type your reply here.\n\n--Original message follows--\n" ..message.body
  291. mail.show_compose(name, message.sender, "Re: "..message.subject, replyfooter)
  292. end
  293. function mail.replyall(name, message)
  294. mail.ensure_new_format(message)
  295. local replyfooter = "Type your reply here.\n\n--Original message follows--\n" ..message.body
  296. -- new recipients are the sender plus the original recipients, minus ourselves
  297. local recipients = message.to or ""
  298. if message.sender ~= nil then
  299. recipients = message.sender .. ", " .. recipients
  300. end
  301. recipients = mail.parse_player_list(recipients)
  302. for k,v in pairs(recipients) do
  303. if v == name then
  304. table.remove(recipients, k)
  305. break
  306. end
  307. end
  308. recipients = mail.concat_player_list(recipients)
  309. -- new CC is old CC minus ourselves
  310. local cc = mail.parse_player_list(message.cc)
  311. for k,v in pairs(cc) do
  312. if v == name then
  313. table.remove(cc, k)
  314. break
  315. end
  316. end
  317. cc = mail.concat_player_list(cc)
  318. mail.show_compose(name, recipients, "Re: "..message.subject, replyfooter, cc)
  319. end
  320. function mail.forward(name, message)
  321. local fwfooter = "Type your message here.\n\n--Original message follows--\n" .. (message.body or "")
  322. mail.show_compose(name, "", "Fw: " .. (message.subject or ""), fwfooter)
  323. end
  324. function mail.handle_receivefields(player, formname, fields)
  325. if formname == "" and fields and fields.quit and minetest.get_modpath("unified_inventory") then
  326. unified_inventory.set_inventory_formspec(player, "craft")
  327. end
  328. if formname == "mail:about" then
  329. minetest.after(0.5, function()
  330. mail.show_inbox(player:get_player_name())
  331. end)
  332. elseif formname == "mail:inbox" then
  333. local name = player:get_player_name()
  334. local messages = mail.getMessages(name)
  335. if fields.messages then
  336. local evt = minetest.explode_table_event(fields.messages)
  337. selected_idxs.messages[name] = evt.row - 1
  338. if evt.type == "DCL" and messages[selected_idxs.messages[name]] then
  339. mail.show_message(name, selected_idxs.messages[name])
  340. end
  341. return true
  342. end
  343. if fields.read then
  344. if messages[selected_idxs.messages[name]] then
  345. mail.show_message(name, selected_idxs.messages[name])
  346. end
  347. elseif fields.delete then
  348. if messages[selected_idxs.messages[name]] then
  349. table.remove(messages, selected_idxs.messages[name])
  350. mail.setMessages(name, messages)
  351. end
  352. mail.show_inbox(name)
  353. elseif fields.reply and messages[selected_idxs.messages[name]] then
  354. local message = messages[selected_idxs.messages[name]]
  355. mail.reply(name, message)
  356. elseif fields.replyall and messages[selected_idxs.messages[name]] then
  357. local message = messages[selected_idxs.messages[name]]
  358. mail.replyall(name, message)
  359. elseif fields.forward and messages[selected_idxs.messages[name]] then
  360. local message = messages[selected_idxs.messages[name]]
  361. mail.forward(name, message)
  362. elseif fields.markread then
  363. if messages[selected_idxs.messages[name]] then
  364. messages[selected_idxs.messages[name]].unread = false
  365. -- set messages immediately, so it shows up already when updating the inbox
  366. mail.setMessages(name, messages)
  367. end
  368. mail.show_inbox(name)
  369. return true
  370. elseif fields.markunread then
  371. if messages[selected_idxs.messages[name]] then
  372. messages[selected_idxs.messages[name]].unread = true
  373. -- set messages immediately, so it shows up already when updating the inbox
  374. mail.setMessages(name, messages)
  375. end
  376. mail.show_inbox(name)
  377. return true
  378. elseif fields.new then
  379. mail.show_compose(name)
  380. elseif fields.contacts then
  381. mail.show_contacts(name)
  382. elseif fields.quit then
  383. if minetest.get_modpath("unified_inventory") then
  384. unified_inventory.set_inventory_formspec(player, "craft")
  385. end
  386. elseif fields.about then
  387. mail.show_about(name)
  388. end
  389. return true
  390. elseif formname == "mail:message" then
  391. local name = player:get_player_name()
  392. local messages = mail.getMessages(name)
  393. if fields.back then
  394. mail.show_inbox(name)
  395. return true -- don't uselessly set messages
  396. elseif fields.reply then
  397. local message = messages[selected_idxs.messages[name]]
  398. mail.reply(name, message)
  399. elseif fields.replyall then
  400. local message = messages[selected_idxs.messages[name]]
  401. mail.replyall(name, message)
  402. elseif fields.forward then
  403. local message = messages[selected_idxs.messages[name]]
  404. mail.forward(name, message.subject)
  405. elseif fields.delete then
  406. if messages[selected_idxs.messages[name]] then
  407. table.remove(messages,selected_idxs.messages[name])
  408. mail.setMessages(name, messages)
  409. end
  410. mail.show_inbox(name)
  411. end
  412. return true
  413. elseif formname == "mail:compose" then
  414. local name = player:get_player_name()
  415. if fields.send then
  416. mail.send({
  417. from = name,
  418. to = fields.to,
  419. cc = fields.cc,
  420. bcc = fields.bcc,
  421. subject = fields.subject,
  422. body = fields.body,
  423. })
  424. local contacts = mail.getContacts(name)
  425. local recipients = mail.parse_player_list(fields.to)
  426. local changed = false
  427. for _,v in pairs(recipients) do
  428. if contacts[string.lower(v)] == nil then
  429. contacts[string.lower(v)] = {
  430. name = v,
  431. note = "",
  432. }
  433. changed = true
  434. end
  435. end
  436. if changed then
  437. mail.setContacts(name, contacts)
  438. end
  439. minetest.after(0.5, function()
  440. mail.show_inbox(name)
  441. end)
  442. elseif fields.tocontacts or fields.cccontacts or fields.bcccontacts then
  443. message_drafts[name] = {
  444. to = fields.to,
  445. cc = fields.cc,
  446. bcc = fields.bcc,
  447. subject = fields.subject,
  448. body = fields.body,
  449. }
  450. mail.show_select_contact(name, fields.to, fields.cc, fields.bcc)
  451. elseif fields.cancel then
  452. message_drafts[name] = nil
  453. mail.show_inbox(name)
  454. end
  455. return true
  456. elseif formname == "mail:selectcontact" then
  457. local name = player:get_player_name()
  458. local contacts = mail.getContacts(name)
  459. local draft = message_drafts[name]
  460. -- get indexes for fields with selected rows
  461. -- execute their default button's actions if double clicked
  462. for k,action in pairs({
  463. contacts = "toadd",
  464. to = "toremove",
  465. cc = "ccremove",
  466. bcc = "bccremove"
  467. }) do
  468. if fields[k] then
  469. local evt = minetest.explode_table_event(fields[k])
  470. selected_idxs[k][name] = evt.row - 1
  471. if evt.type == "DCL" and selected_idxs[k][name] then
  472. fields[action] = true
  473. end
  474. return true
  475. end
  476. end
  477. local update = false
  478. -- add
  479. for _,v in pairs({"to","cc","bcc"}) do
  480. if fields[v.."add"] then
  481. update = true
  482. if selected_idxs.contacts[name] then
  483. for k, contact, i in mail.pairsByKeys(contacts) do
  484. if k == selected_idxs.contacts[name] or i == selected_idxs.contacts[name] then
  485. local list = mail.parse_player_list(draft[v])
  486. list[#list+1] = contact.name
  487. selected_idxs[v][name] = #list
  488. draft[v] = mail.concat_player_list(list)
  489. break
  490. end
  491. end
  492. end
  493. end
  494. end
  495. -- remove
  496. for _,v in pairs({"to","cc","bcc"}) do
  497. if fields[v.."remove"] then
  498. update = true
  499. if selected_idxs[v][name] then
  500. local list = mail.parse_player_list(draft[v])
  501. table.remove(list, selected_idxs[v][name])
  502. if #list < selected_idxs[v][name] then
  503. selected_idxs[v][name] = #list
  504. end
  505. draft[v] = mail.concat_player_list(list)
  506. end
  507. end
  508. end
  509. if update then
  510. mail.show_select_contact(name, draft.to, draft.cc, draft.bcc)
  511. return true
  512. end
  513. -- delete old idxs
  514. for _,v in ipairs({"contacts","to","cc","bcc"}) do
  515. selected_idxs[v][name] = nil
  516. end
  517. mail.show_compose(name, draft.to, draft.subject, draft.body, draft.cc, draft.bcc)
  518. return true
  519. elseif formname == "mail:contacts" then
  520. local name = player:get_player_name()
  521. local contacts = mail.getContacts(name)
  522. if fields.contacts then
  523. local evt = minetest.explode_table_event(fields.contacts)
  524. for k, _, i in mail.pairsByKeys(contacts) do
  525. if i == evt.row - 1 then
  526. selected_idxs.contacts[name] = k
  527. break
  528. end
  529. end
  530. if evt.type == "DCL" and contacts[selected_idxs.contacts[name]] then
  531. mail.show_edit_contact(
  532. name,
  533. contacts[selected_idxs.contacts[name]].name,
  534. contacts[selected_idxs.contacts[name]].note
  535. )
  536. end
  537. return true
  538. elseif fields.new then
  539. selected_idxs.contacts[name] = "#NEW#"
  540. mail.show_edit_contact(name, "", "")
  541. elseif fields.edit and selected_idxs.contacts[name] and contacts[selected_idxs.contacts[name]] then
  542. mail.show_edit_contact(
  543. name,
  544. contacts[selected_idxs.contacts[name]].name,
  545. contacts[selected_idxs.contacts[name]].note
  546. )
  547. elseif fields.delete then
  548. if contacts[selected_idxs.contacts[name]] then
  549. -- delete the contact and set the selected to the next in the list,
  550. -- except if it was the last. Then determine the new last
  551. local found = false
  552. local last = nil
  553. for k in mail.pairsByKeys(contacts) do
  554. if found then
  555. selected_idxs.contacts[name] = k
  556. break
  557. elseif k == selected_idxs.contacts[name] then
  558. contacts[selected_idxs.contacts[name]] = nil
  559. selected_idxs.contacts[name] = nil
  560. found = true
  561. else
  562. last = k
  563. end
  564. end
  565. if found and not selected_idxs.contacts[name] then
  566. -- was the last in the list, so take the previous (new last)
  567. selected_idxs.contacts[name] = last
  568. end
  569. mail.setContacts(name, contacts)
  570. end
  571. mail.show_contacts(name)
  572. elseif fields.back then
  573. mail.show_inbox(name)
  574. end
  575. elseif formname == "mail:editcontact" then
  576. local name = player:get_player_name()
  577. local contacts = mail.getContacts(name)
  578. if fields.save then
  579. if selected_idxs.contacts[name] and selected_idxs.contacts[name] ~= "#NEW#" then
  580. local contact = contacts[selected_idxs.contacts[name]]
  581. if selected_idxs.contacts[name] ~= string.lower(fields.name) then
  582. -- name changed!
  583. if #fields.name == 0 then
  584. mail.show_edit_contact(name, contact.name, fields.note, "empty")
  585. return true
  586. elseif contacts[string.lower(fields.name)] ~= nil then
  587. mail.show_edit_contact(name, contact.name, fields.note, "collision")
  588. return true
  589. else
  590. contacts[string.lower(fields.name)] = contact
  591. contacts[selected_idxs.contacts[name]] = nil
  592. end
  593. end
  594. contact.name = fields.name
  595. contact.note = fields.note
  596. else
  597. local contact = {
  598. name = fields.name,
  599. note = fields.note,
  600. }
  601. contacts[string.lower(contact.name)] = contact
  602. end
  603. mail.setContacts(name, contacts)
  604. mail.show_contacts(name)
  605. elseif fields.back then
  606. mail.show_contacts(name)
  607. end
  608. elseif fields.mail then
  609. mail.show_inbox(player:get_player_name())
  610. else
  611. return false
  612. end
  613. end
  614. minetest.register_on_player_receive_fields(mail.handle_receivefields)