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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# src/tools/gmail_mcp_client.py

import base64
import os
from typing import List, Optional, Dict, Any

from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

# If modifying these SCOPES, delete the token file to re-authenticate.
SCOPES = ["https://www.googleapis.com/auth/gmail.modify"]

# OAuth2 credentials from environment variables
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID")
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET")
GOOGLE_REFRESH_TOKEN = os.getenv("GOOGLE_REFRESH_TOKEN")
GOOGLE_ACCESS_TOKEN = os.getenv("GOOGLE_ACCESS_TOKEN")

# Allowed email addresses and domains for security
ALLOWED_SENDERS = ["habib.adoum01@gmail.com", "news@alphasignal.ai"]


def _get_gmail_service():
    """
    Authenticate (or refresh) and return a Gmail API service instance using environment variables.
    """
    try:
        # Check if we have the required environment variables
        if not GOOGLE_CLIENT_ID or not GOOGLE_CLIENT_SECRET:
            raise ValueError(
                "Missing required environment variables: GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET. "
                "Please set these environment variables with your OAuth 2.0 credentials."
            )

        creds = None

        # Try to create credentials from environment variables
        if GOOGLE_ACCESS_TOKEN and GOOGLE_REFRESH_TOKEN:
            print("Creating credentials from environment variables...")
            creds = Credentials(
                token=GOOGLE_ACCESS_TOKEN,
                refresh_token=GOOGLE_REFRESH_TOKEN,
                token_uri="https://oauth2.googleapis.com/token",
                client_id=GOOGLE_CLIENT_ID,
                client_secret=GOOGLE_CLIENT_SECRET,
                scopes=SCOPES,
            )
        elif GOOGLE_REFRESH_TOKEN:
            print("Creating credentials from refresh token...")
            creds = Credentials(
                token=None,
                refresh_token=GOOGLE_REFRESH_TOKEN,
                token_uri="https://oauth2.googleapis.com/token",
                client_id=GOOGLE_CLIENT_ID,
                client_secret=GOOGLE_CLIENT_SECRET,
                scopes=SCOPES,
            )

        # If no valid credentials from environment variables, try to refresh or start OAuth flow
        if not creds or not creds.valid:
            if creds and creds.refresh_token:
                print("Refreshing Gmail credentials using refresh token...")
                try:
                    creds.refresh(Request())
                    print("Gmail credentials refreshed successfully.")
                except Exception as e:
                    print(f"Failed to refresh credentials: {e}")
                    creds = None  # Force OAuth flow

            if not creds or not creds.valid:
                # For deployment environments, we can't run interactive OAuth flow
                # User must provide refresh token via environment variable
                if os.getenv("DEPLOYMENT_ENV") == "production":
                    raise ValueError(
                        "No valid credentials found in production environment. "
                        "Please set GOOGLE_REFRESH_TOKEN environment variable with a valid refresh token. "
                        "Run the setup script locally first to obtain the refresh token."
                    )

                # For local development, create OAuth client info from environment variables
                client_config = {
                    "installed": {
                        "client_id": GOOGLE_CLIENT_ID,
                        "client_secret": GOOGLE_CLIENT_SECRET,
                        "auth_uri": "https://accounts.google.com/o/oauth2/auth",
                        "token_uri": "https://oauth2.googleapis.com/token",
                        "redirect_uris": ["http://localhost:3000"],
                    }
                }

                print("Starting Gmail OAuth flow...")
                flow = InstalledAppFlow.from_client_config(client_config, SCOPES)
                # Use port 3000 to match the redirect URI in your OAuth config
                creds = flow.run_local_server(port=3000, open_browser=True)

                # Print the refresh token for the user to set as environment variable
                print("\n" + "=" * 60)
                print("IMPORTANT: Save this refresh token as an environment variable:")
                print(f"GOOGLE_REFRESH_TOKEN={creds.refresh_token}")
                print("Set this in your .env file or deployment environment.")
                print("=" * 60 + "\n")

        # Build the Gmail API client
        print("Gmail API service initialized successfully.")
        return build("gmail", "v1", credentials=creds)

    except Exception as e:
        print(f"Error initializing Gmail service: {str(e)}")
        raise


