send_one_group_spam_message

one of the documented procedures in this installation of the ACS
Usage:
send_one_group_spam_message   spam_id
What it does:
This procedure sends out a group spam message with the given spam_id, provided the spam is approved ( i.e. either the spam policy of the group is "open", or policy is "wait" and the group administrator approved it)
Defined in: /web/philip/tcl/user-group-defs.tcl

Source code:



    ns_log Notice "running send_one_group_spam_message"
    
    set db [ns_db gethandle]
    
    # Get information related to this spam from the group_spam_history_table
    
    set selection [ns_db select $db "select *
    from group_spam_history
    where spam_id = $spam_id
    and approved_p = 't'
    and send_date is null"]
    
    if { [ns_db getrow $db $selection] == 0} {
	# no sendable spam 
	ns_db releasehandle $db
	ns_log Notice "send_one_group_spam_message : no spam to send"
	return 
    } 

    set_variables_after_query
 
    set group_name  [database_to_tcl_string $db "
    select group_name
    from user_groups
    where group_id = $group_id"]

    set admin_email  [database_to_tcl_string $db "
    select admin_email
    from user_groups
    where group_id = $group_id"]

    set group_spam_removal_string "[group_spam_removal_blurb $db $group_id]"

    if { [lsearch $send_to "all"] != -1 } {
	set role_clause ""
    } else {
	foreach recipient_role $send_to {
	    append ug_role_clause "ug.role='$recipient_role' "
	}
	set role_clause "and ("
	append role_clause [join $ug_role_clause " or "] )
    }

    set sql_query "select distinct u.email as receiver_email,
                          u.user_id as receiver_id ,
                          u.first_names as receiver_first_names,
                          u.last_name as receiver_last_name
	from user_group_map ug, users_spammable u
	where ug.group_id = $group_id
	$role_clause
	and ug.user_id = u.user_id
	and not exists ( select 1 
	                 from group_member_email_preferences
                         where group_id = $group_id
	                 and user_id =  u.user_id 
                         and dont_spam_me_p = 't')
        and not exists ( select 1 
	                 from user_user_bozo_filter
                         where origin_user_id = u.user_id 
	                 and target_user_id =  $sender_id)"

    set selection_list [database_to_tcl_list_list $db $sql_query]

    # Release the handle while sending mail
    ns_db releasehandle $db 

    # query sets email for each recipient

    regsub -all "\r" $body "" body_stripped

    foreach row $selection_list {
	set receiver_email [lindex $row 0]
	set receiver_id [lindex $row 1]
	set receiver_first_names [lindex $row 2]
	set receiver_last_name [lindex $row 3]

	set message_body $body_stripped

	# This appends group-wide removal blurb
	append message_body $group_spam_removal_string 

	# This appends site-wide bozo-filter blurb,
	# so, the receiver doesn't get any more email from sender
	append message_body "[bozo_filter_blurb $sender_id]"

	# substitute all user/group specific data in the message body

	regsub -all "<first_names>" $message_body $receiver_first_names message_body
	regsub -all "<last_name>" $message_body $receiver_last_name message_body
	regsub -all "<email>" $message_body $receiver_email message_body
	regsub -all "<group_name>" $message_body $group_name message_body
	regsub -all "<admin_email>" $message_body $admin_email message_body

	ns_sendmail $receiver_email $from_address $subject $message_body

	incr n_receivers_actual	
    }

    # Log that the emails went out
    set db [ns_db gethandle subquery]

    ns_db dml $db "update group_spam_history
                   set n_receivers_actual=$n_receivers_actual,
                       send_date = sysdate
                   where spam_id = $spam_id "

    ns_db releasehandle $db
    ns_log Notice "send_one_group_spam_message : finished sending group spam id $spam_id"


philg@mit.edu