# ================================================================== # Gossamer Community # # Website : http://gossamer-threads.com/ # Support : http://gossamer-threads.com/scripts/support/ # CVS Info : # Revision : $Id: GMail.pm,v 1.32 2003/05/15 18:18:12 aki Exp $ # # Copyright (c) 2002 Gossamer Threads Inc. All Rights Reserved. # Redistribution in part or in whole strictly prohibited. Please # see LICENSE file for full details. # ================================================================== package Community::Apps::Local::GMail; # =================================================================== use strict; use Community qw/ comm_language comm_debug comm_fatal comm_config /; sub install { # ------------------------------------------------------------------- # Install is called whenever a new application is setup. It passes in the # comm_app table row. If your application does not need to add any extra # custom columns, you can just return 1 safely. # my $app = shift; if ($app->{app_local}) { unless (-e "$app->{app_location}/GMail.pm") { comm_debug("Error installing application, can't find GMail.pm at: $app->{app_location}/GMail.pm"); $Community::error = comm_language( 'ADMIN_APPS_INSTALL_NOTFOUND', 'Gossamer Mail', $app->{app_location} ); return; } } _setup($app) or return; # Add a custom column to store the gossamer mail userid. my $col = 'app_' . $app->{app_keyword} . '_userid'; my $editor = $Community::DB->editor('comm_users'); comm_debug("creating new column '$col' to store gmail userid") if ($Community::CFG->{debug}); my $res = $editor->add_col($col, { type => 'INT', not_null => 0, form_display => comm_language( 'COL_DISPLAY_USER_ID', 'GMAIL' ) }); if (! $res) { comm_debug("adding column failed: $GT::SQL::error") if ($Community::CFG->{debug}); $Community::error = comm_language( 'ADMIN_APPS_INSTALL_SQL_COLUMNINSERT', $col, $GT::SQL::error ); return; } return 1; } sub uninstall { # ------------------------------------------------------------------- # Uninstall is called whenever an application is removed. It passes in the # comm_app table row. Your application should remove any changes it added # during installation, or just return 1 otherwise. # my $app = shift; # Drop the custom column. my $col = 'app_' . $app->{app_keyword} . '_userid'; my $editor = $Community::DB->editor('comm_users'); comm_debug("dropping custom column '$col' that stores gmail userid") if ($Community::CFG->{debug}); my $res = $editor->drop_col($col); if (! $res) { comm_debug("dropping custom column failed: $GT::SQL::error") if ($Community::CFG->{debug}); $Community::error = comm_language( 'ADMIN_APPS_INSTALL_SQL_COLUMNDROP', $col, $GT::SQL::error ); return; } return 1; } sub insert { # ------------------------------------------------------------------- # Insert is called whenever a new user is created. You should add the # user to your application, and return 1 on success, 0 on error and have # the error message stored in $Community::error. # my ($app, $user) = @_; _setup($app) or return; my $email = $user->{"app_$app->{app_keyword}_email"}; if (! $email) { $email = $user->{"app_$app->{app_keyword}_email_username"} . '@' . $user->{"app_$app->{app_keyword}_email_domain"}; $user->{"app_$app->{app_keyword}_email"} = $email; } if ($email !~ m,\A[\w\-.]+\@[\w\-.]+\Z,) { $Community::error = comm_language( 'ADMIN_APPS_INSTALL_GMAIL_INVEMAIL', $email ); return; } if ($GMail::DB->table('users')->count( { email => $email })) { $Community::error = comm_language( 'ADMIN_APPS_INSTALL_GMAIL_EMAILEXISTS', $email ); return; } comm_debug("inserting user: |$user->{comm_username}| -> |$email|") if ($Community::CFG->{debug}); # Gossamer Mail needs a password in order to deliver mail. my $user_ins = { email => $email, password => $user->{comm_password}, users_status => 'Validated', users_space_allowed => $GMail::CFG->{defaults}->{space_allowed}, users_join_time => time, }; my $dgraph_ins = { dgraph_email => $user->{comm_email}, dgraph_first_name => $user->{prof_first_name}, dgraph_last_name => $user->{prof_last_name} }; my $id = $GMail::DB->table('users')->insert($user_ins)->insert_id; if (! $id) { $Community::error = comm_language( 'ADMIN_APPS_USERINSERT', $email, $GT::SQL::error ); return; } $dgraph_ins->{dgraph_userid} = $id; my $res = $GMail::DB->table('dgraph')->insert($dgraph_ins); if (! $res) { $Community::error = comm_language( 'ADMIN_APPS_USERINSERT', $email, $GT::SQL::error ); $GMail::DB->table('users')->delete( { userid => $id } ); return; } $user->{ "app_$app->{app_keyword}_userid" } = $id; comm_debug("success, created gossamer mail userid |$id|") if ($Community::CFG->{debug}); return 1; } sub delete { # ------------------------------------------------------------------- # Delete is called whenever a user is removed from Community. Your application # should remove the user from it's tables. # my ($app, $user) = @_; _setup($app) or return; my $userid = $user->{"app_$app->{app_keyword}_userid"}; unless (defined $userid and length $userid) { comm_debug("user does not have an email address defined") if ($Community::CFG->{debug}); return 1; } if ( $GMail::DB->table('dgraph', 'users')->count( { userid => $userid } )) { comm_debug("removing user |$userid|") if ($Community::CFG->{debug}); my $rows = $GMail::DB->table('dgraph', 'users')->delete( { userid => $userid } ); comm_debug("success, $rows deleted") if ($Community::CFG->{debug}); } return 1; } sub update { # ------------------------------------------------------------------- # Update is called whenever a community user information is changed. # my ($app, $user) = @_; _setup($app) or return; my $email = $user->{"app_$app->{app_keyword}_email"}; my $userid = $user->{"app_$app->{app_keyword}_userid"}; my $res = $GMail::DB->table('dgraph')->update( { dgraph_email => $user->{comm_email} }, { dgraph_userid => $userid } ); unless ( $email ) { my $email_username = $user->{"app_$app->{app_keyword}_email_username"}; my $email_domain = $user->{"app_$app->{app_keyword}_email_domain"}; if ( $email_username and $email_domain ) { $email = $email_username . '@' . $email_domain; $user->{"app_$app->{app_keyword}_email"} = $email; } elsif ( int ( $userid || 0 ) ) { return 1; } } if ($email !~ m,\A[\w\-.]+\@[\w\-.]+\Z,) { $Community::error = comm_language( 'ADMIN_APPS_INSTALL_GMAIL_INVEMAIL', $email ); return; } my ($orig_email) = $GMail::DB->table('users')->select( 'email', { userid => $userid })->fetchrow_array; $GMail::DB->table('users')->update( { email => $email }, { userid => $userid }); $GMail::DB->table('session')->update( { session_user_id => $email }, { session_user_id => $orig_email }); comm_debug("updated gossamer mail userid |$userid| to email |$email|") if ($Community::CFG->{debug}); return 1; } sub logout { # ------------------------------------------------------------------- # Logout is called whenever a logout is called from Community. If you are # managing your own sessions, you should remove the sessions for that user. If you # are using Community to manage sessions, no action is needed. # my ($app, $user) = @_; _setup($app) or return; my $userid = $user->{'app_' . $app->{app_keyword} . '_userid'}; my ($email) = $GMail::DB->table('users')->select('email', { userid => $userid })->fetchrow_array; my $res = $GMail::DB->table('session')->delete( { session_user_id => $email }); comm_debug("Removed $res sessions for user |$userid| with email |$email|") if ($Community::CFG->{debug}); return 1; } sub signup_form { # ------------------------------------------------------------------- # Signup form is called whenever the user signup or admin add user forms # are displayed. You should display whatever form fields you need in order # to get any extra information. If you don't have extra columns to deal with, # just return an empty string. # my ($app, $opts) = @_; _setup($app) or return $Community::error; $opts->{tr} ||= 'class="body"'; $opts->{td_l} ||= 'class="body" width="40%" align="right"'; $opts->{td_r} ||= 'class="body" align="left"'; my $disp_name = $opts->{tags}->{$app->{app_keyword} . '_column'} || comm_language( 'COL_DISPLAY_WEBMAIL_ADDRESS' ); my $userid = $opts->{tags}->{'app_' . $app->{app_keyword} . '_userid'}; my $col = 'app_' . $app->{app_keyword} . '_email'; my $username = $opts->{tags}->{$col . '_username'} || ''; my $domain = $opts->{tags}->{$col . '_domain'} || ''; if (! $username or ! $domain) { my $email = $opts->{tags}->{$col}; ($username, $domain) = $email =~ /(.*)\@(.*)/; } if ($userid and (!$username or !$domain)) { my ($email) = $GMail::DB->table('users')->select('email', { userid => $userid })->fetchrow_array; ($username, $domain) = $email =~ /(.*)\@(.*)/; } my $domain_list = GMail->domains($domain); $$domain_list =~ s/"domain"/${col}_domain/; my $html = qq~ {tr}> {td_l}>$disp_name {td_r}> @ $$domain_list ~; return $html; } sub prototype_template_form { # ------------------------------------------------------------------- # The prototype form is called when the admin wishes to bulk # activate a large number of users via the application permissions # screen. This function should return the html that displays the # template which will be used to template how the system will initialize # a user should it be required to insert them into the local application # database. If there are no special fields required, simply return # an empty string. # Name all your custom fields with the prefix "proto_". That way # the system will be able to detect which fields you wish to have # automatically parsed. The system will parse and remove the "proto_" # prefix before passing the arguments into "sub insert" # my ($app, $opts) = @_; _setup($app) or return $Community::error; $opts->{tr} ||= 'class="body"'; $opts->{td_l} ||= 'class="body" width="40%" align="right"'; $opts->{td_r} ||= 'class="body" align="left"'; my $disp_name = $opts->{tags}->{$app->{app_keyword} . '_column'} || comm_language( 'COL_DISPLAY_WEBMAIL_ADDRESS' ); my $col = 'app_' . $app->{app_keyword} . '_email'; my $domain_list = GMail->domains; $$domain_list =~ s/"domain"/proto_${col}_domain/; my $html = qq~ {tr}> {td_l}>$disp_name {td_r}> @ $$domain_list ~; return $html; } sub _setup { # ------------------------------------------------------------------- my $app = shift; @INC = (grep($_ ne $app->{app_location}, @INC), $app->{app_location}); # Must push, see bug 199 my $res = eval { require GMail; GMail->init($app->{app_location}); 1; }; if (! $res) { $Community::error = comm_language( 'ADMIN_APPS_INSTALL_LOADFAIL', 'Gossamer Mail', $@ ); comm_debug("unable to load Gossamer Mail installation at: $app->{app_location}. Reason: $@") if ($Community::CFG->{debug}); return; } # Check to ensure some domains have been configured for GMail unless ( GMail->domain_list ) { $Community::error = comm_language( 'ADMIN_APPS_INSTALL_NOTCONFIGURED', 'Gossamer Mail' ); comm_debug("unable to load Gossamer Mail installation since no domains were found.") if ($Community::CFG->{debug}); return; } return 1; } 1; __END__ =head1 NAME Community::Apps::Local::GMail - Gossamer Mail interface =head1 DESCRIPTION This module handles creating/deleting users in Gossamer Mail. =head1 MAINTAINER Alex Krohn =head1 COPYRIGHT Copyright (c) 2002 Gossamer Threads Inc. All Rights Reserved. http://www.gossamer-threads.com/ =head1 VERSION Revision: $Id: GMail.pm,v 1.32 2003/05/15 18:18:12 aki Exp $ =cut