def setup_gmail_credentials():
    """
    Helper function to setup Gmail credentials and get the refresh token.
    Run this once locally to get the refresh token for your environment variables.
    """
    print("Setting up Gmail credentials...")
    print("Make sure you have set GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables.")

    if not GOOGLE_CLIENT_ID or not GOOGLE_CLIENT_SECRET:
        print("Error: Missing GOOGLE_CLIENT_ID or GOOGLE_CLIENT_SECRET environment variables.")
        print("\nTo get these values:")
        print("1. Go to https://console.cloud.google.com/apis/credentials")
        print("2. Create OAuth 2.0 Client IDs (Application type: Desktop application)")
        print("3. Download the JSON file and extract client_id and client_secret")
        print("4. Set them as environment variables:")
        print("   export GOOGLE_CLIENT_ID='your_client_id_here'")
        print("   export GOOGLE_CLIENT_SECRET='your_client_secret_here'")
        return None

    client_config = {
        "installed": {
            "client_id": GOOGLE_CLIENT_ID,
            "client_secret": GOOGLE_CLIENT_SECRET,
            "auth_uri": "https://accounts.google.com/o/oauth2/auth",
            "token_uri": "https://oauth2.googleapis.com/token",
            "redirect_uris": ["http://localhost:3000"],
        }
    }

    flow = InstalledAppFlow.from_client_config(client_config, SCOPES)
    creds = flow.run_local_server(port=3000, open_browser=True)

    print("\n" + "=" * 80)
    print("SUCCESS! Gmail credentials obtained.")
    print("\nAdd these to your environment variables (.env file or deployment platform):")
    print(f"GOOGLE_CLIENT_ID={GOOGLE_CLIENT_ID}")
    print(f"GOOGLE_CLIENT_SECRET={GOOGLE_CLIENT_SECRET}")
    print(f"GOOGLE_REFRESH_TOKEN={creds.refresh_token}")
    if creds.token:
        print(f"GOOGLE_ACCESS_TOKEN={creds.token}")
    print("\nFor deployment (like Hugging Face Spaces), you only need:")
    print("- GOOGLE_CLIENT_ID")
    print("- GOOGLE_CLIENT_SECRET")
    print("- GOOGLE_REFRESH_TOKEN")
    print("- DEPLOYMENT_ENV=production")
    print("=" * 80)

    return creds


def _is_allowed_sender(email_dict: Dict[str, Any]) -> bool:
    """
    Check if the email is from an allowed sender.
    """
    try:
        headers = email_dict.get("payload", {}).get("headers", [])
        for header in headers:
            if header.get("name") == "From":
                from_address = header.get("value", "").lower()
                for allowed in ALLOWED_SENDERS:
                    if allowed.lower() in from_address:
                        return True
        return False
    except Exception as e:
        print(f"Error checking sender permissions: {str(e)}")
        return False


def _extract_email_info(email_dict: Dict[str, Any]) -> Dict[str, str]:
    """
    Extract relevant information from an email dictionary with error handling.
    """
    try:
        headers = email_dict.get("payload", {}).get("headers", [])
        subject = ""
        sender = ""
        date = ""

        for header in headers:
            name = header.get("name", "")
            value = header.get("value", "")
            if name == "Subject":
                subject = value
            elif name == "From":
                sender = value
            elif name == "Date":
                date = value

        result = {
            "id": email_dict.get("id", ""),
            "subject": subject,
            "sender": sender,
            "date": date,
            "snippet": email_dict.get("snippet", ""),
        }

        print(f"Extracted email info: {subject} from {sender}")
        return result

    except Exception as e:
        error_msg = f"Error parsing email: {str(e)}"
        print(error_msg)
        return {
            "id": email_dict.get("id", ""),
            "subject": "Error parsing subject",
            "sender": "Error parsing sender",
            "date": "Error parsing date",
            "snippet": error_msg,
        }


def get_recent_emails(max_results: int = 10) -> List[Dict[str, str]]:
    """
    Get the most recent emails from allowed senders only.

    Args:
        max_results: Maximum number of emails to return (default 10, max 50).

    Returns:
        List of email dictionaries with id, subject, sender, date, and snippet.
        Returns empty list if no emails found or on error.
    """
    try:
        # Limit max_results to prevent token overflow
        max_results = min(max_results, 50)
        print(f"Fetching up to {max_results} recent emails from allowed senders...")

        service = _get_gmail_service()

        # Build query for allowed senders
        query_parts = []
        for sender in ALLOWED_SENDERS:
            query_parts.append(f"from:{sender}")
        query = " OR ".join(query_parts)

        print(f"Using Gmail query: {query}")

        # Get message list
        response = service.users().messages().list(userId="me", q=query, maxResults=max_results).execute()

        messages = response.get("messages", [])
        print(f"Found {len(messages)} messages from allowed senders.")

        if not messages:
            print("No emails found from allowed senders.")
            return []

        email_list = []

        for i, message in enumerate(messages):
            try:
                print(f"Processing email {i + 1}/{len(messages)}: {message['id']}")

                # Get message metadata only to save tokens
                msg = service.users().messages().get(userId="me", id=message["id"], format="metadata").execute()

                # Extract and filter
                if _is_allowed_sender(msg):
                    email_info = _extract_email_info(msg)
                    email_list.append(email_info)
                else:
                    print(f"Email {message['id']} not from allowed sender, skipping.")

            except Exception as e:
                print(f"Error processing message {message.get('id', 'unknown')}: {str(e)}")
                continue

        print(f"Successfully processed {len(email_list)} emails.")
        return email_list

    except Exception as e:
        error_msg = f"Failed to get recent emails: {str(e)}"
        print(error_msg)
        return [{"error": error_msg}]


