Skip to content

Commit

Permalink
Finalize automatic signature creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Gijsdeman committed Aug 20, 2023
1 parent 1765799 commit 51926af
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ const config: ContainerConfig = {

let LDAPConnector: Client;

export async function getLDAPDisplayName(email: string) : Promise<string> {
const LDAPUsers = (await LDAPConnector.search(config.LDAP_BASE_DN, {
scope: 'sub',
filter: `(&(objectClass=user)(objectCategory=person)(mail=${email})`,
attributes: ['displayName'],
})).searchEntries as unknown as LDAPResults[];

return LDAPUsers[0].displayName;
}

async function getUserMails(users: string[], skipEntry: LDAPResults): Promise<string[]> {
const result = [];
for (const user of users) {
Expand Down
21 changes: 14 additions & 7 deletions src/mailcowDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { DataSource, Repository } from 'typeorm';
import { ContainerConfig, Defaults, SOGoMailIdentity } from './types';
import { SogoUserProfile } from './entities/SogoUserProfile';
import { Users } from './entities/User';
import { getLDAPDisplayName } from './index';
import axios from 'axios';

// Connection options for the DB
let dataSource : DataSource;
let SogoUserProfileRepository: Repository<SogoUserProfile>;


/**
* Initialize database connection. Setup database if it does not yet exist
*/
Expand All @@ -33,7 +34,7 @@ export async function initializeMailcowDB(config: ContainerConfig): Promise<void
}

export async function editUserSignature(user: Users, SOBs: string[]): Promise<void> {
if (user.email !== '[email protected]') return;
if (user.email !== '[email protected]' && user.email !== '[email protected]') return;
console.log(`Changing signatures for ${user.email}`);

let profile = await SogoUserProfileRepository.findOneOrFail({
Expand All @@ -46,25 +47,31 @@ export async function editUserSignature(user: Users, SOBs: string[]): Promise<vo
let newIdentities : SOGoMailIdentity[] = [];

for (let identity of cDefaults.SOGoMailIdentities) {
if (!identity.isAutoGenerated) {
if (identity.signature.indexOf('class="autogenerated"') === -1) {
newIdentities.push(identity);
}
}

for (let identityMail of SOBs) {
const committeeDisplayName = await getLDAPDisplayName(identityMail);
let signature : string = (await axios.get(`https://signature.gewis.nl/${identityMail}`)).data;
signature = signature.replace('{{displayName}}', user.displayName);
signature = signature.replaceAll('{{committeeDisplayName}}', committeeDisplayName);
signature = signature.replaceAll('{{identityMail}}', identityMail);

signature.replaceAll('', '');

let newIdentity : SOGoMailIdentity = {
email: identityMail,
isAutoGenerated: true,
fullName: `${user.displayName}`,
fullName: `${user.displayName} | ${committeeDisplayName}`,
replyTo: user.email,
signature: 'SIGNATURE FOR',
signature: `${signature}`,
};
newIdentities.push(newIdentity);
}

cDefaults.SOGoMailIdentities = newIdentities;
profile.c_defaults = JSON.stringify(cDefaults);
console.log(cDefaults.SOGoMailIdentities);

await SogoUserProfileRepository.update(user.email, profile);
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"lib": [
"es6",
"es2020",
"DOM"
"DOM",
"ES2021.String"
],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.tsbuildinfo

Large diffs are not rendered by default.

0 comments on commit 51926af

Please sign in to comment.