home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Extra 1997 #5 / AmigaPlus_Extra-CD_5-97.iso / online-tools / mail / ems / rexx / areastat.ems < prev    next >
Text File  |  1993-08-27  |  10KB  |  474 lines

  1. /*
  2. ** AreaStat.ems by Adam Sjøgren 1993
  3. **
  4. ** and improved by Bernd Ernesti (Version 1.2a) (14-Aug-93)
  5. **
  6. ** Inspired by AreaStat.spot by Brian Jacobsen.
  7. **
  8. ** Set up i config/Externals like this:
  9. **
  10. **   Title="Area statistics" Command="AreaStat.ems %s %a" ScreenPos=DoNothing
  11. **
  12. */
  13. /***************************************/
  14. /* $VER: AreaStat.ems 1.2a (27.08.93)  */
  15. /***************************************/
  16.  
  17. script_version = 'v1.2a'
  18.  
  19.  
  20. options results
  21. signal on error
  22. signal on syntax
  23.  
  24. parse arg session_number current_area .
  25.  
  26. /* Variables: */
  27. fromlist. = 0
  28. tolist.   = 0
  29. subjlist. = 0
  30. names.    = 0
  31.  
  32. /* Make sure EMS is listening: */
  33. if( ~show( 'l', "ems_rexx.library" ) ) then
  34. do
  35.    if( ~addlib( "ems_rexx.library", 0, -30, 0 ) ) then
  36.    do
  37.       say "Could not open ems_rexx.library"
  38.       exit 10
  39.    end
  40. end
  41.  
  42.  
  43. /* Here comes the main program: */
  44.  
  45. if session_number ~= '' then
  46. do
  47.  
  48.    res.0 = 2
  49.    res.1 = 'Current Area'
  50.    res.2 = 'Choose from list'
  51.    sel   = 1
  52.    ok    = EMS_Do_Choice_Single( 'Process which area?', 'res', 'sel' ); if ok = 'CANCEL' then signal ScriptEnd
  53.  
  54. end
  55. else
  56. do
  57.  
  58.    sel = 2
  59.  
  60. end
  61.  
  62. if sel = 1 then
  63. do
  64.  
  65.    msgarea = current_area
  66.  
  67. end
  68. else
  69. do
  70.  
  71.    ok = EMS_Area_Select( 'msgarea' ); if ok = 'CANCEL' then signal ScriptEnd
  72.  
  73. end
  74.  
  75. call EMS_Area_Item_List( msgarea, 'MSG', 'itemlist' )
  76.  
  77. if itemlist.0 = 0 then
  78. do
  79.  
  80.    call EMS_Do_Request( 'The area is empty.', 'I knew that!' )
  81.    signal ScriptEnd
  82.  
  83. end
  84.  
  85. StartMsg = 1
  86. EndMsg   = itemlist.0
  87.  
  88. /* A full Messagebase, or just the last month? */
  89. res.0 = 4
  90. res.1 = 'Full Messagebase'
  91. res.2 = 'By the Real-Message-Number'
  92. res.3 = 'By month and year'
  93. res.4 = 'By the year'
  94. sel   = 1
  95. ok    = EMS_Do_Choice_Single( 'How long search for ?', 'res', 'sel' ); if ok = 'CANCEL' then signal ScriptEnd
  96.  
  97. smb   = 1
  98. month = ''
  99. year  = ''
  100.  
  101. if sel = 1 then month='#?'
  102.  
  103. if sel = 2 then
  104. do
  105.  
  106.    ok  = EMS_String_Select( 'Start Msg-Number ?', 'StartMsg' ); if StartMsg > itemlist.0 | StartMsg < 1        then signal ScriptEnd
  107.    ok  = EMS_String_Select( 'End Msg-Number ?'  , 'EndMsg'   ); if EndMsg   > itemlist.0 | EndMsg   < StartMsg then signal ScriptEnd
  108.    smb = 2
  109.  
  110. end
  111.  
  112. if sel = 3 then
  113. do
  114.  
  115.    ok = EMS_String_Select( 'Which Month (3 chars, e.g. JAN) ?', 'month' )
  116.  
  117.    month = left( compress( upper( month ) ), 3 )
  118.  
  119.    test = pos( month, 'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC' )
  120.  
  121.    if test ~= 0 & (test=1 | (test - 1)//3 = 0) then smb   = 3
  122.                                                else month = '#?'
  123. end
  124.  
  125. if sel = 3 | sel = 4 then
  126. do
  127.  
  128.    ok = EMS_String_Select( 'Which Year ?', 'year' )
  129.  
  130.    year = COMPRESS( year ); if year = '' | length( Year ) < 2 | DATATYPE( year ) ~= 'NUM' then
  131.    do
  132.  
  133.       month = '#?'
  134.       year  = '#?'
  135.       smb   = 1
  136.  
  137.    end
  138.    else
  139.    do
  140.  
  141.       year = right( year, 2 )
  142.  
  143.       if smb ~= 3 then
  144.       do
  145.          smb   = 4
  146.          month = ''
  147.       end
  148.  
  149.    end
  150.  
  151. end
  152.  
  153. /* A full stat's file, or just the first 10? */
  154. res.0 = 2
  155. res.1 = 'Top 10 only'
  156. res.2 = 'Full statistics'
  157. sel   = 1
  158. ok    = EMS_Do_Choice_Single( 'What kind of statistics?', 'res', 'sel' ); if ok = 'CANCEL' then signal ScriptEnd
  159.  
  160. if sel = 1 then
  161. do
  162.  
  163.    toptext = 'Top Ten'
  164.    top     = 1
  165.  
  166. end
  167. else
  168. do
  169.  
  170.    toptext = 'All'
  171.    top     = 0
  172.  
  173. end
  174.  
  175. /* How do you want the output? */
  176. output_area = ''
  177. output_file = ''
  178. res.1 = 'Send as a message'
  179. res.2 = 'To a file'
  180. sel   = 1
  181. ok    = EMS_Do_Choice_Single( 'Where do you want the chart?', 'res', 'sel' ); if ok = 'CANCEL' then signal ScriptEnd
  182.  
  183. if sel=1 then
  184. do
  185.  
  186.    res.1 = 'Current Area'
  187.    res.2 = 'Choose from list'
  188.    sel2  = 1
  189.    ok    = EMS_Do_Choice_Single( 'Post in what area?', 'res', 'sel2' ); if ok = 'CANCEL' then signal ScriptEnd
  190.  
  191.    if sel2 = 1 then
  192.    do
  193.  
  194.       output_area = current_area
  195.  
  196.    end
  197.    else
  198.    do
  199.  
  200.       ok = EMS_Area_Select( 'output_area' ); if ok='CANCEL' then signal ScriptEnd
  201.  
  202.    end
  203.  
  204. end
  205. else
  206. do
  207.  
  208.    output_file = ''
  209.    ok          = EMS_File_Select( 'Filename for chart:', 'output_file' )
  210.  
  211. end
  212.  
  213. TotalMsg = 0
  214. do i = StartMsg to EndMsg
  215.  
  216.    date = EMS_Area_Item_Header_Date_Created( msgarea, 'MSG', itemlist.i )
  217.    to   = EMS_Area_Item_Header_To(           msgarea, 'MSG', itemlist.i )
  218.    from = EMS_Area_Item_Header_From(         msgarea, 'MSG', itemlist.i )
  219.    subj = EMS_Area_Item_Header_Subject(      msgarea, 'MSG', itemlist.i ); subj = upper( strip( subj ) )
  220.  
  221.    if i = StartMsg then StartMsgDate = date
  222.    if i = EndMsg   then EndMsgDate   = date
  223.  
  224.    test = pos( month || year, compress( upper( date ) ) )
  225.  
  226.    if test = 0 & smb > 2 then iterate
  227.    if smb  = 4 & (test ~= 9 & test ~= 6) then iterate
  228.  
  229.    if left( subj, 4 ) = 'RE: ' then subj = substr( subj, 5 )
  230.  
  231.    call EMS_Add_To_Stem( 'names', to  , 'UNIQUE' )
  232.    call EMS_Add_To_Stem( 'names', from, 'UNIQUE' )
  233.    call EMS_Add_To_Stem( 'subjs', subj, 'UNIQUE' )
  234.  
  235.    tolist.to     = tolist.to     + 1
  236.    fromlist.from = fromlist.from + 1
  237.    subjlist.subj = subjlist.subj + 1
  238.    TotalMsg      = TotalMsg      + 1
  239.  
  240. end
  241.  
  242. if TotalMsg = 0 then
  243. do
  244.  
  245.    say 'No messages found !'
  246.    signal ScriptEnd
  247.  
  248. end
  249.  
  250. /* Make the TO: stem ready */
  251. do i = 1 to TotalMsg
  252.  
  253.    name = names.i
  254.  
  255.    if tolist.name > 0 then
  256.    do
  257.  
  258.       percent   = tolist.name * 100 / TotalMsg
  259.       remainder = percent // 1
  260.       percent   = percent  % 1
  261.  
  262.       if remainder > 0.5 then percent = percent + 1
  263.  
  264.       out_1 =            copies( ' ', (length( names.0 ) + 1) - length( tolist.name ) ) || tolist.name
  265.       out_2 = 'msgs(' || copies( ' ',                      2  - length( percent     ) ) || percent || '%) to' names.i
  266.  
  267.  
  268.       call EMS_Add_To_Stem( 'to_out', out_1 out_2 )
  269.  
  270.    end
  271.  
  272. end
  273.  
  274. call EMS_Sort_Stem( 'to_out' )
  275.  
  276.  
  277. /* The FROM: stem: */
  278. do i = 1 to TotalMsg
  279.  
  280.    name = names.i
  281.  
  282.    if fromlist.name > 0 then
  283.    do
  284.  
  285.       percent   = fromlist.name * 100 / TotalMsg
  286.       remainder = percent // 1
  287.       percent   = percent  % 1
  288.  
  289.       if remainder > 0.5 then percent = percent + 1
  290.  
  291.       out_1 =            copies( ' ', (length( names.0 ) + 1) - length( fromlist.name ) ) || fromlist.name
  292.       out_2 = 'msgs(' || copies( ' ',                      2  - length( percent     ) ) || percent || '%) from' names.i
  293.  
  294.  
  295.       call EMS_Add_To_Stem( 'from_out', out_1 out_2 )
  296.  
  297.    end
  298.  
  299. end
  300.  
  301. call EMS_Sort_Stem( 'from_out' )
  302.  
  303.  
  304. /* The SUBJ: stem: */
  305. do i = 1 to TotalMsg
  306.  
  307.    subj = subjs.i
  308.  
  309.    if subjlist.subj > 0 then
  310.    do
  311.  
  312.       percent   = subjlist.subj * 100 / TotalMsg
  313.       remainder = percent // 1
  314.       percent   = percent  % 1
  315.  
  316.       if remainder > 0.5 then percent = percent + 1
  317.  
  318.       out_1 =              copies( ' ', (length( subjs.0 ) + 1) - length( subjlist.subj ) ) || subjlist.subj
  319.       out_2 = 'topics(' || copies( ' ',                      2  - length( percent     ) ) || percent || '%) :' subjs.i
  320.  
  321.       call EMS_Add_To_Stem( 'subj_out', out_1 out_2 )
  322.  
  323.    end
  324.  
  325. end
  326.  
  327. call EMS_Sort_Stem( 'subj_out' )
  328.  
  329.  
  330. /* Do the output: */
  331. temp_file = 't:areastat.ems.' || session_number
  332. mline = '----------------------------------------------------------------------------'
  333.  
  334. call open( 'temp', temp_file, 'W' )
  335.  
  336. call writeln( 'temp', ''                            )
  337. call writeln( 'temp', 'Area-Statistics for' msgarea )
  338.  
  339. if smb = 1 then call writeln( 'temp', 'Full Messagebase with' TotalMsg 'messages')
  340. if smb = 2 then call writeln( 'temp', 'Msg #' || StartMsg '(' || StartMsgDate || ')' '-' '#' || EndMsg '(' || EndMsgDate || ')' 'with' TotalMsg 'msgs')
  341. if smb > 2 then
  342. do
  343.  
  344.    if smb = 3 then part = 'Month' month
  345.               else part = 'Year'
  346.  
  347.    if year > 77 then part = part '19' || year
  348.                 else part = part '20' || year
  349.  
  350.    call writeln( 'temp', 'Messagebase from' part 'with' TotalMsg 'messages' )
  351.  
  352. end
  353.  
  354. call writeln( 'temp', 'Statistics generated on  :' DATE() 'at' TIME() )
  355. call writeln( 'temp', 'Active users in this area:' names.0            )
  356. call writeln( 'temp', mline                                           )
  357. call writeln( 'temp', ''                                              )
  358.  
  359. if top = 1 then
  360. do
  361.  
  362.    low_f = (from_out.0) - 10; j_f = 10
  363.    low_t = (  to_out.0) - 10; j_t = 10
  364.    low_s = (subj_out.0) - 10; j_s = 10
  365.  
  366. end
  367. else
  368. do
  369.  
  370.    low_f = 0; j_f   = from_out.0
  371.    low_t = 0; j_t   =   to_out.0
  372.    low_s = 0; j_s   = subj_out.0
  373.  
  374. end
  375.  
  376. j = 0
  377. call writeln( 'temp', toptext 'Submitters:' )
  378. call writeln( 'temp', mline                 )
  379.  
  380. do i = from_out.0 to 1 by -1
  381.  
  382.    if i <= low_f then leave
  383.  
  384.    j = j + 1
  385.  
  386.    call writeln( 'temp', copies( ' ', length( j_f ) - length( j ) ) || j || '. ' || from_out.i )
  387. end
  388.  
  389. call writeln( 'temp', '' )
  390.  
  391.  
  392. j = 0
  393. call writeln( 'temp', toptext 'Recipiants:' )
  394. call writeln( 'temp', mline                 )
  395.  
  396. do i = to_out.0 to 1 by -1
  397.  
  398.    if i <= low_t then leave
  399.  
  400.    j = j + 1
  401.  
  402.    call writeln( 'temp', copies( ' ', length( j_t ) - length( j ) ) || j || '. ' || to_out.i )
  403.  
  404. end
  405.  
  406. call writeln( 'temp', '' )
  407.  
  408.  
  409. j = 0
  410. call writeln( 'temp', toptext 'Topics:' )
  411. call writeln( 'temp', mline             )
  412. do i = subj_out.0 to 1 by -1
  413.  
  414.    if i <= low_s then leave
  415.  
  416.    j = j + 1
  417.    call writeln( 'temp', copies( ' ', length( j_s ) - length( j ) ) || j || '. ' || subj_out.i )
  418.  
  419. end
  420.  
  421. call writeln( 'temp', '' )
  422.  
  423.  
  424. call close( 'temp' )
  425.  
  426. if output_file ~= '' then address command 'copy' temp_file output_file
  427.  
  428. if output_area ~= '' then
  429. do
  430.  
  431.    area_addr = EMS_Area_Address( output_area )
  432.  
  433.    call EMS_Item_Alloc(               'newmsg', output_area, 'MSG'       )
  434.    call EMS_Item_Header_From(         'newmsg', 'Statistics Generator'   )
  435.    call EMS_Item_Header_To(           'newmsg', 'All'                    )
  436.    call EMS_Item_Header_Subject(      'newmsg', 'Stats for area' msgarea )
  437.    call EMS_Item_Header_Address_From( 'newmsg', area_addr                )
  438.    call EMS_Item_Header_Address_To(   'newmsg', area_addr                )
  439.  
  440.    call EMS_Item_Line_Unique(        'newmsg', 'HEADER', '--- '       , '--- EMS AreaStat' script_version                                    )
  441.    call EMS_Item_Line_Unique(        'newmsg', 'HEADER', ' * Origin: ', ' * Origin: > EMS Area Statistics Generator < (' || area_addr || ')' )
  442.  
  443.    if exists( temp_file ) then call EMS_Item_File_Read( 'newmsg', 'text', temp_file )
  444.  
  445.    call EMS_Item_Write( 'newmsg' )
  446.    call EMS_Item_Free(  'newmsg' )
  447.  
  448. end
  449.  
  450. address command 'Delete >NIL:' temp_file
  451.  
  452. /*
  453.  * Tell EMS that we're finished playing:
  454.  */
  455. ScriptEnd:
  456.  
  457. call EMS_FreeScriptData()
  458. exit 0
  459.  
  460. /*
  461.  * Oh no! lAmEr, dEwD!
  462.  */
  463. error:
  464. syntax:
  465.  
  466. error_text = EMS_LastError()
  467.  
  468. if error_text = '' then error_text = rc ErrorText( rc )
  469.  
  470. say '| ***BREAK: error at' sigl error_text
  471.  
  472. call EMS_FreeScriptData()
  473. exit rc
  474.