def search_emails_simple(query: str, max_results: int = 10) -> List[Dict[str, str]]:
    """
    Search for emails with a simple query, automatically filtered to allowed senders.

    Args:
        query: Simple search term (e.g. "AI", "newsletter"). Complex Gmail operators not needed.
        max_results: Maximum number of emails to return (default 10, max 50).

    Returns:
        List of email dictionaries with basic info, or empty list if none found.
    """
    try:
        max_results = min(max_results, 50)
        print(f"Searching for emails containing '{query}' from allowed senders...")

        service = _get_gmail_service()

        # Build query with sender filtering
        sender_filter = " OR ".join([f"from:{sender}" for sender in ALLOWED_SENDERS])
        full_query = f"({sender_filter}) AND ({query})"

        print(f"Using Gmail search query: {full_query}")

        response = service.users().messages().list(userId="me", q=full_query, maxResults=max_results).execute()

        messages = response.get("messages", [])
        result_count = response.get("resultSizeEstimate", 0)

        print(f"Search found {result_count} total results, processing {len(messages)} messages...")

        if not messages:
            print("No emails found matching the search criteria.")
            return []

        email_list = []

        for i, message in enumerate(messages):
            try:
                print(f"Processing search result {i + 1}/{len(messages)}: {message['id']}")

                msg = service.users().messages().get(userId="me", id=message["id"], format="metadata").execute()

                if _is_allowed_sender(msg):
                    email_info = _extract_email_info(msg)
                    email_list.append(email_info)

            except Exception as e:
                print(f"Error processing search result {message.get('id', 'unknown')}: {str(e)}")
                continue

        print(f"Search completed: found {len(email_list)} relevant emails.")
        return email_list

    except Exception as e:
        error_msg = f"Email search failed: {str(e)}"
        print(error_msg)
        return [{"error": error_msg}]


def read_email_content(message_id: str) -> Dict[str, Any]:
    """
    Read the full content of a specific email by ID, with security filtering.

    Args:
        message_id: The Gmail message ID (from search or recent emails list).

    Returns:
        Dictionary with email content including subject, sender, date, and body.
        Returns error dict if email not accessible or not from allowed sender.
    """
    try:
        print(f"Reading email content for message ID: {message_id}")

        service = _get_gmail_service()
        message = service.users().messages().get(userId="me", id=message_id, format="full").execute()

        # Security check: only return emails from allowed senders
        if not _is_allowed_sender(message):
            error_msg = f"Access denied: Email {message_id} not from allowed sender"
            print(error_msg)
            return {"error": error_msg, "allowed_senders": ALLOWED_SENDERS}

        # Extract basic info
        email_info = _extract_email_info(message)

        # Try to extract body content
        body_text = ""
        try:
            payload = message.get("payload", {})
            if "parts" in payload:
                # Multipart message
                for part in payload["parts"]:
                    if part.get("mimeType") == "text/plain":
                        body_data = part.get("body", {}).get("data", "")
                        if body_data:
                            body_text = base64.urlsafe_b64decode(body_data).decode("utf-8")
                            break
            else:
                # Simple message
                body_data = payload.get("body", {}).get("data", "")
                if body_data:
                    body_text = base64.urlsafe_b64decode(body_data).decode("utf-8")

        except Exception as e:
            print(f"Could not extract email body: {str(e)}")
            body_text = "Error extracting email body"

        # Limit body text to prevent token overflow
        if len(body_text) > 2000:
            body_text = body_text[:2000] + "... [content truncated]"
            print("Email body truncated to prevent token overflow")

        result = {**email_info, "body": body_text}

        print(f"Successfully read email: {email_info['subject']}")
        return result

    except Exception as e:
        error_msg = f"Failed to read email {message_id}: {str(e)}"
        print(error_msg)
        return {"error": error_msg}


# Legacy functions for backward compatibility
def send_email(
    to: List[str],
    subject: str,
    body: str,
    html_body: Optional[str] = None,
    cc: Optional[List[str]] = None,
    bcc: Optional[List[str]] = None,
) -> Dict[str, Any]:
    """Send email - currently not implemented for security"""
    return {"error": "Email sending not implemented for security reasons"}


def search_emails(query: str, max_results: int = 10) -> Dict[str, Any]:
    """Legacy search function - use search_emails_simple instead"""
    results = search_emails_simple(query, max_results)
    return {"messages": [{"id": r["id"]} for r in results if "id" in r], "resultSizeEstimate": len(results)}


def read_email(message_id: str, format: str = "full") -> Dict[str, Any]:
    """Legacy read function - use read_email_content instead"""
    return read_email_content(message_id)