home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / webmail1.zip / webmail2.cmd < prev   
OS/2 REXX Batch file  |  2000-02-18  |  13KB  |  392 lines

  1. /* WebMail/2 v1.0 by sehh <sehh@altered.com> */
  2. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  3. call SysLoadFuncs
  4. call RxFuncAdd 'SockLoadFuncs','RxSock','SockLoadFuncs'
  5. call SockLoadFuncs
  6. /*
  7. call RxFuncAdd 'MD5LoadFuncs','rxmd5','MD5LoadFuncs'
  8. call MD5LoadFuncs
  9. */
  10. Globals.!myinfo = "WebMail/2 v1.0 beta 1"
  11. tmp = value("QUERY_STRING",,'os2environment')
  12. parse value tmp with Globals.!stat1 "&" Globals.!user "&" Globals.!pazz "&" Globals.!stat2
  13. Globals.!mail.0 = 0
  14. Globals.!totalmail = 0
  15. Globals.!myself = value("SCRIPT_NAME",,'os2environment')
  16. if pos(Globals.!myself,value("HTTP_REFERER",,'os2environment'))<1 then call UserLoginScreen
  17. else if pos("login=",Globals.!stat1)>0 & Globals.!stat2="" then call CheckLogon("checkemail")
  18. else if pos("read",Globals.!stat1)>0 & Globals.!stat2<>"" then call CheckLogon("read")
  19. else if pos("delete",Globals.!stat1)>0 & Globals.!stat2<>"" then call CheckLogon("delete")
  20. else call UserLoginScreen
  21. return
  22.  
  23. CheckLogon: procedure expose Globals.
  24. ConfigFile = value("ETC",,'os2environment')||"\webmail2.cfg"
  25. i = stream(ConfigFile, 'c', 'open')
  26. if i == "" then call CGIErrorScreen("Configuration error")
  27. i=0
  28. do while lines(ConfigFile)>0
  29.  tmp = linein(ConfigFile)
  30.  if substr(tmp,1,1)<>"#" & tmp<>"" & tmp<>'0d0a'x then do
  31.   i=i+1
  32.   Globals.!data.i=tmp
  33.  end
  34. end
  35. call stream ConfigFile, 'c', 'close'
  36. Globals.!data.0=i
  37. Globals.!user = substr(Globals.!user,3,length(Globals.!user)-2)
  38. Globals.!pazz = substr(Globals.!pazz,3,length(Globals.!pazz)-2)
  39. do i=1 to Globals.!data.0
  40.  parse value Globals.!data.i with Globals.!serv ":" Globals.!port ":" username ":" password
  41.  if Globals.!user = username & Globals.!pazz = password then do
  42.   if ARG(1) = "checkemail" then call CheckEmail
  43.   else if ARG(1) = "read" then call ReceiveEmail
  44.   else if ARG(1) = "delete" then call DeleteEmail
  45.  end
  46. end
  47. call SysSleep 6
  48. call ServerErrorScreen("Login refused")
  49. return
  50.  
  51. ReceiveEmail: procedure expose Globals.
  52. rc = SockGetHostByName(Globals.!serv,"host.!")
  53. if (rc = 0) then call CGIErrorScreen("Error in hostname/ip resolv")
  54. server = host.!addr;
  55. port = Globals.!port
  56. socket  = SockSocket("AF_INET","SOCK_STREAM",0)
  57. if (socket = -1) then call CGIErrorScreen("Error open socket")
  58. server.!family = "AF_INET"
  59. server.!port   = port
  60. server.!addr   = server
  61. rc = SockConnect(socket,"server.!")
  62. if (rc = -1) then call CGIErrorScreen("Error on Socket/Port Connection")
  63. call ReceiveData
  64. if substr(newData,1,3)<>"+OK" then call ServerErrorScreen("Server refused connection")
  65. call SendData("USER "||Globals.!user||'0d0a'x)
  66. call ReceiveData
  67. if substr(newData,1,3)<>"+OK" then do
  68.  call SendData("QUIT"||'0d0a'x)
  69.  call ServerErrorScreen("Server refused user login")
  70. end
  71. call SendData("PASS "||Globals.!pazz||'0d0a'x)
  72. call ReceiveData
  73. if substr(newData,1,3)<>"+OK" then do
  74.  call SendData("QUIT"||'0d0a'x)
  75.  call ServerErrorScreen("Failed login due to password error")
  76. end
  77. call SendData("RETR "||Globals.!stat2||'0d0a'x)
  78. call ReceiveData
  79. if substr(newData,1,3)<>"+OK" then do
  80.  call SendData("QUIT"||'0d0a'x)
  81.  call ServerErrorScreen("Server couldn't retrieve email")
  82. end
  83. call ReceiveData
  84. call SendData("QUIT"||'0d0a'x)
  85. rc = SockSoClose(socket)
  86. if (rc = -1) then call CGIErrorScreen("Error on SockSoClose")
  87. call ReadEmailScreen(newData)
  88. return
  89.  
  90. DeleteEmail: procedure expose Globals.
  91. rc = SockGetHostByName(Globals.!serv,"host.!")
  92. if (rc = 0) then call CGIErrorScreen("Error in hostname/ip resolv")
  93. server = host.!addr;
  94. port = Globals.!port
  95. socket  = SockSocket("AF_INET","SOCK_STREAM",0)
  96. if (socket = -1) then call CGIErrorScreen("Error open socket")
  97. server.!family = "AF_INET"
  98. server.!port   = port
  99. server.!addr   = server
  100. rc = SockConnect(socket,"server.!")
  101. if (rc = -1) then call CGIErrorScreen("Error on Socket/Port Connection")
  102. call ReceiveData
  103. if substr(newData,1,3)<>"+OK" then call ServerErrorScreen("Server refused connection")
  104. call SendData("USER "||Globals.!user||'0d0a'x)
  105. call ReceiveData
  106. if substr(newData,1,3)<>"+OK" then do
  107.  call SendData("QUIT"||'0d0a'x)
  108.  call ServerErrorScreen("Server refused user login")
  109. end
  110. call SendData("PASS "||Globals.!pazz||'0d0a'x)
  111. call ReceiveData
  112. if substr(newData,1,3)<>"+OK" then do
  113.  call SendData("QUIT"||'0d0a'x)
  114.  call ServerErrorScreen("Failed login due to password error")
  115. end
  116. if pos("all",Globals.!stat2)>0 then do
  117.  call SendData("STAT"||'0d0a'x)
  118.  call ReceiveData
  119.  if substr(newData,1,3)<>"+OK" then do
  120.   call SendData("QUIT"||'0d0a'x)
  121.   call ServerErrorScreen("Email server couldn't get status")
  122.  end
  123.  parse value newData with . Globals.!totalmail .
  124.  if datatype(Globals.!totalmail,"N")<>1 then do
  125.   call SendData("QUIT"||'0d0a'x)
  126.   call ServerErrorScreen("Server returned wrong value")
  127.  end
  128.  do i=1 to Globals.!totalmail
  129.   call SendData("DELE "||i||'0d0a'x)
  130.   call ReceiveData
  131.   if substr(newData,1,3)<>"+OK" then do
  132.    call SendData("QUIT"||'0d0a'x)
  133.    call ServerErrorScreen("Server couldn't delete email<br> or email already deleted")
  134.   end
  135.  end
  136. end; else do
  137.  call SendData("DELE "||Globals.!stat2||'0d0a'x)
  138.  call ReceiveData
  139.  if substr(newData,1,3)<>"+OK" then do
  140.   call SendData("QUIT"||'0d0a'x)
  141.   call ServerErrorScreen("Server couldn't delete email<br> or email already deleted")
  142.  end
  143. end
  144. call SendData("QUIT"||'0d0a'x)
  145. rc = SockSoClose(socket)
  146. if (rc = -1) then call CGIErrorScreen("Error on SockSoClose")
  147. call CheckEmail
  148. return
  149.  
  150. CheckEmail: procedure expose Globals.
  151. rc = SockGetHostByName(Globals.!serv,"host.!")
  152. if (rc = 0) then call CGIErrorScreen("Error in hostname/ip resolv")
  153. server = host.!addr;
  154. port = Globals.!port
  155. socket  = SockSocket("AF_INET","SOCK_STREAM",0)
  156. if (socket = -1) then call CGIErrorScreen("Error open socket")
  157. server.!family = "AF_INET"
  158. server.!port   = port
  159. server.!addr   = server
  160. rc = SockConnect(socket,"server.!")
  161. if (rc = -1) then call CGIErrorScreen("Error on Socket/Port Connection"||port)
  162. call ReceiveData
  163. if substr(newData,1,3)<>"+OK" then call ServerErrorScreen("Server refused connection")
  164. call SendData("USER "||Globals.!user||'0d0a'x)
  165. call ReceiveData
  166. if substr(newData,1,3)<>"+OK" then do
  167.  call SendData("QUIT"||'0d0a'x)
  168.  call ServerErrorScreen("Server refused user login")
  169. end
  170. call SendData("PASS "||Globals.!pazz||'0d0a'x)
  171. call ReceiveData
  172. if substr(newData,1,3)<>"+OK" then do
  173.  call SendData("QUIT"||'0d0a'x)
  174.  call ServerErrorScreen("Failed login due to password error")
  175. end
  176. call SendData("STAT"||'0d0a'x)
  177. call ReceiveData
  178. if substr(newData,1,3)<>"+OK" then do
  179.  call SendData("QUIT"||'0d0a'x)
  180.  call ServerErrorScreen("Email server couldn't get status")
  181. end
  182. parse value newData with . Globals.!totalmail .
  183. if datatype(Globals.!totalmail,"N")<>1 then do
  184.  call SendData("QUIT"||'0d0a'x)
  185.  call ServerErrorScreen("Server returned wrong value")
  186. end
  187. do i=1 to Globals.!totalmail
  188.  call SendData("TOP "||i||" 1"||'0d0a'x)
  189.  call ReceiveData
  190.  if substr(newData,1,3)="+OK" then do
  191.   call ReceiveData
  192.   Globals.!mail.i = newData
  193.  end
  194. end
  195. Globals.!mail.0 = i
  196. ListMail.0 = 0
  197. do i=1 to Globals.!totalmail
  198.  tmp1 = pos("From:",Globals.!mail.i)+6
  199.  tmp2 = pos('0d0a'x,Globals.!mail.i,tmp1)
  200.  tmp3 = space(substr(Globals.!mail.i,tmp1,tmp2-tmp1))
  201.  ListMail.i.!From = space(substr(tmp3,1,pos("<",tmp3)-1)||"("||substr(tmp3,pos("<",tmp3)+1,(lastpos(">",tmp3))-(pos("<",tmp3)+1))||")")
  202.  tmp1 = pos("Subject:",Globals.!mail.i)+9
  203.  tmp2 = pos('0d0a'x,Globals.!mail.i,tmp1)
  204.  ListMail.i.!Subject = space(substr(Globals.!mail.i,tmp1,tmp2-tmp1))
  205.  tmp1 = pos("Date:",Globals.!mail.i)+6
  206.  tmp2 = pos('0d0a'x,Globals.!mail.i,tmp1)
  207.  ListMail.i.!Date = space(substr(Globals.!mail.i,tmp1,16))
  208. end
  209. call SendData("QUIT"||'0d0a'x)
  210. rc = SockSoClose(socket)
  211. if (rc = -1) then call CGIErrorScreen("Error on SockSoClose")
  212. call CheckScreen
  213. return
  214.  
  215. SendData: procedure expose socket
  216. rc = SockSend(socket,ARG(1))
  217. if (rc = -1) then call CGIErrorScreen("Error on SockSend")
  218. return
  219.  
  220. ReceiveData: procedure expose socket newData
  221. length = ARG(1)
  222. if length = "" then length = 1024
  223. rc = SockRecv(socket,"newData",length)
  224. if (rc = -1) then call CGIErrorScreen("Error on SockRecv")
  225. return
  226.  
  227. UserLoginScreen: procedure expose Globals.
  228. say "Content-type: text/html"
  229. say ""
  230. say "<html>"
  231. say "<title> "||Globals.!myinfo||" created by sehh </title>"
  232. say "<body text=""#ffffff"" bgcolor=""#000000"" link=""#00BFFF"" vlink=""#00BFFF"">"
  233. say "<center>"
  234. say "<br><p><br>"
  235. say "<b>Welcome to WebMail/2</b><br>"
  236. say "<br><p><br>"
  237. say "<pre>User Login"
  238. say "<form action="""||Globals.!myself||""" method=get>"
  239. say "<input type=""Hidden"" name=""login"" value=""1"">"
  240. say "login: <INPUT type=text value="""" framewidth=4 name=l size=8>  password: <INPUT type=password value="""" framewidth=4 name=p size=8><br>"
  241. say " "
  242. say "<input type=submit value=""Login"">"
  243. say "</form>"
  244. say "</pre>"
  245. say "<br><p><br>"
  246. say "</center>"
  247. say "</body>"
  248. say "</html>"
  249. return
  250.  
  251. CheckScreen: procedure expose Globals. ListMail.
  252. say "Content-type: text/html"
  253. say ""
  254. say "<html>"
  255. say "<title> "||Globals.!myinfo||" </title>"
  256. say "<body text=""#ffffff"" bgcolor=""#000000"" link=""#00BFFF"" vlink=""#00BFFF"">"
  257. say "<center>"
  258. say "<br><p><br>"
  259. say "<b>Email account for "||Globals.!user||"</b><br>"
  260. say "<br><p><br>"
  261. if Globals.!totalmail > 0 then do
  262.  say "<table Nowrap Border=5 Align=Center>"
  263.  say "<caption><nobr><b>Email Listing</b></nobr></caption>"
  264.  say "<Tr bgcolor=""#000000"" text=""ffffff"">"
  265.  say "<Td Align=Center>"
  266.  say "Actions"
  267.  say "<Td Align=Center>"
  268.  say "From"
  269.  say "<Td Align=Center>"
  270.  say "Subject"
  271.  say "<Td Align=Center>"
  272.  say "Date"
  273.  say "</Tr>"
  274.  do i=1 to Globals.!totalmail
  275.   say "<Tr bgcolor=""#000000"" text=""ffffff"">"
  276.   say "<Td Align=Center>"
  277.   say "<a href="""||Globals.!myself||"?read&l="||Globals.!user||"&p="||Globals.!pazz||"&"||i||""">READ</a> - <a href="""||Globals.!myself||"?delete&l="||Globals.!user||"&p="||Globals.!pazz||"&"||i||""">DELETE</a>"
  278.   say "<Td Align=Center>"
  279.   say ListMail.i.!From
  280.   say "<Td Align=Center>"
  281.   say ListMail.i.!Subject
  282.   say "<Td Align=Center>"
  283.   say ListMail.i.!Date
  284.   say "</Tr>"
  285.  end
  286.  say "</table>"
  287. end; else do
  288.  say "<b>No mail</b><br>"
  289. end
  290. say "<br><p><br>"
  291. say "Options<br>"
  292. say "<br>"
  293. if Globals.!totalmail > 0 then say "<a href="""||Globals.!myself||"?delete&l="||Globals.!user||"&p="||Globals.!pazz||"&all"">Delete all mail</a><br>"
  294. say "<a href="""||Globals.!myself||""">LogOff</a><br>"
  295. say "<br><p><br>"
  296. say "</center>"
  297. say "</body>"
  298. say "</html>"
  299. exit
  300. return
  301.  
  302. ReadEmailScreen: procedure expose Globals.
  303. i=1
  304. tmp1 = pos("From:",ARG(1))+6
  305. tmp2 = pos('0d0a'x,ARG(1),tmp1)
  306. tmp3 = space(substr(ARG(1),tmp1,tmp2-tmp1))
  307. ListMail.i.!From = space(substr(tmp3,1,pos("<",tmp3)-1)||"("||substr(tmp3,pos("<",tmp3)+1,(lastpos(">",tmp3))-(pos("<",tmp3)+1))||")")
  308. tmp1 = pos("Subject:",ARG(1))+9
  309. tmp2 = pos('0d0a'x,ARG(1),tmp1)
  310. ListMail.i.!Subject = space(substr(ARG(1),tmp1,tmp2-tmp1))
  311. tmp1 = pos("Date:",ARG(1))+6
  312. tmp2 = pos('0d0a'x,ARG(1),tmp1)
  313. ListMail.i.!Date = space(substr(ARG(1),tmp1,16))
  314. say "Content-type: text/html"
  315. say ""
  316. say "<html>"
  317. say "<title> "||Globals.!myinfo||" </title>"
  318. say "<body text=""#ffffff"" bgcolor=""#000000"" link=""#00BFFF"" vlink=""#00BFFF"">"
  319. say "<center>"
  320. say "<p>"
  321. say "<table Nowrap Border=5 Align=Center>"
  322. say "<caption><nobr><b>Email</b></nobr></caption>"
  323. say "<Tr bgcolor=""#000000"" text=""ffffff"">"
  324. say "<Td Align=Center>"
  325. say "Actions"
  326. say "<Td Align=Center>"
  327. say "From"
  328. say "<Td Align=Center>"
  329. say "Subject"
  330. say "<Td Align=Center>"
  331. say "Date"
  332. say "</Tr>"
  333. say "<Tr bgcolor=""#000000"" text=""ffffff"">"
  334. say "<Td Align=Center>"
  335. say "<a href="""||Globals.!myself||"?delete&l="||Globals.!user||"&p="||Globals.!pazz||"&"||i||""">DELETE</a>"
  336. say "<Td Align=Center>"
  337. say ListMail.i.!From
  338. say "<Td Align=Center>"
  339. say ListMail.i.!Subject
  340. say "<Td Align=Center>"
  341. say ListMail.i.!Date
  342. say "</Tr>"
  343. say "</table><br></center><pre>"
  344. say ARG(1)
  345. say "</pre><center><br><p><br>"
  346. say "Options<br>"
  347. say "<br>"
  348. say "<a href="""||value("HTTP_REFERER",,'os2environment')||""">Return to email listing</a><br>"
  349. say "<br><p><br>"
  350. say "</center>"
  351. say "</body>"
  352. say "</html>"
  353. exit
  354. return
  355.  
  356. ServerErrorScreen: procedure expose Globals.
  357. say "Content-type: text/html"
  358. say ""
  359. say "<html>"
  360. say "<title> "||Globals.!myinfo||" </title>"
  361. say "<body text=""#ffffff"" bgcolor=""#000000"" link=""#00BFFF"" vlink=""#00BFFF"">"
  362. say "<center>"
  363. say "<br><p><br>"
  364. say "<b>WebMail/2: An error occured:</b><br>"
  365. say "<p><b>"||ARG(1)||"</b><br>"
  366. say "<br><p><br>"
  367. say "</center>"
  368. say "</body>"
  369. say "</html>"
  370. exit
  371. return
  372.  
  373. CGIErrorScreen: procedure expose Globals.
  374. say "Content-type: text/html"
  375. say ""
  376. say "<html>"
  377. say "<title> "||Globals.!myinfo||" </title>"
  378. say "<body text=""#ffffff"" bgcolor=""#000000"" link=""#00BFFF"" vlink=""#00BFFF"">"
  379. say "<center>"
  380. say "<br><p><br>"
  381. say "<b>WebMail/2: There was an internal error,<br>"
  382. say "please contact your administrator.</b><br>"
  383. say "<br><p><br>"
  384. say "<b>"||ARG(1)||"</b><br>"
  385. say "<br><p><br>"
  386. say "</center>"
  387. say "</body>"
  388. say "</html>"
  389. exit
  390. return
  391.  
  392.